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 * @si_str: sysinfo string
53 * @max: Max index of the tokenized string to pick. Counting starts from 0
63 static const struct map_sysinfo sysinfo_to_dt
[] = {
64 { .si_node
= "system", .si_str
= "product", .dt_str
= "model", 2 },
65 { .si_node
= "system", .si_str
= "manufacturer", .dt_str
= "compatible", 1 },
66 { .si_node
= "baseboard", .si_str
= "product", .dt_str
= "model", 2 },
67 { .si_node
= "baseboard", .si_str
= "manufacturer", .dt_str
= "compatible", 1 },
71 * struct smbios_ctx - context for writing SMBIOS tables
73 * @node: node containing the information to write (ofnode_null()
75 * @dev: sysinfo device to use (NULL if none)
76 * @subnode_name: sysinfo subnode_name. Used for DT fallback
77 * @eos: end-of-string pointer for the table being processed.
78 * This is set up when we start processing a table
79 * @next_ptr: pointer to the start of the next string to be added.
80 * When the table is not empty, this points to the byte
81 * after the \0 of the previous string.
82 * @last_str: points to the last string that was written to the table,
88 const char *subnode_name
;
95 * Function prototype to write a specific type of SMBIOS structure
97 * @addr: start address to write the structure
98 * @handle: the structure's handle, a unique 16-bit number
99 * @ctx: context for writing the tables
100 * Return: size of the structure
102 typedef int (*smbios_write_type
)(ulong
*addr
, int handle
,
103 struct smbios_ctx
*ctx
);
106 * struct smbios_write_method - Information about a table-writing function
108 * @write: Function to call
109 * @subnode_name: Name of subnode which has the information for this function,
112 struct smbios_write_method
{
113 smbios_write_type write
;
114 const char *subnode_name
;
117 static const struct map_sysinfo
*convert_sysinfo_to_dt(const char *node
, const char *si
)
121 for (i
= 0; i
< ARRAY_SIZE(sysinfo_to_dt
); i
++) {
122 if (node
&& !strcmp(node
, sysinfo_to_dt
[i
].si_node
) &&
123 !strcmp(si
, sysinfo_to_dt
[i
].si_str
))
124 return &sysinfo_to_dt
[i
];
131 * smbios_add_string() - add a string to the string area
133 * This adds a string to the string area which is appended directly after
134 * the formatted portion of an SMBIOS structure.
136 * @ctx: SMBIOS context
137 * @str: string to add
138 * Return: string number in the string area (1 or more)
140 static int smbios_add_string(struct smbios_ctx
*ctx
, const char *str
)
157 if (!strcmp(p
, str
)) {
168 * get_str_from_dt - Get a substring from a DT property.
169 * After finding the property in the DT, the function
170 * will parse comma-separated values and return the value.
171 * If nprop->max exceeds the number of comma-separated
172 * elements, the last non NULL value will be returned.
173 * Counting starts from zero.
175 * @nprop: sysinfo property to use
176 * @str: pointer to fill with data
177 * @size: str buffer length
180 void get_str_from_dt(const struct map_sysinfo
*nprop
, char *str
, size_t size
)
186 memset(str
, 0, size
);
187 if (!nprop
|| !nprop
->max
)
190 dt_str
= ofnode_read_string(ofnode_root(), nprop
->dt_str
);
194 memcpy(str
, dt_str
, size
);
195 token
= strtok(str
, ",");
196 while (token
&& cnt
< nprop
->max
) {
197 strlcpy(str
, token
, strlen(token
) + 1);
198 token
= strtok(NULL
, ",");
204 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
206 * Sysinfo is used if available, with a fallback to devicetree
208 * @ctx: context for writing the tables
209 * @prop: property to write
210 * @dval: Default value to use if the string is not found or is empty
211 * Return: 0 if not found, else SMBIOS string number (1 or more)
213 static int smbios_add_prop_si(struct smbios_ctx
*ctx
, const char *prop
,
214 int sysinfo_id
, const char *dval
)
222 return smbios_add_string(ctx
, dval
);
224 if (sysinfo_id
&& ctx
->dev
) {
225 char val
[SMBIOS_STR_MAX
];
227 ret
= sysinfo_get_str(ctx
->dev
, sysinfo_id
, sizeof(val
), val
);
229 return smbios_add_string(ctx
, val
);
231 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
232 const char *str
= NULL
;
233 char str_dt
[128] = { 0 };
235 * If the node is not valid fallback and try the entire DT
236 * so we can at least fill in manufacturer and board type
238 if (ofnode_valid(ctx
->node
)) {
239 str
= ofnode_read_string(ctx
->node
, prop
);
241 const struct map_sysinfo
*nprop
;
243 nprop
= convert_sysinfo_to_dt(ctx
->subnode_name
, prop
);
244 get_str_from_dt(nprop
, str_dt
, sizeof(str_dt
));
245 str
= (const char *)str_dt
;
248 ret
= smbios_add_string(ctx
, str
&& *str
? str
: dval
);
256 * smbios_add_prop() - Add a property from the devicetree
258 * @prop: property to write. The default string will be written if
260 * @dval: Default value to use if the string is not found or is empty
261 * Return: 0 if not found, else SMBIOS string number (1 or more)
263 static int smbios_add_prop(struct smbios_ctx
*ctx
, const char *prop
,
266 return smbios_add_prop_si(ctx
, prop
, SYSINFO_ID_NONE
, dval
);
269 static void smbios_set_eos(struct smbios_ctx
*ctx
, char *eos
)
273 ctx
->last_str
= NULL
;
276 int smbios_update_version(const char *version
)
278 char *ptr
= gd
->smbios_version
;
282 return log_ret(-ENOENT
);
285 * This string is supposed to have at least enough bytes and is
286 * padded with spaces. Update it, taking care not to move the
287 * \0 terminator, so that other strings in the string table
288 * are not disturbed. See smbios_add_string()
290 old_len
= strnlen(ptr
, SMBIOS_STR_MAX
);
291 len
= strnlen(version
, SMBIOS_STR_MAX
);
293 return log_ret(-ENOSPC
);
295 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr
);
296 memcpy(ptr
, version
, len
);
298 print_buffer((ulong
)ptr
, ptr
, 1, old_len
+ 1, 0);
305 * smbios_string_table_len() - compute the string area size
307 * This computes the size of the string area including the string terminator.
309 * @ctx: SMBIOS context
310 * Return: string area size
312 static int smbios_string_table_len(const struct smbios_ctx
*ctx
)
314 /* Allow for the final \0 after all strings */
315 return (ctx
->next_ptr
+ 1) - ctx
->eos
;
318 static int smbios_write_type0(ulong
*current
, int handle
,
319 struct smbios_ctx
*ctx
)
321 struct smbios_type0
*t
;
322 int len
= sizeof(struct smbios_type0
);
324 t
= map_sysmem(*current
, len
);
325 memset(t
, 0, sizeof(struct smbios_type0
));
326 fill_smbios_header(t
, SMBIOS_BIOS_INFORMATION
, len
, handle
);
327 smbios_set_eos(ctx
, t
->eos
);
328 t
->vendor
= smbios_add_prop(ctx
, NULL
, "U-Boot");
330 t
->bios_ver
= smbios_add_prop(ctx
, "version", PLAIN_VERSION
);
332 gd
->smbios_version
= ctx
->last_str
;
333 log_debug("smbios_version = %p: '%s'\n", gd
->smbios_version
,
336 print_buffer((ulong
)gd
->smbios_version
, gd
->smbios_version
,
337 1, strlen(gd
->smbios_version
) + 1, 0);
339 t
->bios_release_date
= smbios_add_prop(ctx
, NULL
, U_BOOT_DMI_DATE
);
340 #ifdef CONFIG_ROM_SIZE
341 t
->bios_rom_size
= (CONFIG_ROM_SIZE
/ 65536) - 1;
343 t
->bios_characteristics
= BIOS_CHARACTERISTICS_PCI_SUPPORTED
|
344 BIOS_CHARACTERISTICS_SELECTABLE_BOOT
|
345 BIOS_CHARACTERISTICS_UPGRADEABLE
;
346 #ifdef CONFIG_GENERATE_ACPI_TABLE
347 t
->bios_characteristics_ext1
= BIOS_CHARACTERISTICS_EXT1_ACPI
;
349 #ifdef CONFIG_EFI_LOADER
350 t
->bios_characteristics_ext2
|= BIOS_CHARACTERISTICS_EXT2_UEFI
;
352 t
->bios_characteristics_ext2
|= BIOS_CHARACTERISTICS_EXT2_TARGET
;
354 /* bios_major_release has only one byte, so drop century */
355 t
->bios_major_release
= U_BOOT_VERSION_NUM
% 100;
356 t
->bios_minor_release
= U_BOOT_VERSION_NUM_PATCH
;
357 t
->ec_major_release
= 0xff;
358 t
->ec_minor_release
= 0xff;
360 len
= t
->length
+ smbios_string_table_len(ctx
);
367 static int smbios_write_type1(ulong
*current
, int handle
,
368 struct smbios_ctx
*ctx
)
370 struct smbios_type1
*t
;
371 int len
= sizeof(struct smbios_type1
);
372 char *serial_str
= env_get("serial#");
374 t
= map_sysmem(*current
, len
);
375 memset(t
, 0, sizeof(struct smbios_type1
));
376 fill_smbios_header(t
, SMBIOS_SYSTEM_INFORMATION
, len
, handle
);
377 smbios_set_eos(ctx
, t
->eos
);
378 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
379 t
->product_name
= smbios_add_prop(ctx
, "product", "Unknown");
380 t
->version
= smbios_add_prop_si(ctx
, "version",
381 SYSINFO_ID_SMBIOS_SYSTEM_VERSION
,
384 t
->serial_number
= smbios_add_prop(ctx
, NULL
, serial_str
);
385 strncpy((char *)t
->uuid
, serial_str
, sizeof(t
->uuid
));
387 t
->serial_number
= smbios_add_prop(ctx
, "serial", "Unknown");
389 t
->sku_number
= smbios_add_prop(ctx
, "sku", "Unknown");
390 t
->family
= smbios_add_prop(ctx
, "family", "Unknown");
392 len
= t
->length
+ smbios_string_table_len(ctx
);
399 static int smbios_write_type2(ulong
*current
, int handle
,
400 struct smbios_ctx
*ctx
)
402 struct smbios_type2
*t
;
403 int len
= sizeof(struct smbios_type2
);
405 t
= map_sysmem(*current
, len
);
406 memset(t
, 0, sizeof(struct smbios_type2
));
407 fill_smbios_header(t
, SMBIOS_BOARD_INFORMATION
, len
, handle
);
408 smbios_set_eos(ctx
, t
->eos
);
409 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
410 t
->product_name
= smbios_add_prop(ctx
, "product", "Unknown");
411 t
->version
= smbios_add_prop_si(ctx
, "version",
412 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION
,
414 t
->asset_tag_number
= smbios_add_prop(ctx
, "asset-tag", "Unknown");
415 t
->feature_flags
= SMBIOS_BOARD_FEATURE_HOSTING
;
416 t
->board_type
= SMBIOS_BOARD_MOTHERBOARD
;
418 len
= t
->length
+ smbios_string_table_len(ctx
);
425 static int smbios_write_type3(ulong
*current
, int handle
,
426 struct smbios_ctx
*ctx
)
428 struct smbios_type3
*t
;
429 int len
= sizeof(struct smbios_type3
);
431 t
= map_sysmem(*current
, len
);
432 memset(t
, 0, sizeof(struct smbios_type3
));
433 fill_smbios_header(t
, SMBIOS_SYSTEM_ENCLOSURE
, len
, handle
);
434 smbios_set_eos(ctx
, t
->eos
);
435 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer", "Unknown");
436 t
->chassis_type
= SMBIOS_ENCLOSURE_DESKTOP
;
437 t
->bootup_state
= SMBIOS_STATE_SAFE
;
438 t
->power_supply_state
= SMBIOS_STATE_SAFE
;
439 t
->thermal_state
= SMBIOS_STATE_SAFE
;
440 t
->security_status
= SMBIOS_SECURITY_NONE
;
442 len
= t
->length
+ smbios_string_table_len(ctx
);
449 static void smbios_write_type4_dm(struct smbios_type4
*t
,
450 struct smbios_ctx
*ctx
)
452 u16 processor_family
= SMBIOS_PROCESSOR_FAMILY_UNKNOWN
;
453 const char *vendor
= "Unknown";
454 const char *name
= "Unknown";
457 char processor_name
[49];
458 char vendor_name
[49];
459 struct udevice
*cpu
= NULL
;
461 uclass_find_first_device(UCLASS_CPU
, &cpu
);
463 struct cpu_plat
*plat
= dev_get_parent_plat(cpu
);
466 processor_family
= plat
->family
;
467 t
->processor_id
[0] = plat
->id
[0];
468 t
->processor_id
[1] = plat
->id
[1];
470 if (!cpu_get_vendor(cpu
, vendor_name
, sizeof(vendor_name
)))
471 vendor
= vendor_name
;
472 if (!cpu_get_desc(cpu
, processor_name
, sizeof(processor_name
)))
473 name
= processor_name
;
477 t
->processor_family
= 0xfe;
478 t
->processor_family2
= processor_family
;
479 t
->processor_manufacturer
= smbios_add_prop(ctx
, NULL
, vendor
);
480 t
->processor_version
= smbios_add_prop(ctx
, NULL
, name
);
483 static int smbios_write_type4(ulong
*current
, int handle
,
484 struct smbios_ctx
*ctx
)
486 struct smbios_type4
*t
;
487 int len
= sizeof(struct smbios_type4
);
489 t
= map_sysmem(*current
, len
);
490 memset(t
, 0, sizeof(struct smbios_type4
));
491 fill_smbios_header(t
, SMBIOS_PROCESSOR_INFORMATION
, len
, handle
);
492 smbios_set_eos(ctx
, t
->eos
);
493 t
->processor_type
= SMBIOS_PROCESSOR_TYPE_CENTRAL
;
494 smbios_write_type4_dm(t
, ctx
);
495 t
->status
= SMBIOS_PROCESSOR_STATUS_ENABLED
;
496 t
->processor_upgrade
= SMBIOS_PROCESSOR_UPGRADE_NONE
;
497 t
->l1_cache_handle
= 0xffff;
498 t
->l2_cache_handle
= 0xffff;
499 t
->l3_cache_handle
= 0xffff;
501 len
= t
->length
+ smbios_string_table_len(ctx
);
508 static int smbios_write_type32(ulong
*current
, int handle
,
509 struct smbios_ctx
*ctx
)
511 struct smbios_type32
*t
;
512 int len
= sizeof(struct smbios_type32
);
514 t
= map_sysmem(*current
, len
);
515 memset(t
, 0, sizeof(struct smbios_type32
));
516 fill_smbios_header(t
, SMBIOS_SYSTEM_BOOT_INFORMATION
, len
, handle
);
517 smbios_set_eos(ctx
, t
->eos
);
525 static int smbios_write_type127(ulong
*current
, int handle
,
526 struct smbios_ctx
*ctx
)
528 struct smbios_type127
*t
;
529 int len
= sizeof(struct smbios_type127
);
531 t
= map_sysmem(*current
, len
);
532 memset(t
, 0, sizeof(struct smbios_type127
));
533 fill_smbios_header(t
, SMBIOS_END_OF_TABLE
, len
, handle
);
541 static struct smbios_write_method smbios_write_funcs
[] = {
542 { smbios_write_type0
, "bios", },
543 { smbios_write_type1
, "system", },
544 { smbios_write_type2
, "baseboard", },
545 { smbios_write_type3
, "chassis", },
546 { smbios_write_type4
, },
547 { smbios_write_type32
, },
548 { smbios_write_type127
},
551 ulong
write_smbios_table(ulong addr
)
553 ofnode parent_node
= ofnode_null();
554 ulong table_addr
, start_addr
;
555 struct smbios3_entry
*se
;
556 struct smbios_ctx ctx
;
559 int max_struct_size
= 0;
563 ctx
.node
= ofnode_null();
564 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
565 uclass_first_device(UCLASS_SYSINFO
, &ctx
.dev
);
567 parent_node
= dev_read_subnode(ctx
.dev
, "smbios");
574 /* move past the (so-far-unwritten) header to start writing structs */
575 addr
= ALIGN(addr
+ sizeof(struct smbios3_entry
), 16);
578 /* populate minimum required tables */
579 for (i
= 0; i
< ARRAY_SIZE(smbios_write_funcs
); i
++) {
580 const struct smbios_write_method
*method
;
583 method
= &smbios_write_funcs
[i
];
584 ctx
.subnode_name
= NULL
;
585 if (method
->subnode_name
) {
586 ctx
.subnode_name
= method
->subnode_name
;
587 if (IS_ENABLED(CONFIG_OF_CONTROL
))
588 ctx
.node
= ofnode_find_subnode(parent_node
,
589 method
->subnode_name
);
591 tmp
= method
->write((ulong
*)&addr
, handle
++, &ctx
);
593 max_struct_size
= max(max_struct_size
, tmp
);
598 * We must use a pointer here so things work correctly on sandbox. The
599 * user of this table is not aware of the mapping of addresses to
600 * sandbox's DRAM buffer.
602 table_addr
= (ulong
)map_sysmem(tables
, 0);
604 /* now go back and write the SMBIOS3 header */
605 se
= map_sysmem(start_addr
, sizeof(struct smbios3_entry
));
606 memset(se
, '\0', sizeof(struct smbios3_entry
));
607 memcpy(se
->anchor
, "_SM3_", 5);
608 se
->length
= sizeof(struct smbios3_entry
);
609 se
->major_ver
= SMBIOS_MAJOR_VER
;
610 se
->minor_ver
= SMBIOS_MINOR_VER
;
612 se
->entry_point_rev
= 1;
613 se
->max_struct_size
= len
;
614 se
->struct_table_address
= table_addr
;
615 se
->checksum
= table_compute_checksum(se
, sizeof(struct smbios3_entry
));