blob: 78cee8c0c2642c7d9fc534b59a20151641559e49 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Mengae5bedb2015-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 Mengae5bedb2015-10-12 05:23:41 -07006 */
7
Simon Glassbd5515d2024-06-23 14:30:33 -06008#define LOG_CATEGORY LOGC_BOARD
9
Peng Fana98a25e2024-12-03 21:42:57 +080010#include <display_options.h>
Simon Glass0dd7ca22020-11-05 06:32:07 -070011#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Pali Rohárebe9fd82021-04-22 18:09:57 +020013#include <linux/stringify.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020014#include <linux/string.h>
Simon Glass2d93add2018-11-22 13:46:37 -070015#include <mapmem.h>
Alexander Graffb228082016-08-19 01:23:23 +020016#include <smbios.h>
Simon Glasscf67d6d2021-02-04 21:17:23 -070017#include <sysinfo.h>
Alexander Graffb228082016-08-19 01:23:23 +020018#include <tables_csum.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070019#include <version.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020020#include <malloc.h>
21#include <dm/ofnode.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020022#ifdef CONFIG_CPU
23#include <cpu.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020024#include <dm/uclass-internal.h>
25#endif
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +020026#include <linux/sizes.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070027
Pali Rohárebe9fd82021-04-22 18:09:57 +020028/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
29#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
30 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
31#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
32#endif
33
34/*
35 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
36 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
37 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
38 * BIOS Release Date should contain date when code was released
39 * and not when it was built or compiled.
40 */
41#if U_BOOT_VERSION_NUM_PATCH < 10
42#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
43#else
44#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
45#endif
46#define U_BOOT_DMI_DAY "01"
47#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
48#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
49
Simon Glassa05eb042021-02-04 21:17:20 -070050DECLARE_GLOBAL_DATA_PTR;
51
Bin Mengae5bedb2015-10-12 05:23:41 -070052/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +020053 * struct map_sysinfo - Mapping of sysinfo strings to DT
54 *
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020055 * @si_str: sysinfo string
Ilias Apalodimasada83d92023-12-07 11:18:50 +020056 * @dt_str: DT string
57 * @max: Max index of the tokenized string to pick. Counting starts from 0
58 *
59 */
60struct map_sysinfo {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020061 const char *si_node;
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020062 const char *si_str;
Ilias Apalodimasada83d92023-12-07 11:18:50 +020063 const char *dt_str;
64 int max;
65};
66
67static const struct map_sysinfo sysinfo_to_dt[] = {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020068 { .si_node = "system", .si_str = "product", .dt_str = "model", 2 },
69 { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 },
70 { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 },
71 { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 },
Ilias Apalodimasada83d92023-12-07 11:18:50 +020072};
73
74/**
Simon Glass8e5ddb02021-02-04 21:17:17 -070075 * struct smbios_ctx - context for writing SMBIOS tables
76 *
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020077 * @node: node containing the information to write (ofnode_null()
78 * if none)
79 * @dev: sysinfo device to use (NULL if none)
80 * @subnode_name: sysinfo subnode_name. Used for DT fallback
81 * @eos: end-of-string pointer for the table being processed.
82 * This is set up when we start processing a table
83 * @next_ptr: pointer to the start of the next string to be added.
84 * When the table is not empty, this points to the byte
85 * after the \0 of the previous string.
86 * @last_str: points to the last string that was written to the table,
87 * or NULL if none
Simon Glass8e5ddb02021-02-04 21:17:17 -070088 */
89struct smbios_ctx {
90 ofnode node;
91 struct udevice *dev;
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020092 const char *subnode_name;
Simon Glassdff4e862021-02-04 21:17:18 -070093 char *eos;
Simon Glass721b4662021-02-04 21:17:19 -070094 char *next_ptr;
Simon Glassa05eb042021-02-04 21:17:20 -070095 char *last_str;
Simon Glass8e5ddb02021-02-04 21:17:17 -070096};
97
98/**
Simon Glass099d3ae2021-02-04 21:17:14 -070099 * Function prototype to write a specific type of SMBIOS structure
100 *
101 * @addr: start address to write the structure
102 * @handle: the structure's handle, a unique 16-bit number
Simon Glass8e5ddb02021-02-04 21:17:17 -0700103 * @ctx: context for writing the tables
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200104 * Return: size of the structure
Simon Glass099d3ae2021-02-04 21:17:14 -0700105 */
Simon Glass8e5ddb02021-02-04 21:17:17 -0700106typedef int (*smbios_write_type)(ulong *addr, int handle,
107 struct smbios_ctx *ctx);
Simon Glass099d3ae2021-02-04 21:17:14 -0700108
109/**
Simon Glass6f3332a2020-11-05 06:32:08 -0700110 * struct smbios_write_method - Information about a table-writing function
111 *
112 * @write: Function to call
113 * @subnode_name: Name of subnode which has the information for this function,
114 * NULL if none
115 */
116struct smbios_write_method {
117 smbios_write_type write;
118 const char *subnode_name;
119};
120
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200121static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200122{
123 int i;
124
125 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200126 if (node && !strcmp(node, sysinfo_to_dt[i].si_node) &&
127 !strcmp(si, sysinfo_to_dt[i].si_str))
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200128 return &sysinfo_to_dt[i];
129 }
130
131 return NULL;
132}
133
Simon Glass6f3332a2020-11-05 06:32:08 -0700134/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700135 * smbios_add_string() - add a string to the string area
136 *
137 * This adds a string to the string area which is appended directly after
138 * the formatted portion of an SMBIOS structure.
139 *
Simon Glassdff4e862021-02-04 21:17:18 -0700140 * @ctx: SMBIOS context
Bin Mengae5bedb2015-10-12 05:23:41 -0700141 * @str: string to add
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100142 * Return: string number in the string area. 0 if str is NULL.
Bin Mengae5bedb2015-10-12 05:23:41 -0700143 */
Simon Glassdff4e862021-02-04 21:17:18 -0700144static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Mengae5bedb2015-10-12 05:23:41 -0700145{
146 int i = 1;
Simon Glassdff4e862021-02-04 21:17:18 -0700147 char *p = ctx->eos;
148
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100149 if (!str)
150 return 0;
151
Bin Mengae5bedb2015-10-12 05:23:41 -0700152 for (;;) {
153 if (!*p) {
Simon Glassa05eb042021-02-04 21:17:20 -0700154 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700155 strcpy(p, str);
156 p += strlen(str);
157 *p++ = '\0';
Simon Glass721b4662021-02-04 21:17:19 -0700158 ctx->next_ptr = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700159 *p++ = '\0';
160
161 return i;
162 }
163
Simon Glassa05eb042021-02-04 21:17:20 -0700164 if (!strcmp(p, str)) {
165 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700166 return i;
Simon Glassa05eb042021-02-04 21:17:20 -0700167 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700168
169 p += strlen(p) + 1;
170 i++;
171 }
172}
173
174/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200175 * get_str_from_dt - Get a substring from a DT property.
176 * After finding the property in the DT, the function
177 * will parse comma-separated values and return the value.
178 * If nprop->max exceeds the number of comma-separated
179 * elements, the last non NULL value will be returned.
180 * Counting starts from zero.
181 *
182 * @nprop: sysinfo property to use
183 * @str: pointer to fill with data
184 * @size: str buffer length
185 */
186static
187void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
188{
189 const char *dt_str;
190 int cnt = 0;
191 char *token;
192
193 memset(str, 0, size);
194 if (!nprop || !nprop->max)
195 return;
196
197 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
198 if (!dt_str)
199 return;
200
201 memcpy(str, dt_str, size);
202 token = strtok(str, ",");
203 while (token && cnt < nprop->max) {
204 strlcpy(str, token, strlen(token) + 1);
205 token = strtok(NULL, ",");
206 cnt++;
207 }
208}
209
210/**
Raymond Maoa28ef802024-12-06 14:54:22 -0800211 * smbios_get_val_si() - Get value from the devicetree or sysinfo
212 *
213 * @ctx: context of SMBIOS
214 * @prop: property to read
215 * @sysinfo_id: unique identifier for the value to be read
216 * @val_def: Default value
217 * Return: Valid value from sysinfo or device tree, otherwise val_def.
218 */
219static int smbios_get_val_si(struct smbios_ctx * __maybe_unused ctx,
220 const char * __maybe_unused prop,
221 int __maybe_unused sysinfo_id, int val_def)
222{
223#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
224 int val;
225
226 if (!ctx->dev)
227 return val_def;
228
229 if (!sysinfo_get_int(ctx->dev, sysinfo_id, &val))
230 return val;
231
232 if (!IS_ENABLED(CONFIG_OF_CONTROL) || !prop)
233 return val_def;
234
235 if (ofnode_valid(ctx->node) && !ofnode_read_u32(ctx->node, prop, &val))
236 return val;
237
238 /*
239 * If the node or property is not valid fallback and try the root
240 */
241 if (!ofnode_read_u32(ofnode_root(), prop, &val))
242 return val;
243#endif
244 return val_def;
245}
246
247/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700248 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
249 *
250 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700251 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700252 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700253 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100254 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200255 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200256 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700257 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700258static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200259 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700260{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200261 int ret;
262
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200263 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100264 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200265
Simon Glasscf67d6d2021-02-04 21:17:23 -0700266 if (sysinfo_id && ctx->dev) {
267 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700268
269 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
270 if (!ret)
271 return smbios_add_string(ctx, val);
272 }
Raymond Maoa28ef802024-12-06 14:54:22 -0800273 if (!prop)
274 return smbios_add_string(ctx, dval);
275
Simon Glass95f47082020-11-05 06:32:18 -0700276 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200277 const char *str = NULL;
278 char str_dt[128] = { 0 };
279 /*
280 * If the node is not valid fallback and try the entire DT
281 * so we can at least fill in manufacturer and board type
282 */
283 if (ofnode_valid(ctx->node)) {
284 str = ofnode_read_string(ctx->node, prop);
285 } else {
286 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700287
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200288 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200289 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
290 str = (const char *)str_dt;
291 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200292
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200293 ret = smbios_add_string(ctx, str && *str ? str : dval);
294 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700295 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700296
297 return 0;
298}
299
Simon Glasscf67d6d2021-02-04 21:17:23 -0700300/**
301 * smbios_add_prop() - Add a property from the devicetree
302 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200303 * @prop: property to write. The default string will be written if
304 * prop is NULL
305 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200306 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700307 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200308static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
309 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700310{
Simon Glassff3edc22024-10-31 18:50:23 +0100311 return smbios_add_prop_si(ctx, prop, SYSID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700312}
313
Simon Glassdff4e862021-02-04 21:17:18 -0700314static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
315{
316 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700317 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700318 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700319}
320
Simon Glassa05eb042021-02-04 21:17:20 -0700321int smbios_update_version(const char *version)
322{
323 char *ptr = gd->smbios_version;
324 uint old_len, len;
325
326 if (!ptr)
327 return log_ret(-ENOENT);
328
329 /*
330 * This string is supposed to have at least enough bytes and is
331 * padded with spaces. Update it, taking care not to move the
332 * \0 terminator, so that other strings in the string table
333 * are not disturbed. See smbios_add_string()
334 */
335 old_len = strnlen(ptr, SMBIOS_STR_MAX);
336 len = strnlen(version, SMBIOS_STR_MAX);
337 if (len > old_len)
338 return log_ret(-ENOSPC);
339
340 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
341 memcpy(ptr, version, len);
342#ifdef LOG_DEBUG
343 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
344#endif
345
346 return 0;
347}
348
Simon Glass6f3332a2020-11-05 06:32:08 -0700349/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700350 * smbios_string_table_len() - compute the string area size
351 *
352 * This computes the size of the string area including the string terminator.
353 *
Simon Glass721b4662021-02-04 21:17:19 -0700354 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200355 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700356 */
Simon Glass721b4662021-02-04 21:17:19 -0700357static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700358{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200359 /* In case no string is defined we have to return two \0 */
360 if (ctx->next_ptr == ctx->eos)
361 return 2;
362
Simon Glass721b4662021-02-04 21:17:19 -0700363 /* Allow for the final \0 after all strings */
364 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700365}
366
Simon Glass8e5ddb02021-02-04 21:17:17 -0700367static int smbios_write_type0(ulong *current, int handle,
368 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700369{
Simon Glass2d93add2018-11-22 13:46:37 -0700370 struct smbios_type0 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800371 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700372
Simon Glass2d93add2018-11-22 13:46:37 -0700373 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800374 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700375 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700376 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800377 t->vendor = smbios_add_prop_si(ctx, NULL, SYSID_SM_BIOS_VENDOR,
378 "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700379
Raymond Maoa28ef802024-12-06 14:54:22 -0800380 t->bios_ver = smbios_add_prop_si(ctx, "version", SYSID_SM_BIOS_VER,
381 PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700382 if (t->bios_ver)
383 gd->smbios_version = ctx->last_str;
384 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
385 gd->smbios_version);
386#ifdef LOG_DEBUG
387 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
388 1, strlen(gd->smbios_version) + 1, 0);
389#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800390 t->bios_release_date = smbios_add_prop_si(ctx, NULL,
391 SYSID_SM_BIOS_REL_DATE,
392 U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200393#ifdef CONFIG_ROM_SIZE
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +0200394 if (CONFIG_ROM_SIZE < SZ_16M) {
395 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
396 } else {
397 /* CONFIG_ROM_SIZE < 8 GiB */
398 t->bios_rom_size = 0xff;
399 t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20;
400 }
Alexander Graf66f96e12016-08-19 01:23:29 +0200401#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700402 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
403 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
404 BIOS_CHARACTERISTICS_UPGRADEABLE;
405#ifdef CONFIG_GENERATE_ACPI_TABLE
406 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
407#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200408#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300409 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200410#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300411 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200412
Simon Glassd7d16822021-02-04 21:17:16 -0700413 /* bios_major_release has only one byte, so drop century */
414 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
415 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700416 t->ec_major_release = 0xff;
417 t->ec_minor_release = 0xff;
418
Raymond Maof4b933d2024-12-06 14:54:18 -0800419 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700420 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700421 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700422
423 return len;
424}
425
Simon Glass8e5ddb02021-02-04 21:17:17 -0700426static int smbios_write_type1(ulong *current, int handle,
427 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700428{
Simon Glass2d93add2018-11-22 13:46:37 -0700429 struct smbios_type1 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800430 int len = sizeof(*t);
Simon Glass64b723f2017-08-03 12:22:12 -0600431 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700432
Simon Glass2d93add2018-11-22 13:46:37 -0700433 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800434 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700435 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700436 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800437
Michal Simeka7aa3952024-04-26 15:38:13 +0200438 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100439 SYSID_SM_SYSTEM_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200440 NULL);
441 t->product_name = smbios_add_prop_si(ctx, "product",
Raymond Maoa28ef802024-12-06 14:54:22 -0800442 SYSID_SM_SYSTEM_PRODUCT, NULL);
443 t->version = smbios_add_prop_si(ctx, "version", SYSID_SM_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100444 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200445 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200446 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Raymond Maof4b933d2024-12-06 14:54:18 -0800447 strlcpy((char *)t->uuid, serial_str, sizeof(t->uuid));
Simon Glass6f3332a2020-11-05 06:32:08 -0700448 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200449 t->serial_number = smbios_add_prop_si(ctx, "serial",
Simon Glassff3edc22024-10-31 18:50:23 +0100450 SYSID_SM_SYSTEM_SERIAL,
Michal Simeka7aa3952024-04-26 15:38:13 +0200451 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200452 }
Raymond Maoa28ef802024-12-06 14:54:22 -0800453 t->wakeup_type = smbios_get_val_si(ctx, "wakeup-type",
454 SYSID_SM_SYSTEM_WAKEUP,
455 SMBIOS_WAKEUP_TYPE_UNKNOWN);
456 t->sku_number = smbios_add_prop_si(ctx, "sku", SYSID_SM_SYSTEM_SKU,
457 NULL);
458 t->family = smbios_add_prop_si(ctx, "family", SYSID_SM_SYSTEM_FAMILY,
459 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700460
Raymond Maof4b933d2024-12-06 14:54:18 -0800461 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700462 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700463 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700464
465 return len;
466}
467
Simon Glass8e5ddb02021-02-04 21:17:17 -0700468static int smbios_write_type2(ulong *current, int handle,
469 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700470{
Simon Glass2d93add2018-11-22 13:46:37 -0700471 struct smbios_type2 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800472 int len = sizeof(*t);
Raymond Maoa28ef802024-12-06 14:54:22 -0800473 u8 *eos_addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700474
Raymond Maoa28ef802024-12-06 14:54:22 -0800475 /*
476 * reserve the space for the dynamic bytes of contained object handles.
477 * TODO: len += <obj_handle_num> * SMBIOS_TYPE2_CON_OBJ_HANDLE_SIZE
478 * obj_handle_num can be from DT node "baseboard" or sysinfo driver.
479 */
Simon Glass2d93add2018-11-22 13:46:37 -0700480 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800481 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700482 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Raymond Maoa28ef802024-12-06 14:54:22 -0800483
484 /* eos is at the end of the structure */
485 eos_addr = (u8 *)t + len - sizeof(t->eos);
486 smbios_set_eos(ctx, eos_addr);
487
Michal Simeka7aa3952024-04-26 15:38:13 +0200488 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100489 SYSID_SM_BASEBOARD_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200490 NULL);
491 t->product_name = smbios_add_prop_si(ctx, "product",
Raymond Maoa28ef802024-12-06 14:54:22 -0800492 SYSID_SM_BASEBOARD_PRODUCT, NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700493 t->version = smbios_add_prop_si(ctx, "version",
Raymond Maoa28ef802024-12-06 14:54:22 -0800494 SYSID_SM_BASEBOARD_VERSION, NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200495 t->serial_number = smbios_add_prop_si(ctx, "serial",
Raymond Maoa28ef802024-12-06 14:54:22 -0800496 SYSID_SM_BASEBOARD_SERIAL, NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200497 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
Simon Glassff3edc22024-10-31 18:50:23 +0100498 SYSID_SM_BASEBOARD_ASSET_TAG,
Michal Simeka7aa3952024-04-26 15:38:13 +0200499 NULL);
Raymond Maoa28ef802024-12-06 14:54:22 -0800500 t->feature_flags = smbios_get_val_si(ctx, "feature-flags",
501 SYSID_SM_BASEBOARD_FEATURE, 0);
502
503 t->chassis_location =
504 smbios_add_prop_si(ctx, "chassis-location",
505 SYSID_SM_BASEBOARD_CHASSIS_LOCAT, NULL);
506 t->board_type = smbios_get_val_si(ctx, "board-type",
507 SYSID_SM_BASEBOARD_TYPE,
508 SMBIOS_BOARD_TYPE_UNKNOWN);
509
510 /*
511 * TODO:
512 * Populate the Contained Object Handles if they exist
513 * t->number_contained_objects = <obj_handle_num>;
514 */
515
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100516 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700517
Raymond Maof4b933d2024-12-06 14:54:18 -0800518 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700519 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700520 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700521
522 return len;
523}
524
Simon Glass8e5ddb02021-02-04 21:17:17 -0700525static int smbios_write_type3(ulong *current, int handle,
526 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700527{
Simon Glass2d93add2018-11-22 13:46:37 -0700528 struct smbios_type3 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800529 int len = sizeof(*t);
Raymond Maoa28ef802024-12-06 14:54:22 -0800530 u8 *eos_addr;
531 size_t elem_size = 0;
Raymond Mao94aa78d2024-12-06 14:54:23 -0800532 __maybe_unused u8 *elem_addr;
533 __maybe_unused u8 *sku_num_addr;
Raymond Maoa28ef802024-12-06 14:54:22 -0800534
535 /*
536 * reserve the space for the dynamic bytes of contained elements.
537 * TODO: elem_size = <element_count> * <element_record_length>
538 * element_count and element_record_length can be from DT node
539 * "chassis" or sysinfo driver.
540 */
541 len += elem_size;
Bin Mengae5bedb2015-10-12 05:23:41 -0700542
Simon Glass2d93add2018-11-22 13:46:37 -0700543 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800544 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700545 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800546#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
547 elem_addr = (u8 *)t + offsetof(struct smbios_type3, sku_number);
548 sku_num_addr = elem_addr + elem_size;
549#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800550 /* eos is at the end of the structure */
551 eos_addr = (u8 *)t + len - sizeof(t->eos);
552 smbios_set_eos(ctx, eos_addr);
553
554 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
555 SYSID_SM_ENCLOSURE_MANUFACTURER,
556 NULL);
557 t->chassis_type = smbios_get_val_si(ctx, "chassis-type",
558 SYSID_SM_ENCLOSURE_TYPE,
559 SMBIOS_ENCLOSURE_UNKNOWN);
560 t->bootup_state = smbios_get_val_si(ctx, "bootup-state",
561 SYSID_SM_ENCLOSURE_BOOTUP,
562 SMBIOS_STATE_UNKNOWN);
563 t->power_supply_state = smbios_get_val_si(ctx, "power-supply-state",
564 SYSID_SM_ENCLOSURE_POW,
565 SMBIOS_STATE_UNKNOWN);
566 t->thermal_state = smbios_get_val_si(ctx, "thermal-state",
567 SYSID_SM_ENCLOSURE_THERMAL,
568 SMBIOS_STATE_UNKNOWN);
569 t->security_status = smbios_get_val_si(ctx, "security-status",
570 SYSID_SM_ENCLOSURE_SECURITY,
571 SMBIOS_SECURITY_UNKNOWN);
Bin Mengae5bedb2015-10-12 05:23:41 -0700572
Raymond Mao94aa78d2024-12-06 14:54:23 -0800573#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
574 t->version = smbios_add_prop_si(ctx, "version",
575 SYSID_SM_ENCLOSURE_VERSION, NULL);
576 t->serial_number = smbios_add_prop_si(ctx, "serial",
577 SYSID_SM_ENCLOSURE_SERIAL, NULL);
578 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
579 SYSID_SM_BASEBOARD_ASSET_TAG,
580 NULL);
581 t->oem_defined = smbios_get_val_si(ctx, "oem-defined",
582 SYSID_SM_ENCLOSURE_OEM, 0);
583 t->height = smbios_get_val_si(ctx, "height",
584 SYSID_SM_ENCLOSURE_HEIGHT, 0);
585 t->number_of_power_cords =
586 smbios_get_val_si(ctx, "number-of-power-cords",
587 SYSID_SM_ENCLOSURE_POWCORE_NUM, 0);
588
589 /*
590 * TODO: Populate the Contained Element Record if they exist
591 * t->element_count = <element_num>;
592 * t->element_record_length = <element_len>;
593 */
Bin Mengae5bedb2015-10-12 05:23:41 -0700594
Raymond Mao94aa78d2024-12-06 14:54:23 -0800595 *sku_num_addr = smbios_add_prop_si(ctx, "sku", SYSID_SM_ENCLOSURE_SKU,
596 NULL);
597#endif
598
Raymond Maof4b933d2024-12-06 14:54:18 -0800599 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700600 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700601 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700602
603 return len;
604}
605
Simon Glass8e5ddb02021-02-04 21:17:17 -0700606static void smbios_write_type4_dm(struct smbios_type4 *t,
607 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200608{
609 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100610 const char *vendor = NULL;
611 const char *name = NULL;
Raymond Mao94aa78d2024-12-06 14:54:23 -0800612 __maybe_unused void *id_data = NULL;
613 __maybe_unused size_t id_size = 0;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200614
615#ifdef CONFIG_CPU
616 char processor_name[49];
617 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700618 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200619
Simon Glass0dd7ca22020-11-05 06:32:07 -0700620 uclass_find_first_device(UCLASS_CPU, &cpu);
621 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700622 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200623
624 if (plat->family)
625 processor_family = plat->family;
626 t->processor_id[0] = plat->id[0];
627 t->processor_id[1] = plat->id[1];
628
Simon Glass0dd7ca22020-11-05 06:32:07 -0700629 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200630 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700631 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200632 name = processor_name;
633 }
634#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800635 if (processor_family == SMBIOS_PROCESSOR_FAMILY_UNKNOWN)
636 processor_family =
637 smbios_get_val_si(ctx, "family",
638 SYSID_SM_PROCESSOR_FAMILY,
639 SMBIOS_PROCESSOR_FAMILY_UNKNOWN);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200640
Raymond Maoa28ef802024-12-06 14:54:22 -0800641 if (processor_family == SMBIOS_PROCESSOR_FAMILY_EXT)
642 t->processor_family2 =
643 smbios_get_val_si(ctx, "family2",
644 SYSID_SM_PROCESSOR_FAMILY2,
645 SMBIOS_PROCESSOR_FAMILY_UNKNOWN);
646
647 t->processor_family = processor_family;
648 t->processor_manufacturer =
649 smbios_add_prop_si(ctx, "manufacturer",
650 SYSID_SM_PROCESSOR_MANUFACT, vendor);
651 t->processor_version = smbios_add_prop_si(ctx, "version",
652 SYSID_SM_PROCESSOR_VERSION,
653 name);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800654
655#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
656 if (t->processor_id[0] || t->processor_id[1] ||
657 sysinfo_get_data(ctx->dev, SYSID_SM_PROCESSOR_ID, &id_data,
658 &id_size))
659 return;
660
661 if (id_data && id_size == sizeof(t->processor_id))
662 memcpy((u8 *)t->processor_id, id_data, id_size);
663#endif
Alexander Grafe44b3c02016-08-19 01:23:28 +0200664}
665
Simon Glass8e5ddb02021-02-04 21:17:17 -0700666static int smbios_write_type4(ulong *current, int handle,
667 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700668{
Simon Glass2d93add2018-11-22 13:46:37 -0700669 struct smbios_type4 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800670 int len = sizeof(*t);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800671 __maybe_unused void *hdl;
672 __maybe_unused size_t hdl_size;
Bin Mengae5bedb2015-10-12 05:23:41 -0700673
Simon Glass2d93add2018-11-22 13:46:37 -0700674 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800675 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700676 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700677 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800678 t->socket_design = smbios_add_prop_si(ctx, "socket-design",
679 SYSID_SM_PROCESSOR_SOCKET, NULL);
680 t->processor_type = smbios_get_val_si(ctx, "processor-type",
681 SYSID_SM_PROCESSOR_TYPE,
682 SMBIOS_PROCESSOR_TYPE_UNKNOWN);
Simon Glass8e5ddb02021-02-04 21:17:17 -0700683 smbios_write_type4_dm(t, ctx);
Raymond Maoa28ef802024-12-06 14:54:22 -0800684
685 t->status = smbios_get_val_si(ctx, "processor-status",
686 SYSID_SM_PROCESSOR_STATUS,
687 SMBIOS_PROCESSOR_STATUS_UNKNOWN);
688 t->processor_upgrade =
689 smbios_get_val_si(ctx, "upgrade", SYSID_SM_PROCESSOR_UPGRADE,
690 SMBIOS_PROCESSOR_UPGRADE_UNKNOWN);
691
692 t->l1_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
693 t->l2_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
694 t->l3_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
Bin Mengae5bedb2015-10-12 05:23:41 -0700695
Raymond Mao94aa78d2024-12-06 14:54:23 -0800696#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
697 t->voltage = smbios_get_val_si(ctx, "voltage",
698 SYSID_SM_PROCESSOR_VOLTAGE, 0);
699 t->external_clock = smbios_get_val_si(ctx, "external-clock",
700 SYSID_SM_PROCESSOR_EXT_CLOCK, 0);
701 t->max_speed = smbios_get_val_si(ctx, "max-speed",
702 SYSID_SM_PROCESSOR_MAX_SPEED, 0);
703 t->current_speed = smbios_get_val_si(ctx, "current-speed",
704 SYSID_SM_PROCESSOR_CUR_SPEED, 0);
705
706 /* Read the cache handles */
707 if (!sysinfo_get_data(ctx->dev, SYSID_SM_CACHE_HANDLE, &hdl,
708 &hdl_size) &&
709 (hdl_size == SYSINFO_CACHE_LVL_MAX * sizeof(u16))) {
710 u16 *handle = (u16 *)hdl;
711
712 if (*handle)
713 t->l1_cache_handle = *handle;
714
715 handle++;
716 if (*handle)
717 t->l2_cache_handle = *handle;
718
719 handle++;
720 if (*handle)
721 t->l3_cache_handle = *handle;
722 }
723
724 t->serial_number = smbios_add_prop_si(ctx, "serial",
725 SYSID_SM_PROCESSOR_SN, NULL);
726 t->asset_tag = smbios_add_prop_si(ctx, "asset-tag",
727 SYSID_SM_PROCESSOR_ASSET_TAG, NULL);
728 t->part_number = smbios_add_prop_si(ctx, "part-number",
729 SYSID_SM_PROCESSOR_PN, NULL);
730 t->core_count = smbios_get_val_si(ctx, "core-count",
731 SYSID_SM_PROCESSOR_CORE_CNT, 0);
732 t->core_enabled = smbios_get_val_si(ctx, "core-enabled",
733 SYSID_SM_PROCESSOR_CORE_EN, 0);
734 t->thread_count = smbios_get_val_si(ctx, "thread-count",
735 SYSID_SM_PROCESSOR_THREAD_CNT, 0);
736 t->processor_characteristics =
737 smbios_get_val_si(ctx, "characteristics",
738 SYSID_SM_PROCESSOR_CHARA,
739 SMBIOS_PROCESSOR_UND);
740 t->core_count2 = smbios_get_val_si(ctx, "core-count2",
741 SYSID_SM_PROCESSOR_CORE_CNT2, 0);
742 t->core_enabled2 = smbios_get_val_si(ctx, "core-enabled2",
743 SYSID_SM_PROCESSOR_CORE_EN2, 0);
744 t->thread_count2 = smbios_get_val_si(ctx, "thread-count2",
745 SYSID_SM_PROCESSOR_THREAD_CNT2, 0);
746 t->thread_enabled = smbios_get_val_si(ctx, "thread-enabled",
747 SYSID_SM_PROCESSOR_THREAD_EN, 0);
748#endif
749
750 len = t->hdr.length + smbios_string_table_len(ctx);
751 *current += len;
752 unmap_sysmem(t);
753
754 return len;
755}
Bin Mengae5bedb2015-10-12 05:23:41 -0700756
Raymond Mao94aa78d2024-12-06 14:54:23 -0800757#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
758
759static int smbios_write_type7_1level(ulong *current, int handle,
760 struct smbios_ctx *ctx, int level)
761{
762 struct smbios_type7 *t;
763 int len = sizeof(*t);
764 void *hdl;
765 size_t hdl_size;
766
767 t = map_sysmem(*current, len);
768 memset(t, 0, len);
769 fill_smbios_header(t, SMBIOS_CACHE_INFORMATION, len, handle);
770 smbios_set_eos(ctx, t->eos);
771
772 t->socket_design = smbios_add_prop_si(ctx, "socket-design",
773 SYSID_SM_CACHE_SOCKET + level,
774 NULL);
775 t->config.data = smbios_get_val_si(ctx, "config",
776 SYSID_SM_CACHE_CONFIG + level,
777 (level - 1) | SMBIOS_CACHE_OP_UND);
778 t->max_size.data = smbios_get_val_si(ctx, "max-size",
779 SYSID_SM_CACHE_MAX_SIZE + level,
780 0);
781 t->inst_size.data = smbios_get_val_si(ctx, "installed-size",
782 SYSID_SM_CACHE_INST_SIZE + level,
783 0);
784 t->supp_sram_type.data =
785 smbios_get_val_si(ctx, "supported-sram-type",
786 SYSID_SM_CACHE_SUPSRAM_TYPE + level,
787 SMBIOS_CACHE_SRAM_TYPE_UNKNOWN);
788 t->curr_sram_type.data =
789 smbios_get_val_si(ctx, "current-sram-type",
790 SYSID_SM_CACHE_CURSRAM_TYPE + level,
791 SMBIOS_CACHE_SRAM_TYPE_UNKNOWN);
792 t->speed = smbios_get_val_si(ctx, "speed", SYSID_SM_CACHE_SPEED + level,
793 0);
794 t->err_corr_type = smbios_get_val_si(ctx, "error-correction-type",
795 SYSID_SM_CACHE_ERRCOR_TYPE + level,
796 SMBIOS_CACHE_ERRCORR_UNKNOWN);
797 t->sys_cache_type =
798 smbios_get_val_si(ctx, "system-cache-type",
799 SYSID_SM_CACHE_SCACHE_TYPE + level,
800 SMBIOS_CACHE_SYSCACHE_TYPE_UNKNOWN);
801 t->associativity = smbios_get_val_si(ctx, "associativity",
802 SYSID_SM_CACHE_ASSOC + level,
803 SMBIOS_CACHE_ASSOC_UNKNOWN);
804 t->max_size2.data = smbios_get_val_si(ctx, "max-size2",
805 SYSID_SM_CACHE_MAX_SIZE2 + level,
806 0);
807 t->inst_size2.data =
808 smbios_get_val_si(ctx, "installed-size2",
809 SYSID_SM_CACHE_INST_SIZE2 + level, 0);
810
811 /* Save the cache handles */
812 if (!sysinfo_get_data(ctx->dev, SYSID_SM_CACHE_HANDLE, &hdl,
813 &hdl_size)) {
814 if (hdl_size == SYSINFO_CACHE_LVL_MAX * sizeof(u16))
815 *((u16 *)hdl + level) = handle;
816 }
817
Raymond Maof4b933d2024-12-06 14:54:18 -0800818 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700819 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700820 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700821
822 return len;
823}
824
Raymond Mao94aa78d2024-12-06 14:54:23 -0800825static int smbios_write_type7(ulong *current, int handle,
826 struct smbios_ctx *ctx)
827{
828 int len = 0;
829 int i, level;
830 ofnode parent = ctx->node;
831 struct smbios_ctx ctx_bak;
832
833 memcpy(&ctx_bak, ctx, sizeof(ctx_bak));
834
835 /* Get the number of level */
836 level = smbios_get_val_si(ctx, NULL, SYSID_SM_CACHE_LEVEL, 0);
837 if (level >= SYSINFO_CACHE_LVL_MAX) /* Error, return 0-length */
838 return 0;
839
840 for (i = 0; i <= level; i++) {
841 char buf[9] = "";
842
843 if (!snprintf(buf, sizeof(buf), "l%d-cache", i + 1))
844 return 0;
845 ctx->subnode_name = buf;
846 ctx->node = ofnode_find_subnode(parent, ctx->subnode_name);
847 len += smbios_write_type7_1level(current, handle++, ctx, i);
848 memcpy(ctx, &ctx_bak, sizeof(*ctx));
849 }
850 return len;
851}
852
853#endif /* #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE) */
854
Simon Glass8e5ddb02021-02-04 21:17:17 -0700855static int smbios_write_type32(ulong *current, int handle,
856 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700857{
Simon Glass2d93add2018-11-22 13:46:37 -0700858 struct smbios_type32 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800859 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700860
Simon Glass2d93add2018-11-22 13:46:37 -0700861 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800862 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700863 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700864 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700865
866 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700867 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700868
869 return len;
870}
871
Simon Glass8e5ddb02021-02-04 21:17:17 -0700872static int smbios_write_type127(ulong *current, int handle,
873 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700874{
Simon Glass2d93add2018-11-22 13:46:37 -0700875 struct smbios_type127 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800876 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700877
Simon Glass2d93add2018-11-22 13:46:37 -0700878 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800879 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700880 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
881
882 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700883 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700884
885 return len;
886}
887
Simon Glass6f3332a2020-11-05 06:32:08 -0700888static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700889 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700890 { smbios_write_type1, "system", },
891 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100892 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700893 { smbios_write_type3, "chassis", },
Raymond Mao94aa78d2024-12-06 14:54:23 -0800894#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
895 /* Type 7 must ahead of type 4 to get cache handles. */
896 { smbios_write_type7, "cache", },
897#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800898 { smbios_write_type4, "processor"},
Simon Glass6f3332a2020-11-05 06:32:08 -0700899 { smbios_write_type32, },
900 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700901};
902
Simon Glassca37a392017-01-16 07:03:35 -0700903ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700904{
Simon Glass6f3332a2020-11-05 06:32:08 -0700905 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700906 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700907 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700908 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700909 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700910 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700911 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700912 int i;
913
Simon Glass8e5ddb02021-02-04 21:17:17 -0700914 ctx.node = ofnode_null();
Raymond Maoa28ef802024-12-06 14:54:22 -0800915 if (CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700916 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200917 if (ctx.dev) {
918 int ret;
919
Simon Glass8e5ddb02021-02-04 21:17:17 -0700920 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200921 ret = sysinfo_detect(ctx.dev);
Simon Glassbd5515d2024-06-23 14:30:33 -0600922
923 /*
924 * ignore the error since many boards don't implement
925 * this and we can still use the info in the devicetree
926 */
927 ret = log_msg_ret("sys", ret);
Michal Simek7d955002024-04-26 15:38:12 +0200928 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700929 } else {
930 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700931 }
932
Simon Glass3905e922023-12-31 08:25:45 -0700933 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700934
Simon Glassfc4b5522023-12-31 08:25:51 -0700935 /* move past the (so-far-unwritten) header to start writing structs */
936 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700937 tables = addr;
938
939 /* populate minimum required tables */
940 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700941 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700942
943 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200944 ctx.subnode_name = NULL;
945 if (method->subnode_name) {
946 ctx.subnode_name = method->subnode_name;
947 if (IS_ENABLED(CONFIG_OF_CONTROL))
948 ctx.node = ofnode_find_subnode(parent_node,
949 method->subnode_name);
950 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100951 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700952 }
953
Simon Glass2d93add2018-11-22 13:46:37 -0700954 /*
955 * We must use a pointer here so things work correctly on sandbox. The
956 * user of this table is not aware of the mapping of addresses to
957 * sandbox's DRAM buffer.
958 */
959 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700960
Simon Glassfc4b5522023-12-31 08:25:51 -0700961 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100962 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
963 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700964 memcpy(se->anchor, "_SM3_", 5);
965 se->length = sizeof(struct smbios3_entry);
966 se->major_ver = SMBIOS_MAJOR_VER;
967 se->minor_ver = SMBIOS_MINOR_VER;
968 se->doc_rev = 0;
969 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100970 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700971 se->struct_table_address = table_addr;
972 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
973 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700974
975 return addr;
976}