blob: 7c24ea129eb5fce3540d72ada56a21547a9c7dc7 [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/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700210 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
211 *
212 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700213 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700214 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700215 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100216 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200217 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200218 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700219 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700220static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200221 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700222{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200223 int ret;
224
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200225 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100226 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200227
228 if (!prop)
229 return smbios_add_string(ctx, dval);
230
Simon Glasscf67d6d2021-02-04 21:17:23 -0700231 if (sysinfo_id && ctx->dev) {
232 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700233
234 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
235 if (!ret)
236 return smbios_add_string(ctx, val);
237 }
Simon Glass95f47082020-11-05 06:32:18 -0700238 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200239 const char *str = NULL;
240 char str_dt[128] = { 0 };
241 /*
242 * If the node is not valid fallback and try the entire DT
243 * so we can at least fill in manufacturer and board type
244 */
245 if (ofnode_valid(ctx->node)) {
246 str = ofnode_read_string(ctx->node, prop);
247 } else {
248 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700249
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200250 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200251 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
252 str = (const char *)str_dt;
253 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200254
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200255 ret = smbios_add_string(ctx, str && *str ? str : dval);
256 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700257 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700258
259 return 0;
260}
261
Simon Glasscf67d6d2021-02-04 21:17:23 -0700262/**
263 * smbios_add_prop() - Add a property from the devicetree
264 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200265 * @prop: property to write. The default string will be written if
266 * prop is NULL
267 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200268 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700269 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200270static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
271 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700272{
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200273 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700274}
275
Simon Glassdff4e862021-02-04 21:17:18 -0700276static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
277{
278 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700279 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700280 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700281}
282
Simon Glassa05eb042021-02-04 21:17:20 -0700283int smbios_update_version(const char *version)
284{
285 char *ptr = gd->smbios_version;
286 uint old_len, len;
287
288 if (!ptr)
289 return log_ret(-ENOENT);
290
291 /*
292 * This string is supposed to have at least enough bytes and is
293 * padded with spaces. Update it, taking care not to move the
294 * \0 terminator, so that other strings in the string table
295 * are not disturbed. See smbios_add_string()
296 */
297 old_len = strnlen(ptr, SMBIOS_STR_MAX);
298 len = strnlen(version, SMBIOS_STR_MAX);
299 if (len > old_len)
300 return log_ret(-ENOSPC);
301
302 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
303 memcpy(ptr, version, len);
304#ifdef LOG_DEBUG
305 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
306#endif
307
308 return 0;
309}
310
Simon Glass6f3332a2020-11-05 06:32:08 -0700311/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700312 * smbios_string_table_len() - compute the string area size
313 *
314 * This computes the size of the string area including the string terminator.
315 *
Simon Glass721b4662021-02-04 21:17:19 -0700316 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200317 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700318 */
Simon Glass721b4662021-02-04 21:17:19 -0700319static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700320{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200321 /* In case no string is defined we have to return two \0 */
322 if (ctx->next_ptr == ctx->eos)
323 return 2;
324
Simon Glass721b4662021-02-04 21:17:19 -0700325 /* Allow for the final \0 after all strings */
326 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700327}
328
Simon Glass8e5ddb02021-02-04 21:17:17 -0700329static int smbios_write_type0(ulong *current, int handle,
330 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700331{
Simon Glass2d93add2018-11-22 13:46:37 -0700332 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700333 int len = sizeof(struct smbios_type0);
334
Simon Glass2d93add2018-11-22 13:46:37 -0700335 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700336 memset(t, 0, sizeof(struct smbios_type0));
337 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700338 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200339 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700340
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200341 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700342 if (t->bios_ver)
343 gd->smbios_version = ctx->last_str;
344 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
345 gd->smbios_version);
346#ifdef LOG_DEBUG
347 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
348 1, strlen(gd->smbios_version) + 1, 0);
349#endif
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200350 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200351#ifdef CONFIG_ROM_SIZE
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +0200352 if (CONFIG_ROM_SIZE < SZ_16M) {
353 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
354 } else {
355 /* CONFIG_ROM_SIZE < 8 GiB */
356 t->bios_rom_size = 0xff;
357 t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20;
358 }
Alexander Graf66f96e12016-08-19 01:23:29 +0200359#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700360 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
361 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
362 BIOS_CHARACTERISTICS_UPGRADEABLE;
363#ifdef CONFIG_GENERATE_ACPI_TABLE
364 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
365#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200366#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300367 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200368#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300369 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200370
Simon Glassd7d16822021-02-04 21:17:16 -0700371 /* bios_major_release has only one byte, so drop century */
372 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
373 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700374 t->ec_major_release = 0xff;
375 t->ec_minor_release = 0xff;
376
Simon Glass721b4662021-02-04 21:17:19 -0700377 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700378 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700379 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700380
381 return len;
382}
383
Simon Glass8e5ddb02021-02-04 21:17:17 -0700384static int smbios_write_type1(ulong *current, int handle,
385 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700386{
Simon Glass2d93add2018-11-22 13:46:37 -0700387 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700388 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600389 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700390
Simon Glass2d93add2018-11-22 13:46:37 -0700391 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700392 memset(t, 0, sizeof(struct smbios_type1));
393 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700394 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200395 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
396 SYSINFO_ID_SMBIOS_SYSTEM_MANUFACTURER,
397 NULL);
398 t->product_name = smbios_add_prop_si(ctx, "product",
399 SYSINFO_ID_SMBIOS_SYSTEM_PRODUCT,
400 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700401 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200402 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100403 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200404 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200405 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700406 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
407 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200408 t->serial_number = smbios_add_prop_si(ctx, "serial",
409 SYSINFO_ID_SMBIOS_SYSTEM_SERIAL,
410 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200411 }
Heinrich Schuchardta0501b52024-02-10 12:06:48 +0100412 t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN;
Michal Simeka7aa3952024-04-26 15:38:13 +0200413 t->sku_number = smbios_add_prop_si(ctx, "sku",
414 SYSINFO_ID_SMBIOS_SYSTEM_SKU, NULL);
415 t->family = smbios_add_prop_si(ctx, "family",
416 SYSINFO_ID_SMBIOS_SYSTEM_FAMILY, NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700417
Simon Glass721b4662021-02-04 21:17:19 -0700418 len = t->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_type2(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_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700429 int len = sizeof(struct smbios_type2);
430
Simon Glass2d93add2018-11-22 13:46:37 -0700431 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700432 memset(t, 0, sizeof(struct smbios_type2));
433 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700434 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200435 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
436 SYSINFO_ID_SMBIOS_BASEBOARD_MANUFACTURER,
437 NULL);
438 t->product_name = smbios_add_prop_si(ctx, "product",
439 SYSINFO_ID_SMBIOS_BASEBOARD_PRODUCT,
440 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700441 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200442 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100443 NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200444
445 t->serial_number = smbios_add_prop_si(ctx, "serial",
446 SYSINFO_ID_SMBIOS_BASEBOARD_SERIAL,
447 NULL);
448 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
449 SYSINFO_ID_SMBIOS_BASEBOARD_ASSET_TAG,
450 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700451 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
452 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100453 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700454
Simon Glass721b4662021-02-04 21:17:19 -0700455 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700456 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700457 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700458
459 return len;
460}
461
Simon Glass8e5ddb02021-02-04 21:17:17 -0700462static int smbios_write_type3(ulong *current, int handle,
463 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700464{
Simon Glass2d93add2018-11-22 13:46:37 -0700465 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700466 int len = sizeof(struct smbios_type3);
467
Simon Glass2d93add2018-11-22 13:46:37 -0700468 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700469 memset(t, 0, sizeof(struct smbios_type3));
470 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700471 smbios_set_eos(ctx, t->eos);
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100472 t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700473 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
474 t->bootup_state = SMBIOS_STATE_SAFE;
475 t->power_supply_state = SMBIOS_STATE_SAFE;
476 t->thermal_state = SMBIOS_STATE_SAFE;
477 t->security_status = SMBIOS_SECURITY_NONE;
478
Simon Glass721b4662021-02-04 21:17:19 -0700479 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700480 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700481 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700482
483 return len;
484}
485
Simon Glass8e5ddb02021-02-04 21:17:17 -0700486static void smbios_write_type4_dm(struct smbios_type4 *t,
487 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200488{
489 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100490 const char *vendor = NULL;
491 const char *name = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200492
493#ifdef CONFIG_CPU
494 char processor_name[49];
495 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700496 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200497
Simon Glass0dd7ca22020-11-05 06:32:07 -0700498 uclass_find_first_device(UCLASS_CPU, &cpu);
499 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700500 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200501
502 if (plat->family)
503 processor_family = plat->family;
504 t->processor_id[0] = plat->id[0];
505 t->processor_id[1] = plat->id[1];
506
Simon Glass0dd7ca22020-11-05 06:32:07 -0700507 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200508 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700509 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200510 name = processor_name;
511 }
512#endif
513
Heinrich Schuchardtb5f9aef2023-12-28 08:30:23 +0100514 t->processor_family = 0xfe;
515 t->processor_family2 = processor_family;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200516 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
517 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200518}
519
Simon Glass8e5ddb02021-02-04 21:17:17 -0700520static int smbios_write_type4(ulong *current, int handle,
521 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700522{
Simon Glass2d93add2018-11-22 13:46:37 -0700523 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700524 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700525
Simon Glass2d93add2018-11-22 13:46:37 -0700526 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700527 memset(t, 0, sizeof(struct smbios_type4));
528 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700529 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700530 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700531 smbios_write_type4_dm(t, ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700532 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
533 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
534 t->l1_cache_handle = 0xffff;
535 t->l2_cache_handle = 0xffff;
536 t->l3_cache_handle = 0xffff;
Bin Mengae5bedb2015-10-12 05:23:41 -0700537
Simon Glass721b4662021-02-04 21:17:19 -0700538 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700539 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700540 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700541
542 return len;
543}
544
Simon Glass8e5ddb02021-02-04 21:17:17 -0700545static int smbios_write_type32(ulong *current, int handle,
546 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700547{
Simon Glass2d93add2018-11-22 13:46:37 -0700548 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700549 int len = sizeof(struct smbios_type32);
550
Simon Glass2d93add2018-11-22 13:46:37 -0700551 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700552 memset(t, 0, sizeof(struct smbios_type32));
553 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700554 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700555
556 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700557 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700558
559 return len;
560}
561
Simon Glass8e5ddb02021-02-04 21:17:17 -0700562static int smbios_write_type127(ulong *current, int handle,
563 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700564{
Simon Glass2d93add2018-11-22 13:46:37 -0700565 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700566 int len = sizeof(struct smbios_type127);
567
Simon Glass2d93add2018-11-22 13:46:37 -0700568 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700569 memset(t, 0, sizeof(struct smbios_type127));
570 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
571
572 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700573 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700574
575 return len;
576}
577
Simon Glass6f3332a2020-11-05 06:32:08 -0700578static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700579 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700580 { smbios_write_type1, "system", },
581 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100582 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700583 { smbios_write_type3, "chassis", },
584 { smbios_write_type4, },
585 { smbios_write_type32, },
586 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700587};
588
Simon Glassca37a392017-01-16 07:03:35 -0700589ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700590{
Simon Glass6f3332a2020-11-05 06:32:08 -0700591 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700592 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700593 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700594 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700595 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700596 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700597 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700598 int i;
599
Simon Glass8e5ddb02021-02-04 21:17:17 -0700600 ctx.node = ofnode_null();
Quentin Schulz60b515d2024-06-10 18:13:46 +0200601 if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700602 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200603 if (ctx.dev) {
604 int ret;
605
Simon Glass8e5ddb02021-02-04 21:17:17 -0700606 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200607 ret = sysinfo_detect(ctx.dev);
Simon Glassbd5515d2024-06-23 14:30:33 -0600608
609 /*
610 * ignore the error since many boards don't implement
611 * this and we can still use the info in the devicetree
612 */
613 ret = log_msg_ret("sys", ret);
Michal Simek7d955002024-04-26 15:38:12 +0200614 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700615 } else {
616 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700617 }
618
Simon Glass3905e922023-12-31 08:25:45 -0700619 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700620
Simon Glassfc4b5522023-12-31 08:25:51 -0700621 /* move past the (so-far-unwritten) header to start writing structs */
622 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700623 tables = addr;
624
625 /* populate minimum required tables */
626 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700627 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700628
629 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200630 ctx.subnode_name = NULL;
631 if (method->subnode_name) {
632 ctx.subnode_name = method->subnode_name;
633 if (IS_ENABLED(CONFIG_OF_CONTROL))
634 ctx.node = ofnode_find_subnode(parent_node,
635 method->subnode_name);
636 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100637 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700638 }
639
Simon Glass2d93add2018-11-22 13:46:37 -0700640 /*
641 * We must use a pointer here so things work correctly on sandbox. The
642 * user of this table is not aware of the mapping of addresses to
643 * sandbox's DRAM buffer.
644 */
645 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700646
Simon Glassfc4b5522023-12-31 08:25:51 -0700647 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100648 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
649 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700650 memcpy(se->anchor, "_SM3_", 5);
651 se->length = sizeof(struct smbios3_entry);
652 se->major_ver = SMBIOS_MAJOR_VER;
653 se->minor_ver = SMBIOS_MINOR_VER;
654 se->doc_rev = 0;
655 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100656 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700657 se->struct_table_address = table_addr;
658 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
659 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700660
661 return addr;
662}