blob: defb6b42f45a85f275c0f0518dedf98b5cab870d [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/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700211 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
212 *
213 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700214 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700215 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700216 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100217 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200218 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200219 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700220 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700221static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200222 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700223{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200224 int ret;
225
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200226 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100227 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200228
229 if (!prop)
230 return smbios_add_string(ctx, dval);
231
Simon Glasscf67d6d2021-02-04 21:17:23 -0700232 if (sysinfo_id && ctx->dev) {
233 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700234
235 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
236 if (!ret)
237 return smbios_add_string(ctx, val);
238 }
Simon Glass95f47082020-11-05 06:32:18 -0700239 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200240 const char *str = NULL;
241 char str_dt[128] = { 0 };
242 /*
243 * If the node is not valid fallback and try the entire DT
244 * so we can at least fill in manufacturer and board type
245 */
246 if (ofnode_valid(ctx->node)) {
247 str = ofnode_read_string(ctx->node, prop);
248 } else {
249 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700250
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200251 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200252 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
253 str = (const char *)str_dt;
254 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200255
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200256 ret = smbios_add_string(ctx, str && *str ? str : dval);
257 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700258 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700259
260 return 0;
261}
262
Simon Glasscf67d6d2021-02-04 21:17:23 -0700263/**
264 * smbios_add_prop() - Add a property from the devicetree
265 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200266 * @prop: property to write. The default string will be written if
267 * prop is NULL
268 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200269 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700270 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200271static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
272 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700273{
Simon Glassff3edc22024-10-31 18:50:23 +0100274 return smbios_add_prop_si(ctx, prop, SYSID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700275}
276
Simon Glassdff4e862021-02-04 21:17:18 -0700277static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
278{
279 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700280 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700281 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700282}
283
Simon Glassa05eb042021-02-04 21:17:20 -0700284int smbios_update_version(const char *version)
285{
286 char *ptr = gd->smbios_version;
287 uint old_len, len;
288
289 if (!ptr)
290 return log_ret(-ENOENT);
291
292 /*
293 * This string is supposed to have at least enough bytes and is
294 * padded with spaces. Update it, taking care not to move the
295 * \0 terminator, so that other strings in the string table
296 * are not disturbed. See smbios_add_string()
297 */
298 old_len = strnlen(ptr, SMBIOS_STR_MAX);
299 len = strnlen(version, SMBIOS_STR_MAX);
300 if (len > old_len)
301 return log_ret(-ENOSPC);
302
303 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
304 memcpy(ptr, version, len);
305#ifdef LOG_DEBUG
306 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
307#endif
308
309 return 0;
310}
311
Simon Glass6f3332a2020-11-05 06:32:08 -0700312/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700313 * smbios_string_table_len() - compute the string area size
314 *
315 * This computes the size of the string area including the string terminator.
316 *
Simon Glass721b4662021-02-04 21:17:19 -0700317 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200318 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700319 */
Simon Glass721b4662021-02-04 21:17:19 -0700320static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700321{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200322 /* In case no string is defined we have to return two \0 */
323 if (ctx->next_ptr == ctx->eos)
324 return 2;
325
Simon Glass721b4662021-02-04 21:17:19 -0700326 /* Allow for the final \0 after all strings */
327 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700328}
329
Simon Glass8e5ddb02021-02-04 21:17:17 -0700330static int smbios_write_type0(ulong *current, int handle,
331 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700332{
Simon Glass2d93add2018-11-22 13:46:37 -0700333 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700334 int len = sizeof(struct smbios_type0);
335
Simon Glass2d93add2018-11-22 13:46:37 -0700336 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700337 memset(t, 0, sizeof(struct smbios_type0));
338 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700339 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200340 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700341
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200342 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700343 if (t->bios_ver)
344 gd->smbios_version = ctx->last_str;
345 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
346 gd->smbios_version);
347#ifdef LOG_DEBUG
348 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
349 1, strlen(gd->smbios_version) + 1, 0);
350#endif
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200351 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200352#ifdef CONFIG_ROM_SIZE
Heinrich Schuchardtbfeb45f2024-07-23 16:44:23 +0200353 if (CONFIG_ROM_SIZE < SZ_16M) {
354 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
355 } else {
356 /* CONFIG_ROM_SIZE < 8 GiB */
357 t->bios_rom_size = 0xff;
358 t->extended_bios_rom_size = CONFIG_ROM_SIZE >> 20;
359 }
Alexander Graf66f96e12016-08-19 01:23:29 +0200360#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700361 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
362 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
363 BIOS_CHARACTERISTICS_UPGRADEABLE;
364#ifdef CONFIG_GENERATE_ACPI_TABLE
365 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
366#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200367#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300368 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200369#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300370 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200371
Simon Glassd7d16822021-02-04 21:17:16 -0700372 /* bios_major_release has only one byte, so drop century */
373 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
374 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700375 t->ec_major_release = 0xff;
376 t->ec_minor_release = 0xff;
377
Simon Glass721b4662021-02-04 21:17:19 -0700378 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700379 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700380 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700381
382 return len;
383}
384
Simon Glass8e5ddb02021-02-04 21:17:17 -0700385static int smbios_write_type1(ulong *current, int handle,
386 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700387{
Simon Glass2d93add2018-11-22 13:46:37 -0700388 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700389 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600390 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700391
Simon Glass2d93add2018-11-22 13:46:37 -0700392 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700393 memset(t, 0, sizeof(struct smbios_type1));
394 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700395 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200396 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100397 SYSID_SM_SYSTEM_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200398 NULL);
399 t->product_name = smbios_add_prop_si(ctx, "product",
Simon Glassff3edc22024-10-31 18:50:23 +0100400 SYSID_SM_SYSTEM_PRODUCT,
Michal Simeka7aa3952024-04-26 15:38:13 +0200401 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700402 t->version = smbios_add_prop_si(ctx, "version",
Simon Glassff3edc22024-10-31 18:50:23 +0100403 SYSID_SM_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100404 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200405 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200406 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700407 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
408 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200409 t->serial_number = smbios_add_prop_si(ctx, "serial",
Simon Glassff3edc22024-10-31 18:50:23 +0100410 SYSID_SM_SYSTEM_SERIAL,
Michal Simeka7aa3952024-04-26 15:38:13 +0200411 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200412 }
Heinrich Schuchardta0501b52024-02-10 12:06:48 +0100413 t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN;
Michal Simeka7aa3952024-04-26 15:38:13 +0200414 t->sku_number = smbios_add_prop_si(ctx, "sku",
Simon Glassff3edc22024-10-31 18:50:23 +0100415 SYSID_SM_SYSTEM_SKU, NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200416 t->family = smbios_add_prop_si(ctx, "family",
Simon Glassff3edc22024-10-31 18:50:23 +0100417 SYSID_SM_SYSTEM_FAMILY, NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700418
Simon Glass721b4662021-02-04 21:17:19 -0700419 len = t->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_type2(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_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700430 int len = sizeof(struct smbios_type2);
431
Simon Glass2d93add2018-11-22 13:46:37 -0700432 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700433 memset(t, 0, sizeof(struct smbios_type2));
434 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700435 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200436 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
Simon Glassff3edc22024-10-31 18:50:23 +0100437 SYSID_SM_BASEBOARD_MANUFACTURER,
Michal Simeka7aa3952024-04-26 15:38:13 +0200438 NULL);
439 t->product_name = smbios_add_prop_si(ctx, "product",
Simon Glassff3edc22024-10-31 18:50:23 +0100440 SYSID_SM_BASEBOARD_PRODUCT,
Michal Simeka7aa3952024-04-26 15:38:13 +0200441 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700442 t->version = smbios_add_prop_si(ctx, "version",
Simon Glassff3edc22024-10-31 18:50:23 +0100443 SYSID_SM_BASEBOARD_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100444 NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200445
446 t->serial_number = smbios_add_prop_si(ctx, "serial",
Simon Glassff3edc22024-10-31 18:50:23 +0100447 SYSID_SM_BASEBOARD_SERIAL,
Michal Simeka7aa3952024-04-26 15:38:13 +0200448 NULL);
449 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
Simon Glassff3edc22024-10-31 18:50:23 +0100450 SYSID_SM_BASEBOARD_ASSET_TAG,
Michal Simeka7aa3952024-04-26 15:38:13 +0200451 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700452 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
453 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100454 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700455
Simon Glass721b4662021-02-04 21:17:19 -0700456 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700457 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700458 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700459
460 return len;
461}
462
Simon Glass8e5ddb02021-02-04 21:17:17 -0700463static int smbios_write_type3(ulong *current, int handle,
464 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700465{
Simon Glass2d93add2018-11-22 13:46:37 -0700466 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700467 int len = sizeof(struct smbios_type3);
468
Simon Glass2d93add2018-11-22 13:46:37 -0700469 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700470 memset(t, 0, sizeof(struct smbios_type3));
471 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700472 smbios_set_eos(ctx, t->eos);
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100473 t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700474 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
475 t->bootup_state = SMBIOS_STATE_SAFE;
476 t->power_supply_state = SMBIOS_STATE_SAFE;
477 t->thermal_state = SMBIOS_STATE_SAFE;
478 t->security_status = SMBIOS_SECURITY_NONE;
479
Simon Glass721b4662021-02-04 21:17:19 -0700480 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700481 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700482 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700483
484 return len;
485}
486
Simon Glass8e5ddb02021-02-04 21:17:17 -0700487static void smbios_write_type4_dm(struct smbios_type4 *t,
488 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200489{
490 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100491 const char *vendor = NULL;
492 const char *name = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200493
494#ifdef CONFIG_CPU
495 char processor_name[49];
496 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700497 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200498
Simon Glass0dd7ca22020-11-05 06:32:07 -0700499 uclass_find_first_device(UCLASS_CPU, &cpu);
500 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700501 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200502
503 if (plat->family)
504 processor_family = plat->family;
505 t->processor_id[0] = plat->id[0];
506 t->processor_id[1] = plat->id[1];
507
Simon Glass0dd7ca22020-11-05 06:32:07 -0700508 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200509 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700510 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200511 name = processor_name;
512 }
513#endif
514
Heinrich Schuchardtb5f9aef2023-12-28 08:30:23 +0100515 t->processor_family = 0xfe;
516 t->processor_family2 = processor_family;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200517 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
518 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200519}
520
Simon Glass8e5ddb02021-02-04 21:17:17 -0700521static int smbios_write_type4(ulong *current, int handle,
522 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700523{
Simon Glass2d93add2018-11-22 13:46:37 -0700524 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700525 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700526
Simon Glass2d93add2018-11-22 13:46:37 -0700527 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700528 memset(t, 0, sizeof(struct smbios_type4));
529 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700530 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700531 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700532 smbios_write_type4_dm(t, ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700533 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
534 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
535 t->l1_cache_handle = 0xffff;
536 t->l2_cache_handle = 0xffff;
537 t->l3_cache_handle = 0xffff;
Bin Mengae5bedb2015-10-12 05:23:41 -0700538
Simon Glass721b4662021-02-04 21:17:19 -0700539 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700540 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700541 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700542
543 return len;
544}
545
Simon Glass8e5ddb02021-02-04 21:17:17 -0700546static int smbios_write_type32(ulong *current, int handle,
547 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700548{
Simon Glass2d93add2018-11-22 13:46:37 -0700549 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700550 int len = sizeof(struct smbios_type32);
551
Simon Glass2d93add2018-11-22 13:46:37 -0700552 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700553 memset(t, 0, sizeof(struct smbios_type32));
554 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700555 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700556
557 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700558 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700559
560 return len;
561}
562
Simon Glass8e5ddb02021-02-04 21:17:17 -0700563static int smbios_write_type127(ulong *current, int handle,
564 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700565{
Simon Glass2d93add2018-11-22 13:46:37 -0700566 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700567 int len = sizeof(struct smbios_type127);
568
Simon Glass2d93add2018-11-22 13:46:37 -0700569 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700570 memset(t, 0, sizeof(struct smbios_type127));
571 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
572
573 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700574 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700575
576 return len;
577}
578
Simon Glass6f3332a2020-11-05 06:32:08 -0700579static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700580 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700581 { smbios_write_type1, "system", },
582 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100583 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700584 { smbios_write_type3, "chassis", },
585 { smbios_write_type4, },
586 { smbios_write_type32, },
587 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700588};
589
Simon Glassca37a392017-01-16 07:03:35 -0700590ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700591{
Simon Glass6f3332a2020-11-05 06:32:08 -0700592 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700593 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700594 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700595 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700596 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700597 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700598 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700599 int i;
600
Simon Glass8e5ddb02021-02-04 21:17:17 -0700601 ctx.node = ofnode_null();
Quentin Schulz60b515d2024-06-10 18:13:46 +0200602 if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700603 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200604 if (ctx.dev) {
605 int ret;
606
Simon Glass8e5ddb02021-02-04 21:17:17 -0700607 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200608 ret = sysinfo_detect(ctx.dev);
Simon Glassbd5515d2024-06-23 14:30:33 -0600609
610 /*
611 * ignore the error since many boards don't implement
612 * this and we can still use the info in the devicetree
613 */
614 ret = log_msg_ret("sys", ret);
Michal Simek7d955002024-04-26 15:38:12 +0200615 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700616 } else {
617 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700618 }
619
Simon Glass3905e922023-12-31 08:25:45 -0700620 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700621
Simon Glassfc4b5522023-12-31 08:25:51 -0700622 /* move past the (so-far-unwritten) header to start writing structs */
623 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700624 tables = addr;
625
626 /* populate minimum required tables */
627 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700628 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700629
630 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200631 ctx.subnode_name = NULL;
632 if (method->subnode_name) {
633 ctx.subnode_name = method->subnode_name;
634 if (IS_ENABLED(CONFIG_OF_CONTROL))
635 ctx.node = ofnode_find_subnode(parent_node,
636 method->subnode_name);
637 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100638 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700639 }
640
Simon Glass2d93add2018-11-22 13:46:37 -0700641 /*
642 * We must use a pointer here so things work correctly on sandbox. The
643 * user of this table is not aware of the mapping of addresses to
644 * sandbox's DRAM buffer.
645 */
646 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700647
Simon Glassfc4b5522023-12-31 08:25:51 -0700648 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100649 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
650 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700651 memcpy(se->anchor, "_SM3_", 5);
652 se->length = sizeof(struct smbios3_entry);
653 se->major_ver = SMBIOS_MAJOR_VER;
654 se->minor_ver = SMBIOS_MINOR_VER;
655 se->doc_rev = 0;
656 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100657 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700658 se->struct_table_address = table_addr;
659 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
660 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700661
662 return addr;
663}