blob: e8ee55c4aeb941bb46218be5d34e2dc27f28dd9f [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 Glassa2505fc2018-11-22 13:46:37 -07009#include <mapmem.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020010#include <smbios.h>
11#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070012#include <version.h>
Alexander Graf96476202016-08-19 01:23:28 +020013#ifdef CONFIG_CPU
14#include <cpu.h>
15#include <dm.h>
16#include <dm/uclass-internal.h>
17#endif
Bin Meng721e9922015-10-12 05:23:41 -070018
Bin Meng721e9922015-10-12 05:23:41 -070019/**
20 * smbios_add_string() - add a string to the string area
21 *
22 * This adds a string to the string area which is appended directly after
23 * the formatted portion of an SMBIOS structure.
24 *
25 * @start: string area start address
26 * @str: string to add
27 * @return: string number in the string area
28 */
29static int smbios_add_string(char *start, const char *str)
30{
31 int i = 1;
32 char *p = start;
33
34 for (;;) {
35 if (!*p) {
36 strcpy(p, str);
37 p += strlen(str);
38 *p++ = '\0';
39 *p++ = '\0';
40
41 return i;
42 }
43
44 if (!strcmp(p, str))
45 return i;
46
47 p += strlen(p) + 1;
48 i++;
49 }
50}
51
52/**
53 * smbios_string_table_len() - compute the string area size
54 *
55 * This computes the size of the string area including the string terminator.
56 *
57 * @start: string area start address
58 * @return: string area size
59 */
60static int smbios_string_table_len(char *start)
61{
62 char *p = start;
63 int i, len = 0;
64
65 while (*p) {
66 i = strlen(p) + 1;
67 p += i;
68 len += i;
69 }
70
71 return len + 1;
72}
73
Simon Glass42fd8c12017-01-16 07:03:35 -070074static int smbios_write_type0(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -070075{
Simon Glassa2505fc2018-11-22 13:46:37 -070076 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -070077 int len = sizeof(struct smbios_type0);
78
Simon Glassa2505fc2018-11-22 13:46:37 -070079 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -070080 memset(t, 0, sizeof(struct smbios_type0));
81 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
82 t->vendor = smbios_add_string(t->eos, "U-Boot");
83 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
84 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +020085#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -070086 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +020087#endif
Bin Meng721e9922015-10-12 05:23:41 -070088 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
89 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
90 BIOS_CHARACTERISTICS_UPGRADEABLE;
91#ifdef CONFIG_GENERATE_ACPI_TABLE
92 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
93#endif
Alexander Grafe663b352016-08-19 01:23:29 +020094#ifdef CONFIG_EFI_LOADER
95 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
96#endif
Bin Meng721e9922015-10-12 05:23:41 -070097 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +020098
Bin Meng721e9922015-10-12 05:23:41 -070099 t->bios_major_release = 0xff;
100 t->bios_minor_release = 0xff;
101 t->ec_major_release = 0xff;
102 t->ec_minor_release = 0xff;
103
104 len = t->length + smbios_string_table_len(t->eos);
105 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700106 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700107
108 return len;
109}
110
Simon Glass42fd8c12017-01-16 07:03:35 -0700111static int smbios_write_type1(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700112{
Simon Glassa2505fc2018-11-22 13:46:37 -0700113 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700114 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600115 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700116
Simon Glassa2505fc2018-11-22 13:46:37 -0700117 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700118 memset(t, 0, sizeof(struct smbios_type1));
119 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700120 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
121 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Alexander Graf6fb580d2016-08-19 01:23:31 +0200122 if (serial_str) {
Christian Gmeiner5113ff82018-07-30 13:22:06 +0200123 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
Alexander Graf6fb580d2016-08-19 01:23:31 +0200124 t->serial_number = smbios_add_string(t->eos, serial_str);
125 }
Bin Meng721e9922015-10-12 05:23:41 -0700126
127 len = t->length + smbios_string_table_len(t->eos);
128 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700129 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700130
131 return len;
132}
133
Simon Glass42fd8c12017-01-16 07:03:35 -0700134static int smbios_write_type2(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700135{
Simon Glassa2505fc2018-11-22 13:46:37 -0700136 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700137 int len = sizeof(struct smbios_type2);
138
Simon Glassa2505fc2018-11-22 13:46:37 -0700139 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700140 memset(t, 0, sizeof(struct smbios_type2));
141 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700142 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
143 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Bin Meng721e9922015-10-12 05:23:41 -0700144 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
145 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
146
147 len = t->length + smbios_string_table_len(t->eos);
148 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700149 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700150
151 return len;
152}
153
Simon Glass42fd8c12017-01-16 07:03:35 -0700154static int smbios_write_type3(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700155{
Simon Glassa2505fc2018-11-22 13:46:37 -0700156 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700157 int len = sizeof(struct smbios_type3);
158
Simon Glassa2505fc2018-11-22 13:46:37 -0700159 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700160 memset(t, 0, sizeof(struct smbios_type3));
161 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700162 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
Bin Meng721e9922015-10-12 05:23:41 -0700163 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
164 t->bootup_state = SMBIOS_STATE_SAFE;
165 t->power_supply_state = SMBIOS_STATE_SAFE;
166 t->thermal_state = SMBIOS_STATE_SAFE;
167 t->security_status = SMBIOS_SECURITY_NONE;
168
169 len = t->length + smbios_string_table_len(t->eos);
170 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700171 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700172
173 return len;
174}
175
Alexander Graf96476202016-08-19 01:23:28 +0200176static void smbios_write_type4_dm(struct smbios_type4 *t)
177{
178 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
179 const char *vendor = "Unknown";
180 const char *name = "Unknown";
181
182#ifdef CONFIG_CPU
183 char processor_name[49];
184 char vendor_name[49];
185 struct udevice *dev = NULL;
186
187 uclass_find_first_device(UCLASS_CPU, &dev);
188 if (dev) {
189 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
190
191 if (plat->family)
192 processor_family = plat->family;
193 t->processor_id[0] = plat->id[0];
194 t->processor_id[1] = plat->id[1];
195
196 if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
197 vendor = vendor_name;
198 if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
199 name = processor_name;
200 }
201#endif
202
203 t->processor_family = processor_family;
204 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
205 t->processor_version = smbios_add_string(t->eos, name);
206}
207
Simon Glass42fd8c12017-01-16 07:03:35 -0700208static int smbios_write_type4(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700209{
Simon Glassa2505fc2018-11-22 13:46:37 -0700210 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700211 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700212
Simon Glassa2505fc2018-11-22 13:46:37 -0700213 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700214 memset(t, 0, sizeof(struct smbios_type4));
215 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
216 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Alexander Graf96476202016-08-19 01:23:28 +0200217 smbios_write_type4_dm(t);
Bin Meng721e9922015-10-12 05:23:41 -0700218 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
219 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
220 t->l1_cache_handle = 0xffff;
221 t->l2_cache_handle = 0xffff;
222 t->l3_cache_handle = 0xffff;
223 t->processor_family2 = t->processor_family;
224
225 len = t->length + smbios_string_table_len(t->eos);
226 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700227 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700228
229 return len;
230}
231
Simon Glass42fd8c12017-01-16 07:03:35 -0700232static int smbios_write_type32(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700233{
Simon Glassa2505fc2018-11-22 13:46:37 -0700234 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700235 int len = sizeof(struct smbios_type32);
236
Simon Glassa2505fc2018-11-22 13:46:37 -0700237 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700238 memset(t, 0, sizeof(struct smbios_type32));
239 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
240
241 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700242 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700243
244 return len;
245}
246
Simon Glass42fd8c12017-01-16 07:03:35 -0700247static int smbios_write_type127(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700248{
Simon Glassa2505fc2018-11-22 13:46:37 -0700249 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700250 int len = sizeof(struct smbios_type127);
251
Simon Glassa2505fc2018-11-22 13:46:37 -0700252 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700253 memset(t, 0, sizeof(struct smbios_type127));
254 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
255
256 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700257 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700258
259 return len;
260}
261
262static smbios_write_type smbios_write_funcs[] = {
263 smbios_write_type0,
264 smbios_write_type1,
265 smbios_write_type2,
266 smbios_write_type3,
267 smbios_write_type4,
268 smbios_write_type32,
269 smbios_write_type127
270};
271
Simon Glass42fd8c12017-01-16 07:03:35 -0700272ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700273{
274 struct smbios_entry *se;
Simon Glassa2505fc2018-11-22 13:46:37 -0700275 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700276 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700277 int len = 0;
278 int max_struct_size = 0;
279 int handle = 0;
280 char *istart;
281 int isize;
282 int i;
283
284 /* 16 byte align the table address */
285 addr = ALIGN(addr, 16);
286
Simon Glassa2505fc2018-11-22 13:46:37 -0700287 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700288 memset(se, 0, sizeof(struct smbios_entry));
289
290 addr += sizeof(struct smbios_entry);
291 addr = ALIGN(addr, 16);
292 tables = addr;
293
294 /* populate minimum required tables */
295 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass42fd8c12017-01-16 07:03:35 -0700296 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200297
Bin Meng721e9922015-10-12 05:23:41 -0700298 max_struct_size = max(max_struct_size, tmp);
299 len += tmp;
300 }
301
302 memcpy(se->anchor, "_SM_", 4);
303 se->length = sizeof(struct smbios_entry);
304 se->major_ver = SMBIOS_MAJOR_VER;
305 se->minor_ver = SMBIOS_MINOR_VER;
306 se->max_struct_size = max_struct_size;
307 memcpy(se->intermediate_anchor, "_DMI_", 5);
308 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700309
310 /*
311 * We must use a pointer here so things work correctly on sandbox. The
312 * user of this table is not aware of the mapping of addresses to
313 * sandbox's DRAM buffer.
314 */
315 table_addr = (ulong)map_sysmem(tables, 0);
316 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
317 /*
318 * We need to put this >32-bit pointer into the table but the
319 * field is only 32 bits wide.
320 */
321 printf("WARNING: SMBIOS table_address overflow %llx\n",
322 (unsigned long long)table_addr);
323 table_addr = 0;
324 }
325 se->struct_table_address = table_addr;
326
Bin Meng721e9922015-10-12 05:23:41 -0700327 se->struct_count = handle;
328
329 /* calculate checksums */
330 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
331 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
332 se->intermediate_checksum = table_compute_checksum(istart, isize);
333 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700334 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700335
336 return addr;
337}