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
13 #include <tables_csum.h>
17 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR
;
23 SMBIOS_STR_MAX
= 64, /* Maximum length allowed for a string */
27 * struct smbios_ctx - context for writing SMBIOS tables
29 * @node: node containing the information to write (ofnode_null() if none)
30 * @dev: sysinfo device to use (NULL if none)
31 * @eos: end-of-string pointer for the table being processed. This is set
32 * up when we start processing a table
33 * @next_ptr: pointer to the start of the next string to be added. When the
34 * table is nopt empty, this points to the byte after the \0 of the
36 * @last_str: points to the last string that was written to the table, or NULL
48 * Function prototype to write a specific type of SMBIOS structure
50 * @addr: start address to write the structure
51 * @handle: the structure's handle, a unique 16-bit number
52 * @ctx: context for writing the tables
53 * @return: size of the structure
55 typedef int (*smbios_write_type
)(ulong
*addr
, int handle
,
56 struct smbios_ctx
*ctx
);
59 * struct smbios_write_method - Information about a table-writing function
61 * @write: Function to call
62 * @subnode_name: Name of subnode which has the information for this function,
65 struct smbios_write_method
{
66 smbios_write_type write
;
67 const char *subnode_name
;
71 * smbios_add_string() - add a string to the string area
73 * This adds a string to the string area which is appended directly after
74 * the formatted portion of an SMBIOS structure.
76 * @ctx: SMBIOS context
78 * @return: string number in the string area (1 or more)
80 static int smbios_add_string(struct smbios_ctx
*ctx
, const char *str
)
100 if (!strcmp(p
, str
)) {
111 * smbios_add_prop() - Add a property from the device tree
113 * @ctx: context for writing the tables
114 * @prop: property to write
115 * @return 0 if not found, else SMBIOS string number (1 or more)
117 static int smbios_add_prop(struct smbios_ctx
*ctx
, const char *prop
)
119 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
122 str
= ofnode_read_string(ctx
->node
, prop
);
124 return smbios_add_string(ctx
, str
);
130 static void smbios_set_eos(struct smbios_ctx
*ctx
, char *eos
)
134 ctx
->last_str
= NULL
;
137 int smbios_update_version(const char *version
)
139 char *ptr
= gd
->smbios_version
;
143 return log_ret(-ENOENT
);
146 * This string is supposed to have at least enough bytes and is
147 * padded with spaces. Update it, taking care not to move the
148 * \0 terminator, so that other strings in the string table
149 * are not disturbed. See smbios_add_string()
151 old_len
= strnlen(ptr
, SMBIOS_STR_MAX
);
152 len
= strnlen(version
, SMBIOS_STR_MAX
);
154 return log_ret(-ENOSPC
);
156 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr
);
157 memcpy(ptr
, version
, len
);
159 print_buffer((ulong
)ptr
, ptr
, 1, old_len
+ 1, 0);
166 * smbios_string_table_len() - compute the string area size
168 * This computes the size of the string area including the string terminator.
170 * @ctx: SMBIOS context
171 * @return: string area size
173 static int smbios_string_table_len(const struct smbios_ctx
*ctx
)
175 /* Allow for the final \0 after all strings */
176 return (ctx
->next_ptr
+ 1) - ctx
->eos
;
179 static int smbios_write_type0(ulong
*current
, int handle
,
180 struct smbios_ctx
*ctx
)
182 struct smbios_type0
*t
;
183 int len
= sizeof(struct smbios_type0
);
185 t
= map_sysmem(*current
, len
);
186 memset(t
, 0, sizeof(struct smbios_type0
));
187 fill_smbios_header(t
, SMBIOS_BIOS_INFORMATION
, len
, handle
);
188 smbios_set_eos(ctx
, t
->eos
);
189 t
->vendor
= smbios_add_string(ctx
, "U-Boot");
191 t
->bios_ver
= smbios_add_prop(ctx
, "version");
193 t
->bios_ver
= smbios_add_string(ctx
, PLAIN_VERSION
);
195 gd
->smbios_version
= ctx
->last_str
;
196 log_debug("smbios_version = %p: '%s'\n", gd
->smbios_version
,
199 print_buffer((ulong
)gd
->smbios_version
, gd
->smbios_version
,
200 1, strlen(gd
->smbios_version
) + 1, 0);
202 t
->bios_release_date
= smbios_add_string(ctx
, U_BOOT_DMI_DATE
);
203 #ifdef CONFIG_ROM_SIZE
204 t
->bios_rom_size
= (CONFIG_ROM_SIZE
/ 65536) - 1;
206 t
->bios_characteristics
= BIOS_CHARACTERISTICS_PCI_SUPPORTED
|
207 BIOS_CHARACTERISTICS_SELECTABLE_BOOT
|
208 BIOS_CHARACTERISTICS_UPGRADEABLE
;
209 #ifdef CONFIG_GENERATE_ACPI_TABLE
210 t
->bios_characteristics_ext1
= BIOS_CHARACTERISTICS_EXT1_ACPI
;
212 #ifdef CONFIG_EFI_LOADER
213 t
->bios_characteristics_ext1
|= BIOS_CHARACTERISTICS_EXT1_UEFI
;
215 t
->bios_characteristics_ext2
= BIOS_CHARACTERISTICS_EXT2_TARGET
;
217 /* bios_major_release has only one byte, so drop century */
218 t
->bios_major_release
= U_BOOT_VERSION_NUM
% 100;
219 t
->bios_minor_release
= U_BOOT_VERSION_NUM_PATCH
;
220 t
->ec_major_release
= 0xff;
221 t
->ec_minor_release
= 0xff;
223 len
= t
->length
+ smbios_string_table_len(ctx
);
230 static int smbios_write_type1(ulong
*current
, int handle
,
231 struct smbios_ctx
*ctx
)
233 struct smbios_type1
*t
;
234 int len
= sizeof(struct smbios_type1
);
235 char *serial_str
= env_get("serial#");
237 t
= map_sysmem(*current
, len
);
238 memset(t
, 0, sizeof(struct smbios_type1
));
239 fill_smbios_header(t
, SMBIOS_SYSTEM_INFORMATION
, len
, handle
);
240 smbios_set_eos(ctx
, t
->eos
);
241 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer");
242 t
->product_name
= smbios_add_prop(ctx
, "product");
243 t
->version
= smbios_add_prop(ctx
, "version");
245 t
->serial_number
= smbios_add_string(ctx
, serial_str
);
246 strncpy((char *)t
->uuid
, serial_str
, sizeof(t
->uuid
));
248 t
->serial_number
= smbios_add_prop(ctx
, "serial");
250 t
->sku_number
= smbios_add_prop(ctx
, "sku");
251 t
->family
= smbios_add_prop(ctx
, "family");
253 len
= t
->length
+ smbios_string_table_len(ctx
);
260 static int smbios_write_type2(ulong
*current
, int handle
,
261 struct smbios_ctx
*ctx
)
263 struct smbios_type2
*t
;
264 int len
= sizeof(struct smbios_type2
);
266 t
= map_sysmem(*current
, len
);
267 memset(t
, 0, sizeof(struct smbios_type2
));
268 fill_smbios_header(t
, SMBIOS_BOARD_INFORMATION
, len
, handle
);
269 smbios_set_eos(ctx
, t
->eos
);
270 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer");
271 t
->product_name
= smbios_add_prop(ctx
, "product");
272 t
->asset_tag_number
= smbios_add_prop(ctx
, "asset-tag");
273 t
->feature_flags
= SMBIOS_BOARD_FEATURE_HOSTING
;
274 t
->board_type
= SMBIOS_BOARD_MOTHERBOARD
;
276 len
= t
->length
+ smbios_string_table_len(ctx
);
283 static int smbios_write_type3(ulong
*current
, int handle
,
284 struct smbios_ctx
*ctx
)
286 struct smbios_type3
*t
;
287 int len
= sizeof(struct smbios_type3
);
289 t
= map_sysmem(*current
, len
);
290 memset(t
, 0, sizeof(struct smbios_type3
));
291 fill_smbios_header(t
, SMBIOS_SYSTEM_ENCLOSURE
, len
, handle
);
292 smbios_set_eos(ctx
, t
->eos
);
293 t
->manufacturer
= smbios_add_prop(ctx
, "manufacturer");
294 t
->chassis_type
= SMBIOS_ENCLOSURE_DESKTOP
;
295 t
->bootup_state
= SMBIOS_STATE_SAFE
;
296 t
->power_supply_state
= SMBIOS_STATE_SAFE
;
297 t
->thermal_state
= SMBIOS_STATE_SAFE
;
298 t
->security_status
= SMBIOS_SECURITY_NONE
;
300 len
= t
->length
+ smbios_string_table_len(ctx
);
307 static void smbios_write_type4_dm(struct smbios_type4
*t
,
308 struct smbios_ctx
*ctx
)
310 u16 processor_family
= SMBIOS_PROCESSOR_FAMILY_UNKNOWN
;
311 const char *vendor
= "Unknown";
312 const char *name
= "Unknown";
315 char processor_name
[49];
316 char vendor_name
[49];
317 struct udevice
*cpu
= NULL
;
319 uclass_find_first_device(UCLASS_CPU
, &cpu
);
321 struct cpu_plat
*plat
= dev_get_parent_plat(cpu
);
324 processor_family
= plat
->family
;
325 t
->processor_id
[0] = plat
->id
[0];
326 t
->processor_id
[1] = plat
->id
[1];
328 if (!cpu_get_vendor(cpu
, vendor_name
, sizeof(vendor_name
)))
329 vendor
= vendor_name
;
330 if (!cpu_get_desc(cpu
, processor_name
, sizeof(processor_name
)))
331 name
= processor_name
;
335 t
->processor_family
= processor_family
;
336 t
->processor_manufacturer
= smbios_add_string(ctx
, vendor
);
337 t
->processor_version
= smbios_add_string(ctx
, name
);
340 static int smbios_write_type4(ulong
*current
, int handle
,
341 struct smbios_ctx
*ctx
)
343 struct smbios_type4
*t
;
344 int len
= sizeof(struct smbios_type4
);
346 t
= map_sysmem(*current
, len
);
347 memset(t
, 0, sizeof(struct smbios_type4
));
348 fill_smbios_header(t
, SMBIOS_PROCESSOR_INFORMATION
, len
, handle
);
349 smbios_set_eos(ctx
, t
->eos
);
350 t
->processor_type
= SMBIOS_PROCESSOR_TYPE_CENTRAL
;
351 smbios_write_type4_dm(t
, ctx
);
352 t
->status
= SMBIOS_PROCESSOR_STATUS_ENABLED
;
353 t
->processor_upgrade
= SMBIOS_PROCESSOR_UPGRADE_NONE
;
354 t
->l1_cache_handle
= 0xffff;
355 t
->l2_cache_handle
= 0xffff;
356 t
->l3_cache_handle
= 0xffff;
357 t
->processor_family2
= t
->processor_family
;
359 len
= t
->length
+ smbios_string_table_len(ctx
);
366 static int smbios_write_type32(ulong
*current
, int handle
,
367 struct smbios_ctx
*ctx
)
369 struct smbios_type32
*t
;
370 int len
= sizeof(struct smbios_type32
);
372 t
= map_sysmem(*current
, len
);
373 memset(t
, 0, sizeof(struct smbios_type32
));
374 fill_smbios_header(t
, SMBIOS_SYSTEM_BOOT_INFORMATION
, len
, handle
);
375 smbios_set_eos(ctx
, t
->eos
);
383 static int smbios_write_type127(ulong
*current
, int handle
,
384 struct smbios_ctx
*ctx
)
386 struct smbios_type127
*t
;
387 int len
= sizeof(struct smbios_type127
);
389 t
= map_sysmem(*current
, len
);
390 memset(t
, 0, sizeof(struct smbios_type127
));
391 fill_smbios_header(t
, SMBIOS_END_OF_TABLE
, len
, handle
);
399 static struct smbios_write_method smbios_write_funcs
[] = {
400 { smbios_write_type0
, "bios", },
401 { smbios_write_type1
, "system", },
402 { smbios_write_type2
, "baseboard", },
403 { smbios_write_type3
, "chassis", },
404 { smbios_write_type4
, },
405 { smbios_write_type32
, },
406 { smbios_write_type127
},
409 ulong
write_smbios_table(ulong addr
)
411 ofnode parent_node
= ofnode_null();
412 struct smbios_entry
*se
;
413 struct smbios_ctx ctx
;
417 int max_struct_size
= 0;
423 ctx
.node
= ofnode_null();
424 if (IS_ENABLED(CONFIG_OF_CONTROL
)) {
425 uclass_first_device(UCLASS_SYSINFO
, &ctx
.dev
);
427 parent_node
= dev_read_subnode(ctx
.dev
, "smbios");
432 /* 16 byte align the table address */
433 addr
= ALIGN(addr
, 16);
435 se
= map_sysmem(addr
, sizeof(struct smbios_entry
));
436 memset(se
, 0, sizeof(struct smbios_entry
));
438 addr
+= sizeof(struct smbios_entry
);
439 addr
= ALIGN(addr
, 16);
442 /* populate minimum required tables */
443 for (i
= 0; i
< ARRAY_SIZE(smbios_write_funcs
); i
++) {
444 const struct smbios_write_method
*method
;
447 method
= &smbios_write_funcs
[i
];
448 if (IS_ENABLED(CONFIG_OF_CONTROL
) && method
->subnode_name
)
449 ctx
.node
= ofnode_find_subnode(parent_node
,
450 method
->subnode_name
);
451 tmp
= method
->write((ulong
*)&addr
, handle
++, &ctx
);
453 max_struct_size
= max(max_struct_size
, tmp
);
457 memcpy(se
->anchor
, "_SM_", 4);
458 se
->length
= sizeof(struct smbios_entry
);
459 se
->major_ver
= SMBIOS_MAJOR_VER
;
460 se
->minor_ver
= SMBIOS_MINOR_VER
;
461 se
->max_struct_size
= max_struct_size
;
462 memcpy(se
->intermediate_anchor
, "_DMI_", 5);
463 se
->struct_table_length
= len
;
466 * We must use a pointer here so things work correctly on sandbox. The
467 * user of this table is not aware of the mapping of addresses to
468 * sandbox's DRAM buffer.
470 table_addr
= (ulong
)map_sysmem(tables
, 0);
471 if (sizeof(table_addr
) > sizeof(u32
) && table_addr
> (ulong
)UINT_MAX
) {
473 * We need to put this >32-bit pointer into the table but the
474 * field is only 32 bits wide.
476 printf("WARNING: SMBIOS table_address overflow %llx\n",
477 (unsigned long long)table_addr
);
480 se
->struct_table_address
= table_addr
;
482 se
->struct_count
= handle
;
484 /* calculate checksums */
485 istart
= (char *)se
+ SMBIOS_INTERMEDIATE_OFFSET
;
486 isize
= sizeof(struct smbios_entry
) - SMBIOS_INTERMEDIATE_OFFSET
;
487 se
->intermediate_checksum
= table_compute_checksum(istart
, isize
);
488 se
->checksum
= table_compute_checksum(se
, sizeof(struct smbios_entry
));