blob: 9a62b3c760d3b41f790e28e157907466eeb8cb3f [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
Heinrich Schuchardt85e490b2024-01-28 11:24:21 +010018 if (!address || !size)
Christian Gmeiner415eab02020-11-03 15:34:51 +010019 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
Heinrich Schuchardtddcfb9e2023-12-23 02:17:33 +010042static struct smbios_header *get_next_header(const struct smbios_header *curr)
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090043{
44 u8 *pos = ((u8 *)curr) + curr->length;
45
46 return (struct smbios_header *)find_next_header(pos);
Christian Gmeiner415eab02020-11-03 15:34:51 +010047}
48
49const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
50{
51 const unsigned int num_header = entry->struct_count;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090052 const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
Christian Gmeiner415eab02020-11-03 15:34:51 +010053
54 for (unsigned int i = 0; i < num_header; i++) {
55 if (header->type == type)
56 return header;
57
Heinrich Schuchardtddcfb9e2023-12-23 02:17:33 +010058 header = get_next_header(header);
Christian Gmeiner415eab02020-11-03 15:34:51 +010059 }
60
61 return NULL;
62}
63
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090064static char *string_from_smbios_table(const struct smbios_header *header,
65 int idx)
Christian Gmeiner415eab02020-11-03 15:34:51 +010066{
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 Kojima3d49ee82021-10-26 17:27:24 +090082 return (char *)pos;
Christian Gmeiner415eab02020-11-03 15:34:51 +010083}
84
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090085char *smbios_string(const struct smbios_header *header, int index)
Christian Gmeiner415eab02020-11-03 15:34:51 +010086{
87 if (!header)
88 return NULL;
89
90 return string_from_smbios_table(header, index);
91}
Simon Glass272e62c2021-03-15 18:00:11 +130092
93int 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 Kojima3d49ee82021-10-26 17:27:24 +0900105 ptr = smbios_string(hdr, bios->bios_ver);
Simon Glass272e62c2021-03-15 18:00:11 +1300106 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 Kojima3d49ee82021-10-26 17:27:24 +0900128
129struct smbios_filter_param {
130 u32 offset;
131 u32 size;
132 bool is_string;
133};
134
135struct smbios_filter_table {
136 int type;
137 struct smbios_filter_param *params;
138 u32 count;
139};
140
141struct 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
150struct 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
157struct 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
164struct 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
187struct 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
198static 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 Kojima2497f6a2024-01-26 09:53:42 +0900226void smbios_prepare_measurement(const struct smbios3_entry *entry,
Masahisa Kojima3d49ee82021-10-26 17:27:24 +0900227 struct smbios_header *smbios_copy)
228{
229 u32 i, j;
Masahisa Kojima2497f6a2024-01-26 09:53:42 +0900230 void *table_end;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +0900231 struct smbios_header *header;
232
Heinrich Schuchardt406c410e2024-01-31 23:49:34 +0100233 table_end = (void *)((u8 *)smbios_copy + entry->table_maximum_size);
Masahisa Kojima2497f6a2024-01-26 09:53:42 +0900234
Masahisa Kojima3d49ee82021-10-26 17:27:24 +0900235 for (i = 0; i < ARRAY_SIZE(smbios_filter_tables); i++) {
236 header = smbios_copy;
Masahisa Kojima2497f6a2024-01-26 09:53:42 +0900237 for (j = 0; (void *)header < table_end; j++) {
Masahisa Kojima3d49ee82021-10-26 17:27:24 +0900238 if (header->type == smbios_filter_tables[i].type)
239 break;
240
241 header = get_next_header(header);
242 }
Masahisa Kojima2497f6a2024-01-26 09:53:42 +0900243 if ((void *)header >= table_end)
Masahisa Kojima3d49ee82021-10-26 17:27:24 +0900244 continue;
245
246 clear_smbios_table(header,
247 smbios_filter_tables[i].params,
248 smbios_filter_tables[i].count);
249 }
250}