Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
| 4 | * |
| 5 | * Adapted from coreboot src/arch/x86/smbios.c |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 11 | #include <mapmem.h> |
Alexander Graf | 4b6dddc | 2016-08-19 01:23:23 +0200 | [diff] [blame] | 12 | #include <smbios.h> |
| 13 | #include <tables_csum.h> |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 14 | #include <version.h> |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 15 | #ifdef CONFIG_CPU |
| 16 | #include <cpu.h> |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
| 18 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 19 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 20 | /** |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 21 | * 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) |
| 25 | */ |
| 26 | struct smbios_ctx { |
| 27 | ofnode node; |
| 28 | struct udevice *dev; |
| 29 | }; |
| 30 | |
| 31 | /** |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 32 | * Function prototype to write a specific type of SMBIOS structure |
| 33 | * |
| 34 | * @addr: start address to write the structure |
| 35 | * @handle: the structure's handle, a unique 16-bit number |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 36 | * @ctx: context for writing the tables |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 37 | * @return: size of the structure |
| 38 | */ |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 39 | typedef int (*smbios_write_type)(ulong *addr, int handle, |
| 40 | struct smbios_ctx *ctx); |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 43 | * struct smbios_write_method - Information about a table-writing function |
| 44 | * |
| 45 | * @write: Function to call |
| 46 | * @subnode_name: Name of subnode which has the information for this function, |
| 47 | * NULL if none |
| 48 | */ |
| 49 | struct smbios_write_method { |
| 50 | smbios_write_type write; |
| 51 | const char *subnode_name; |
| 52 | }; |
| 53 | |
| 54 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 55 | * smbios_add_string() - add a string to the string area |
| 56 | * |
| 57 | * This adds a string to the string area which is appended directly after |
| 58 | * the formatted portion of an SMBIOS structure. |
| 59 | * |
| 60 | * @start: string area start address |
| 61 | * @str: string to add |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 62 | * @return: string number in the string area (1 or more) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 63 | */ |
| 64 | static int smbios_add_string(char *start, const char *str) |
| 65 | { |
| 66 | int i = 1; |
| 67 | char *p = start; |
Heinrich Schuchardt | 00a871d | 2020-06-01 15:44:00 +0200 | [diff] [blame] | 68 | if (!*str) |
| 69 | str = "Unknown"; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 70 | |
| 71 | for (;;) { |
| 72 | if (!*p) { |
| 73 | strcpy(p, str); |
| 74 | p += strlen(str); |
| 75 | *p++ = '\0'; |
| 76 | *p++ = '\0'; |
| 77 | |
| 78 | return i; |
| 79 | } |
| 80 | |
| 81 | if (!strcmp(p, str)) |
| 82 | return i; |
| 83 | |
| 84 | p += strlen(p) + 1; |
| 85 | i++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 90 | * smbios_add_prop() - Add a property from the device tree |
| 91 | * |
| 92 | * @start: string area start address |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 93 | * @ctx: context for writing the tables |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 94 | * @prop: property to write |
| 95 | * @return 0 if not found, else SMBIOS string number (1 or more) |
| 96 | */ |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 97 | static int smbios_add_prop(char *start, struct smbios_ctx *ctx, |
| 98 | const char *prop) |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 99 | { |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 100 | |
| 101 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
| 102 | const char *str; |
| 103 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 104 | str = ofnode_read_string(ctx->node, prop); |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 105 | if (str) |
| 106 | return smbios_add_string(start, str); |
| 107 | } |
| 108 | |
| 109 | return 0; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 113 | * smbios_string_table_len() - compute the string area size |
| 114 | * |
| 115 | * This computes the size of the string area including the string terminator. |
| 116 | * |
| 117 | * @start: string area start address |
| 118 | * @return: string area size |
| 119 | */ |
| 120 | static int smbios_string_table_len(char *start) |
| 121 | { |
| 122 | char *p = start; |
| 123 | int i, len = 0; |
| 124 | |
| 125 | while (*p) { |
| 126 | i = strlen(p) + 1; |
| 127 | p += i; |
| 128 | len += i; |
| 129 | } |
| 130 | |
| 131 | return len + 1; |
| 132 | } |
| 133 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 134 | static int smbios_write_type0(ulong *current, int handle, |
| 135 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 136 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 137 | struct smbios_type0 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 138 | int len = sizeof(struct smbios_type0); |
| 139 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 140 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 141 | memset(t, 0, sizeof(struct smbios_type0)); |
| 142 | fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); |
| 143 | t->vendor = smbios_add_string(t->eos, "U-Boot"); |
| 144 | t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION); |
| 145 | t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE); |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 146 | #ifdef CONFIG_ROM_SIZE |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 147 | t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 148 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 149 | t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED | |
| 150 | BIOS_CHARACTERISTICS_SELECTABLE_BOOT | |
| 151 | BIOS_CHARACTERISTICS_UPGRADEABLE; |
| 152 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 153 | t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI; |
| 154 | #endif |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 155 | #ifdef CONFIG_EFI_LOADER |
| 156 | t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI; |
| 157 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 158 | t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 159 | |
Simon Glass | 7617f99 | 2021-02-04 21:17:16 -0700 | [diff] [blame] | 160 | /* bios_major_release has only one byte, so drop century */ |
| 161 | t->bios_major_release = U_BOOT_VERSION_NUM % 100; |
| 162 | t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 163 | t->ec_major_release = 0xff; |
| 164 | t->ec_minor_release = 0xff; |
| 165 | |
| 166 | len = t->length + smbios_string_table_len(t->eos); |
| 167 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 168 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 169 | |
| 170 | return len; |
| 171 | } |
| 172 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 173 | static int smbios_write_type1(ulong *current, int handle, |
| 174 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 175 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 176 | struct smbios_type1 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 177 | int len = sizeof(struct smbios_type1); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 178 | char *serial_str = env_get("serial#"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 179 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 180 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 181 | memset(t, 0, sizeof(struct smbios_type1)); |
| 182 | fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 183 | t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer"); |
| 184 | t->product_name = smbios_add_prop(t->eos, ctx, "product"); |
| 185 | t->version = smbios_add_prop(t->eos, ctx, "version"); |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 186 | if (serial_str) { |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 187 | t->serial_number = smbios_add_string(t->eos, serial_str); |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 188 | strncpy((char *)t->uuid, serial_str, sizeof(t->uuid)); |
| 189 | } else { |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 190 | t->serial_number = smbios_add_prop(t->eos, ctx, "serial"); |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 191 | } |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 192 | t->sku_number = smbios_add_prop(t->eos, ctx, "sku"); |
| 193 | t->family = smbios_add_prop(t->eos, ctx, "family"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 194 | |
| 195 | len = t->length + smbios_string_table_len(t->eos); |
| 196 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 197 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 198 | |
| 199 | return len; |
| 200 | } |
| 201 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 202 | static int smbios_write_type2(ulong *current, int handle, |
| 203 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 204 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 205 | struct smbios_type2 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 206 | int len = sizeof(struct smbios_type2); |
| 207 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 208 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 209 | memset(t, 0, sizeof(struct smbios_type2)); |
| 210 | fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 211 | t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer"); |
| 212 | t->product_name = smbios_add_prop(t->eos, ctx, "product"); |
| 213 | t->asset_tag_number = smbios_add_prop(t->eos, ctx, "asset-tag"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 214 | t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; |
| 215 | t->board_type = SMBIOS_BOARD_MOTHERBOARD; |
| 216 | |
| 217 | len = t->length + smbios_string_table_len(t->eos); |
| 218 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 219 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 220 | |
| 221 | return len; |
| 222 | } |
| 223 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 224 | static int smbios_write_type3(ulong *current, int handle, |
| 225 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 226 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 227 | struct smbios_type3 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 228 | int len = sizeof(struct smbios_type3); |
| 229 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 230 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 231 | memset(t, 0, sizeof(struct smbios_type3)); |
| 232 | fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 233 | t->manufacturer = smbios_add_prop(t->eos, ctx, "manufacturer"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 234 | t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP; |
| 235 | t->bootup_state = SMBIOS_STATE_SAFE; |
| 236 | t->power_supply_state = SMBIOS_STATE_SAFE; |
| 237 | t->thermal_state = SMBIOS_STATE_SAFE; |
| 238 | t->security_status = SMBIOS_SECURITY_NONE; |
| 239 | |
| 240 | len = t->length + smbios_string_table_len(t->eos); |
| 241 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 242 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 243 | |
| 244 | return len; |
| 245 | } |
| 246 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 247 | static void smbios_write_type4_dm(struct smbios_type4 *t, |
| 248 | struct smbios_ctx *ctx) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 249 | { |
| 250 | u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN; |
| 251 | const char *vendor = "Unknown"; |
| 252 | const char *name = "Unknown"; |
| 253 | |
| 254 | #ifdef CONFIG_CPU |
| 255 | char processor_name[49]; |
| 256 | char vendor_name[49]; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 257 | struct udevice *cpu = NULL; |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 258 | |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 259 | uclass_find_first_device(UCLASS_CPU, &cpu); |
| 260 | if (cpu) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 261 | struct cpu_plat *plat = dev_get_parent_plat(cpu); |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 262 | |
| 263 | if (plat->family) |
| 264 | processor_family = plat->family; |
| 265 | t->processor_id[0] = plat->id[0]; |
| 266 | t->processor_id[1] = plat->id[1]; |
| 267 | |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 268 | if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 269 | vendor = vendor_name; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 270 | if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 271 | name = processor_name; |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | t->processor_family = processor_family; |
| 276 | t->processor_manufacturer = smbios_add_string(t->eos, vendor); |
| 277 | t->processor_version = smbios_add_string(t->eos, name); |
| 278 | } |
| 279 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 280 | static int smbios_write_type4(ulong *current, int handle, |
| 281 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 282 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 283 | struct smbios_type4 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 284 | int len = sizeof(struct smbios_type4); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 285 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 286 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 287 | memset(t, 0, sizeof(struct smbios_type4)); |
| 288 | fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); |
| 289 | t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 290 | smbios_write_type4_dm(t, ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 291 | t->status = SMBIOS_PROCESSOR_STATUS_ENABLED; |
| 292 | t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE; |
| 293 | t->l1_cache_handle = 0xffff; |
| 294 | t->l2_cache_handle = 0xffff; |
| 295 | t->l3_cache_handle = 0xffff; |
| 296 | t->processor_family2 = t->processor_family; |
| 297 | |
| 298 | len = t->length + smbios_string_table_len(t->eos); |
| 299 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 300 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 301 | |
| 302 | return len; |
| 303 | } |
| 304 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 305 | static int smbios_write_type32(ulong *current, int handle, |
| 306 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 307 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 308 | struct smbios_type32 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 309 | int len = sizeof(struct smbios_type32); |
| 310 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 311 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 312 | memset(t, 0, sizeof(struct smbios_type32)); |
| 313 | fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); |
| 314 | |
| 315 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 316 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 317 | |
| 318 | return len; |
| 319 | } |
| 320 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 321 | static int smbios_write_type127(ulong *current, int handle, |
| 322 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 323 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 324 | struct smbios_type127 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 325 | int len = sizeof(struct smbios_type127); |
| 326 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 327 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 328 | memset(t, 0, sizeof(struct smbios_type127)); |
| 329 | fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); |
| 330 | |
| 331 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 332 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 333 | |
| 334 | return len; |
| 335 | } |
| 336 | |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 337 | static struct smbios_write_method smbios_write_funcs[] = { |
| 338 | { smbios_write_type0, }, |
| 339 | { smbios_write_type1, "system", }, |
| 340 | { smbios_write_type2, "baseboard", }, |
| 341 | { smbios_write_type3, "chassis", }, |
| 342 | { smbios_write_type4, }, |
| 343 | { smbios_write_type32, }, |
| 344 | { smbios_write_type127 }, |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 345 | }; |
| 346 | |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 347 | ulong write_smbios_table(ulong addr) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 348 | { |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 349 | ofnode parent_node = ofnode_null(); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 350 | struct smbios_entry *se; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 351 | struct smbios_ctx ctx; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 352 | ulong table_addr; |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 353 | ulong tables; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 354 | int len = 0; |
| 355 | int max_struct_size = 0; |
| 356 | int handle = 0; |
| 357 | char *istart; |
| 358 | int isize; |
| 359 | int i; |
| 360 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 361 | ctx.node = ofnode_null(); |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 362 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 363 | uclass_first_device(UCLASS_SYSINFO, &ctx.dev); |
| 364 | if (ctx.dev) |
| 365 | parent_node = dev_read_subnode(ctx.dev, "smbios"); |
| 366 | } else { |
| 367 | ctx.dev = NULL; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 370 | /* 16 byte align the table address */ |
| 371 | addr = ALIGN(addr, 16); |
| 372 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 373 | se = map_sysmem(addr, sizeof(struct smbios_entry)); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 374 | memset(se, 0, sizeof(struct smbios_entry)); |
| 375 | |
| 376 | addr += sizeof(struct smbios_entry); |
| 377 | addr = ALIGN(addr, 16); |
| 378 | tables = addr; |
| 379 | |
| 380 | /* populate minimum required tables */ |
| 381 | for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) { |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 382 | const struct smbios_write_method *method; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 383 | int tmp; |
| 384 | |
| 385 | method = &smbios_write_funcs[i]; |
| 386 | if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name) |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame^] | 387 | ctx.node = ofnode_find_subnode(parent_node, |
| 388 | method->subnode_name); |
| 389 | tmp = method->write((ulong *)&addr, handle++, &ctx); |
Christian Gmeiner | 60a4df3 | 2018-07-30 13:22:07 +0200 | [diff] [blame] | 390 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 391 | max_struct_size = max(max_struct_size, tmp); |
| 392 | len += tmp; |
| 393 | } |
| 394 | |
| 395 | memcpy(se->anchor, "_SM_", 4); |
| 396 | se->length = sizeof(struct smbios_entry); |
| 397 | se->major_ver = SMBIOS_MAJOR_VER; |
| 398 | se->minor_ver = SMBIOS_MINOR_VER; |
| 399 | se->max_struct_size = max_struct_size; |
| 400 | memcpy(se->intermediate_anchor, "_DMI_", 5); |
| 401 | se->struct_table_length = len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * We must use a pointer here so things work correctly on sandbox. The |
| 405 | * user of this table is not aware of the mapping of addresses to |
| 406 | * sandbox's DRAM buffer. |
| 407 | */ |
| 408 | table_addr = (ulong)map_sysmem(tables, 0); |
| 409 | if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) { |
| 410 | /* |
| 411 | * We need to put this >32-bit pointer into the table but the |
| 412 | * field is only 32 bits wide. |
| 413 | */ |
| 414 | printf("WARNING: SMBIOS table_address overflow %llx\n", |
| 415 | (unsigned long long)table_addr); |
| 416 | table_addr = 0; |
| 417 | } |
| 418 | se->struct_table_address = table_addr; |
| 419 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 420 | se->struct_count = handle; |
| 421 | |
| 422 | /* calculate checksums */ |
| 423 | istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET; |
| 424 | isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET; |
| 425 | se->intermediate_checksum = table_compute_checksum(istart, isize); |
| 426 | se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry)); |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 427 | unmap_sysmem(se); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 428 | |
| 429 | return addr; |
| 430 | } |