1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020, Bachmann electronic GmbH
6 #define LOG_CATEGORY LOGC_BOOT
11 #include <tables_csum.h>
12 #include <linux/kernel.h>
14 const struct smbios_entry *smbios_entry(u64 address, u32 size)
16 const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
18 if (!address || !size)
21 if (memcmp(entry->anchor, "_SM_", 4))
24 if (table_compute_checksum(entry, entry->length))
30 static u8 *find_next_header(u8 *pos)
32 /* search for _double_ NULL bytes */
33 while (!((*pos == 0) && (*(pos + 1) == 0)))
36 /* step behind the double NULL bytes */
42 static struct smbios_header *get_next_header(const struct smbios_header *curr)
44 u8 *pos = ((u8 *)curr) + curr->length;
46 return (struct smbios_header *)find_next_header(pos);
49 const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
51 const unsigned int num_header = entry->struct_count;
52 const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
54 for (unsigned int i = 0; i < num_header; i++) {
55 if (header->type == type)
58 header = get_next_header(header);
64 static char *string_from_smbios_table(const struct smbios_header *header,
73 pos = ((u8 *)header) + header->length;
85 char *smbios_string(const struct smbios_header *header, int index)
90 return string_from_smbios_table(header, index);
93 int smbios_update_version_full(void *smbios_tab, const char *version)
95 const struct smbios_header *hdr;
96 struct smbios_type0 *bios;
100 log_info("Updating SMBIOS table at %p\n", smbios_tab);
101 hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
103 return log_msg_ret("tab", -ENOENT);
104 bios = (struct smbios_type0 *)hdr;
105 ptr = smbios_string(hdr, bios->bios_ver);
107 return log_msg_ret("str", -ENOMEDIUM);
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()
115 old_len = strnlen(ptr, SMBIOS_STR_MAX);
116 len = strnlen(version, SMBIOS_STR_MAX);
118 return log_ret(-ENOSPC);
120 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
121 memcpy(ptr, version, len);
123 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
129 struct smbios_filter_param {
135 struct smbios_filter_table {
137 struct smbios_filter_param *params;
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},
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},
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},
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},
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)},
198 static void clear_smbios_table(struct smbios_header *header,
199 struct smbios_filter_param *filter,
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 */
212 str = smbios_string(header, string_id);
216 /* string is cleared to space, keep '\0' terminator */
217 memset(str, ' ', strlen(str));
220 memset((void *)((u8 *)header + filter[i].offset),
226 void smbios_prepare_measurement(const struct smbios3_entry *entry,
227 struct smbios_header *smbios_copy)
231 struct smbios_header *header;
233 table_end = (void *)((u8 *)smbios_copy + entry->table_maximum_size);
235 for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) {
236 header = smbios_copy;
237 for (j = 0; (void *)header < table_end; j++) {
238 if (header->type == smbios_filter_tables[i].type)
241 header = get_next_header(header);
243 if ((void *)header >= table_end)
246 clear_smbios_table(header,
247 smbios_filter_tables[i].params,
248 smbios_filter_tables[i].count);