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