blob: 9eb226ec9fbd5d486a9ff2c439e4e3b440571422 [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
Simon Glass0e89b852021-02-04 21:17:14 -070050 * @return: size of the structure
51 */
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
Simon Glass78227d42020-11-05 06:32:07 -070075 * @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
114 * @return 0 if not found, else SMBIOS string number (1 or more)
115 */
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
142 * @return 0 if not found, else SMBIOS string number (1 or more)
143 */
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
Bin Meng721e9922015-10-12 05:23:41 -0700190 * @return: string area size
191 */
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
232 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
233#endif
Bin Meng721e9922015-10-12 05:23:41 -0700234 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");
261 t->product_name = smbios_add_prop(ctx, "product");
Simon Glass07c9e682021-02-04 21:17:23 -0700262 t->version = smbios_add_prop_si(ctx, "version",
263 SYSINFO_ID_SMBIOS_SYSTEM_VERSION);
Alexander Graf6fb580d2016-08-19 01:23:31 +0200264 if (serial_str) {
Simon Glass0c95fff2021-02-04 21:17:18 -0700265 t->serial_number = smbios_add_string(ctx, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700266 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
267 } else {
Simon Glass0c95fff2021-02-04 21:17:18 -0700268 t->serial_number = smbios_add_prop(ctx, "serial");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200269 }
Simon Glass0c95fff2021-02-04 21:17:18 -0700270 t->sku_number = smbios_add_prop(ctx, "sku");
271 t->family = smbios_add_prop(ctx, "family");
Bin Meng721e9922015-10-12 05:23:41 -0700272
Simon Glassfd3b8262021-02-04 21:17:19 -0700273 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700274 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700275 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700276
277 return len;
278}
279
Simon Glass1e8989a2021-02-04 21:17:17 -0700280static int smbios_write_type2(ulong *current, int handle,
281 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700282{
Simon Glassa2505fc2018-11-22 13:46:37 -0700283 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700284 int len = sizeof(struct smbios_type2);
285
Simon Glassa2505fc2018-11-22 13:46:37 -0700286 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700287 memset(t, 0, sizeof(struct smbios_type2));
288 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700289 smbios_set_eos(ctx, t->eos);
290 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
291 t->product_name = smbios_add_prop(ctx, "product");
Simon Glass07c9e682021-02-04 21:17:23 -0700292 t->version = smbios_add_prop_si(ctx, "version",
293 SYSINFO_ID_SMBIOS_BASEBOARD_VERSION);
Simon Glass0c95fff2021-02-04 21:17:18 -0700294 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
Bin Meng721e9922015-10-12 05:23:41 -0700295 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
296 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
297
Simon Glassfd3b8262021-02-04 21:17:19 -0700298 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700299 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700300 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700301
302 return len;
303}
304
Simon Glass1e8989a2021-02-04 21:17:17 -0700305static int smbios_write_type3(ulong *current, int handle,
306 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700307{
Simon Glassa2505fc2018-11-22 13:46:37 -0700308 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700309 int len = sizeof(struct smbios_type3);
310
Simon Glassa2505fc2018-11-22 13:46:37 -0700311 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700312 memset(t, 0, sizeof(struct smbios_type3));
313 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700314 smbios_set_eos(ctx, t->eos);
315 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Bin Meng721e9922015-10-12 05:23:41 -0700316 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
317 t->bootup_state = SMBIOS_STATE_SAFE;
318 t->power_supply_state = SMBIOS_STATE_SAFE;
319 t->thermal_state = SMBIOS_STATE_SAFE;
320 t->security_status = SMBIOS_SECURITY_NONE;
321
Simon Glassfd3b8262021-02-04 21:17:19 -0700322 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700323 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700324 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700325
326 return len;
327}
328
Simon Glass1e8989a2021-02-04 21:17:17 -0700329static void smbios_write_type4_dm(struct smbios_type4 *t,
330 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200331{
332 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
333 const char *vendor = "Unknown";
334 const char *name = "Unknown";
335
336#ifdef CONFIG_CPU
337 char processor_name[49];
338 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700339 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200340
Simon Glass78227d42020-11-05 06:32:07 -0700341 uclass_find_first_device(UCLASS_CPU, &cpu);
342 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700343 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200344
345 if (plat->family)
346 processor_family = plat->family;
347 t->processor_id[0] = plat->id[0];
348 t->processor_id[1] = plat->id[1];
349
Simon Glass78227d42020-11-05 06:32:07 -0700350 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200351 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700352 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200353 name = processor_name;
354 }
355#endif
356
357 t->processor_family = processor_family;
Simon Glass0c95fff2021-02-04 21:17:18 -0700358 t->processor_manufacturer = smbios_add_string(ctx, vendor);
359 t->processor_version = smbios_add_string(ctx, name);
Alexander Graf96476202016-08-19 01:23:28 +0200360}
361
Simon Glass1e8989a2021-02-04 21:17:17 -0700362static int smbios_write_type4(ulong *current, int handle,
363 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700364{
Simon Glassa2505fc2018-11-22 13:46:37 -0700365 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700366 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700367
Simon Glassa2505fc2018-11-22 13:46:37 -0700368 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700369 memset(t, 0, sizeof(struct smbios_type4));
370 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700371 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700372 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700373 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700374 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
375 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
376 t->l1_cache_handle = 0xffff;
377 t->l2_cache_handle = 0xffff;
378 t->l3_cache_handle = 0xffff;
379 t->processor_family2 = t->processor_family;
380
Simon Glassfd3b8262021-02-04 21:17:19 -0700381 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700382 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700383 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700384
385 return len;
386}
387
Simon Glass1e8989a2021-02-04 21:17:17 -0700388static int smbios_write_type32(ulong *current, int handle,
389 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700390{
Simon Glassa2505fc2018-11-22 13:46:37 -0700391 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700392 int len = sizeof(struct smbios_type32);
393
Simon Glassa2505fc2018-11-22 13:46:37 -0700394 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700395 memset(t, 0, sizeof(struct smbios_type32));
396 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700397 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700398
399 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700400 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700401
402 return len;
403}
404
Simon Glass1e8989a2021-02-04 21:17:17 -0700405static int smbios_write_type127(ulong *current, int handle,
406 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700407{
Simon Glassa2505fc2018-11-22 13:46:37 -0700408 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700409 int len = sizeof(struct smbios_type127);
410
Simon Glassa2505fc2018-11-22 13:46:37 -0700411 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700412 memset(t, 0, sizeof(struct smbios_type127));
413 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
414
415 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700416 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700417
418 return len;
419}
420
Simon Glass44ffb6f2020-11-05 06:32:08 -0700421static struct smbios_write_method smbios_write_funcs[] = {
Simon Glasse9adaa72021-02-04 21:17:20 -0700422 { smbios_write_type0, "bios", },
Simon Glass44ffb6f2020-11-05 06:32:08 -0700423 { smbios_write_type1, "system", },
424 { smbios_write_type2, "baseboard", },
425 { smbios_write_type3, "chassis", },
426 { smbios_write_type4, },
427 { smbios_write_type32, },
428 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700429};
430
Simon Glass42fd8c12017-01-16 07:03:35 -0700431ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700432{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700433 ofnode parent_node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700434 struct smbios_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700435 struct smbios_ctx ctx;
Simon Glassa2505fc2018-11-22 13:46:37 -0700436 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700437 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700438 int len = 0;
439 int max_struct_size = 0;
440 int handle = 0;
441 char *istart;
442 int isize;
443 int i;
444
Simon Glass1e8989a2021-02-04 21:17:17 -0700445 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700446 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700447 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
448 if (ctx.dev)
449 parent_node = dev_read_subnode(ctx.dev, "smbios");
450 } else {
451 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700452 }
453
Bin Meng721e9922015-10-12 05:23:41 -0700454 /* 16 byte align the table address */
455 addr = ALIGN(addr, 16);
456
Simon Glassa2505fc2018-11-22 13:46:37 -0700457 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700458 memset(se, 0, sizeof(struct smbios_entry));
459
460 addr += sizeof(struct smbios_entry);
461 addr = ALIGN(addr, 16);
462 tables = addr;
463
464 /* populate minimum required tables */
465 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700466 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700467 int tmp;
468
469 method = &smbios_write_funcs[i];
470 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass1e8989a2021-02-04 21:17:17 -0700471 ctx.node = ofnode_find_subnode(parent_node,
472 method->subnode_name);
473 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200474
Bin Meng721e9922015-10-12 05:23:41 -0700475 max_struct_size = max(max_struct_size, tmp);
476 len += tmp;
477 }
478
479 memcpy(se->anchor, "_SM_", 4);
480 se->length = sizeof(struct smbios_entry);
481 se->major_ver = SMBIOS_MAJOR_VER;
482 se->minor_ver = SMBIOS_MINOR_VER;
483 se->max_struct_size = max_struct_size;
484 memcpy(se->intermediate_anchor, "_DMI_", 5);
485 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700486
487 /*
488 * We must use a pointer here so things work correctly on sandbox. The
489 * user of this table is not aware of the mapping of addresses to
490 * sandbox's DRAM buffer.
491 */
492 table_addr = (ulong)map_sysmem(tables, 0);
493 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
494 /*
495 * We need to put this >32-bit pointer into the table but the
496 * field is only 32 bits wide.
497 */
498 printf("WARNING: SMBIOS table_address overflow %llx\n",
499 (unsigned long long)table_addr);
500 table_addr = 0;
501 }
502 se->struct_table_address = table_addr;
503
Bin Meng721e9922015-10-12 05:23:41 -0700504 se->struct_count = handle;
505
506 /* calculate checksums */
507 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
508 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
509 se->intermediate_checksum = table_compute_checksum(istart, isize);
510 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700511 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700512
513 return addr;
514}