smbios: Move smbios_write_type to the C file
[platform/kernel/u-boot.git] / lib / smbios.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * Adapted from coreboot src/arch/x86/smbios.c
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <env.h>
11 #include <mapmem.h>
12 #include <smbios.h>
13 #include <tables_csum.h>
14 #include <version.h>
15 #ifdef CONFIG_CPU
16 #include <cpu.h>
17 #include <dm/uclass-internal.h>
18 #endif
19
20 /**
21  * Function prototype to write a specific type of SMBIOS structure
22  *
23  * @addr:       start address to write the structure
24  * @handle:     the structure's handle, a unique 16-bit number
25  * @node:       node containing the information to write (ofnode_null() if none)
26  * @return:     size of the structure
27  */
28 typedef int (*smbios_write_type)(ulong *addr, int handle, ofnode node);
29
30 /**
31  * struct smbios_write_method - Information about a table-writing function
32  *
33  * @write: Function to call
34  * @subnode_name: Name of subnode which has the information for this function,
35  *      NULL if none
36  */
37 struct smbios_write_method {
38         smbios_write_type write;
39         const char *subnode_name;
40 };
41
42 /**
43  * smbios_add_string() - add a string to the string area
44  *
45  * This adds a string to the string area which is appended directly after
46  * the formatted portion of an SMBIOS structure.
47  *
48  * @start:      string area start address
49  * @str:        string to add
50  * @return:     string number in the string area (1 or more)
51  */
52 static int smbios_add_string(char *start, const char *str)
53 {
54         int i = 1;
55         char *p = start;
56         if (!*str)
57                 str = "Unknown";
58
59         for (;;) {
60                 if (!*p) {
61                         strcpy(p, str);
62                         p += strlen(str);
63                         *p++ = '\0';
64                         *p++ = '\0';
65
66                         return i;
67                 }
68
69                 if (!strcmp(p, str))
70                         return i;
71
72                 p += strlen(p) + 1;
73                 i++;
74         }
75 }
76
77 /**
78  * smbios_add_prop() - Add a property from the device tree
79  *
80  * @start:      string area start address
81  * @node:       node containing the information to write (ofnode_null() if none)
82  * @prop:       property to write
83  * @return 0 if not found, else SMBIOS string number (1 or more)
84  */
85 static int smbios_add_prop(char *start, ofnode node, const char *prop)
86 {
87
88         if (IS_ENABLED(CONFIG_OF_CONTROL)) {
89                 const char *str;
90
91                 str = ofnode_read_string(node, prop);
92                 if (str)
93                         return smbios_add_string(start, str);
94         }
95
96         return 0;
97 }
98
99 /**
100  * smbios_string_table_len() - compute the string area size
101  *
102  * This computes the size of the string area including the string terminator.
103  *
104  * @start:      string area start address
105  * @return:     string area size
106  */
107 static int smbios_string_table_len(char *start)
108 {
109         char *p = start;
110         int i, len = 0;
111
112         while (*p) {
113                 i = strlen(p) + 1;
114                 p += i;
115                 len += i;
116         }
117
118         return len + 1;
119 }
120
121 static int smbios_write_type0(ulong *current, int handle, ofnode node)
122 {
123         struct smbios_type0 *t;
124         int len = sizeof(struct smbios_type0);
125
126         t = map_sysmem(*current, len);
127         memset(t, 0, sizeof(struct smbios_type0));
128         fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
129         t->vendor = smbios_add_string(t->eos, "U-Boot");
130         t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
131         t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
132 #ifdef CONFIG_ROM_SIZE
133         t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
134 #endif
135         t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
136                                   BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
137                                   BIOS_CHARACTERISTICS_UPGRADEABLE;
138 #ifdef CONFIG_GENERATE_ACPI_TABLE
139         t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
140 #endif
141 #ifdef CONFIG_EFI_LOADER
142         t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
143 #endif
144         t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
145
146         t->bios_major_release = 0xff;
147         t->bios_minor_release = 0xff;
148         t->ec_major_release = 0xff;
149         t->ec_minor_release = 0xff;
150
151         len = t->length + smbios_string_table_len(t->eos);
152         *current += len;
153         unmap_sysmem(t);
154
155         return len;
156 }
157
158 static int smbios_write_type1(ulong *current, int handle, ofnode node)
159 {
160         struct smbios_type1 *t;
161         int len = sizeof(struct smbios_type1);
162         char *serial_str = env_get("serial#");
163
164         t = map_sysmem(*current, len);
165         memset(t, 0, sizeof(struct smbios_type1));
166         fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
167         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
168         t->product_name = smbios_add_prop(t->eos, node, "product");
169         t->version = smbios_add_prop(t->eos, node, "version");
170         if (serial_str) {
171                 t->serial_number = smbios_add_string(t->eos, serial_str);
172                 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
173         } else {
174                 t->serial_number = smbios_add_prop(t->eos, node, "serial");
175         }
176         t->sku_number = smbios_add_prop(t->eos, node, "sku");
177         t->family = smbios_add_prop(t->eos, node, "family");
178
179         len = t->length + smbios_string_table_len(t->eos);
180         *current += len;
181         unmap_sysmem(t);
182
183         return len;
184 }
185
186 static int smbios_write_type2(ulong *current, int handle, ofnode node)
187 {
188         struct smbios_type2 *t;
189         int len = sizeof(struct smbios_type2);
190
191         t = map_sysmem(*current, len);
192         memset(t, 0, sizeof(struct smbios_type2));
193         fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
194         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
195         t->product_name = smbios_add_prop(t->eos, node, "product");
196         t->asset_tag_number = smbios_add_prop(t->eos, node, "asset-tag");
197         t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
198         t->board_type = SMBIOS_BOARD_MOTHERBOARD;
199
200         len = t->length + smbios_string_table_len(t->eos);
201         *current += len;
202         unmap_sysmem(t);
203
204         return len;
205 }
206
207 static int smbios_write_type3(ulong *current, int handle, ofnode node)
208 {
209         struct smbios_type3 *t;
210         int len = sizeof(struct smbios_type3);
211
212         t = map_sysmem(*current, len);
213         memset(t, 0, sizeof(struct smbios_type3));
214         fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
215         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
216         t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
217         t->bootup_state = SMBIOS_STATE_SAFE;
218         t->power_supply_state = SMBIOS_STATE_SAFE;
219         t->thermal_state = SMBIOS_STATE_SAFE;
220         t->security_status = SMBIOS_SECURITY_NONE;
221
222         len = t->length + smbios_string_table_len(t->eos);
223         *current += len;
224         unmap_sysmem(t);
225
226         return len;
227 }
228
229 static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
230 {
231         u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
232         const char *vendor = "Unknown";
233         const char *name = "Unknown";
234
235 #ifdef CONFIG_CPU
236         char processor_name[49];
237         char vendor_name[49];
238         struct udevice *cpu = NULL;
239
240         uclass_find_first_device(UCLASS_CPU, &cpu);
241         if (cpu) {
242                 struct cpu_plat *plat = dev_get_parent_plat(cpu);
243
244                 if (plat->family)
245                         processor_family = plat->family;
246                 t->processor_id[0] = plat->id[0];
247                 t->processor_id[1] = plat->id[1];
248
249                 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
250                         vendor = vendor_name;
251                 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
252                         name = processor_name;
253         }
254 #endif
255
256         t->processor_family = processor_family;
257         t->processor_manufacturer = smbios_add_string(t->eos, vendor);
258         t->processor_version = smbios_add_string(t->eos, name);
259 }
260
261 static int smbios_write_type4(ulong *current, int handle, ofnode node)
262 {
263         struct smbios_type4 *t;
264         int len = sizeof(struct smbios_type4);
265
266         t = map_sysmem(*current, len);
267         memset(t, 0, sizeof(struct smbios_type4));
268         fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
269         t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
270         smbios_write_type4_dm(t, node);
271         t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
272         t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
273         t->l1_cache_handle = 0xffff;
274         t->l2_cache_handle = 0xffff;
275         t->l3_cache_handle = 0xffff;
276         t->processor_family2 = t->processor_family;
277
278         len = t->length + smbios_string_table_len(t->eos);
279         *current += len;
280         unmap_sysmem(t);
281
282         return len;
283 }
284
285 static int smbios_write_type32(ulong *current, int handle, ofnode node)
286 {
287         struct smbios_type32 *t;
288         int len = sizeof(struct smbios_type32);
289
290         t = map_sysmem(*current, len);
291         memset(t, 0, sizeof(struct smbios_type32));
292         fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
293
294         *current += len;
295         unmap_sysmem(t);
296
297         return len;
298 }
299
300 static int smbios_write_type127(ulong *current, int handle, ofnode node)
301 {
302         struct smbios_type127 *t;
303         int len = sizeof(struct smbios_type127);
304
305         t = map_sysmem(*current, len);
306         memset(t, 0, sizeof(struct smbios_type127));
307         fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
308
309         *current += len;
310         unmap_sysmem(t);
311
312         return len;
313 }
314
315 static struct smbios_write_method smbios_write_funcs[] = {
316         { smbios_write_type0, },
317         { smbios_write_type1, "system", },
318         { smbios_write_type2, "baseboard", },
319         { smbios_write_type3, "chassis", },
320         { smbios_write_type4, },
321         { smbios_write_type32, },
322         { smbios_write_type127 },
323 };
324
325 ulong write_smbios_table(ulong addr)
326 {
327         ofnode parent_node = ofnode_null();
328         struct smbios_entry *se;
329         struct udevice *dev;
330         ulong table_addr;
331         ulong tables;
332         int len = 0;
333         int max_struct_size = 0;
334         int handle = 0;
335         char *istart;
336         int isize;
337         int i;
338
339         if (IS_ENABLED(CONFIG_OF_CONTROL)) {
340                 uclass_first_device(UCLASS_SYSINFO, &dev);
341                 if (dev)
342                         parent_node = dev_read_subnode(dev, "smbios");
343         }
344
345         /* 16 byte align the table address */
346         addr = ALIGN(addr, 16);
347
348         se = map_sysmem(addr, sizeof(struct smbios_entry));
349         memset(se, 0, sizeof(struct smbios_entry));
350
351         addr += sizeof(struct smbios_entry);
352         addr = ALIGN(addr, 16);
353         tables = addr;
354
355         /* populate minimum required tables */
356         for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
357                 const struct smbios_write_method *method;
358                 ofnode node = ofnode_null();
359                 int tmp;
360
361                 method = &smbios_write_funcs[i];
362                 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
363                         node = ofnode_find_subnode(parent_node,
364                                                    method->subnode_name);
365                 tmp = method->write((ulong *)&addr, handle++, node);
366
367                 max_struct_size = max(max_struct_size, tmp);
368                 len += tmp;
369         }
370
371         memcpy(se->anchor, "_SM_", 4);
372         se->length = sizeof(struct smbios_entry);
373         se->major_ver = SMBIOS_MAJOR_VER;
374         se->minor_ver = SMBIOS_MINOR_VER;
375         se->max_struct_size = max_struct_size;
376         memcpy(se->intermediate_anchor, "_DMI_", 5);
377         se->struct_table_length = len;
378
379         /*
380          * We must use a pointer here so things work correctly on sandbox. The
381          * user of this table is not aware of the mapping of addresses to
382          * sandbox's DRAM buffer.
383          */
384         table_addr = (ulong)map_sysmem(tables, 0);
385         if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
386                 /*
387                  * We need to put this >32-bit pointer into the table but the
388                  * field is only 32 bits wide.
389                  */
390                 printf("WARNING: SMBIOS table_address overflow %llx\n",
391                        (unsigned long long)table_addr);
392                 table_addr = 0;
393         }
394         se->struct_table_address = table_addr;
395
396         se->struct_count = handle;
397
398         /* calculate checksums */
399         istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
400         isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
401         se->intermediate_checksum = table_compute_checksum(istart, isize);
402         se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
403         unmap_sysmem(se);
404
405         return addr;
406 }