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