smbios_platform.c revision 1.1.4.2 1 1.1.4.2 thorpej /* $NetBSD: smbios_platform.c,v 1.1.4.2 2021/08/01 22:42:21 thorpej Exp $ */
2 1.1.4.2 thorpej
3 1.1.4.2 thorpej /*-
4 1.1.4.2 thorpej * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1.4.2 thorpej * All rights reserved.
6 1.1.4.2 thorpej *
7 1.1.4.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 thorpej * modification, are permitted provided that the following conditions
9 1.1.4.2 thorpej * are met:
10 1.1.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.1.4.2 thorpej *
16 1.1.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.4.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.4.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.4.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.4.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.4.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.4.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.4.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 thorpej */
28 1.1.4.2 thorpej
29 1.1.4.2 thorpej #include "isa.h"
30 1.1.4.2 thorpej
31 1.1.4.2 thorpej #include <sys/cdefs.h>
32 1.1.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: smbios_platform.c,v 1.1.4.2 2021/08/01 22:42:21 thorpej Exp $");
33 1.1.4.2 thorpej
34 1.1.4.2 thorpej #include <sys/types.h>
35 1.1.4.2 thorpej #include <sys/param.h>
36 1.1.4.2 thorpej #include <sys/kernel.h>
37 1.1.4.2 thorpej #include <sys/sysctl.h>
38 1.1.4.2 thorpej #include <sys/uuid.h>
39 1.1.4.2 thorpej #include <sys/pmf.h>
40 1.1.4.2 thorpej
41 1.1.4.2 thorpej #if NISA > 0
42 1.1.4.2 thorpej #include <dev/isa/isavar.h>
43 1.1.4.2 thorpej #endif
44 1.1.4.2 thorpej
45 1.1.4.2 thorpej #include <dev/smbiosvar.h>
46 1.1.4.2 thorpej
47 1.1.4.2 thorpej static int platform_dminode = CTL_EOL;
48 1.1.4.2 thorpej
49 1.1.4.2 thorpej void platform_init(void); /* XXX */
50 1.1.4.2 thorpej static void platform_add(struct smbtable *, const char *, int);
51 1.1.4.2 thorpej static void platform_add_word(struct smbtable *, const char *, uint16_t,
52 1.1.4.2 thorpej const char *);
53 1.1.4.2 thorpej static void platform_add_date(struct smbtable *, const char *, int);
54 1.1.4.2 thorpej static void platform_add_uuid(struct smbtable *, const char *,
55 1.1.4.2 thorpej const uint8_t *);
56 1.1.4.2 thorpej static int platform_dmi_sysctl(SYSCTLFN_PROTO);
57 1.1.4.2 thorpej
58 1.1.4.2 thorpej /* list of private DMI sysctl nodes */
59 1.1.4.2 thorpej static const char *platform_private_nodes[] = {
60 1.1.4.2 thorpej "chassis-serial",
61 1.1.4.2 thorpej "board-serial",
62 1.1.4.2 thorpej "system-serial",
63 1.1.4.2 thorpej "system-uuid",
64 1.1.4.2 thorpej NULL
65 1.1.4.2 thorpej };
66 1.1.4.2 thorpej
67 1.1.4.2 thorpej void
68 1.1.4.2 thorpej platform_init(void)
69 1.1.4.2 thorpej {
70 1.1.4.2 thorpej struct smbtable smbios;
71 1.1.4.2 thorpej struct smbios_sys *psys;
72 1.1.4.2 thorpej struct smbios_struct_bios *pbios;
73 1.1.4.2 thorpej struct smbios_board *pboard;
74 1.1.4.2 thorpej struct smbios_chassis *pchassis;
75 1.1.4.2 thorpej struct smbios_processor *pproc;
76 1.1.4.2 thorpej struct smbios_slot *pslot;
77 1.1.4.2 thorpej int nisa, nother;
78 1.1.4.2 thorpej
79 1.1.4.2 thorpej smbios.cookie = 0;
80 1.1.4.2 thorpej if (smbios_find_table(SMBIOS_TYPE_SYSTEM, &smbios)) {
81 1.1.4.2 thorpej psys = smbios.tblhdr;
82 1.1.4.2 thorpej
83 1.1.4.2 thorpej platform_add(&smbios, "system-vendor", psys->vendor);
84 1.1.4.2 thorpej platform_add(&smbios, "system-product", psys->product);
85 1.1.4.2 thorpej platform_add(&smbios, "system-version", psys->version);
86 1.1.4.2 thorpej platform_add(&smbios, "system-serial", psys->serial);
87 1.1.4.2 thorpej platform_add_uuid(&smbios, "system-uuid", psys->uuid);
88 1.1.4.2 thorpej }
89 1.1.4.2 thorpej
90 1.1.4.2 thorpej smbios.cookie = 0;
91 1.1.4.2 thorpej if (smbios_find_table(SMBIOS_TYPE_BIOS, &smbios)) {
92 1.1.4.2 thorpej pbios = smbios.tblhdr;
93 1.1.4.2 thorpej
94 1.1.4.2 thorpej platform_add(&smbios, "bios-vendor", pbios->vendor);
95 1.1.4.2 thorpej platform_add(&smbios, "bios-version", pbios->version);
96 1.1.4.2 thorpej platform_add_date(&smbios, "bios-date", pbios->release);
97 1.1.4.2 thorpej }
98 1.1.4.2 thorpej
99 1.1.4.2 thorpej smbios.cookie = 0;
100 1.1.4.2 thorpej if (smbios_find_table(SMBIOS_TYPE_BASEBOARD, &smbios)) {
101 1.1.4.2 thorpej pboard = smbios.tblhdr;
102 1.1.4.2 thorpej
103 1.1.4.2 thorpej platform_add(&smbios, "board-vendor", pboard->vendor);
104 1.1.4.2 thorpej platform_add(&smbios, "board-product", pboard->product);
105 1.1.4.2 thorpej platform_add(&smbios, "board-version", pboard->version);
106 1.1.4.2 thorpej platform_add(&smbios, "board-serial", pboard->serial);
107 1.1.4.2 thorpej platform_add(&smbios, "board-asset-tag", pboard->asset);
108 1.1.4.2 thorpej }
109 1.1.4.2 thorpej
110 1.1.4.2 thorpej smbios.cookie = 0;
111 1.1.4.2 thorpej if (smbios_find_table(SMBIOS_TYPE_ENCLOSURE, &smbios)) {
112 1.1.4.2 thorpej pchassis = smbios.tblhdr;
113 1.1.4.2 thorpej
114 1.1.4.2 thorpej platform_add(&smbios, "chassis-vendor", pchassis->vendor);
115 1.1.4.2 thorpej platform_add(&smbios, "chassis-type", pchassis->shape);
116 1.1.4.2 thorpej platform_add(&smbios, "chassis-version", pchassis->version);
117 1.1.4.2 thorpej platform_add(&smbios, "chassis-serial", pchassis->serial);
118 1.1.4.2 thorpej platform_add(&smbios, "chassis-asset-tag", pchassis->asset);
119 1.1.4.2 thorpej }
120 1.1.4.2 thorpej
121 1.1.4.2 thorpej smbios.cookie = 0;
122 1.1.4.2 thorpej if (smbios_find_table(SMBIOS_TYPE_PROCESSOR, &smbios)) {
123 1.1.4.2 thorpej pproc = smbios.tblhdr;
124 1.1.4.2 thorpej
125 1.1.4.2 thorpej platform_add(&smbios, "processor-vendor", pproc->vendor);
126 1.1.4.2 thorpej platform_add(&smbios, "processor-version", pproc->version);
127 1.1.4.2 thorpej platform_add_word(&smbios, "processor-frequency",
128 1.1.4.2 thorpej pproc->curspeed, " MHz");
129 1.1.4.2 thorpej }
130 1.1.4.2 thorpej
131 1.1.4.2 thorpej smbios.cookie = 0;
132 1.1.4.2 thorpej nisa = 0;
133 1.1.4.2 thorpej nother = 0;
134 1.1.4.2 thorpej while (smbios_find_table(SMBIOS_TYPE_SLOTS, &smbios)) {
135 1.1.4.2 thorpej pslot = smbios.tblhdr;
136 1.1.4.2 thorpej switch (pslot->type) {
137 1.1.4.2 thorpej case SMBIOS_SLOT_ISA:
138 1.1.4.2 thorpej case SMBIOS_SLOT_EISA:
139 1.1.4.2 thorpej nisa++;
140 1.1.4.2 thorpej break;
141 1.1.4.2 thorpej default:
142 1.1.4.2 thorpej nother++;
143 1.1.4.2 thorpej break;
144 1.1.4.2 thorpej }
145 1.1.4.2 thorpej }
146 1.1.4.2 thorpej
147 1.1.4.2 thorpej #if NISA > 0
148 1.1.4.2 thorpej if ((nother | nisa) != 0) {
149 1.1.4.2 thorpej /* Only if there seems to be good expansion slot info. */
150 1.1.4.2 thorpej isa_set_slotcount(nisa);
151 1.1.4.2 thorpej }
152 1.1.4.2 thorpej #endif
153 1.1.4.2 thorpej }
154 1.1.4.2 thorpej
155 1.1.4.2 thorpej static bool
156 1.1.4.2 thorpej platform_sysctl_is_private(const char *key)
157 1.1.4.2 thorpej {
158 1.1.4.2 thorpej unsigned int n;
159 1.1.4.2 thorpej
160 1.1.4.2 thorpej for (n = 0; platform_private_nodes[n] != NULL; n++) {
161 1.1.4.2 thorpej if (strcmp(key, platform_private_nodes[n]) == 0) {
162 1.1.4.2 thorpej return true;
163 1.1.4.2 thorpej }
164 1.1.4.2 thorpej }
165 1.1.4.2 thorpej
166 1.1.4.2 thorpej return false;
167 1.1.4.2 thorpej }
168 1.1.4.2 thorpej
169 1.1.4.2 thorpej static void
170 1.1.4.2 thorpej platform_create_sysctl(const char *key)
171 1.1.4.2 thorpej {
172 1.1.4.2 thorpej int flags = 0, err;
173 1.1.4.2 thorpej
174 1.1.4.2 thorpej if (pmf_get_platform(key) == NULL)
175 1.1.4.2 thorpej return;
176 1.1.4.2 thorpej
177 1.1.4.2 thorpej /* If the key is marked private, set CTLFLAG_PRIVATE flag */
178 1.1.4.2 thorpej if (platform_sysctl_is_private(key))
179 1.1.4.2 thorpej flags |= CTLFLAG_PRIVATE;
180 1.1.4.2 thorpej
181 1.1.4.2 thorpej err = sysctl_createv(NULL, 0, NULL, NULL,
182 1.1.4.2 thorpej CTLFLAG_READONLY | flags, CTLTYPE_STRING,
183 1.1.4.2 thorpej key, NULL, platform_dmi_sysctl, 0, NULL, 0,
184 1.1.4.2 thorpej CTL_MACHDEP, platform_dminode, CTL_CREATE, CTL_EOL);
185 1.1.4.2 thorpej if (err != 0)
186 1.1.4.2 thorpej printf("platform: sysctl_createv "
187 1.1.4.2 thorpej "(machdep.dmi.%s) failed, err = %d\n",
188 1.1.4.2 thorpej key, err);
189 1.1.4.2 thorpej }
190 1.1.4.2 thorpej
191 1.1.4.2 thorpej static void
192 1.1.4.2 thorpej platform_add(struct smbtable *tbl, const char *key, int idx)
193 1.1.4.2 thorpej {
194 1.1.4.2 thorpej char tmpbuf[128]; /* XXX is this long enough? */
195 1.1.4.2 thorpej
196 1.1.4.2 thorpej if (smbios_get_string(tbl, idx, tmpbuf, 128) != NULL) {
197 1.1.4.2 thorpej /* add to platform dictionary */
198 1.1.4.2 thorpej pmf_set_platform(key, tmpbuf);
199 1.1.4.2 thorpej
200 1.1.4.2 thorpej /* create sysctl node */
201 1.1.4.2 thorpej platform_create_sysctl(key);
202 1.1.4.2 thorpej }
203 1.1.4.2 thorpej }
204 1.1.4.2 thorpej
205 1.1.4.2 thorpej static void
206 1.1.4.2 thorpej platform_add_word(struct smbtable *tbl, const char *key, uint16_t val,
207 1.1.4.2 thorpej const char *suf)
208 1.1.4.2 thorpej {
209 1.1.4.2 thorpej char tmpbuf[128]; /* XXX is this long enough? */
210 1.1.4.2 thorpej
211 1.1.4.2 thorpej if (snprintf(tmpbuf, sizeof(tmpbuf), "%u%s", val, suf)) {
212 1.1.4.2 thorpej /* add to platform dictionary */
213 1.1.4.2 thorpej pmf_set_platform(key, tmpbuf);
214 1.1.4.2 thorpej
215 1.1.4.2 thorpej /* create sysctl node */
216 1.1.4.2 thorpej platform_create_sysctl(key);
217 1.1.4.2 thorpej }
218 1.1.4.2 thorpej }
219 1.1.4.2 thorpej
220 1.1.4.2 thorpej static int
221 1.1.4.2 thorpej platform_scan_date(char *buf, unsigned int *month, unsigned int *day,
222 1.1.4.2 thorpej unsigned int *year)
223 1.1.4.2 thorpej {
224 1.1.4.2 thorpej char *p, *s;
225 1.1.4.2 thorpej
226 1.1.4.2 thorpej s = buf;
227 1.1.4.2 thorpej p = strchr(s, '/');
228 1.1.4.2 thorpej if (p) *p = '\0';
229 1.1.4.2 thorpej *month = strtoul(s, NULL, 10);
230 1.1.4.2 thorpej if (!p) return 1;
231 1.1.4.2 thorpej
232 1.1.4.2 thorpej s = p + 1;
233 1.1.4.2 thorpej p = strchr(s, '/');
234 1.1.4.2 thorpej if (p) *p = '\0';
235 1.1.4.2 thorpej *day = strtoul(s, NULL, 10);
236 1.1.4.2 thorpej if (!p) return 2;
237 1.1.4.2 thorpej
238 1.1.4.2 thorpej s = p + 1;
239 1.1.4.2 thorpej *year = strtoul(s, NULL, 10);
240 1.1.4.2 thorpej return 3;
241 1.1.4.2 thorpej }
242 1.1.4.2 thorpej
243 1.1.4.2 thorpej static void
244 1.1.4.2 thorpej platform_add_date(struct smbtable *tbl, const char *key, int idx)
245 1.1.4.2 thorpej {
246 1.1.4.2 thorpej unsigned int month, day, year;
247 1.1.4.2 thorpej char tmpbuf[128], datestr[9];
248 1.1.4.2 thorpej
249 1.1.4.2 thorpej if (smbios_get_string(tbl, idx, tmpbuf, 128) == NULL)
250 1.1.4.2 thorpej return;
251 1.1.4.2 thorpej if (platform_scan_date(tmpbuf, &month, &day, &year) != 3)
252 1.1.4.2 thorpej return;
253 1.1.4.2 thorpej if (month == 0 || month > 12 || day == 0 || day > 31)
254 1.1.4.2 thorpej return;
255 1.1.4.2 thorpej if (year > 9999)
256 1.1.4.2 thorpej return;
257 1.1.4.2 thorpej if (year < 70)
258 1.1.4.2 thorpej year += 2000;
259 1.1.4.2 thorpej else if (year < 100)
260 1.1.4.2 thorpej year += 1900;
261 1.1.4.2 thorpej snprintf(datestr, sizeof(datestr), "%04u%02u%02u", year, month, day);
262 1.1.4.2 thorpej pmf_set_platform(key, datestr);
263 1.1.4.2 thorpej platform_create_sysctl(key);
264 1.1.4.2 thorpej }
265 1.1.4.2 thorpej
266 1.1.4.2 thorpej static void
267 1.1.4.2 thorpej platform_add_uuid(struct smbtable *tbl, const char *key, const uint8_t *buf)
268 1.1.4.2 thorpej {
269 1.1.4.2 thorpej struct uuid uuid;
270 1.1.4.2 thorpej char tmpbuf[UUID_STR_LEN];
271 1.1.4.2 thorpej
272 1.1.4.2 thorpej uuid_dec_le(buf, &uuid);
273 1.1.4.2 thorpej uuid_snprintf(tmpbuf, sizeof(tmpbuf), &uuid);
274 1.1.4.2 thorpej
275 1.1.4.2 thorpej pmf_set_platform(key, tmpbuf);
276 1.1.4.2 thorpej platform_create_sysctl(key);
277 1.1.4.2 thorpej }
278 1.1.4.2 thorpej
279 1.1.4.2 thorpej static int
280 1.1.4.2 thorpej platform_dmi_sysctl(SYSCTLFN_ARGS)
281 1.1.4.2 thorpej {
282 1.1.4.2 thorpej struct sysctlnode node;
283 1.1.4.2 thorpej const char *v;
284 1.1.4.2 thorpej int err = 0;
285 1.1.4.2 thorpej
286 1.1.4.2 thorpej node = *rnode;
287 1.1.4.2 thorpej
288 1.1.4.2 thorpej v = pmf_get_platform(node.sysctl_name);
289 1.1.4.2 thorpej if (v == NULL)
290 1.1.4.2 thorpej return ENOENT;
291 1.1.4.2 thorpej
292 1.1.4.2 thorpej node.sysctl_data = __UNCONST(v);
293 1.1.4.2 thorpej err = sysctl_lookup(SYSCTLFN_CALL(&node));
294 1.1.4.2 thorpej if (err || newp == NULL)
295 1.1.4.2 thorpej return err;
296 1.1.4.2 thorpej
297 1.1.4.2 thorpej return 0;
298 1.1.4.2 thorpej }
299 1.1.4.2 thorpej
300 1.1.4.2 thorpej SYSCTL_SETUP(sysctl_dmi_setup, "sysctl machdep.dmi subtree setup")
301 1.1.4.2 thorpej {
302 1.1.4.2 thorpej const struct sysctlnode *rnode;
303 1.1.4.2 thorpej int err;
304 1.1.4.2 thorpej
305 1.1.4.2 thorpej err = sysctl_createv(clog, 0, NULL, &rnode,
306 1.1.4.2 thorpej CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
307 1.1.4.2 thorpej NULL, NULL, 0, NULL, 0,
308 1.1.4.2 thorpej CTL_MACHDEP, CTL_EOL);
309 1.1.4.2 thorpej if (err)
310 1.1.4.2 thorpej return;
311 1.1.4.2 thorpej
312 1.1.4.2 thorpej err = sysctl_createv(clog, 0, &rnode, &rnode,
313 1.1.4.2 thorpej CTLFLAG_PERMANENT, CTLTYPE_NODE, "dmi",
314 1.1.4.2 thorpej SYSCTL_DESCR("DMI table information"),
315 1.1.4.2 thorpej NULL, 0, NULL, 0,
316 1.1.4.2 thorpej CTL_CREATE, CTL_EOL);
317 1.1.4.2 thorpej if (err)
318 1.1.4.2 thorpej return;
319 1.1.4.2 thorpej
320 1.1.4.2 thorpej platform_dminode = rnode->sysctl_num;
321 1.1.4.2 thorpej }
322