]> Git Repo - J-u-boot.git/blame - lib/smbios.c
acpi: Add a function to get a device path and scope
[J-u-boot.git] / lib / smbios.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
721e992a
BM
2/*
3 * Copyright (C) 2015, Bin Meng <[email protected]>
4 *
5 * Adapted from coreboot src/arch/x86/smbios.c
721e992a
BM
6 */
7
8#include <common.h>
7b51b576 9#include <env.h>
a2505fc8 10#include <mapmem.h>
4b6dddc2
AG
11#include <smbios.h>
12#include <tables_csum.h>
721e992a 13#include <version.h>
96476206
AG
14#ifdef CONFIG_CPU
15#include <cpu.h>
16#include <dm.h>
17#include <dm/uclass-internal.h>
18#endif
721e992a 19
721e992a
BM
20/**
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
28 * @return: string number in the string area
29 */
30static int smbios_add_string(char *start, const char *str)
31{
32 int i = 1;
33 char *p = start;
00a871d3
HS
34 if (!*str)
35 str = "Unknown";
721e992a
BM
36
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
42fd8c19 77static int smbios_write_type0(ulong *current, int handle)
721e992a 78{
a2505fc8 79 struct smbios_type0 *t;
721e992a
BM
80 int len = sizeof(struct smbios_type0);
81
a2505fc8 82 t = map_sysmem(*current, len);
721e992a
BM
83 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);
e663b350 88#ifdef CONFIG_ROM_SIZE
721e992a 89 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
e663b350 90#endif
721e992a
BM
91 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;
e663b350
AG
96#endif
97#ifdef CONFIG_EFI_LOADER
98 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
721e992a
BM
99#endif
100 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
e663b350 101
721e992a
BM
102 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;
a2505fc8 109 unmap_sysmem(t);
721e992a
BM
110
111 return len;
112}
113
42fd8c19 114static int smbios_write_type1(ulong *current, int handle)
721e992a 115{
a2505fc8 116 struct smbios_type1 *t;
721e992a 117 int len = sizeof(struct smbios_type1);
00caae6d 118 char *serial_str = env_get("serial#");
721e992a 119
a2505fc8 120 t = map_sysmem(*current, len);
721e992a
BM
121 memset(t, 0, sizeof(struct smbios_type1));
122 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
4cdce9f5
BM
123 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
124 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
6fb580d7 125 if (serial_str) {
5113ff8a 126 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
6fb580d7
AG
127 t->serial_number = smbios_add_string(t->eos, serial_str);
128 }
721e992a
BM
129
130 len = t->length + smbios_string_table_len(t->eos);
131 *current += len;
a2505fc8 132 unmap_sysmem(t);
721e992a
BM
133
134 return len;
135}
136
42fd8c19 137static int smbios_write_type2(ulong *current, int handle)
721e992a 138{
a2505fc8 139 struct smbios_type2 *t;
721e992a
BM
140 int len = sizeof(struct smbios_type2);
141
a2505fc8 142 t = map_sysmem(*current, len);
721e992a
BM
143 memset(t, 0, sizeof(struct smbios_type2));
144 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
4cdce9f5
BM
145 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
146 t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
721e992a
BM
147 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;
a2505fc8 152 unmap_sysmem(t);
721e992a
BM
153
154 return len;
155}
156
42fd8c19 157static int smbios_write_type3(ulong *current, int handle)
721e992a 158{
a2505fc8 159 struct smbios_type3 *t;
721e992a
BM
160 int len = sizeof(struct smbios_type3);
161
a2505fc8 162 t = map_sysmem(*current, len);
721e992a
BM
163 memset(t, 0, sizeof(struct smbios_type3));
164 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
4cdce9f5 165 t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
721e992a
BM
166 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;
a2505fc8 174 unmap_sysmem(t);
721e992a
BM
175
176 return len;
177}
178
96476206
AG
179static void smbios_write_type4_dm(struct smbios_type4 *t)
180{
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];
188 struct udevice *dev = NULL;
189
190 uclass_find_first_device(UCLASS_CPU, &dev);
191 if (dev) {
192 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
193
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
199 if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
200 vendor = vendor_name;
201 if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
202 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
42fd8c19 211static int smbios_write_type4(ulong *current, int handle)
721e992a 212{
a2505fc8 213 struct smbios_type4 *t;
721e992a 214 int len = sizeof(struct smbios_type4);
721e992a 215
a2505fc8 216 t = map_sysmem(*current, len);
721e992a
BM
217 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;
96476206 220 smbios_write_type4_dm(t);
721e992a
BM
221 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;
a2505fc8 230 unmap_sysmem(t);
721e992a
BM
231
232 return len;
233}
234
42fd8c19 235static int smbios_write_type32(ulong *current, int handle)
721e992a 236{
a2505fc8 237 struct smbios_type32 *t;
721e992a
BM
238 int len = sizeof(struct smbios_type32);
239
a2505fc8 240 t = map_sysmem(*current, len);
721e992a
BM
241 memset(t, 0, sizeof(struct smbios_type32));
242 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
243
244 *current += len;
a2505fc8 245 unmap_sysmem(t);
721e992a
BM
246
247 return len;
248}
249
42fd8c19 250static int smbios_write_type127(ulong *current, int handle)
721e992a 251{
a2505fc8 252 struct smbios_type127 *t;
721e992a
BM
253 int len = sizeof(struct smbios_type127);
254
a2505fc8 255 t = map_sysmem(*current, len);
721e992a
BM
256 memset(t, 0, sizeof(struct smbios_type127));
257 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
258
259 *current += len;
a2505fc8 260 unmap_sysmem(t);
721e992a
BM
261
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
42fd8c19 275ulong write_smbios_table(ulong addr)
721e992a
BM
276{
277 struct smbios_entry *se;
a2505fc8 278 ulong table_addr;
42fd8c19 279 ulong tables;
721e992a
BM
280 int len = 0;
281 int max_struct_size = 0;
282 int handle = 0;
283 char *istart;
284 int isize;
285 int i;
286
287 /* 16 byte align the table address */
288 addr = ALIGN(addr, 16);
289
a2505fc8 290 se = map_sysmem(addr, sizeof(struct smbios_entry));
721e992a
BM
291 memset(se, 0, sizeof(struct smbios_entry));
292
293 addr += sizeof(struct smbios_entry);
294 addr = ALIGN(addr, 16);
295 tables = addr;
296
297 /* populate minimum required tables */
298 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
42fd8c19 299 int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
60a4df32 300
721e992a
BM
301 max_struct_size = max(max_struct_size, tmp);
302 len += tmp;
303 }
304
305 memcpy(se->anchor, "_SM_", 4);
306 se->length = sizeof(struct smbios_entry);
307 se->major_ver = SMBIOS_MAJOR_VER;
308 se->minor_ver = SMBIOS_MINOR_VER;
309 se->max_struct_size = max_struct_size;
310 memcpy(se->intermediate_anchor, "_DMI_", 5);
311 se->struct_table_length = len;
a2505fc8
SG
312
313 /*
314 * We must use a pointer here so things work correctly on sandbox. The
315 * user of this table is not aware of the mapping of addresses to
316 * sandbox's DRAM buffer.
317 */
318 table_addr = (ulong)map_sysmem(tables, 0);
319 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
320 /*
321 * We need to put this >32-bit pointer into the table but the
322 * field is only 32 bits wide.
323 */
324 printf("WARNING: SMBIOS table_address overflow %llx\n",
325 (unsigned long long)table_addr);
326 table_addr = 0;
327 }
328 se->struct_table_address = table_addr;
329
721e992a
BM
330 se->struct_count = handle;
331
332 /* calculate checksums */
333 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
334 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
335 se->intermediate_checksum = table_compute_checksum(istart, isize);
336 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
a2505fc8 337 unmap_sysmem(se);
721e992a
BM
338
339 return addr;
340}
This page took 0.34315 seconds and 4 git commands to generate.