blob: 00b521eca92b5ad51734d3cac22f09a6e3a036cf [file] [log] [blame]
Bin Mengae5bedb2015-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 Graffb228082016-08-19 01:23:23 +020010#include <smbios.h>
11#include <tables_csum.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070012#include <version.h>
Alexander Grafe44b3c02016-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 Mengae5bedb2015-10-12 05:23:41 -070018
Bin Mengae5bedb2015-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 Glassca37a392017-01-16 07:03:35 -070074static int smbios_write_type0(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -070075{
76 struct smbios_type0 *t = (struct smbios_type0 *)*current;
77 int len = sizeof(struct smbios_type0);
78
79 memset(t, 0, sizeof(struct smbios_type0));
80 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
81 t->vendor = smbios_add_string(t->eos, "U-Boot");
82 t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
83 t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +020084#ifdef CONFIG_ROM_SIZE
Bin Mengae5bedb2015-10-12 05:23:41 -070085 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Graf66f96e12016-08-19 01:23:29 +020086#endif
Bin Mengae5bedb2015-10-12 05:23:41 -070087 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
88 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
89 BIOS_CHARACTERISTICS_UPGRADEABLE;
90#ifdef CONFIG_GENERATE_ACPI_TABLE
91 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
92#endif
Alexander Graf66f96e12016-08-19 01:23:29 +020093#ifdef CONFIG_EFI_LOADER
94 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
95#endif
Bin Mengae5bedb2015-10-12 05:23:41 -070096 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +020097
Bin Mengae5bedb2015-10-12 05:23:41 -070098 t->bios_major_release = 0xff;
99 t->bios_minor_release = 0xff;
100 t->ec_major_release = 0xff;
101 t->ec_minor_release = 0xff;
102
103 len = t->length + smbios_string_table_len(t->eos);
104 *current += len;
105
106 return len;
107}
108
Simon Glassca37a392017-01-16 07:03:35 -0700109static int smbios_write_type1(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700110{
111 struct smbios_type1 *t = (struct smbios_type1 *)*current;
112 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600113 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700114
115 memset(t, 0, sizeof(struct smbios_type1));
116 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Bin Meng543a82b2016-05-22 01:45:38 -0700117 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
118 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200119 if (serial_str) {
120 strncpy((char*)t->uuid, serial_str, sizeof(t->uuid));
121 t->serial_number = smbios_add_string(t->eos, serial_str);
122 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700123
124 len = t->length + smbios_string_table_len(t->eos);
125 *current += len;
126
127 return len;
128}
129
Simon Glassca37a392017-01-16 07:03:35 -0700130static int smbios_write_type2(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700131{
132 struct smbios_type2 *t = (struct smbios_type2 *)*current;
133 int len = sizeof(struct smbios_type2);
134
135 memset(t, 0, sizeof(struct smbios_type2));
136 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Bin Meng543a82b2016-05-22 01:45:38 -0700137 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
138 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
Bin Mengae5bedb2015-10-12 05:23:41 -0700139 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
140 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
141
142 len = t->length + smbios_string_table_len(t->eos);
143 *current += len;
144
145 return len;
146}
147
Simon Glassca37a392017-01-16 07:03:35 -0700148static int smbios_write_type3(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700149{
150 struct smbios_type3 *t = (struct smbios_type3 *)*current;
151 int len = sizeof(struct smbios_type3);
152
153 memset(t, 0, sizeof(struct smbios_type3));
154 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Bin Meng543a82b2016-05-22 01:45:38 -0700155 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
Bin Mengae5bedb2015-10-12 05:23:41 -0700156 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
157 t->bootup_state = SMBIOS_STATE_SAFE;
158 t->power_supply_state = SMBIOS_STATE_SAFE;
159 t->thermal_state = SMBIOS_STATE_SAFE;
160 t->security_status = SMBIOS_SECURITY_NONE;
161
162 len = t->length + smbios_string_table_len(t->eos);
163 *current += len;
164
165 return len;
166}
167
Alexander Grafe44b3c02016-08-19 01:23:28 +0200168static void smbios_write_type4_dm(struct smbios_type4 *t)
169{
170 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
171 const char *vendor = "Unknown";
172 const char *name = "Unknown";
173
174#ifdef CONFIG_CPU
175 char processor_name[49];
176 char vendor_name[49];
177 struct udevice *dev = NULL;
178
179 uclass_find_first_device(UCLASS_CPU, &dev);
180 if (dev) {
181 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
182
183 if (plat->family)
184 processor_family = plat->family;
185 t->processor_id[0] = plat->id[0];
186 t->processor_id[1] = plat->id[1];
187
188 if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
189 vendor = vendor_name;
190 if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
191 name = processor_name;
192 }
193#endif
194
195 t->processor_family = processor_family;
196 t->processor_manufacturer = smbios_add_string(t->eos, vendor);
197 t->processor_version = smbios_add_string(t->eos, name);
198}
199
Simon Glassca37a392017-01-16 07:03:35 -0700200static int smbios_write_type4(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700201{
202 struct smbios_type4 *t = (struct smbios_type4 *)*current;
203 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700204
205 memset(t, 0, sizeof(struct smbios_type4));
206 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
207 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200208 smbios_write_type4_dm(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700209 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
210 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
211 t->l1_cache_handle = 0xffff;
212 t->l2_cache_handle = 0xffff;
213 t->l3_cache_handle = 0xffff;
214 t->processor_family2 = t->processor_family;
215
216 len = t->length + smbios_string_table_len(t->eos);
217 *current += len;
218
219 return len;
220}
221
Simon Glassca37a392017-01-16 07:03:35 -0700222static int smbios_write_type32(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700223{
224 struct smbios_type32 *t = (struct smbios_type32 *)*current;
225 int len = sizeof(struct smbios_type32);
226
227 memset(t, 0, sizeof(struct smbios_type32));
228 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
229
230 *current += len;
231
232 return len;
233}
234
Simon Glassca37a392017-01-16 07:03:35 -0700235static int smbios_write_type127(ulong *current, int handle)
Bin Mengae5bedb2015-10-12 05:23:41 -0700236{
237 struct smbios_type127 *t = (struct smbios_type127 *)*current;
238 int len = sizeof(struct smbios_type127);
239
240 memset(t, 0, sizeof(struct smbios_type127));
241 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
242
243 *current += len;
244
245 return len;
246}
247
248static smbios_write_type smbios_write_funcs[] = {
249 smbios_write_type0,
250 smbios_write_type1,
251 smbios_write_type2,
252 smbios_write_type3,
253 smbios_write_type4,
254 smbios_write_type32,
255 smbios_write_type127
256};
257
Simon Glassca37a392017-01-16 07:03:35 -0700258ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700259{
260 struct smbios_entry *se;
Simon Glassca37a392017-01-16 07:03:35 -0700261 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700262 int len = 0;
263 int max_struct_size = 0;
264 int handle = 0;
265 char *istart;
266 int isize;
267 int i;
268
269 /* 16 byte align the table address */
270 addr = ALIGN(addr, 16);
271
Simon Glassca37a392017-01-16 07:03:35 -0700272 se = (struct smbios_entry *)(uintptr_t)addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700273 memset(se, 0, sizeof(struct smbios_entry));
274
275 addr += sizeof(struct smbios_entry);
276 addr = ALIGN(addr, 16);
277 tables = addr;
278
279 /* populate minimum required tables */
280 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glassca37a392017-01-16 07:03:35 -0700281 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
Bin Mengae5bedb2015-10-12 05:23:41 -0700282 max_struct_size = max(max_struct_size, tmp);
283 len += tmp;
284 }
285
286 memcpy(se->anchor, "_SM_", 4);
287 se->length = sizeof(struct smbios_entry);
288 se->major_ver = SMBIOS_MAJOR_VER;
289 se->minor_ver = SMBIOS_MINOR_VER;
290 se->max_struct_size = max_struct_size;
291 memcpy(se->intermediate_anchor, "_DMI_", 5);
292 se->struct_table_length = len;
293 se->struct_table_address = tables;
294 se->struct_count = handle;
295
296 /* calculate checksums */
297 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
298 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
299 se->intermediate_checksum = table_compute_checksum(istart, isize);
300 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
301
302 return addr;
303}