blob: e8bfc9ee1ce56de06bac28bc64e8151d4f4049ec [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
Simon Glasse9adaa72021-02-04 21:17:20 -070020DECLARE_GLOBAL_DATA_PTR;
21
22enum {
23 SMBIOS_STR_MAX = 64, /* Maximum length allowed for a string */
24};
25
Bin Meng721e9922015-10-12 05:23:41 -070026/**
Simon Glass1e8989a2021-02-04 21:17:17 -070027 * struct smbios_ctx - context for writing SMBIOS tables
28 *
29 * @node: node containing the information to write (ofnode_null() if none)
30 * @dev: sysinfo device to use (NULL if none)
Simon Glass0c95fff2021-02-04 21:17:18 -070031 * @eos: end-of-string pointer for the table being processed. This is set
32 * up when we start processing a table
Simon Glassfd3b8262021-02-04 21:17:19 -070033 * @next_ptr: pointer to the start of the next string to be added. When the
34 * table is nopt empty, this points to the byte after the \0 of the
35 * previous string.
Simon Glasse9adaa72021-02-04 21:17:20 -070036 * @last_str: points to the last string that was written to the table, or NULL
37 * if none
Simon Glass1e8989a2021-02-04 21:17:17 -070038 */
39struct smbios_ctx {
40 ofnode node;
41 struct udevice *dev;
Simon Glass0c95fff2021-02-04 21:17:18 -070042 char *eos;
Simon Glassfd3b8262021-02-04 21:17:19 -070043 char *next_ptr;
Simon Glasse9adaa72021-02-04 21:17:20 -070044 char *last_str;
Simon Glass1e8989a2021-02-04 21:17:17 -070045};
46
47/**
Simon Glass0e89b852021-02-04 21:17:14 -070048 * Function prototype to write a specific type of SMBIOS structure
49 *
50 * @addr: start address to write the structure
51 * @handle: the structure's handle, a unique 16-bit number
Simon Glass1e8989a2021-02-04 21:17:17 -070052 * @ctx: context for writing the tables
Simon Glass0e89b852021-02-04 21:17:14 -070053 * @return: size of the structure
54 */
Simon Glass1e8989a2021-02-04 21:17:17 -070055typedef int (*smbios_write_type)(ulong *addr, int handle,
56 struct smbios_ctx *ctx);
Simon Glass0e89b852021-02-04 21:17:14 -070057
58/**
Simon Glass44ffb6f2020-11-05 06:32:08 -070059 * struct smbios_write_method - Information about a table-writing function
60 *
61 * @write: Function to call
62 * @subnode_name: Name of subnode which has the information for this function,
63 * NULL if none
64 */
65struct smbios_write_method {
66 smbios_write_type write;
67 const char *subnode_name;
68};
69
70/**
Bin Meng721e9922015-10-12 05:23:41 -070071 * smbios_add_string() - add a string to the string area
72 *
73 * This adds a string to the string area which is appended directly after
74 * the formatted portion of an SMBIOS structure.
75 *
Simon Glass0c95fff2021-02-04 21:17:18 -070076 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -070077 * @str: string to add
Simon Glass78227d42020-11-05 06:32:07 -070078 * @return: string number in the string area (1 or more)
Bin Meng721e9922015-10-12 05:23:41 -070079 */
Simon Glass0c95fff2021-02-04 21:17:18 -070080static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
Bin Meng721e9922015-10-12 05:23:41 -070081{
82 int i = 1;
Simon Glass0c95fff2021-02-04 21:17:18 -070083 char *p = ctx->eos;
84
Heinrich Schuchardt00a871d2020-06-01 15:44:00 +020085 if (!*str)
86 str = "Unknown";
Bin Meng721e9922015-10-12 05:23:41 -070087
88 for (;;) {
89 if (!*p) {
Simon Glasse9adaa72021-02-04 21:17:20 -070090 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -070091 strcpy(p, str);
92 p += strlen(str);
93 *p++ = '\0';
Simon Glassfd3b8262021-02-04 21:17:19 -070094 ctx->next_ptr = p;
Bin Meng721e9922015-10-12 05:23:41 -070095 *p++ = '\0';
96
97 return i;
98 }
99
Simon Glasse9adaa72021-02-04 21:17:20 -0700100 if (!strcmp(p, str)) {
101 ctx->last_str = p;
Bin Meng721e9922015-10-12 05:23:41 -0700102 return i;
Simon Glasse9adaa72021-02-04 21:17:20 -0700103 }
Bin Meng721e9922015-10-12 05:23:41 -0700104
105 p += strlen(p) + 1;
106 i++;
107 }
108}
109
110/**
Simon Glass44ffb6f2020-11-05 06:32:08 -0700111 * smbios_add_prop() - Add a property from the device tree
112 *
Simon Glass1e8989a2021-02-04 21:17:17 -0700113 * @ctx: context for writing the tables
Simon Glass44ffb6f2020-11-05 06:32:08 -0700114 * @prop: property to write
115 * @return 0 if not found, else SMBIOS string number (1 or more)
116 */
Simon Glass0c95fff2021-02-04 21:17:18 -0700117static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
Simon Glass44ffb6f2020-11-05 06:32:08 -0700118{
Simon Glasse4f8e542020-11-05 06:32:18 -0700119 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
120 const char *str;
121
Simon Glass1e8989a2021-02-04 21:17:17 -0700122 str = ofnode_read_string(ctx->node, prop);
Simon Glasse4f8e542020-11-05 06:32:18 -0700123 if (str)
Simon Glass0c95fff2021-02-04 21:17:18 -0700124 return smbios_add_string(ctx, str);
Simon Glasse4f8e542020-11-05 06:32:18 -0700125 }
126
127 return 0;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700128}
129
Simon Glass0c95fff2021-02-04 21:17:18 -0700130static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
131{
132 ctx->eos = eos;
Simon Glassfd3b8262021-02-04 21:17:19 -0700133 ctx->next_ptr = eos;
Simon Glasse9adaa72021-02-04 21:17:20 -0700134 ctx->last_str = NULL;
135}
136
137int smbios_update_version(const char *version)
138{
139 char *ptr = gd->smbios_version;
140 uint old_len, len;
141
142 if (!ptr)
143 return log_ret(-ENOENT);
144
145 /*
146 * This string is supposed to have at least enough bytes and is
147 * padded with spaces. Update it, taking care not to move the
148 * \0 terminator, so that other strings in the string table
149 * are not disturbed. See smbios_add_string()
150 */
151 old_len = strnlen(ptr, SMBIOS_STR_MAX);
152 len = strnlen(version, SMBIOS_STR_MAX);
153 if (len > old_len)
154 return log_ret(-ENOSPC);
155
156 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
157 memcpy(ptr, version, len);
158#ifdef LOG_DEBUG
159 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
160#endif
161
162 return 0;
Simon Glass0c95fff2021-02-04 21:17:18 -0700163}
164
Simon Glass44ffb6f2020-11-05 06:32:08 -0700165/**
Bin Meng721e9922015-10-12 05:23:41 -0700166 * smbios_string_table_len() - compute the string area size
167 *
168 * This computes the size of the string area including the string terminator.
169 *
Simon Glassfd3b8262021-02-04 21:17:19 -0700170 * @ctx: SMBIOS context
Bin Meng721e9922015-10-12 05:23:41 -0700171 * @return: string area size
172 */
Simon Glassfd3b8262021-02-04 21:17:19 -0700173static int smbios_string_table_len(const struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700174{
Simon Glassfd3b8262021-02-04 21:17:19 -0700175 /* Allow for the final \0 after all strings */
176 return (ctx->next_ptr + 1) - ctx->eos;
Bin Meng721e9922015-10-12 05:23:41 -0700177}
178
Simon Glass1e8989a2021-02-04 21:17:17 -0700179static int smbios_write_type0(ulong *current, int handle,
180 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700181{
Simon Glassa2505fc2018-11-22 13:46:37 -0700182 struct smbios_type0 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700183 int len = sizeof(struct smbios_type0);
184
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_type0));
187 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700188 smbios_set_eos(ctx, t->eos);
189 t->vendor = smbios_add_string(ctx, "U-Boot");
Simon Glasse9adaa72021-02-04 21:17:20 -0700190
191 t->bios_ver = smbios_add_prop(ctx, "version");
192 if (!t->bios_ver)
193 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
194 if (t->bios_ver)
195 gd->smbios_version = ctx->last_str;
196 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
197 gd->smbios_version);
198#ifdef LOG_DEBUG
199 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
200 1, strlen(gd->smbios_version) + 1, 0);
201#endif
Simon Glass0c95fff2021-02-04 21:17:18 -0700202 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
Alexander Grafe663b352016-08-19 01:23:29 +0200203#ifdef CONFIG_ROM_SIZE
Bin Meng721e9922015-10-12 05:23:41 -0700204 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
Alexander Grafe663b352016-08-19 01:23:29 +0200205#endif
Bin Meng721e9922015-10-12 05:23:41 -0700206 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
207 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
208 BIOS_CHARACTERISTICS_UPGRADEABLE;
209#ifdef CONFIG_GENERATE_ACPI_TABLE
210 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
211#endif
Alexander Grafe663b352016-08-19 01:23:29 +0200212#ifdef CONFIG_EFI_LOADER
213 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
214#endif
Bin Meng721e9922015-10-12 05:23:41 -0700215 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
Alexander Grafe663b352016-08-19 01:23:29 +0200216
Simon Glass7617f992021-02-04 21:17:16 -0700217 /* bios_major_release has only one byte, so drop century */
218 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
219 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
Bin Meng721e9922015-10-12 05:23:41 -0700220 t->ec_major_release = 0xff;
221 t->ec_minor_release = 0xff;
222
Simon Glassfd3b8262021-02-04 21:17:19 -0700223 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700224 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700225 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700226
227 return len;
228}
229
Simon Glass1e8989a2021-02-04 21:17:17 -0700230static int smbios_write_type1(ulong *current, int handle,
231 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700232{
Simon Glassa2505fc2018-11-22 13:46:37 -0700233 struct smbios_type1 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700234 int len = sizeof(struct smbios_type1);
Simon Glass00caae62017-08-03 12:22:12 -0600235 char *serial_str = env_get("serial#");
Bin Meng721e9922015-10-12 05:23:41 -0700236
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_type1));
239 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700240 smbios_set_eos(ctx, t->eos);
241 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
242 t->product_name = smbios_add_prop(ctx, "product");
243 t->version = smbios_add_prop(ctx, "version");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200244 if (serial_str) {
Simon Glass0c95fff2021-02-04 21:17:18 -0700245 t->serial_number = smbios_add_string(ctx, serial_str);
Simon Glass44ffb6f2020-11-05 06:32:08 -0700246 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
247 } else {
Simon Glass0c95fff2021-02-04 21:17:18 -0700248 t->serial_number = smbios_add_prop(ctx, "serial");
Alexander Graf6fb580d2016-08-19 01:23:31 +0200249 }
Simon Glass0c95fff2021-02-04 21:17:18 -0700250 t->sku_number = smbios_add_prop(ctx, "sku");
251 t->family = smbios_add_prop(ctx, "family");
Bin Meng721e9922015-10-12 05:23:41 -0700252
Simon Glassfd3b8262021-02-04 21:17:19 -0700253 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700254 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700255 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700256
257 return len;
258}
259
Simon Glass1e8989a2021-02-04 21:17:17 -0700260static int smbios_write_type2(ulong *current, int handle,
261 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700262{
Simon Glassa2505fc2018-11-22 13:46:37 -0700263 struct smbios_type2 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700264 int len = sizeof(struct smbios_type2);
265
Simon Glassa2505fc2018-11-22 13:46:37 -0700266 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700267 memset(t, 0, sizeof(struct smbios_type2));
268 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700269 smbios_set_eos(ctx, t->eos);
270 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
271 t->product_name = smbios_add_prop(ctx, "product");
272 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
Bin Meng721e9922015-10-12 05:23:41 -0700273 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
274 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
275
Simon Glassfd3b8262021-02-04 21:17:19 -0700276 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700277 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700278 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700279
280 return len;
281}
282
Simon Glass1e8989a2021-02-04 21:17:17 -0700283static int smbios_write_type3(ulong *current, int handle,
284 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700285{
Simon Glassa2505fc2018-11-22 13:46:37 -0700286 struct smbios_type3 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700287 int len = sizeof(struct smbios_type3);
288
Simon Glassa2505fc2018-11-22 13:46:37 -0700289 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700290 memset(t, 0, sizeof(struct smbios_type3));
291 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700292 smbios_set_eos(ctx, t->eos);
293 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
Bin Meng721e9922015-10-12 05:23:41 -0700294 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
295 t->bootup_state = SMBIOS_STATE_SAFE;
296 t->power_supply_state = SMBIOS_STATE_SAFE;
297 t->thermal_state = SMBIOS_STATE_SAFE;
298 t->security_status = SMBIOS_SECURITY_NONE;
299
Simon Glassfd3b8262021-02-04 21:17:19 -0700300 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700301 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700302 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700303
304 return len;
305}
306
Simon Glass1e8989a2021-02-04 21:17:17 -0700307static void smbios_write_type4_dm(struct smbios_type4 *t,
308 struct smbios_ctx *ctx)
Alexander Graf96476202016-08-19 01:23:28 +0200309{
310 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
311 const char *vendor = "Unknown";
312 const char *name = "Unknown";
313
314#ifdef CONFIG_CPU
315 char processor_name[49];
316 char vendor_name[49];
Simon Glass78227d42020-11-05 06:32:07 -0700317 struct udevice *cpu = NULL;
Alexander Graf96476202016-08-19 01:23:28 +0200318
Simon Glass78227d42020-11-05 06:32:07 -0700319 uclass_find_first_device(UCLASS_CPU, &cpu);
320 if (cpu) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700321 struct cpu_plat *plat = dev_get_parent_plat(cpu);
Alexander Graf96476202016-08-19 01:23:28 +0200322
323 if (plat->family)
324 processor_family = plat->family;
325 t->processor_id[0] = plat->id[0];
326 t->processor_id[1] = plat->id[1];
327
Simon Glass78227d42020-11-05 06:32:07 -0700328 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200329 vendor = vendor_name;
Simon Glass78227d42020-11-05 06:32:07 -0700330 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
Alexander Graf96476202016-08-19 01:23:28 +0200331 name = processor_name;
332 }
333#endif
334
335 t->processor_family = processor_family;
Simon Glass0c95fff2021-02-04 21:17:18 -0700336 t->processor_manufacturer = smbios_add_string(ctx, vendor);
337 t->processor_version = smbios_add_string(ctx, name);
Alexander Graf96476202016-08-19 01:23:28 +0200338}
339
Simon Glass1e8989a2021-02-04 21:17:17 -0700340static int smbios_write_type4(ulong *current, int handle,
341 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700342{
Simon Glassa2505fc2018-11-22 13:46:37 -0700343 struct smbios_type4 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700344 int len = sizeof(struct smbios_type4);
Bin Meng721e9922015-10-12 05:23:41 -0700345
Simon Glassa2505fc2018-11-22 13:46:37 -0700346 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700347 memset(t, 0, sizeof(struct smbios_type4));
348 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700349 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700350 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
Simon Glass1e8989a2021-02-04 21:17:17 -0700351 smbios_write_type4_dm(t, ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700352 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
353 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
354 t->l1_cache_handle = 0xffff;
355 t->l2_cache_handle = 0xffff;
356 t->l3_cache_handle = 0xffff;
357 t->processor_family2 = t->processor_family;
358
Simon Glassfd3b8262021-02-04 21:17:19 -0700359 len = t->length + smbios_string_table_len(ctx);
Bin Meng721e9922015-10-12 05:23:41 -0700360 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700361 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700362
363 return len;
364}
365
Simon Glass1e8989a2021-02-04 21:17:17 -0700366static int smbios_write_type32(ulong *current, int handle,
367 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700368{
Simon Glassa2505fc2018-11-22 13:46:37 -0700369 struct smbios_type32 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700370 int len = sizeof(struct smbios_type32);
371
Simon Glassa2505fc2018-11-22 13:46:37 -0700372 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700373 memset(t, 0, sizeof(struct smbios_type32));
374 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
Simon Glass0c95fff2021-02-04 21:17:18 -0700375 smbios_set_eos(ctx, t->eos);
Bin Meng721e9922015-10-12 05:23:41 -0700376
377 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700378 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700379
380 return len;
381}
382
Simon Glass1e8989a2021-02-04 21:17:17 -0700383static int smbios_write_type127(ulong *current, int handle,
384 struct smbios_ctx *ctx)
Bin Meng721e9922015-10-12 05:23:41 -0700385{
Simon Glassa2505fc2018-11-22 13:46:37 -0700386 struct smbios_type127 *t;
Bin Meng721e9922015-10-12 05:23:41 -0700387 int len = sizeof(struct smbios_type127);
388
Simon Glassa2505fc2018-11-22 13:46:37 -0700389 t = map_sysmem(*current, len);
Bin Meng721e9922015-10-12 05:23:41 -0700390 memset(t, 0, sizeof(struct smbios_type127));
391 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
392
393 *current += len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700394 unmap_sysmem(t);
Bin Meng721e9922015-10-12 05:23:41 -0700395
396 return len;
397}
398
Simon Glass44ffb6f2020-11-05 06:32:08 -0700399static struct smbios_write_method smbios_write_funcs[] = {
Simon Glasse9adaa72021-02-04 21:17:20 -0700400 { smbios_write_type0, "bios", },
Simon Glass44ffb6f2020-11-05 06:32:08 -0700401 { smbios_write_type1, "system", },
402 { smbios_write_type2, "baseboard", },
403 { smbios_write_type3, "chassis", },
404 { smbios_write_type4, },
405 { smbios_write_type32, },
406 { smbios_write_type127 },
Bin Meng721e9922015-10-12 05:23:41 -0700407};
408
Simon Glass42fd8c12017-01-16 07:03:35 -0700409ulong write_smbios_table(ulong addr)
Bin Meng721e9922015-10-12 05:23:41 -0700410{
Simon Glass44ffb6f2020-11-05 06:32:08 -0700411 ofnode parent_node = ofnode_null();
Bin Meng721e9922015-10-12 05:23:41 -0700412 struct smbios_entry *se;
Simon Glass1e8989a2021-02-04 21:17:17 -0700413 struct smbios_ctx ctx;
Simon Glassa2505fc2018-11-22 13:46:37 -0700414 ulong table_addr;
Simon Glass42fd8c12017-01-16 07:03:35 -0700415 ulong tables;
Bin Meng721e9922015-10-12 05:23:41 -0700416 int len = 0;
417 int max_struct_size = 0;
418 int handle = 0;
419 char *istart;
420 int isize;
421 int i;
422
Simon Glass1e8989a2021-02-04 21:17:17 -0700423 ctx.node = ofnode_null();
Simon Glass78227d42020-11-05 06:32:07 -0700424 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
Simon Glass1e8989a2021-02-04 21:17:17 -0700425 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
426 if (ctx.dev)
427 parent_node = dev_read_subnode(ctx.dev, "smbios");
428 } else {
429 ctx.dev = NULL;
Simon Glass78227d42020-11-05 06:32:07 -0700430 }
431
Bin Meng721e9922015-10-12 05:23:41 -0700432 /* 16 byte align the table address */
433 addr = ALIGN(addr, 16);
434
Simon Glassa2505fc2018-11-22 13:46:37 -0700435 se = map_sysmem(addr, sizeof(struct smbios_entry));
Bin Meng721e9922015-10-12 05:23:41 -0700436 memset(se, 0, sizeof(struct smbios_entry));
437
438 addr += sizeof(struct smbios_entry);
439 addr = ALIGN(addr, 16);
440 tables = addr;
441
442 /* populate minimum required tables */
443 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
Simon Glass44ffb6f2020-11-05 06:32:08 -0700444 const struct smbios_write_method *method;
Simon Glass44ffb6f2020-11-05 06:32:08 -0700445 int tmp;
446
447 method = &smbios_write_funcs[i];
448 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
Simon Glass1e8989a2021-02-04 21:17:17 -0700449 ctx.node = ofnode_find_subnode(parent_node,
450 method->subnode_name);
451 tmp = method->write((ulong *)&addr, handle++, &ctx);
Christian Gmeiner60a4df32018-07-30 13:22:07 +0200452
Bin Meng721e9922015-10-12 05:23:41 -0700453 max_struct_size = max(max_struct_size, tmp);
454 len += tmp;
455 }
456
457 memcpy(se->anchor, "_SM_", 4);
458 se->length = sizeof(struct smbios_entry);
459 se->major_ver = SMBIOS_MAJOR_VER;
460 se->minor_ver = SMBIOS_MINOR_VER;
461 se->max_struct_size = max_struct_size;
462 memcpy(se->intermediate_anchor, "_DMI_", 5);
463 se->struct_table_length = len;
Simon Glassa2505fc2018-11-22 13:46:37 -0700464
465 /*
466 * We must use a pointer here so things work correctly on sandbox. The
467 * user of this table is not aware of the mapping of addresses to
468 * sandbox's DRAM buffer.
469 */
470 table_addr = (ulong)map_sysmem(tables, 0);
471 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
472 /*
473 * We need to put this >32-bit pointer into the table but the
474 * field is only 32 bits wide.
475 */
476 printf("WARNING: SMBIOS table_address overflow %llx\n",
477 (unsigned long long)table_addr);
478 table_addr = 0;
479 }
480 se->struct_table_address = table_addr;
481
Bin Meng721e9922015-10-12 05:23:41 -0700482 se->struct_count = handle;
483
484 /* calculate checksums */
485 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
486 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
487 se->intermediate_checksum = table_compute_checksum(istart, isize);
488 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
Simon Glassa2505fc2018-11-22 13:46:37 -0700489 unmap_sysmem(se);
Bin Meng721e9922015-10-12 05:23:41 -0700490
491 return addr;
492}