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