blob: b0f5e93604483692c0de22708de69a2089101dbc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng721e9922015-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 Meng721e9922015-10-12 05:23:41 -07006 */
7
8#include <common.h>
Simon Glass78227d42020-11-05 06:32:07 -07009#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Simon Glassa2505fc2018-11-22 13:46:37 -070011#include <mapmem.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020012#include <smbios.h>
13#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070014#include <version.h>
Alexander Graf96476202016-08-19 01:23:28 +020015#ifdef CONFIG_CPU
16#include <cpu.h>
Alexander Graf96476202016-08-19 01:23:28 +020017#include <dm/uclass-internal.h>
18#endif
Bin Meng721e9922015-10-12 05:23:41 -070019
Bin Meng721e9922015-10-12 05:23:41 -070020/**
21 * smbios_add_string() - add a string to the string area
22 *
23 * This adds a string to the string area which is appended directly after
24 * the formatted portion of an SMBIOS structure.
25 *
26 * @start: string area start address
27 * @str: string to add
Simon Glass78227d42020-11-05 06:32:07 -070028 * @return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -070029 */
30static int smbios_add_string(char *start, const char *str)
31{
32 int i = 1;
33 char *p = start;
Heinrich Schuchardt00a871d2020-06-01 15:44:00 +020034 if (!*str)
35 str = "Unknown";
Bin Meng721e9922015-10-12 05:23:41 -070036
37 for (;;) {
38 if (!*p) {
39 strcpy(p, str);
40 p += strlen(str);
41 *p++ = '\0';
42 *p++ = '\0';
43
44 return i;
45 }
46
47 if (!strcmp(p, str))
48 return i;
49
50 p += strlen(p) + 1;
51 i++;
52 }
53}
54
55/**
56 * smbios_string_table_len() - compute the string area size
57 *
58 * This computes the size of the string area including the string terminator.
59 *
60 * @start: string area start address
61 * @return: string area size
62 */
63static int smbios_string_table_len(char *start)
64{
65 char *p = start;
66 int i, len = 0;
67
68 while (*p) {
69 i = strlen(p) + 1;
70 p += i;
71 len += i;
72 }
73
74 return len + 1;
75}
76
Simon Glass78227d42020-11-05 06:32:07 -070077static int smbios_write_type0(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -070078{
Simon Glassa2505fc2018-11-22 13:46:37 -070079 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -070080 int len = sizeof(struct smbios_type0);
81
Simon Glassa2505fc2018-11-22 13:46:37 -070082 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -070083 memset(t, 0, sizeof(struct smbios_type0));
84 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
85 t->vendor = smbios_add_string(t->eos, "U-Boot");
86 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
87 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +020088#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -070089 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +020090#endif
Bin Meng721e9922015-10-12 05:23:41 -070091 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
92 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
93 BIOS_CHARACTERISTICS_UPGRADEABLE;
94#ifdef CONFIG_GENERATE_ACPI_TABLE
95 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
96#endif
Alexander Grafe663b352016-08-19 01:23:29 +020097#ifdef CONFIG_EFI_LOADER
98 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
99#endif
Bin Meng721e9922015-10-12 05:23:41 -0700100 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200101
Bin Meng721e9922015-10-12 05:23:41 -0700102 t->bios_major_release = 0xff;
103 t->bios_minor_release = 0xff;
104 t->ec_major_release = 0xff;
105 t->ec_minor_release = 0xff;
106
107 len = t->length + smbios_string_table_len(t->eos);
108 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700109 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700110
111 return len;
112}
113
Simon Glass78227d42020-11-05 06:32:07 -0700114static int smbios_write_type1(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700115{
Simon Glassa2505fc2018-11-22 13:46:37 -0700116 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700117 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600118 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700119
Simon Glassa2505fc2018-11-22 13:46:37 -0700120 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700121 memset(t, 0, sizeof(struct smbios_type1));
122 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700123 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
124 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Alexander Graf6fb580d2016-08-19 01:23:31 +0200125 if (serial_str) {
Christian Gmeiner5113ff82018-07-30 13:22:06 +0200126 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
Alexander Graf6fb580d2016-08-19 01:23:31 +0200127 t->serial_number = smbios_add_string(t->eos, serial_str);
128 }
Bin Meng721e9922015-10-12 05:23:41 -0700129
130 len = t->length + smbios_string_table_len(t->eos);
131 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700132 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700133
134 return len;
135}
136
Simon Glass78227d42020-11-05 06:32:07 -0700137static int smbios_write_type2(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700138{
Simon Glassa2505fc2018-11-22 13:46:37 -0700139 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700140 int len = sizeof(struct smbios_type2);
141
Simon Glassa2505fc2018-11-22 13:46:37 -0700142 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700143 memset(t, 0, sizeof(struct smbios_type2));
144 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700145 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
146 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Bin Meng721e9922015-10-12 05:23:41 -0700147 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
148 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
149
150 len = t->length + smbios_string_table_len(t->eos);
151 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700152 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700153
154 return len;
155}
156
Simon Glass78227d42020-11-05 06:32:07 -0700157static int smbios_write_type3(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700158{
Simon Glassa2505fc2018-11-22 13:46:37 -0700159 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700160 int len = sizeof(struct smbios_type3);
161
Simon Glassa2505fc2018-11-22 13:46:37 -0700162 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700163 memset(t, 0, sizeof(struct smbios_type3));
164 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700165 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
Bin Meng721e9922015-10-12 05:23:41 -0700166 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
167 t->bootup_state = SMBIOS_STATE_SAFE;
168 t->power_supply_state = SMBIOS_STATE_SAFE;
169 t->thermal_state = SMBIOS_STATE_SAFE;
170 t->security_status = SMBIOS_SECURITY_NONE;
171
172 len = t->length + smbios_string_table_len(t->eos);
173 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700174 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700175
176 return len;
177}
178
Simon Glass78227d42020-11-05 06:32:07 -0700179static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
Alexander Graf96476202016-08-19 01:23:28 +0200180{
181 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
182 const char *vendor = "Unknown";
183 const char *name = "Unknown";
184
185#ifdef CONFIG_CPU
186 char processor_name[49];
187 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700188 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200189
Simon Glass78227d42020-11-05 06:32:07 -0700190 uclass_find_first_device(UCLASS_CPU, &cpu);
191 if (cpu) {
192 struct cpu_platdata *plat = dev_get_parent_platdata(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200193
194 if (plat->family)
195 processor_family = plat->family;
196 t->processor_id[0] = plat->id[0];
197 t->processor_id[1] = plat->id[1];
198
Simon Glass78227d42020-11-05 06:32:07 -0700199 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200200 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700201 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200202 name = processor_name;
203 }
204#endif
205
206 t->processor_family = processor_family;
207 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
208 t->processor_version = smbios_add_string(t->eos, name);
209}
210
Simon Glass78227d42020-11-05 06:32:07 -0700211static int smbios_write_type4(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700212{
Simon Glassa2505fc2018-11-22 13:46:37 -0700213 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700214 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700215
Simon Glassa2505fc2018-11-22 13:46:37 -0700216 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700217 memset(t, 0, sizeof(struct smbios_type4));
218 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
219 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass78227d42020-11-05 06:32:07 -0700220 smbios_write_type4_dm(t, node);
Bin Meng721e9922015-10-12 05:23:41 -0700221 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
222 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
223 t->l1_cache_handle = 0xffff;
224 t->l2_cache_handle = 0xffff;
225 t->l3_cache_handle = 0xffff;
226 t->processor_family2 = t->processor_family;
227
228 len = t->length + smbios_string_table_len(t->eos);
229 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700230 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700231
232 return len;
233}
234
Simon Glass78227d42020-11-05 06:32:07 -0700235static int smbios_write_type32(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700236{
Simon Glassa2505fc2018-11-22 13:46:37 -0700237 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700238 int len = sizeof(struct smbios_type32);
239
Simon Glassa2505fc2018-11-22 13:46:37 -0700240 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700241 memset(t, 0, sizeof(struct smbios_type32));
242 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
243
244 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700245 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700246
247 return len;
248}
249
Simon Glass78227d42020-11-05 06:32:07 -0700250static int smbios_write_type127(ulong *current, int handle, ofnode node)
Bin Meng721e9922015-10-12 05:23:41 -0700251{
Simon Glassa2505fc2018-11-22 13:46:37 -0700252 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700253 int len = sizeof(struct smbios_type127);
254
Simon Glassa2505fc2018-11-22 13:46:37 -0700255 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700256 memset(t, 0, sizeof(struct smbios_type127));
257 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
258
259 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700260 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700261
262 return len;
263}
264
265static smbios_write_type smbios_write_funcs[] = {
266 smbios_write_type0,
267 smbios_write_type1,
268 smbios_write_type2,
269 smbios_write_type3,
270 smbios_write_type4,
271 smbios_write_type32,
272 smbios_write_type127
273};
274
Simon Glass42fd8c12017-01-16 07:03:35 -0700275ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700276{
Simon Glass78227d42020-11-05 06:32:07 -0700277 ofnode node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700278 struct smbios_entry *se;
Simon Glass78227d42020-11-05 06:32:07 -0700279 struct udevice *dev;
Simon Glassa2505fc2018-11-22 13:46:37 -0700280 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700281 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700282 int len = 0;
283 int max_struct_size = 0;
284 int handle = 0;
285 char *istart;
286 int isize;
287 int i;
288
Simon Glass78227d42020-11-05 06:32:07 -0700289 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
290 uclass_first_device(UCLASS_SYSINFO, &dev);
291 if (dev)
292 node = dev_read_subnode(dev, "smbios");
293 }
294
Bin Meng721e9922015-10-12 05:23:41 -0700295 /* 16 byte align the table address */
296 addr = ALIGN(addr, 16);
297
Simon Glassa2505fc2018-11-22 13:46:37 -0700298 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700299 memset(se, 0, sizeof(struct smbios_entry));
300
301 addr += sizeof(struct smbios_entry);
302 addr = ALIGN(addr, 16);
303 tables = addr;
304
305 /* populate minimum required tables */
306 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass78227d42020-11-05 06:32:07 -0700307 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++, node);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200308
Bin Meng721e9922015-10-12 05:23:41 -0700309 max_struct_size = max(max_struct_size, tmp);
310 len += tmp;
311 }
312
313 memcpy(se->anchor, "_SM_", 4);
314 se->length = sizeof(struct smbios_entry);
315 se->major_ver = SMBIOS_MAJOR_VER;
316 se->minor_ver = SMBIOS_MINOR_VER;
317 se->max_struct_size = max_struct_size;
318 memcpy(se->intermediate_anchor, "_DMI_", 5);
319 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700320
321 /*
322 * We must use a pointer here so things work correctly on sandbox. The
323 * user of this table is not aware of the mapping of addresses to
324 * sandbox's DRAM buffer.
325 */
326 table_addr = (ulong)map_sysmem(tables, 0);
327 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
328 /*
329 * We need to put this >32-bit pointer into the table but the
330 * field is only 32 bits wide.
331 */
332 printf("WARNING: SMBIOS table_address overflow %llx\n",
333 (unsigned long long)table_addr);
334 table_addr = 0;
335 }
336 se->struct_table_address = table_addr;
337
Bin Meng721e9922015-10-12 05:23:41 -0700338 se->struct_count = handle;
339
340 /* calculate checksums */
341 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
342 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
343 se->intermediate_checksum = table_compute_checksum(istart, isize);
344 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700345 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700346
347 return addr;
348}