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> |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 13 | #include <sysinfo.h> |
Alexander Graf | 4b6dddc | 2016-08-19 01:23:23 +0200 | [diff] [blame] | 14 | #include <tables_csum.h> |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 15 | #include <version.h> |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 16 | #ifdef CONFIG_CPU |
| 17 | #include <cpu.h> |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 18 | #include <dm/uclass-internal.h> |
| 19 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 20 | |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 23 | /** |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 24 | * struct smbios_ctx - context for writing SMBIOS tables |
| 25 | * |
| 26 | * @node: node containing the information to write (ofnode_null() if none) |
| 27 | * @dev: sysinfo device to use (NULL if none) |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 28 | * @eos: end-of-string pointer for the table being processed. This is set |
| 29 | * up when we start processing a table |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 30 | * @next_ptr: pointer to the start of the next string to be added. When the |
| 31 | * table is nopt empty, this points to the byte after the \0 of the |
| 32 | * previous string. |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 33 | * @last_str: points to the last string that was written to the table, or NULL |
| 34 | * if none |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 35 | */ |
| 36 | struct smbios_ctx { |
| 37 | ofnode node; |
| 38 | struct udevice *dev; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 39 | char *eos; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 40 | char *next_ptr; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 41 | char *last_str; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 45 | * Function prototype to write a specific type of SMBIOS structure |
| 46 | * |
| 47 | * @addr: start address to write the structure |
| 48 | * @handle: the structure's handle, a unique 16-bit number |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 49 | * @ctx: context for writing the tables |
Heinrich Schuchardt | 8c6532d | 2021-06-10 12:13:52 +0200 | [diff] [blame] | 50 | * Return: size of the structure |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 51 | */ |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 52 | typedef int (*smbios_write_type)(ulong *addr, int handle, |
| 53 | struct smbios_ctx *ctx); |
Simon Glass | 0e89b85 | 2021-02-04 21:17:14 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 56 | * struct smbios_write_method - Information about a table-writing function |
| 57 | * |
| 58 | * @write: Function to call |
| 59 | * @subnode_name: Name of subnode which has the information for this function, |
| 60 | * NULL if none |
| 61 | */ |
| 62 | struct smbios_write_method { |
| 63 | smbios_write_type write; |
| 64 | const char *subnode_name; |
| 65 | }; |
| 66 | |
| 67 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 68 | * smbios_add_string() - add a string to the string area |
| 69 | * |
| 70 | * This adds a string to the string area which is appended directly after |
| 71 | * the formatted portion of an SMBIOS structure. |
| 72 | * |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 73 | * @ctx: SMBIOS context |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 74 | * @str: string to add |
Heinrich Schuchardt | 8c6532d | 2021-06-10 12:13:52 +0200 | [diff] [blame] | 75 | * Return: string number in the string area (1 or more) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 76 | */ |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 77 | static int smbios_add_string(struct smbios_ctx *ctx, const char *str) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 78 | { |
| 79 | int i = 1; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 80 | char *p = ctx->eos; |
| 81 | |
Heinrich Schuchardt | 00a871d | 2020-06-01 15:44:00 +0200 | [diff] [blame] | 82 | if (!*str) |
| 83 | str = "Unknown"; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 84 | |
| 85 | for (;;) { |
| 86 | if (!*p) { |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 87 | ctx->last_str = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 88 | strcpy(p, str); |
| 89 | p += strlen(str); |
| 90 | *p++ = '\0'; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 91 | ctx->next_ptr = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 92 | *p++ = '\0'; |
| 93 | |
| 94 | return i; |
| 95 | } |
| 96 | |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 97 | if (!strcmp(p, str)) { |
| 98 | ctx->last_str = p; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 99 | return i; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 100 | } |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 101 | |
| 102 | p += strlen(p) + 1; |
| 103 | i++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 108 | * smbios_add_prop_si() - Add a property from the devicetree or sysinfo |
| 109 | * |
| 110 | * Sysinfo is used if available, with a fallback to devicetree |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 111 | * |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 112 | * @ctx: context for writing the tables |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 113 | * @prop: property to write |
Heinrich Schuchardt | 8c6532d | 2021-06-10 12:13:52 +0200 | [diff] [blame] | 114 | * Return: 0 if not found, else SMBIOS string number (1 or more) |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 115 | */ |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 116 | static int smbios_add_prop_si(struct smbios_ctx *ctx, const char *prop, |
| 117 | int sysinfo_id) |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 118 | { |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 119 | if (sysinfo_id && ctx->dev) { |
| 120 | char val[SMBIOS_STR_MAX]; |
| 121 | int ret; |
| 122 | |
| 123 | ret = sysinfo_get_str(ctx->dev, sysinfo_id, sizeof(val), val); |
| 124 | if (!ret) |
| 125 | return smbios_add_string(ctx, val); |
| 126 | } |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 127 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
| 128 | const char *str; |
| 129 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 130 | str = ofnode_read_string(ctx->node, prop); |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 131 | if (str) |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 132 | return smbios_add_string(ctx, str); |
Simon Glass | e4f8e54 | 2020-11-05 06:32:18 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 138 | /** |
| 139 | * smbios_add_prop() - Add a property from the devicetree |
| 140 | * |
| 141 | * @prop: property to write |
Heinrich Schuchardt | 8c6532d | 2021-06-10 12:13:52 +0200 | [diff] [blame] | 142 | * Return: 0 if not found, else SMBIOS string number (1 or more) |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 143 | */ |
| 144 | static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop) |
| 145 | { |
| 146 | return smbios_add_prop_si(ctx, prop, SYSINFO_ID_NONE); |
| 147 | } |
| 148 | |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 149 | static void smbios_set_eos(struct smbios_ctx *ctx, char *eos) |
| 150 | { |
| 151 | ctx->eos = eos; |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 152 | ctx->next_ptr = eos; |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 153 | ctx->last_str = NULL; |
| 154 | } |
| 155 | |
| 156 | int smbios_update_version(const char *version) |
| 157 | { |
| 158 | char *ptr = gd->smbios_version; |
| 159 | uint old_len, len; |
| 160 | |
| 161 | if (!ptr) |
| 162 | return log_ret(-ENOENT); |
| 163 | |
| 164 | /* |
| 165 | * This string is supposed to have at least enough bytes and is |
| 166 | * padded with spaces. Update it, taking care not to move the |
| 167 | * \0 terminator, so that other strings in the string table |
| 168 | * are not disturbed. See smbios_add_string() |
| 169 | */ |
| 170 | old_len = strnlen(ptr, SMBIOS_STR_MAX); |
| 171 | len = strnlen(version, SMBIOS_STR_MAX); |
| 172 | if (len > old_len) |
| 173 | return log_ret(-ENOSPC); |
| 174 | |
| 175 | log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr); |
| 176 | memcpy(ptr, version, len); |
| 177 | #ifdef LOG_DEBUG |
| 178 | print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0); |
| 179 | #endif |
| 180 | |
| 181 | return 0; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 184 | /** |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 185 | * smbios_string_table_len() - compute the string area size |
| 186 | * |
| 187 | * This computes the size of the string area including the string terminator. |
| 188 | * |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 189 | * @ctx: SMBIOS context |
Heinrich Schuchardt | 8c6532d | 2021-06-10 12:13:52 +0200 | [diff] [blame] | 190 | * Return: string area size |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 191 | */ |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 192 | static int smbios_string_table_len(const struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 193 | { |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 194 | /* Allow for the final \0 after all strings */ |
| 195 | return (ctx->next_ptr + 1) - ctx->eos; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 198 | static int smbios_write_type0(ulong *current, int handle, |
| 199 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 200 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 201 | struct smbios_type0 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 202 | int len = sizeof(struct smbios_type0); |
| 203 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 204 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 205 | memset(t, 0, sizeof(struct smbios_type0)); |
| 206 | fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 207 | smbios_set_eos(ctx, t->eos); |
| 208 | t->vendor = smbios_add_string(ctx, "U-Boot"); |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 209 | |
| 210 | t->bios_ver = smbios_add_prop(ctx, "version"); |
| 211 | if (!t->bios_ver) |
| 212 | t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION); |
| 213 | if (t->bios_ver) |
| 214 | gd->smbios_version = ctx->last_str; |
| 215 | log_debug("smbios_version = %p: '%s'\n", gd->smbios_version, |
| 216 | gd->smbios_version); |
| 217 | #ifdef LOG_DEBUG |
| 218 | print_buffer((ulong)gd->smbios_version, gd->smbios_version, |
| 219 | 1, strlen(gd->smbios_version) + 1, 0); |
| 220 | #endif |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 221 | t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE); |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 222 | #ifdef CONFIG_ROM_SIZE |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 223 | t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 224 | #endif |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 225 | t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED | |
| 226 | BIOS_CHARACTERISTICS_SELECTABLE_BOOT | |
| 227 | BIOS_CHARACTERISTICS_UPGRADEABLE; |
| 228 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 229 | t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI; |
| 230 | #endif |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 231 | #ifdef CONFIG_EFI_LOADER |
Ilias Apalodimas | ff19230 | 2021-06-09 18:14:47 +0300 | [diff] [blame] | 232 | t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_UEFI; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 233 | #endif |
Ilias Apalodimas | ff19230 | 2021-06-09 18:14:47 +0300 | [diff] [blame] | 234 | t->bios_characteristics_ext2 |= BIOS_CHARACTERISTICS_EXT2_TARGET; |
Alexander Graf | e663b35 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 235 | |
Simon Glass | 7617f99 | 2021-02-04 21:17:16 -0700 | [diff] [blame] | 236 | /* bios_major_release has only one byte, so drop century */ |
| 237 | t->bios_major_release = U_BOOT_VERSION_NUM % 100; |
| 238 | t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 239 | t->ec_major_release = 0xff; |
| 240 | t->ec_minor_release = 0xff; |
| 241 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 242 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 243 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 244 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 245 | |
| 246 | return len; |
| 247 | } |
| 248 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 249 | static int smbios_write_type1(ulong *current, int handle, |
| 250 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 251 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 252 | struct smbios_type1 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 253 | int len = sizeof(struct smbios_type1); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 254 | char *serial_str = env_get("serial#"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 255 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 256 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 257 | memset(t, 0, sizeof(struct smbios_type1)); |
| 258 | fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 259 | smbios_set_eos(ctx, t->eos); |
| 260 | t->manufacturer = smbios_add_prop(ctx, "manufacturer"); |
Ilias Apalodimas | 70e8066 | 2021-06-10 12:33:15 +0300 | [diff] [blame] | 261 | if (!t->manufacturer) |
| 262 | t->manufacturer = smbios_add_string(ctx, "Unknown"); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 263 | t->product_name = smbios_add_prop(ctx, "product"); |
Ilias Apalodimas | 70e8066 | 2021-06-10 12:33:15 +0300 | [diff] [blame] | 264 | if (!t->product_name) |
| 265 | t->product_name = smbios_add_string(ctx, "Unknown Product"); |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 266 | t->version = smbios_add_prop_si(ctx, "version", |
| 267 | SYSINFO_ID_SMBIOS_SYSTEM_VERSION); |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 268 | if (serial_str) { |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 269 | t->serial_number = smbios_add_string(ctx, serial_str); |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 270 | strncpy((char *)t->uuid, serial_str, sizeof(t->uuid)); |
| 271 | } else { |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 272 | t->serial_number = smbios_add_prop(ctx, "serial"); |
Alexander Graf | 6fb580d | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 273 | } |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 274 | t->sku_number = smbios_add_prop(ctx, "sku"); |
| 275 | t->family = smbios_add_prop(ctx, "family"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 276 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 277 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 278 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 279 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 280 | |
| 281 | return len; |
| 282 | } |
| 283 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 284 | static int smbios_write_type2(ulong *current, int handle, |
| 285 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 286 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 287 | struct smbios_type2 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 288 | int len = sizeof(struct smbios_type2); |
| 289 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 290 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 291 | memset(t, 0, sizeof(struct smbios_type2)); |
| 292 | fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 293 | smbios_set_eos(ctx, t->eos); |
| 294 | t->manufacturer = smbios_add_prop(ctx, "manufacturer"); |
Ilias Apalodimas | 70e8066 | 2021-06-10 12:33:15 +0300 | [diff] [blame] | 295 | if (!t->manufacturer) |
| 296 | t->manufacturer = smbios_add_string(ctx, "Unknown"); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 297 | t->product_name = smbios_add_prop(ctx, "product"); |
Ilias Apalodimas | 70e8066 | 2021-06-10 12:33:15 +0300 | [diff] [blame] | 298 | if (!t->product_name) |
| 299 | t->product_name = smbios_add_string(ctx, "Unknown Product"); |
Simon Glass | 07c9e68 | 2021-02-04 21:17:23 -0700 | [diff] [blame] | 300 | t->version = smbios_add_prop_si(ctx, "version", |
| 301 | SYSINFO_ID_SMBIOS_BASEBOARD_VERSION); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 302 | t->asset_tag_number = smbios_add_prop(ctx, "asset-tag"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 303 | t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; |
| 304 | t->board_type = SMBIOS_BOARD_MOTHERBOARD; |
| 305 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 306 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 307 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 308 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 309 | |
| 310 | return len; |
| 311 | } |
| 312 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 313 | static int smbios_write_type3(ulong *current, int handle, |
| 314 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 315 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 316 | struct smbios_type3 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 317 | int len = sizeof(struct smbios_type3); |
| 318 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 319 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 320 | memset(t, 0, sizeof(struct smbios_type3)); |
| 321 | fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 322 | smbios_set_eos(ctx, t->eos); |
| 323 | t->manufacturer = smbios_add_prop(ctx, "manufacturer"); |
Ilias Apalodimas | 70e8066 | 2021-06-10 12:33:15 +0300 | [diff] [blame] | 324 | if (!t->manufacturer) |
| 325 | t->manufacturer = smbios_add_string(ctx, "Unknown"); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 326 | t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP; |
| 327 | t->bootup_state = SMBIOS_STATE_SAFE; |
| 328 | t->power_supply_state = SMBIOS_STATE_SAFE; |
| 329 | t->thermal_state = SMBIOS_STATE_SAFE; |
| 330 | t->security_status = SMBIOS_SECURITY_NONE; |
| 331 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 332 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 333 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 334 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 335 | |
| 336 | return len; |
| 337 | } |
| 338 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 339 | static void smbios_write_type4_dm(struct smbios_type4 *t, |
| 340 | struct smbios_ctx *ctx) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 341 | { |
| 342 | u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN; |
| 343 | const char *vendor = "Unknown"; |
| 344 | const char *name = "Unknown"; |
| 345 | |
| 346 | #ifdef CONFIG_CPU |
| 347 | char processor_name[49]; |
| 348 | char vendor_name[49]; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 349 | struct udevice *cpu = NULL; |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 350 | |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 351 | uclass_find_first_device(UCLASS_CPU, &cpu); |
| 352 | if (cpu) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 353 | struct cpu_plat *plat = dev_get_parent_plat(cpu); |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 354 | |
| 355 | if (plat->family) |
| 356 | processor_family = plat->family; |
| 357 | t->processor_id[0] = plat->id[0]; |
| 358 | t->processor_id[1] = plat->id[1]; |
| 359 | |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 360 | if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 361 | vendor = vendor_name; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 362 | if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name))) |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 363 | name = processor_name; |
| 364 | } |
| 365 | #endif |
| 366 | |
| 367 | t->processor_family = processor_family; |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 368 | t->processor_manufacturer = smbios_add_string(ctx, vendor); |
| 369 | t->processor_version = smbios_add_string(ctx, name); |
Alexander Graf | 9647620 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 372 | static int smbios_write_type4(ulong *current, int handle, |
| 373 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 374 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 375 | struct smbios_type4 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 376 | int len = sizeof(struct smbios_type4); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 377 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 378 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 379 | memset(t, 0, sizeof(struct smbios_type4)); |
| 380 | fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 381 | smbios_set_eos(ctx, t->eos); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 382 | t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 383 | smbios_write_type4_dm(t, ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 384 | t->status = SMBIOS_PROCESSOR_STATUS_ENABLED; |
| 385 | t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE; |
| 386 | t->l1_cache_handle = 0xffff; |
| 387 | t->l2_cache_handle = 0xffff; |
| 388 | t->l3_cache_handle = 0xffff; |
| 389 | t->processor_family2 = t->processor_family; |
| 390 | |
Simon Glass | fd3b826 | 2021-02-04 21:17:19 -0700 | [diff] [blame] | 391 | len = t->length + smbios_string_table_len(ctx); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 392 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 393 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 394 | |
| 395 | return len; |
| 396 | } |
| 397 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 398 | static int smbios_write_type32(ulong *current, int handle, |
| 399 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 400 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 401 | struct smbios_type32 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 402 | int len = sizeof(struct smbios_type32); |
| 403 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 404 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 405 | memset(t, 0, sizeof(struct smbios_type32)); |
| 406 | fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); |
Simon Glass | 0c95fff | 2021-02-04 21:17:18 -0700 | [diff] [blame] | 407 | smbios_set_eos(ctx, t->eos); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 408 | |
| 409 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 410 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 411 | |
| 412 | return len; |
| 413 | } |
| 414 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 415 | static int smbios_write_type127(ulong *current, int handle, |
| 416 | struct smbios_ctx *ctx) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 417 | { |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 418 | struct smbios_type127 *t; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 419 | int len = sizeof(struct smbios_type127); |
| 420 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 421 | t = map_sysmem(*current, len); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 422 | memset(t, 0, sizeof(struct smbios_type127)); |
| 423 | fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); |
| 424 | |
| 425 | *current += len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 426 | unmap_sysmem(t); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 427 | |
| 428 | return len; |
| 429 | } |
| 430 | |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 431 | static struct smbios_write_method smbios_write_funcs[] = { |
Simon Glass | e9adaa7 | 2021-02-04 21:17:20 -0700 | [diff] [blame] | 432 | { smbios_write_type0, "bios", }, |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 433 | { smbios_write_type1, "system", }, |
| 434 | { smbios_write_type2, "baseboard", }, |
| 435 | { smbios_write_type3, "chassis", }, |
| 436 | { smbios_write_type4, }, |
| 437 | { smbios_write_type32, }, |
| 438 | { smbios_write_type127 }, |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 441 | ulong write_smbios_table(ulong addr) |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 442 | { |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 443 | ofnode parent_node = ofnode_null(); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 444 | struct smbios_entry *se; |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 445 | struct smbios_ctx ctx; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 446 | ulong table_addr; |
Simon Glass | 42fd8c1 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 447 | ulong tables; |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 448 | int len = 0; |
| 449 | int max_struct_size = 0; |
| 450 | int handle = 0; |
| 451 | char *istart; |
| 452 | int isize; |
| 453 | int i; |
| 454 | |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 455 | ctx.node = ofnode_null(); |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 456 | if (IS_ENABLED(CONFIG_OF_CONTROL)) { |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 457 | uclass_first_device(UCLASS_SYSINFO, &ctx.dev); |
| 458 | if (ctx.dev) |
| 459 | parent_node = dev_read_subnode(ctx.dev, "smbios"); |
| 460 | } else { |
| 461 | ctx.dev = NULL; |
Simon Glass | 78227d4 | 2020-11-05 06:32:07 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 464 | /* 16 byte align the table address */ |
| 465 | addr = ALIGN(addr, 16); |
| 466 | |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 467 | se = map_sysmem(addr, sizeof(struct smbios_entry)); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 468 | memset(se, 0, sizeof(struct smbios_entry)); |
| 469 | |
| 470 | addr += sizeof(struct smbios_entry); |
| 471 | addr = ALIGN(addr, 16); |
| 472 | tables = addr; |
| 473 | |
| 474 | /* populate minimum required tables */ |
| 475 | for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) { |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 476 | const struct smbios_write_method *method; |
Simon Glass | 44ffb6f | 2020-11-05 06:32:08 -0700 | [diff] [blame] | 477 | int tmp; |
| 478 | |
| 479 | method = &smbios_write_funcs[i]; |
| 480 | if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name) |
Simon Glass | 1e8989a | 2021-02-04 21:17:17 -0700 | [diff] [blame] | 481 | ctx.node = ofnode_find_subnode(parent_node, |
| 482 | method->subnode_name); |
| 483 | tmp = method->write((ulong *)&addr, handle++, &ctx); |
Christian Gmeiner | 60a4df3 | 2018-07-30 13:22:07 +0200 | [diff] [blame] | 484 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 485 | max_struct_size = max(max_struct_size, tmp); |
| 486 | len += tmp; |
| 487 | } |
| 488 | |
| 489 | memcpy(se->anchor, "_SM_", 4); |
| 490 | se->length = sizeof(struct smbios_entry); |
| 491 | se->major_ver = SMBIOS_MAJOR_VER; |
| 492 | se->minor_ver = SMBIOS_MINOR_VER; |
| 493 | se->max_struct_size = max_struct_size; |
| 494 | memcpy(se->intermediate_anchor, "_DMI_", 5); |
| 495 | se->struct_table_length = len; |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * We must use a pointer here so things work correctly on sandbox. The |
| 499 | * user of this table is not aware of the mapping of addresses to |
| 500 | * sandbox's DRAM buffer. |
| 501 | */ |
| 502 | table_addr = (ulong)map_sysmem(tables, 0); |
| 503 | if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) { |
| 504 | /* |
| 505 | * We need to put this >32-bit pointer into the table but the |
| 506 | * field is only 32 bits wide. |
| 507 | */ |
| 508 | printf("WARNING: SMBIOS table_address overflow %llx\n", |
| 509 | (unsigned long long)table_addr); |
| 510 | table_addr = 0; |
| 511 | } |
| 512 | se->struct_table_address = table_addr; |
| 513 | |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 514 | se->struct_count = handle; |
| 515 | |
| 516 | /* calculate checksums */ |
| 517 | istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET; |
| 518 | isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET; |
| 519 | se->intermediate_checksum = table_compute_checksum(istart, isize); |
| 520 | se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry)); |
Simon Glass | a2505fc | 2018-11-22 13:46:37 -0700 | [diff] [blame] | 521 | unmap_sysmem(se); |
Bin Meng | 721e992 | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 522 | |
| 523 | return addr; |
| 524 | } |