]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lscpu-dmi.c
su: change error message
[thirdparty/util-linux.git] / sys-utils / lscpu-dmi.c
1 /*
2 * lscpu-dmi - Module to parse SMBIOS information
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Code originally taken from the dmidecode utility and slightly rewritten
19 * to suite the needs of lscpu
20 */
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "lscpu.h"
31
32 #define _PATH_SYS_DMI "/sys/firmware/dmi/tables/DMI"
33
34 #define WORD(x) (uint16_t)(*(const uint16_t *)(x))
35 #define DWORD(x) (uint32_t)(*(const uint32_t *)(x))
36
37 struct dmi_header
38 {
39 uint8_t type;
40 uint8_t length;
41 uint16_t handle;
42 uint8_t *data;
43 };
44
45 static int checksum(const uint8_t *buf, size_t len)
46 {
47 uint8_t sum = 0;
48 size_t a;
49
50 for (a = 0; a < len; a++)
51 sum += buf[a];
52 return (sum == 0);
53 }
54
55 static void *get_mem_chunk(size_t base, size_t len, const char *devmem)
56 {
57 void *p = NULL;
58 int fd;
59
60 if ((fd = open(devmem, O_RDONLY)) < 0)
61 return NULL;
62
63 if (!(p = malloc(len)))
64 goto nothing;
65 if (lseek(fd, base, SEEK_SET) == -1)
66 goto nothing;
67 if (read_all(fd, p, len) == -1)
68 goto nothing;
69
70 close(fd);
71 return p;
72
73 nothing:
74 free(p);
75 close(fd);
76 return NULL;
77 }
78
79 static void to_dmi_header(struct dmi_header *h, uint8_t *data)
80 {
81 h->type = data[0];
82 h->length = data[1];
83 h->handle = WORD(data + 2);
84 h->data = data;
85 }
86
87 static char *dmi_string(const struct dmi_header *dm, uint8_t s)
88 {
89 char *bp = (char *)dm->data;
90
91 if (s == 0)
92 return NULL;
93
94 bp += dm->length;
95 while (s > 1 && *bp)
96 {
97 bp += strlen(bp);
98 bp++;
99 s--;
100 }
101
102 if (!*bp)
103 return NULL;
104
105 return bp;
106 }
107
108 static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
109 uint16_t num, const char *devmem)
110 {
111 uint8_t *buf;
112 uint8_t *data;
113 int i = 0;
114 char *vendor = NULL;
115 char *product = NULL;
116 char *manufacturer = NULL;
117 int rc = HYPER_NONE;
118
119 data = buf = get_mem_chunk(base, len, devmem);
120 if (!buf)
121 goto done;
122
123 /* 4 is the length of an SMBIOS structure header */
124 while (i < num && data + 4 <= buf + len) {
125 uint8_t *next;
126 struct dmi_header h;
127
128 to_dmi_header(&h, data);
129
130 /*
131 * If a short entry is found (less than 4 bytes), not only it
132 * is invalid, but we cannot reliably locate the next entry.
133 * Better stop at this point.
134 */
135 if (h.length < 4)
136 goto done;
137
138 /* look for the next handle */
139 next = data + h.length;
140 while (next - buf + 1 < len && (next[0] != 0 || next[1] != 0))
141 next++;
142 next += 2;
143 switch (h.type) {
144 case 0:
145 vendor = dmi_string(&h, data[0x04]);
146 break;
147 case 1:
148 manufacturer = dmi_string(&h, data[0x04]);
149 product = dmi_string(&h, data[0x05]);
150 break;
151 default:
152 break;
153 }
154
155 data = next;
156 i++;
157 }
158 if (manufacturer && !strcmp(manufacturer, "innotek GmbH"))
159 rc = HYPER_INNOTEK;
160 else if (manufacturer && strstr(manufacturer, "HITACHI") &&
161 product && strstr(product, "LPAR"))
162 rc = HYPER_HITACHI;
163 else if (vendor && !strcmp(vendor, "Parallels"))
164 rc = HYPER_PARALLELS;
165 done:
166 free(buf);
167 return rc;
168 }
169
170 #if defined(__x86_64__) || defined(__i386__)
171 static int hypervisor_decode_legacy(uint8_t *buf, const char *devmem)
172 {
173 if (!checksum(buf, 0x0F))
174 return -1;
175
176 return hypervisor_from_dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06),
177 WORD(buf + 0x0C),
178 devmem);
179 }
180 #endif
181
182 static int hypervisor_decode_smbios(uint8_t *buf, const char *devmem)
183 {
184 if (!checksum(buf, buf[0x05])
185 || memcmp(buf + 0x10, "_DMI_", 5) != 0
186 || !checksum(buf + 0x10, 0x0F))
187 return -1;
188
189 return hypervisor_from_dmi_table(DWORD(buf + 0x18), WORD(buf + 0x16),
190 WORD(buf + 0x1C),
191 devmem);
192 }
193
194 static int hypervisor_decode_sysfw(void)
195 {
196 static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI;
197 struct stat st;
198
199 if (stat(sys_fw_dmi_tables, &st))
200 return -1;
201
202 return hypervisor_from_dmi_table(0, st.st_size, st.st_size / 4,
203 sys_fw_dmi_tables);
204 }
205
206 /*
207 * Probe for EFI interface
208 */
209 #define EFI_NOT_FOUND (-1)
210 #define EFI_NO_SMBIOS (-2)
211 static int address_from_efi(size_t *address)
212 {
213 FILE *tab;
214 char linebuf[64];
215 int ret;
216
217 *address = 0; /* Prevent compiler warning */
218
219 /*
220 * Linux up to 2.6.6: /proc/efi/systab
221 * Linux 2.6.7 and up: /sys/firmware/efi/systab
222 */
223 if (!(tab = fopen("/sys/firmware/efi/systab", "r")) &&
224 !(tab = fopen("/proc/efi/systab", "r")))
225 return EFI_NOT_FOUND; /* No EFI interface */
226
227 ret = EFI_NO_SMBIOS;
228 while ((fgets(linebuf, sizeof(linebuf) - 1, tab)) != NULL) {
229 char *addrp = strchr(linebuf, '=');
230 if (!addrp)
231 continue;
232 *(addrp++) = '\0';
233 if (strcmp(linebuf, "SMBIOS") == 0) {
234 *address = strtoul(addrp, NULL, 0);
235 ret = 0;
236 break;
237 }
238 }
239
240 fclose(tab);
241 return ret;
242 }
243
244 int read_hypervisor_dmi(void)
245 {
246 int rc = HYPER_NONE;
247 uint8_t *buf = NULL;
248 size_t fp = 0;
249
250 if (sizeof(uint8_t) != 1
251 || sizeof(uint16_t) != 2
252 || sizeof(uint32_t) != 4
253 || '\0' != 0)
254 goto done;
255
256 /* -1 : no DMI in /sys,
257 * 0 : DMI exist, nothing detected (HYPER_NONE)
258 * >0 : hypervisor detected
259 */
260 rc = hypervisor_decode_sysfw();
261 if (rc >= HYPER_NONE)
262 goto done;
263
264 /* First try EFI (ia64, Intel-based Mac) */
265 switch (address_from_efi(&fp)) {
266 case EFI_NOT_FOUND:
267 goto memory_scan;
268 case EFI_NO_SMBIOS:
269 goto done;
270 }
271
272 buf = get_mem_chunk(fp, 0x20, _PATH_DEV_MEM);
273 if (!buf)
274 goto done;
275
276 rc = hypervisor_decode_smbios(buf, _PATH_DEV_MEM);
277 if (rc >= HYPER_NONE)
278 goto done;
279
280 free(buf);
281 buf = NULL;
282 memory_scan:
283 #if defined(__x86_64__) || defined(__i386__)
284 /* Fallback to memory scan (x86, x86_64) */
285 buf = get_mem_chunk(0xF0000, 0x10000, _PATH_DEV_MEM);
286 if (!buf)
287 goto done;
288
289 for (fp = 0; fp <= 0xFFF0; fp += 16) {
290 if (memcmp(buf + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
291 rc = hypervisor_decode_smbios(buf + fp, _PATH_DEV_MEM);
292 if (rc < 0)
293 fp += 16;
294
295 } else if (memcmp(buf + fp, "_DMI_", 5) == 0)
296 rc = hypervisor_decode_legacy(buf + fp, _PATH_DEV_MEM);
297
298 if (rc >= HYPER_NONE)
299 break;
300 }
301 #endif
302 done:
303 free(buf);
304 return rc < 0 ? HYPER_NONE : rc;
305 }