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