]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/lscpu-dmi.c
Merge branch 'maybe-for-v2.32' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / sys-utils / lscpu-dmi.c
CommitLineData
fb2627ce
OO
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
fb2627ce
OO
30#include "lscpu.h"
31
73be2127
KZ
32#define _PATH_SYS_DMI "/sys/firmware/dmi/tables/DMI"
33
fb2627ce
OO
34#define WORD(x) (uint16_t)(*(const uint16_t *)(x))
35#define DWORD(x) (uint32_t)(*(const uint32_t *)(x))
36
37struct dmi_header
38{
39 uint8_t type;
40 uint8_t length;
41 uint16_t handle;
42 uint8_t *data;
43};
44
45static 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
55static void *get_mem_chunk(size_t base, size_t len, const char *devmem)
56{
dbbf8395 57 void *p = NULL;
fb2627ce
OO
58 int fd;
59
dbbf8395 60 if ((fd = open(devmem, O_RDONLY)) < 0)
fb2627ce 61 return NULL;
dbbf8395
KZ
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;
fb2627ce
OO
69
70 close(fd);
71 return p;
dbbf8395
KZ
72
73nothing:
74 free(p);
75 close(fd);
76 return NULL;
fb2627ce
OO
77}
78
79static 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
87static 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
108static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
109 uint16_t num, const char *devmem)
110{
dbbf8395 111 uint8_t *buf;
fb2627ce
OO
112 uint8_t *data;
113 int i = 0;
114 char *vendor = NULL;
115 char *product = NULL;
116 char *manufacturer = NULL;
dbbf8395 117 int rc = HYPER_NONE;
fb2627ce 118
dbbf8395
KZ
119 data = buf = get_mem_chunk(base, len, devmem);
120 if (!buf)
121 goto done;
fb2627ce
OO
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 */
dbbf8395
KZ
135 if (h.length < 4)
136 goto done;
fb2627ce
OO
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 }
bbff0890 158 if (manufacturer && !strcmp(manufacturer, "innotek GmbH"))
dbbf8395 159 rc = HYPER_INNOTEK;
bbff0890
KZ
160 else if (manufacturer && strstr(manufacturer, "HITACHI") &&
161 product && strstr(product, "LPAR"))
dbbf8395 162 rc = HYPER_HITACHI;
73f2bec5 163 else if (vendor && !strcmp(vendor, "Parallels"))
dbbf8395
KZ
164 rc = HYPER_PARALLELS;
165done:
fb2627ce 166 free(buf);
dbbf8395 167 return rc;
fb2627ce
OO
168}
169
60cb2c37 170#if defined(__x86_64__) || defined(__i386__)
fb2627ce
OO
171static int hypervisor_decode_legacy(uint8_t *buf, const char *devmem)
172{
173 if (!checksum(buf, 0x0F))
c972852b 174 return -1;
fb2627ce
OO
175
176 return hypervisor_from_dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06),
177 WORD(buf + 0x0C),
178 devmem);
179}
60cb2c37 180#endif
fb2627ce
OO
181
182static 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
92a6392c
AB
194static int hypervisor_decode_sysfw(void)
195{
73be2127 196 static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI;
92a6392c
AB
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
fb2627ce
OO
206/*
207 * Probe for EFI interface
208 */
209#define EFI_NOT_FOUND (-1)
210#define EFI_NO_SMBIOS (-2)
211static 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, '=');
6d707c1d
KZ
230 if (!addrp)
231 continue;
fb2627ce
OO
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
244int read_hypervisor_dmi(void)
245{
dbbf8395 246 int rc = HYPER_NONE;
fb2627ce
OO
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)
c972852b 254 goto done;
fb2627ce 255
c972852b
KZ
256 /* -1 : no DMI in /sys,
257 * 0 : DMI exist, nothing detected (HYPER_NONE)
258 * >0 : hypervisor detected
259 */
92a6392c 260 rc = hypervisor_decode_sysfw();
c972852b
KZ
261 if (rc >= HYPER_NONE)
262 goto done;
92a6392c 263
fb2627ce
OO
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:
dbbf8395 269 goto done;
fb2627ce
OO
270 }
271
272 buf = get_mem_chunk(fp, 0x20, _PATH_DEV_MEM);
273 if (!buf)
dbbf8395 274 goto done;
fb2627ce 275
dbbf8395 276 rc = hypervisor_decode_smbios(buf, _PATH_DEV_MEM);
c972852b 277 if (rc >= HYPER_NONE)
fb2627ce 278 goto done;
c972852b 279
dbbf8395 280 free(buf);
4e6604e5 281 buf = NULL;
fb2627ce 282memory_scan:
6f7234f6 283#if defined(__x86_64__) || defined(__i386__)
fb2627ce
OO
284 /* Fallback to memory scan (x86, x86_64) */
285 buf = get_mem_chunk(0xF0000, 0x10000, _PATH_DEV_MEM);
286 if (!buf)
dbbf8395 287 goto done;
fb2627ce
OO
288
289 for (fp = 0; fp <= 0xFFF0; fp += 16) {
290 if (memcmp(buf + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
dbbf8395 291 rc = hypervisor_decode_smbios(buf + fp, _PATH_DEV_MEM);
c972852b 292 if (rc < 0)
fb2627ce
OO
293 fp += 16;
294
295 } else if (memcmp(buf + fp, "_DMI_", 5) == 0)
dbbf8395
KZ
296 rc = hypervisor_decode_legacy(buf + fp, _PATH_DEV_MEM);
297
c972852b 298 if (rc >= HYPER_NONE)
dbbf8395 299 break;
fb2627ce 300 }
6f7234f6 301#endif
fb2627ce
OO
302done:
303 free(buf);
c972852b 304 return rc < 0 ? HYPER_NONE : rc;
fb2627ce 305}