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>
21 * struct smbios_ctx - context for writing SMBIOS tables
23 * @node: node containing the information to write (ofnode_null() if none)
24 * @dev: sysinfo device to use (NULL if none)
32 * Function prototype to write a specific type of SMBIOS structure
34 * @addr: start address to write the structure
35 * @handle: the structure's handle, a unique 16-bit number
36 * @ctx: context for writing the tables
37 * @return: size of the structure
39 typedef int (*smbios_write_type)(ulong *addr, int handle,
40 struct smbios_ctx *ctx);
43 * struct smbios_write_method - Information about a table-writing function
45 * @write: Function to call
46 * @subnode_name: Name of subnode which has the information for this function,
49 struct smbios_write_method {
50 smbios_write_type write;
51 const char *subnode_name;
55 * smbios_add_string() - add a string to the string area
57 * This adds a string to the string area which is appended directly after
58 * the formatted portion of an SMBIOS structure.
60 * @start: string area start address
62 * @return: string number in the string area (1 or more)
64 static int smbios_add_string(char *start, const char *str)
90 * smbios_add_prop() - Add a property from the device tree
92 * @start: string area start address
93 * @ctx: context for writing the tables
94 * @prop: property to write
95 * @return 0 if not found, else SMBIOS string number (1 or more)
97 static int smbios_add_prop(char *start, struct smbios_ctx *ctx,
101 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
104 str = ofnode_read_string(ctx->node, prop);
106 return smbios_add_string(start, str);
113 * smbios_string_table_len() - compute the string area size
115 * This computes the size of the string area including the string terminator.
117 * @start: string area start address
118 * @return: string area size
120 static int smbios_string_table_len(char *start)
134 static int smbios_write_type0(ulong *current, int handle,
135 struct smbios_ctx *ctx)
137 struct smbios_type0 *t;
138 int len = sizeof(struct smbios_type0);
140 t = map_sysmem(*current, len);
141 memset(t, 0, sizeof(struct smbios_type0));
142 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
143 t->vendor = smbios_add_string(t->eos, "U-Boot");
144 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
145 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
146 #ifdef CONFIG_ROM_SIZE
147 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
149 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
150 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
151 BIOS_CHARACTERISTICS_UPGRADEABLE;
152 #ifdef CONFIG_GENERATE_ACPI_TABLE
153 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
155 #ifdef CONFIG_EFI_LOADER
156 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
158 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
160 /* bios_major_release has only one byte, so drop century */
161 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
162 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
163 t->ec_major_release = 0xff;
164 t->ec_minor_release = 0xff;
166 len = t->length + smbios_string_table_len(t->eos);
173 static int smbios_write_type1(ulong *current, int handle,
174 struct smbios_ctx *ctx)
176 struct smbios_type1 *t;
177 int len = sizeof(struct smbios_type1);
178 char *serial_str = env_get("serial#");
180 t = map_sysmem(*current, len);
181 memset(t, 0, sizeof(struct smbios_type1));
182 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
183 t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer");
184 t->product_name = smbios_add_prop(t->eos, ctx, "product");
185 t->version = smbios_add_prop(t->eos, ctx, "version");
187 t->serial_number = smbios_add_string(t->eos, serial_str);
188 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
190 t->serial_number = smbios_add_prop(t->eos, ctx, "serial");
192 t->sku_number = smbios_add_prop(t->eos, ctx, "sku");
193 t->family = smbios_add_prop(t->eos, ctx, "family");
195 len = t->length + smbios_string_table_len(t->eos);
202 static int smbios_write_type2(ulong *current, int handle,
203 struct smbios_ctx *ctx)
205 struct smbios_type2 *t;
206 int len = sizeof(struct smbios_type2);
208 t = map_sysmem(*current, len);
209 memset(t, 0, sizeof(struct smbios_type2));
210 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
211 t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer");
212 t->product_name = smbios_add_prop(t->eos, ctx, "product");
213 t->asset_tag_number = smbios_add_prop(t->eos, ctx, "asset-tag");
214 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
215 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
217 len = t->length + smbios_string_table_len(t->eos);
224 static int smbios_write_type3(ulong *current, int handle,
225 struct smbios_ctx *ctx)
227 struct smbios_type3 *t;
228 int len = sizeof(struct smbios_type3);
230 t = map_sysmem(*current, len);
231 memset(t, 0, sizeof(struct smbios_type3));
232 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
233 t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer");
234 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
235 t->bootup_state = SMBIOS_STATE_SAFE;
236 t->power_supply_state = SMBIOS_STATE_SAFE;
237 t->thermal_state = SMBIOS_STATE_SAFE;
238 t->security_status = SMBIOS_SECURITY_NONE;
240 len = t->length + smbios_string_table_len(t->eos);
247 static void smbios_write_type4_dm(struct smbios_type4 *t,
248 struct smbios_ctx *ctx)
250 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
251 const char *vendor = "Unknown";
252 const char *name = "Unknown";
255 char processor_name[49];
256 char vendor_name[49];
257 struct udevice *cpu = NULL;
259 uclass_find_first_device(UCLASS_CPU, &cpu);
261 struct cpu_plat *plat = dev_get_parent_plat(cpu);
264 processor_family = plat->family;
265 t->processor_id[0] = plat->id[0];
266 t->processor_id[1] = plat->id[1];
268 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
269 vendor = vendor_name;
270 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
271 name = processor_name;
275 t->processor_family = processor_family;
276 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
277 t->processor_version = smbios_add_string(t->eos, name);
280 static int smbios_write_type4(ulong *current, int handle,
281 struct smbios_ctx *ctx)
283 struct smbios_type4 *t;
284 int len = sizeof(struct smbios_type4);
286 t = map_sysmem(*current, len);
287 memset(t, 0, sizeof(struct smbios_type4));
288 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
289 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
290 smbios_write_type4_dm(t, ctx);
291 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
292 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
293 t->l1_cache_handle = 0xffff;
294 t->l2_cache_handle = 0xffff;
295 t->l3_cache_handle = 0xffff;
296 t->processor_family2 = t->processor_family;
298 len = t->length + smbios_string_table_len(t->eos);
305 static int smbios_write_type32(ulong *current, int handle,
306 struct smbios_ctx *ctx)
308 struct smbios_type32 *t;
309 int len = sizeof(struct smbios_type32);
311 t = map_sysmem(*current, len);
312 memset(t, 0, sizeof(struct smbios_type32));
313 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
321 static int smbios_write_type127(ulong *current, int handle,
322 struct smbios_ctx *ctx)
324 struct smbios_type127 *t;
325 int len = sizeof(struct smbios_type127);
327 t = map_sysmem(*current, len);
328 memset(t, 0, sizeof(struct smbios_type127));
329 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
337 static struct smbios_write_method smbios_write_funcs[] = {
338 { smbios_write_type0, },
339 { smbios_write_type1, "system", },
340 { smbios_write_type2, "baseboard", },
341 { smbios_write_type3, "chassis", },
342 { smbios_write_type4, },
343 { smbios_write_type32, },
344 { smbios_write_type127 },
347 ulong write_smbios_table(ulong addr)
349 ofnode parent_node = ofnode_null();
350 struct smbios_entry *se;
351 struct smbios_ctx ctx;
355 int max_struct_size = 0;
361 ctx.node = ofnode_null();
362 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
363 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
365 parent_node = dev_read_subnode(ctx.dev, "smbios");
370 /* 16 byte align the table address */
371 addr = ALIGN(addr, 16);
373 se = map_sysmem(addr, sizeof(struct smbios_entry));
374 memset(se, 0, sizeof(struct smbios_entry));
376 addr += sizeof(struct smbios_entry);
377 addr = ALIGN(addr, 16);
380 /* populate minimum required tables */
381 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
382 const struct smbios_write_method *method;
385 method = &smbios_write_funcs[i];
386 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
387 ctx.node = ofnode_find_subnode(parent_node,
388 method->subnode_name);
389 tmp = method->write((ulong *)&addr, handle++, &ctx);
391 max_struct_size = max(max_struct_size, tmp);
395 memcpy(se->anchor, "_SM_", 4);
396 se->length = sizeof(struct smbios_entry);
397 se->major_ver = SMBIOS_MAJOR_VER;
398 se->minor_ver = SMBIOS_MINOR_VER;
399 se->max_struct_size = max_struct_size;
400 memcpy(se->intermediate_anchor, "_DMI_", 5);
401 se->struct_table_length = len;
404 * We must use a pointer here so things work correctly on sandbox. The
405 * user of this table is not aware of the mapping of addresses to
406 * sandbox's DRAM buffer.
408 table_addr = (ulong)map_sysmem(tables, 0);
409 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
411 * We need to put this >32-bit pointer into the table but the
412 * field is only 32 bits wide.
414 printf("WARNING: SMBIOS table_address overflow %llx\n",
415 (unsigned long long)table_addr);
418 se->struct_table_address = table_addr;
420 se->struct_count = handle;
422 /* calculate checksums */
423 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
424 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
425 se->intermediate_checksum = table_compute_checksum(istart, isize);
426 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));