blob: 8f19ad89c121ebbc2ae1a7842edc8a8e432e3b8b [file] [log] [blame]
Bin Meng721e9922015-10-12 05:23:41 -07001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * Adapted from coreboot src/arch/x86/smbios.c
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.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
19DECLARE_GLOBAL_DATA_PTR;
20
21/**
22 * smbios_add_string() - add a string to the string area
23 *
24 * This adds a string to the string area which is appended directly after
25 * the formatted portion of an SMBIOS structure.
26 *
27 * @start: string area start address
28 * @str: string to add
29 * @return: string number in the string area
30 */
31static int smbios_add_string(char *start, const char *str)
32{
33 int i = 1;
34 char *p = start;
35
36 for (;;) {
37 if (!*p) {
38 strcpy(p, str);
39 p += strlen(str);
40 *p++ = '\0';
41 *p++ = '\0';
42
43 return i;
44 }
45
46 if (!strcmp(p, str))
47 return i;
48
49 p += strlen(p) + 1;
50 i++;
51 }
52}
53
54/**
55 * smbios_string_table_len() - compute the string area size
56 *
57 * This computes the size of the string area including the string terminator.
58 *
59 * @start: string area start address
60 * @return: string area size
61 */
62static int smbios_string_table_len(char *start)
63{
64 char *p = start;
65 int i, len = 0;
66
67 while (*p) {
68 i = strlen(p) + 1;
69 p += i;
70 len += i;
71 }
72
73 return len + 1;
74}
75
Simon Glass42fd8c12017-01-16 07:03:35 -070076static int smbios_write_type0(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -070077{
78 struct smbios_type0 *t = (struct smbios_type0 *)*current;
79 int len = sizeof(struct smbios_type0);
80
81 memset(t, 0, sizeof(struct smbios_type0));
82 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
83 t->vendor = smbios_add_string(t->eos, "U-Boot");
84 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
85 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +020086#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -070087 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +020088#endif
Bin Meng721e9922015-10-12 05:23:41 -070089 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
90 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
91 BIOS_CHARACTERISTICS_UPGRADEABLE;
92#ifdef CONFIG_GENERATE_ACPI_TABLE
93 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
94#endif
Alexander Grafe663b352016-08-19 01:23:29 +020095#ifdef CONFIG_EFI_LOADER
96 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
97#endif
Bin Meng721e9922015-10-12 05:23:41 -070098 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +020099
Bin Meng721e9922015-10-12 05:23:41 -0700100 t->bios_major_release = 0xff;
101 t->bios_minor_release = 0xff;
102 t->ec_major_release = 0xff;
103 t->ec_minor_release = 0xff;
104
105 len = t->length + smbios_string_table_len(t->eos);
106 *current += len;
107
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{
113 struct smbios_type1 *t = (struct smbios_type1 *)*current;
114 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
117 memset(t, 0, sizeof(struct smbios_type1));
118 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700119 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
120 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Alexander Graf6fb580d2016-08-19 01:23:31 +0200121 if (serial_str) {
122 strncpy((char*)t->uuid, serial_str, sizeof(t->uuid));
123 t->serial_number = smbios_add_string(t->eos, serial_str);
124 }
Bin Meng721e9922015-10-12 05:23:41 -0700125
126 len = t->length + smbios_string_table_len(t->eos);
127 *current += len;
128
129 return len;
130}
131
Simon Glass42fd8c12017-01-16 07:03:35 -0700132static int smbios_write_type2(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700133{
134 struct smbios_type2 *t = (struct smbios_type2 *)*current;
135 int len = sizeof(struct smbios_type2);
136
137 memset(t, 0, sizeof(struct smbios_type2));
138 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700139 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
140 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Bin Meng721e9922015-10-12 05:23:41 -0700141 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
142 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
143
144 len = t->length + smbios_string_table_len(t->eos);
145 *current += len;
146
147 return len;
148}
149
Simon Glass42fd8c12017-01-16 07:03:35 -0700150static int smbios_write_type3(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700151{
152 struct smbios_type3 *t = (struct smbios_type3 *)*current;
153 int len = sizeof(struct smbios_type3);
154
155 memset(t, 0, sizeof(struct smbios_type3));
156 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Bin Meng4cdce9f2016-05-22 01:45:38 -0700157 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
Bin Meng721e9922015-10-12 05:23:41 -0700158 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
159 t->bootup_state = SMBIOS_STATE_SAFE;
160 t->power_supply_state = SMBIOS_STATE_SAFE;
161 t->thermal_state = SMBIOS_STATE_SAFE;
162 t->security_status = SMBIOS_SECURITY_NONE;
163
164 len = t->length + smbios_string_table_len(t->eos);
165 *current += len;
166
167 return len;
168}
169
Alexander Graf96476202016-08-19 01:23:28 +0200170static void smbios_write_type4_dm(struct smbios_type4 *t)
171{
172 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
173 const char *vendor = "Unknown";
174 const char *name = "Unknown";
175
176#ifdef CONFIG_CPU
177 char processor_name[49];
178 char vendor_name[49];
179 struct udevice *dev = NULL;
180
181 uclass_find_first_device(UCLASS_CPU, &dev);
182 if (dev) {
183 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
184
185 if (plat->family)
186 processor_family = plat->family;
187 t->processor_id[0] = plat->id[0];
188 t->processor_id[1] = plat->id[1];
189
190 if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
191 vendor = vendor_name;
192 if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
193 name = processor_name;
194 }
195#endif
196
197 t->processor_family = processor_family;
198 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
199 t->processor_version = smbios_add_string(t->eos, name);
200}
201
Simon Glass42fd8c12017-01-16 07:03:35 -0700202static int smbios_write_type4(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700203{
204 struct smbios_type4 *t = (struct smbios_type4 *)*current;
205 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700206
207 memset(t, 0, sizeof(struct smbios_type4));
208 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
209 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Alexander Graf96476202016-08-19 01:23:28 +0200210 smbios_write_type4_dm(t);
Bin Meng721e9922015-10-12 05:23:41 -0700211 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
212 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
213 t->l1_cache_handle = 0xffff;
214 t->l2_cache_handle = 0xffff;
215 t->l3_cache_handle = 0xffff;
216 t->processor_family2 = t->processor_family;
217
218 len = t->length + smbios_string_table_len(t->eos);
219 *current += len;
220
221 return len;
222}
223
Simon Glass42fd8c12017-01-16 07:03:35 -0700224static int smbios_write_type32(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700225{
226 struct smbios_type32 *t = (struct smbios_type32 *)*current;
227 int len = sizeof(struct smbios_type32);
228
229 memset(t, 0, sizeof(struct smbios_type32));
230 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
231
232 *current += len;
233
234 return len;
235}
236
Simon Glass42fd8c12017-01-16 07:03:35 -0700237static int smbios_write_type127(ulong *current, int handle)
Bin Meng721e9922015-10-12 05:23:41 -0700238{
239 struct smbios_type127 *t = (struct smbios_type127 *)*current;
240 int len = sizeof(struct smbios_type127);
241
242 memset(t, 0, sizeof(struct smbios_type127));
243 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
244
245 *current += len;
246
247 return len;
248}
249
250static smbios_write_type smbios_write_funcs[] = {
251 smbios_write_type0,
252 smbios_write_type1,
253 smbios_write_type2,
254 smbios_write_type3,
255 smbios_write_type4,
256 smbios_write_type32,
257 smbios_write_type127
258};
259
Simon Glass42fd8c12017-01-16 07:03:35 -0700260ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700261{
262 struct smbios_entry *se;
Simon Glass42fd8c12017-01-16 07:03:35 -0700263 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700264 int len = 0;
265 int max_struct_size = 0;
266 int handle = 0;
267 char *istart;
268 int isize;
269 int i;
270
271 /* 16 byte align the table address */
272 addr = ALIGN(addr, 16);
273
Simon Glass42fd8c12017-01-16 07:03:35 -0700274 se = (struct smbios_entry *)(uintptr_t)addr;
Bin Meng721e9922015-10-12 05:23:41 -0700275 memset(se, 0, sizeof(struct smbios_entry));
276
277 addr += sizeof(struct smbios_entry);
278 addr = ALIGN(addr, 16);
279 tables = addr;
280
281 /* populate minimum required tables */
282 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass42fd8c12017-01-16 07:03:35 -0700283 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
Bin Meng721e9922015-10-12 05:23:41 -0700284 max_struct_size = max(max_struct_size, tmp);
285 len += tmp;
286 }
287
288 memcpy(se->anchor, "_SM_", 4);
289 se->length = sizeof(struct smbios_entry);
290 se->major_ver = SMBIOS_MAJOR_VER;
291 se->minor_ver = SMBIOS_MINOR_VER;
292 se->max_struct_size = max_struct_size;
293 memcpy(se->intermediate_anchor, "_DMI_", 5);
294 se->struct_table_length = len;
295 se->struct_table_address = tables;
296 se->struct_count = handle;
297
298 /* calculate checksums */
299 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
300 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
301 se->intermediate_checksum = table_compute_checksum(istart, isize);
302 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
303
304 return addr;
305}