Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020, Bachmann electronic GmbH |
| 4 | */ |
| 5 | |
Simon Glass | 3971a2a | 2021-03-15 18:00:11 +1300 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_BOOT |
| 7 | |
Heinrich Schuchardt | 34e97aa | 2024-01-03 09:07:20 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 9 | #include <smbios.h> |
Heinrich Schuchardt | 34e97aa | 2024-01-03 09:07:20 +0100 | [diff] [blame] | 10 | #include <string.h> |
Heinrich Schuchardt | 70abd04 | 2023-12-26 11:22:28 +0100 | [diff] [blame] | 11 | #include <tables_csum.h> |
Heinrich Schuchardt | 34e97aa | 2024-01-03 09:07:20 +0100 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 13 | |
| 14 | const struct smbios_entry *smbios_entry(u64 address, u32 size) |
| 15 | { |
| 16 | const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address; |
| 17 | |
Heinrich Schuchardt | 6ab5c2c | 2024-01-28 11:24:21 +0100 | [diff] [blame] | 18 | if (!address || !size) |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 19 | return NULL; |
| 20 | |
| 21 | if (memcmp(entry->anchor, "_SM_", 4)) |
| 22 | return NULL; |
| 23 | |
Heinrich Schuchardt | 70abd04 | 2023-12-26 11:22:28 +0100 | [diff] [blame] | 24 | if (table_compute_checksum(entry, entry->length)) |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 25 | return NULL; |
| 26 | |
| 27 | return entry; |
| 28 | } |
| 29 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 30 | static u8 *find_next_header(u8 *pos) |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 31 | { |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 32 | /* search for _double_ NULL bytes */ |
| 33 | while (!((*pos == 0) && (*(pos + 1) == 0))) |
| 34 | pos++; |
| 35 | |
| 36 | /* step behind the double NULL bytes */ |
| 37 | pos += 2; |
| 38 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 39 | return pos; |
| 40 | } |
| 41 | |
Heinrich Schuchardt | 6ac005b | 2023-12-23 02:17:33 +0100 | [diff] [blame] | 42 | static struct smbios_header *get_next_header(const struct smbios_header *curr) |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 43 | { |
| 44 | u8 *pos = ((u8 *)curr) + curr->length; |
| 45 | |
| 46 | return (struct smbios_header *)find_next_header(pos); |
| 47 | } |
| 48 | |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 49 | const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type) |
| 50 | { |
| 51 | const unsigned int num_header = entry->struct_count; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 52 | const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address); |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 53 | |
| 54 | for (unsigned int i = 0; i < num_header; i++) { |
| 55 | if (header->type == type) |
| 56 | return header; |
| 57 | |
Heinrich Schuchardt | 6ac005b | 2023-12-23 02:17:33 +0100 | [diff] [blame] | 58 | header = get_next_header(header); |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 64 | static char *string_from_smbios_table(const struct smbios_header *header, |
| 65 | int idx) |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 66 | { |
| 67 | unsigned int i = 1; |
| 68 | u8 *pos; |
| 69 | |
| 70 | if (!header) |
| 71 | return NULL; |
| 72 | |
| 73 | pos = ((u8 *)header) + header->length; |
| 74 | |
| 75 | while (i < idx) { |
| 76 | if (*pos == 0x0) |
| 77 | i++; |
| 78 | |
| 79 | pos++; |
| 80 | } |
| 81 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 82 | return (char *)pos; |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 85 | char *smbios_string(const struct smbios_header *header, int index) |
Christian Gmeiner | 1985998 | 2020-11-03 15:34:51 +0100 | [diff] [blame] | 86 | { |
| 87 | if (!header) |
| 88 | return NULL; |
| 89 | |
| 90 | return string_from_smbios_table(header, index); |
| 91 | } |
Simon Glass | 3971a2a | 2021-03-15 18:00:11 +1300 | [diff] [blame] | 92 | |
| 93 | int smbios_update_version_full(void *smbios_tab, const char *version) |
| 94 | { |
| 95 | const struct smbios_header *hdr; |
| 96 | struct smbios_type0 *bios; |
| 97 | uint old_len, len; |
| 98 | char *ptr; |
| 99 | |
| 100 | log_info("Updating SMBIOS table at %p\n", smbios_tab); |
| 101 | hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION); |
| 102 | if (!hdr) |
| 103 | return log_msg_ret("tab", -ENOENT); |
| 104 | bios = (struct smbios_type0 *)hdr; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 105 | ptr = smbios_string(hdr, bios->bios_ver); |
Simon Glass | 3971a2a | 2021-03-15 18:00:11 +1300 | [diff] [blame] | 106 | if (!ptr) |
| 107 | return log_msg_ret("str", -ENOMEDIUM); |
| 108 | |
| 109 | /* |
| 110 | * This string is supposed to have at least enough bytes and is |
| 111 | * padded with spaces. Update it, taking care not to move the |
| 112 | * \0 terminator, so that other strings in the string table |
| 113 | * are not disturbed. See smbios_add_string() |
| 114 | */ |
| 115 | old_len = strnlen(ptr, SMBIOS_STR_MAX); |
| 116 | len = strnlen(version, SMBIOS_STR_MAX); |
| 117 | if (len > old_len) |
| 118 | return log_ret(-ENOSPC); |
| 119 | |
| 120 | log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr); |
| 121 | memcpy(ptr, version, len); |
| 122 | #ifdef LOG_DEBUG |
| 123 | print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0); |
| 124 | #endif |
| 125 | |
| 126 | return 0; |
| 127 | } |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 128 | |
| 129 | struct smbios_filter_param { |
| 130 | u32 offset; |
| 131 | u32 size; |
| 132 | bool is_string; |
| 133 | }; |
| 134 | |
| 135 | struct smbios_filter_table { |
| 136 | int type; |
| 137 | struct smbios_filter_param *params; |
| 138 | u32 count; |
| 139 | }; |
| 140 | |
| 141 | struct smbios_filter_param smbios_type1_filter_params[] = { |
| 142 | {offsetof(struct smbios_type1, serial_number), |
| 143 | FIELD_SIZEOF(struct smbios_type1, serial_number), true}, |
| 144 | {offsetof(struct smbios_type1, uuid), |
| 145 | FIELD_SIZEOF(struct smbios_type1, uuid), false}, |
| 146 | {offsetof(struct smbios_type1, wakeup_type), |
| 147 | FIELD_SIZEOF(struct smbios_type1, wakeup_type), false}, |
| 148 | }; |
| 149 | |
| 150 | struct smbios_filter_param smbios_type2_filter_params[] = { |
| 151 | {offsetof(struct smbios_type2, serial_number), |
| 152 | FIELD_SIZEOF(struct smbios_type2, serial_number), true}, |
| 153 | {offsetof(struct smbios_type2, chassis_location), |
| 154 | FIELD_SIZEOF(struct smbios_type2, chassis_location), false}, |
| 155 | }; |
| 156 | |
| 157 | struct smbios_filter_param smbios_type3_filter_params[] = { |
| 158 | {offsetof(struct smbios_type3, serial_number), |
| 159 | FIELD_SIZEOF(struct smbios_type3, serial_number), true}, |
| 160 | {offsetof(struct smbios_type3, asset_tag_number), |
| 161 | FIELD_SIZEOF(struct smbios_type3, asset_tag_number), true}, |
| 162 | }; |
| 163 | |
| 164 | struct smbios_filter_param smbios_type4_filter_params[] = { |
| 165 | {offsetof(struct smbios_type4, serial_number), |
| 166 | FIELD_SIZEOF(struct smbios_type4, serial_number), true}, |
| 167 | {offsetof(struct smbios_type4, asset_tag), |
| 168 | FIELD_SIZEOF(struct smbios_type4, asset_tag), true}, |
| 169 | {offsetof(struct smbios_type4, part_number), |
| 170 | FIELD_SIZEOF(struct smbios_type4, part_number), true}, |
| 171 | {offsetof(struct smbios_type4, core_count), |
| 172 | FIELD_SIZEOF(struct smbios_type4, core_count), false}, |
| 173 | {offsetof(struct smbios_type4, core_enabled), |
| 174 | FIELD_SIZEOF(struct smbios_type4, core_enabled), false}, |
| 175 | {offsetof(struct smbios_type4, thread_count), |
| 176 | FIELD_SIZEOF(struct smbios_type4, thread_count), false}, |
| 177 | {offsetof(struct smbios_type4, core_count2), |
| 178 | FIELD_SIZEOF(struct smbios_type4, core_count2), false}, |
| 179 | {offsetof(struct smbios_type4, core_enabled2), |
| 180 | FIELD_SIZEOF(struct smbios_type4, core_enabled2), false}, |
| 181 | {offsetof(struct smbios_type4, thread_count2), |
| 182 | FIELD_SIZEOF(struct smbios_type4, thread_count2), false}, |
| 183 | {offsetof(struct smbios_type4, voltage), |
| 184 | FIELD_SIZEOF(struct smbios_type4, voltage), false}, |
| 185 | }; |
| 186 | |
| 187 | struct smbios_filter_table smbios_filter_tables[] = { |
| 188 | {SMBIOS_SYSTEM_INFORMATION, smbios_type1_filter_params, |
| 189 | ARRAY_SIZE(smbios_type1_filter_params)}, |
| 190 | {SMBIOS_BOARD_INFORMATION, smbios_type2_filter_params, |
| 191 | ARRAY_SIZE(smbios_type2_filter_params)}, |
| 192 | {SMBIOS_SYSTEM_ENCLOSURE, smbios_type3_filter_params, |
| 193 | ARRAY_SIZE(smbios_type3_filter_params)}, |
| 194 | {SMBIOS_PROCESSOR_INFORMATION, smbios_type4_filter_params, |
| 195 | ARRAY_SIZE(smbios_type4_filter_params)}, |
| 196 | }; |
| 197 | |
| 198 | static void clear_smbios_table(struct smbios_header *header, |
| 199 | struct smbios_filter_param *filter, |
| 200 | u32 count) |
| 201 | { |
| 202 | u32 i; |
| 203 | char *str; |
| 204 | u8 string_id; |
| 205 | |
| 206 | for (i = 0; i < count; i++) { |
| 207 | if (filter[i].is_string) { |
| 208 | string_id = *((u8 *)header + filter[i].offset); |
| 209 | if (string_id == 0) /* string is empty */ |
| 210 | continue; |
| 211 | |
| 212 | str = smbios_string(header, string_id); |
| 213 | if (!str) |
| 214 | continue; |
| 215 | |
| 216 | /* string is cleared to space, keep '\0' terminator */ |
| 217 | memset(str, ' ', strlen(str)); |
| 218 | |
| 219 | } else { |
| 220 | memset((void *)((u8 *)header + filter[i].offset), |
| 221 | 0, filter[i].size); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 226 | void smbios_prepare_measurement(const struct smbios3_entry *entry, |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 227 | struct smbios_header *smbios_copy) |
| 228 | { |
| 229 | u32 i, j; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 230 | void *table_end; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 231 | struct smbios_header *header; |
| 232 | |
Heinrich Schuchardt | 68e948a | 2024-01-31 23:49:34 +0100 | [diff] [blame] | 233 | table_end = (void *)((u8 *)smbios_copy + entry->table_maximum_size); |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 234 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 235 | for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) { |
| 236 | header = smbios_copy; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 237 | for (j = 0; (void *)header < table_end; j++) { |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 238 | if (header->type == smbios_filter_tables[i].type) |
| 239 | break; |
| 240 | |
| 241 | header = get_next_header(header); |
| 242 | } |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 243 | if ((void *)header >= table_end) |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 244 | continue; |
| 245 | |
| 246 | clear_smbios_table(header, |
| 247 | smbios_filter_tables[i].params, |
| 248 | smbios_filter_tables[i].count); |
| 249 | } |
| 250 | } |