board: starfive: support Milk-V Mars board
[pandora-u-boot.git] / lib / smbios-parser.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020, Bachmann electronic GmbH
4  */
5
6 #define LOG_CATEGORY    LOGC_BOOT
7
8 #include <errno.h>
9 #include <smbios.h>
10 #include <string.h>
11 #include <tables_csum.h>
12 #include <linux/kernel.h>
13
14 const 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
24         if (table_compute_checksum(entry, entry->length))
25                 return NULL;
26
27         return entry;
28 }
29
30 static u8 *find_next_header(u8 *pos)
31 {
32         /* 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
39         return pos;
40 }
41
42 static struct smbios_header *get_next_header(const struct smbios_header *curr)
43 {
44         u8 *pos = ((u8 *)curr) + curr->length;
45
46         return (struct smbios_header *)find_next_header(pos);
47 }
48
49 const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type)
50 {
51         const unsigned int num_header = entry->struct_count;
52         const struct smbios_header *header = (struct smbios_header *)((uintptr_t)entry->struct_table_address);
53
54         for (unsigned int i = 0; i < num_header; i++) {
55                 if (header->type == type)
56                         return header;
57
58                 header = get_next_header(header);
59         }
60
61         return NULL;
62 }
63
64 static char *string_from_smbios_table(const struct smbios_header *header,
65                                       int idx)
66 {
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
82         return (char *)pos;
83 }
84
85 char *smbios_string(const struct smbios_header *header, int index)
86 {
87         if (!header)
88                 return NULL;
89
90         return string_from_smbios_table(header, index);
91 }
92
93 int 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;
105         ptr = smbios_string(hdr, bios->bios_ver);
106         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 }
128
129 struct smbios_filter_param {
130         u32 offset;
131         u32 size;
132         bool is_string;
133 };
134
135 struct smbios_filter_table {
136         int type;
137         struct smbios_filter_param *params;
138         u32 count;
139 };
140
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},
148 };
149
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},
155 };
156
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},
162 };
163
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},
185 };
186
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)},
196 };
197
198 static 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
226 void smbios_prepare_measurement(const struct smbios3_entry *entry,
227                                 struct smbios_header *smbios_copy)
228 {
229         u32 i, j;
230         void *table_end;
231         struct smbios_header *header;
232
233         table_end = (void *)((u8 *)smbios_copy + entry->table_maximum_size);
234
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)
239                                 break;
240
241                         header = get_next_header(header);
242                 }
243                 if ((void *)header >= table_end)
244                         continue;
245
246                 clear_smbios_table(header,
247                                    smbios_filter_tables[i].params,
248                                    smbios_filter_tables[i].count);
249         }
250 }