blob: 3f0e1d529537f5f0bfbcacb5499c995103843302 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng721e9922015-10-12 05:23:41 -07002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * Adapted from coreboot src/arch/x86/smbios.c
Bin Meng721e9922015-10-12 05:23:41 -07006 */
7
8#include <common.h>
Simon Glass78227d42020-11-05 06:32:07 -07009#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Pali Rohár11275e42021-04-22 18:09:57 +020011#include <linux/stringify.h>
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +020012#include <linux/string.h>
Simon Glassa2505fc2018-11-22 13:46:37 -070013#include <mapmem.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020014#include <smbios.h>
Simon Glass07c9e682021-02-04 21:17:23 -070015#include <sysinfo.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020016#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070017#include <version.h>
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +020018#include <malloc.h>
19#include <dm/ofnode.h>
Alexander Graf96476202016-08-19 01:23:28 +020020#ifdef CONFIG_CPU
21#include <cpu.h>
Alexander Graf96476202016-08-19 01:23:28 +020022#include <dm/uclass-internal.h>
23#endif
Bin Meng721e9922015-10-12 05:23:41 -070024
Pali Rohár11275e42021-04-22 18:09:57 +020025/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
26#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
27 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
28#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
29#endif
30
31/*
32 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
33 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
34 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
35 * BIOS Release Date should contain date when code was released
36 * and not when it was built or compiled.
37 */
38#if U_BOOT_VERSION_NUM_PATCH < 10
39#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
40#else
41#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
42#endif
43#define U_BOOT_DMI_DAY "01"
44#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
45#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
46
Simon Glasse9adaa72021-02-04 21:17:20 -070047DECLARE_GLOBAL_DATA_PTR;
48
Bin Meng721e9922015-10-12 05:23:41 -070049/**
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +020050 * struct map_sysinfo - Mapping of sysinfo strings to DT
51 *
52 * @sysinfo_str: sysinfo string
53 * @dt_str: DT string
54 * @max: Max index of the tokenized string to pick. Counting starts from 0
55 *
56 */
57struct map_sysinfo {
58 const char *sysinfo_str;
59 const char *dt_str;
60 int max;
61};
62
63static const struct map_sysinfo sysinfo_to_dt[] = {
64 { .sysinfo_str = "product", .dt_str = "model", 2 },
65 { .sysinfo_str = "manufacturer", .dt_str = "compatible", 1 },
66};
67
68/**
Simon Glass1e8989a2021-02-04 21:17:17 -070069 * struct smbios_ctx - context for writing SMBIOS tables
70 *
71 * @node: node containing the information to write (ofnode_null() if none)
72 * @dev: sysinfo device to use (NULL if none)
Simon Glass0c95fff2021-02-04 21:17:18 -070073 * @eos: end-of-string pointer for the table being processed. This is set
74 * up when we start processing a table
Simon Glassfd3b8262021-02-04 21:17:19 -070075 * @next_ptr: pointer to the start of the next string to be added. When the
76 * table is nopt empty, this points to the byte after the \0 of the
77 * previous string.
Simon Glasse9adaa72021-02-04 21:17:20 -070078 * @last_str: points to the last string that was written to the table, or NULL
79 * if none
Simon Glass1e8989a2021-02-04 21:17:17 -070080 */
81struct smbios_ctx {
82 ofnode node;
83 struct udevice *dev;
Simon Glass0c95fff2021-02-04 21:17:18 -070084 char *eos;
Simon Glassfd3b8262021-02-04 21:17:19 -070085 char *next_ptr;
Simon Glasse9adaa72021-02-04 21:17:20 -070086 char *last_str;
Simon Glass1e8989a2021-02-04 21:17:17 -070087};
88
89/**
Simon Glass0e89b852021-02-04 21:17:14 -070090 * Function prototype to write a specific type of SMBIOS structure
91 *
92 * @addr: start address to write the structure
93 * @handle: the structure's handle, a unique 16-bit number
Simon Glass1e8989a2021-02-04 21:17:17 -070094 * @ctx: context for writing the tables
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +020095 * Return: size of the structure
Simon Glass0e89b852021-02-04 21:17:14 -070096 */
Simon Glass1e8989a2021-02-04 21:17:17 -070097typedef int (*smbios_write_type)(ulong *addr, int handle,
98 struct smbios_ctx *ctx);
Simon Glass0e89b852021-02-04 21:17:14 -070099
100/**
Simon Glass44ffb6f2020-11-05 06:32:08 -0700101 * struct smbios_write_method - Information about a table-writing function
102 *
103 * @write: Function to call
104 * @subnode_name: Name of subnode which has the information for this function,
105 * NULL if none
106 */
107struct smbios_write_method {
108 smbios_write_type write;
109 const char *subnode_name;
110};
111
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200112static const struct map_sysinfo *convert_sysinfo_to_dt(const char *sysinfo_str)
113{
114 int i;
115
116 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
117 if (!strcmp(sysinfo_str, sysinfo_to_dt[i].sysinfo_str))
118 return &sysinfo_to_dt[i];
119 }
120
121 return NULL;
122}
123
Simon Glass44ffb6f2020-11-05 06:32:08 -0700124/**
Bin Meng721e9922015-10-12 05:23:41 -0700125 * smbios_add_string() - add a string to the string area
126 *
127 * This adds a string to the string area which is appended directly after
128 * the formatted portion of an SMBIOS structure.
129 *
Simon Glass0c95fff2021-02-04 21:17:18 -0700130 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -0700131 * @str: string to add
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200132 * Return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -0700133 */
Simon Glass0c95fff2021-02-04 21:17:18 -0700134static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Meng721e9922015-10-12 05:23:41 -0700135{
136 int i = 1;
Simon Glass0c95fff2021-02-04 21:17:18 -0700137 char *p = ctx->eos;
138
Bin Meng721e9922015-10-12 05:23:41 -0700139 for (;;) {
140 if (!*p) {
Simon Glasse9adaa72021-02-04 21:17:20 -0700141 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -0700142 strcpy(p, str);
143 p += strlen(str);
144 *p++ = '\0';
Simon Glassfd3b8262021-02-04 21:17:19 -0700145 ctx->next_ptr = p;
Bin Meng721e9922015-10-12 05:23:41 -0700146 *p++ = '\0';
147
148 return i;
149 }
150
Simon Glasse9adaa72021-02-04 21:17:20 -0700151 if (!strcmp(p, str)) {
152 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -0700153 return i;
Simon Glasse9adaa72021-02-04 21:17:20 -0700154 }
Bin Meng721e9922015-10-12 05:23:41 -0700155
156 p += strlen(p) + 1;
157 i++;
158 }
159}
160
161/**
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200162 * get_str_from_dt - Get a substring from a DT property.
163 * After finding the property in the DT, the function
164 * will parse comma-separated values and return the value.
165 * If nprop->max exceeds the number of comma-separated
166 * elements, the last non NULL value will be returned.
167 * Counting starts from zero.
168 *
169 * @nprop: sysinfo property to use
170 * @str: pointer to fill with data
171 * @size: str buffer length
172 */
173static
174void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
175{
176 const char *dt_str;
177 int cnt = 0;
178 char *token;
179
180 memset(str, 0, size);
181 if (!nprop || !nprop->max)
182 return;
183
184 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
185 if (!dt_str)
186 return;
187
188 memcpy(str, dt_str, size);
189 token = strtok(str, ",");
190 while (token && cnt < nprop->max) {
191 strlcpy(str, token, strlen(token) + 1);
192 token = strtok(NULL, ",");
193 cnt++;
194 }
195}
196
197/**
Simon Glass07c9e682021-02-04 21:17:23 -0700198 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
199 *
200 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass44ffb6f2020-11-05 06:32:08 -0700201 *
Simon Glass1e8989a2021-02-04 21:17:17 -0700202 * @ctx: context for writing the tables
Simon Glass44ffb6f2020-11-05 06:32:08 -0700203 * @prop: property to write
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200204 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200205 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700206 */
Simon Glass07c9e682021-02-04 21:17:23 -0700207static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200208 int sysinfo_id, const char *dval)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700209{
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200210 int ret;
211
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200212 if (!dval || !*dval)
213 dval = "Unknown";
214
215 if (!prop)
216 return smbios_add_string(ctx, dval);
217
Simon Glass07c9e682021-02-04 21:17:23 -0700218 if (sysinfo_id && ctx->dev) {
219 char val[SMBIOS_STR_MAX];
Simon Glass07c9e682021-02-04 21:17:23 -0700220
221 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
222 if (!ret)
223 return smbios_add_string(ctx, val);
224 }
Simon Glasse4f8e542020-11-05 06:32:18 -0700225 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200226 const char *str = NULL;
227 char str_dt[128] = { 0 };
228 /*
229 * If the node is not valid fallback and try the entire DT
230 * so we can at least fill in manufacturer and board type
231 */
232 if (ofnode_valid(ctx->node)) {
233 str = ofnode_read_string(ctx->node, prop);
234 } else {
235 const struct map_sysinfo *nprop;
Simon Glasse4f8e542020-11-05 06:32:18 -0700236
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200237 nprop = convert_sysinfo_to_dt(prop);
238 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
239 str = (const char *)str_dt;
240 }
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200241
Ilias Apalodimas4e2b80f2023-12-07 11:18:50 +0200242 ret = smbios_add_string(ctx, str && *str ? str : dval);
243 return ret;
Simon Glasse4f8e542020-11-05 06:32:18 -0700244 }
245
246 return 0;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700247}
248
Simon Glass07c9e682021-02-04 21:17:23 -0700249/**
250 * smbios_add_prop() - Add a property from the devicetree
251 *
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200252 * @prop: property to write. The default string will be written if
253 * prop is NULL
254 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200255 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass07c9e682021-02-04 21:17:23 -0700256 */
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200257static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
258 const char *dval)
Simon Glass07c9e682021-02-04 21:17:23 -0700259{
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200260 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval);
Simon Glass07c9e682021-02-04 21:17:23 -0700261}
262
Simon Glass0c95fff2021-02-04 21:17:18 -0700263static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
264{
265 ctx->eos = eos;
Simon Glassfd3b8262021-02-04 21:17:19 -0700266 ctx->next_ptr = eos;
Simon Glasse9adaa72021-02-04 21:17:20 -0700267 ctx->last_str = NULL;
268}
269
270int smbios_update_version(const char *version)
271{
272 char *ptr = gd->smbios_version;
273 uint old_len, len;
274
275 if (!ptr)
276 return log_ret(-ENOENT);
277
278 /*
279 * This string is supposed to have at least enough bytes and is
280 * padded with spaces. Update it, taking care not to move the
281 * \0 terminator, so that other strings in the string table
282 * are not disturbed. See smbios_add_string()
283 */
284 old_len = strnlen(ptr, SMBIOS_STR_MAX);
285 len = strnlen(version, SMBIOS_STR_MAX);
286 if (len > old_len)
287 return log_ret(-ENOSPC);
288
289 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
290 memcpy(ptr, version, len);
291#ifdef LOG_DEBUG
292 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
293#endif
294
295 return 0;
Simon Glass0c95fff2021-02-04 21:17:18 -0700296}
297
Simon Glass44ffb6f2020-11-05 06:32:08 -0700298/**
Bin Meng721e9922015-10-12 05:23:41 -0700299 * smbios_string_table_len() - compute the string area size
300 *
301 * This computes the size of the string area including the string terminator.
302 *
Simon Glassfd3b8262021-02-04 21:17:19 -0700303 * @ctx: SMBIOS context
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200304 * Return: string area size
Bin Meng721e9922015-10-12 05:23:41 -0700305 */
Simon Glassfd3b8262021-02-04 21:17:19 -0700306static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700307{
Simon Glassfd3b8262021-02-04 21:17:19 -0700308 /* Allow for the final \0 after all strings */
309 return (ctx->next_ptr + 1) - ctx->eos;
Bin Meng721e9922015-10-12 05:23:41 -0700310}
311
Simon Glass1e8989a2021-02-04 21:17:17 -0700312static int smbios_write_type0(ulong *current, int handle,
313 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700314{
Simon Glassa2505fc2018-11-22 13:46:37 -0700315 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700316 int len = sizeof(struct smbios_type0);
317
Simon Glassa2505fc2018-11-22 13:46:37 -0700318 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700319 memset(t, 0, sizeof(struct smbios_type0));
320 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700321 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200322 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glasse9adaa72021-02-04 21:17:20 -0700323
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200324 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glasse9adaa72021-02-04 21:17:20 -0700325 if (t->bios_ver)
326 gd->smbios_version = ctx->last_str;
327 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
328 gd->smbios_version);
329#ifdef LOG_DEBUG
330 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
331 1, strlen(gd->smbios_version) + 1, 0);
332#endif
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200333 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +0200334#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -0700335 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +0200336#endif
Bin Meng721e9922015-10-12 05:23:41 -0700337 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
338 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
339 BIOS_CHARACTERISTICS_UPGRADEABLE;
340#ifdef CONFIG_GENERATE_ACPI_TABLE
341 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
342#endif
Alexander Grafe663b352016-08-19 01:23:29 +0200343#ifdef CONFIG_EFI_LOADER
Ilias Apalodimasff192302021-06-09 18:14:47 +0300344 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Grafe663b352016-08-19 01:23:29 +0200345#endif
Ilias Apalodimasff192302021-06-09 18:14:47 +0300346 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200347
Simon Glass7617f992021-02-04 21:17:16 -0700348 /* bios_major_release has only one byte, so drop century */
349 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
350 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Meng721e9922015-10-12 05:23:41 -0700351 t->ec_major_release = 0xff;
352 t->ec_minor_release = 0xff;
353
Simon Glassfd3b8262021-02-04 21:17:19 -0700354 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700355 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700356 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700357
358 return len;
359}
360
Simon Glass1e8989a2021-02-04 21:17:17 -0700361static int smbios_write_type1(ulong *current, int handle,
362 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700363{
Simon Glassa2505fc2018-11-22 13:46:37 -0700364 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700365 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600366 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700367
Simon Glassa2505fc2018-11-22 13:46:37 -0700368 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700369 memset(t, 0, sizeof(struct smbios_type1));
370 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700371 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200372 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
373 t->product_name = smbios_add_prop(ctx, "product", "Unknown");
Simon Glass07c9e682021-02-04 21:17:23 -0700374 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200375 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
376 "Unknown");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200377 if (serial_str) {
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200378 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700379 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
380 } else {
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200381 t->serial_number = smbios_add_prop(ctx, "serial", "Unknown");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200382 }
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200383 t->sku_number = smbios_add_prop(ctx, "sku", "Unknown");
384 t->family = smbios_add_prop(ctx, "family", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700385
Simon Glassfd3b8262021-02-04 21:17:19 -0700386 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700387 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700388 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700389
390 return len;
391}
392
Simon Glass1e8989a2021-02-04 21:17:17 -0700393static int smbios_write_type2(ulong *current, int handle,
394 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700395{
Simon Glassa2505fc2018-11-22 13:46:37 -0700396 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700397 int len = sizeof(struct smbios_type2);
398
Simon Glassa2505fc2018-11-22 13:46:37 -0700399 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700400 memset(t, 0, sizeof(struct smbios_type2));
401 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700402 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200403 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
404 t->product_name = smbios_add_prop(ctx, "product", "Unknown");
Simon Glass07c9e682021-02-04 21:17:23 -0700405 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200406 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
407 "Unknown");
408 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700409 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
410 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
411
Simon Glassfd3b8262021-02-04 21:17:19 -0700412 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700413 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700414 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700415
416 return len;
417}
418
Simon Glass1e8989a2021-02-04 21:17:17 -0700419static int smbios_write_type3(ulong *current, int handle,
420 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700421{
Simon Glassa2505fc2018-11-22 13:46:37 -0700422 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700423 int len = sizeof(struct smbios_type3);
424
Simon Glassa2505fc2018-11-22 13:46:37 -0700425 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700426 memset(t, 0, sizeof(struct smbios_type3));
427 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700428 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200429 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700430 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
431 t->bootup_state = SMBIOS_STATE_SAFE;
432 t->power_supply_state = SMBIOS_STATE_SAFE;
433 t->thermal_state = SMBIOS_STATE_SAFE;
434 t->security_status = SMBIOS_SECURITY_NONE;
435
Simon Glassfd3b8262021-02-04 21:17:19 -0700436 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700437 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700438 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700439
440 return len;
441}
442
Simon Glass1e8989a2021-02-04 21:17:17 -0700443static void smbios_write_type4_dm(struct smbios_type4 *t,
444 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200445{
446 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
447 const char *vendor = "Unknown";
448 const char *name = "Unknown";
449
450#ifdef CONFIG_CPU
451 char processor_name[49];
452 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700453 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200454
Simon Glass78227d42020-11-05 06:32:07 -0700455 uclass_find_first_device(UCLASS_CPU, &cpu);
456 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700457 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200458
459 if (plat->family)
460 processor_family = plat->family;
461 t->processor_id[0] = plat->id[0];
462 t->processor_id[1] = plat->id[1];
463
Simon Glass78227d42020-11-05 06:32:07 -0700464 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200465 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700466 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200467 name = processor_name;
468 }
469#endif
470
471 t->processor_family = processor_family;
Ilias Apalodimas9d823882023-12-07 11:18:49 +0200472 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
473 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Graf96476202016-08-19 01:23:28 +0200474}
475
Simon Glass1e8989a2021-02-04 21:17:17 -0700476static int smbios_write_type4(ulong *current, int handle,
477 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700478{
Simon Glassa2505fc2018-11-22 13:46:37 -0700479 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700480 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700481
Simon Glassa2505fc2018-11-22 13:46:37 -0700482 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700483 memset(t, 0, sizeof(struct smbios_type4));
484 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700485 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700486 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700487 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700488 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
489 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
490 t->l1_cache_handle = 0xffff;
491 t->l2_cache_handle = 0xffff;
492 t->l3_cache_handle = 0xffff;
493 t->processor_family2 = t->processor_family;
494
Simon Glassfd3b8262021-02-04 21:17:19 -0700495 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700496 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700497 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700498
499 return len;
500}
501
Simon Glass1e8989a2021-02-04 21:17:17 -0700502static int smbios_write_type32(ulong *current, int handle,
503 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700504{
Simon Glassa2505fc2018-11-22 13:46:37 -0700505 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700506 int len = sizeof(struct smbios_type32);
507
Simon Glassa2505fc2018-11-22 13:46:37 -0700508 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700509 memset(t, 0, sizeof(struct smbios_type32));
510 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700511 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700512
513 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700514 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700515
516 return len;
517}
518
Simon Glass1e8989a2021-02-04 21:17:17 -0700519static int smbios_write_type127(ulong *current, int handle,
520 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700521{
Simon Glassa2505fc2018-11-22 13:46:37 -0700522 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700523 int len = sizeof(struct smbios_type127);
524
Simon Glassa2505fc2018-11-22 13:46:37 -0700525 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700526 memset(t, 0, sizeof(struct smbios_type127));
527 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
528
529 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700530 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700531
532 return len;
533}
534
Simon Glass44ffb6f2020-11-05 06:32:08 -0700535static struct smbios_write_method smbios_write_funcs[] = {
Simon Glasse9adaa72021-02-04 21:17:20 -0700536 { smbios_write_type0, "bios", },
Simon Glass44ffb6f2020-11-05 06:32:08 -0700537 { smbios_write_type1, "system", },
538 { smbios_write_type2, "baseboard", },
539 { smbios_write_type3, "chassis", },
540 { smbios_write_type4, },
541 { smbios_write_type32, },
542 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700543};
544
Simon Glass42fd8c12017-01-16 07:03:35 -0700545ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700546{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700547 ofnode parent_node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700548 struct smbios_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700549 struct smbios_ctx ctx;
Simon Glassa2505fc2018-11-22 13:46:37 -0700550 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700551 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700552 int len = 0;
553 int max_struct_size = 0;
554 int handle = 0;
555 char *istart;
556 int isize;
557 int i;
558
Simon Glass1e8989a2021-02-04 21:17:17 -0700559 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700560 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700561 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
562 if (ctx.dev)
563 parent_node = dev_read_subnode(ctx.dev, "smbios");
564 } else {
565 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700566 }
567
Bin Meng721e9922015-10-12 05:23:41 -0700568 /* 16 byte align the table address */
569 addr = ALIGN(addr, 16);
570
Simon Glassa2505fc2018-11-22 13:46:37 -0700571 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700572 memset(se, 0, sizeof(struct smbios_entry));
573
574 addr += sizeof(struct smbios_entry);
575 addr = ALIGN(addr, 16);
576 tables = addr;
577
578 /* populate minimum required tables */
579 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700580 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700581 int tmp;
582
583 method = &smbios_write_funcs[i];
584 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass1e8989a2021-02-04 21:17:17 -0700585 ctx.node = ofnode_find_subnode(parent_node,
586 method->subnode_name);
587 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200588
Bin Meng721e9922015-10-12 05:23:41 -0700589 max_struct_size = max(max_struct_size, tmp);
590 len += tmp;
591 }
592
593 memcpy(se->anchor, "_SM_", 4);
594 se->length = sizeof(struct smbios_entry);
595 se->major_ver = SMBIOS_MAJOR_VER;
596 se->minor_ver = SMBIOS_MINOR_VER;
597 se->max_struct_size = max_struct_size;
598 memcpy(se->intermediate_anchor, "_DMI_", 5);
599 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700600
601 /*
602 * We must use a pointer here so things work correctly on sandbox. The
603 * user of this table is not aware of the mapping of addresses to
604 * sandbox's DRAM buffer.
605 */
606 table_addr = (ulong)map_sysmem(tables, 0);
607 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
608 /*
609 * We need to put this >32-bit pointer into the table but the
610 * field is only 32 bits wide.
611 */
612 printf("WARNING: SMBIOS table_address overflow %llx\n",
613 (unsigned long long)table_addr);
Heinrich Schuchardtc193d9b2021-05-15 18:07:47 +0200614 addr = 0;
615 goto out;
Simon Glassa2505fc2018-11-22 13:46:37 -0700616 }
617 se->struct_table_address = table_addr;
618
Bin Meng721e9922015-10-12 05:23:41 -0700619 se->struct_count = handle;
620
621 /* calculate checksums */
622 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
623 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
624 se->intermediate_checksum = table_compute_checksum(istart, isize);
625 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Heinrich Schuchardtc193d9b2021-05-15 18:07:47 +0200626out:
Simon Glassa2505fc2018-11-22 13:46:37 -0700627 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700628
629 return addr;
630}