blob: a7273529cc4db96954e2b37b624d721f47ff1429 [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>
13#include <tables_csum.h>
Bin Meng721e9922015-10-12 05:23:41 -070014#include <version.h>
Alexander Graf96476202016-08-19 01:23:28 +020015#ifdef CONFIG_CPU
16#include <cpu.h>
Alexander Graf96476202016-08-19 01:23:28 +020017#include <dm/uclass-internal.h>
18#endif
Bin Meng721e9922015-10-12 05:23:41 -070019
Bin Meng721e9922015-10-12 05:23:41 -070020/**
Simon Glass1e8989a2021-02-04 21:17:17 -070021 * struct smbios_ctx - context for writing SMBIOS tables
22 *
23 * @node: node containing the information to write (ofnode_null() if none)
24 * @dev: sysinfo device to use (NULL if none)
Simon Glass0c95fff2021-02-04 21:17:18 -070025 * @eos: end-of-string pointer for the table being processed. This is set
26 * up when we start processing a table
Simon Glassfd3b8262021-02-04 21:17:19 -070027 * @next_ptr: pointer to the start of the next string to be added. When the
28 * table is nopt empty, this points to the byte after the \0 of the
29 * previous string.
Simon Glass1e8989a2021-02-04 21:17:17 -070030 */
31struct smbios_ctx {
32 ofnode node;
33 struct udevice *dev;
Simon Glass0c95fff2021-02-04 21:17:18 -070034 char *eos;
Simon Glassfd3b8262021-02-04 21:17:19 -070035 char *next_ptr;
Simon Glass1e8989a2021-02-04 21:17:17 -070036};
37
38/**
Simon Glass0e89b852021-02-04 21:17:14 -070039 * Function prototype to write a specific type of SMBIOS structure
40 *
41 * @addr: start address to write the structure
42 * @handle: the structure's handle, a unique 16-bit number
Simon Glass1e8989a2021-02-04 21:17:17 -070043 * @ctx: context for writing the tables
Simon Glass0e89b852021-02-04 21:17:14 -070044 * @return: size of the structure
45 */
Simon Glass1e8989a2021-02-04 21:17:17 -070046typedef int (*smbios_write_type)(ulong *addr, int handle,
47 struct smbios_ctx *ctx);
Simon Glass0e89b852021-02-04 21:17:14 -070048
49/**
Simon Glass44ffb6f2020-11-05 06:32:08 -070050 * struct smbios_write_method - Information about a table-writing function
51 *
52 * @write: Function to call
53 * @subnode_name: Name of subnode which has the information for this function,
54 * NULL if none
55 */
56struct smbios_write_method {
57 smbios_write_type write;
58 const char *subnode_name;
59};
60
61/**
Bin Meng721e9922015-10-12 05:23:41 -070062 * smbios_add_string() - add a string to the string area
63 *
64 * This adds a string to the string area which is appended directly after
65 * the formatted portion of an SMBIOS structure.
66 *
Simon Glass0c95fff2021-02-04 21:17:18 -070067 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -070068 * @str: string to add
Simon Glass78227d42020-11-05 06:32:07 -070069 * @return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -070070 */
Simon Glass0c95fff2021-02-04 21:17:18 -070071static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Meng721e9922015-10-12 05:23:41 -070072{
73 int i = 1;
Simon Glass0c95fff2021-02-04 21:17:18 -070074 char *p = ctx->eos;
75
Heinrich Schuchardt00a871d2020-06-01 15:44:00 +020076 if (!*str)
77 str = "Unknown";
Bin Meng721e9922015-10-12 05:23:41 -070078
79 for (;;) {
80 if (!*p) {
81 strcpy(p, str);
82 p += strlen(str);
83 *p++ = '\0';
Simon Glassfd3b8262021-02-04 21:17:19 -070084 ctx->next_ptr = p;
Bin Meng721e9922015-10-12 05:23:41 -070085 *p++ = '\0';
86
87 return i;
88 }
89
90 if (!strcmp(p, str))
91 return i;
92
93 p += strlen(p) + 1;
94 i++;
95 }
96}
97
98/**
Simon Glass44ffb6f2020-11-05 06:32:08 -070099 * smbios_add_prop() - Add a property from the device tree
100 *
Simon Glass1e8989a2021-02-04 21:17:17 -0700101 * @ctx: context for writing the tables
Simon Glass44ffb6f2020-11-05 06:32:08 -0700102 * @prop: property to write
103 * @return 0 if not found, else SMBIOS string number (1 or more)
104 */
Simon Glass0c95fff2021-02-04 21:17:18 -0700105static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700106{
Simon Glasse4f8e542020-11-05 06:32:18 -0700107 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
108 const char *str;
109
Simon Glass1e8989a2021-02-04 21:17:17 -0700110 str = ofnode_read_string(ctx->node, prop);
Simon Glasse4f8e542020-11-05 06:32:18 -0700111 if (str)
Simon Glass0c95fff2021-02-04 21:17:18 -0700112 return smbios_add_string(ctx, str);
Simon Glasse4f8e542020-11-05 06:32:18 -0700113 }
114
115 return 0;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700116}
117
Simon Glass0c95fff2021-02-04 21:17:18 -0700118static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
119{
120 ctx->eos = eos;
Simon Glassfd3b8262021-02-04 21:17:19 -0700121 ctx->next_ptr = eos;
Simon Glass0c95fff2021-02-04 21:17:18 -0700122}
123
Simon Glass44ffb6f2020-11-05 06:32:08 -0700124/**
Bin Meng721e9922015-10-12 05:23:41 -0700125 * smbios_string_table_len() - compute the string area size
126 *
127 * This computes the size of the string area including the string terminator.
128 *
Simon Glassfd3b8262021-02-04 21:17:19 -0700129 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -0700130 * @return: string area size
131 */
Simon Glassfd3b8262021-02-04 21:17:19 -0700132static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700133{
Simon Glassfd3b8262021-02-04 21:17:19 -0700134 /* Allow for the final \0 after all strings */
135 return (ctx->next_ptr + 1) - ctx->eos;
Bin Meng721e9922015-10-12 05:23:41 -0700136}
137
Simon Glass1e8989a2021-02-04 21:17:17 -0700138static int smbios_write_type0(ulong *current, int handle,
139 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700140{
Simon Glassa2505fc2018-11-22 13:46:37 -0700141 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700142 int len = sizeof(struct smbios_type0);
143
Simon Glassa2505fc2018-11-22 13:46:37 -0700144 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700145 memset(t, 0, sizeof(struct smbios_type0));
146 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700147 smbios_set_eos(ctx, t->eos);
148 t->vendor = smbios_add_string(ctx, "U-Boot");
149 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
150 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +0200151#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -0700152 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +0200153#endif
Bin Meng721e9922015-10-12 05:23:41 -0700154 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
155 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
156 BIOS_CHARACTERISTICS_UPGRADEABLE;
157#ifdef CONFIG_GENERATE_ACPI_TABLE
158 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
159#endif
Alexander Grafe663b352016-08-19 01:23:29 +0200160#ifdef CONFIG_EFI_LOADER
161 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
162#endif
Bin Meng721e9922015-10-12 05:23:41 -0700163 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200164
Simon Glass7617f992021-02-04 21:17:16 -0700165 /* bios_major_release has only one byte, so drop century */
166 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
167 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Meng721e9922015-10-12 05:23:41 -0700168 t->ec_major_release = 0xff;
169 t->ec_minor_release = 0xff;
170
Simon Glassfd3b8262021-02-04 21:17:19 -0700171 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700172 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700173 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700174
175 return len;
176}
177
Simon Glass1e8989a2021-02-04 21:17:17 -0700178static int smbios_write_type1(ulong *current, int handle,
179 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700180{
Simon Glassa2505fc2018-11-22 13:46:37 -0700181 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700182 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600183 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700184
Simon Glassa2505fc2018-11-22 13:46:37 -0700185 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700186 memset(t, 0, sizeof(struct smbios_type1));
187 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700188 smbios_set_eos(ctx, t->eos);
189 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
190 t->product_name = smbios_add_prop(ctx, "product");
191 t->version = smbios_add_prop(ctx, "version");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200192 if (serial_str) {
Simon Glass0c95fff2021-02-04 21:17:18 -0700193 t->serial_number = smbios_add_string(ctx, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700194 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
195 } else {
Simon Glass0c95fff2021-02-04 21:17:18 -0700196 t->serial_number = smbios_add_prop(ctx, "serial");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200197 }
Simon Glass0c95fff2021-02-04 21:17:18 -0700198 t->sku_number = smbios_add_prop(ctx, "sku");
199 t->family = smbios_add_prop(ctx, "family");
Bin Meng721e9922015-10-12 05:23:41 -0700200
Simon Glassfd3b8262021-02-04 21:17:19 -0700201 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700202 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700203 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700204
205 return len;
206}
207
Simon Glass1e8989a2021-02-04 21:17:17 -0700208static int smbios_write_type2(ulong *current, int handle,
209 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700210{
Simon Glassa2505fc2018-11-22 13:46:37 -0700211 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700212 int len = sizeof(struct smbios_type2);
213
Simon Glassa2505fc2018-11-22 13:46:37 -0700214 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700215 memset(t, 0, sizeof(struct smbios_type2));
216 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700217 smbios_set_eos(ctx, t->eos);
218 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
219 t->product_name = smbios_add_prop(ctx, "product");
220 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
Bin Meng721e9922015-10-12 05:23:41 -0700221 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
222 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
223
Simon Glassfd3b8262021-02-04 21:17:19 -0700224 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700225 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700226 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700227
228 return len;
229}
230
Simon Glass1e8989a2021-02-04 21:17:17 -0700231static int smbios_write_type3(ulong *current, int handle,
232 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700233{
Simon Glassa2505fc2018-11-22 13:46:37 -0700234 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700235 int len = sizeof(struct smbios_type3);
236
Simon Glassa2505fc2018-11-22 13:46:37 -0700237 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700238 memset(t, 0, sizeof(struct smbios_type3));
239 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700240 smbios_set_eos(ctx, t->eos);
241 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Bin Meng721e9922015-10-12 05:23:41 -0700242 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
243 t->bootup_state = SMBIOS_STATE_SAFE;
244 t->power_supply_state = SMBIOS_STATE_SAFE;
245 t->thermal_state = SMBIOS_STATE_SAFE;
246 t->security_status = SMBIOS_SECURITY_NONE;
247
Simon Glassfd3b8262021-02-04 21:17:19 -0700248 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700249 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700250 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700251
252 return len;
253}
254
Simon Glass1e8989a2021-02-04 21:17:17 -0700255static void smbios_write_type4_dm(struct smbios_type4 *t,
256 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200257{
258 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
259 const char *vendor = "Unknown";
260 const char *name = "Unknown";
261
262#ifdef CONFIG_CPU
263 char processor_name[49];
264 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700265 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200266
Simon Glass78227d42020-11-05 06:32:07 -0700267 uclass_find_first_device(UCLASS_CPU, &cpu);
268 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700269 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200270
271 if (plat->family)
272 processor_family = plat->family;
273 t->processor_id[0] = plat->id[0];
274 t->processor_id[1] = plat->id[1];
275
Simon Glass78227d42020-11-05 06:32:07 -0700276 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200277 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700278 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200279 name = processor_name;
280 }
281#endif
282
283 t->processor_family = processor_family;
Simon Glass0c95fff2021-02-04 21:17:18 -0700284 t->processor_manufacturer = smbios_add_string(ctx, vendor);
285 t->processor_version = smbios_add_string(ctx, name);
Alexander Graf96476202016-08-19 01:23:28 +0200286}
287
Simon Glass1e8989a2021-02-04 21:17:17 -0700288static int smbios_write_type4(ulong *current, int handle,
289 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700290{
Simon Glassa2505fc2018-11-22 13:46:37 -0700291 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700292 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700293
Simon Glassa2505fc2018-11-22 13:46:37 -0700294 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700295 memset(t, 0, sizeof(struct smbios_type4));
296 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700297 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700298 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700299 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700300 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
301 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
302 t->l1_cache_handle = 0xffff;
303 t->l2_cache_handle = 0xffff;
304 t->l3_cache_handle = 0xffff;
305 t->processor_family2 = t->processor_family;
306
Simon Glassfd3b8262021-02-04 21:17:19 -0700307 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700308 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700309 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700310
311 return len;
312}
313
Simon Glass1e8989a2021-02-04 21:17:17 -0700314static int smbios_write_type32(ulong *current, int handle,
315 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700316{
Simon Glassa2505fc2018-11-22 13:46:37 -0700317 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700318 int len = sizeof(struct smbios_type32);
319
Simon Glassa2505fc2018-11-22 13:46:37 -0700320 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700321 memset(t, 0, sizeof(struct smbios_type32));
322 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700323 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700324
325 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700326 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700327
328 return len;
329}
330
Simon Glass1e8989a2021-02-04 21:17:17 -0700331static int smbios_write_type127(ulong *current, int handle,
332 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700333{
Simon Glassa2505fc2018-11-22 13:46:37 -0700334 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700335 int len = sizeof(struct smbios_type127);
336
Simon Glassa2505fc2018-11-22 13:46:37 -0700337 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700338 memset(t, 0, sizeof(struct smbios_type127));
339 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
340
341 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700342 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700343
344 return len;
345}
346
Simon Glass44ffb6f2020-11-05 06:32:08 -0700347static struct smbios_write_method smbios_write_funcs[] = {
348 { smbios_write_type0, },
349 { smbios_write_type1, "system", },
350 { smbios_write_type2, "baseboard", },
351 { smbios_write_type3, "chassis", },
352 { smbios_write_type4, },
353 { smbios_write_type32, },
354 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700355};
356
Simon Glass42fd8c12017-01-16 07:03:35 -0700357ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700358{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700359 ofnode parent_node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700360 struct smbios_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700361 struct smbios_ctx ctx;
Simon Glassa2505fc2018-11-22 13:46:37 -0700362 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700363 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700364 int len = 0;
365 int max_struct_size = 0;
366 int handle = 0;
367 char *istart;
368 int isize;
369 int i;
370
Simon Glass1e8989a2021-02-04 21:17:17 -0700371 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700372 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700373 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
374 if (ctx.dev)
375 parent_node = dev_read_subnode(ctx.dev, "smbios");
376 } else {
377 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700378 }
379
Bin Meng721e9922015-10-12 05:23:41 -0700380 /* 16 byte align the table address */
381 addr = ALIGN(addr, 16);
382
Simon Glassa2505fc2018-11-22 13:46:37 -0700383 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700384 memset(se, 0, sizeof(struct smbios_entry));
385
386 addr += sizeof(struct smbios_entry);
387 addr = ALIGN(addr, 16);
388 tables = addr;
389
390 /* populate minimum required tables */
391 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700392 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700393 int tmp;
394
395 method = &smbios_write_funcs[i];
396 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass1e8989a2021-02-04 21:17:17 -0700397 ctx.node = ofnode_find_subnode(parent_node,
398 method->subnode_name);
399 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200400
Bin Meng721e9922015-10-12 05:23:41 -0700401 max_struct_size = max(max_struct_size, tmp);
402 len += tmp;
403 }
404
405 memcpy(se->anchor, "_SM_", 4);
406 se->length = sizeof(struct smbios_entry);
407 se->major_ver = SMBIOS_MAJOR_VER;
408 se->minor_ver = SMBIOS_MINOR_VER;
409 se->max_struct_size = max_struct_size;
410 memcpy(se->intermediate_anchor, "_DMI_", 5);
411 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700412
413 /*
414 * We must use a pointer here so things work correctly on sandbox. The
415 * user of this table is not aware of the mapping of addresses to
416 * sandbox's DRAM buffer.
417 */
418 table_addr = (ulong)map_sysmem(tables, 0);
419 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
420 /*
421 * We need to put this >32-bit pointer into the table but the
422 * field is only 32 bits wide.
423 */
424 printf("WARNING: SMBIOS table_address overflow %llx\n",
425 (unsigned long long)table_addr);
426 table_addr = 0;
427 }
428 se->struct_table_address = table_addr;
429
Bin Meng721e9922015-10-12 05:23:41 -0700430 se->struct_count = handle;
431
432 /* calculate checksums */
433 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
434 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
435 se->intermediate_checksum = table_compute_checksum(istart, isize);
436 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700437 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700438
439 return addr;
440}