blob: 4126466e34a84438bf65032e1378f43baf4aab2b [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
Bin Mengae5bedb2015-10-12 05:23:41 -070025
Pali Rohárebe9fd82021-04-22 18:09:57 +020026/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
27#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
28 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
29#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
30#endif
31
32/*
33 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
34 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
35 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
36 * BIOS Release Date should contain date when code was released
37 * and not when it was built or compiled.
38 */
39#if U_BOOT_VERSION_NUM_PATCH < 10
40#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
41#else
42#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
43#endif
44#define U_BOOT_DMI_DAY "01"
45#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
46#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
47
Simon Glassa05eb042021-02-04 21:17:20 -070048DECLARE_GLOBAL_DATA_PTR;
49
Bin Mengae5bedb2015-10-12 05:23:41 -070050/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +020051 * struct map_sysinfo - Mapping of sysinfo strings to DT
52 *
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020053 * @si_str: sysinfo string
Ilias Apalodimasada83d92023-12-07 11:18:50 +020054 * @dt_str: DT string
55 * @max: Max index of the tokenized string to pick. Counting starts from 0
56 *
57 */
58struct map_sysinfo {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020059 const char *si_node;
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020060 const char *si_str;
Ilias Apalodimasada83d92023-12-07 11:18:50 +020061 const char *dt_str;
62 int max;
63};
64
65static const struct map_sysinfo sysinfo_to_dt[] = {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020066 { .si_node = "system", .si_str = "product", .dt_str = "model", 2 },
67 { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 },
68 { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 },
69 { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 },
Ilias Apalodimasada83d92023-12-07 11:18:50 +020070};
71
72/**
Simon Glass8e5ddb02021-02-04 21:17:17 -070073 * struct smbios_ctx - context for writing SMBIOS tables
74 *
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020075 * @node: node containing the information to write (ofnode_null()
76 * if none)
77 * @dev: sysinfo device to use (NULL if none)
78 * @subnode_name: sysinfo subnode_name. Used for DT fallback
79 * @eos: end-of-string pointer for the table being processed.
80 * This is set up when we start processing a table
81 * @next_ptr: pointer to the start of the next string to be added.
82 * When the table is not empty, this points to the byte
83 * after the \0 of the previous string.
84 * @last_str: points to the last string that was written to the table,
85 * or NULL if none
Simon Glass8e5ddb02021-02-04 21:17:17 -070086 */
87struct smbios_ctx {
88 ofnode node;
89 struct udevice *dev;
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020090 const char *subnode_name;
Simon Glassdff4e862021-02-04 21:17:18 -070091 char *eos;
Simon Glass721b4662021-02-04 21:17:19 -070092 char *next_ptr;
Simon Glassa05eb042021-02-04 21:17:20 -070093 char *last_str;
Simon Glass8e5ddb02021-02-04 21:17:17 -070094};
95
96/**
Simon Glass099d3ae2021-02-04 21:17:14 -070097 * Function prototype to write a specific type of SMBIOS structure
98 *
99 * @addr: start address to write the structure
100 * @handle: the structure's handle, a unique 16-bit number
Simon Glass8e5ddb02021-02-04 21:17:17 -0700101 * @ctx: context for writing the tables
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200102 * Return: size of the structure
Simon Glass099d3ae2021-02-04 21:17:14 -0700103 */
Simon Glass8e5ddb02021-02-04 21:17:17 -0700104typedef int (*smbios_write_type)(ulong *addr, int handle,
105 struct smbios_ctx *ctx);
Simon Glass099d3ae2021-02-04 21:17:14 -0700106
107/**
Simon Glass6f3332a2020-11-05 06:32:08 -0700108 * struct smbios_write_method - Information about a table-writing function
109 *
110 * @write: Function to call
111 * @subnode_name: Name of subnode which has the information for this function,
112 * NULL if none
113 */
114struct smbios_write_method {
115 smbios_write_type write;
116 const char *subnode_name;
117};
118
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200119static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200120{
121 int i;
122
123 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200124 if (node && !strcmp(node, sysinfo_to_dt[i].si_node) &&
125 !strcmp(si, sysinfo_to_dt[i].si_str))
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200126 return &sysinfo_to_dt[i];
127 }
128
129 return NULL;
130}
131
Simon Glass6f3332a2020-11-05 06:32:08 -0700132/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700133 * smbios_add_string() - add a string to the string area
134 *
135 * This adds a string to the string area which is appended directly after
136 * the formatted portion of an SMBIOS structure.
137 *
Simon Glassdff4e862021-02-04 21:17:18 -0700138 * @ctx: SMBIOS context
Bin Mengae5bedb2015-10-12 05:23:41 -0700139 * @str: string to add
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100140 * Return: string number in the string area. 0 if str is NULL.
Bin Mengae5bedb2015-10-12 05:23:41 -0700141 */
Simon Glassdff4e862021-02-04 21:17:18 -0700142static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Mengae5bedb2015-10-12 05:23:41 -0700143{
144 int i = 1;
Simon Glassdff4e862021-02-04 21:17:18 -0700145 char *p = ctx->eos;
146
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100147 if (!str)
148 return 0;
149
Bin Mengae5bedb2015-10-12 05:23:41 -0700150 for (;;) {
151 if (!*p) {
Simon Glassa05eb042021-02-04 21:17:20 -0700152 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700153 strcpy(p, str);
154 p += strlen(str);
155 *p++ = '\0';
Simon Glass721b4662021-02-04 21:17:19 -0700156 ctx->next_ptr = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700157 *p++ = '\0';
158
159 return i;
160 }
161
Simon Glassa05eb042021-02-04 21:17:20 -0700162 if (!strcmp(p, str)) {
163 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700164 return i;
Simon Glassa05eb042021-02-04 21:17:20 -0700165 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700166
167 p += strlen(p) + 1;
168 i++;
169 }
170}
171
172/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200173 * get_str_from_dt - Get a substring from a DT property.
174 * After finding the property in the DT, the function
175 * will parse comma-separated values and return the value.
176 * If nprop->max exceeds the number of comma-separated
177 * elements, the last non NULL value will be returned.
178 * Counting starts from zero.
179 *
180 * @nprop: sysinfo property to use
181 * @str: pointer to fill with data
182 * @size: str buffer length
183 */
184static
185void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
186{
187 const char *dt_str;
188 int cnt = 0;
189 char *token;
190
191 memset(str, 0, size);
192 if (!nprop || !nprop->max)
193 return;
194
195 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
196 if (!dt_str)
197 return;
198
199 memcpy(str, dt_str, size);
200 token = strtok(str, ",");
201 while (token && cnt < nprop->max) {
202 strlcpy(str, token, strlen(token) + 1);
203 token = strtok(NULL, ",");
204 cnt++;
205 }
206}
207
208/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700209 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
210 *
211 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700212 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700213 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700214 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100215 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200216 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200217 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700218 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700219static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200220 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700221{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200222 int ret;
223
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200224 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100225 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200226
227 if (!prop)
228 return smbios_add_string(ctx, dval);
229
Simon Glasscf67d6d2021-02-04 21:17:23 -0700230 if (sysinfo_id && ctx->dev) {
231 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700232
233 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
234 if (!ret)
235 return smbios_add_string(ctx, val);
236 }
Simon Glass95f47082020-11-05 06:32:18 -0700237 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200238 const char *str = NULL;
239 char str_dt[128] = { 0 };
240 /*
241 * If the node is not valid fallback and try the entire DT
242 * so we can at least fill in manufacturer and board type
243 */
244 if (ofnode_valid(ctx->node)) {
245 str = ofnode_read_string(ctx->node, prop);
246 } else {
247 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700248
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200249 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200250 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
251 str = (const char *)str_dt;
252 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200253
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200254 ret = smbios_add_string(ctx, str && *str ? str : dval);
255 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700256 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700257
258 return 0;
259}
260
Simon Glasscf67d6d2021-02-04 21:17:23 -0700261/**
262 * smbios_add_prop() - Add a property from the devicetree
263 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200264 * @prop: property to write. The default string will be written if
265 * prop is NULL
266 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200267 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700268 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200269static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
270 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700271{
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200272 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700273}
274
Simon Glassdff4e862021-02-04 21:17:18 -0700275static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
276{
277 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700278 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700279 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700280}
281
Simon Glassa05eb042021-02-04 21:17:20 -0700282int smbios_update_version(const char *version)
283{
284 char *ptr = gd->smbios_version;
285 uint old_len, len;
286
287 if (!ptr)
288 return log_ret(-ENOENT);
289
290 /*
291 * This string is supposed to have at least enough bytes and is
292 * padded with spaces. Update it, taking care not to move the
293 * \0 terminator, so that other strings in the string table
294 * are not disturbed. See smbios_add_string()
295 */
296 old_len = strnlen(ptr, SMBIOS_STR_MAX);
297 len = strnlen(version, SMBIOS_STR_MAX);
298 if (len > old_len)
299 return log_ret(-ENOSPC);
300
301 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
302 memcpy(ptr, version, len);
303#ifdef LOG_DEBUG
304 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
305#endif
306
307 return 0;
308}
309
Simon Glass6f3332a2020-11-05 06:32:08 -0700310/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700311 * smbios_string_table_len() - compute the string area size
312 *
313 * This computes the size of the string area including the string terminator.
314 *
Simon Glass721b4662021-02-04 21:17:19 -0700315 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200316 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700317 */
Simon Glass721b4662021-02-04 21:17:19 -0700318static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700319{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200320 /* In case no string is defined we have to return two \0 */
321 if (ctx->next_ptr == ctx->eos)
322 return 2;
323
Simon Glass721b4662021-02-04 21:17:19 -0700324 /* Allow for the final \0 after all strings */
325 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700326}
327
Simon Glass8e5ddb02021-02-04 21:17:17 -0700328static int smbios_write_type0(ulong *current, int handle,
329 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700330{
Simon Glass2d93add2018-11-22 13:46:37 -0700331 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700332 int len = sizeof(struct smbios_type0);
333
Simon Glass2d93add2018-11-22 13:46:37 -0700334 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700335 memset(t, 0, sizeof(struct smbios_type0));
336 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700337 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200338 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700339
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200340 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700341 if (t->bios_ver)
342 gd->smbios_version = ctx->last_str;
343 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
344 gd->smbios_version);
345#ifdef LOG_DEBUG
346 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
347 1, strlen(gd->smbios_version) + 1, 0);
348#endif
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200349 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200350#ifdef CONFIG_ROM_SIZE
Bin Mengae5bedb2015-10-12 05:23:41 -0700351 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Graf66f96e12016-08-19 01:23:29 +0200352#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700353 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
354 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
355 BIOS_CHARACTERISTICS_UPGRADEABLE;
356#ifdef CONFIG_GENERATE_ACPI_TABLE
357 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
358#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200359#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300360 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200361#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300362 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200363
Simon Glassd7d16822021-02-04 21:17:16 -0700364 /* bios_major_release has only one byte, so drop century */
365 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
366 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700367 t->ec_major_release = 0xff;
368 t->ec_minor_release = 0xff;
369
Simon Glass721b4662021-02-04 21:17:19 -0700370 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700371 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700372 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700373
374 return len;
375}
376
Simon Glass8e5ddb02021-02-04 21:17:17 -0700377static int smbios_write_type1(ulong *current, int handle,
378 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700379{
Simon Glass2d93add2018-11-22 13:46:37 -0700380 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700381 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600382 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700383
Simon Glass2d93add2018-11-22 13:46:37 -0700384 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700385 memset(t, 0, sizeof(struct smbios_type1));
386 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700387 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200388 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
389 SYSINFO_ID_SMBIOS_SYSTEM_MANUFACTURER,
390 NULL);
391 t->product_name = smbios_add_prop_si(ctx, "product",
392 SYSINFO_ID_SMBIOS_SYSTEM_PRODUCT,
393 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700394 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200395 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100396 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200397 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200398 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700399 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
400 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200401 t->serial_number = smbios_add_prop_si(ctx, "serial",
402 SYSINFO_ID_SMBIOS_SYSTEM_SERIAL,
403 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200404 }
Heinrich Schuchardta0501b52024-02-10 12:06:48 +0100405 t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN;
Michal Simeka7aa3952024-04-26 15:38:13 +0200406 t->sku_number = smbios_add_prop_si(ctx, "sku",
407 SYSINFO_ID_SMBIOS_SYSTEM_SKU, NULL);
408 t->family = smbios_add_prop_si(ctx, "family",
409 SYSINFO_ID_SMBIOS_SYSTEM_FAMILY, NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700410
Simon Glass721b4662021-02-04 21:17:19 -0700411 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700412 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700413 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700414
415 return len;
416}
417
Simon Glass8e5ddb02021-02-04 21:17:17 -0700418static int smbios_write_type2(ulong *current, int handle,
419 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700420{
Simon Glass2d93add2018-11-22 13:46:37 -0700421 struct smbios_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700422 int len = sizeof(struct smbios_type2);
423
Simon Glass2d93add2018-11-22 13:46:37 -0700424 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700425 memset(t, 0, sizeof(struct smbios_type2));
426 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700427 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200428 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
429 SYSINFO_ID_SMBIOS_BASEBOARD_MANUFACTURER,
430 NULL);
431 t->product_name = smbios_add_prop_si(ctx, "product",
432 SYSINFO_ID_SMBIOS_BASEBOARD_PRODUCT,
433 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700434 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200435 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100436 NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200437
438 t->serial_number = smbios_add_prop_si(ctx, "serial",
439 SYSINFO_ID_SMBIOS_BASEBOARD_SERIAL,
440 NULL);
441 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
442 SYSINFO_ID_SMBIOS_BASEBOARD_ASSET_TAG,
443 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700444 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
445 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100446 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700447
Simon Glass721b4662021-02-04 21:17:19 -0700448 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700449 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700450 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700451
452 return len;
453}
454
Simon Glass8e5ddb02021-02-04 21:17:17 -0700455static int smbios_write_type3(ulong *current, int handle,
456 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700457{
Simon Glass2d93add2018-11-22 13:46:37 -0700458 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700459 int len = sizeof(struct smbios_type3);
460
Simon Glass2d93add2018-11-22 13:46:37 -0700461 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700462 memset(t, 0, sizeof(struct smbios_type3));
463 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700464 smbios_set_eos(ctx, t->eos);
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100465 t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700466 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
467 t->bootup_state = SMBIOS_STATE_SAFE;
468 t->power_supply_state = SMBIOS_STATE_SAFE;
469 t->thermal_state = SMBIOS_STATE_SAFE;
470 t->security_status = SMBIOS_SECURITY_NONE;
471
Simon Glass721b4662021-02-04 21:17:19 -0700472 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700473 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700474 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700475
476 return len;
477}
478
Simon Glass8e5ddb02021-02-04 21:17:17 -0700479static void smbios_write_type4_dm(struct smbios_type4 *t,
480 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200481{
482 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100483 const char *vendor = NULL;
484 const char *name = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200485
486#ifdef CONFIG_CPU
487 char processor_name[49];
488 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700489 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200490
Simon Glass0dd7ca22020-11-05 06:32:07 -0700491 uclass_find_first_device(UCLASS_CPU, &cpu);
492 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700493 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200494
495 if (plat->family)
496 processor_family = plat->family;
497 t->processor_id[0] = plat->id[0];
498 t->processor_id[1] = plat->id[1];
499
Simon Glass0dd7ca22020-11-05 06:32:07 -0700500 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200501 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700502 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200503 name = processor_name;
504 }
505#endif
506
Heinrich Schuchardtb5f9aef2023-12-28 08:30:23 +0100507 t->processor_family = 0xfe;
508 t->processor_family2 = processor_family;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200509 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
510 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200511}
512
Simon Glass8e5ddb02021-02-04 21:17:17 -0700513static int smbios_write_type4(ulong *current, int handle,
514 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700515{
Simon Glass2d93add2018-11-22 13:46:37 -0700516 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700517 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700518
Simon Glass2d93add2018-11-22 13:46:37 -0700519 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700520 memset(t, 0, sizeof(struct smbios_type4));
521 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700522 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700523 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700524 smbios_write_type4_dm(t, ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700525 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
526 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
527 t->l1_cache_handle = 0xffff;
528 t->l2_cache_handle = 0xffff;
529 t->l3_cache_handle = 0xffff;
Bin Mengae5bedb2015-10-12 05:23:41 -0700530
Simon Glass721b4662021-02-04 21:17:19 -0700531 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700532 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700533 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700534
535 return len;
536}
537
Simon Glass8e5ddb02021-02-04 21:17:17 -0700538static int smbios_write_type32(ulong *current, int handle,
539 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700540{
Simon Glass2d93add2018-11-22 13:46:37 -0700541 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700542 int len = sizeof(struct smbios_type32);
543
Simon Glass2d93add2018-11-22 13:46:37 -0700544 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700545 memset(t, 0, sizeof(struct smbios_type32));
546 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700547 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700548
549 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700550 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700551
552 return len;
553}
554
Simon Glass8e5ddb02021-02-04 21:17:17 -0700555static int smbios_write_type127(ulong *current, int handle,
556 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700557{
Simon Glass2d93add2018-11-22 13:46:37 -0700558 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700559 int len = sizeof(struct smbios_type127);
560
Simon Glass2d93add2018-11-22 13:46:37 -0700561 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700562 memset(t, 0, sizeof(struct smbios_type127));
563 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
564
565 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700566 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700567
568 return len;
569}
570
Simon Glass6f3332a2020-11-05 06:32:08 -0700571static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700572 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700573 { smbios_write_type1, "system", },
574 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100575 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700576 { smbios_write_type3, "chassis", },
577 { smbios_write_type4, },
578 { smbios_write_type32, },
579 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700580};
581
Simon Glassca37a392017-01-16 07:03:35 -0700582ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700583{
Simon Glass6f3332a2020-11-05 06:32:08 -0700584 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700585 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700586 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700587 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700588 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700589 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700590 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700591 int i;
592
Simon Glass8e5ddb02021-02-04 21:17:17 -0700593 ctx.node = ofnode_null();
Quentin Schulz60b515d2024-06-10 18:13:46 +0200594 if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700595 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200596 if (ctx.dev) {
597 int ret;
598
Simon Glass8e5ddb02021-02-04 21:17:17 -0700599 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200600 ret = sysinfo_detect(ctx.dev);
Simon Glassbd5515d2024-06-23 14:30:33 -0600601
602 /*
603 * ignore the error since many boards don't implement
604 * this and we can still use the info in the devicetree
605 */
606 ret = log_msg_ret("sys", ret);
Michal Simek7d955002024-04-26 15:38:12 +0200607 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700608 } else {
609 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700610 }
611
Simon Glass3905e922023-12-31 08:25:45 -0700612 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700613
Simon Glassfc4b5522023-12-31 08:25:51 -0700614 /* move past the (so-far-unwritten) header to start writing structs */
615 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700616 tables = addr;
617
618 /* populate minimum required tables */
619 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700620 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700621
622 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200623 ctx.subnode_name = NULL;
624 if (method->subnode_name) {
625 ctx.subnode_name = method->subnode_name;
626 if (IS_ENABLED(CONFIG_OF_CONTROL))
627 ctx.node = ofnode_find_subnode(parent_node,
628 method->subnode_name);
629 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100630 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700631 }
632
Simon Glass2d93add2018-11-22 13:46:37 -0700633 /*
634 * We must use a pointer here so things work correctly on sandbox. The
635 * user of this table is not aware of the mapping of addresses to
636 * sandbox's DRAM buffer.
637 */
638 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700639
Simon Glassfc4b5522023-12-31 08:25:51 -0700640 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100641 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
642 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700643 memcpy(se->anchor, "_SM3_", 5);
644 se->length = sizeof(struct smbios3_entry);
645 se->major_ver = SMBIOS_MAJOR_VER;
646 se->minor_ver = SMBIOS_MINOR_VER;
647 se->doc_rev = 0;
648 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100649 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700650 se->struct_table_address = table_addr;
651 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
652 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700653
654 return addr;
655}