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