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 * Function prototype to write a specific type of SMBIOS structure
23 * @addr: start address to write the structure
24 * @handle: the structure's handle, a unique 16-bit number
25 * @node: node containing the information to write (ofnode_null() if none)
26 * @return: size of the structure
28 typedef int (*smbios_write_type)(ulong *addr, int handle, ofnode node);
31 * struct smbios_write_method - Information about a table-writing function
33 * @write: Function to call
34 * @subnode_name: Name of subnode which has the information for this function,
37 struct smbios_write_method {
38 smbios_write_type write;
39 const char *subnode_name;
43 * smbios_add_string() - add a string to the string area
45 * This adds a string to the string area which is appended directly after
46 * the formatted portion of an SMBIOS structure.
48 * @start: string area start address
50 * @return: string number in the string area (1 or more)
52 static int smbios_add_string(char *start, const char *str)
78 * smbios_add_prop() - Add a property from the device tree
80 * @start: string area start address
81 * @node: node containing the information to write (ofnode_null() if none)
82 * @prop: property to write
83 * @return 0 if not found, else SMBIOS string number (1 or more)
85 static int smbios_add_prop(char *start, ofnode node, const char *prop)
88 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
91 str = ofnode_read_string(node, prop);
93 return smbios_add_string(start, str);
100 * smbios_string_table_len() - compute the string area size
102 * This computes the size of the string area including the string terminator.
104 * @start: string area start address
105 * @return: string area size
107 static int smbios_string_table_len(char *start)
121 static int smbios_write_type0(ulong *current, int handle, ofnode node)
123 struct smbios_type0 *t;
124 int len = sizeof(struct smbios_type0);
126 t = map_sysmem(*current, len);
127 memset(t, 0, sizeof(struct smbios_type0));
128 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
129 t->vendor = smbios_add_string(t->eos, "U-Boot");
130 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
131 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
132 #ifdef CONFIG_ROM_SIZE
133 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
135 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
136 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
137 BIOS_CHARACTERISTICS_UPGRADEABLE;
138 #ifdef CONFIG_GENERATE_ACPI_TABLE
139 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
141 #ifdef CONFIG_EFI_LOADER
142 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
144 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
146 t->bios_major_release = 0xff;
147 t->bios_minor_release = 0xff;
148 t->ec_major_release = 0xff;
149 t->ec_minor_release = 0xff;
151 len = t->length + smbios_string_table_len(t->eos);
158 static int smbios_write_type1(ulong *current, int handle, ofnode node)
160 struct smbios_type1 *t;
161 int len = sizeof(struct smbios_type1);
162 char *serial_str = env_get("serial#");
164 t = map_sysmem(*current, len);
165 memset(t, 0, sizeof(struct smbios_type1));
166 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
167 t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
168 t->product_name = smbios_add_prop(t->eos, node, "product");
169 t->version = smbios_add_prop(t->eos, node, "version");
171 t->serial_number = smbios_add_string(t->eos, serial_str);
172 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
174 t->serial_number = smbios_add_prop(t->eos, node, "serial");
176 t->sku_number = smbios_add_prop(t->eos, node, "sku");
177 t->family = smbios_add_prop(t->eos, node, "family");
179 len = t->length + smbios_string_table_len(t->eos);
186 static int smbios_write_type2(ulong *current, int handle, ofnode node)
188 struct smbios_type2 *t;
189 int len = sizeof(struct smbios_type2);
191 t = map_sysmem(*current, len);
192 memset(t, 0, sizeof(struct smbios_type2));
193 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
194 t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
195 t->product_name = smbios_add_prop(t->eos, node, "product");
196 t->asset_tag_number = smbios_add_prop(t->eos, node, "asset-tag");
197 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
198 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
200 len = t->length + smbios_string_table_len(t->eos);
207 static int smbios_write_type3(ulong *current, int handle, ofnode node)
209 struct smbios_type3 *t;
210 int len = sizeof(struct smbios_type3);
212 t = map_sysmem(*current, len);
213 memset(t, 0, sizeof(struct smbios_type3));
214 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
215 t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
216 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
217 t->bootup_state = SMBIOS_STATE_SAFE;
218 t->power_supply_state = SMBIOS_STATE_SAFE;
219 t->thermal_state = SMBIOS_STATE_SAFE;
220 t->security_status = SMBIOS_SECURITY_NONE;
222 len = t->length + smbios_string_table_len(t->eos);
229 static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
231 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
232 const char *vendor = "Unknown";
233 const char *name = "Unknown";
236 char processor_name[49];
237 char vendor_name[49];
238 struct udevice *cpu = NULL;
240 uclass_find_first_device(UCLASS_CPU, &cpu);
242 struct cpu_plat *plat = dev_get_parent_plat(cpu);
245 processor_family = plat->family;
246 t->processor_id[0] = plat->id[0];
247 t->processor_id[1] = plat->id[1];
249 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
250 vendor = vendor_name;
251 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
252 name = processor_name;
256 t->processor_family = processor_family;
257 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
258 t->processor_version = smbios_add_string(t->eos, name);
261 static int smbios_write_type4(ulong *current, int handle, ofnode node)
263 struct smbios_type4 *t;
264 int len = sizeof(struct smbios_type4);
266 t = map_sysmem(*current, len);
267 memset(t, 0, sizeof(struct smbios_type4));
268 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
269 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
270 smbios_write_type4_dm(t, node);
271 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
272 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
273 t->l1_cache_handle = 0xffff;
274 t->l2_cache_handle = 0xffff;
275 t->l3_cache_handle = 0xffff;
276 t->processor_family2 = t->processor_family;
278 len = t->length + smbios_string_table_len(t->eos);
285 static int smbios_write_type32(ulong *current, int handle, ofnode node)
287 struct smbios_type32 *t;
288 int len = sizeof(struct smbios_type32);
290 t = map_sysmem(*current, len);
291 memset(t, 0, sizeof(struct smbios_type32));
292 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
300 static int smbios_write_type127(ulong *current, int handle, ofnode node)
302 struct smbios_type127 *t;
303 int len = sizeof(struct smbios_type127);
305 t = map_sysmem(*current, len);
306 memset(t, 0, sizeof(struct smbios_type127));
307 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
315 static struct smbios_write_method smbios_write_funcs[] = {
316 { smbios_write_type0, },
317 { smbios_write_type1, "system", },
318 { smbios_write_type2, "baseboard", },
319 { smbios_write_type3, "chassis", },
320 { smbios_write_type4, },
321 { smbios_write_type32, },
322 { smbios_write_type127 },
325 ulong write_smbios_table(ulong addr)
327 ofnode parent_node = ofnode_null();
328 struct smbios_entry *se;
333 int max_struct_size = 0;
339 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
340 uclass_first_device(UCLASS_SYSINFO, &dev);
342 parent_node = dev_read_subnode(dev, "smbios");
345 /* 16 byte align the table address */
346 addr = ALIGN(addr, 16);
348 se = map_sysmem(addr, sizeof(struct smbios_entry));
349 memset(se, 0, sizeof(struct smbios_entry));
351 addr += sizeof(struct smbios_entry);
352 addr = ALIGN(addr, 16);
355 /* populate minimum required tables */
356 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
357 const struct smbios_write_method *method;
358 ofnode node = ofnode_null();
361 method = &smbios_write_funcs[i];
362 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
363 node = ofnode_find_subnode(parent_node,
364 method->subnode_name);
365 tmp = method->write((ulong *)&addr, handle++, node);
367 max_struct_size = max(max_struct_size, tmp);
371 memcpy(se->anchor, "_SM_", 4);
372 se->length = sizeof(struct smbios_entry);
373 se->major_ver = SMBIOS_MAJOR_VER;
374 se->minor_ver = SMBIOS_MINOR_VER;
375 se->max_struct_size = max_struct_size;
376 memcpy(se->intermediate_anchor, "_DMI_", 5);
377 se->struct_table_length = len;
380 * We must use a pointer here so things work correctly on sandbox. The
381 * user of this table is not aware of the mapping of addresses to
382 * sandbox's DRAM buffer.
384 table_addr = (ulong)map_sysmem(tables, 0);
385 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
387 * We need to put this >32-bit pointer into the table but the
388 * field is only 32 bits wide.
390 printf("WARNING: SMBIOS table_address overflow %llx\n",
391 (unsigned long long)table_addr);
394 se->struct_table_address = table_addr;
396 se->struct_count = handle;
398 /* calculate checksums */
399 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
400 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
401 se->intermediate_checksum = table_compute_checksum(istart, isize);
402 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));