]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/smbios.c
efi_loader: correct OpenProtocol()
[thirdparty/u-boot.git] / lib / smbios.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
721e992a
BM
2/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * Adapted from coreboot src/arch/x86/smbios.c
721e992a
BM
6 */
7
8#include <common.h>
a2505fc8 9#include <mapmem.h>
4b6dddc2
AG
10#include <smbios.h>
11#include <tables_csum.h>
721e992a 12#include <version.h>
96476206
AG
13#ifdef CONFIG_CPU
14#include <cpu.h>
15#include <dm.h>
16#include <dm/uclass-internal.h>
17#endif
721e992a 18
721e992a
BM
19/**
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
42fd8c19 74static int smbios_write_type0(ulong *current, int handle)
721e992a 75{
a2505fc8 76 struct smbios_type0 *t;
721e992a
BM
77 int len = sizeof(struct smbios_type0);
78
a2505fc8 79 t = map_sysmem(*current, len);
721e992a
BM
80 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);
e663b350 85#ifdef CONFIG_ROM_SIZE
721e992a 86 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
e663b350 87#endif
721e992a
BM
88 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;
e663b350
AG
93#endif
94#ifdef CONFIG_EFI_LOADER
95 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
721e992a
BM
96#endif
97 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
e663b350 98
721e992a
BM
99 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;
a2505fc8 106 unmap_sysmem(t);
721e992a
BM
107
108 return len;
109}
110
42fd8c19 111static int smbios_write_type1(ulong *current, int handle)
721e992a 112{
a2505fc8 113 struct smbios_type1 *t;
721e992a 114 int len = sizeof(struct smbios_type1);
00caae6d 115 char *serial_str = env_get("serial#");
721e992a 116
a2505fc8 117 t = map_sysmem(*current, len);
721e992a
BM
118 memset(t, 0, sizeof(struct smbios_type1));
119 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
4cdce9f5
BM
120 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
121 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
6fb580d7 122 if (serial_str) {
5113ff8a 123 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
6fb580d7
AG
124 t->serial_number = smbios_add_string(t->eos, serial_str);
125 }
721e992a
BM
126
127 len = t->length + smbios_string_table_len(t->eos);
128 *current += len;
a2505fc8 129 unmap_sysmem(t);
721e992a
BM
130
131 return len;
132}
133
42fd8c19 134static int smbios_write_type2(ulong *current, int handle)
721e992a 135{
a2505fc8 136 struct smbios_type2 *t;
721e992a
BM
137 int len = sizeof(struct smbios_type2);
138
a2505fc8 139 t = map_sysmem(*current, len);
721e992a
BM
140 memset(t, 0, sizeof(struct smbios_type2));
141 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
4cdce9f5
BM
142 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
143 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
721e992a
BM
144 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;
a2505fc8 149 unmap_sysmem(t);
721e992a
BM
150
151 return len;
152}
153
42fd8c19 154static int smbios_write_type3(ulong *current, int handle)
721e992a 155{
a2505fc8 156 struct smbios_type3 *t;
721e992a
BM
157 int len = sizeof(struct smbios_type3);
158
a2505fc8 159 t = map_sysmem(*current, len);
721e992a
BM
160 memset(t, 0, sizeof(struct smbios_type3));
161 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
4cdce9f5 162 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
721e992a
BM
163 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;
a2505fc8 171 unmap_sysmem(t);
721e992a
BM
172
173 return len;
174}
175
96476206
AG
176static 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
42fd8c19 208static int smbios_write_type4(ulong *current, int handle)
721e992a 209{
a2505fc8 210 struct smbios_type4 *t;
721e992a 211 int len = sizeof(struct smbios_type4);
721e992a 212
a2505fc8 213 t = map_sysmem(*current, len);
721e992a
BM
214 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;
96476206 217 smbios_write_type4_dm(t);
721e992a
BM
218 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;
a2505fc8 227 unmap_sysmem(t);
721e992a
BM
228
229 return len;
230}
231
42fd8c19 232static int smbios_write_type32(ulong *current, int handle)
721e992a 233{
a2505fc8 234 struct smbios_type32 *t;
721e992a
BM
235 int len = sizeof(struct smbios_type32);
236
a2505fc8 237 t = map_sysmem(*current, len);
721e992a
BM
238 memset(t, 0, sizeof(struct smbios_type32));
239 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
240
241 *current += len;
a2505fc8 242 unmap_sysmem(t);
721e992a
BM
243
244 return len;
245}
246
42fd8c19 247static int smbios_write_type127(ulong *current, int handle)
721e992a 248{
a2505fc8 249 struct smbios_type127 *t;
721e992a
BM
250 int len = sizeof(struct smbios_type127);
251
a2505fc8 252 t = map_sysmem(*current, len);
721e992a
BM
253 memset(t, 0, sizeof(struct smbios_type127));
254 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
255
256 *current += len;
a2505fc8 257 unmap_sysmem(t);
721e992a
BM
258
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
42fd8c19 272ulong write_smbios_table(ulong addr)
721e992a
BM
273{
274 struct smbios_entry *se;
a2505fc8 275 ulong table_addr;
42fd8c19 276 ulong tables;
721e992a
BM
277 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
a2505fc8 287 se = map_sysmem(addr, sizeof(struct smbios_entry));
721e992a
BM
288 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++) {
42fd8c19 296 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
60a4df32 297
721e992a
BM
298 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;
a2505fc8
SG
309
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
721e992a
BM
327 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));
a2505fc8 334 unmap_sysmem(se);
721e992a
BM
335
336 return addr;
337}