]>
Commit | Line | Data |
---|---|---|
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 | ||
4a8a54c3 SG |
8 | #define LOG_CATEGORY LOGC_BOARD |
9 | ||
78227d4e | 10 | #include <dm.h> |
7b51b576 | 11 | #include <env.h> |
11275e4f | 12 | #include <linux/stringify.h> |
738b3466 | 13 | #include <linux/string.h> |
a2505fc8 | 14 | #include <mapmem.h> |
4b6dddc2 | 15 | #include <smbios.h> |
07c9e683 | 16 | #include <sysinfo.h> |
4b6dddc2 | 17 | #include <tables_csum.h> |
721e992a | 18 | #include <version.h> |
738b3466 IA |
19 | #include <malloc.h> |
20 | #include <dm/ofnode.h> | |
96476206 AG |
21 | #ifdef CONFIG_CPU |
22 | #include <cpu.h> | |
96476206 AG |
23 | #include <dm/uclass-internal.h> |
24 | #endif | |
8c919fcd | 25 | #include <linux/sizes.h> |
721e992a | 26 | |
11275e4f T |
27 | /* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */ |
28 | #if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \ | |
29 | U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12 | |
30 | #error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros | |
31 | #endif | |
32 | ||
33 | /* | |
34 | * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy. | |
35 | * BIOS Release Date is calculated from U-Boot version and fixed day 01. | |
36 | * So for U-Boot version 2021.04 it is calculated as "04/01/2021". | |
37 | * BIOS Release Date should contain date when code was released | |
38 | * and not when it was built or compiled. | |
39 | */ | |
40 | #if U_BOOT_VERSION_NUM_PATCH < 10 | |
41 | #define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH) | |
42 | #else | |
43 | #define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH) | |
44 | #endif | |
45 | #define U_BOOT_DMI_DAY "01" | |
46 | #define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM) | |
47 | #define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR | |
48 | ||
e9adaa75 SG |
49 | DECLARE_GLOBAL_DATA_PTR; |
50 | ||
738b3466 IA |
51 | /** |
52 | * struct map_sysinfo - Mapping of sysinfo strings to DT | |
53 | * | |
5e2b472b | 54 | * @si_str: sysinfo string |
738b3466 IA |
55 | * @dt_str: DT string |
56 | * @max: Max index of the tokenized string to pick. Counting starts from 0 | |
57 | * | |
58 | */ | |
59 | struct map_sysinfo { | |
b6488caa | 60 | const char *si_node; |
5e2b472b | 61 | const char *si_str; |
738b3466 IA |
62 | const char *dt_str; |
63 | int max; | |
64 | }; | |
65 | ||
66 | static const struct map_sysinfo sysinfo_to_dt[] = { | |
b6488caa IA |
67 | { .si_node = "system", .si_str = "product", .dt_str = "model", 2 }, |
68 | { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 }, | |
69 | { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 }, | |
70 | { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 }, | |
738b3466 IA |
71 | }; |
72 | ||
1e8989ad SG |
73 | /** |
74 | * struct smbios_ctx - context for writing SMBIOS tables | |
75 | * | |
b6488caa IA |
76 | * @node: node containing the information to write (ofnode_null() |
77 | * if none) | |
78 | * @dev: sysinfo device to use (NULL if none) | |
79 | * @subnode_name: sysinfo subnode_name. Used for DT fallback | |
80 | * @eos: end-of-string pointer for the table being processed. | |
81 | * This is set up when we start processing a table | |
82 | * @next_ptr: pointer to the start of the next string to be added. | |
83 | * When the table is not empty, this points to the byte | |
84 | * after the \0 of the previous string. | |
85 | * @last_str: points to the last string that was written to the table, | |
86 | * or NULL if none | |
1e8989ad SG |
87 | */ |
88 | struct smbios_ctx { | |
89 | ofnode node; | |
90 | struct udevice *dev; | |
b6488caa | 91 | const char *subnode_name; |
0c95fff3 | 92 | char *eos; |
fd3b826d | 93 | char *next_ptr; |
e9adaa75 | 94 | char *last_str; |
1e8989ad SG |
95 | }; |
96 | ||
0e89b859 SG |
97 | /** |
98 | * Function prototype to write a specific type of SMBIOS structure | |
99 | * | |
100 | * @addr: start address to write the structure | |
101 | * @handle: the structure's handle, a unique 16-bit number | |
1e8989ad | 102 | * @ctx: context for writing the tables |
8c6532d7 | 103 | * Return: size of the structure |
0e89b859 | 104 | */ |
1e8989ad SG |
105 | typedef int (*smbios_write_type)(ulong *addr, int handle, |
106 | struct smbios_ctx *ctx); | |
0e89b859 | 107 | |
44ffb6f0 SG |
108 | /** |
109 | * struct smbios_write_method - Information about a table-writing function | |
110 | * | |
111 | * @write: Function to call | |
112 | * @subnode_name: Name of subnode which has the information for this function, | |
113 | * NULL if none | |
114 | */ | |
115 | struct smbios_write_method { | |
116 | smbios_write_type write; | |
117 | const char *subnode_name; | |
118 | }; | |
119 | ||
b6488caa | 120 | static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si) |
738b3466 IA |
121 | { |
122 | int i; | |
123 | ||
124 | for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) { | |
b6488caa IA |
125 | if (node && !strcmp(node, sysinfo_to_dt[i].si_node) && |
126 | !strcmp(si, sysinfo_to_dt[i].si_str)) | |
738b3466 IA |
127 | return &sysinfo_to_dt[i]; |
128 | } | |
129 | ||
130 | return NULL; | |
131 | } | |
132 | ||
721e992a BM |
133 | /** |
134 | * smbios_add_string() - add a string to the string area | |
135 | * | |
136 | * This adds a string to the string area which is appended directly after | |
137 | * the formatted portion of an SMBIOS structure. | |
138 | * | |
0c95fff3 | 139 | * @ctx: SMBIOS context |
721e992a | 140 | * @str: string to add |
6ebf9136 | 141 | * Return: string number in the string area. 0 if str is NULL. |
721e992a | 142 | */ |
0c95fff3 | 143 | static int smbios_add_string(struct smbios_ctx *ctx, const char *str) |
721e992a BM |
144 | { |
145 | int i = 1; | |
0c95fff3 SG |
146 | char *p = ctx->eos; |
147 | ||
6ebf9136 HS |
148 | if (!str) |
149 | return 0; | |
150 | ||
721e992a BM |
151 | for (;;) { |
152 | if (!*p) { | |
e9adaa75 | 153 | ctx->last_str = p; |
721e992a BM |
154 | strcpy(p, str); |
155 | p += strlen(str); | |
156 | *p++ = '\0'; | |
fd3b826d | 157 | ctx->next_ptr = p; |
721e992a BM |
158 | *p++ = '\0'; |
159 | ||
160 | return i; | |
161 | } | |
162 | ||
e9adaa75 SG |
163 | if (!strcmp(p, str)) { |
164 | ctx->last_str = p; | |
721e992a | 165 | return i; |
e9adaa75 | 166 | } |
721e992a BM |
167 | |
168 | p += strlen(p) + 1; | |
169 | i++; | |
170 | } | |
171 | } | |
172 | ||
738b3466 IA |
173 | /** |
174 | * get_str_from_dt - Get a substring from a DT property. | |
175 | * After finding the property in the DT, the function | |
176 | * will parse comma-separated values and return the value. | |
177 | * If nprop->max exceeds the number of comma-separated | |
178 | * elements, the last non NULL value will be returned. | |
179 | * Counting starts from zero. | |
180 | * | |
181 | * @nprop: sysinfo property to use | |
182 | * @str: pointer to fill with data | |
183 | * @size: str buffer length | |
184 | */ | |
185 | static | |
186 | void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size) | |
187 | { | |
188 | const char *dt_str; | |
189 | int cnt = 0; | |
190 | char *token; | |
191 | ||
192 | memset(str, 0, size); | |
193 | if (!nprop || !nprop->max) | |
194 | return; | |
195 | ||
196 | dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str); | |
197 | if (!dt_str) | |
198 | return; | |
199 | ||
200 | memcpy(str, dt_str, size); | |
201 | token = strtok(str, ","); | |
202 | while (token && cnt < nprop->max) { | |
203 | strlcpy(str, token, strlen(token) + 1); | |
204 | token = strtok(NULL, ","); | |
205 | cnt++; | |
206 | } | |
207 | } | |
208 | ||
44ffb6f0 | 209 | /** |
07c9e683 SG |
210 | * smbios_add_prop_si() - Add a property from the devicetree or sysinfo |
211 | * | |
212 | * Sysinfo is used if available, with a fallback to devicetree | |
44ffb6f0 | 213 | * |
1e8989ad | 214 | * @ctx: context for writing the tables |
44ffb6f0 | 215 | * @prop: property to write |
b327e64d | 216 | * @sysinfo_id: unique identifier for the string value to be read |
a986ccea | 217 | * @dval: Default value to use if the string is not found or is empty |
8c6532d7 | 218 | * Return: 0 if not found, else SMBIOS string number (1 or more) |
44ffb6f0 | 219 | */ |
07c9e683 | 220 | static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop, |
a986ccea | 221 | int sysinfo_id, const char *dval) |
44ffb6f0 | 222 | { |
738b3466 IA |
223 | int ret; |
224 | ||
a986ccea | 225 | if (!dval || !*dval) |
6ebf9136 | 226 | dval = NULL; |
a986ccea IA |
227 | |
228 | if (!prop) | |
229 | return smbios_add_string(ctx, dval); | |
230 | ||
07c9e683 SG |
231 | if (sysinfo_id && ctx->dev) { |
232 | char val[SMBIOS_STR_MAX]; | |
07c9e683 SG |
233 | |
234 | ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val); | |
235 | if (!ret) | |
236 | return smbios_add_string(ctx, val); | |
237 | } | |
e4f8e543 | 238 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
738b3466 IA |
239 | const char *str = NULL; |
240 | char str_dt[128] = { 0 }; | |
241 | /* | |
242 | * If the node is not valid fallback and try the entire DT | |
243 | * so we can at least fill in manufacturer and board type | |
244 | */ | |
245 | if (ofnode_valid(ctx->node)) { | |
246 | str = ofnode_read_string(ctx->node, prop); | |
247 | } else { | |
248 | const struct map_sysinfo *nprop; | |
e4f8e543 | 249 | |
b6488caa | 250 | nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop); |
738b3466 IA |
251 | get_str_from_dt(nprop, str_dt, sizeof(str_dt)); |
252 | str = (const char *)str_dt; | |
253 | } | |
a986ccea | 254 | |
738b3466 IA |
255 | ret = smbios_add_string(ctx, str && *str ? str : dval); |
256 | return ret; | |
e4f8e543 | 257 | } |
44ffb6f0 SG |
258 | |
259 | return 0; | |
260 | } | |
261 | ||
07c9e683 SG |
262 | /** |
263 | * smbios_add_prop() - Add a property from the devicetree | |
264 | * | |
a986ccea IA |
265 | * @prop: property to write. The default string will be written if |
266 | * prop is NULL | |
267 | * @dval: Default value to use if the string is not found or is empty | |
8c6532d7 | 268 | * Return: 0 if not found, else SMBIOS string number (1 or more) |
07c9e683 | 269 | */ |
a986ccea IA |
270 | static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop, |
271 | const char *dval) | |
07c9e683 | 272 | { |
a986ccea | 273 | return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval); |
07c9e683 SG |
274 | } |
275 | ||
0c95fff3 SG |
276 | static void smbios_set_eos(struct smbios_ctx *ctx, char *eos) |
277 | { | |
278 | ctx->eos = eos; | |
fd3b826d | 279 | ctx->next_ptr = eos; |
e9adaa75 SG |
280 | ctx->last_str = NULL; |
281 | } | |
282 | ||
283 | int smbios_update_version(const char *version) | |
284 | { | |
285 | char *ptr = gd->smbios_version; | |
286 | uint old_len, len; | |
287 | ||
288 | if (!ptr) | |
289 | return log_ret(-ENOENT); | |
290 | ||
291 | /* | |
292 | * This string is supposed to have at least enough bytes and is | |
293 | * padded with spaces. Update it, taking care not to move the | |
294 | * \0 terminator, so that other strings in the string table | |
295 | * are not disturbed. See smbios_add_string() | |
296 | */ | |
297 | old_len = strnlen(ptr, SMBIOS_STR_MAX); | |
298 | len = strnlen(version, SMBIOS_STR_MAX); | |
299 | if (len > old_len) | |
300 | return log_ret(-ENOSPC); | |
301 | ||
302 | log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr); | |
303 | memcpy(ptr, version, len); | |
304 | #ifdef LOG_DEBUG | |
305 | print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0); | |
306 | #endif | |
307 | ||
308 | return 0; | |
0c95fff3 SG |
309 | } |
310 | ||
721e992a BM |
311 | /** |
312 | * smbios_string_table_len() - compute the string area size | |
313 | * | |
314 | * This computes the size of the string area including the string terminator. | |
315 | * | |
fd3b826d | 316 | * @ctx: SMBIOS context |
8c6532d7 | 317 | * Return: string area size |
721e992a | 318 | */ |
fd3b826d | 319 | static int smbios_string_table_len(const struct smbios_ctx *ctx) |
721e992a | 320 | { |
e31efe50 MB |
321 | /* In case no string is defined we have to return two \0 */ |
322 | if (ctx->next_ptr == ctx->eos) | |
323 | return 2; | |
324 | ||
fd3b826d SG |
325 | /* Allow for the final \0 after all strings */ |
326 | return (ctx->next_ptr + 1) - ctx->eos; | |
721e992a BM |
327 | } |
328 | ||
1e8989ad SG |
329 | static int smbios_write_type0(ulong *current, int handle, |
330 | struct smbios_ctx *ctx) | |
721e992a | 331 | { |
a2505fc8 | 332 | struct smbios_type0 *t; |
721e992a BM |
333 | int len = sizeof(struct smbios_type0); |
334 | ||
a2505fc8 | 335 | t = map_sysmem(*current, len); |
721e992a BM |
336 | memset(t, 0, sizeof(struct smbios_type0)); |
337 | fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); | |
0c95fff3 | 338 | smbios_set_eos(ctx, t->eos); |
a986ccea | 339 | t->vendor = smbios_add_prop(ctx, NULL, "U-Boot"); |
e9adaa75 | 340 | |
a986ccea | 341 | t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION); |
e9adaa75 SG |
342 | if (t->bios_ver) |
343 | gd->smbios_version = ctx->last_str; | |
344 | log_debug("smbios_version = %p: '%s'\n", gd->smbios_version, | |
345 | gd->smbios_version); | |
346 | #ifdef LOG_DEBUG | |
347 | print_buffer((ulong)gd->smbios_version, gd->smbios_version, | |
348 | 1, strlen(gd->smbios_version) + 1, 0); | |
349 | #endif | |
a986ccea | 350 | t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE); |
e663b350 | 351 | #ifdef CONFIG_ROM_SIZE |
8c919fcd HS |
352 | if (CONFIG_ROM_SIZE < SZ_16M) { |
353 | t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; | |
354 | } else { | |
355 | /* CONFIG_ROM_SIZE < 8 GiB */ | |
356 | t->bios_rom_size = 0xff; | |
357 | t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20; | |
358 | } | |
e663b350 | 359 | #endif |
721e992a BM |
360 | t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED | |
361 | BIOS_CHARACTERISTICS_SELECTABLE_BOOT | | |
362 | BIOS_CHARACTERISTICS_UPGRADEABLE; | |
363 | #ifdef CONFIG_GENERATE_ACPI_TABLE | |
364 | t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI; | |
e663b350 AG |
365 | #endif |
366 | #ifdef CONFIG_EFI_LOADER | |
ff192304 | 367 | t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI; |
721e992a | 368 | #endif |
ff192304 | 369 | t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET; |
e663b350 | 370 | |
7617f996 SG |
371 | /* bios_major_release has only one byte, so drop century */ |
372 | t->bios_major_release = U_BOOT_VERSION_NUM % 100; | |
373 | t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH; | |
721e992a BM |
374 | t->ec_major_release = 0xff; |
375 | t->ec_minor_release = 0xff; | |
376 | ||
fd3b826d | 377 | len = t->length + smbios_string_table_len(ctx); |
721e992a | 378 | *current += len; |
a2505fc8 | 379 | unmap_sysmem(t); |
721e992a BM |
380 | |
381 | return len; | |
382 | } | |
383 | ||
1e8989ad SG |
384 | static int smbios_write_type1(ulong *current, int handle, |
385 | struct smbios_ctx *ctx) | |
721e992a | 386 | { |
a2505fc8 | 387 | struct smbios_type1 *t; |
721e992a | 388 | int len = sizeof(struct smbios_type1); |
00caae6d | 389 | char *serial_str = env_get("serial#"); |
721e992a | 390 | |
a2505fc8 | 391 | t = map_sysmem(*current, len); |
721e992a BM |
392 | memset(t, 0, sizeof(struct smbios_type1)); |
393 | fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); | |
0c95fff3 | 394 | smbios_set_eos(ctx, t->eos); |
a5a57562 MS |
395 | t->manufacturer = smbios_add_prop_si(ctx, "manufacturer", |
396 | SYSINFO_ID_SMBIOS_SYSTEM_MANUFACTURER, | |
397 | NULL); | |
398 | t->product_name = smbios_add_prop_si(ctx, "product", | |
399 | SYSINFO_ID_SMBIOS_SYSTEM_PRODUCT, | |
400 | NULL); | |
07c9e683 | 401 | t->version = smbios_add_prop_si(ctx, "version", |
a986ccea | 402 | SYSINFO_ID_SMBIOS_SYSTEM_VERSION, |
6ebf9136 | 403 | NULL); |
6fb580d7 | 404 | if (serial_str) { |
a986ccea | 405 | t->serial_number = smbios_add_prop(ctx, NULL, serial_str); |
44ffb6f0 SG |
406 | strncpy((char *)t->uuid, serial_str, sizeof(t->uuid)); |
407 | } else { | |
a5a57562 MS |
408 | t->serial_number = smbios_add_prop_si(ctx, "serial", |
409 | SYSINFO_ID_SMBIOS_SYSTEM_SERIAL, | |
410 | NULL); | |
6fb580d7 | 411 | } |
6eca28b6 | 412 | t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN; |
a5a57562 MS |
413 | t->sku_number = smbios_add_prop_si(ctx, "sku", |
414 | SYSINFO_ID_SMBIOS_SYSTEM_SKU, NULL); | |
415 | t->family = smbios_add_prop_si(ctx, "family", | |
416 | SYSINFO_ID_SMBIOS_SYSTEM_FAMILY, NULL); | |
721e992a | 417 | |
fd3b826d | 418 | len = t->length + smbios_string_table_len(ctx); |
721e992a | 419 | *current += len; |
a2505fc8 | 420 | unmap_sysmem(t); |
721e992a BM |
421 | |
422 | return len; | |
423 | } | |
424 | ||
1e8989ad SG |
425 | static int smbios_write_type2(ulong *current, int handle, |
426 | struct smbios_ctx *ctx) | |
721e992a | 427 | { |
a2505fc8 | 428 | struct smbios_type2 *t; |
721e992a BM |
429 | int len = sizeof(struct smbios_type2); |
430 | ||
a2505fc8 | 431 | t = map_sysmem(*current, len); |
721e992a BM |
432 | memset(t, 0, sizeof(struct smbios_type2)); |
433 | fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); | |
0c95fff3 | 434 | smbios_set_eos(ctx, t->eos); |
a5a57562 MS |
435 | t->manufacturer = smbios_add_prop_si(ctx, "manufacturer", |
436 | SYSINFO_ID_SMBIOS_BASEBOARD_MANUFACTURER, | |
437 | NULL); | |
438 | t->product_name = smbios_add_prop_si(ctx, "product", | |
439 | SYSINFO_ID_SMBIOS_BASEBOARD_PRODUCT, | |
440 | NULL); | |
07c9e683 | 441 | t->version = smbios_add_prop_si(ctx, "version", |
a986ccea | 442 | SYSINFO_ID_SMBIOS_BASEBOARD_VERSION, |
6ebf9136 | 443 | NULL); |
a5a57562 MS |
444 | |
445 | t->serial_number = smbios_add_prop_si(ctx, "serial", | |
446 | SYSINFO_ID_SMBIOS_BASEBOARD_SERIAL, | |
447 | NULL); | |
448 | t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag", | |
449 | SYSINFO_ID_SMBIOS_BASEBOARD_ASSET_TAG, | |
450 | NULL); | |
721e992a BM |
451 | t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; |
452 | t->board_type = SMBIOS_BOARD_MOTHERBOARD; | |
5778c88e | 453 | t->chassis_handle = handle + 1; |
721e992a | 454 | |
fd3b826d | 455 | len = t->length + smbios_string_table_len(ctx); |
721e992a | 456 | *current += len; |
a2505fc8 | 457 | unmap_sysmem(t); |
721e992a BM |
458 | |
459 | return len; | |
460 | } | |
461 | ||
1e8989ad SG |
462 | static int smbios_write_type3(ulong *current, int handle, |
463 | struct smbios_ctx *ctx) | |
721e992a | 464 | { |
a2505fc8 | 465 | struct smbios_type3 *t; |
721e992a BM |
466 | int len = sizeof(struct smbios_type3); |
467 | ||
a2505fc8 | 468 | t = map_sysmem(*current, len); |
721e992a BM |
469 | memset(t, 0, sizeof(struct smbios_type3)); |
470 | fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); | |
0c95fff3 | 471 | smbios_set_eos(ctx, t->eos); |
6ebf9136 | 472 | t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL); |
721e992a BM |
473 | t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP; |
474 | t->bootup_state = SMBIOS_STATE_SAFE; | |
475 | t->power_supply_state = SMBIOS_STATE_SAFE; | |
476 | t->thermal_state = SMBIOS_STATE_SAFE; | |
477 | t->security_status = SMBIOS_SECURITY_NONE; | |
478 | ||
fd3b826d | 479 | len = t->length + smbios_string_table_len(ctx); |
721e992a | 480 | *current += len; |
a2505fc8 | 481 | unmap_sysmem(t); |
721e992a BM |
482 | |
483 | return len; | |
484 | } | |
485 | ||
1e8989ad SG |
486 | static void smbios_write_type4_dm(struct smbios_type4 *t, |
487 | struct smbios_ctx *ctx) | |
96476206 AG |
488 | { |
489 | u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN; | |
6ebf9136 HS |
490 | const char *vendor = NULL; |
491 | const char *name = NULL; | |
96476206 AG |
492 | |
493 | #ifdef CONFIG_CPU | |
494 | char processor_name[49]; | |
495 | char vendor_name[49]; | |
78227d4e | 496 | struct udevice *cpu = NULL; |
96476206 | 497 | |
78227d4e SG |
498 | uclass_find_first_device(UCLASS_CPU, &cpu); |
499 | if (cpu) { | |
8a8d24bd | 500 | struct cpu_plat *plat = dev_get_parent_plat(cpu); |
96476206 AG |
501 | |
502 | if (plat->family) | |
503 | processor_family = plat->family; | |
504 | t->processor_id[0] = plat->id[0]; | |
505 | t->processor_id[1] = plat->id[1]; | |
506 | ||
78227d4e | 507 | if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name))) |
96476206 | 508 | vendor = vendor_name; |
78227d4e | 509 | if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name))) |
96476206 AG |
510 | name = processor_name; |
511 | } | |
512 | #endif | |
513 | ||
0920bd50 HS |
514 | t->processor_family = 0xfe; |
515 | t->processor_family2 = processor_family; | |
a986ccea IA |
516 | t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor); |
517 | t->processor_version = smbios_add_prop(ctx, NULL, name); | |
96476206 AG |
518 | } |
519 | ||
1e8989ad SG |
520 | static int smbios_write_type4(ulong *current, int handle, |
521 | struct smbios_ctx *ctx) | |
721e992a | 522 | { |
a2505fc8 | 523 | struct smbios_type4 *t; |
721e992a | 524 | int len = sizeof(struct smbios_type4); |
721e992a | 525 | |
a2505fc8 | 526 | t = map_sysmem(*current, len); |
721e992a BM |
527 | memset(t, 0, sizeof(struct smbios_type4)); |
528 | fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); | |
0c95fff3 | 529 | smbios_set_eos(ctx, t->eos); |
721e992a | 530 | t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; |
1e8989ad | 531 | smbios_write_type4_dm(t, ctx); |
721e992a BM |
532 | t->status = SMBIOS_PROCESSOR_STATUS_ENABLED; |
533 | t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE; | |
534 | t->l1_cache_handle = 0xffff; | |
535 | t->l2_cache_handle = 0xffff; | |
536 | t->l3_cache_handle = 0xffff; | |
721e992a | 537 | |
fd3b826d | 538 | len = t->length + smbios_string_table_len(ctx); |
721e992a | 539 | *current += len; |
a2505fc8 | 540 | unmap_sysmem(t); |
721e992a BM |
541 | |
542 | return len; | |
543 | } | |
544 | ||
1e8989ad SG |
545 | static int smbios_write_type32(ulong *current, int handle, |
546 | struct smbios_ctx *ctx) | |
721e992a | 547 | { |
a2505fc8 | 548 | struct smbios_type32 *t; |
721e992a BM |
549 | int len = sizeof(struct smbios_type32); |
550 | ||
a2505fc8 | 551 | t = map_sysmem(*current, len); |
721e992a BM |
552 | memset(t, 0, sizeof(struct smbios_type32)); |
553 | fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); | |
0c95fff3 | 554 | smbios_set_eos(ctx, t->eos); |
721e992a BM |
555 | |
556 | *current += len; | |
a2505fc8 | 557 | unmap_sysmem(t); |
721e992a BM |
558 | |
559 | return len; | |
560 | } | |
561 | ||
1e8989ad SG |
562 | static int smbios_write_type127(ulong *current, int handle, |
563 | struct smbios_ctx *ctx) | |
721e992a | 564 | { |
a2505fc8 | 565 | struct smbios_type127 *t; |
721e992a BM |
566 | int len = sizeof(struct smbios_type127); |
567 | ||
a2505fc8 | 568 | t = map_sysmem(*current, len); |
721e992a BM |
569 | memset(t, 0, sizeof(struct smbios_type127)); |
570 | fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); | |
571 | ||
572 | *current += len; | |
a2505fc8 | 573 | unmap_sysmem(t); |
721e992a BM |
574 | |
575 | return len; | |
576 | } | |
577 | ||
44ffb6f0 | 578 | static struct smbios_write_method smbios_write_funcs[] = { |
e9adaa75 | 579 | { smbios_write_type0, "bios", }, |
44ffb6f0 SG |
580 | { smbios_write_type1, "system", }, |
581 | { smbios_write_type2, "baseboard", }, | |
5778c88e | 582 | /* Type 3 must immediately follow type 2 due to chassis handle. */ |
44ffb6f0 SG |
583 | { smbios_write_type3, "chassis", }, |
584 | { smbios_write_type4, }, | |
585 | { smbios_write_type32, }, | |
586 | { smbios_write_type127 }, | |
721e992a BM |
587 | }; |
588 | ||
42fd8c19 | 589 | ulong write_smbios_table(ulong addr) |
721e992a | 590 | { |
44ffb6f0 | 591 | ofnode parent_node = ofnode_null(); |
f19cf8d4 | 592 | ulong table_addr, start_addr; |
1c5f6fa3 | 593 | struct smbios3_entry *se; |
1e8989ad | 594 | struct smbios_ctx ctx; |
42fd8c19 | 595 | ulong tables; |
721e992a | 596 | int len = 0; |
721e992a | 597 | int handle = 0; |
721e992a BM |
598 | int i; |
599 | ||
1e8989ad | 600 | ctx.node = ofnode_null(); |
512ed81e | 601 | if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) { |
1e8989ad | 602 | uclass_first_device(UCLASS_SYSINFO, &ctx.dev); |
85df7f17 MS |
603 | if (ctx.dev) { |
604 | int ret; | |
605 | ||
1e8989ad | 606 | parent_node = dev_read_subnode(ctx.dev, "smbios"); |
85df7f17 | 607 | ret = sysinfo_detect(ctx.dev); |
4a8a54c3 SG |
608 | |
609 | /* | |
610 | * ignore the error since many boards don't implement | |
611 | * this and we can still use the info in the devicetree | |
612 | */ | |
613 | ret = log_msg_ret("sys", ret); | |
85df7f17 | 614 | } |
1e8989ad SG |
615 | } else { |
616 | ctx.dev = NULL; | |
78227d4e SG |
617 | } |
618 | ||
f19cf8d4 | 619 | start_addr = addr; |
721e992a | 620 | |
1c5f6fa3 SG |
621 | /* move past the (so-far-unwritten) header to start writing structs */ |
622 | addr = ALIGN(addr + sizeof(struct smbios3_entry), 16); | |
721e992a BM |
623 | tables = addr; |
624 | ||
625 | /* populate minimum required tables */ | |
626 | for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) { | |
44ffb6f0 | 627 | const struct smbios_write_method *method; |
44ffb6f0 SG |
628 | |
629 | method = &smbios_write_funcs[i]; | |
b6488caa IA |
630 | ctx.subnode_name = NULL; |
631 | if (method->subnode_name) { | |
632 | ctx.subnode_name = method->subnode_name; | |
633 | if (IS_ENABLED(CONFIG_OF_CONTROL)) | |
634 | ctx.node = ofnode_find_subnode(parent_node, | |
635 | method->subnode_name); | |
636 | } | |
e494258d | 637 | len += method->write((ulong *)&addr, handle++, &ctx); |
721e992a BM |
638 | } |
639 | ||
a2505fc8 SG |
640 | /* |
641 | * We must use a pointer here so things work correctly on sandbox. The | |
642 | * user of this table is not aware of the mapping of addresses to | |
643 | * sandbox's DRAM buffer. | |
644 | */ | |
645 | table_addr = (ulong)map_sysmem(tables, 0); | |
1c5f6fa3 SG |
646 | |
647 | /* now go back and write the SMBIOS3 header */ | |
ccefbf32 HS |
648 | se = map_sysmem(start_addr, sizeof(struct smbios3_entry)); |
649 | memset(se, '\0', sizeof(struct smbios3_entry)); | |
1c5f6fa3 SG |
650 | memcpy(se->anchor, "_SM3_", 5); |
651 | se->length = sizeof(struct smbios3_entry); | |
652 | se->major_ver = SMBIOS_MAJOR_VER; | |
653 | se->minor_ver = SMBIOS_MINOR_VER; | |
654 | se->doc_rev = 0; | |
655 | se->entry_point_rev = 1; | |
406c410e | 656 | se->table_maximum_size = len; |
1c5f6fa3 SG |
657 | se->struct_table_address = table_addr; |
658 | se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry)); | |
659 | unmap_sysmem(se); | |
721e992a BM |
660 | |
661 | return addr; | |
662 | } |