]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/lscpu-dmi.c
Merge remote-tracking branch 'pali/multisesssion'
[thirdparty/util-linux.git] / sys-utils / lscpu-dmi.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2020 FUJITSU LIMITED. All rights reserved.
4 */
5
6 #include "lscpu.h"
7
8 void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data)
9 {
10 h->type = data[0];
11 h->length = data[1];
12 memcpy(&h->handle, data + 2, sizeof(h->handle));
13 h->data = data;
14 }
15
16 char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s)
17 {
18 char *bp = (char *)dm->data;
19
20 if (!s || !bp)
21 return NULL;
22
23 bp += dm->length;
24 while (s > 1 && *bp) {
25 bp += strlen(bp);
26 bp++;
27 s--;
28 }
29
30 return !*bp ? NULL : bp;
31 }
32
33 int parse_dmi_table(uint16_t len, uint16_t num,
34 uint8_t *data,
35 struct dmi_info *di)
36 {
37 uint8_t *buf = data;
38 int rc = -1;
39 int i = 0;
40
41 /* 4 is the length of an SMBIOS structure header */
42 while (i < num && data + 4 <= buf + len) {
43 uint8_t *next;
44 struct lscpu_dmi_header h;
45
46 to_dmi_header(&h, data);
47
48 /*
49 * If a short entry is found (less than 4 bytes), not only it
50 * is invalid, but we cannot reliably locate the next entry.
51 * Better stop at this point.
52 */
53 if (h.length < 4)
54 goto done;
55
56 /* look for the next handle */
57 next = data + h.length;
58 while (next - buf + 1 < len && (next[0] != 0 || next[1] != 0))
59 next++;
60 next += 2;
61 switch (h.type) {
62 case 0:
63 di->vendor = dmi_string(&h, data[0x04]);
64 break;
65 case 1:
66 di->manufacturer = dmi_string(&h, data[0x04]);
67 di->product = dmi_string(&h, data[0x05]);
68 break;
69 case 4:
70 di->sockets++;
71 break;
72 default:
73 break;
74 }
75
76 data = next;
77 i++;
78 }
79 rc = 0;
80 done:
81 return rc;
82 }
83
84 size_t get_number_of_physical_sockets_from_dmi(void)
85 {
86 static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI;
87 struct dmi_info di;
88 struct stat st;
89 uint8_t *data;
90 int rc = 0;
91
92 if (stat(sys_fw_dmi_tables, &st))
93 return rc;
94
95 data = get_mem_chunk(0, st.st_size, sys_fw_dmi_tables);
96 if (!data)
97 return rc;
98
99 memset(&di, 0, sizeof(struct dmi_info));
100 rc = parse_dmi_table(st.st_size, st.st_size/4, data, &di);
101
102 free(data);
103
104 if ((rc < 0) || !di.sockets)
105 return 0;
106 else
107 return di.sockets;
108 }