1 // SPDX-License-Identifier: GPL-2.0+
5 * Adapted from coreboot src/arch/x86/smbios.c
8 #define LOG_CATEGORY LOGC_BOARD
10 #include <display_options.h>
13 #include <linux/stringify.h>
14 #include <linux/string.h>
18 #include <tables_csum.h>
21 #include <dm/ofnode.h>
24 #include <dm/uclass-internal.h>
26 #include <linux/sizes.h>
28 /* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
29 #if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
30 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
31 #error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
35 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
36 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
37 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
38 * BIOS Release Date should contain date when code was released
39 * and not when it was built or compiled.
41 #if U_BOOT_VERSION_NUM_PATCH < 10
42 #define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
44 #define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
46 #define U_BOOT_DMI_DAY "01"
47 #define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
48 #define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
50 DECLARE_GLOBAL_DATA_PTR;
53 * struct map_sysinfo - Mapping of sysinfo strings to DT
55 * @si_str: sysinfo string
57 * @max: Max index of the tokenized string to pick. Counting starts from 0
67 static const struct map_sysinfo sysinfo_to_dt[] = {
68 { .si_node = "system", .si_str = "product", .dt_str = "model", 2 },
69 { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 },
70 { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 },
71 { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 },
75 * struct smbios_ctx - context for writing SMBIOS tables
77 * @node: node containing the information to write (ofnode_null()
79 * @dev: sysinfo device to use (NULL if none)
80 * @subnode_name: sysinfo subnode_name. Used for DT fallback
81 * @eos: end-of-string pointer for the table being processed.
82 * This is set up when we start processing a table
83 * @next_ptr: pointer to the start of the next string to be added.
84 * When the table is not empty, this points to the byte
85 * after the \0 of the previous string.
86 * @last_str: points to the last string that was written to the table,
92 const char *subnode_name;
99 * Function prototype to write a specific type of SMBIOS structure
101 * @addr: start address to write the structure
102 * @handle: the structure's handle, a unique 16-bit number
103 * @ctx: context for writing the tables
104 * Return: size of the structure
106 typedef int (*smbios_write_type)(ulong *addr, int handle,
107 struct smbios_ctx *ctx);
110 * struct smbios_write_method - Information about a table-writing function
112 * @write: Function to call
113 * @subnode_name: Name of subnode which has the information for this function,
116 struct smbios_write_method {
117 smbios_write_type write;
118 const char *subnode_name;
121 static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
125 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
126 if (node && !strcmp(node, sysinfo_to_dt[i].si_node) &&
127 !strcmp(si, sysinfo_to_dt[i].si_str))
128 return &sysinfo_to_dt[i];
135 * smbios_add_string() - add a string to the string area
137 * This adds a string to the string area which is appended directly after
138 * the formatted portion of an SMBIOS structure.
140 * @ctx: SMBIOS context
141 * @str: string to add
142 * Return: string number in the string area. 0 if str is NULL.
144 static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
164 if (!strcmp(p, str)) {
175 * get_str_from_dt - Get a substring from a DT property.
176 * After finding the property in the DT, the function
177 * will parse comma-separated values and return the value.
178 * If nprop->max exceeds the number of comma-separated
179 * elements, the last non NULL value will be returned.
180 * Counting starts from zero.
182 * @nprop: sysinfo property to use
183 * @str: pointer to fill with data
184 * @size: str buffer length
187 void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
193 memset(str, 0, size);
194 if (!nprop || !nprop->max)
197 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
201 memcpy(str, dt_str, size);
202 token = strtok(str, ",");
203 while (token && cnt < nprop->max) {
204 strlcpy(str, token, strlen(token) + 1);
205 token = strtok(NULL, ",");
211 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
213 * Sysinfo is used if available, with a fallback to devicetree
215 * @ctx: context for writing the tables
216 * @prop: property to write
217 * @sysinfo_id: unique identifier for the string value to be read
218 * @dval: Default value to use if the string is not found or is empty
219 * Return: 0 if not found, else SMBIOS string number (1 or more)
221 static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
222 int sysinfo_id, const char *dval)
230 return smbios_add_string(ctx, dval);
232 if (sysinfo_id && ctx->dev) {
233 char val[SMBIOS_STR_MAX];
235 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
237 return smbios_add_string(ctx, val);
239 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
240 const char *str = NULL;
241 char str_dt[128] = { 0 };
243 * If the node is not valid fallback and try the entire DT
244 * so we can at least fill in manufacturer and board type
246 if (ofnode_valid(ctx->node)) {
247 str = ofnode_read_string(ctx->node, prop);
249 const struct map_sysinfo *nprop;
251 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
252 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
253 str = (const char *)str_dt;
256 ret = smbios_add_string(ctx, str && *str ? str : dval);
264 * smbios_add_prop() - Add a property from the devicetree
266 * @prop: property to write. The default string will be written if
268 * @dval: Default value to use if the string is not found or is empty
269 * Return: 0 if not found, else SMBIOS string number (1 or more)
271 static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
274 return smbios_add_prop_si(ctx, prop, SYSID_NONE, dval);
277 static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
281 ctx->last_str = NULL;
284 int smbios_update_version(const char *version)
286 char *ptr = gd->smbios_version;
290 return log_ret(-ENOENT);
293 * This string is supposed to have at least enough bytes and is
294 * padded with spaces. Update it, taking care not to move the
295 * \0 terminator, so that other strings in the string table
296 * are not disturbed. See smbios_add_string()
298 old_len = strnlen(ptr, SMBIOS_STR_MAX);
299 len = strnlen(version, SMBIOS_STR_MAX);
301 return log_ret(-ENOSPC);
303 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
304 memcpy(ptr, version, len);
306 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
313 * smbios_string_table_len() - compute the string area size
315 * This computes the size of the string area including the string terminator.
317 * @ctx: SMBIOS context
318 * Return: string area size
320 static int smbios_string_table_len(const struct smbios_ctx *ctx)
322 /* In case no string is defined we have to return two \0 */
323 if (ctx->next_ptr == ctx->eos)
326 /* Allow for the final \0 after all strings */
327 return (ctx->next_ptr + 1) - ctx->eos;
330 static int smbios_write_type0(ulong *current, int handle,
331 struct smbios_ctx *ctx)
333 struct smbios_type0 *t;
334 int len = sizeof(struct smbios_type0);
336 t = map_sysmem(*current, len);
337 memset(t, 0, sizeof(struct smbios_type0));
338 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
339 smbios_set_eos(ctx, t->eos);
340 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
342 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
344 gd->smbios_version = ctx->last_str;
345 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
348 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
349 1, strlen(gd->smbios_version) + 1, 0);
351 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
352 #ifdef CONFIG_ROM_SIZE
353 if (CONFIG_ROM_SIZE < SZ_16M) {
354 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
356 /* CONFIG_ROM_SIZE < 8 GiB */
357 t->bios_rom_size = 0xff;
358 t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20;
361 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
362 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
363 BIOS_CHARACTERISTICS_UPGRADEABLE;
364 #ifdef CONFIG_GENERATE_ACPI_TABLE
365 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
367 #ifdef CONFIG_EFI_LOADER
368 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
370 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
372 /* bios_major_release has only one byte, so drop century */
373 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
374 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
375 t->ec_major_release = 0xff;
376 t->ec_minor_release = 0xff;
378 len = t->length + smbios_string_table_len(ctx);
385 static int smbios_write_type1(ulong *current, int handle,
386 struct smbios_ctx *ctx)
388 struct smbios_type1 *t;
389 int len = sizeof(struct smbios_type1);
390 char *serial_str = env_get("serial#");
392 t = map_sysmem(*current, len);
393 memset(t, 0, sizeof(struct smbios_type1));
394 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
395 smbios_set_eos(ctx, t->eos);
396 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
397 SYSID_SM_SYSTEM_MANUFACTURER,
399 t->product_name = smbios_add_prop_si(ctx, "product",
400 SYSID_SM_SYSTEM_PRODUCT,
402 t->version = smbios_add_prop_si(ctx, "version",
403 SYSID_SM_SYSTEM_VERSION,
406 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
407 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
409 t->serial_number = smbios_add_prop_si(ctx, "serial",
410 SYSID_SM_SYSTEM_SERIAL,
413 t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN;
414 t->sku_number = smbios_add_prop_si(ctx, "sku",
415 SYSID_SM_SYSTEM_SKU, NULL);
416 t->family = smbios_add_prop_si(ctx, "family",
417 SYSID_SM_SYSTEM_FAMILY, NULL);
419 len = t->length + smbios_string_table_len(ctx);
426 static int smbios_write_type2(ulong *current, int handle,
427 struct smbios_ctx *ctx)
429 struct smbios_type2 *t;
430 int len = sizeof(struct smbios_type2);
432 t = map_sysmem(*current, len);
433 memset(t, 0, sizeof(struct smbios_type2));
434 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
435 smbios_set_eos(ctx, t->eos);
436 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
437 SYSID_SM_BASEBOARD_MANUFACTURER,
439 t->product_name = smbios_add_prop_si(ctx, "product",
440 SYSID_SM_BASEBOARD_PRODUCT,
442 t->version = smbios_add_prop_si(ctx, "version",
443 SYSID_SM_BASEBOARD_VERSION,
446 t->serial_number = smbios_add_prop_si(ctx, "serial",
447 SYSID_SM_BASEBOARD_SERIAL,
449 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
450 SYSID_SM_BASEBOARD_ASSET_TAG,
452 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
453 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
454 t->chassis_handle = handle + 1;
456 len = t->length + smbios_string_table_len(ctx);
463 static int smbios_write_type3(ulong *current, int handle,
464 struct smbios_ctx *ctx)
466 struct smbios_type3 *t;
467 int len = sizeof(struct smbios_type3);
469 t = map_sysmem(*current, len);
470 memset(t, 0, sizeof(struct smbios_type3));
471 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
472 smbios_set_eos(ctx, t->eos);
473 t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL);
474 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
475 t->bootup_state = SMBIOS_STATE_SAFE;
476 t->power_supply_state = SMBIOS_STATE_SAFE;
477 t->thermal_state = SMBIOS_STATE_SAFE;
478 t->security_status = SMBIOS_SECURITY_NONE;
480 len = t->length + smbios_string_table_len(ctx);
487 static void smbios_write_type4_dm(struct smbios_type4 *t,
488 struct smbios_ctx *ctx)
490 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
491 const char *vendor = NULL;
492 const char *name = NULL;
495 char processor_name[49];
496 char vendor_name[49];
497 struct udevice *cpu = NULL;
499 uclass_find_first_device(UCLASS_CPU, &cpu);
501 struct cpu_plat *plat = dev_get_parent_plat(cpu);
504 processor_family = plat->family;
505 t->processor_id[0] = plat->id[0];
506 t->processor_id[1] = plat->id[1];
508 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
509 vendor = vendor_name;
510 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
511 name = processor_name;
515 t->processor_family = 0xfe;
516 t->processor_family2 = processor_family;
517 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
518 t->processor_version = smbios_add_prop(ctx, NULL, name);
521 static int smbios_write_type4(ulong *current, int handle,
522 struct smbios_ctx *ctx)
524 struct smbios_type4 *t;
525 int len = sizeof(struct smbios_type4);
527 t = map_sysmem(*current, len);
528 memset(t, 0, sizeof(struct smbios_type4));
529 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
530 smbios_set_eos(ctx, t->eos);
531 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
532 smbios_write_type4_dm(t, ctx);
533 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
534 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
535 t->l1_cache_handle = 0xffff;
536 t->l2_cache_handle = 0xffff;
537 t->l3_cache_handle = 0xffff;
539 len = t->length + smbios_string_table_len(ctx);
546 static int smbios_write_type32(ulong *current, int handle,
547 struct smbios_ctx *ctx)
549 struct smbios_type32 *t;
550 int len = sizeof(struct smbios_type32);
552 t = map_sysmem(*current, len);
553 memset(t, 0, sizeof(struct smbios_type32));
554 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
555 smbios_set_eos(ctx, t->eos);
563 static int smbios_write_type127(ulong *current, int handle,
564 struct smbios_ctx *ctx)
566 struct smbios_type127 *t;
567 int len = sizeof(struct smbios_type127);
569 t = map_sysmem(*current, len);
570 memset(t, 0, sizeof(struct smbios_type127));
571 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
579 static struct smbios_write_method smbios_write_funcs[] = {
580 { smbios_write_type0, "bios", },
581 { smbios_write_type1, "system", },
582 { smbios_write_type2, "baseboard", },
583 /* Type 3 must immediately follow type 2 due to chassis handle. */
584 { smbios_write_type3, "chassis", },
585 { smbios_write_type4, },
586 { smbios_write_type32, },
587 { smbios_write_type127 },
590 ulong write_smbios_table(ulong addr)
592 ofnode parent_node = ofnode_null();
593 ulong table_addr, start_addr;
594 struct smbios3_entry *se;
595 struct smbios_ctx ctx;
601 ctx.node = ofnode_null();
602 if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) {
603 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
607 parent_node = dev_read_subnode(ctx.dev, "smbios");
608 ret = sysinfo_detect(ctx.dev);
611 * ignore the error since many boards don't implement
612 * this and we can still use the info in the devicetree
614 ret = log_msg_ret("sys", ret);
622 /* move past the (so-far-unwritten) header to start writing structs */
623 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
626 /* populate minimum required tables */
627 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
628 const struct smbios_write_method *method;
630 method = &smbios_write_funcs[i];
631 ctx.subnode_name = NULL;
632 if (method->subnode_name) {
633 ctx.subnode_name = method->subnode_name;
634 if (IS_ENABLED(CONFIG_OF_CONTROL))
635 ctx.node = ofnode_find_subnode(parent_node,
636 method->subnode_name);
638 len += method->write((ulong *)&addr, handle++, &ctx);
642 * We must use a pointer here so things work correctly on sandbox. The
643 * user of this table is not aware of the mapping of addresses to
644 * sandbox's DRAM buffer.
646 table_addr = (ulong)map_sysmem(tables, 0);
648 /* now go back and write the SMBIOS3 header */
649 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
650 memset(se, '\0', sizeof(struct smbios3_entry));
651 memcpy(se->anchor, "_SM3_", 5);
652 se->length = sizeof(struct smbios3_entry);
653 se->major_ver = SMBIOS_MAJOR_VER;
654 se->minor_ver = SMBIOS_MINOR_VER;
656 se->entry_point_rev = 1;
657 se->table_maximum_size = len;
658 se->struct_table_address = table_addr;
659 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));