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