1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
5 * Adapted from coreboot src/arch/x86/smbios.c
10 #include <linux/stringify.h>
11 #include <linux/string.h>
15 #include <tables_csum.h>
18 #include <dm/ofnode.h>
21 #include <dm/uclass-internal.h>
24 /* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
25 #if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
26 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
27 #error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
31 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
32 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
33 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
34 * BIOS Release Date should contain date when code was released
35 * and not when it was built or compiled.
37 #if U_BOOT_VERSION_NUM_PATCH < 10
38 #define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
40 #define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
42 #define U_BOOT_DMI_DAY "01"
43 #define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
44 #define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
46 DECLARE_GLOBAL_DATA_PTR
;
49 * struct map_sysinfo - Mapping of sysinfo strings to DT
51 * @sysinfo_str: sysinfo string
53 * @max: Max index of the tokenized string to pick. Counting starts from 0
57 const char *sysinfo_str
;
62 static const struct map_sysinfo sysinfo_to_dt
[] = {
63 { .sysinfo_str
= "product", .dt_str
= "model", 2 },
64 { .sysinfo_str
= "manufacturer", .dt_str
= "compatible", 1 },
68 * struct smbios_ctx - context for writing SMBIOS tables
70 * @node: node containing the information to write (ofnode_null() if none)
71 * @dev: sysinfo device to use (NULL if none)
72 * @eos: end-of-string pointer for the table being processed. This is set
73 * up when we start processing a table
74 * @next_ptr: pointer to the start of the next string to be added. When the
75 * table is nopt empty, this points to the byte after the \0 of the
77 * @last_str: points to the last string that was written to the table, or NULL
89 * Function prototype to write a specific type of SMBIOS structure
91 * @addr: start address to write the structure
92 * @handle: the structure's handle, a unique 16-bit number
93 * @ctx: context for writing the tables
94 * Return: size of the structure
96 typedef int (*smbios_write_type
)(ulong
*addr
, int handle
,
97 struct smbios_ctx
*ctx
);
100 * struct smbios_write_method - Information about a table-writing function
102 * @write: Function to call
103 * @subnode_name: Name of subnode which has the information for this function,
106 struct smbios_write_method
{
107 smbios_write_type write
;
108 const char *subnode_name
;
111 static const struct map_sysinfo
*convert_sysinfo_to_dt(const char *sysinfo_str
)
115 for (i
= 0; i
< ARRAY_SIZE(sysinfo_to_dt
); i
++) {
116 if (!strcmp(sysinfo_str
, sysinfo_to_dt
[i
].sysinfo_str
))
117 return &sysinfo_to_dt
[i
];
124 * smbios_add_string() - add a string to the string area
126 * This adds a string to the string area which is appended directly after
127 * the formatted portion of an SMBIOS structure.
129 * @ctx: SMBIOS context
130 * @str: string to add
131 * Return: string number in the string area (1 or more)
133 static int smbios_add_string(struct smbios_ctx
*ctx
, const char *str
)
150 if (!strcmp(p
, str
)) {
161 * get_str_from_dt - Get a substring from a DT property.
162 * After finding the property in the DT, the function
163 * will parse comma-separated values and return the value.
164 * If nprop->max exceeds the number of comma-separated
165 * elements, the last non NULL value will be returned.
166 * Counting starts from zero.
168 * @nprop: sysinfo property to use
169 * @str: pointer to fill with data
170 * @size: str buffer length
173 void get_str_from_dt(const struct map_sysinfo
*nprop
, char *str
, size_t size
)
179 memset(str
, 0, size
);
180 if (!nprop
|| !nprop
->max
)
183 dt_str
= ofnode_read_string(ofnode_root(), nprop
->dt_str
);
187 memcpy(str
, dt_str
, size
);
188 token
= strtok(str
, ",");
189 while (token
&& cnt
< nprop
->max
) {
190 strlcpy(str
, token
, strlen(token
) + 1);
191 token
= strtok(NULL
, ",");
197 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
199 * Sysinfo is used if available, with a fallback to devicetree
201 * @ctx: context for writing the tables
202 * @prop: property to write
203 * @dval: Default value to use if the string is not found or is empty
204 * Return: 0 if not found, else SMBIOS string number (1 or more)
206 static int smbios_add_prop_si(struct smbios_ctx
*ctx
, const char *prop
,
207 int sysinfo_id
, const char *dval
)
215 return smbios_add_string(ctx
, dval
);
217 if (sysinfo_id
&& ctx
->dev
) {
218 char val
[SMBIOS_STR_MAX
];
220 ret
= sysinfo_get_str(ctx
->dev
, sysinfo_id
, sizeof(val
), val
);
222 return smbios_add_string(ctx
, val
);
224 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
225 const char *str
= NULL
;
226 char str_dt
[128] = { 0 };
228 * If the node is not valid fallback and try the entire DT
229 * so we can at least fill in manufacturer and board type
231 if (ofnode_valid(ctx
->node
)) {
232 str
= ofnode_read_string(ctx
->node
, prop
);
234 const struct map_sysinfo
*nprop
;
236 nprop
= convert_sysinfo_to_dt(prop
);
237 get_str_from_dt(nprop
, str_dt
, sizeof(str_dt
));
238 str
= (const char *)str_dt
;
241 ret
= smbios_add_string(ctx
, str
&& *str
? str
: dval
);
249 * smbios_add_prop() - Add a property from the devicetree
251 * @prop: property to write. The default string will be written if
253 * @dval: Default value to use if the string is not found or is empty
254 * Return: 0 if not found, else SMBIOS string number (1 or more)
256 static int smbios_add_prop(struct smbios_ctx
*ctx
, const char *prop
,
259 return smbios_add_prop_si(ctx
, prop
, SYSINFO_ID_NONE
, dval
);
262 static void smbios_set_eos(struct smbios_ctx
*ctx
, char *eos
)
266 ctx
->last_str
= NULL
;
269 int smbios_update_version(const char *version
)
271 char *ptr
= gd
->smbios_version
;
275 return log_ret(-ENOENT
);
278 * This string is supposed to have at least enough bytes and is
279 * padded with spaces. Update it, taking care not to move the
280 * \0 terminator, so that other strings in the string table
281 * are not disturbed. See smbios_add_string()
283 old_len
= strnlen(ptr
, SMBIOS_STR_MAX
);
284 len
= strnlen(version
, SMBIOS_STR_MAX
);
286 return log_ret(-ENOSPC
);
288 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr
);
289 memcpy(ptr
, version
, len
);
291 print_buffer((ulong
)ptr
, ptr
, 1, old_len
+ 1, 0);
298 * smbios_string_table_len() - compute the string area size
300 * This computes the size of the string area including the string terminator.
302 * @ctx: SMBIOS context
303 * Return: string area size
305 static int smbios_string_table_len(const struct smbios_ctx
*ctx
)
307 /* Allow for the final \0 after all strings */
308 return (ctx
->next_ptr
+ 1) - ctx
->eos
;
311 static int smbios_write_type0(ulong
*current
, int handle
,
312 struct smbios_ctx
*ctx
)
314 struct smbios_type0
*t
;
315 int len
= sizeof(struct smbios_type0
);
317 t
= map_sysmem(*current
, len
);
318 memset(t
, 0, sizeof(struct smbios_type0
));
319 fill_smbios_header(t
, SMBIOS_BIOS_INFORMATION
, len
, handle
);
320 smbios_set_eos(ctx
, t
->eos
);
321 t
->vendor
= smbios_add_prop(ctx
, NULL
, "U-Boot");
323 t
->bios_ver
= smbios_add_prop(ctx
, "version", PLAIN_VERSION
);
325 gd
->smbios_version
= ctx
->last_str
;
326 log_debug("smbios_version = %p: '%s'\n", gd
->smbios_version
,
329 print_buffer((ulong
)gd
->smbios_version
, gd
->smbios_version
,
330 1, strlen(gd
->smbios_version
) + 1, 0);
332 t
->bios_release_date
= smbios_add_prop(ctx
, NULL
, U_BOOT_DMI_DATE
);
333 #ifdef CONFIG_ROM_SIZE
334 t
->bios_rom_size
= (CONFIG_ROM_SIZE
/ 65536) - 1;
336 t
->bios_characteristics
= BIOS_CHARACTERISTICS_PCI_SUPPORTED
|
337 BIOS_CHARACTERISTICS_SELECTABLE_BOOT
|
338 BIOS_CHARACTERISTICS_UPGRADEABLE
;
339 #ifdef CONFIG_GENERATE_ACPI_TABLE
340 t
->bios_characteristics_ext1
= BIOS_CHARACTERISTICS_EXT1_ACPI
;
342 #ifdef CONFIG_EFI_LOADER
343 t
->bios_characteristics_ext2
|= BIOS_CHARACTERISTICS_EXT2_UEFI
;
345 t
->bios_characteristics_ext2
|= BIOS_CHARACTERISTICS_EXT2_TARGET
;
347 /* bios_major_release has only one byte, so drop century */
348 t
->bios_major_release
= U_BOOT_VERSION_NUM
% 100;
349 t
->bios_minor_release
= U_BOOT_VERSION_NUM_PATCH
;
350 t
->ec_major_release
= 0xff;
351 t
->ec_minor_release
= 0xff;
353 len
= t
->length
+ smbios_string_table_len(ctx
);
360 static int smbios_write_type1(ulong
*current
, int handle
,
361 struct smbios_ctx
*ctx
)
363 struct smbios_type1
*t
;
364 int len
= sizeof(struct smbios_type1
);
365 char *serial_str
= env_get("serial#");
367 t
= map_sysmem(*current
, len
);
368 memset(t
, 0, sizeof(struct smbios_type1
));
369 fill_smbios_header(t
, SMBIOS_SYSTEM_INFORMATION
, len
, handle
);
370 smbios_set_eos(ctx
, t
->eos
);
371 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
372 t
->product_name
= smbios_add_prop(ctx
, "product", "Unknown");
373 t
->version
= smbios_add_prop_si(ctx
, "version",
374 SYSINFO_ID_SMBIOS_SYSTEM_VERSION
,
377 t
->serial_number
= smbios_add_prop(ctx
, NULL
, serial_str
);
378 strncpy((char *)t
->uuid
, serial_str
, sizeof(t
->uuid
));
380 t
->serial_number
= smbios_add_prop(ctx
, "serial", "Unknown");
382 t
->sku_number
= smbios_add_prop(ctx
, "sku", "Unknown");
383 t
->family
= smbios_add_prop(ctx
, "family", "Unknown");
385 len
= t
->length
+ smbios_string_table_len(ctx
);
392 static int smbios_write_type2(ulong
*current
, int handle
,
393 struct smbios_ctx
*ctx
)
395 struct smbios_type2
*t
;
396 int len
= sizeof(struct smbios_type2
);
398 t
= map_sysmem(*current
, len
);
399 memset(t
, 0, sizeof(struct smbios_type2
));
400 fill_smbios_header(t
, SMBIOS_BOARD_INFORMATION
, len
, handle
);
401 smbios_set_eos(ctx
, t
->eos
);
402 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
403 t
->product_name
= smbios_add_prop(ctx
, "product", "Unknown");
404 t
->version
= smbios_add_prop_si(ctx
, "version",
405 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION
,
407 t
->asset_tag_number
= smbios_add_prop(ctx
, "asset-tag", "Unknown");
408 t
->feature_flags
= SMBIOS_BOARD_FEATURE_HOSTING
;
409 t
->board_type
= SMBIOS_BOARD_MOTHERBOARD
;
411 len
= t
->length
+ smbios_string_table_len(ctx
);
418 static int smbios_write_type3(ulong
*current
, int handle
,
419 struct smbios_ctx
*ctx
)
421 struct smbios_type3
*t
;
422 int len
= sizeof(struct smbios_type3
);
424 t
= map_sysmem(*current
, len
);
425 memset(t
, 0, sizeof(struct smbios_type3
));
426 fill_smbios_header(t
, SMBIOS_SYSTEM_ENCLOSURE
, len
, handle
);
427 smbios_set_eos(ctx
, t
->eos
);
428 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
429 t
->chassis_type
= SMBIOS_ENCLOSURE_DESKTOP
;
430 t
->bootup_state
= SMBIOS_STATE_SAFE
;
431 t
->power_supply_state
= SMBIOS_STATE_SAFE
;
432 t
->thermal_state
= SMBIOS_STATE_SAFE
;
433 t
->security_status
= SMBIOS_SECURITY_NONE
;
435 len
= t
->length
+ smbios_string_table_len(ctx
);
442 static void smbios_write_type4_dm(struct smbios_type4
*t
,
443 struct smbios_ctx
*ctx
)
445 u16 processor_family
= SMBIOS_PROCESSOR_FAMILY_UNKNOWN
;
446 const char *vendor
= "Unknown";
447 const char *name
= "Unknown";
450 char processor_name
[49];
451 char vendor_name
[49];
452 struct udevice
*cpu
= NULL
;
454 uclass_find_first_device(UCLASS_CPU
, &cpu
);
456 struct cpu_plat
*plat
= dev_get_parent_plat(cpu
);
459 processor_family
= plat
->family
;
460 t
->processor_id
[0] = plat
->id
[0];
461 t
->processor_id
[1] = plat
->id
[1];
463 if (!cpu_get_vendor(cpu
, vendor_name
, sizeof(vendor_name
)))
464 vendor
= vendor_name
;
465 if (!cpu_get_desc(cpu
, processor_name
, sizeof(processor_name
)))
466 name
= processor_name
;
470 t
->processor_family
= processor_family
;
471 t
->processor_manufacturer
= smbios_add_prop(ctx
, NULL
, vendor
);
472 t
->processor_version
= smbios_add_prop(ctx
, NULL
, name
);
475 static int smbios_write_type4(ulong
*current
, int handle
,
476 struct smbios_ctx
*ctx
)
478 struct smbios_type4
*t
;
479 int len
= sizeof(struct smbios_type4
);
481 t
= map_sysmem(*current
, len
);
482 memset(t
, 0, sizeof(struct smbios_type4
));
483 fill_smbios_header(t
, SMBIOS_PROCESSOR_INFORMATION
, len
, handle
);
484 smbios_set_eos(ctx
, t
->eos
);
485 t
->processor_type
= SMBIOS_PROCESSOR_TYPE_CENTRAL
;
486 smbios_write_type4_dm(t
, ctx
);
487 t
->status
= SMBIOS_PROCESSOR_STATUS_ENABLED
;
488 t
->processor_upgrade
= SMBIOS_PROCESSOR_UPGRADE_NONE
;
489 t
->l1_cache_handle
= 0xffff;
490 t
->l2_cache_handle
= 0xffff;
491 t
->l3_cache_handle
= 0xffff;
492 t
->processor_family2
= t
->processor_family
;
494 len
= t
->length
+ smbios_string_table_len(ctx
);
501 static int smbios_write_type32(ulong
*current
, int handle
,
502 struct smbios_ctx
*ctx
)
504 struct smbios_type32
*t
;
505 int len
= sizeof(struct smbios_type32
);
507 t
= map_sysmem(*current
, len
);
508 memset(t
, 0, sizeof(struct smbios_type32
));
509 fill_smbios_header(t
, SMBIOS_SYSTEM_BOOT_INFORMATION
, len
, handle
);
510 smbios_set_eos(ctx
, t
->eos
);
518 static int smbios_write_type127(ulong
*current
, int handle
,
519 struct smbios_ctx
*ctx
)
521 struct smbios_type127
*t
;
522 int len
= sizeof(struct smbios_type127
);
524 t
= map_sysmem(*current
, len
);
525 memset(t
, 0, sizeof(struct smbios_type127
));
526 fill_smbios_header(t
, SMBIOS_END_OF_TABLE
, len
, handle
);
534 static struct smbios_write_method smbios_write_funcs
[] = {
535 { smbios_write_type0
, "bios", },
536 { smbios_write_type1
, "system", },
537 { smbios_write_type2
, "baseboard", },
538 { smbios_write_type3
, "chassis", },
539 { smbios_write_type4
, },
540 { smbios_write_type32
, },
541 { smbios_write_type127
},
544 ulong
write_smbios_table(ulong addr
)
546 ofnode parent_node
= ofnode_null();
547 struct smbios_entry
*se
;
548 struct smbios_ctx ctx
;
552 int max_struct_size
= 0;
558 ctx
.node
= ofnode_null();
559 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
560 uclass_first_device(UCLASS_SYSINFO
, &ctx
.dev
);
562 parent_node
= dev_read_subnode(ctx
.dev
, "smbios");
567 /* 16 byte align the table address */
568 addr
= ALIGN(addr
, 16);
570 se
= map_sysmem(addr
, sizeof(struct smbios_entry
));
571 memset(se
, 0, sizeof(struct smbios_entry
));
573 addr
+= sizeof(struct smbios_entry
);
574 addr
= ALIGN(addr
, 16);
577 /* populate minimum required tables */
578 for (i
= 0; i
< ARRAY_SIZE(smbios_write_funcs
); i
++) {
579 const struct smbios_write_method
*method
;
582 method
= &smbios_write_funcs
[i
];
583 if (IS_ENABLED(CONFIG_OF_CONTROL
) && method
->subnode_name
)
584 ctx
.node
= ofnode_find_subnode(parent_node
,
585 method
->subnode_name
);
586 tmp
= method
->write((ulong
*)&addr
, handle
++, &ctx
);
588 max_struct_size
= max(max_struct_size
, tmp
);
592 memcpy(se
->anchor
, "_SM_", 4);
593 se
->length
= sizeof(struct smbios_entry
);
594 se
->major_ver
= SMBIOS_MAJOR_VER
;
595 se
->minor_ver
= SMBIOS_MINOR_VER
;
596 se
->max_struct_size
= max_struct_size
;
597 memcpy(se
->intermediate_anchor
, "_DMI_", 5);
598 se
->struct_table_length
= len
;
601 * We must use a pointer here so things work correctly on sandbox. The
602 * user of this table is not aware of the mapping of addresses to
603 * sandbox's DRAM buffer.
605 table_addr
= (ulong
)map_sysmem(tables
, 0);
606 if (sizeof(table_addr
) > sizeof(u32
) && table_addr
> (ulong
)UINT_MAX
) {
608 * We need to put this >32-bit pointer into the table but the
609 * field is only 32 bits wide.
611 printf("WARNING: SMBIOS table_address overflow %llx\n",
612 (unsigned long long)table_addr
);
616 se
->struct_table_address
= table_addr
;
618 se
->struct_count
= handle
;
620 /* calculate checksums */
621 istart
= (char *)se
+ SMBIOS_INTERMEDIATE_OFFSET
;
622 isize
= sizeof(struct smbios_entry
) - SMBIOS_INTERMEDIATE_OFFSET
;
623 se
->intermediate_checksum
= table_compute_checksum(istart
, isize
);
624 se
->checksum
= table_compute_checksum(se
, sizeof(struct smbios_entry
));