blob: b52e125eeb1401ad1d5b885abd429fbd248f8cf5 [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
8#include <common.h>
Simon Glass78227d42020-11-05 06:32:07 -07009#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Simon Glassa2505fc2018-11-22 13:46:37 -070011#include <mapmem.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020012#include <smbios.h>
Simon Glass07c9e682021-02-04 21:17:23 -070013#include <sysinfo.h>
Alexander Graf4b6dddc2016-08-19 01:23:23 +020014#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070015#include <version.h>
Alexander Graf96476202016-08-19 01:23:28 +020016#ifdef CONFIG_CPU
17#include <cpu.h>
Alexander Graf96476202016-08-19 01:23:28 +020018#include <dm/uclass-internal.h>
19#endif
Bin Meng721e9922015-10-12 05:23:41 -070020
Simon Glasse9adaa72021-02-04 21:17:20 -070021DECLARE_GLOBAL_DATA_PTR;
22
Bin Meng721e9922015-10-12 05:23:41 -070023/**
Simon Glass1e8989a2021-02-04 21:17:17 -070024 * struct smbios_ctx - context for writing SMBIOS tables
25 *
26 * @node: node containing the information to write (ofnode_null() if none)
27 * @dev: sysinfo device to use (NULL if none)
Simon Glass0c95fff2021-02-04 21:17:18 -070028 * @eos: end-of-string pointer for the table being processed. This is set
29 * up when we start processing a table
Simon Glassfd3b8262021-02-04 21:17:19 -070030 * @next_ptr: pointer to the start of the next string to be added. When the
31 * table is nopt empty, this points to the byte after the \0 of the
32 * previous string.
Simon Glasse9adaa72021-02-04 21:17:20 -070033 * @last_str: points to the last string that was written to the table, or NULL
34 * if none
Simon Glass1e8989a2021-02-04 21:17:17 -070035 */
36struct smbios_ctx {
37 ofnode node;
38 struct udevice *dev;
Simon Glass0c95fff2021-02-04 21:17:18 -070039 char *eos;
Simon Glassfd3b8262021-02-04 21:17:19 -070040 char *next_ptr;
Simon Glasse9adaa72021-02-04 21:17:20 -070041 char *last_str;
Simon Glass1e8989a2021-02-04 21:17:17 -070042};
43
44/**
Simon Glass0e89b852021-02-04 21:17:14 -070045 * Function prototype to write a specific type of SMBIOS structure
46 *
47 * @addr: start address to write the structure
48 * @handle: the structure's handle, a unique 16-bit number
Simon Glass1e8989a2021-02-04 21:17:17 -070049 * @ctx: context for writing the tables
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +020050 * Return: size of the structure
Simon Glass0e89b852021-02-04 21:17:14 -070051 */
Simon Glass1e8989a2021-02-04 21:17:17 -070052typedef int (*smbios_write_type)(ulong *addr, int handle,
53 struct smbios_ctx *ctx);
Simon Glass0e89b852021-02-04 21:17:14 -070054
55/**
Simon Glass44ffb6f2020-11-05 06:32:08 -070056 * struct smbios_write_method - Information about a table-writing function
57 *
58 * @write: Function to call
59 * @subnode_name: Name of subnode which has the information for this function,
60 * NULL if none
61 */
62struct smbios_write_method {
63 smbios_write_type write;
64 const char *subnode_name;
65};
66
67/**
Bin Meng721e9922015-10-12 05:23:41 -070068 * smbios_add_string() - add a string to the string area
69 *
70 * This adds a string to the string area which is appended directly after
71 * the formatted portion of an SMBIOS structure.
72 *
Simon Glass0c95fff2021-02-04 21:17:18 -070073 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -070074 * @str: string to add
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +020075 * Return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -070076 */
Simon Glass0c95fff2021-02-04 21:17:18 -070077static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Meng721e9922015-10-12 05:23:41 -070078{
79 int i = 1;
Simon Glass0c95fff2021-02-04 21:17:18 -070080 char *p = ctx->eos;
81
Heinrich Schuchardt00a871d2020-06-01 15:44:00 +020082 if (!*str)
83 str = "Unknown";
Bin Meng721e9922015-10-12 05:23:41 -070084
85 for (;;) {
86 if (!*p) {
Simon Glasse9adaa72021-02-04 21:17:20 -070087 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -070088 strcpy(p, str);
89 p += strlen(str);
90 *p++ = '\0';
Simon Glassfd3b8262021-02-04 21:17:19 -070091 ctx->next_ptr = p;
Bin Meng721e9922015-10-12 05:23:41 -070092 *p++ = '\0';
93
94 return i;
95 }
96
Simon Glasse9adaa72021-02-04 21:17:20 -070097 if (!strcmp(p, str)) {
98 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -070099 return i;
Simon Glasse9adaa72021-02-04 21:17:20 -0700100 }
Bin Meng721e9922015-10-12 05:23:41 -0700101
102 p += strlen(p) + 1;
103 i++;
104 }
105}
106
107/**
Simon Glass07c9e682021-02-04 21:17:23 -0700108 * smbios_add_prop_si() - Add a property from the devicetree or sysinfo
109 *
110 * Sysinfo is used if available, with a fallback to devicetree
Simon Glass44ffb6f2020-11-05 06:32:08 -0700111 *
Simon Glass1e8989a2021-02-04 21:17:17 -0700112 * @ctx: context for writing the tables
Simon Glass44ffb6f2020-11-05 06:32:08 -0700113 * @prop: property to write
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200114 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700115 */
Simon Glass07c9e682021-02-04 21:17:23 -0700116static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop,
117 int sysinfo_id)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700118{
Simon Glass07c9e682021-02-04 21:17:23 -0700119 if (sysinfo_id && ctx->dev) {
120 char val[SMBIOS_STR_MAX];
121 int ret;
122
123 ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val);
124 if (!ret)
125 return smbios_add_string(ctx, val);
126 }
Simon Glasse4f8e542020-11-05 06:32:18 -0700127 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
128 const char *str;
129
Simon Glass1e8989a2021-02-04 21:17:17 -0700130 str = ofnode_read_string(ctx->node, prop);
Simon Glasse4f8e542020-11-05 06:32:18 -0700131 if (str)
Simon Glass0c95fff2021-02-04 21:17:18 -0700132 return smbios_add_string(ctx, str);
Simon Glasse4f8e542020-11-05 06:32:18 -0700133 }
134
135 return 0;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700136}
137
Simon Glass07c9e682021-02-04 21:17:23 -0700138/**
139 * smbios_add_prop() - Add a property from the devicetree
140 *
141 * @prop: property to write
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200142 * Return: 0 if not found, else SMBIOS string number (1 or more)
Simon Glass07c9e682021-02-04 21:17:23 -0700143 */
144static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
145{
146 return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE);
147}
148
Simon Glass0c95fff2021-02-04 21:17:18 -0700149static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
150{
151 ctx->eos = eos;
Simon Glassfd3b8262021-02-04 21:17:19 -0700152 ctx->next_ptr = eos;
Simon Glasse9adaa72021-02-04 21:17:20 -0700153 ctx->last_str = NULL;
154}
155
156int smbios_update_version(const char *version)
157{
158 char *ptr = gd->smbios_version;
159 uint old_len, len;
160
161 if (!ptr)
162 return log_ret(-ENOENT);
163
164 /*
165 * This string is supposed to have at least enough bytes and is
166 * padded with spaces. Update it, taking care not to move the
167 * \0 terminator, so that other strings in the string table
168 * are not disturbed. See smbios_add_string()
169 */
170 old_len = strnlen(ptr, SMBIOS_STR_MAX);
171 len = strnlen(version, SMBIOS_STR_MAX);
172 if (len > old_len)
173 return log_ret(-ENOSPC);
174
175 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
176 memcpy(ptr, version, len);
177#ifdef LOG_DEBUG
178 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
179#endif
180
181 return 0;
Simon Glass0c95fff2021-02-04 21:17:18 -0700182}
183
Simon Glass44ffb6f2020-11-05 06:32:08 -0700184/**
Bin Meng721e9922015-10-12 05:23:41 -0700185 * smbios_string_table_len() - compute the string area size
186 *
187 * This computes the size of the string area including the string terminator.
188 *
Simon Glassfd3b8262021-02-04 21:17:19 -0700189 * @ctx: SMBIOS context
Heinrich Schuchardt8c6532d2021-06-10 12:13:52 +0200190 * Return: string area size
Bin Meng721e9922015-10-12 05:23:41 -0700191 */
Simon Glassfd3b8262021-02-04 21:17:19 -0700192static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700193{
Simon Glassfd3b8262021-02-04 21:17:19 -0700194 /* Allow for the final \0 after all strings */
195 return (ctx->next_ptr + 1) - ctx->eos;
Bin Meng721e9922015-10-12 05:23:41 -0700196}
197
Simon Glass1e8989a2021-02-04 21:17:17 -0700198static int smbios_write_type0(ulong *current, int handle,
199 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700200{
Simon Glassa2505fc2018-11-22 13:46:37 -0700201 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700202 int len = sizeof(struct smbios_type0);
203
Simon Glassa2505fc2018-11-22 13:46:37 -0700204 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700205 memset(t, 0, sizeof(struct smbios_type0));
206 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700207 smbios_set_eos(ctx, t->eos);
208 t->vendor = smbios_add_string(ctx, "U-Boot");
Simon Glasse9adaa72021-02-04 21:17:20 -0700209
210 t->bios_ver = smbios_add_prop(ctx, "version");
211 if (!t->bios_ver)
212 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
213 if (t->bios_ver)
214 gd->smbios_version = ctx->last_str;
215 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
216 gd->smbios_version);
217#ifdef LOG_DEBUG
218 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
219 1, strlen(gd->smbios_version) + 1, 0);
220#endif
Simon Glass0c95fff2021-02-04 21:17:18 -0700221 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +0200222#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -0700223 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +0200224#endif
Bin Meng721e9922015-10-12 05:23:41 -0700225 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
226 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
227 BIOS_CHARACTERISTICS_UPGRADEABLE;
228#ifdef CONFIG_GENERATE_ACPI_TABLE
229 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
230#endif
Alexander Grafe663b352016-08-19 01:23:29 +0200231#ifdef CONFIG_EFI_LOADER
Ilias Apalodimasff192302021-06-09 18:14:47 +0300232 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI;
Alexander Grafe663b352016-08-19 01:23:29 +0200233#endif
Ilias Apalodimasff192302021-06-09 18:14:47 +0300234 t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200235
Simon Glass7617f992021-02-04 21:17:16 -0700236 /* bios_major_release has only one byte, so drop century */
237 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
238 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Meng721e9922015-10-12 05:23:41 -0700239 t->ec_major_release = 0xff;
240 t->ec_minor_release = 0xff;
241
Simon Glassfd3b8262021-02-04 21:17:19 -0700242 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700243 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700244 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700245
246 return len;
247}
248
Simon Glass1e8989a2021-02-04 21:17:17 -0700249static int smbios_write_type1(ulong *current, int handle,
250 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700251{
Simon Glassa2505fc2018-11-22 13:46:37 -0700252 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700253 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600254 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700255
Simon Glassa2505fc2018-11-22 13:46:37 -0700256 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700257 memset(t, 0, sizeof(struct smbios_type1));
258 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700259 smbios_set_eos(ctx, t->eos);
260 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas70e80662021-06-10 12:33:15 +0300261 if (!t->manufacturer)
262 t->manufacturer = smbios_add_string(ctx, "Unknown");
Simon Glass0c95fff2021-02-04 21:17:18 -0700263 t->product_name = smbios_add_prop(ctx, "product");
Ilias Apalodimas70e80662021-06-10 12:33:15 +0300264 if (!t->product_name)
265 t->product_name = smbios_add_string(ctx, "Unknown Product");
Simon Glass07c9e682021-02-04 21:17:23 -0700266 t->version = smbios_add_prop_si(ctx, "version",
267 SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
Alexander Graf6fb580d2016-08-19 01:23:31 +0200268 if (serial_str) {
Simon Glass0c95fff2021-02-04 21:17:18 -0700269 t->serial_number = smbios_add_string(ctx, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700270 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
271 } else {
Simon Glass0c95fff2021-02-04 21:17:18 -0700272 t->serial_number = smbios_add_prop(ctx, "serial");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200273 }
Simon Glass0c95fff2021-02-04 21:17:18 -0700274 t->sku_number = smbios_add_prop(ctx, "sku");
275 t->family = smbios_add_prop(ctx, "family");
Bin Meng721e9922015-10-12 05:23:41 -0700276
Simon Glassfd3b8262021-02-04 21:17:19 -0700277 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700278 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700279 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700280
281 return len;
282}
283
Simon Glass1e8989a2021-02-04 21:17:17 -0700284static int smbios_write_type2(ulong *current, int handle,
285 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700286{
Simon Glassa2505fc2018-11-22 13:46:37 -0700287 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700288 int len = sizeof(struct smbios_type2);
289
Simon Glassa2505fc2018-11-22 13:46:37 -0700290 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700291 memset(t, 0, sizeof(struct smbios_type2));
292 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700293 smbios_set_eos(ctx, t->eos);
294 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas70e80662021-06-10 12:33:15 +0300295 if (!t->manufacturer)
296 t->manufacturer = smbios_add_string(ctx, "Unknown");
Simon Glass0c95fff2021-02-04 21:17:18 -0700297 t->product_name = smbios_add_prop(ctx, "product");
Ilias Apalodimas70e80662021-06-10 12:33:15 +0300298 if (!t->product_name)
299 t->product_name = smbios_add_string(ctx, "Unknown Product");
Simon Glass07c9e682021-02-04 21:17:23 -0700300 t->version = smbios_add_prop_si(ctx, "version",
301 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
Simon Glass0c95fff2021-02-04 21:17:18 -0700302 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
Bin Meng721e9922015-10-12 05:23:41 -0700303 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
304 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
305
Simon Glassfd3b8262021-02-04 21:17:19 -0700306 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700307 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700308 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700309
310 return len;
311}
312
Simon Glass1e8989a2021-02-04 21:17:17 -0700313static int smbios_write_type3(ulong *current, int handle,
314 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700315{
Simon Glassa2505fc2018-11-22 13:46:37 -0700316 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700317 int len = sizeof(struct smbios_type3);
318
Simon Glassa2505fc2018-11-22 13:46:37 -0700319 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700320 memset(t, 0, sizeof(struct smbios_type3));
321 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700322 smbios_set_eos(ctx, t->eos);
323 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Ilias Apalodimas70e80662021-06-10 12:33:15 +0300324 if (!t->manufacturer)
325 t->manufacturer = smbios_add_string(ctx, "Unknown");
Bin Meng721e9922015-10-12 05:23:41 -0700326 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
327 t->bootup_state = SMBIOS_STATE_SAFE;
328 t->power_supply_state = SMBIOS_STATE_SAFE;
329 t->thermal_state = SMBIOS_STATE_SAFE;
330 t->security_status = SMBIOS_SECURITY_NONE;
331
Simon Glassfd3b8262021-02-04 21:17:19 -0700332 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700333 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700334 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700335
336 return len;
337}
338
Simon Glass1e8989a2021-02-04 21:17:17 -0700339static void smbios_write_type4_dm(struct smbios_type4 *t,
340 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200341{
342 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
343 const char *vendor = "Unknown";
344 const char *name = "Unknown";
345
346#ifdef CONFIG_CPU
347 char processor_name[49];
348 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700349 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200350
Simon Glass78227d42020-11-05 06:32:07 -0700351 uclass_find_first_device(UCLASS_CPU, &cpu);
352 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700353 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200354
355 if (plat->family)
356 processor_family = plat->family;
357 t->processor_id[0] = plat->id[0];
358 t->processor_id[1] = plat->id[1];
359
Simon Glass78227d42020-11-05 06:32:07 -0700360 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200361 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700362 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200363 name = processor_name;
364 }
365#endif
366
367 t->processor_family = processor_family;
Simon Glass0c95fff2021-02-04 21:17:18 -0700368 t->processor_manufacturer = smbios_add_string(ctx, vendor);
369 t->processor_version = smbios_add_string(ctx, name);
Alexander Graf96476202016-08-19 01:23:28 +0200370}
371
Simon Glass1e8989a2021-02-04 21:17:17 -0700372static int smbios_write_type4(ulong *current, int handle,
373 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700374{
Simon Glassa2505fc2018-11-22 13:46:37 -0700375 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700376 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700377
Simon Glassa2505fc2018-11-22 13:46:37 -0700378 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700379 memset(t, 0, sizeof(struct smbios_type4));
380 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700381 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700382 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700383 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700384 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
385 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
386 t->l1_cache_handle = 0xffff;
387 t->l2_cache_handle = 0xffff;
388 t->l3_cache_handle = 0xffff;
389 t->processor_family2 = t->processor_family;
390
Simon Glassfd3b8262021-02-04 21:17:19 -0700391 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700392 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700393 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700394
395 return len;
396}
397
Simon Glass1e8989a2021-02-04 21:17:17 -0700398static int smbios_write_type32(ulong *current, int handle,
399 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700400{
Simon Glassa2505fc2018-11-22 13:46:37 -0700401 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700402 int len = sizeof(struct smbios_type32);
403
Simon Glassa2505fc2018-11-22 13:46:37 -0700404 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700405 memset(t, 0, sizeof(struct smbios_type32));
406 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700407 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700408
409 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700410 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700411
412 return len;
413}
414
Simon Glass1e8989a2021-02-04 21:17:17 -0700415static int smbios_write_type127(ulong *current, int handle,
416 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700417{
Simon Glassa2505fc2018-11-22 13:46:37 -0700418 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700419 int len = sizeof(struct smbios_type127);
420
Simon Glassa2505fc2018-11-22 13:46:37 -0700421 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700422 memset(t, 0, sizeof(struct smbios_type127));
423 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
424
425 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700426 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700427
428 return len;
429}
430
Simon Glass44ffb6f2020-11-05 06:32:08 -0700431static struct smbios_write_method smbios_write_funcs[] = {
Simon Glasse9adaa72021-02-04 21:17:20 -0700432 { smbios_write_type0, "bios", },
Simon Glass44ffb6f2020-11-05 06:32:08 -0700433 { smbios_write_type1, "system", },
434 { smbios_write_type2, "baseboard", },
435 { smbios_write_type3, "chassis", },
436 { smbios_write_type4, },
437 { smbios_write_type32, },
438 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700439};
440
Simon Glass42fd8c12017-01-16 07:03:35 -0700441ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700442{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700443 ofnode parent_node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700444 struct smbios_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700445 struct smbios_ctx ctx;
Simon Glassa2505fc2018-11-22 13:46:37 -0700446 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700447 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700448 int len = 0;
449 int max_struct_size = 0;
450 int handle = 0;
451 char *istart;
452 int isize;
453 int i;
454
Simon Glass1e8989a2021-02-04 21:17:17 -0700455 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700456 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700457 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
458 if (ctx.dev)
459 parent_node = dev_read_subnode(ctx.dev, "smbios");
460 } else {
461 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700462 }
463
Bin Meng721e9922015-10-12 05:23:41 -0700464 /* 16 byte align the table address */
465 addr = ALIGN(addr, 16);
466
Simon Glassa2505fc2018-11-22 13:46:37 -0700467 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700468 memset(se, 0, sizeof(struct smbios_entry));
469
470 addr += sizeof(struct smbios_entry);
471 addr = ALIGN(addr, 16);
472 tables = addr;
473
474 /* populate minimum required tables */
475 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700476 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700477 int tmp;
478
479 method = &smbios_write_funcs[i];
480 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass1e8989a2021-02-04 21:17:17 -0700481 ctx.node = ofnode_find_subnode(parent_node,
482 method->subnode_name);
483 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200484
Bin Meng721e9922015-10-12 05:23:41 -0700485 max_struct_size = max(max_struct_size, tmp);
486 len += tmp;
487 }
488
489 memcpy(se->anchor, "_SM_", 4);
490 se->length = sizeof(struct smbios_entry);
491 se->major_ver = SMBIOS_MAJOR_VER;
492 se->minor_ver = SMBIOS_MINOR_VER;
493 se->max_struct_size = max_struct_size;
494 memcpy(se->intermediate_anchor, "_DMI_", 5);
495 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700496
497 /*
498 * We must use a pointer here so things work correctly on sandbox. The
499 * user of this table is not aware of the mapping of addresses to
500 * sandbox's DRAM buffer.
501 */
502 table_addr = (ulong)map_sysmem(tables, 0);
503 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
504 /*
505 * We need to put this >32-bit pointer into the table but the
506 * field is only 32 bits wide.
507 */
508 printf("WARNING: SMBIOS table_address overflow %llx\n",
509 (unsigned long long)table_addr);
510 table_addr = 0;
511 }
512 se->struct_table_address = table_addr;
513
Bin Meng721e9922015-10-12 05:23:41 -0700514 se->struct_count = handle;
515
516 /* calculate checksums */
517 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
518 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
519 se->intermediate_checksum = table_compute_checksum(istart, isize);
520 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700521 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700522
523 return addr;
524}