blob: 7bd9805fec0353067d040372d7f434844bcede03 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng721e9922015-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 Meng721e9922015-10-12 05:23:41 -07006 */
7
Simon Glass78227d42020-11-05 06:32:07 -07008#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -06009#include <env.h>
Pali Rohár11275e42021-04-22 18:09:57 +020010#include <linux/stringify.h>
Ilias Apalodimas738b3462023-12-07 11:18:50 +020011#include <linux/string.h>
Simon Glassa2505fc2018-11-22 13:46:37 -070012#include <mapmem.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020013#include <smbios.h>
Simon Glass07c9e682021-02-04 21:17:23 -070014#include <sysinfo.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020015#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070016#include <version.h>
Ilias Apalodimas738b3462023-12-07 11:18:50 +020017#include <malloc.h>
18#include <dm/ofnode.h>
Alexander Graf96476202016-08-19 01:23:28 +020019#ifdef CONFIG_CPU
20#include <cpu.h>
Alexander Graf96476202016-08-19 01:23:28 +020021#include <dm/uclass-internal.h>
22#endif
Bin Meng721e9922015-10-12 05:23:41 -070023
Pali Rohár11275e42021-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 Glasse9adaa72021-02-04 21:17:20 -070046DECLARE_GLOBAL_DATA_PTR;
47
Bin Meng721e9922015-10-12 05:23:41 -070048/**
Ilias Apalodimas738b3462023-12-07 11:18:50 +020049 * struct map_sysinfo - Mapping of sysinfo strings to DT
50 *
Ilias Apalodimas5e2b4722024-01-11 16:39:35 +020051 * @si_str: sysinfo string
Ilias Apalodimas738b3462023-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 Apalodimasb6488ca2024-01-11 16:39:36 +020057 const char *si_node;
Ilias Apalodimas5e2b4722024-01-11 16:39:35 +020058 const char *si_str;
Ilias Apalodimas738b3462023-12-07 11:18:50 +020059 const char *dt_str;
60 int max;
61};
62
63static const struct map_sysinfo sysinfo_to_dt[] = {
Ilias Apalodimasb6488ca2024-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 Apalodimas738b3462023-12-07 11:18:50 +020068};
69
70/**
Simon Glass1e8989a2021-02-04 21:17:17 -070071 * struct smbios_ctx - context for writing SMBIOS tables
72 *
Ilias Apalodimasb6488ca2024-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 Glass1e8989a2021-02-04 21:17:17 -070084 */
85struct smbios_ctx {
86 ofnode node;
87 struct udevice *dev;
Ilias Apalodimasb6488ca2024-01-11 16:39:36 +020088 const char *subnode_name;
Simon Glass0c95fff2021-02-04 21:17:18 -070089 char *eos;
Simon Glassfd3b8262021-02-04 21:17:19 -070090 char *next_ptr;
Simon Glasse9adaa72021-02-04 21:17:20 -070091 char *last_str;
Simon Glass1e8989a2021-02-04 21:17:17 -070092};
93
94/**
Simon Glass0e89b852021-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 Glass1e8989a2021-02-04 21:17:17 -070099 * @ctx: context for writing the tables
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200100 * Return: size of the structure
Simon Glass0e89b852021-02-04 21:17:14 -0700101 */
Simon Glass1e8989a2021-02-04 21:17:17 -0700102typedef int (*smbios_write_type)(ulong *addr, int handle,
103 struct smbios_ctx *ctx);
Simon Glass0e89b852021-02-04 21:17:14 -0700104
105/**
Simon Glass44ffb6f2020-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 Apalodimasb6488ca2024-01-11 16:39:36 +0200117static const struct map_sysinfo *convert_sysinfo_to_dt(const char *node, const char *si)
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200118{
119 int i;
120
121 for (i = 0; i < ARRAY_SIZE(sysinfo_to_dt); i++) {
Ilias Apalodimasb6488ca2024-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 Apalodimas738b3462023-12-07 11:18:50 +0200124 return &sysinfo_to_dt[i];
125 }
126
127 return NULL;
128}
129
Simon Glass44ffb6f2020-11-05 06:32:08 -0700130/**
Bin Meng721e9922015-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 Glass0c95fff2021-02-04 21:17:18 -0700136 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -0700137 * @str: string to add
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200138 * Return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -0700139 */
Simon Glass0c95fff2021-02-04 21:17:18 -0700140static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Meng721e9922015-10-12 05:23:41 -0700141{
142 int i = 1;
Simon Glass0c95fff2021-02-04 21:17:18 -0700143 char *p = ctx->eos;
144
Bin Meng721e9922015-10-12 05:23:41 -0700145 for (;;) {
146 if (!*p) {
Simon Glasse9adaa72021-02-04 21:17:20 -0700147 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -0700148 strcpy(p, str);
149 p += strlen(str);
150 *p++ = '\0';
Simon Glassfd3b8262021-02-04 21:17:19 -0700151 ctx->next_ptr = p;
Bin Meng721e9922015-10-12 05:23:41 -0700152 *p++ = '\0';
153
154 return i;
155 }
156
Simon Glasse9adaa72021-02-04 21:17:20 -0700157 if (!strcmp(p, str)) {
158 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -0700159 return i;
Simon Glasse9adaa72021-02-04 21:17:20 -0700160 }
Bin Meng721e9922015-10-12 05:23:41 -0700161
162 p += strlen(p) + 1;
163 i++;
164 }
165}
166
167/**
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200168 * get_str_from_dt - Get a substring from a DT property.
169 * After finding the property in the DT, the function
170 * will parse comma-separated values and return the value.
171 * If nprop->max exceeds the number of comma-separated
172 * elements, the last non NULL value will be returned.
173 * Counting starts from zero.
174 *
175 * @nprop: sysinfo property to use
176 * @str: pointer to fill with data
177 * @size: str buffer length
178 */
179static
180void get_str_from_dt(const struct map_sysinfo *nprop, char *str, size_t size)
181{
182 const char *dt_str;
183 int cnt = 0;
184 char *token;
185
186 memset(str, 0, size);
187 if (!nprop || !nprop->max)
188 return;
189
190 dt_str = ofnode_read_string(ofnode_root(), nprop->dt_str);
191 if (!dt_str)
192 return;
193
194 memcpy(str, dt_str, size);
195 token = strtok(str, ",");
196 while (token && cnt < nprop->max) {
197 strlcpy(str, token, strlen(token) + 1);
198 token = strtok(NULL, ",");
199 cnt++;
200 }
201}
202
203/**
Simon Glass07c9e682021-02-04 21:17:23 -0700204 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
205 *
206 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass44ffb6f2020-11-05 06:32:08 -0700207 *
Simon Glass1e8989a2021-02-04 21:17:17 -0700208 * @ctx: context for writing the tables
Simon Glass44ffb6f2020-11-05 06:32:08 -0700209 * @prop: property to write
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200210 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200211 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700212 */
Simon Glass07c9e682021-02-04 21:17:23 -0700213static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200214 int sysinfo_id, const char *dval)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700215{
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200216 int ret;
217
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200218 if (!dval || !*dval)
219 dval = "Unknown";
220
221 if (!prop)
222 return smbios_add_string(ctx, dval);
223
Simon Glass07c9e682021-02-04 21:17:23 -0700224 if (sysinfo_id && ctx->dev) {
225 char val[SMBIOS_STR_MAX];
Simon Glass07c9e682021-02-04 21:17:23 -0700226
227 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
228 if (!ret)
229 return smbios_add_string(ctx, val);
230 }
Simon Glasse4f8e542020-11-05 06:32:18 -0700231 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200232 const char *str = NULL;
233 char str_dt[128] = { 0 };
234 /*
235 * If the node is not valid fallback and try the entire DT
236 * so we can at least fill in manufacturer and board type
237 */
238 if (ofnode_valid(ctx->node)) {
239 str = ofnode_read_string(ctx->node, prop);
240 } else {
241 const struct map_sysinfo *nprop;
Simon Glasse4f8e542020-11-05 06:32:18 -0700242
Ilias Apalodimasb6488ca2024-01-11 16:39:36 +0200243 nprop = convert_sysinfo_to_dt(ctx->subnode_name, prop);
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200244 get_str_from_dt(nprop, str_dt, sizeof(str_dt));
245 str = (const char *)str_dt;
246 }
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200247
Ilias Apalodimas738b3462023-12-07 11:18:50 +0200248 ret = smbios_add_string(ctx, str && *str ? str : dval);
249 return ret;
Simon Glasse4f8e542020-11-05 06:32:18 -0700250 }
251
252 return 0;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700253}
254
Simon Glass07c9e682021-02-04 21:17:23 -0700255/**
256 * smbios_add_prop() - Add a property from the devicetree
257 *
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200258 * @prop: property to write. The default string will be written if
259 * prop is NULL
260 * @dval: Default value to use if the string is not found or is empty
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200261 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass07c9e682021-02-04 21:17:23 -0700262 */
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200263static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop,
264 const char *dval)
Simon Glass07c9e682021-02-04 21:17:23 -0700265{
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200266 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE, dval);
Simon Glass07c9e682021-02-04 21:17:23 -0700267}
268
Simon Glass0c95fff2021-02-04 21:17:18 -0700269static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
270{
271 ctx->eos = eos;
Simon Glassfd3b8262021-02-04 21:17:19 -0700272 ctx->next_ptr = eos;
Simon Glasse9adaa72021-02-04 21:17:20 -0700273 ctx->last_str = NULL;
274}
275
276int smbios_update_version(const char *version)
277{
278 char *ptr = gd->smbios_version;
279 uint old_len, len;
280
281 if (!ptr)
282 return log_ret(-ENOENT);
283
284 /*
285 * This string is supposed to have at least enough bytes and is
286 * padded with spaces. Update it, taking care not to move the
287 * \0 terminator, so that other strings in the string table
288 * are not disturbed. See smbios_add_string()
289 */
290 old_len = strnlen(ptr, SMBIOS_STR_MAX);
291 len = strnlen(version, SMBIOS_STR_MAX);
292 if (len > old_len)
293 return log_ret(-ENOSPC);
294
295 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
296 memcpy(ptr, version, len);
297#ifdef LOG_DEBUG
298 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
299#endif
300
301 return 0;
Simon Glass0c95fff2021-02-04 21:17:18 -0700302}
303
Simon Glass44ffb6f2020-11-05 06:32:08 -0700304/**
Bin Meng721e9922015-10-12 05:23:41 -0700305 * smbios_string_table_len() - compute the string area size
306 *
307 * This computes the size of the string area including the string terminator.
308 *
Simon Glassfd3b8262021-02-04 21:17:19 -0700309 * @ctx: SMBIOS context
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200310 * Return: string area size
Bin Meng721e9922015-10-12 05:23:41 -0700311 */
Simon Glassfd3b8262021-02-04 21:17:19 -0700312static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700313{
Simon Glassfd3b8262021-02-04 21:17:19 -0700314 /* Allow for the final \0 after all strings */
315 return (ctx->next_ptr + 1) - ctx->eos;
Bin Meng721e9922015-10-12 05:23:41 -0700316}
317
Simon Glass1e8989a2021-02-04 21:17:17 -0700318static int smbios_write_type0(ulong *current, int handle,
319 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700320{
Simon Glassa2505fc2018-11-22 13:46:37 -0700321 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700322 int len = sizeof(struct smbios_type0);
323
Simon Glassa2505fc2018-11-22 13:46:37 -0700324 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700325 memset(t, 0, sizeof(struct smbios_type0));
326 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700327 smbios_set_eos(ctx, t->eos);
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200328 t->vendor = smbios_add_prop(ctx, NULL, "U-Boot");
Simon Glasse9adaa72021-02-04 21:17:20 -0700329
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200330 t->bios_ver = smbios_add_prop(ctx, "version", PLAIN_VERSION);
Simon Glasse9adaa72021-02-04 21:17:20 -0700331 if (t->bios_ver)
332 gd->smbios_version = ctx->last_str;
333 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
334 gd->smbios_version);
335#ifdef LOG_DEBUG
336 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
337 1, strlen(gd->smbios_version) + 1, 0);
338#endif
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200339 t->bios_release_date = smbios_add_prop(ctx, NULL, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +0200340#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -0700341 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +0200342#endif
Bin Meng721e9922015-10-12 05:23:41 -0700343 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
344 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
345 BIOS_CHARACTERISTICS_UPGRADEABLE;
346#ifdef CONFIG_GENERATE_ACPI_TABLE
347 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
348#endif
Alexander Grafe663b352016-08-19 01:23:29 +0200349#ifdef CONFIG_EFI_LOADER
Ilias Apalodimasff192302021-06-09 18:14:47 +0300350 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Grafe663b352016-08-19 01:23:29 +0200351#endif
Ilias Apalodimasff192302021-06-09 18:14:47 +0300352 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200353
Simon Glass7617f992021-02-04 21:17:16 -0700354 /* bios_major_release has only one byte, so drop century */
355 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
356 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Meng721e9922015-10-12 05:23:41 -0700357 t->ec_major_release = 0xff;
358 t->ec_minor_release = 0xff;
359
Simon Glassfd3b8262021-02-04 21:17:19 -0700360 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700361 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700362 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700363
364 return len;
365}
366
Simon Glass1e8989a2021-02-04 21:17:17 -0700367static int smbios_write_type1(ulong *current, int handle,
368 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700369{
Simon Glassa2505fc2018-11-22 13:46:37 -0700370 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700371 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600372 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700373
Simon Glassa2505fc2018-11-22 13:46:37 -0700374 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700375 memset(t, 0, sizeof(struct smbios_type1));
376 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700377 smbios_set_eos(ctx, t->eos);
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200378 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
379 t->product_name = smbios_add_prop(ctx, "product", "Unknown");
Simon Glass07c9e682021-02-04 21:17:23 -0700380 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200381 SYSINFO_ID_SMBIOS_SYSTEM_VERSION,
382 "Unknown");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200383 if (serial_str) {
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200384 t->serial_number = smbios_add_prop(ctx, NULL, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700385 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
386 } else {
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200387 t->serial_number = smbios_add_prop(ctx, "serial", "Unknown");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200388 }
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200389 t->sku_number = smbios_add_prop(ctx, "sku", "Unknown");
390 t->family = smbios_add_prop(ctx, "family", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700391
Simon Glassfd3b8262021-02-04 21:17:19 -0700392 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700393 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700394 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700395
396 return len;
397}
398
Simon Glass1e8989a2021-02-04 21:17:17 -0700399static int smbios_write_type2(ulong *current, int handle,
400 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700401{
Simon Glassa2505fc2018-11-22 13:46:37 -0700402 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700403 int len = sizeof(struct smbios_type2);
404
Simon Glassa2505fc2018-11-22 13:46:37 -0700405 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700406 memset(t, 0, sizeof(struct smbios_type2));
407 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700408 smbios_set_eos(ctx, t->eos);
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200409 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
410 t->product_name = smbios_add_prop(ctx, "product", "Unknown");
Simon Glass07c9e682021-02-04 21:17:23 -0700411 t->version = smbios_add_prop_si(ctx, "version",
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200412 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION,
413 "Unknown");
414 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700415 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
416 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
417
Simon Glassfd3b8262021-02-04 21:17:19 -0700418 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700419 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700420 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700421
422 return len;
423}
424
Simon Glass1e8989a2021-02-04 21:17:17 -0700425static int smbios_write_type3(ulong *current, int handle,
426 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700427{
Simon Glassa2505fc2018-11-22 13:46:37 -0700428 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700429 int len = sizeof(struct smbios_type3);
430
Simon Glassa2505fc2018-11-22 13:46:37 -0700431 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700432 memset(t, 0, sizeof(struct smbios_type3));
433 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700434 smbios_set_eos(ctx, t->eos);
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200435 t->manufacturer = smbios_add_prop(ctx, "manufacturer", "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700436 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
437 t->bootup_state = SMBIOS_STATE_SAFE;
438 t->power_supply_state = SMBIOS_STATE_SAFE;
439 t->thermal_state = SMBIOS_STATE_SAFE;
440 t->security_status = SMBIOS_SECURITY_NONE;
441
Simon Glassfd3b8262021-02-04 21:17:19 -0700442 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700443 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700444 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700445
446 return len;
447}
448
Simon Glass1e8989a2021-02-04 21:17:17 -0700449static void smbios_write_type4_dm(struct smbios_type4 *t,
450 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200451{
452 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
453 const char *vendor = "Unknown";
454 const char *name = "Unknown";
455
456#ifdef CONFIG_CPU
457 char processor_name[49];
458 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700459 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200460
Simon Glass78227d42020-11-05 06:32:07 -0700461 uclass_find_first_device(UCLASS_CPU, &cpu);
462 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700463 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200464
465 if (plat->family)
466 processor_family = plat->family;
467 t->processor_id[0] = plat->id[0];
468 t->processor_id[1] = plat->id[1];
469
Simon Glass78227d42020-11-05 06:32:07 -0700470 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200471 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700472 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200473 name = processor_name;
474 }
475#endif
476
Heinrich Schuchardt0920bd52023-12-28 08:30:23 +0100477 t->processor_family = 0xfe;
478 t->processor_family2 = processor_family;
Ilias Apalodimasa986cce2023-12-07 11:18:49 +0200479 t->processor_manufacturer = smbios_add_prop(ctx, NULL, vendor);
480 t->processor_version = smbios_add_prop(ctx, NULL, name);
Alexander Graf96476202016-08-19 01:23:28 +0200481}
482
Simon Glass1e8989a2021-02-04 21:17:17 -0700483static int smbios_write_type4(ulong *current, int handle,
484 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700485{
Simon Glassa2505fc2018-11-22 13:46:37 -0700486 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700487 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700488
Simon Glassa2505fc2018-11-22 13:46:37 -0700489 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700490 memset(t, 0, sizeof(struct smbios_type4));
491 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700492 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700493 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700494 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700495 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
496 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
497 t->l1_cache_handle = 0xffff;
498 t->l2_cache_handle = 0xffff;
499 t->l3_cache_handle = 0xffff;
Bin Meng721e9922015-10-12 05:23:41 -0700500
Simon Glassfd3b8262021-02-04 21:17:19 -0700501 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700502 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700503 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700504
505 return len;
506}
507
Simon Glass1e8989a2021-02-04 21:17:17 -0700508static int smbios_write_type32(ulong *current, int handle,
509 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700510{
Simon Glassa2505fc2018-11-22 13:46:37 -0700511 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700512 int len = sizeof(struct smbios_type32);
513
Simon Glassa2505fc2018-11-22 13:46:37 -0700514 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700515 memset(t, 0, sizeof(struct smbios_type32));
516 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700517 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700518
519 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700520 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700521
522 return len;
523}
524
Simon Glass1e8989a2021-02-04 21:17:17 -0700525static int smbios_write_type127(ulong *current, int handle,
526 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700527{
Simon Glassa2505fc2018-11-22 13:46:37 -0700528 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700529 int len = sizeof(struct smbios_type127);
530
Simon Glassa2505fc2018-11-22 13:46:37 -0700531 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700532 memset(t, 0, sizeof(struct smbios_type127));
533 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
534
535 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700536 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700537
538 return len;
539}
540
Simon Glass44ffb6f2020-11-05 06:32:08 -0700541static struct smbios_write_method smbios_write_funcs[] = {
Simon Glasse9adaa72021-02-04 21:17:20 -0700542 { smbios_write_type0, "bios", },
Simon Glass44ffb6f2020-11-05 06:32:08 -0700543 { smbios_write_type1, "system", },
544 { smbios_write_type2, "baseboard", },
545 { smbios_write_type3, "chassis", },
546 { smbios_write_type4, },
547 { smbios_write_type32, },
548 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700549};
550
Simon Glass42fd8c12017-01-16 07:03:35 -0700551ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700552{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700553 ofnode parent_node = ofnode_null();
Simon Glassf19cf8d2023-12-31 08:25:45 -0700554 ulong table_addr, start_addr;
Simon Glass1c5f6fa2023-12-31 08:25:51 -0700555 struct smbios3_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700556 struct smbios_ctx ctx;
Simon Glass42fd8c12017-01-16 07:03:35 -0700557 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700558 int len = 0;
559 int max_struct_size = 0;
560 int handle = 0;
Bin Meng721e9922015-10-12 05:23:41 -0700561 int i;
562
Simon Glass1e8989a2021-02-04 21:17:17 -0700563 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700564 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700565 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
566 if (ctx.dev)
567 parent_node = dev_read_subnode(ctx.dev, "smbios");
568 } else {
569 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700570 }
571
Simon Glassf19cf8d2023-12-31 08:25:45 -0700572 start_addr = addr;
Bin Meng721e9922015-10-12 05:23:41 -0700573
Simon Glass1c5f6fa2023-12-31 08:25:51 -0700574 /* move past the (so-far-unwritten) header to start writing structs */
575 addr = ALIGN(addr + sizeof(struct smbios3_entry), 16);
Bin Meng721e9922015-10-12 05:23:41 -0700576 tables = addr;
577
578 /* populate minimum required tables */
579 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700580 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700581 int tmp;
582
583 method = &smbios_write_funcs[i];
Ilias Apalodimasb6488ca2024-01-11 16:39:36 +0200584 ctx.subnode_name = NULL;
585 if (method->subnode_name) {
586 ctx.subnode_name = method->subnode_name;
587 if (IS_ENABLED(CONFIG_OF_CONTROL))
588 ctx.node = ofnode_find_subnode(parent_node,
589 method->subnode_name);
590 }
Simon Glass1e8989a2021-02-04 21:17:17 -0700591 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200592
Bin Meng721e9922015-10-12 05:23:41 -0700593 max_struct_size = max(max_struct_size, tmp);
594 len += tmp;
595 }
596
Simon Glassa2505fc2018-11-22 13:46:37 -0700597 /*
598 * We must use a pointer here so things work correctly on sandbox. The
599 * user of this table is not aware of the mapping of addresses to
600 * sandbox's DRAM buffer.
601 */
602 table_addr = (ulong)map_sysmem(tables, 0);
Simon Glassf19cf8d2023-12-31 08:25:45 -0700603
Simon Glass1c5f6fa2023-12-31 08:25:51 -0700604 /* now go back and write the SMBIOS3 header */
Heinrich Schuchardtccefbf32024-01-11 07:34:08 +0100605 se = map_sysmem(start_addr, sizeof(struct smbios3_entry));
606 memset(se, '\0', sizeof(struct smbios3_entry));
Simon Glass1c5f6fa2023-12-31 08:25:51 -0700607 memcpy(se->anchor, "_SM3_", 5);
608 se->length = sizeof(struct smbios3_entry);
609 se->major_ver = SMBIOS_MAJOR_VER;
610 se->minor_ver = SMBIOS_MINOR_VER;
611 se->doc_rev = 0;
612 se->entry_point_rev = 1;
613 se->max_struct_size = len;
614 se->struct_table_address = table_addr;
615 se->checksum = table_compute_checksum(se, sizeof(struct smbios3_entry));
616 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700617
618 return addr;
619}