blob: fb6eaf1d5ca1a544295c0c1c756cb387c7584d1c [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 Glass0dd7ca22020-11-05 06:32:07 -07008#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06009#include <env.h>
Pali Rohárebe9fd82021-04-22 18:09:57 +020010#include <linux/stringify.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020011#include <linux/string.h>
Simon Glass2d93add2018-11-22 13:46:37 -070012#include <mapmem.h>
Alexander Graffb228082016-08-19 01:23:23 +020013#include <smbios.h>
Simon Glasscf67d6d2021-02-04 21:17:23 -070014#include <sysinfo.h>
Alexander Graffb228082016-08-19 01:23:23 +020015#include <tables_csum.h>
Bin Mengae5bedb2015-10-12 05:23:41 -070016#include <version.h>
Ilias Apalodimasada83d92023-12-07 11:18:50 +020017#include <malloc.h>
18#include <dm/ofnode.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020019#ifdef CONFIG_CPU
20#include <cpu.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020021#include <dm/uclass-internal.h>
22#endif
Bin Mengae5bedb2015-10-12 05:23:41 -070023
Pali Rohárebe9fd82021-04-22 18:09:57 +020024/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
25#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
26 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
27#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
28#endif
29
30/*
31 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
32 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
33 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
34 * BIOS Release Date should contain date when code was released
35 * and not when it was built or compiled.
36 */
37#if U_BOOT_VERSION_NUM_PATCH < 10
38#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
39#else
40#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
41#endif
42#define U_BOOT_DMI_DAY "01"
43#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
44#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
45
Simon Glassa05eb042021-02-04 21:17:20 -070046DECLARE_GLOBAL_DATA_PTR;
47
Bin Mengae5bedb2015-10-12 05:23:41 -070048/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +020049 * struct map_sysinfo - Mapping of sysinfo strings to DT
50 *
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020051 * @si_str: sysinfo string
Ilias Apalodimasada83d92023-12-07 11:18:50 +020052 * @dt_str: DT string
53 * @max: Max index of the tokenized string to pick. Counting starts from 0
54 *
55 */
56struct map_sysinfo {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020057 const char *si_node;
Ilias Apalodimas3adf8a62024-01-11 16:39:35 +020058 const char *si_str;
Ilias Apalodimasada83d92023-12-07 11:18:50 +020059 const char *dt_str;
60 int max;
61};
62
63static const struct map_sysinfo sysinfo_to_dt[] = {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020064 { .si_node = "system", .si_str = "product", .dt_str = "model", 2 },
65 { .si_node = "system", .si_str = "manufacturer", .dt_str = "compatible", 1 },
66 { .si_node = "baseboard", .si_str = "product", .dt_str = "model", 2 },
67 { .si_node = "baseboard", .si_str = "manufacturer", .dt_str = "compatible", 1 },
Ilias Apalodimasada83d92023-12-07 11:18:50 +020068};
69
70/**
Simon Glass8e5ddb02021-02-04 21:17:17 -070071 * struct smbios_ctx - context for writing SMBIOS tables
72 *
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020073 * @node: node containing the information to write (ofnode_null()
74 * if none)
75 * @dev: sysinfo device to use (NULL if none)
76 * @subnode_name: sysinfo subnode_name. Used for DT fallback
77 * @eos: end-of-string pointer for the table being processed.
78 * This is set up when we start processing a table
79 * @next_ptr: pointer to the start of the next string to be added.
80 * When the table is not empty, this points to the byte
81 * after the \0 of the previous string.
82 * @last_str: points to the last string that was written to the table,
83 * or NULL if none
Simon Glass8e5ddb02021-02-04 21:17:17 -070084 */
85struct smbios_ctx {
86 ofnode node;
87 struct udevice *dev;
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +020088 const char *subnode_name;
Simon Glassdff4e862021-02-04 21:17:18 -070089 char *eos;
Simon Glass721b4662021-02-04 21:17:19 -070090 char *next_ptr;
Simon Glassa05eb042021-02-04 21:17:20 -070091 char *last_str;
Simon Glass8e5ddb02021-02-04 21:17:17 -070092};
93
94/**
Simon Glass099d3ae2021-02-04 21:17:14 -070095 * Function prototype to write a specific type of SMBIOS structure
96 *
97 * @addr: start address to write the structure
98 * @handle: the structure's handle, a unique 16-bit number
Simon Glass8e5ddb02021-02-04 21:17:17 -070099 * @ctx: context for writing the tables
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200100 * Return: size of the structure
Simon Glass099d3ae2021-02-04 21:17:14 -0700101 */
Simon Glass8e5ddb02021-02-04 21:17:17 -0700102typedef int (*smbios_write_type)(ulong *addr, int handle,
103 struct smbios_ctx *ctx);
Simon Glass099d3ae2021-02-04 21:17:14 -0700104
105/**
Simon Glass6f3332a2020-11-05 06:32:08 -0700106 * struct smbios_write_method - Information about a table-writing function
107 *
108 * @write: Function to call
109 * @subnode_name: Name of subnode which has the information for this function,
110 * NULL if none
111 */
112struct smbios_write_method {
113 smbios_write_type write;
114 const char *subnode_name;
115};
116
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200117static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200118{
119 int i;
120
121 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200122 if (node && !strcmp(node, sysinfo_to_dt[i].si_node) &&
123 !strcmp(si, sysinfo_to_dt[i].si_str))
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200124 return &sysinfo_to_dt[i];
125 }
126
127 return NULL;
128}
129
Simon Glass6f3332a2020-11-05 06:32:08 -0700130/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700131 * smbios_add_string() - add a string to the string area
132 *
133 * This adds a string to the string area which is appended directly after
134 * the formatted portion of an SMBIOS structure.
135 *
Simon Glassdff4e862021-02-04 21:17:18 -0700136 * @ctx: SMBIOS context
Bin Mengae5bedb2015-10-12 05:23:41 -0700137 * @str: string to add
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100138 * Return: string number in the string area. 0 if str is NULL.
Bin Mengae5bedb2015-10-12 05:23:41 -0700139 */
Simon Glassdff4e862021-02-04 21:17:18 -0700140static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Mengae5bedb2015-10-12 05:23:41 -0700141{
142 int i = 1;
Simon Glassdff4e862021-02-04 21:17:18 -0700143 char *p = ctx->eos;
144
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100145 if (!str)
146 return 0;
147
Bin Mengae5bedb2015-10-12 05:23:41 -0700148 for (;;) {
149 if (!*p) {
Simon Glassa05eb042021-02-04 21:17:20 -0700150 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700151 strcpy(p, str);
152 p += strlen(str);
153 *p++ = '\0';
Simon Glass721b4662021-02-04 21:17:19 -0700154 ctx->next_ptr = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700155 *p++ = '\0';
156
157 return i;
158 }
159
Simon Glassa05eb042021-02-04 21:17:20 -0700160 if (!strcmp(p, str)) {
161 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700162 return i;
Simon Glassa05eb042021-02-04 21:17:20 -0700163 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700164
165 p += strlen(p) + 1;
166 i++;
167 }
168}
169
170/**
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200171 * get_str_from_dt - Get a substring from a DT property.
172 * After finding the property in the DT, the function
173 * will parse comma-separated values and return the value.
174 * If nprop->max exceeds the number of comma-separated
175 * elements, the last non NULL value will be returned.
176 * Counting starts from zero.
177 *
178 * @nprop: sysinfo property to use
179 * @str: pointer to fill with data
180 * @size: str buffer length
181 */
182static
183void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
184{
185 const char *dt_str;
186 int cnt = 0;
187 char *token;
188
189 memset(str, 0, size);
190 if (!nprop || !nprop->max)
191 return;
192
193 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
194 if (!dt_str)
195 return;
196
197 memcpy(str, dt_str, size);
198 token = strtok(str, ",");
199 while (token && cnt < nprop->max) {
200 strlcpy(str, token, strlen(token) + 1);
201 token = strtok(NULL, ",");
202 cnt++;
203 }
204}
205
206/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700207 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
208 *
209 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700210 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700211 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700212 * @prop: property to write
Heinrich Schuchardtda0428c2024-01-29 13:58:18 +0100213 * @sysinfo_id: unique identifier for the string value to be read
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200214 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200215 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700216 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700217static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200218 int sysinfo_id, const char *dval)
Simon Glass6f3332a2020-11-05 06:32:08 -0700219{
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200220 int ret;
221
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200222 if (!dval || !*dval)
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100223 dval = NULL;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200224
225 if (!prop)
226 return smbios_add_string(ctx, dval);
227
Simon Glasscf67d6d2021-02-04 21:17:23 -0700228 if (sysinfo_id && ctx->dev) {
229 char val[SMBIOS_STR_MAX];
Simon Glasscf67d6d2021-02-04 21:17:23 -0700230
231 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
232 if (!ret)
233 return smbios_add_string(ctx, val);
234 }
Simon Glass95f47082020-11-05 06:32:18 -0700235 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200236 const char *str = NULL;
237 char str_dt[128] = { 0 };
238 /*
239 * If the node is not valid fallback and try the entire DT
240 * so we can at least fill in manufacturer and board type
241 */
242 if (ofnode_valid(ctx->node)) {
243 str = ofnode_read_string(ctx->node, prop);
244 } else {
245 const struct map_sysinfo *nprop;
Simon Glass95f47082020-11-05 06:32:18 -0700246
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200247 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200248 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
249 str = (const char *)str_dt;
250 }
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200251
Ilias Apalodimasada83d92023-12-07 11:18:50 +0200252 ret = smbios_add_string(ctx, str && *str ? str : dval);
253 return ret;
Simon Glass95f47082020-11-05 06:32:18 -0700254 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700255
256 return 0;
257}
258
Simon Glasscf67d6d2021-02-04 21:17:23 -0700259/**
260 * smbios_add_prop() - Add a property from the devicetree
261 *
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200262 * @prop: property to write. The default string will be written if
263 * prop is NULL
264 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200265 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700266 */
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200267static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
268 const char *dval)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700269{
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200270 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700271}
272
Simon Glassdff4e862021-02-04 21:17:18 -0700273static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
274{
275 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700276 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700277 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700278}
279
Simon Glassa05eb042021-02-04 21:17:20 -0700280int smbios_update_version(const char *version)
281{
282 char *ptr = gd->smbios_version;
283 uint old_len, len;
284
285 if (!ptr)
286 return log_ret(-ENOENT);
287
288 /*
289 * This string is supposed to have at least enough bytes and is
290 * padded with spaces. Update it, taking care not to move the
291 * \0 terminator, so that other strings in the string table
292 * are not disturbed. See smbios_add_string()
293 */
294 old_len = strnlen(ptr, SMBIOS_STR_MAX);
295 len = strnlen(version, SMBIOS_STR_MAX);
296 if (len > old_len)
297 return log_ret(-ENOSPC);
298
299 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
300 memcpy(ptr, version, len);
301#ifdef LOG_DEBUG
302 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
303#endif
304
305 return 0;
306}
307
Simon Glass6f3332a2020-11-05 06:32:08 -0700308/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700309 * smbios_string_table_len() - compute the string area size
310 *
311 * This computes the size of the string area including the string terminator.
312 *
Simon Glass721b4662021-02-04 21:17:19 -0700313 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200314 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700315 */
Simon Glass721b4662021-02-04 21:17:19 -0700316static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700317{
Matthias Bruggere01fa9f2021-04-06 11:04:35 +0200318 /* In case no string is defined we have to return two \0 */
319 if (ctx->next_ptr == ctx->eos)
320 return 2;
321
Simon Glass721b4662021-02-04 21:17:19 -0700322 /* Allow for the final \0 after all strings */
323 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700324}
325
Simon Glass8e5ddb02021-02-04 21:17:17 -0700326static int smbios_write_type0(ulong *current, int handle,
327 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700328{
Simon Glass2d93add2018-11-22 13:46:37 -0700329 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700330 int len = sizeof(struct smbios_type0);
331
Simon Glass2d93add2018-11-22 13:46:37 -0700332 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700333 memset(t, 0, sizeof(struct smbios_type0));
334 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700335 smbios_set_eos(ctx, t->eos);
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200336 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700337
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200338 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glassa05eb042021-02-04 21:17:20 -0700339 if (t->bios_ver)
340 gd->smbios_version = ctx->last_str;
341 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
342 gd->smbios_version);
343#ifdef LOG_DEBUG
344 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
345 1, strlen(gd->smbios_version) + 1, 0);
346#endif
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200347 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200348#ifdef CONFIG_ROM_SIZE
Bin Mengae5bedb2015-10-12 05:23:41 -0700349 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Graf66f96e12016-08-19 01:23:29 +0200350#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700351 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
352 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
353 BIOS_CHARACTERISTICS_UPGRADEABLE;
354#ifdef CONFIG_GENERATE_ACPI_TABLE
355 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
356#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200357#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300358 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200359#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300360 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200361
Simon Glassd7d16822021-02-04 21:17:16 -0700362 /* bios_major_release has only one byte, so drop century */
363 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
364 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700365 t->ec_major_release = 0xff;
366 t->ec_minor_release = 0xff;
367
Simon Glass721b4662021-02-04 21:17:19 -0700368 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700369 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700370 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700371
372 return len;
373}
374
Simon Glass8e5ddb02021-02-04 21:17:17 -0700375static int smbios_write_type1(ulong *current, int handle,
376 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700377{
Simon Glass2d93add2018-11-22 13:46:37 -0700378 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700379 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600380 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700381
Simon Glass2d93add2018-11-22 13:46:37 -0700382 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700383 memset(t, 0, sizeof(struct smbios_type1));
384 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700385 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200386 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
387 SYSINFO_ID_SMBIOS_SYSTEM_MANUFACTURER,
388 NULL);
389 t->product_name = smbios_add_prop_si(ctx, "product",
390 SYSINFO_ID_SMBIOS_SYSTEM_PRODUCT,
391 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700392 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200393 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100394 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200395 if (serial_str) {
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200396 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700397 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
398 } else {
Michal Simeka7aa3952024-04-26 15:38:13 +0200399 t->serial_number = smbios_add_prop_si(ctx, "serial",
400 SYSINFO_ID_SMBIOS_SYSTEM_SERIAL,
401 NULL);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200402 }
Heinrich Schuchardta0501b52024-02-10 12:06:48 +0100403 t->wakeup_type = SMBIOS_WAKEUP_TYPE_UNKNOWN;
Michal Simeka7aa3952024-04-26 15:38:13 +0200404 t->sku_number = smbios_add_prop_si(ctx, "sku",
405 SYSINFO_ID_SMBIOS_SYSTEM_SKU, NULL);
406 t->family = smbios_add_prop_si(ctx, "family",
407 SYSINFO_ID_SMBIOS_SYSTEM_FAMILY, NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700408
Simon Glass721b4662021-02-04 21:17:19 -0700409 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700410 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700411 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700412
413 return len;
414}
415
Simon Glass8e5ddb02021-02-04 21:17:17 -0700416static int smbios_write_type2(ulong *current, int handle,
417 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700418{
Simon Glass2d93add2018-11-22 13:46:37 -0700419 struct smbios_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700420 int len = sizeof(struct smbios_type2);
421
Simon Glass2d93add2018-11-22 13:46:37 -0700422 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700423 memset(t, 0, sizeof(struct smbios_type2));
424 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700425 smbios_set_eos(ctx, t->eos);
Michal Simeka7aa3952024-04-26 15:38:13 +0200426 t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
427 SYSINFO_ID_SMBIOS_BASEBOARD_MANUFACTURER,
428 NULL);
429 t->product_name = smbios_add_prop_si(ctx, "product",
430 SYSINFO_ID_SMBIOS_BASEBOARD_PRODUCT,
431 NULL);
Simon Glasscf67d6d2021-02-04 21:17:23 -0700432 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200433 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100434 NULL);
Michal Simeka7aa3952024-04-26 15:38:13 +0200435
436 t->serial_number = smbios_add_prop_si(ctx, "serial",
437 SYSINFO_ID_SMBIOS_BASEBOARD_SERIAL,
438 NULL);
439 t->asset_tag_number = smbios_add_prop_si(ctx, "asset-tag",
440 SYSINFO_ID_SMBIOS_BASEBOARD_ASSET_TAG,
441 NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700442 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
443 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100444 t->chassis_handle = handle + 1;
Bin Mengae5bedb2015-10-12 05:23:41 -0700445
Simon Glass721b4662021-02-04 21:17:19 -0700446 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700447 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700448 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700449
450 return len;
451}
452
Simon Glass8e5ddb02021-02-04 21:17:17 -0700453static int smbios_write_type3(ulong *current, int handle,
454 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700455{
Simon Glass2d93add2018-11-22 13:46:37 -0700456 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700457 int len = sizeof(struct smbios_type3);
458
Simon Glass2d93add2018-11-22 13:46:37 -0700459 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700460 memset(t, 0, sizeof(struct smbios_type3));
461 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700462 smbios_set_eos(ctx, t->eos);
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100463 t->manufacturer = smbios_add_prop(ctx, "manufacturer", NULL);
Bin Mengae5bedb2015-10-12 05:23:41 -0700464 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
465 t->bootup_state = SMBIOS_STATE_SAFE;
466 t->power_supply_state = SMBIOS_STATE_SAFE;
467 t->thermal_state = SMBIOS_STATE_SAFE;
468 t->security_status = SMBIOS_SECURITY_NONE;
469
Simon Glass721b4662021-02-04 21:17:19 -0700470 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700471 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700472 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700473
474 return len;
475}
476
Simon Glass8e5ddb02021-02-04 21:17:17 -0700477static void smbios_write_type4_dm(struct smbios_type4 *t,
478 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200479{
480 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
Heinrich Schuchardte952b822024-01-29 14:09:36 +0100481 const char *vendor = NULL;
482 const char *name = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200483
484#ifdef CONFIG_CPU
485 char processor_name[49];
486 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700487 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200488
Simon Glass0dd7ca22020-11-05 06:32:07 -0700489 uclass_find_first_device(UCLASS_CPU, &cpu);
490 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700491 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200492
493 if (plat->family)
494 processor_family = plat->family;
495 t->processor_id[0] = plat->id[0];
496 t->processor_id[1] = plat->id[1];
497
Simon Glass0dd7ca22020-11-05 06:32:07 -0700498 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200499 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700500 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200501 name = processor_name;
502 }
503#endif
504
Heinrich Schuchardtb5f9aef2023-12-28 08:30:23 +0100505 t->processor_family = 0xfe;
506 t->processor_family2 = processor_family;
Ilias Apalodimas07f3eba2023-12-07 11:18:49 +0200507 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
508 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200509}
510
Simon Glass8e5ddb02021-02-04 21:17:17 -0700511static int smbios_write_type4(ulong *current, int handle,
512 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700513{
Simon Glass2d93add2018-11-22 13:46:37 -0700514 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700515 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700516
Simon Glass2d93add2018-11-22 13:46:37 -0700517 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700518 memset(t, 0, sizeof(struct smbios_type4));
519 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700520 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700521 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700522 smbios_write_type4_dm(t, ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700523 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
524 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
525 t->l1_cache_handle = 0xffff;
526 t->l2_cache_handle = 0xffff;
527 t->l3_cache_handle = 0xffff;
Bin Mengae5bedb2015-10-12 05:23:41 -0700528
Simon Glass721b4662021-02-04 21:17:19 -0700529 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700530 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700531 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700532
533 return len;
534}
535
Simon Glass8e5ddb02021-02-04 21:17:17 -0700536static int smbios_write_type32(ulong *current, int handle,
537 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700538{
Simon Glass2d93add2018-11-22 13:46:37 -0700539 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700540 int len = sizeof(struct smbios_type32);
541
Simon Glass2d93add2018-11-22 13:46:37 -0700542 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700543 memset(t, 0, sizeof(struct smbios_type32));
544 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700545 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700546
547 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700548 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700549
550 return len;
551}
552
Simon Glass8e5ddb02021-02-04 21:17:17 -0700553static int smbios_write_type127(ulong *current, int handle,
554 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700555{
Simon Glass2d93add2018-11-22 13:46:37 -0700556 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700557 int len = sizeof(struct smbios_type127);
558
Simon Glass2d93add2018-11-22 13:46:37 -0700559 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700560 memset(t, 0, sizeof(struct smbios_type127));
561 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
562
563 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700564 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700565
566 return len;
567}
568
Simon Glass6f3332a2020-11-05 06:32:08 -0700569static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700570 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700571 { smbios_write_type1, "system", },
572 { smbios_write_type2, "baseboard", },
Heinrich Schuchardtab8f4c82024-01-29 18:32:58 +0100573 /* Type 3 must immediately follow type 2 due to chassis handle. */
Simon Glass6f3332a2020-11-05 06:32:08 -0700574 { smbios_write_type3, "chassis", },
575 { smbios_write_type4, },
576 { smbios_write_type32, },
577 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700578};
579
Simon Glassca37a392017-01-16 07:03:35 -0700580ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700581{
Simon Glass6f3332a2020-11-05 06:32:08 -0700582 ofnode parent_node = ofnode_null();
Simon Glass3905e922023-12-31 08:25:45 -0700583 ulong table_addr, start_addr;
Simon Glassfc4b5522023-12-31 08:25:51 -0700584 struct smbios3_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700585 struct smbios_ctx ctx;
Simon Glassca37a392017-01-16 07:03:35 -0700586 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700587 int len = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700588 int handle = 0;
Bin Mengae5bedb2015-10-12 05:23:41 -0700589 int i;
590
Simon Glass8e5ddb02021-02-04 21:17:17 -0700591 ctx.node = ofnode_null();
Quentin Schulz60b515d2024-06-10 18:13:46 +0200592 if (IS_ENABLED(CONFIG_OF_CONTROL) && CONFIG_IS_ENABLED(SYSINFO)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700593 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
Michal Simek7d955002024-04-26 15:38:12 +0200594 if (ctx.dev) {
595 int ret;
596
Simon Glass8e5ddb02021-02-04 21:17:17 -0700597 parent_node = dev_read_subnode(ctx.dev, "smbios");
Michal Simek7d955002024-04-26 15:38:12 +0200598 ret = sysinfo_detect(ctx.dev);
599 if (ret)
600 return ret;
601 }
Simon Glass8e5ddb02021-02-04 21:17:17 -0700602 } else {
603 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700604 }
605
Simon Glass3905e922023-12-31 08:25:45 -0700606 start_addr = addr;
Bin Mengae5bedb2015-10-12 05:23:41 -0700607
Simon Glassfc4b5522023-12-31 08:25:51 -0700608 /* move past the (so-far-unwritten) header to start writing structs */
609 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Mengae5bedb2015-10-12 05:23:41 -0700610 tables = addr;
611
612 /* populate minimum required tables */
613 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700614 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700615
616 method = &smbios_write_funcs[i];
Ilias Apalodimas5ae2d5c2024-01-11 16:39:36 +0200617 ctx.subnode_name = NULL;
618 if (method->subnode_name) {
619 ctx.subnode_name = method->subnode_name;
620 if (IS_ENABLED(CONFIG_OF_CONTROL))
621 ctx.node = ofnode_find_subnode(parent_node,
622 method->subnode_name);
623 }
Heinrich Schuchardt91f875a2024-01-31 23:42:35 +0100624 len += method->write((ulong *)&addr, handle++, &ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700625 }
626
Simon Glass2d93add2018-11-22 13:46:37 -0700627 /*
628 * We must use a pointer here so things work correctly on sandbox. The
629 * user of this table is not aware of the mapping of addresses to
630 * sandbox's DRAM buffer.
631 */
632 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glass2d93add2018-11-22 13:46:37 -0700633
Simon Glassfc4b5522023-12-31 08:25:51 -0700634 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardt494d0722024-01-11 07:34:08 +0100635 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
636 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glassfc4b5522023-12-31 08:25:51 -0700637 memcpy(se->anchor, "_SM3_", 5);
638 se->length = sizeof(struct smbios3_entry);
639 se->major_ver = SMBIOS_MAJOR_VER;
640 se->minor_ver = SMBIOS_MINOR_VER;
641 se->doc_rev = 0;
642 se->entry_point_rev = 1;
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +0100643 se->table_maximum_size = len;
Simon Glassfc4b5522023-12-31 08:25:51 -0700644 se->struct_table_address = table_addr;
645 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
646 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700647
648 return addr;
649}