]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/smbios.c
doc/usage: cmd-usage help file for askenv
[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>
78227d4e 9#include <dm.h>
7b51b576 10#include <env.h>
a2505fc8 11#include <mapmem.h>
4b6dddc2 12#include <smbios.h>
07c9e683 13#include <sysinfo.h>
4b6dddc2 14#include <tables_csum.h>
721e992a 15#include <version.h>
96476206
AG
16#ifdef CONFIG_CPU
17#include <cpu.h>
96476206
AG
18#include <dm/uclass-internal.h>
19#endif
721e992a 20
e9adaa75
SG
21DECLARE_GLOBAL_DATA_PTR;
22
1e8989ad
SG
23/**
24 * struct smbios_ctx - context for writing SMBIOS tables
25 *
26 * @node: node containing the information to write (ofnode_null() if none)
27 * @dev: sysinfo device to use (NULL if none)
0c95fff3
SG
28 * @eos: end-of-string pointer for the table being processed. This is set
29 * up when we start processing a table
fd3b826d
SG
30 * @next_ptr: pointer to the start of the next string to be added. When the
31 * table is nopt empty, this points to the byte after the \0 of the
32 * previous string.
e9adaa75
SG
33 * @last_str: points to the last string that was written to the table, or NULL
34 * if none
1e8989ad
SG
35 */
36struct smbios_ctx {
37 ofnode node;
38 struct udevice *dev;
0c95fff3 39 char *eos;
fd3b826d 40 char *next_ptr;
e9adaa75 41 char *last_str;
1e8989ad
SG
42};
43
0e89b859
SG
44/**
45 * Function prototype to write a specific type of SMBIOS structure
46 *
47 * @addr: start address to write the structure
48 * @handle: the structure's handle, a unique 16-bit number
1e8989ad 49 * @ctx: context for writing the tables
8c6532d7 50 * Return: size of the structure
0e89b859 51 */
1e8989ad
SG
52typedef int (*smbios_write_type)(ulong *addr, int handle,
53 struct smbios_ctx *ctx);
0e89b859 54
44ffb6f0
SG
55/**
56 * struct smbios_write_method - Information about a table-writing function
57 *
58 * @write: Function to call
59 * @subnode_name: Name of subnode which has the information for this function,
60 * NULL if none
61 */
62struct smbios_write_method {
63 smbios_write_type write;
64 const char *subnode_name;
65};
66
721e992a
BM
67/**
68 * smbios_add_string() - add a string to the string area
69 *
70 * This adds a string to the string area which is appended directly after
71 * the formatted portion of an SMBIOS structure.
72 *
0c95fff3 73 * @ctx: SMBIOS context
721e992a 74 * @str: string to add
8c6532d7 75 * Return: string number in the string area (1 or more)
721e992a 76 */
0c95fff3 77static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
721e992a
BM
78{
79 int i = 1;
0c95fff3
SG
80 char *p = ctx->eos;
81
00a871d3
HS
82 if (!*str)
83 str = "Unknown";
721e992a
BM
84
85 for (;;) {
86 if (!*p) {
e9adaa75 87 ctx->last_str = p;
721e992a
BM
88 strcpy(p, str);
89 p += strlen(str);
90 *p++ = '\0';
fd3b826d 91 ctx->next_ptr = p;
721e992a
BM
92 *p++ = '\0';
93
94 return i;
95 }
96
e9adaa75
SG
97 if (!strcmp(p, str)) {
98 ctx->last_str = p;
721e992a 99 return i;
e9adaa75 100 }
721e992a
BM
101
102 p += strlen(p) + 1;
103 i++;
104 }
105}
106
44ffb6f0 107/**
07c9e683
SG
108 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
109 *
110 * Sysinfo is used if available, with a fallback to devicetree
44ffb6f0 111 *
1e8989ad 112 * @ctx: context for writing the tables
44ffb6f0 113 * @prop: property to write
8c6532d7 114 * Return: 0 if not found, else SMBIOS string number (1 or more)
44ffb6f0 115 */
07c9e683
SG
116static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
117 int sysinfo_id)
44ffb6f0 118{
07c9e683
SG
119 if (sysinfo_id && ctx->dev) {
120 char val[SMBIOS_STR_MAX];
121 int ret;
122
123 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
124 if (!ret)
125 return smbios_add_string(ctx, val);
126 }
e4f8e543
SG
127 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
128 const char *str;
129
1e8989ad 130 str = ofnode_read_string(ctx->node, prop);
e4f8e543 131 if (str)
0c95fff3 132 return smbios_add_string(ctx, str);
e4f8e543 133 }
44ffb6f0
SG
134
135 return 0;
136}
137
07c9e683
SG
138/**
139 * smbios_add_prop() - Add a property from the devicetree
140 *
141 * @prop: property to write
8c6532d7 142 * Return: 0 if not found, else SMBIOS string number (1 or more)
07c9e683
SG
143 */
144static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
145{
146 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE);
147}
148
0c95fff3
SG
149static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
150{
151 ctx->eos = eos;
fd3b826d 152 ctx->next_ptr = eos;
e9adaa75
SG
153 ctx->last_str = NULL;
154}
155
156int smbios_update_version(const char *version)
157{
158 char *ptr = gd->smbios_version;
159 uint old_len, len;
160
161 if (!ptr)
162 return log_ret(-ENOENT);
163
164 /*
165 * This string is supposed to have at least enough bytes and is
166 * padded with spaces. Update it, taking care not to move the
167 * \0 terminator, so that other strings in the string table
168 * are not disturbed. See smbios_add_string()
169 */
170 old_len = strnlen(ptr, SMBIOS_STR_MAX);
171 len = strnlen(version, SMBIOS_STR_MAX);
172 if (len > old_len)
173 return log_ret(-ENOSPC);
174
175 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
176 memcpy(ptr, version, len);
177#ifdef LOG_DEBUG
178 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
179#endif
180
181 return 0;
0c95fff3
SG
182}
183
721e992a
BM
184/**
185 * smbios_string_table_len() - compute the string area size
186 *
187 * This computes the size of the string area including the string terminator.
188 *
fd3b826d 189 * @ctx: SMBIOS context
8c6532d7 190 * Return: string area size
721e992a 191 */
fd3b826d 192static int smbios_string_table_len(const struct smbios_ctx *ctx)
721e992a 193{
fd3b826d
SG
194 /* Allow for the final \0 after all strings */
195 return (ctx->next_ptr + 1) - ctx->eos;
721e992a
BM
196}
197
1e8989ad
SG
198static int smbios_write_type0(ulong *current, int handle,
199 struct smbios_ctx *ctx)
721e992a 200{
a2505fc8 201 struct smbios_type0 *t;
721e992a
BM
202 int len = sizeof(struct smbios_type0);
203
a2505fc8 204 t = map_sysmem(*current, len);
721e992a
BM
205 memset(t, 0, sizeof(struct smbios_type0));
206 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
0c95fff3
SG
207 smbios_set_eos(ctx, t->eos);
208 t->vendor = smbios_add_string(ctx, "U-Boot");
e9adaa75
SG
209
210 t->bios_ver = smbios_add_prop(ctx, "version");
211 if (!t->bios_ver)
212 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
213 if (t->bios_ver)
214 gd->smbios_version = ctx->last_str;
215 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
216 gd->smbios_version);
217#ifdef LOG_DEBUG
218 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
219 1, strlen(gd->smbios_version) + 1, 0);
220#endif
0c95fff3 221 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
e663b350 222#ifdef CONFIG_ROM_SIZE
721e992a 223 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
e663b350 224#endif
721e992a
BM
225 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
226 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
227 BIOS_CHARACTERISTICS_UPGRADEABLE;
228#ifdef CONFIG_GENERATE_ACPI_TABLE
229 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
e663b350
AG
230#endif
231#ifdef CONFIG_EFI_LOADER
232 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
721e992a
BM
233#endif
234 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
e663b350 235
7617f996
SG
236 /* bios_major_release has only one byte, so drop century */
237 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
238 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
721e992a
BM
239 t->ec_major_release = 0xff;
240 t->ec_minor_release = 0xff;
241
fd3b826d 242 len = t->length + smbios_string_table_len(ctx);
721e992a 243 *current += len;
a2505fc8 244 unmap_sysmem(t);
721e992a
BM
245
246 return len;
247}
248
1e8989ad
SG
249static int smbios_write_type1(ulong *current, int handle,
250 struct smbios_ctx *ctx)
721e992a 251{
a2505fc8 252 struct smbios_type1 *t;
721e992a 253 int len = sizeof(struct smbios_type1);
00caae6d 254 char *serial_str = env_get("serial#");
721e992a 255
a2505fc8 256 t = map_sysmem(*current, len);
721e992a
BM
257 memset(t, 0, sizeof(struct smbios_type1));
258 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
0c95fff3
SG
259 smbios_set_eos(ctx, t->eos);
260 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
261 t->product_name = smbios_add_prop(ctx, "product");
07c9e683
SG
262 t->version = smbios_add_prop_si(ctx, "version",
263 SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
6fb580d7 264 if (serial_str) {
0c95fff3 265 t->serial_number = smbios_add_string(ctx, serial_str);
44ffb6f0
SG
266 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
267 } else {
0c95fff3 268 t->serial_number = smbios_add_prop(ctx, "serial");
6fb580d7 269 }
0c95fff3
SG
270 t->sku_number = smbios_add_prop(ctx, "sku");
271 t->family = smbios_add_prop(ctx, "family");
721e992a 272
fd3b826d 273 len = t->length + smbios_string_table_len(ctx);
721e992a 274 *current += len;
a2505fc8 275 unmap_sysmem(t);
721e992a
BM
276
277 return len;
278}
279
1e8989ad
SG
280static int smbios_write_type2(ulong *current, int handle,
281 struct smbios_ctx *ctx)
721e992a 282{
a2505fc8 283 struct smbios_type2 *t;
721e992a
BM
284 int len = sizeof(struct smbios_type2);
285
a2505fc8 286 t = map_sysmem(*current, len);
721e992a
BM
287 memset(t, 0, sizeof(struct smbios_type2));
288 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
0c95fff3
SG
289 smbios_set_eos(ctx, t->eos);
290 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
291 t->product_name = smbios_add_prop(ctx, "product");
07c9e683
SG
292 t->version = smbios_add_prop_si(ctx, "version",
293 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
0c95fff3 294 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
721e992a
BM
295 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
296 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
297
fd3b826d 298 len = t->length + smbios_string_table_len(ctx);
721e992a 299 *current += len;
a2505fc8 300 unmap_sysmem(t);
721e992a
BM
301
302 return len;
303}
304
1e8989ad
SG
305static int smbios_write_type3(ulong *current, int handle,
306 struct smbios_ctx *ctx)
721e992a 307{
a2505fc8 308 struct smbios_type3 *t;
721e992a
BM
309 int len = sizeof(struct smbios_type3);
310
a2505fc8 311 t = map_sysmem(*current, len);
721e992a
BM
312 memset(t, 0, sizeof(struct smbios_type3));
313 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
0c95fff3
SG
314 smbios_set_eos(ctx, t->eos);
315 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
721e992a
BM
316 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
317 t->bootup_state = SMBIOS_STATE_SAFE;
318 t->power_supply_state = SMBIOS_STATE_SAFE;
319 t->thermal_state = SMBIOS_STATE_SAFE;
320 t->security_status = SMBIOS_SECURITY_NONE;
321
fd3b826d 322 len = t->length + smbios_string_table_len(ctx);
721e992a 323 *current += len;
a2505fc8 324 unmap_sysmem(t);
721e992a
BM
325
326 return len;
327}
328
1e8989ad
SG
329static void smbios_write_type4_dm(struct smbios_type4 *t,
330 struct smbios_ctx *ctx)
96476206
AG
331{
332 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
333 const char *vendor = "Unknown";
334 const char *name = "Unknown";
335
336#ifdef CONFIG_CPU
337 char processor_name[49];
338 char vendor_name[49];
78227d4e 339 struct udevice *cpu = NULL;
96476206 340
78227d4e
SG
341 uclass_find_first_device(UCLASS_CPU, &cpu);
342 if (cpu) {
8a8d24bd 343 struct cpu_plat *plat = dev_get_parent_plat(cpu);
96476206
AG
344
345 if (plat->family)
346 processor_family = plat->family;
347 t->processor_id[0] = plat->id[0];
348 t->processor_id[1] = plat->id[1];
349
78227d4e 350 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
96476206 351 vendor = vendor_name;
78227d4e 352 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
96476206
AG
353 name = processor_name;
354 }
355#endif
356
357 t->processor_family = processor_family;
0c95fff3
SG
358 t->processor_manufacturer = smbios_add_string(ctx, vendor);
359 t->processor_version = smbios_add_string(ctx, name);
96476206
AG
360}
361
1e8989ad
SG
362static int smbios_write_type4(ulong *current, int handle,
363 struct smbios_ctx *ctx)
721e992a 364{
a2505fc8 365 struct smbios_type4 *t;
721e992a 366 int len = sizeof(struct smbios_type4);
721e992a 367
a2505fc8 368 t = map_sysmem(*current, len);
721e992a
BM
369 memset(t, 0, sizeof(struct smbios_type4));
370 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
0c95fff3 371 smbios_set_eos(ctx, t->eos);
721e992a 372 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
1e8989ad 373 smbios_write_type4_dm(t, ctx);
721e992a
BM
374 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
375 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
376 t->l1_cache_handle = 0xffff;
377 t->l2_cache_handle = 0xffff;
378 t->l3_cache_handle = 0xffff;
379 t->processor_family2 = t->processor_family;
380
fd3b826d 381 len = t->length + smbios_string_table_len(ctx);
721e992a 382 *current += len;
a2505fc8 383 unmap_sysmem(t);
721e992a
BM
384
385 return len;
386}
387
1e8989ad
SG
388static int smbios_write_type32(ulong *current, int handle,
389 struct smbios_ctx *ctx)
721e992a 390{
a2505fc8 391 struct smbios_type32 *t;
721e992a
BM
392 int len = sizeof(struct smbios_type32);
393
a2505fc8 394 t = map_sysmem(*current, len);
721e992a
BM
395 memset(t, 0, sizeof(struct smbios_type32));
396 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
0c95fff3 397 smbios_set_eos(ctx, t->eos);
721e992a
BM
398
399 *current += len;
a2505fc8 400 unmap_sysmem(t);
721e992a
BM
401
402 return len;
403}
404
1e8989ad
SG
405static int smbios_write_type127(ulong *current, int handle,
406 struct smbios_ctx *ctx)
721e992a 407{
a2505fc8 408 struct smbios_type127 *t;
721e992a
BM
409 int len = sizeof(struct smbios_type127);
410
a2505fc8 411 t = map_sysmem(*current, len);
721e992a
BM
412 memset(t, 0, sizeof(struct smbios_type127));
413 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
414
415 *current += len;
a2505fc8 416 unmap_sysmem(t);
721e992a
BM
417
418 return len;
419}
420
44ffb6f0 421static struct smbios_write_method smbios_write_funcs[] = {
e9adaa75 422 { smbios_write_type0, "bios", },
44ffb6f0
SG
423 { smbios_write_type1, "system", },
424 { smbios_write_type2, "baseboard", },
425 { smbios_write_type3, "chassis", },
426 { smbios_write_type4, },
427 { smbios_write_type32, },
428 { smbios_write_type127 },
721e992a
BM
429};
430
42fd8c19 431ulong write_smbios_table(ulong addr)
721e992a 432{
44ffb6f0 433 ofnode parent_node = ofnode_null();
721e992a 434 struct smbios_entry *se;
1e8989ad 435 struct smbios_ctx ctx;
a2505fc8 436 ulong table_addr;
42fd8c19 437 ulong tables;
721e992a
BM
438 int len = 0;
439 int max_struct_size = 0;
440 int handle = 0;
441 char *istart;
442 int isize;
443 int i;
444
1e8989ad 445 ctx.node = ofnode_null();
78227d4e 446 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
1e8989ad
SG
447 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
448 if (ctx.dev)
449 parent_node = dev_read_subnode(ctx.dev, "smbios");
450 } else {
451 ctx.dev = NULL;
78227d4e
SG
452 }
453
721e992a
BM
454 /* 16 byte align the table address */
455 addr = ALIGN(addr, 16);
456
a2505fc8 457 se = map_sysmem(addr, sizeof(struct smbios_entry));
721e992a
BM
458 memset(se, 0, sizeof(struct smbios_entry));
459
460 addr += sizeof(struct smbios_entry);
461 addr = ALIGN(addr, 16);
462 tables = addr;
463
464 /* populate minimum required tables */
465 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
44ffb6f0 466 const struct smbios_write_method *method;
44ffb6f0
SG
467 int tmp;
468
469 method = &smbios_write_funcs[i];
470 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
1e8989ad
SG
471 ctx.node = ofnode_find_subnode(parent_node,
472 method->subnode_name);
473 tmp = method->write((ulong *)&addr, handle++, &ctx);
60a4df32 474
721e992a
BM
475 max_struct_size = max(max_struct_size, tmp);
476 len += tmp;
477 }
478
479 memcpy(se->anchor, "_SM_", 4);
480 se->length = sizeof(struct smbios_entry);
481 se->major_ver = SMBIOS_MAJOR_VER;
482 se->minor_ver = SMBIOS_MINOR_VER;
483 se->max_struct_size = max_struct_size;
484 memcpy(se->intermediate_anchor, "_DMI_", 5);
485 se->struct_table_length = len;
a2505fc8
SG
486
487 /*
488 * We must use a pointer here so things work correctly on sandbox. The
489 * user of this table is not aware of the mapping of addresses to
490 * sandbox's DRAM buffer.
491 */
492 table_addr = (ulong)map_sysmem(tables, 0);
493 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
494 /*
495 * We need to put this >32-bit pointer into the table but the
496 * field is only 32 bits wide.
497 */
498 printf("WARNING: SMBIOS table_address overflow %llx\n",
499 (unsigned long long)table_addr);
500 table_addr = 0;
501 }
502 se->struct_table_address = table_addr;
503
721e992a
BM
504 se->struct_count = handle;
505
506 /* calculate checksums */
507 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
508 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
509 se->intermediate_checksum = table_compute_checksum(istart, isize);
510 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
a2505fc8 511 unmap_sysmem(se);
721e992a
BM
512
513 return addr;
514}