1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020, Bachmann electronic GmbH
6 #define LOG_CATEGORY LOGC_BOOT
11 static inline int verify_checksum(const struct smbios_entry *e)
14 * Checksums for SMBIOS tables are calculated to have a value, so that
15 * the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
20 for (int i = 0; i < e->length; i++)
26 const struct smbios_entry *smbios_entry(u64 address, u32 size)
28 const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
33 if (memcmp(entry->anchor, "_SM_", 4))
36 if (verify_checksum(entry))
42 static u8 *find_next_header(u8 *pos)
44 /* search for _double_ NULL bytes */
45 while (!((*pos == 0) && (*(pos + 1) == 0)))
48 /* step behind the double NULL bytes */
54 static struct smbios_header *get_next_header(struct smbios_header *curr)
56 u8 *pos = ((u8 *)curr) + curr->length;
58 return (struct smbios_header *)find_next_header(pos);
61 static const struct smbios_header *next_header(const struct smbios_header *curr)
63 u8 *pos = ((u8 *)curr) + curr->length;
65 return (struct smbios_header *)find_next_header(pos);
68 const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
70 const unsigned int num_header = entry->struct_count;
71 const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
73 for (unsigned int i = 0; i < num_header; i++) {
74 if (header->type == type)
77 header = next_header(header);
83 static char *string_from_smbios_table(const struct smbios_header *header,
92 pos = ((u8 *)header) + header->length;
104 char *smbios_string(const struct smbios_header *header, int index)
109 return string_from_smbios_table(header, index);
112 int smbios_update_version_full(void *smbios_tab, const char *version)
114 const struct smbios_header *hdr;
115 struct smbios_type0 *bios;
119 log_info("Updating SMBIOS table at %p\n", smbios_tab);
120 hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
122 return log_msg_ret("tab", -ENOENT);
123 bios = (struct smbios_type0 *)hdr;
124 ptr = smbios_string(hdr, bios->bios_ver);
126 return log_msg_ret("str", -ENOMEDIUM);
129 * This string is supposed to have at least enough bytes and is
130 * padded with spaces. Update it, taking care not to move the
131 * \0 terminator, so that other strings in the string table
132 * are not disturbed. See smbios_add_string()
134 old_len = strnlen(ptr, SMBIOS_STR_MAX);
135 len = strnlen(version, SMBIOS_STR_MAX);
137 return log_ret(-ENOSPC);
139 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
140 memcpy(ptr, version, len);
142 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
148 struct smbios_filter_param {
154 struct smbios_filter_table {
156 struct smbios_filter_param *params;
160 struct smbios_filter_param smbios_type1_filter_params[] = {
161 {offsetof(struct smbios_type1, serial_number),
162 FIELD_SIZEOF(struct smbios_type1, serial_number), true},
163 {offsetof(struct smbios_type1, uuid),
164 FIELD_SIZEOF(struct smbios_type1, uuid), false},
165 {offsetof(struct smbios_type1, wakeup_type),
166 FIELD_SIZEOF(struct smbios_type1, wakeup_type), false},
169 struct smbios_filter_param smbios_type2_filter_params[] = {
170 {offsetof(struct smbios_type2, serial_number),
171 FIELD_SIZEOF(struct smbios_type2, serial_number), true},
172 {offsetof(struct smbios_type2, chassis_location),
173 FIELD_SIZEOF(struct smbios_type2, chassis_location), false},
176 struct smbios_filter_param smbios_type3_filter_params[] = {
177 {offsetof(struct smbios_type3, serial_number),
178 FIELD_SIZEOF(struct smbios_type3, serial_number), true},
179 {offsetof(struct smbios_type3, asset_tag_number),
180 FIELD_SIZEOF(struct smbios_type3, asset_tag_number), true},
183 struct smbios_filter_param smbios_type4_filter_params[] = {
184 {offsetof(struct smbios_type4, serial_number),
185 FIELD_SIZEOF(struct smbios_type4, serial_number), true},
186 {offsetof(struct smbios_type4, asset_tag),
187 FIELD_SIZEOF(struct smbios_type4, asset_tag), true},
188 {offsetof(struct smbios_type4, part_number),
189 FIELD_SIZEOF(struct smbios_type4, part_number), true},
190 {offsetof(struct smbios_type4, core_count),
191 FIELD_SIZEOF(struct smbios_type4, core_count), false},
192 {offsetof(struct smbios_type4, core_enabled),
193 FIELD_SIZEOF(struct smbios_type4, core_enabled), false},
194 {offsetof(struct smbios_type4, thread_count),
195 FIELD_SIZEOF(struct smbios_type4, thread_count), false},
196 {offsetof(struct smbios_type4, core_count2),
197 FIELD_SIZEOF(struct smbios_type4, core_count2), false},
198 {offsetof(struct smbios_type4, core_enabled2),
199 FIELD_SIZEOF(struct smbios_type4, core_enabled2), false},
200 {offsetof(struct smbios_type4, thread_count2),
201 FIELD_SIZEOF(struct smbios_type4, thread_count2), false},
202 {offsetof(struct smbios_type4, voltage),
203 FIELD_SIZEOF(struct smbios_type4, voltage), false},
206 struct smbios_filter_table smbios_filter_tables[] = {
207 {SMBIOS_SYSTEM_INFORMATION, smbios_type1_filter_params,
208 ARRAY_SIZE(smbios_type1_filter_params)},
209 {SMBIOS_BOARD_INFORMATION, smbios_type2_filter_params,
210 ARRAY_SIZE(smbios_type2_filter_params)},
211 {SMBIOS_SYSTEM_ENCLOSURE, smbios_type3_filter_params,
212 ARRAY_SIZE(smbios_type3_filter_params)},
213 {SMBIOS_PROCESSOR_INFORMATION, smbios_type4_filter_params,
214 ARRAY_SIZE(smbios_type4_filter_params)},
217 static void clear_smbios_table(struct smbios_header *header,
218 struct smbios_filter_param *filter,
225 for (i = 0; i < count; i++) {
226 if (filter[i].is_string) {
227 string_id = *((u8 *)header + filter[i].offset);
228 if (string_id == 0) /* string is empty */
231 str = smbios_string(header, string_id);
235 /* string is cleared to space, keep '\0' terminator */
236 memset(str, ' ', strlen(str));
239 memset((void *)((u8 *)header + filter[i].offset),
245 void smbios_prepare_measurement(const struct smbios_entry *entry,
246 struct smbios_header *smbios_copy)
249 struct smbios_header *header;
251 for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) {
252 header = smbios_copy;
253 for (j = 0; j < entry->struct_count; j++) {
254 if (header->type == smbios_filter_tables[i].type)
257 header = get_next_header(header);
259 if (j >= entry->struct_count)
262 clear_smbios_table(header,
263 smbios_filter_tables[i].params,
264 smbios_filter_tables[i].count);