blob: b578c3084081380face3a519070c879d2531bc27 [file] [log] [blame]
Christian Gmeiner19859982020-11-03 15:34:51 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020, Bachmann electronic GmbH
4 */
5
Simon Glass3971a2a2021-03-15 18:00:11 +13006#define LOG_CATEGORY LOGC_BOOT
7
Christian Gmeiner19859982020-11-03 15:34:51 +01008#include <smbios.h>
9
10static inline int verify_checksum(const struct smbios_entry *e)
11{
12 /*
13 * Checksums for SMBIOS tables are calculated to have a value, so that
14 * the sum over all bytes yields zero (using unsigned 8 bit arithmetic).
15 */
16 u8 *byte = (u8 *)e;
17 u8 sum = 0;
18
19 for (int i = 0; i < e->length; i++)
20 sum += byte[i];
21
22 return sum;
23}
24
25const struct smbios_entry *smbios_entry(u64 address, u32 size)
26{
27 const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address;
28
29 if (!address | !size)
30 return NULL;
31
32 if (memcmp(entry->anchor, "_SM_", 4))
33 return NULL;
34
35 if (verify_checksum(entry))
36 return NULL;
37
38 return entry;
39}
40
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090041static u8 *find_next_header(u8 *pos)
Christian Gmeiner19859982020-11-03 15:34:51 +010042{
Christian Gmeiner19859982020-11-03 15:34:51 +010043 /* search for _double_ NULL bytes */
44 while (!((*pos == 0) && (*(pos + 1) == 0)))
45 pos++;
46
47 /* step behind the double NULL bytes */
48 pos += 2;
49
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090050 return pos;
51}
52
53static struct smbios_header *get_next_header(struct smbios_header *curr)
54{
55 u8 *pos = ((u8 *)curr) + curr->length;
56
57 return (struct smbios_header *)find_next_header(pos);
58}
59
60static const struct smbios_header *next_header(const struct smbios_header *curr)
61{
62 u8 *pos = ((u8 *)curr) + curr->length;
63
64 return (struct smbios_header *)find_next_header(pos);
Christian Gmeiner19859982020-11-03 15:34:51 +010065}
66
67const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
68{
69 const unsigned int num_header = entry->struct_count;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090070 const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
Christian Gmeiner19859982020-11-03 15:34:51 +010071
72 for (unsigned int i = 0; i < num_header; i++) {
73 if (header->type == type)
74 return header;
75
76 header = next_header(header);
77 }
78
79 return NULL;
80}
81
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090082static char *string_from_smbios_table(const struct smbios_header *header,
83 int idx)
Christian Gmeiner19859982020-11-03 15:34:51 +010084{
85 unsigned int i = 1;
86 u8 *pos;
87
88 if (!header)
89 return NULL;
90
91 pos = ((u8 *)header) + header->length;
92
93 while (i < idx) {
94 if (*pos == 0x0)
95 i++;
96
97 pos++;
98 }
99
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900100 return (char *)pos;
Christian Gmeiner19859982020-11-03 15:34:51 +0100101}
102
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900103char *smbios_string(const struct smbios_header *header, int index)
Christian Gmeiner19859982020-11-03 15:34:51 +0100104{
105 if (!header)
106 return NULL;
107
108 return string_from_smbios_table(header, index);
109}
Simon Glass3971a2a2021-03-15 18:00:11 +1300110
111int smbios_update_version_full(void *smbios_tab, const char *version)
112{
113 const struct smbios_header *hdr;
114 struct smbios_type0 *bios;
115 uint old_len, len;
116 char *ptr;
117
118 log_info("Updating SMBIOS table at %p\n", smbios_tab);
119 hdr = smbios_header(smbios_tab, SMBIOS_BIOS_INFORMATION);
120 if (!hdr)
121 return log_msg_ret("tab", -ENOENT);
122 bios = (struct smbios_type0 *)hdr;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900123 ptr = smbios_string(hdr, bios->bios_ver);
Simon Glass3971a2a2021-03-15 18:00:11 +1300124 if (!ptr)
125 return log_msg_ret("str", -ENOMEDIUM);
126
127 /*
128 * This string is supposed to have at least enough bytes and is
129 * padded with spaces. Update it, taking care not to move the
130 * \0 terminator, so that other strings in the string table
131 * are not disturbed. See smbios_add_string()
132 */
133 old_len = strnlen(ptr, SMBIOS_STR_MAX);
134 len = strnlen(version, SMBIOS_STR_MAX);
135 if (len > old_len)
136 return log_ret(-ENOSPC);
137
138 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
139 memcpy(ptr, version, len);
140#ifdef LOG_DEBUG
141 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
142#endif
143
144 return 0;
145}
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +0900146
147struct smbios_filter_param {
148 u32 offset;
149 u32 size;
150 bool is_string;
151};
152
153struct smbios_filter_table {
154 int type;
155 struct smbios_filter_param *params;
156 u32 count;
157};
158
159struct smbios_filter_param smbios_type1_filter_params[] = {
160 {offsetof(struct smbios_type1, serial_number),
161 FIELD_SIZEOF(struct smbios_type1, serial_number), true},
162 {offsetof(struct smbios_type1, uuid),
163 FIELD_SIZEOF(struct smbios_type1, uuid), false},
164 {offsetof(struct smbios_type1, wakeup_type),
165 FIELD_SIZEOF(struct smbios_type1, wakeup_type), false},
166};
167
168struct smbios_filter_param smbios_type2_filter_params[] = {
169 {offsetof(struct smbios_type2, serial_number),
170 FIELD_SIZEOF(struct smbios_type2, serial_number), true},
171 {offsetof(struct smbios_type2, chassis_location),
172 FIELD_SIZEOF(struct smbios_type2, chassis_location), false},
173};
174
175struct smbios_filter_param smbios_type3_filter_params[] = {
176 {offsetof(struct smbios_type3, serial_number),
177 FIELD_SIZEOF(struct smbios_type3, serial_number), true},
178 {offsetof(struct smbios_type3, asset_tag_number),
179 FIELD_SIZEOF(struct smbios_type3, asset_tag_number), true},
180};
181
182struct smbios_filter_param smbios_type4_filter_params[] = {
183 {offsetof(struct smbios_type4, serial_number),
184 FIELD_SIZEOF(struct smbios_type4, serial_number), true},
185 {offsetof(struct smbios_type4, asset_tag),
186 FIELD_SIZEOF(struct smbios_type4, asset_tag), true},
187 {offsetof(struct smbios_type4, part_number),
188 FIELD_SIZEOF(struct smbios_type4, part_number), true},
189 {offsetof(struct smbios_type4, core_count),
190 FIELD_SIZEOF(struct smbios_type4, core_count), false},
191 {offsetof(struct smbios_type4, core_enabled),
192 FIELD_SIZEOF(struct smbios_type4, core_enabled), false},
193 {offsetof(struct smbios_type4, thread_count),
194 FIELD_SIZEOF(struct smbios_type4, thread_count), false},
195 {offsetof(struct smbios_type4, core_count2),
196 FIELD_SIZEOF(struct smbios_type4, core_count2), false},
197 {offsetof(struct smbios_type4, core_enabled2),
198 FIELD_SIZEOF(struct smbios_type4, core_enabled2), false},
199 {offsetof(struct smbios_type4, thread_count2),
200 FIELD_SIZEOF(struct smbios_type4, thread_count2), false},
201 {offsetof(struct smbios_type4, voltage),
202 FIELD_SIZEOF(struct smbios_type4, voltage), false},
203};
204
205struct smbios_filter_table smbios_filter_tables[] = {
206 {SMBIOS_SYSTEM_INFORMATION, smbios_type1_filter_params,
207 ARRAY_SIZE(smbios_type1_filter_params)},
208 {SMBIOS_BOARD_INFORMATION, smbios_type2_filter_params,
209 ARRAY_SIZE(smbios_type2_filter_params)},
210 {SMBIOS_SYSTEM_ENCLOSURE, smbios_type3_filter_params,
211 ARRAY_SIZE(smbios_type3_filter_params)},
212 {SMBIOS_PROCESSOR_INFORMATION, smbios_type4_filter_params,
213 ARRAY_SIZE(smbios_type4_filter_params)},
214};
215
216static void clear_smbios_table(struct smbios_header *header,
217 struct smbios_filter_param *filter,
218 u32 count)
219{
220 u32 i;
221 char *str;
222 u8 string_id;
223
224 for (i = 0; i < count; i++) {
225 if (filter[i].is_string) {
226 string_id = *((u8 *)header + filter[i].offset);
227 if (string_id == 0) /* string is empty */
228 continue;
229
230 str = smbios_string(header, string_id);
231 if (!str)
232 continue;
233
234 /* string is cleared to space, keep '\0' terminator */
235 memset(str, ' ', strlen(str));
236
237 } else {
238 memset((void *)((u8 *)header + filter[i].offset),
239 0, filter[i].size);
240 }
241 }
242}
243
244void smbios_prepare_measurement(const struct smbios_entry *entry,
245 struct smbios_header *smbios_copy)
246{
247 u32 i, j;
248 struct smbios_header *header;
249
250 for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) {
251 header = smbios_copy;
252 for (j = 0; j < entry->struct_count; j++) {
253 if (header->type == smbios_filter_tables[i].type)
254 break;
255
256 header = get_next_header(header);
257 }
258 if (j >= entry->struct_count)
259 continue;
260
261 clear_smbios_table(header,
262 smbios_filter_tables[i].params,
263 smbios_filter_tables[i].count);
264 }
265}