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 | |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | enum { |
| 23 | SMBIOS_STR_MAX = 64, /* Maximum length allowed for a string */ |
| 24 | }; |
| 25 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 26 | /** |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 27 | * 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 Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 31 | * @eos: end-of-string pointer for the table being processed. This is set |
| 32 | * up when we start processing a table |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 33 | * @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 Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 36 | * @last_str: points to the last string that was written to the table, or NULL |
| 37 | * if none |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 38 | */ |
| 39 | struct smbios_ctx { |
| 40 | ofnode node; |
| 41 | struct udevice *dev; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 42 | char *eos; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 43 | char *next_ptr; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 44 | char *last_str; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 48 | * 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 Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 52 | * @ctx: context for writing the tables |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 53 | * @return: size of the structure |
| 54 | */ |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 55 | typedef int (*smbios_write_type)(ulong *addr, int handle, |
| 56 | struct smbios_ctx *ctx); |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 59 | * 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 | */ |
| 65 | struct smbios_write_method { |
| 66 | smbios_write_type write; |
| 67 | const char *subnode_name; |
| 68 | }; |
| 69 | |
| 70 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 71 | * 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 Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 76 | * @ctx: SMBIOS context |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 77 | * @str: string to add |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 78 | * @return: string number in the string area (1 or more) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 79 | */ |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 80 | static int smbios_add_string(struct smbios_ctx *ctx, const char *str) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 81 | { |
| 82 | int i = 1; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 83 | char *p = ctx->eos; |
| 84 | |
Heinrich Schuchardt | 00a871d | 2020-06-01 15:44:00 +0200 | [diff] [blame] | 85 | if (!*str) |
| 86 | str = "Unknown"; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 87 | |
| 88 | for (;;) { |
| 89 | if (!*p) { |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 90 | ctx->last_str = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 91 | strcpy(p, str); |
| 92 | p += strlen(str); |
| 93 | *p++ = '\0'; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 94 | ctx->next_ptr = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 95 | *p++ = '\0'; |
| 96 | |
| 97 | return i; |
| 98 | } |
| 99 | |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 100 | if (!strcmp(p, str)) { |
| 101 | ctx->last_str = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 102 | return i; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 103 | } |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 104 | |
| 105 | p += strlen(p) + 1; |
| 106 | i++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 111 | * smbios_add_prop() - Add a property from the device tree |
| 112 | * |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 113 | * @ctx: context for writing the tables |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 114 | * @prop: property to write |
| 115 | * @return 0 if not found, else SMBIOS string number (1 or more) |
| 116 | */ |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 117 | static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop) |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 118 | { |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 119 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
| 120 | const char *str; |
| 121 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 122 | str = ofnode_read_string(ctx->node, prop); |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 123 | if (str) |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 124 | return smbios_add_string(ctx, str); |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return 0; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 130 | static void smbios_set_eos(struct smbios_ctx *ctx, char *eos) |
| 131 | { |
| 132 | ctx->eos = eos; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 133 | ctx->next_ptr = eos; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 134 | ctx->last_str = NULL; |
| 135 | } |
| 136 | |
| 137 | int 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 Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 165 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 166 | * 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 Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 170 | * @ctx: SMBIOS context |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 171 | * @return: string area size |
| 172 | */ |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 173 | static int smbios_string_table_len(const struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 174 | { |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 175 | /* Allow for the final \0 after all strings */ |
| 176 | return (ctx->next_ptr + 1) - ctx->eos; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 179 | static int smbios_write_type0(ulong *current, int handle, |
| 180 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 181 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 182 | struct smbios_type0 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 183 | int len = sizeof(struct smbios_type0); |
| 184 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 185 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 186 | memset(t, 0, sizeof(struct smbios_type0)); |
| 187 | fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 188 | smbios_set_eos(ctx, t->eos); |
| 189 | t->vendor = smbios_add_string(ctx, "U-Boot"); |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 190 | |
| 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 Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 202 | t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE); |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 203 | #ifdef CONFIG_ROM_SIZE |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 204 | t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 205 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 206 | 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 Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 212 | #ifdef CONFIG_EFI_LOADER |
| 213 | t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI; |
| 214 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 215 | t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 216 | |
Simon Glass | 7617f99 | 2021-02-04 21:17:16 -0700 | [diff] [blame] | 217 | /* 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 Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 220 | t->ec_major_release = 0xff; |
| 221 | t->ec_minor_release = 0xff; |
| 222 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 223 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 224 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 225 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 226 | |
| 227 | return len; |
| 228 | } |
| 229 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 230 | static int smbios_write_type1(ulong *current, int handle, |
| 231 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 232 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 233 | struct smbios_type1 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 234 | int len = sizeof(struct smbios_type1); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 235 | char *serial_str = env_get("serial#"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 236 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 237 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 238 | memset(t, 0, sizeof(struct smbios_type1)); |
| 239 | fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 240 | 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 Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 244 | if (serial_str) { |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 245 | t->serial_number = smbios_add_string(ctx, serial_str); |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 246 | strncpy((char *)t->uuid, serial_str, sizeof(t->uuid)); |
| 247 | } else { |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 248 | t->serial_number = smbios_add_prop(ctx, "serial"); |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 249 | } |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 250 | t->sku_number = smbios_add_prop(ctx, "sku"); |
| 251 | t->family = smbios_add_prop(ctx, "family"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 252 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 253 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 254 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 255 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 256 | |
| 257 | return len; |
| 258 | } |
| 259 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 260 | static int smbios_write_type2(ulong *current, int handle, |
| 261 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 262 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 263 | struct smbios_type2 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 264 | int len = sizeof(struct smbios_type2); |
| 265 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 266 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 267 | memset(t, 0, sizeof(struct smbios_type2)); |
| 268 | fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 269 | 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 Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 273 | t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; |
| 274 | t->board_type = SMBIOS_BOARD_MOTHERBOARD; |
| 275 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 276 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 277 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 278 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 279 | |
| 280 | return len; |
| 281 | } |
| 282 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 283 | static int smbios_write_type3(ulong *current, int handle, |
| 284 | struct smbios_ctx *ctx) |
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 | struct smbios_type3 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 287 | int len = sizeof(struct smbios_type3); |
| 288 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 289 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 290 | memset(t, 0, sizeof(struct smbios_type3)); |
| 291 | fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 292 | smbios_set_eos(ctx, t->eos); |
| 293 | t->manufacturer = smbios_add_prop(ctx, "manufacturer"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 294 | 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 Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 300 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 301 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 302 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 303 | |
| 304 | return len; |
| 305 | } |
| 306 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 307 | static void smbios_write_type4_dm(struct smbios_type4 *t, |
| 308 | struct smbios_ctx *ctx) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 309 | { |
| 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 Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 317 | struct udevice *cpu = NULL; |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 318 | |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 319 | uclass_find_first_device(UCLASS_CPU, &cpu); |
| 320 | if (cpu) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 321 | struct cpu_plat *plat = dev_get_parent_plat(cpu); |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 322 | |
| 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 Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 328 | if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 329 | vendor = vendor_name; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 330 | if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 331 | name = processor_name; |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | t->processor_family = processor_family; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 336 | t->processor_manufacturer = smbios_add_string(ctx, vendor); |
| 337 | t->processor_version = smbios_add_string(ctx, name); |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 340 | static int smbios_write_type4(ulong *current, int handle, |
| 341 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 342 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 343 | struct smbios_type4 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 344 | int len = sizeof(struct smbios_type4); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 345 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 346 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 347 | memset(t, 0, sizeof(struct smbios_type4)); |
| 348 | fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 349 | smbios_set_eos(ctx, t->eos); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 350 | t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 351 | smbios_write_type4_dm(t, ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 352 | 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 Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 359 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 360 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 361 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 362 | |
| 363 | return len; |
| 364 | } |
| 365 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 366 | static int smbios_write_type32(ulong *current, int handle, |
| 367 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 368 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 369 | struct smbios_type32 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 370 | int len = sizeof(struct smbios_type32); |
| 371 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 372 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 373 | memset(t, 0, sizeof(struct smbios_type32)); |
| 374 | fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 375 | smbios_set_eos(ctx, t->eos); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 376 | |
| 377 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 378 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 379 | |
| 380 | return len; |
| 381 | } |
| 382 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 383 | static int smbios_write_type127(ulong *current, int handle, |
| 384 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 385 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 386 | struct smbios_type127 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 387 | int len = sizeof(struct smbios_type127); |
| 388 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 389 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 390 | memset(t, 0, sizeof(struct smbios_type127)); |
| 391 | fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); |
| 392 | |
| 393 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 394 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 395 | |
| 396 | return len; |
| 397 | } |
| 398 | |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 399 | static struct smbios_write_method smbios_write_funcs[] = { |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame^] | 400 | { smbios_write_type0, "bios", }, |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 401 | { 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 Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 409 | ulong write_smbios_table(ulong addr) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 410 | { |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 411 | ofnode parent_node = ofnode_null(); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 412 | struct smbios_entry *se; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 413 | struct smbios_ctx ctx; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 414 | ulong table_addr; |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 415 | ulong tables; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 416 | int len = 0; |
| 417 | int max_struct_size = 0; |
| 418 | int handle = 0; |
| 419 | char *istart; |
| 420 | int isize; |
| 421 | int i; |
| 422 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 423 | ctx.node = ofnode_null(); |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 424 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 425 | 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 Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 432 | /* 16 byte align the table address */ |
| 433 | addr = ALIGN(addr, 16); |
| 434 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 435 | se = map_sysmem(addr, sizeof(struct smbios_entry)); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 436 | 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 Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 444 | const struct smbios_write_method *method; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 445 | int tmp; |
| 446 | |
| 447 | method = &smbios_write_funcs[i]; |
| 448 | if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name) |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 449 | ctx.node = ofnode_find_subnode(parent_node, |
| 450 | method->subnode_name); |
| 451 | tmp = method->write((ulong *)&addr, handle++, &ctx); |
Christian Gmeiner | 60a4df3 | 2018-07-30 13:22:07 +0200 | [diff] [blame] | 452 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 453 | 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 Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 464 | |
| 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 Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 482 | 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 Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 489 | unmap_sysmem(se); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 490 | |
| 491 | return addr; |
| 492 | } |