blob: a52a9d5b307725b012bb5541e85e27c531d577b8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Mengae5bedb2015-10-12 05:23:41 -07002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * Adapted from coreboot src/arch/x86/smbios.c
Bin Mengae5bedb2015-10-12 05:23:41 -07006 */
7
8#include <common.h>
Simon Glass0dd7ca22020-11-05 06:32:07 -07009#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Simon Glass2d93add2018-11-22 13:46:37 -070011#include <mapmem.h>
Alexander Graffb228082016-08-19 01:23:23 +020012#include <smbios.h>
13#include <tables_csum.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070014#include <version.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020015#ifdef CONFIG_CPU
16#include <cpu.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020017#include <dm/uclass-internal.h>
18#endif
Bin Mengae5bedb2015-10-12 05:23:41 -070019
Bin Mengae5bedb2015-10-12 05:23:41 -070020/**
Simon Glass6f3332a2020-11-05 06:32:08 -070021 * struct smbios_write_method - Information about a table-writing function
22 *
23 * @write: Function to call
24 * @subnode_name: Name of subnode which has the information for this function,
25 * NULL if none
26 */
27struct smbios_write_method {
28 smbios_write_type write;
29 const char *subnode_name;
30};
31
32/**
Bin Mengae5bedb2015-10-12 05:23:41 -070033 * smbios_add_string() - add a string to the string area
34 *
35 * This adds a string to the string area which is appended directly after
36 * the formatted portion of an SMBIOS structure.
37 *
38 * @start: string area start address
39 * @str: string to add
Simon Glass0dd7ca22020-11-05 06:32:07 -070040 * @return: string number in the string area (1 or more)
Bin Mengae5bedb2015-10-12 05:23:41 -070041 */
42static int smbios_add_string(char *start, const char *str)
43{
44 int i = 1;
45 char *p = start;
Heinrich Schuchardt20dcde42020-06-01 15:44:00 +020046 if (!*str)
47 str = "Unknown";
Bin Mengae5bedb2015-10-12 05:23:41 -070048
49 for (;;) {
50 if (!*p) {
51 strcpy(p, str);
52 p += strlen(str);
53 *p++ = '\0';
54 *p++ = '\0';
55
56 return i;
57 }
58
59 if (!strcmp(p, str))
60 return i;
61
62 p += strlen(p) + 1;
63 i++;
64 }
65}
66
67/**
Simon Glass6f3332a2020-11-05 06:32:08 -070068 * smbios_add_prop_default() - Add a property from the device tree or default
69 *
70 * @start: string area start address
71 * @node: node containing the information to write (ofnode_null() if none)
72 * @prop: property to write
73 * @def: default string if the node has no such property
74 * @return 0 if not found, else SMBIOS string number (1 or more)
75 */
76static int smbios_add_prop_default(char *start, ofnode node, const char *prop,
77 const char *def)
78{
79 const char *str = NULL;
80
81 if (IS_ENABLED(CONFIG_OF_CONTROL))
82 str = ofnode_read_string(node, prop);
83 if (str)
84 return smbios_add_string(start, str);
85 else if (def)
86 return smbios_add_string(start, def);
87
88 return 0;
89}
90
91/**
92 * smbios_add_prop() - Add a property from the device tree
93 *
94 * @start: string area start address
95 * @node: node containing the information to write (ofnode_null() if none)
96 * @prop: property to write
97 * @return 0 if not found, else SMBIOS string number (1 or more)
98 */
99static int smbios_add_prop(char *start, ofnode node, const char *prop)
100{
101 return smbios_add_prop_default(start, node, prop, NULL);
102}
103
104/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700105 * smbios_string_table_len() - compute the string area size
106 *
107 * This computes the size of the string area including the string terminator.
108 *
109 * @start: string area start address
110 * @return: string area size
111 */
112static int smbios_string_table_len(char *start)
113{
114 char *p = start;
115 int i, len = 0;
116
117 while (*p) {
118 i = strlen(p) + 1;
119 p += i;
120 len += i;
121 }
122
123 return len + 1;
124}
125
Simon Glass0dd7ca22020-11-05 06:32:07 -0700126static int smbios_write_type0(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700127{
Simon Glass2d93add2018-11-22 13:46:37 -0700128 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700129 int len = sizeof(struct smbios_type0);
130
Simon Glass2d93add2018-11-22 13:46:37 -0700131 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700132 memset(t, 0, sizeof(struct smbios_type0));
133 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
134 t->vendor = smbios_add_string(t->eos, "U-Boot");
135 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
136 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200137#ifdef CONFIG_ROM_SIZE
Bin Mengae5bedb2015-10-12 05:23:41 -0700138 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Graf66f96e12016-08-19 01:23:29 +0200139#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700140 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
141 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
142 BIOS_CHARACTERISTICS_UPGRADEABLE;
143#ifdef CONFIG_GENERATE_ACPI_TABLE
144 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
145#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200146#ifdef CONFIG_EFI_LOADER
147 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
148#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700149 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200150
Bin Mengae5bedb2015-10-12 05:23:41 -0700151 t->bios_major_release = 0xff;
152 t->bios_minor_release = 0xff;
153 t->ec_major_release = 0xff;
154 t->ec_minor_release = 0xff;
155
156 len = t->length + smbios_string_table_len(t->eos);
157 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700158 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700159
160 return len;
161}
162
Simon Glass0dd7ca22020-11-05 06:32:07 -0700163static int smbios_write_type1(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700164{
Simon Glass2d93add2018-11-22 13:46:37 -0700165 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700166 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600167 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700168
Simon Glass2d93add2018-11-22 13:46:37 -0700169 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700170 memset(t, 0, sizeof(struct smbios_type1));
171 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass6f3332a2020-11-05 06:32:08 -0700172 t->manufacturer = smbios_add_prop_default(t->eos, node, "manufacturer",
173 CONFIG_SMBIOS_MANUFACTURER);
174 t->product_name = smbios_add_prop_default(t->eos, node, "product",
175 CONFIG_SMBIOS_PRODUCT_NAME);
Simon Glassbc14e152020-11-05 06:32:09 -0700176 t->version = smbios_add_prop(t->eos, node, "version");
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200177 if (serial_str) {
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200178 t->serial_number = smbios_add_string(t->eos, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700179 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
180 } else {
181 t->serial_number = smbios_add_prop(t->eos, node, "serial");
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200182 }
Simon Glassbc14e152020-11-05 06:32:09 -0700183 t->sku_number = smbios_add_prop(t->eos, node, "sku");
184 t->family = smbios_add_prop(t->eos, node, "family");
Bin Mengae5bedb2015-10-12 05:23:41 -0700185
186 len = t->length + smbios_string_table_len(t->eos);
187 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700188 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700189
190 return len;
191}
192
Simon Glass0dd7ca22020-11-05 06:32:07 -0700193static int smbios_write_type2(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700194{
Simon Glass2d93add2018-11-22 13:46:37 -0700195 struct smbios_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700196 int len = sizeof(struct smbios_type2);
197
Simon Glass2d93add2018-11-22 13:46:37 -0700198 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700199 memset(t, 0, sizeof(struct smbios_type2));
200 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass6f3332a2020-11-05 06:32:08 -0700201 t->manufacturer = smbios_add_prop_default(t->eos, node, "manufacturer",
202 CONFIG_SMBIOS_MANUFACTURER);
203 t->product_name = smbios_add_prop_default(t->eos, node, "product",
204 CONFIG_SMBIOS_PRODUCT_NAME);
Simon Glassbc14e152020-11-05 06:32:09 -0700205 t->asset_tag_number = smbios_add_prop(t->eos, node, "asset-tag");
Bin Mengae5bedb2015-10-12 05:23:41 -0700206 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
207 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
208
209 len = t->length + smbios_string_table_len(t->eos);
210 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700211 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700212
213 return len;
214}
215
Simon Glass0dd7ca22020-11-05 06:32:07 -0700216static int smbios_write_type3(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700217{
Simon Glass2d93add2018-11-22 13:46:37 -0700218 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700219 int len = sizeof(struct smbios_type3);
220
Simon Glass2d93add2018-11-22 13:46:37 -0700221 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700222 memset(t, 0, sizeof(struct smbios_type3));
223 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass6f3332a2020-11-05 06:32:08 -0700224 t->manufacturer = smbios_add_prop_default(t->eos, node, "manufacturer",
225 CONFIG_SMBIOS_MANUFACTURER);
Bin Mengae5bedb2015-10-12 05:23:41 -0700226 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
227 t->bootup_state = SMBIOS_STATE_SAFE;
228 t->power_supply_state = SMBIOS_STATE_SAFE;
229 t->thermal_state = SMBIOS_STATE_SAFE;
230 t->security_status = SMBIOS_SECURITY_NONE;
231
232 len = t->length + smbios_string_table_len(t->eos);
233 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700234 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700235
236 return len;
237}
238
Simon Glass0dd7ca22020-11-05 06:32:07 -0700239static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200240{
241 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
242 const char *vendor = "Unknown";
243 const char *name = "Unknown";
244
245#ifdef CONFIG_CPU
246 char processor_name[49];
247 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700248 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200249
Simon Glass0dd7ca22020-11-05 06:32:07 -0700250 uclass_find_first_device(UCLASS_CPU, &cpu);
251 if (cpu) {
252 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200253
254 if (plat->family)
255 processor_family = plat->family;
256 t->processor_id[0] = plat->id[0];
257 t->processor_id[1] = plat->id[1];
258
Simon Glass0dd7ca22020-11-05 06:32:07 -0700259 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200260 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700261 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200262 name = processor_name;
263 }
264#endif
265
266 t->processor_family = processor_family;
267 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
268 t->processor_version = smbios_add_string(t->eos, name);
269}
270
Simon Glass0dd7ca22020-11-05 06:32:07 -0700271static int smbios_write_type4(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700272{
Simon Glass2d93add2018-11-22 13:46:37 -0700273 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700274 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700275
Simon Glass2d93add2018-11-22 13:46:37 -0700276 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700277 memset(t, 0, sizeof(struct smbios_type4));
278 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
279 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700280 smbios_write_type4_dm(t, node);
Bin Mengae5bedb2015-10-12 05:23:41 -0700281 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
282 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
283 t->l1_cache_handle = 0xffff;
284 t->l2_cache_handle = 0xffff;
285 t->l3_cache_handle = 0xffff;
286 t->processor_family2 = t->processor_family;
287
288 len = t->length + smbios_string_table_len(t->eos);
289 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700290 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700291
292 return len;
293}
294
Simon Glass0dd7ca22020-11-05 06:32:07 -0700295static int smbios_write_type32(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700296{
Simon Glass2d93add2018-11-22 13:46:37 -0700297 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700298 int len = sizeof(struct smbios_type32);
299
Simon Glass2d93add2018-11-22 13:46:37 -0700300 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700301 memset(t, 0, sizeof(struct smbios_type32));
302 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
303
304 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700305 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700306
307 return len;
308}
309
Simon Glass0dd7ca22020-11-05 06:32:07 -0700310static int smbios_write_type127(ulong *current, int handle, ofnode node)
Bin Mengae5bedb2015-10-12 05:23:41 -0700311{
Simon Glass2d93add2018-11-22 13:46:37 -0700312 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700313 int len = sizeof(struct smbios_type127);
314
Simon Glass2d93add2018-11-22 13:46:37 -0700315 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700316 memset(t, 0, sizeof(struct smbios_type127));
317 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
318
319 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700320 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700321
322 return len;
323}
324
Simon Glass6f3332a2020-11-05 06:32:08 -0700325static struct smbios_write_method smbios_write_funcs[] = {
326 { smbios_write_type0, },
327 { smbios_write_type1, "system", },
328 { smbios_write_type2, "baseboard", },
329 { smbios_write_type3, "chassis", },
330 { smbios_write_type4, },
331 { smbios_write_type32, },
332 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700333};
334
Simon Glassca37a392017-01-16 07:03:35 -0700335ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700336{
Simon Glass6f3332a2020-11-05 06:32:08 -0700337 ofnode parent_node = ofnode_null();
Bin Mengae5bedb2015-10-12 05:23:41 -0700338 struct smbios_entry *se;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700339 struct udevice *dev;
Simon Glass2d93add2018-11-22 13:46:37 -0700340 ulong table_addr;
Simon Glassca37a392017-01-16 07:03:35 -0700341 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700342 int len = 0;
343 int max_struct_size = 0;
344 int handle = 0;
345 char *istart;
346 int isize;
347 int i;
348
Simon Glass0dd7ca22020-11-05 06:32:07 -0700349 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
350 uclass_first_device(UCLASS_SYSINFO, &dev);
351 if (dev)
Simon Glass6f3332a2020-11-05 06:32:08 -0700352 parent_node = dev_read_subnode(dev, "smbios");
Simon Glass0dd7ca22020-11-05 06:32:07 -0700353 }
354
Bin Mengae5bedb2015-10-12 05:23:41 -0700355 /* 16 byte align the table address */
356 addr = ALIGN(addr, 16);
357
Simon Glass2d93add2018-11-22 13:46:37 -0700358 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Mengae5bedb2015-10-12 05:23:41 -0700359 memset(se, 0, sizeof(struct smbios_entry));
360
361 addr += sizeof(struct smbios_entry);
362 addr = ALIGN(addr, 16);
363 tables = addr;
364
365 /* populate minimum required tables */
366 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700367 const struct smbios_write_method *method;
368 ofnode node = ofnode_null();
369 int tmp;
370
371 method = &smbios_write_funcs[i];
372 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
373 node = ofnode_find_subnode(parent_node,
374 method->subnode_name);
375 tmp = method->write((ulong *)&addr, handle++, node);
Christian Gmeiner5566caf2018-07-30 13:22:07 +0200376
Bin Mengae5bedb2015-10-12 05:23:41 -0700377 max_struct_size = max(max_struct_size, tmp);
378 len += tmp;
379 }
380
381 memcpy(se->anchor, "_SM_", 4);
382 se->length = sizeof(struct smbios_entry);
383 se->major_ver = SMBIOS_MAJOR_VER;
384 se->minor_ver = SMBIOS_MINOR_VER;
385 se->max_struct_size = max_struct_size;
386 memcpy(se->intermediate_anchor, "_DMI_", 5);
387 se->struct_table_length = len;
Simon Glass2d93add2018-11-22 13:46:37 -0700388
389 /*
390 * We must use a pointer here so things work correctly on sandbox. The
391 * user of this table is not aware of the mapping of addresses to
392 * sandbox's DRAM buffer.
393 */
394 table_addr = (ulong)map_sysmem(tables, 0);
395 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
396 /*
397 * We need to put this >32-bit pointer into the table but the
398 * field is only 32 bits wide.
399 */
400 printf("WARNING: SMBIOS table_address overflow %llx\n",
401 (unsigned long long)table_addr);
402 table_addr = 0;
403 }
404 se->struct_table_address = table_addr;
405
Bin Mengae5bedb2015-10-12 05:23:41 -0700406 se->struct_count = handle;
407
408 /* calculate checksums */
409 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
410 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
411 se->intermediate_checksum = table_compute_checksum(istart, isize);
412 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glass2d93add2018-11-22 13:46:37 -0700413 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700414
415 return addr;
416}