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)
25 * @eos: end-of-string pointer for the table being processed. This is set
26 * up when we start processing a table
27 * @next_ptr: pointer to the start of the next string to be added. When the
28 * table is nopt empty, this points to the byte after the \0 of the
39 * Function prototype to write a specific type of SMBIOS structure
41 * @addr: start address to write the structure
42 * @handle: the structure's handle, a unique 16-bit number
43 * @ctx: context for writing the tables
44 * @return: size of the structure
46 typedef int (*smbios_write_type)(ulong *addr, int handle,
47 struct smbios_ctx *ctx);
50 * struct smbios_write_method - Information about a table-writing function
52 * @write: Function to call
53 * @subnode_name: Name of subnode which has the information for this function,
56 struct smbios_write_method {
57 smbios_write_type write;
58 const char *subnode_name;
62 * smbios_add_string() - add a string to the string area
64 * This adds a string to the string area which is appended directly after
65 * the formatted portion of an SMBIOS structure.
67 * @ctx: SMBIOS context
69 * @return: string number in the string area (1 or more)
71 static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
99 * smbios_add_prop() - Add a property from the device tree
101 * @ctx: context for writing the tables
102 * @prop: property to write
103 * @return 0 if not found, else SMBIOS string number (1 or more)
105 static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
107 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
110 str = ofnode_read_string(ctx->node, prop);
112 return smbios_add_string(ctx, str);
118 static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
125 * smbios_string_table_len() - compute the string area size
127 * This computes the size of the string area including the string terminator.
129 * @ctx: SMBIOS context
130 * @return: string area size
132 static int smbios_string_table_len(const struct smbios_ctx *ctx)
134 /* Allow for the final \0 after all strings */
135 return (ctx->next_ptr + 1) - ctx->eos;
138 static int smbios_write_type0(ulong *current, int handle,
139 struct smbios_ctx *ctx)
141 struct smbios_type0 *t;
142 int len = sizeof(struct smbios_type0);
144 t = map_sysmem(*current, len);
145 memset(t, 0, sizeof(struct smbios_type0));
146 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
147 smbios_set_eos(ctx, t->eos);
148 t->vendor = smbios_add_string(ctx, "U-Boot");
149 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
150 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
151 #ifdef CONFIG_ROM_SIZE
152 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
154 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
155 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
156 BIOS_CHARACTERISTICS_UPGRADEABLE;
157 #ifdef CONFIG_GENERATE_ACPI_TABLE
158 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
160 #ifdef CONFIG_EFI_LOADER
161 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
163 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
165 /* bios_major_release has only one byte, so drop century */
166 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
167 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
168 t->ec_major_release = 0xff;
169 t->ec_minor_release = 0xff;
171 len = t->length + smbios_string_table_len(ctx);
178 static int smbios_write_type1(ulong *current, int handle,
179 struct smbios_ctx *ctx)
181 struct smbios_type1 *t;
182 int len = sizeof(struct smbios_type1);
183 char *serial_str = env_get("serial#");
185 t = map_sysmem(*current, len);
186 memset(t, 0, sizeof(struct smbios_type1));
187 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
188 smbios_set_eos(ctx, t->eos);
189 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
190 t->product_name = smbios_add_prop(ctx, "product");
191 t->version = smbios_add_prop(ctx, "version");
193 t->serial_number = smbios_add_string(ctx, serial_str);
194 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
196 t->serial_number = smbios_add_prop(ctx, "serial");
198 t->sku_number = smbios_add_prop(ctx, "sku");
199 t->family = smbios_add_prop(ctx, "family");
201 len = t->length + smbios_string_table_len(ctx);
208 static int smbios_write_type2(ulong *current, int handle,
209 struct smbios_ctx *ctx)
211 struct smbios_type2 *t;
212 int len = sizeof(struct smbios_type2);
214 t = map_sysmem(*current, len);
215 memset(t, 0, sizeof(struct smbios_type2));
216 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
217 smbios_set_eos(ctx, t->eos);
218 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
219 t->product_name = smbios_add_prop(ctx, "product");
220 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
221 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
222 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
224 len = t->length + smbios_string_table_len(ctx);
231 static int smbios_write_type3(ulong *current, int handle,
232 struct smbios_ctx *ctx)
234 struct smbios_type3 *t;
235 int len = sizeof(struct smbios_type3);
237 t = map_sysmem(*current, len);
238 memset(t, 0, sizeof(struct smbios_type3));
239 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
240 smbios_set_eos(ctx, t->eos);
241 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
242 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
243 t->bootup_state = SMBIOS_STATE_SAFE;
244 t->power_supply_state = SMBIOS_STATE_SAFE;
245 t->thermal_state = SMBIOS_STATE_SAFE;
246 t->security_status = SMBIOS_SECURITY_NONE;
248 len = t->length + smbios_string_table_len(ctx);
255 static void smbios_write_type4_dm(struct smbios_type4 *t,
256 struct smbios_ctx *ctx)
258 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
259 const char *vendor = "Unknown";
260 const char *name = "Unknown";
263 char processor_name[49];
264 char vendor_name[49];
265 struct udevice *cpu = NULL;
267 uclass_find_first_device(UCLASS_CPU, &cpu);
269 struct cpu_plat *plat = dev_get_parent_plat(cpu);
272 processor_family = plat->family;
273 t->processor_id[0] = plat->id[0];
274 t->processor_id[1] = plat->id[1];
276 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
277 vendor = vendor_name;
278 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
279 name = processor_name;
283 t->processor_family = processor_family;
284 t->processor_manufacturer = smbios_add_string(ctx, vendor);
285 t->processor_version = smbios_add_string(ctx, name);
288 static int smbios_write_type4(ulong *current, int handle,
289 struct smbios_ctx *ctx)
291 struct smbios_type4 *t;
292 int len = sizeof(struct smbios_type4);
294 t = map_sysmem(*current, len);
295 memset(t, 0, sizeof(struct smbios_type4));
296 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
297 smbios_set_eos(ctx, t->eos);
298 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
299 smbios_write_type4_dm(t, ctx);
300 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
301 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
302 t->l1_cache_handle = 0xffff;
303 t->l2_cache_handle = 0xffff;
304 t->l3_cache_handle = 0xffff;
305 t->processor_family2 = t->processor_family;
307 len = t->length + smbios_string_table_len(ctx);
314 static int smbios_write_type32(ulong *current, int handle,
315 struct smbios_ctx *ctx)
317 struct smbios_type32 *t;
318 int len = sizeof(struct smbios_type32);
320 t = map_sysmem(*current, len);
321 memset(t, 0, sizeof(struct smbios_type32));
322 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
323 smbios_set_eos(ctx, t->eos);
331 static int smbios_write_type127(ulong *current, int handle,
332 struct smbios_ctx *ctx)
334 struct smbios_type127 *t;
335 int len = sizeof(struct smbios_type127);
337 t = map_sysmem(*current, len);
338 memset(t, 0, sizeof(struct smbios_type127));
339 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
347 static struct smbios_write_method smbios_write_funcs[] = {
348 { smbios_write_type0, },
349 { smbios_write_type1, "system", },
350 { smbios_write_type2, "baseboard", },
351 { smbios_write_type3, "chassis", },
352 { smbios_write_type4, },
353 { smbios_write_type32, },
354 { smbios_write_type127 },
357 ulong write_smbios_table(ulong addr)
359 ofnode parent_node = ofnode_null();
360 struct smbios_entry *se;
361 struct smbios_ctx ctx;
365 int max_struct_size = 0;
371 ctx.node = ofnode_null();
372 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
373 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
375 parent_node = dev_read_subnode(ctx.dev, "smbios");
380 /* 16 byte align the table address */
381 addr = ALIGN(addr, 16);
383 se = map_sysmem(addr, sizeof(struct smbios_entry));
384 memset(se, 0, sizeof(struct smbios_entry));
386 addr += sizeof(struct smbios_entry);
387 addr = ALIGN(addr, 16);
390 /* populate minimum required tables */
391 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
392 const struct smbios_write_method *method;
395 method = &smbios_write_funcs[i];
396 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
397 ctx.node = ofnode_find_subnode(parent_node,
398 method->subnode_name);
399 tmp = method->write((ulong *)&addr, handle++, &ctx);
401 max_struct_size = max(max_struct_size, tmp);
405 memcpy(se->anchor, "_SM_", 4);
406 se->length = sizeof(struct smbios_entry);
407 se->major_ver = SMBIOS_MAJOR_VER;
408 se->minor_ver = SMBIOS_MINOR_VER;
409 se->max_struct_size = max_struct_size;
410 memcpy(se->intermediate_anchor, "_DMI_", 5);
411 se->struct_table_length = len;
414 * We must use a pointer here so things work correctly on sandbox. The
415 * user of this table is not aware of the mapping of addresses to
416 * sandbox's DRAM buffer.
418 table_addr = (ulong)map_sysmem(tables, 0);
419 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
421 * We need to put this >32-bit pointer into the table but the
422 * field is only 32 bits wide.
424 printf("WARNING: SMBIOS table_address overflow %llx\n",
425 (unsigned long long)table_addr);
428 se->struct_table_address = table_addr;
430 se->struct_count = handle;
432 /* calculate checksums */
433 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
434 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
435 se->intermediate_checksum = table_compute_checksum(istart, isize);
436 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));