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