blob: d7f4999e8b2aee21c745b97a7383a7d16f16078e [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
8#include <common.h>
Simon Glass0dd7ca22020-11-05 06:32:07 -07009#include <dm.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Pali Rohárebe9fd82021-04-22 18:09:57 +020011#include <linux/stringify.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>
Alexander Grafe44b3c02016-08-19 01:23:28 +020017#ifdef CONFIG_CPU
18#include <cpu.h>
Alexander Grafe44b3c02016-08-19 01:23:28 +020019#include <dm/uclass-internal.h>
20#endif
Bin Mengae5bedb2015-10-12 05:23:41 -070021
Pali Rohárebe9fd82021-04-22 18:09:57 +020022/* Safeguard for checking that U_BOOT_VERSION_NUM macros are compatible with U_BOOT_DMI */
23#if U_BOOT_VERSION_NUM < 2000 || U_BOOT_VERSION_NUM > 2099 || \
24 U_BOOT_VERSION_NUM_PATCH < 1 || U_BOOT_VERSION_NUM_PATCH > 12
25#error U_BOOT_VERSION_NUM macros are not compatible with DMI, fix U_BOOT_DMI macros
26#endif
27
28/*
29 * U_BOOT_DMI_DATE contains BIOS Release Date in format mm/dd/yyyy.
30 * BIOS Release Date is calculated from U-Boot version and fixed day 01.
31 * So for U-Boot version 2021.04 it is calculated as "04/01/2021".
32 * BIOS Release Date should contain date when code was released
33 * and not when it was built or compiled.
34 */
35#if U_BOOT_VERSION_NUM_PATCH < 10
36#define U_BOOT_DMI_MONTH "0" __stringify(U_BOOT_VERSION_NUM_PATCH)
37#else
38#define U_BOOT_DMI_MONTH __stringify(U_BOOT_VERSION_NUM_PATCH)
39#endif
40#define U_BOOT_DMI_DAY "01"
41#define U_BOOT_DMI_YEAR __stringify(U_BOOT_VERSION_NUM)
42#define U_BOOT_DMI_DATE U_BOOT_DMI_MONTH "/" U_BOOT_DMI_DAY "/" U_BOOT_DMI_YEAR
43
Simon Glassa05eb042021-02-04 21:17:20 -070044DECLARE_GLOBAL_DATA_PTR;
45
Bin Mengae5bedb2015-10-12 05:23:41 -070046/**
Simon Glass8e5ddb02021-02-04 21:17:17 -070047 * struct smbios_ctx - context for writing SMBIOS tables
48 *
49 * @node: node containing the information to write (ofnode_null() if none)
50 * @dev: sysinfo device to use (NULL if none)
Simon Glassdff4e862021-02-04 21:17:18 -070051 * @eos: end-of-string pointer for the table being processed. This is set
52 * up when we start processing a table
Simon Glass721b4662021-02-04 21:17:19 -070053 * @next_ptr: pointer to the start of the next string to be added. When the
54 * table is nopt empty, this points to the byte after the \0 of the
55 * previous string.
Simon Glassa05eb042021-02-04 21:17:20 -070056 * @last_str: points to the last string that was written to the table, or NULL
57 * if none
Simon Glass8e5ddb02021-02-04 21:17:17 -070058 */
59struct smbios_ctx {
60 ofnode node;
61 struct udevice *dev;
Simon Glassdff4e862021-02-04 21:17:18 -070062 char *eos;
Simon Glass721b4662021-02-04 21:17:19 -070063 char *next_ptr;
Simon Glassa05eb042021-02-04 21:17:20 -070064 char *last_str;
Simon Glass8e5ddb02021-02-04 21:17:17 -070065};
66
67/**
Simon Glass099d3ae2021-02-04 21:17:14 -070068 * Function prototype to write a specific type of SMBIOS structure
69 *
70 * @addr: start address to write the structure
71 * @handle: the structure's handle, a unique 16-bit number
Simon Glass8e5ddb02021-02-04 21:17:17 -070072 * @ctx: context for writing the tables
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +020073 * Return: size of the structure
Simon Glass099d3ae2021-02-04 21:17:14 -070074 */
Simon Glass8e5ddb02021-02-04 21:17:17 -070075typedef int (*smbios_write_type)(ulong *addr, int handle,
76 struct smbios_ctx *ctx);
Simon Glass099d3ae2021-02-04 21:17:14 -070077
78/**
Simon Glass6f3332a2020-11-05 06:32:08 -070079 * struct smbios_write_method - Information about a table-writing function
80 *
81 * @write: Function to call
82 * @subnode_name: Name of subnode which has the information for this function,
83 * NULL if none
84 */
85struct smbios_write_method {
86 smbios_write_type write;
87 const char *subnode_name;
88};
89
90/**
Bin Mengae5bedb2015-10-12 05:23:41 -070091 * smbios_add_string() - add a string to the string area
92 *
93 * This adds a string to the string area which is appended directly after
94 * the formatted portion of an SMBIOS structure.
95 *
Simon Glassdff4e862021-02-04 21:17:18 -070096 * @ctx: SMBIOS context
Bin Mengae5bedb2015-10-12 05:23:41 -070097 * @str: string to add
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +020098 * Return: string number in the string area (1 or more)
Bin Mengae5bedb2015-10-12 05:23:41 -070099 */
Simon Glassdff4e862021-02-04 21:17:18 -0700100static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Mengae5bedb2015-10-12 05:23:41 -0700101{
102 int i = 1;
Simon Glassdff4e862021-02-04 21:17:18 -0700103 char *p = ctx->eos;
104
Heinrich Schuchardt20dcde42020-06-01 15:44:00 +0200105 if (!*str)
106 str = "Unknown";
Bin Mengae5bedb2015-10-12 05:23:41 -0700107
108 for (;;) {
109 if (!*p) {
Simon Glassa05eb042021-02-04 21:17:20 -0700110 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700111 strcpy(p, str);
112 p += strlen(str);
113 *p++ = '\0';
Simon Glass721b4662021-02-04 21:17:19 -0700114 ctx->next_ptr = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700115 *p++ = '\0';
116
117 return i;
118 }
119
Simon Glassa05eb042021-02-04 21:17:20 -0700120 if (!strcmp(p, str)) {
121 ctx->last_str = p;
Bin Mengae5bedb2015-10-12 05:23:41 -0700122 return i;
Simon Glassa05eb042021-02-04 21:17:20 -0700123 }
Bin Mengae5bedb2015-10-12 05:23:41 -0700124
125 p += strlen(p) + 1;
126 i++;
127 }
128}
129
130/**
Simon Glasscf67d6d2021-02-04 21:17:23 -0700131 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
132 *
133 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass6f3332a2020-11-05 06:32:08 -0700134 *
Simon Glass8e5ddb02021-02-04 21:17:17 -0700135 * @ctx: context for writing the tables
Simon Glass6f3332a2020-11-05 06:32:08 -0700136 * @prop: property to write
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200137 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass6f3332a2020-11-05 06:32:08 -0700138 */
Simon Glasscf67d6d2021-02-04 21:17:23 -0700139static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
140 int sysinfo_id)
Simon Glass6f3332a2020-11-05 06:32:08 -0700141{
Simon Glasscf67d6d2021-02-04 21:17:23 -0700142 if (sysinfo_id && ctx->dev) {
143 char val[SMBIOS_STR_MAX];
144 int ret;
145
146 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
147 if (!ret)
148 return smbios_add_string(ctx, val);
149 }
Simon Glass95f47082020-11-05 06:32:18 -0700150 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
151 const char *str;
152
Simon Glass8e5ddb02021-02-04 21:17:17 -0700153 str = ofnode_read_string(ctx->node, prop);
Simon Glass95f47082020-11-05 06:32:18 -0700154 if (str)
Simon Glassdff4e862021-02-04 21:17:18 -0700155 return smbios_add_string(ctx, str);
Simon Glass95f47082020-11-05 06:32:18 -0700156 }
Simon Glass6f3332a2020-11-05 06:32:08 -0700157
158 return 0;
159}
160
Simon Glasscf67d6d2021-02-04 21:17:23 -0700161/**
162 * smbios_add_prop() - Add a property from the devicetree
163 *
164 * @prop: property to write
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200165 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glasscf67d6d2021-02-04 21:17:23 -0700166 */
167static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
168{
169 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE);
170}
171
Simon Glassdff4e862021-02-04 21:17:18 -0700172static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
173{
174 ctx->eos = eos;
Simon Glass721b4662021-02-04 21:17:19 -0700175 ctx->next_ptr = eos;
Simon Glassa05eb042021-02-04 21:17:20 -0700176 ctx->last_str = NULL;
Simon Glassdff4e862021-02-04 21:17:18 -0700177}
178
Simon Glassa05eb042021-02-04 21:17:20 -0700179int smbios_update_version(const char *version)
180{
181 char *ptr = gd->smbios_version;
182 uint old_len, len;
183
184 if (!ptr)
185 return log_ret(-ENOENT);
186
187 /*
188 * This string is supposed to have at least enough bytes and is
189 * padded with spaces. Update it, taking care not to move the
190 * \0 terminator, so that other strings in the string table
191 * are not disturbed. See smbios_add_string()
192 */
193 old_len = strnlen(ptr, SMBIOS_STR_MAX);
194 len = strnlen(version, SMBIOS_STR_MAX);
195 if (len > old_len)
196 return log_ret(-ENOSPC);
197
198 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
199 memcpy(ptr, version, len);
200#ifdef LOG_DEBUG
201 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
202#endif
203
204 return 0;
205}
206
Simon Glass6f3332a2020-11-05 06:32:08 -0700207/**
Bin Mengae5bedb2015-10-12 05:23:41 -0700208 * smbios_string_table_len() - compute the string area size
209 *
210 * This computes the size of the string area including the string terminator.
211 *
Simon Glass721b4662021-02-04 21:17:19 -0700212 * @ctx: SMBIOS context
Heinrich Schuchardt9809bcc2021-06-10 12:13:52 +0200213 * Return: string area size
Bin Mengae5bedb2015-10-12 05:23:41 -0700214 */
Simon Glass721b4662021-02-04 21:17:19 -0700215static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700216{
Simon Glass721b4662021-02-04 21:17:19 -0700217 /* Allow for the final \0 after all strings */
218 return (ctx->next_ptr + 1) - ctx->eos;
Bin Mengae5bedb2015-10-12 05:23:41 -0700219}
220
Simon Glass8e5ddb02021-02-04 21:17:17 -0700221static int smbios_write_type0(ulong *current, int handle,
222 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700223{
Simon Glass2d93add2018-11-22 13:46:37 -0700224 struct smbios_type0 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700225 int len = sizeof(struct smbios_type0);
226
Simon Glass2d93add2018-11-22 13:46:37 -0700227 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700228 memset(t, 0, sizeof(struct smbios_type0));
229 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700230 smbios_set_eos(ctx, t->eos);
231 t->vendor = smbios_add_string(ctx, "U-Boot");
Simon Glassa05eb042021-02-04 21:17:20 -0700232
233 t->bios_ver = smbios_add_prop(ctx, "version");
234 if (!t->bios_ver)
235 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
236 if (t->bios_ver)
237 gd->smbios_version = ctx->last_str;
238 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
239 gd->smbios_version);
240#ifdef LOG_DEBUG
241 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
242 1, strlen(gd->smbios_version) + 1, 0);
243#endif
Simon Glassdff4e862021-02-04 21:17:18 -0700244 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
Alexander Graf66f96e12016-08-19 01:23:29 +0200245#ifdef CONFIG_ROM_SIZE
Bin Mengae5bedb2015-10-12 05:23:41 -0700246 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Graf66f96e12016-08-19 01:23:29 +0200247#endif
Bin Mengae5bedb2015-10-12 05:23:41 -0700248 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
249 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
250 BIOS_CHARACTERISTICS_UPGRADEABLE;
251#ifdef CONFIG_GENERATE_ACPI_TABLE
252 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
253#endif
Alexander Graf66f96e12016-08-19 01:23:29 +0200254#ifdef CONFIG_EFI_LOADER
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300255 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Graf66f96e12016-08-19 01:23:29 +0200256#endif
Ilias Apalodimas15578bc2021-06-09 18:14:47 +0300257 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Graf66f96e12016-08-19 01:23:29 +0200258
Simon Glassd7d16822021-02-04 21:17:16 -0700259 /* bios_major_release has only one byte, so drop century */
260 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
261 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Mengae5bedb2015-10-12 05:23:41 -0700262 t->ec_major_release = 0xff;
263 t->ec_minor_release = 0xff;
264
Simon Glass721b4662021-02-04 21:17:19 -0700265 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700266 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700267 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700268
269 return len;
270}
271
Simon Glass8e5ddb02021-02-04 21:17:17 -0700272static int smbios_write_type1(ulong *current, int handle,
273 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700274{
Simon Glass2d93add2018-11-22 13:46:37 -0700275 struct smbios_type1 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700276 int len = sizeof(struct smbios_type1);
Simon Glass64b723f2017-08-03 12:22:12 -0600277 char *serial_str = env_get("serial#");
Bin Mengae5bedb2015-10-12 05:23:41 -0700278
Simon Glass2d93add2018-11-22 13:46:37 -0700279 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700280 memset(t, 0, sizeof(struct smbios_type1));
281 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700282 smbios_set_eos(ctx, t->eos);
283 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas07e1a132021-06-10 12:33:15 +0300284 if (!t->manufacturer)
285 t->manufacturer = smbios_add_string(ctx, "Unknown");
Simon Glassdff4e862021-02-04 21:17:18 -0700286 t->product_name = smbios_add_prop(ctx, "product");
Ilias Apalodimas07e1a132021-06-10 12:33:15 +0300287 if (!t->product_name)
288 t->product_name = smbios_add_string(ctx, "Unknown Product");
Simon Glasscf67d6d2021-02-04 21:17:23 -0700289 t->version = smbios_add_prop_si(ctx, "version",
290 SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200291 if (serial_str) {
Simon Glassdff4e862021-02-04 21:17:18 -0700292 t->serial_number = smbios_add_string(ctx, serial_str);
Simon Glass6f3332a2020-11-05 06:32:08 -0700293 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
294 } else {
Simon Glassdff4e862021-02-04 21:17:18 -0700295 t->serial_number = smbios_add_prop(ctx, "serial");
Alexander Graf9a3e3e22016-08-19 01:23:31 +0200296 }
Simon Glassdff4e862021-02-04 21:17:18 -0700297 t->sku_number = smbios_add_prop(ctx, "sku");
298 t->family = smbios_add_prop(ctx, "family");
Bin Mengae5bedb2015-10-12 05:23:41 -0700299
Simon Glass721b4662021-02-04 21:17:19 -0700300 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700301 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700302 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700303
304 return len;
305}
306
Simon Glass8e5ddb02021-02-04 21:17:17 -0700307static int smbios_write_type2(ulong *current, int handle,
308 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700309{
Simon Glass2d93add2018-11-22 13:46:37 -0700310 struct smbios_type2 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700311 int len = sizeof(struct smbios_type2);
312
Simon Glass2d93add2018-11-22 13:46:37 -0700313 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700314 memset(t, 0, sizeof(struct smbios_type2));
315 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700316 smbios_set_eos(ctx, t->eos);
317 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas07e1a132021-06-10 12:33:15 +0300318 if (!t->manufacturer)
319 t->manufacturer = smbios_add_string(ctx, "Unknown");
Simon Glassdff4e862021-02-04 21:17:18 -0700320 t->product_name = smbios_add_prop(ctx, "product");
Ilias Apalodimas07e1a132021-06-10 12:33:15 +0300321 if (!t->product_name)
322 t->product_name = smbios_add_string(ctx, "Unknown Product");
Simon Glasscf67d6d2021-02-04 21:17:23 -0700323 t->version = smbios_add_prop_si(ctx, "version",
324 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
Simon Glassdff4e862021-02-04 21:17:18 -0700325 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
Bin Mengae5bedb2015-10-12 05:23:41 -0700326 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
327 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
328
Simon Glass721b4662021-02-04 21:17:19 -0700329 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700330 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700331 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700332
333 return len;
334}
335
Simon Glass8e5ddb02021-02-04 21:17:17 -0700336static int smbios_write_type3(ulong *current, int handle,
337 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700338{
Simon Glass2d93add2018-11-22 13:46:37 -0700339 struct smbios_type3 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700340 int len = sizeof(struct smbios_type3);
341
Simon Glass2d93add2018-11-22 13:46:37 -0700342 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700343 memset(t, 0, sizeof(struct smbios_type3));
344 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700345 smbios_set_eos(ctx, t->eos);
346 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas07e1a132021-06-10 12:33:15 +0300347 if (!t->manufacturer)
348 t->manufacturer = smbios_add_string(ctx, "Unknown");
Bin Mengae5bedb2015-10-12 05:23:41 -0700349 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
350 t->bootup_state = SMBIOS_STATE_SAFE;
351 t->power_supply_state = SMBIOS_STATE_SAFE;
352 t->thermal_state = SMBIOS_STATE_SAFE;
353 t->security_status = SMBIOS_SECURITY_NONE;
354
Simon Glass721b4662021-02-04 21:17:19 -0700355 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700356 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700357 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700358
359 return len;
360}
361
Simon Glass8e5ddb02021-02-04 21:17:17 -0700362static void smbios_write_type4_dm(struct smbios_type4 *t,
363 struct smbios_ctx *ctx)
Alexander Grafe44b3c02016-08-19 01:23:28 +0200364{
365 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
366 const char *vendor = "Unknown";
367 const char *name = "Unknown";
368
369#ifdef CONFIG_CPU
370 char processor_name[49];
371 char vendor_name[49];
Simon Glass0dd7ca22020-11-05 06:32:07 -0700372 struct udevice *cpu = NULL;
Alexander Grafe44b3c02016-08-19 01:23:28 +0200373
Simon Glass0dd7ca22020-11-05 06:32:07 -0700374 uclass_find_first_device(UCLASS_CPU, &cpu);
375 if (cpu) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700376 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200377
378 if (plat->family)
379 processor_family = plat->family;
380 t->processor_id[0] = plat->id[0];
381 t->processor_id[1] = plat->id[1];
382
Simon Glass0dd7ca22020-11-05 06:32:07 -0700383 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200384 vendor = vendor_name;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700385 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Grafe44b3c02016-08-19 01:23:28 +0200386 name = processor_name;
387 }
388#endif
389
390 t->processor_family = processor_family;
Simon Glassdff4e862021-02-04 21:17:18 -0700391 t->processor_manufacturer = smbios_add_string(ctx, vendor);
392 t->processor_version = smbios_add_string(ctx, name);
Alexander Grafe44b3c02016-08-19 01:23:28 +0200393}
394
Simon Glass8e5ddb02021-02-04 21:17:17 -0700395static int smbios_write_type4(ulong *current, int handle,
396 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700397{
Simon Glass2d93add2018-11-22 13:46:37 -0700398 struct smbios_type4 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700399 int len = sizeof(struct smbios_type4);
Bin Mengae5bedb2015-10-12 05:23:41 -0700400
Simon Glass2d93add2018-11-22 13:46:37 -0700401 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700402 memset(t, 0, sizeof(struct smbios_type4));
403 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700404 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700405 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700406 smbios_write_type4_dm(t, ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700407 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
408 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
409 t->l1_cache_handle = 0xffff;
410 t->l2_cache_handle = 0xffff;
411 t->l3_cache_handle = 0xffff;
412 t->processor_family2 = t->processor_family;
413
Simon Glass721b4662021-02-04 21:17:19 -0700414 len = t->length + smbios_string_table_len(ctx);
Bin Mengae5bedb2015-10-12 05:23:41 -0700415 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700416 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700417
418 return len;
419}
420
Simon Glass8e5ddb02021-02-04 21:17:17 -0700421static int smbios_write_type32(ulong *current, int handle,
422 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700423{
Simon Glass2d93add2018-11-22 13:46:37 -0700424 struct smbios_type32 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700425 int len = sizeof(struct smbios_type32);
426
Simon Glass2d93add2018-11-22 13:46:37 -0700427 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700428 memset(t, 0, sizeof(struct smbios_type32));
429 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glassdff4e862021-02-04 21:17:18 -0700430 smbios_set_eos(ctx, t->eos);
Bin Mengae5bedb2015-10-12 05:23:41 -0700431
432 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700433 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700434
435 return len;
436}
437
Simon Glass8e5ddb02021-02-04 21:17:17 -0700438static int smbios_write_type127(ulong *current, int handle,
439 struct smbios_ctx *ctx)
Bin Mengae5bedb2015-10-12 05:23:41 -0700440{
Simon Glass2d93add2018-11-22 13:46:37 -0700441 struct smbios_type127 *t;
Bin Mengae5bedb2015-10-12 05:23:41 -0700442 int len = sizeof(struct smbios_type127);
443
Simon Glass2d93add2018-11-22 13:46:37 -0700444 t = map_sysmem(*current, len);
Bin Mengae5bedb2015-10-12 05:23:41 -0700445 memset(t, 0, sizeof(struct smbios_type127));
446 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
447
448 *current += len;
Simon Glass2d93add2018-11-22 13:46:37 -0700449 unmap_sysmem(t);
Bin Mengae5bedb2015-10-12 05:23:41 -0700450
451 return len;
452}
453
Simon Glass6f3332a2020-11-05 06:32:08 -0700454static struct smbios_write_method smbios_write_funcs[] = {
Simon Glassa05eb042021-02-04 21:17:20 -0700455 { smbios_write_type0, "bios", },
Simon Glass6f3332a2020-11-05 06:32:08 -0700456 { smbios_write_type1, "system", },
457 { smbios_write_type2, "baseboard", },
458 { smbios_write_type3, "chassis", },
459 { smbios_write_type4, },
460 { smbios_write_type32, },
461 { smbios_write_type127 },
Bin Mengae5bedb2015-10-12 05:23:41 -0700462};
463
Simon Glassca37a392017-01-16 07:03:35 -0700464ulong write_smbios_table(ulong addr)
Bin Mengae5bedb2015-10-12 05:23:41 -0700465{
Simon Glass6f3332a2020-11-05 06:32:08 -0700466 ofnode parent_node = ofnode_null();
Bin Mengae5bedb2015-10-12 05:23:41 -0700467 struct smbios_entry *se;
Simon Glass8e5ddb02021-02-04 21:17:17 -0700468 struct smbios_ctx ctx;
Simon Glass2d93add2018-11-22 13:46:37 -0700469 ulong table_addr;
Simon Glassca37a392017-01-16 07:03:35 -0700470 ulong tables;
Bin Mengae5bedb2015-10-12 05:23:41 -0700471 int len = 0;
472 int max_struct_size = 0;
473 int handle = 0;
474 char *istart;
475 int isize;
476 int i;
477
Simon Glass8e5ddb02021-02-04 21:17:17 -0700478 ctx.node = ofnode_null();
Simon Glass0dd7ca22020-11-05 06:32:07 -0700479 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass8e5ddb02021-02-04 21:17:17 -0700480 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
481 if (ctx.dev)
482 parent_node = dev_read_subnode(ctx.dev, "smbios");
483 } else {
484 ctx.dev = NULL;
Simon Glass0dd7ca22020-11-05 06:32:07 -0700485 }
486
Bin Mengae5bedb2015-10-12 05:23:41 -0700487 /* 16 byte align the table address */
488 addr = ALIGN(addr, 16);
489
Simon Glass2d93add2018-11-22 13:46:37 -0700490 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Mengae5bedb2015-10-12 05:23:41 -0700491 memset(se, 0, sizeof(struct smbios_entry));
492
493 addr += sizeof(struct smbios_entry);
494 addr = ALIGN(addr, 16);
495 tables = addr;
496
497 /* populate minimum required tables */
498 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass6f3332a2020-11-05 06:32:08 -0700499 const struct smbios_write_method *method;
Simon Glass6f3332a2020-11-05 06:32:08 -0700500 int tmp;
501
502 method = &smbios_write_funcs[i];
503 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass8e5ddb02021-02-04 21:17:17 -0700504 ctx.node = ofnode_find_subnode(parent_node,
505 method->subnode_name);
506 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner5566caf2018-07-30 13:22:07 +0200507
Bin Mengae5bedb2015-10-12 05:23:41 -0700508 max_struct_size = max(max_struct_size, tmp);
509 len += tmp;
510 }
511
512 memcpy(se->anchor, "_SM_", 4);
513 se->length = sizeof(struct smbios_entry);
514 se->major_ver = SMBIOS_MAJOR_VER;
515 se->minor_ver = SMBIOS_MINOR_VER;
516 se->max_struct_size = max_struct_size;
517 memcpy(se->intermediate_anchor, "_DMI_", 5);
518 se->struct_table_length = len;
Simon Glass2d93add2018-11-22 13:46:37 -0700519
520 /*
521 * We must use a pointer here so things work correctly on sandbox. The
522 * user of this table is not aware of the mapping of addresses to
523 * sandbox's DRAM buffer.
524 */
525 table_addr = (ulong)map_sysmem(tables, 0);
526 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
527 /*
528 * We need to put this >32-bit pointer into the table but the
529 * field is only 32 bits wide.
530 */
531 printf("WARNING: SMBIOS table_address overflow %llx\n",
532 (unsigned long long)table_addr);
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200533 addr = 0;
534 goto out;
Simon Glass2d93add2018-11-22 13:46:37 -0700535 }
536 se->struct_table_address = table_addr;
537
Bin Mengae5bedb2015-10-12 05:23:41 -0700538 se->struct_count = handle;
539
540 /* calculate checksums */
541 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
542 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
543 se->intermediate_checksum = table_compute_checksum(istart, isize);
544 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Heinrich Schuchardt21da91f2021-05-15 18:07:47 +0200545out:
Simon Glass2d93add2018-11-22 13:46:37 -0700546 unmap_sysmem(se);
Bin Mengae5bedb2015-10-12 05:23:41 -0700547
548 return addr;
549}