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