blob: 203ed6d78b0ef90aea6016c8c92b4e29cef00eb3 [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
Simon Glass0dd7ca22020-11-05 06:32:07 -070010#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Pali Rohárebe9fd82021-04-22 18:09:57 +020012#include <linux/stringify.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020013#include <linux/string.h>
Simon Glass2d93add2018-11-22 13:46:37 -070014#include <mapmem.h>
Alexander Graffb228082016-08-19 01:23:23 +020015#include <smbios.h>
Simon Glasscf67d6d2021-02-04 21:17:23 -070016#include <sysinfo.h>
Alexander Graffb228082016-08-19 01:23:23 +020017#include <tables_csum.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070018#include <version.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020019#include <malloc.h>
20#include <dm/ofnode.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020021#ifdef CONFIG_CPU
22#include <cpu.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020023#include <dm/uclass-internal.h>
24#endif
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +020025#include <linux/sizes.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070026
Pali Rohárebe9fd82021-04-22 18:09:57 +020027/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
28#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
29 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
30#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
31#endif
32
33/*
34 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
35 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
36 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
37 * BIOS Release Date should contain date when code was released
38 * and not when it was built or compiled.
39 */
40#if U_BOOT_VERSION_NUM_PATCH < 10
41#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
42#else
43#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
44#endif
45#define U_BOOT_DMI_DAY "01"
46#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
47#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
48
Simon Glassa05eb042021-02-04 21:17:20 -070049DECLARE_GLOBAL_DATA_PTR;
50
Bin Mengae5bedb2015-10-12 05:23:41 -070051/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +020052 * struct map_sysinfo - Mapping of sysinfo strings to DT
53 *
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020054 * @si_str: sysinfo string
Ilias Apalodimasada83d92023-12-07 11:18:50 +020055 * @dt_str: DT string
56 * @max: Max index of the tokenized string to pick. Counting starts from 0
57 *
58 */
59struct map_sysinfo {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020060 const char *si_node;
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020061 const char *si_str;
Ilias Apalodimasada83d92023-12-07 11:18:50 +020062 const char *dt_str;
63 int max;
64};
65
66static const struct map_sysinfo sysinfo_to_dt[] = {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020067 { .si_node = "system", .si_str = "product", .dt_str = "model", 2 },
68 { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 },
69 { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 },
70 { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 },
Ilias Apalodimasada83d92023-12-07 11:18:50 +020071};
72
73/**
Simon Glass8e5ddb02021-02-04 21:17:17 -070074 * struct smbios_ctx - context for writing SMBIOS tables
75 *
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020076 * @node: node containing the information to write (ofnode_null()
77 * if none)
78 * @dev: sysinfo device to use (NULL if none)
79 * @subnode_name: sysinfo subnode_name. Used for DT fallback
80 * @eos: end-of-string pointer for the table being processed.
81 * This is set up when we start processing a table
82 * @next_ptr: pointer to the start of the next string to be added.
83 * When the table is not empty, this points to the byte
84 * after the \0 of the previous string.
85 * @last_str: points to the last string that was written to the table,
86 * or NULL if none
Simon Glass8e5ddb02021-02-04 21:17:17 -070087 */
88struct smbios_ctx {
89 ofnode node;
90 struct udevice *dev;
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020091 const char *subnode_name;
Simon Glassdff4e862021-02-04 21:17:18 -070092 char *eos;
Simon Glass721b4662021-02-04 21:17:19 -070093 char *next_ptr;
Simon Glassa05eb042021-02-04 21:17:20 -070094 char *last_str;
Simon Glass8e5ddb02021-02-04 21:17:17 -070095};
96
97/**
Simon Glass099d3ae2021-02-04 21:17:14 -070098 * Function prototype to write a specific type of SMBIOS structure
99 *
100 * @addr: start address to write the structure
101 * @handle: the structure's handle, a unique 16-bit number
Simon Glass8e5ddb02021-02-04 21:17:17 -0700102 * @ctx: context for writing the tables
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200103 * Return: size of the structure
Simon Glass099d3ae2021-02-04 21:17:14 -0700104 */
Simon Glass8e5ddb02021-02-04 21:17:17 -0700105typedef int (*smbios_write_type)(ulong *addr, int handle,
106 struct smbios_ctx *ctx);
Simon Glass099d3ae2021-02-04 21:17:14 -0700107
108/**
Simon Glass6f3332a2020-11-05 06:32:08 -0700109 * struct smbios_write_method - Information about a table-writing function
110 *
111 * @write: Function to call
112 * @subnode_name: Name of subnode which has the information for this function,
113 * NULL if none
114 */
115struct smbios_write_method {
116 smbios_write_type write;
117 const char *subnode_name;
118};
119
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200120static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200121{
122 int i;
123
124 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200125 if (node && !strcmp(node, sysinfo_to_dt[i].si_node) &&
126 !strcmp(si, sysinfo_to_dt[i].si_str))
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200127 return &sysinfo_to_dt[i];
128 }
129
130 return NULL;
131}
132
Simon Glass6f3332a2020-11-05 06:32:08 -0700133/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700134 * smbios_add_string() - add a string to the string area
135 *
136 * This adds a string to the string area which is appended directly after
137 * the formatted portion of an SMBIOS structure.
138 *
Simon Glassdff4e862021-02-04 21:17:18 -0700139 * @ctx: SMBIOS context
Bin Mengae5bedb2015-10-12 05:23:41 -0700140 * @str: string to add
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100141 * Return: string number in the string area. 0 if str is NULL.
Bin Mengae5bedb2015-10-12 05:23:41 -0700142 */
Simon Glassdff4e862021-02-04 21:17:18 -0700143static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Mengae5bedb2015-10-12 05:23:41 -0700144{
145 int i = 1;
Simon Glassdff4e862021-02-04 21:17:18 -0700146 char *p = ctx->eos;
147
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100148 if (!str)
149 return 0;
150
Bin Mengae5bedb2015-10-12 05:23:41 -0700151 for (;;) {
152 if (!*p) {
Simon Glassa05eb042021-02-04 21:17:20 -0700153 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700154 strcpy(p, str);
155 p += strlen(str);
156 *p++ = '\0';
Simon Glass721b4662021-02-04 21:17:19 -0700157 ctx->next_ptr = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700158 *p++ = '\0';
159
160 return i;
161 }
162
Simon Glassa05eb042021-02-04 21:17:20 -0700163 if (!strcmp(p, str)) {
164 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700165 return i;
Simon Glassa05eb042021-02-04 21:17:20 -0700166 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700167
168 p += strlen(p) + 1;
169 i++;
170 }
171}
172
173/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200174 * get_str_from_dt - Get a substring from a DT property.
175 * After finding the property in the DT, the function
176 * will parse comma-separated values and return the value.
177 * If nprop->max exceeds the number of comma-separated
178 * elements, the last non NULL value will be returned.
179 * Counting starts from zero.
180 *
181 * @nprop: sysinfo property to use
182 * @str: pointer to fill with data
183 * @size: str buffer length
184 */
185static
186void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
187{
188 const char *dt_str;
189 int cnt = 0;
190 char *token;
191
192 memset(str, 0, size);
193 if (!nprop || !nprop->max)
194 return;
195
196 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
197 if (!dt_str)
198 return;
199
200 memcpy(str, dt_str, size);
201 token = strtok(str, ",");
202 while (token && cnt < nprop->max) {
203 strlcpy(str, token, strlen(token) + 1);
204 token = strtok(NULL, ",");
205 cnt++;
206 }
207}
208
209/**
Raymond Maoa28ef802024-12-06 14:54:22 -0800210 * smbios_get_val_si() - Get value from the devicetree or sysinfo
211 *
212 * @ctx: context of SMBIOS
213 * @prop: property to read
214 * @sysinfo_id: unique identifier for the value to be read
215 * @val_def: Default value
216 * Return: Valid value from sysinfo or device tree, otherwise val_def.
217 */
218static int smbios_get_val_si(struct smbios_ctx * __maybe_unused ctx,
219 const char * __maybe_unused prop,
220 int __maybe_unused sysinfo_id, int val_def)
221{
222#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
223 int val;
224
225 if (!ctx->dev)
226 return val_def;
227
228 if (!sysinfo_get_int(ctx->dev, sysinfo_id, &val))
229 return val;
230
231 if (!IS_ENABLED(CONFIG_OF_CONTROL) || !prop)
232 return val_def;
233
234 if (ofnode_valid(ctx->node) && !ofnode_read_u32(ctx->node, prop, &val))
235 return val;
236
237 /*
238 * If the node or property is not valid fallback and try the root
239 */
240 if (!ofnode_read_u32(ofnode_root(), prop, &val))
241 return val;
242#endif
243 return val_def;
244}
245
246/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700247 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
248 *
249 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700250 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700251 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700252 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100253 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200254 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200255 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700256 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700257static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200258 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700259{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200260 int ret;
261
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200262 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100263 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200264
Simon Glasscf67d6d2021-02-04 21:17:23 -0700265 if (sysinfo_id && ctx->dev) {
266 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700267
268 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
269 if (!ret)
270 return smbios_add_string(ctx, val);
271 }
Raymond Maoa28ef802024-12-06 14:54:22 -0800272 if (!prop)
273 return smbios_add_string(ctx, dval);
274
Simon Glass95f47082020-11-05 06:32:18 -0700275 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200276 const char *str = NULL;
277 char str_dt[128] = { 0 };
278 /*
279 * If the node is not valid fallback and try the entire DT
280 * so we can at least fill in manufacturer and board type
281 */
282 if (ofnode_valid(ctx->node)) {
283 str = ofnode_read_string(ctx->node, prop);
284 } else {
285 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700286
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200287 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200288 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
289 str = (const char *)str_dt;
290 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200291
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200292 ret = smbios_add_string(ctx, str && *str ? str : dval);
293 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700294 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700295
296 return 0;
297}
298
Simon Glasscf67d6d2021-02-04 21:17:23 -0700299/**
300 * smbios_add_prop() - Add a property from the devicetree
301 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200302 * @prop: property to write. The default string will be written if
303 * prop is NULL
304 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200305 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700306 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200307static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
308 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700309{
Simon Glassff3edc22024-10-31 18:50:23 +0100310 return smbios_add_prop_si(ctx, prop, SYSID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700311}
312
Simon Glassdff4e862021-02-04 21:17:18 -0700313static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
314{
315 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700316 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700317 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700318}
319
Simon Glassa05eb042021-02-04 21:17:20 -0700320int smbios_update_version(const char *version)
321{
322 char *ptr = gd->smbios_version;
323 uint old_len, len;
324
325 if (!ptr)
326 return log_ret(-ENOENT);
327
328 /*
329 * This string is supposed to have at least enough bytes and is
330 * padded with spaces. Update it, taking care not to move the
331 * \0 terminator, so that other strings in the string table
332 * are not disturbed. See smbios_add_string()
333 */
334 old_len = strnlen(ptr, SMBIOS_STR_MAX);
335 len = strnlen(version, SMBIOS_STR_MAX);
336 if (len > old_len)
337 return log_ret(-ENOSPC);
338
339 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
340 memcpy(ptr, version, len);
341#ifdef LOG_DEBUG
342 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
343#endif
344
345 return 0;
346}
347
Simon Glass6f3332a2020-11-05 06:32:08 -0700348/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700349 * smbios_string_table_len() - compute the string area size
350 *
351 * This computes the size of the string area including the string terminator.
352 *
Simon Glass721b4662021-02-04 21:17:19 -0700353 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200354 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700355 */
Simon Glass721b4662021-02-04 21:17:19 -0700356static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700357{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200358 /* In case no string is defined we have to return two \0 */
359 if (ctx->next_ptr == ctx->eos)
360 return 2;
361
Simon Glass721b4662021-02-04 21:17:19 -0700362 /* Allow for the final \0 after all strings */
363 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700364}
365
Simon Glass8e5ddb02021-02-04 21:17:17 -0700366static int smbios_write_type0(ulong *current, int handle,
367 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700368{
Simon Glass2d93add2018-11-22 13:46:37 -0700369 struct smbios_type0 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800370 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700371
Simon Glass2d93add2018-11-22 13:46:37 -0700372 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800373 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700374 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700375 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800376 t->vendor = smbios_add_prop_si(ctx, NULL, SYSID_SM_BIOS_VENDOR,
377 "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700378
Raymond Maoa28ef802024-12-06 14:54:22 -0800379 t->bios_ver = smbios_add_prop_si(ctx, "version", SYSID_SM_BIOS_VER,
380 PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700381 if (t->bios_ver)
382 gd->smbios_version = ctx->last_str;
383 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
384 gd->smbios_version);
385#ifdef LOG_DEBUG
386 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
387 1, strlen(gd->smbios_version) + 1, 0);
388#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800389 t->bios_release_date = smbios_add_prop_si(ctx, NULL,
390 SYSID_SM_BIOS_REL_DATE,
391 U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200392#ifdef CONFIG_ROM_SIZE
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +0200393 if (CONFIG_ROM_SIZE < SZ_16M) {
394 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
395 } else {
396 /* CONFIG_ROM_SIZE < 8 GiB */
397 t->bios_rom_size = 0xff;
398 t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20;
399 }
Alexander Graf66f96e12016-08-19 01:23:29 +0200400#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700401 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
402 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
403 BIOS_CHARACTERISTICS_UPGRADEABLE;
404#ifdef CONFIG_GENERATE_ACPI_TABLE
405 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
406#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200407#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300408 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200409#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300410 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200411
Simon Glassd7d16822021-02-04 21:17:16 -0700412 /* bios_major_release has only one byte, so drop century */
413 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
414 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700415 t->ec_major_release = 0xff;
416 t->ec_minor_release = 0xff;
417
Raymond Maof4b933d2024-12-06 14:54:18 -0800418 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700419 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700420 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700421
422 return len;
423}
424
Simon Glass8e5ddb02021-02-04 21:17:17 -0700425static int smbios_write_type1(ulong *current, int handle,
426 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700427{
Simon Glass2d93add2018-11-22 13:46:37 -0700428 struct smbios_type1 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800429 int len = sizeof(*t);
Simon Glass64b723f2017-08-03 12:22:12 -0600430 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700431
Simon Glass2d93add2018-11-22 13:46:37 -0700432 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800433 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700434 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700435 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800436
Michal Simeka7aa3952024-04-26 15:38:13 +0200437 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100438 SYSID_SM_SYSTEM_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200439 NULL);
440 t->product_name = smbios_add_prop_si(ctx, "product",
Raymond Maoa28ef802024-12-06 14:54:22 -0800441 SYSID_SM_SYSTEM_PRODUCT, NULL);
442 t->version = smbios_add_prop_si(ctx, "version", SYSID_SM_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100443 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200444 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200445 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Raymond Maof4b933d2024-12-06 14:54:18 -0800446 strlcpy((char *)t->uuid, serial_str, sizeof(t->uuid));
Simon Glass6f3332a2020-11-05 06:32:08 -0700447 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200448 t->serial_number = smbios_add_prop_si(ctx, "serial",
Simon Glassff3edc22024-10-31 18:50:23 +0100449 SYSID_SM_SYSTEM_SERIAL,
Michal Simeka7aa3952024-04-26 15:38:13 +0200450 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200451 }
Raymond Maoa28ef802024-12-06 14:54:22 -0800452 t->wakeup_type = smbios_get_val_si(ctx, "wakeup-type",
453 SYSID_SM_SYSTEM_WAKEUP,
454 SMBIOS_WAKEUP_TYPE_UNKNOWN);
455 t->sku_number = smbios_add_prop_si(ctx, "sku", SYSID_SM_SYSTEM_SKU,
456 NULL);
457 t->family = smbios_add_prop_si(ctx, "family", SYSID_SM_SYSTEM_FAMILY,
458 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700459
Raymond Maof4b933d2024-12-06 14:54:18 -0800460 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700461 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700462 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700463
464 return len;
465}
466
Simon Glass8e5ddb02021-02-04 21:17:17 -0700467static int smbios_write_type2(ulong *current, int handle,
468 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700469{
Simon Glass2d93add2018-11-22 13:46:37 -0700470 struct smbios_type2 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800471 int len = sizeof(*t);
Raymond Maoa28ef802024-12-06 14:54:22 -0800472 u8 *eos_addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700473
Raymond Maoa28ef802024-12-06 14:54:22 -0800474 /*
475 * reserve the space for the dynamic bytes of contained object handles.
476 * TODO: len += <obj_handle_num> * SMBIOS_TYPE2_CON_OBJ_HANDLE_SIZE
477 * obj_handle_num can be from DT node "baseboard" or sysinfo driver.
478 */
Simon Glass2d93add2018-11-22 13:46:37 -0700479 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800480 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700481 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Raymond Maoa28ef802024-12-06 14:54:22 -0800482
483 /* eos is at the end of the structure */
484 eos_addr = (u8 *)t + len - sizeof(t->eos);
485 smbios_set_eos(ctx, eos_addr);
486
Michal Simeka7aa3952024-04-26 15:38:13 +0200487 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100488 SYSID_SM_BASEBOARD_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200489 NULL);
490 t->product_name = smbios_add_prop_si(ctx, "product",
Raymond Maoa28ef802024-12-06 14:54:22 -0800491 SYSID_SM_BASEBOARD_PRODUCT, NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700492 t->version = smbios_add_prop_si(ctx, "version",
Raymond Maoa28ef802024-12-06 14:54:22 -0800493 SYSID_SM_BASEBOARD_VERSION, NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200494 t->serial_number = smbios_add_prop_si(ctx, "serial",
Raymond Maoa28ef802024-12-06 14:54:22 -0800495 SYSID_SM_BASEBOARD_SERIAL, NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200496 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
Simon Glassff3edc22024-10-31 18:50:23 +0100497 SYSID_SM_BASEBOARD_ASSET_TAG,
Michal Simeka7aa3952024-04-26 15:38:13 +0200498 NULL);
Raymond Maoa28ef802024-12-06 14:54:22 -0800499 t->feature_flags = smbios_get_val_si(ctx, "feature-flags",
500 SYSID_SM_BASEBOARD_FEATURE, 0);
501
502 t->chassis_location =
503 smbios_add_prop_si(ctx, "chassis-location",
504 SYSID_SM_BASEBOARD_CHASSIS_LOCAT, NULL);
505 t->board_type = smbios_get_val_si(ctx, "board-type",
506 SYSID_SM_BASEBOARD_TYPE,
507 SMBIOS_BOARD_TYPE_UNKNOWN);
508
509 /*
510 * TODO:
511 * Populate the Contained Object Handles if they exist
512 * t->number_contained_objects = <obj_handle_num>;
513 */
514
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100515 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700516
Raymond Maof4b933d2024-12-06 14:54:18 -0800517 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700518 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700519 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700520
521 return len;
522}
523
Simon Glass8e5ddb02021-02-04 21:17:17 -0700524static int smbios_write_type3(ulong *current, int handle,
525 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700526{
Simon Glass2d93add2018-11-22 13:46:37 -0700527 struct smbios_type3 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800528 int len = sizeof(*t);
Raymond Maoa28ef802024-12-06 14:54:22 -0800529 u8 *eos_addr;
530 size_t elem_size = 0;
Raymond Mao94aa78d2024-12-06 14:54:23 -0800531 __maybe_unused u8 *elem_addr;
532 __maybe_unused u8 *sku_num_addr;
Raymond Maoa28ef802024-12-06 14:54:22 -0800533
534 /*
535 * reserve the space for the dynamic bytes of contained elements.
536 * TODO: elem_size = <element_count> * <element_record_length>
537 * element_count and element_record_length can be from DT node
538 * "chassis" or sysinfo driver.
539 */
540 len += elem_size;
Bin Mengae5bedb2015-10-12 05:23:41 -0700541
Simon Glass2d93add2018-11-22 13:46:37 -0700542 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800543 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700544 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800545#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
546 elem_addr = (u8 *)t + offsetof(struct smbios_type3, sku_number);
547 sku_num_addr = elem_addr + elem_size;
548#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800549 /* eos is at the end of the structure */
550 eos_addr = (u8 *)t + len - sizeof(t->eos);
551 smbios_set_eos(ctx, eos_addr);
552
553 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
554 SYSID_SM_ENCLOSURE_MANUFACTURER,
555 NULL);
556 t->chassis_type = smbios_get_val_si(ctx, "chassis-type",
557 SYSID_SM_ENCLOSURE_TYPE,
558 SMBIOS_ENCLOSURE_UNKNOWN);
559 t->bootup_state = smbios_get_val_si(ctx, "bootup-state",
560 SYSID_SM_ENCLOSURE_BOOTUP,
561 SMBIOS_STATE_UNKNOWN);
562 t->power_supply_state = smbios_get_val_si(ctx, "power-supply-state",
563 SYSID_SM_ENCLOSURE_POW,
564 SMBIOS_STATE_UNKNOWN);
565 t->thermal_state = smbios_get_val_si(ctx, "thermal-state",
566 SYSID_SM_ENCLOSURE_THERMAL,
567 SMBIOS_STATE_UNKNOWN);
568 t->security_status = smbios_get_val_si(ctx, "security-status",
569 SYSID_SM_ENCLOSURE_SECURITY,
570 SMBIOS_SECURITY_UNKNOWN);
Bin Mengae5bedb2015-10-12 05:23:41 -0700571
Raymond Mao94aa78d2024-12-06 14:54:23 -0800572#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
573 t->version = smbios_add_prop_si(ctx, "version",
574 SYSID_SM_ENCLOSURE_VERSION, NULL);
575 t->serial_number = smbios_add_prop_si(ctx, "serial",
576 SYSID_SM_ENCLOSURE_SERIAL, NULL);
577 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
578 SYSID_SM_BASEBOARD_ASSET_TAG,
579 NULL);
580 t->oem_defined = smbios_get_val_si(ctx, "oem-defined",
581 SYSID_SM_ENCLOSURE_OEM, 0);
582 t->height = smbios_get_val_si(ctx, "height",
583 SYSID_SM_ENCLOSURE_HEIGHT, 0);
584 t->number_of_power_cords =
585 smbios_get_val_si(ctx, "number-of-power-cords",
586 SYSID_SM_ENCLOSURE_POWCORE_NUM, 0);
587
588 /*
589 * TODO: Populate the Contained Element Record if they exist
590 * t->element_count = <element_num>;
591 * t->element_record_length = <element_len>;
592 */
593
594 *sku_num_addr = smbios_add_prop_si(ctx, "sku", SYSID_SM_ENCLOSURE_SKU,
595 NULL);
596#endif
597
Raymond Maof4b933d2024-12-06 14:54:18 -0800598 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700599 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700600 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700601
602 return len;
603}
604
Simon Glass8e5ddb02021-02-04 21:17:17 -0700605static void smbios_write_type4_dm(struct smbios_type4 *t,
606 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200607{
608 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100609 const char *vendor = NULL;
610 const char *name = NULL;
Raymond Mao94aa78d2024-12-06 14:54:23 -0800611 __maybe_unused void *id_data = NULL;
612 __maybe_unused size_t id_size = 0;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200613
614#ifdef CONFIG_CPU
615 char processor_name[49];
616 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700617 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200618
Simon Glass0dd7ca22020-11-05 06:32:07 -0700619 uclass_find_first_device(UCLASS_CPU, &cpu);
620 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700621 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200622
623 if (plat->family)
624 processor_family = plat->family;
625 t->processor_id[0] = plat->id[0];
626 t->processor_id[1] = plat->id[1];
627
Simon Glass0dd7ca22020-11-05 06:32:07 -0700628 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200629 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700630 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200631 name = processor_name;
632 }
633#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800634 if (processor_family == SMBIOS_PROCESSOR_FAMILY_UNKNOWN)
635 processor_family =
636 smbios_get_val_si(ctx, "family",
637 SYSID_SM_PROCESSOR_FAMILY,
638 SMBIOS_PROCESSOR_FAMILY_UNKNOWN);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200639
Raymond Maoa28ef802024-12-06 14:54:22 -0800640 if (processor_family == SMBIOS_PROCESSOR_FAMILY_EXT)
641 t->processor_family2 =
642 smbios_get_val_si(ctx, "family2",
643 SYSID_SM_PROCESSOR_FAMILY2,
644 SMBIOS_PROCESSOR_FAMILY_UNKNOWN);
645
646 t->processor_family = processor_family;
647 t->processor_manufacturer =
648 smbios_add_prop_si(ctx, "manufacturer",
649 SYSID_SM_PROCESSOR_MANUFACT, vendor);
650 t->processor_version = smbios_add_prop_si(ctx, "version",
651 SYSID_SM_PROCESSOR_VERSION,
652 name);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800653
654#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
655 if (t->processor_id[0] || t->processor_id[1] ||
656 sysinfo_get_data(ctx->dev, SYSID_SM_PROCESSOR_ID, &id_data,
657 &id_size))
658 return;
659
660 if (id_data && id_size == sizeof(t->processor_id))
661 memcpy((u8 *)t->processor_id, id_data, id_size);
662#endif
Alexander Grafe44b3c02016-08-19 01:23:28 +0200663}
664
Simon Glass8e5ddb02021-02-04 21:17:17 -0700665static int smbios_write_type4(ulong *current, int handle,
666 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700667{
Simon Glass2d93add2018-11-22 13:46:37 -0700668 struct smbios_type4 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800669 int len = sizeof(*t);
Raymond Mao94aa78d2024-12-06 14:54:23 -0800670 __maybe_unused void *hdl;
671 __maybe_unused size_t hdl_size;
Bin Mengae5bedb2015-10-12 05:23:41 -0700672
Simon Glass2d93add2018-11-22 13:46:37 -0700673 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800674 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700675 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700676 smbios_set_eos(ctx, t->eos);
Raymond Maoa28ef802024-12-06 14:54:22 -0800677 t->socket_design = smbios_add_prop_si(ctx, "socket-design",
678 SYSID_SM_PROCESSOR_SOCKET, NULL);
679 t->processor_type = smbios_get_val_si(ctx, "processor-type",
680 SYSID_SM_PROCESSOR_TYPE,
681 SMBIOS_PROCESSOR_TYPE_UNKNOWN);
Simon Glass8e5ddb02021-02-04 21:17:17 -0700682 smbios_write_type4_dm(t, ctx);
Raymond Maoa28ef802024-12-06 14:54:22 -0800683
684 t->status = smbios_get_val_si(ctx, "processor-status",
685 SYSID_SM_PROCESSOR_STATUS,
686 SMBIOS_PROCESSOR_STATUS_UNKNOWN);
687 t->processor_upgrade =
688 smbios_get_val_si(ctx, "upgrade", SYSID_SM_PROCESSOR_UPGRADE,
689 SMBIOS_PROCESSOR_UPGRADE_UNKNOWN);
690
691 t->l1_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
692 t->l2_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
693 t->l3_cache_handle = SMBIOS_CACHE_HANDLE_NONE;
Bin Mengae5bedb2015-10-12 05:23:41 -0700694
Raymond Mao94aa78d2024-12-06 14:54:23 -0800695#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
696 t->voltage = smbios_get_val_si(ctx, "voltage",
697 SYSID_SM_PROCESSOR_VOLTAGE, 0);
698 t->external_clock = smbios_get_val_si(ctx, "external-clock",
699 SYSID_SM_PROCESSOR_EXT_CLOCK, 0);
700 t->max_speed = smbios_get_val_si(ctx, "max-speed",
701 SYSID_SM_PROCESSOR_MAX_SPEED, 0);
702 t->current_speed = smbios_get_val_si(ctx, "current-speed",
703 SYSID_SM_PROCESSOR_CUR_SPEED, 0);
704
705 /* Read the cache handles */
706 if (!sysinfo_get_data(ctx->dev, SYSID_SM_CACHE_HANDLE, &hdl,
707 &hdl_size) &&
708 (hdl_size == SYSINFO_CACHE_LVL_MAX * sizeof(u16))) {
709 u16 *handle = (u16 *)hdl;
710
711 if (*handle)
712 t->l1_cache_handle = *handle;
713
714 handle++;
715 if (*handle)
716 t->l2_cache_handle = *handle;
717
718 handle++;
719 if (*handle)
720 t->l3_cache_handle = *handle;
721 }
722
723 t->serial_number = smbios_add_prop_si(ctx, "serial",
724 SYSID_SM_PROCESSOR_SN, NULL);
725 t->asset_tag = smbios_add_prop_si(ctx, "asset-tag",
726 SYSID_SM_PROCESSOR_ASSET_TAG, NULL);
727 t->part_number = smbios_add_prop_si(ctx, "part-number",
728 SYSID_SM_PROCESSOR_PN, NULL);
729 t->core_count = smbios_get_val_si(ctx, "core-count",
730 SYSID_SM_PROCESSOR_CORE_CNT, 0);
731 t->core_enabled = smbios_get_val_si(ctx, "core-enabled",
732 SYSID_SM_PROCESSOR_CORE_EN, 0);
733 t->thread_count = smbios_get_val_si(ctx, "thread-count",
734 SYSID_SM_PROCESSOR_THREAD_CNT, 0);
735 t->processor_characteristics =
736 smbios_get_val_si(ctx, "characteristics",
737 SYSID_SM_PROCESSOR_CHARA,
738 SMBIOS_PROCESSOR_UND);
739 t->core_count2 = smbios_get_val_si(ctx, "core-count2",
740 SYSID_SM_PROCESSOR_CORE_CNT2, 0);
741 t->core_enabled2 = smbios_get_val_si(ctx, "core-enabled2",
742 SYSID_SM_PROCESSOR_CORE_EN2, 0);
743 t->thread_count2 = smbios_get_val_si(ctx, "thread-count2",
744 SYSID_SM_PROCESSOR_THREAD_CNT2, 0);
745 t->thread_enabled = smbios_get_val_si(ctx, "thread-enabled",
746 SYSID_SM_PROCESSOR_THREAD_EN, 0);
747#endif
748
749 len = t->hdr.length + smbios_string_table_len(ctx);
750 *current += len;
751 unmap_sysmem(t);
752
753 return len;
754}
755
756#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
757
758static int smbios_write_type7_1level(ulong *current, int handle,
759 struct smbios_ctx *ctx, int level)
760{
761 struct smbios_type7 *t;
762 int len = sizeof(*t);
763 void *hdl;
764 size_t hdl_size;
765
766 t = map_sysmem(*current, len);
767 memset(t, 0, len);
768 fill_smbios_header(t, SMBIOS_CACHE_INFORMATION, len, handle);
769 smbios_set_eos(ctx, t->eos);
770
771 t->socket_design = smbios_add_prop_si(ctx, "socket-design",
772 SYSID_SM_CACHE_SOCKET + level,
773 NULL);
774 t->config.data = smbios_get_val_si(ctx, "config",
775 SYSID_SM_CACHE_CONFIG + level,
776 (level - 1) | SMBIOS_CACHE_OP_UND);
777 t->max_size.data = smbios_get_val_si(ctx, "max-size",
778 SYSID_SM_CACHE_MAX_SIZE + level,
779 0);
780 t->inst_size.data = smbios_get_val_si(ctx, "installed-size",
781 SYSID_SM_CACHE_INST_SIZE + level,
782 0);
783 t->supp_sram_type.data =
784 smbios_get_val_si(ctx, "supported-sram-type",
785 SYSID_SM_CACHE_SUPSRAM_TYPE + level,
786 SMBIOS_CACHE_SRAM_TYPE_UNKNOWN);
787 t->curr_sram_type.data =
788 smbios_get_val_si(ctx, "current-sram-type",
789 SYSID_SM_CACHE_CURSRAM_TYPE + level,
790 SMBIOS_CACHE_SRAM_TYPE_UNKNOWN);
791 t->speed = smbios_get_val_si(ctx, "speed", SYSID_SM_CACHE_SPEED + level,
792 0);
793 t->err_corr_type = smbios_get_val_si(ctx, "error-correction-type",
794 SYSID_SM_CACHE_ERRCOR_TYPE + level,
795 SMBIOS_CACHE_ERRCORR_UNKNOWN);
796 t->sys_cache_type =
797 smbios_get_val_si(ctx, "system-cache-type",
798 SYSID_SM_CACHE_SCACHE_TYPE + level,
799 SMBIOS_CACHE_SYSCACHE_TYPE_UNKNOWN);
800 t->associativity = smbios_get_val_si(ctx, "associativity",
801 SYSID_SM_CACHE_ASSOC + level,
802 SMBIOS_CACHE_ASSOC_UNKNOWN);
803 t->max_size2.data = smbios_get_val_si(ctx, "max-size2",
804 SYSID_SM_CACHE_MAX_SIZE2 + level,
805 0);
806 t->inst_size2.data =
807 smbios_get_val_si(ctx, "installed-size2",
808 SYSID_SM_CACHE_INST_SIZE2 + level, 0);
809
810 /* Save the cache handles */
811 if (!sysinfo_get_data(ctx->dev, SYSID_SM_CACHE_HANDLE, &hdl,
812 &hdl_size)) {
813 if (hdl_size == SYSINFO_CACHE_LVL_MAX * sizeof(u16))
814 *((u16 *)hdl + level) = handle;
815 }
816
Raymond Maof4b933d2024-12-06 14:54:18 -0800817 len = t->hdr.length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700818 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700819 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700820
821 return len;
822}
823
Raymond Mao94aa78d2024-12-06 14:54:23 -0800824static int smbios_write_type7(ulong *current, int handle,
825 struct smbios_ctx *ctx)
826{
827 int len = 0;
828 int i, level;
829 ofnode parent = ctx->node;
830 struct smbios_ctx ctx_bak;
831
832 memcpy(&ctx_bak, ctx, sizeof(ctx_bak));
833
834 /* Get the number of level */
835 level = smbios_get_val_si(ctx, NULL, SYSID_SM_CACHE_LEVEL, 0);
836 if (level >= SYSINFO_CACHE_LVL_MAX) /* Error, return 0-length */
837 return 0;
838
839 for (i = 0; i <= level; i++) {
840 char buf[9] = "";
841
842 if (!snprintf(buf, sizeof(buf), "l%d-cache", i + 1))
843 return 0;
844 ctx->subnode_name = buf;
845 ctx->node = ofnode_find_subnode(parent, ctx->subnode_name);
846 len += smbios_write_type7_1level(current, handle++, ctx, i);
847 memcpy(ctx, &ctx_bak, sizeof(*ctx));
848 }
849 return len;
850}
851
852#endif /* #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE) */
853
Simon Glass8e5ddb02021-02-04 21:17:17 -0700854static int smbios_write_type32(ulong *current, int handle,
855 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700856{
Simon Glass2d93add2018-11-22 13:46:37 -0700857 struct smbios_type32 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800858 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700859
Simon Glass2d93add2018-11-22 13:46:37 -0700860 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800861 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700862 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700863 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700864
865 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700866 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700867
868 return len;
869}
870
Simon Glass8e5ddb02021-02-04 21:17:17 -0700871static int smbios_write_type127(ulong *current, int handle,
872 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700873{
Simon Glass2d93add2018-11-22 13:46:37 -0700874 struct smbios_type127 *t;
Raymond Maof4b933d2024-12-06 14:54:18 -0800875 int len = sizeof(*t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700876
Simon Glass2d93add2018-11-22 13:46:37 -0700877 t = map_sysmem(*current, len);
Raymond Maof4b933d2024-12-06 14:54:18 -0800878 memset(t, 0, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700879 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
880
881 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700882 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700883
884 return len;
885}
886
Simon Glass6f3332a2020-11-05 06:32:08 -0700887static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700888 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700889 { smbios_write_type1, "system", },
890 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100891 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700892 { smbios_write_type3, "chassis", },
Raymond Mao94aa78d2024-12-06 14:54:23 -0800893#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
894 /* Type 7 must ahead of type 4 to get cache handles. */
895 { smbios_write_type7, "cache", },
896#endif
Raymond Maoa28ef802024-12-06 14:54:22 -0800897 { smbios_write_type4, "processor"},
Simon Glass6f3332a2020-11-05 06:32:08 -0700898 { smbios_write_type32, },
899 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700900};
901
Simon Glassca37a392017-01-16 07:03:35 -0700902ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700903{
Simon Glass6f3332a2020-11-05 06:32:08 -0700904 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700905 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700906 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700907 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700908 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700909 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700910 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700911 int i;
912
Simon Glass8e5ddb02021-02-04 21:17:17 -0700913 ctx.node = ofnode_null();
Raymond Maoa28ef802024-12-06 14:54:22 -0800914 if (CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700915 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200916 if (ctx.dev) {
917 int ret;
918
Simon Glass8e5ddb02021-02-04 21:17:17 -0700919 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200920 ret = sysinfo_detect(ctx.dev);
Simon Glassbd5515d2024-06-23 14:30:33 -0600921
922 /*
923 * ignore the error since many boards don't implement
924 * this and we can still use the info in the devicetree
925 */
926 ret = log_msg_ret("sys", ret);
Michal Simek7d955002024-04-26 15:38:12 +0200927 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700928 } else {
929 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700930 }
931
Simon Glass3905e922023-12-31 08:25:45 -0700932 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700933
Simon Glassfc4b5522023-12-31 08:25:51 -0700934 /* move past the (so-far-unwritten) header to start writing structs */
935 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700936 tables = addr;
937
938 /* populate minimum required tables */
939 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700940 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700941
942 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200943 ctx.subnode_name = NULL;
944 if (method->subnode_name) {
945 ctx.subnode_name = method->subnode_name;
946 if (IS_ENABLED(CONFIG_OF_CONTROL))
947 ctx.node = ofnode_find_subnode(parent_node,
948 method->subnode_name);
949 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100950 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700951 }
952
Simon Glass2d93add2018-11-22 13:46:37 -0700953 /*
954 * We must use a pointer here so things work correctly on sandbox. The
955 * user of this table is not aware of the mapping of addresses to
956 * sandbox's DRAM buffer.
957 */
958 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700959
Simon Glassfc4b5522023-12-31 08:25:51 -0700960 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100961 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
962 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700963 memcpy(se->anchor, "_SM3_", 5);
964 se->length = sizeof(struct smbios3_entry);
965 se->major_ver = SMBIOS_MAJOR_VER;
966 se->minor_ver = SMBIOS_MINOR_VER;
967 se->doc_rev = 0;
968 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100969 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700970 se->struct_table_address = table_addr;
971 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
972 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700973
974 return addr;
975}