xref: /illumos-gate/usr/src/common/smbios/smb_open.c (revision 00bfaff92dad2fa278f0e40718333cf4864ad7d5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2015 OmniTI Computer Consulting, Inc.  All rights reserved.
24  * Copyright 2018 Joyent, Inc.
25  * Copyright 2022 Oxide Computer Company
26  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include <sys/smbios_impl.h>
31 
32 static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
33 static const char _smb_emptystr[] = "";		/* empty string to return */
34 int _smb_debug = 0;				/* default debug mode */
35 
36 /*
37  * Strip out identification information for you privacy weenies.  This is quite
38  * simple using our smbios_info_common() abstraction: we just locate any serial
39  * numbers and asset tags for each record, and then zero out those strings.
40  * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
41  * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
42  * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
43  * the SMBIOS string table has a special meaning (denotes end-of-record).
44  */
45 static void
46 smb_strip(smbios_hdl_t *shp)
47 {
48 	uint_t i;
49 
50 	for (i = 0; i < shp->sh_nstructs; i++) {
51 		const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
52 		smbios_info_t info;
53 		char *p;
54 
55 		if (hp->smbh_type == SMB_TYPE_SYSTEM &&
56 		    hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
57 			smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
58 			bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
59 		}
60 
61 		if (hp->smbh_type == SMB_TYPE_BATTERY &&
62 		    hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
63 			smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
64 			bp->smbbat_ssn = 0;
65 		}
66 
67 		if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
68 			for (p = (char *)info.smbi_serial; *p != '\0'; p++)
69 				*p = '0';
70 			for (p = (char *)info.smbi_asset; *p != '\0'; p++)
71 				*p = '0';
72 		}
73 	}
74 }
75 
76 static int
77 smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
78     int flags)
79 {
80 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
81 		return (ESMB_HEADER);
82 
83 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
84 		return (ESMB_HEADER);
85 
86 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
87 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
88 
89 	if (!(flags & SMB_O_NOVERS)) {
90 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
91 			return (ESMB_NEW);
92 
93 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
94 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
95 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
96 			return (ESMB_OLD);
97 	}
98 
99 	if (len < sizeof (smb_header_t) ||
100 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
101 		return (ESMB_SHORT);
102 
103 	if (!(flags & SMB_O_NOCKSUM)) {
104 		uint8_t esum = 0, isum = 0;
105 		const uchar_t *p, *q;
106 		q = (uchar_t *)ep;
107 
108 		for (p = q; p < q + ep->smbe_elen; p++)
109 			esum += *p;
110 
111 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
112 			isum += *p;
113 
114 		if (esum != 0 || isum != 0) {
115 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
116 			return (ESMB_CKSUM);
117 		}
118 	}
119 
120 	/*
121 	 * Copy the entry point into our handle.  The underlying entry point
122 	 * may be larger than our structure definition, so reset smbe_elen
123 	 * to our internal size and recompute good checksums for our copy.
124 	 */
125 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
126 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
127 	shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
128 	smbios_checksum(shp, &shp->sh_ent);
129 
130 	shp->sh_ent_stnum = ep->smbe_stnum;
131 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
132 	return (0);
133 }
134 
135 static int
136 smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
137     int flags)
138 {
139 	if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
140 	    SMB3_ENTRY_EANCHORLEN))
141 		return (ESMB_HEADER);
142 
143 	smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
144 	    ep->smbe_major, ep->smbe_minor);
145 
146 	if (!(flags & SMB_O_NOVERS)) {
147 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
148 			return (ESMB_NEW);
149 
150 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
151 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
152 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
153 			return (ESMB_OLD);
154 	}
155 
156 	if (len < sizeof (smb_header_t) ||
157 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
158 		return (ESMB_SHORT);
159 
160 	if (!(flags & SMB_O_NOCKSUM)) {
161 		uint8_t esum = 0;
162 		const uchar_t *p, *q;
163 		q = (uchar_t *)ep;
164 
165 		for (p = q; p < q + ep->smbe_elen; p++)
166 			esum += *p;
167 
168 		if (esum != 0) {
169 			smb_dprintf(shp, "bad cksum: e=%x\n", esum);
170 			return (ESMB_CKSUM);
171 		}
172 	}
173 
174 	/*
175 	 * Copy the entry point into our handle.  The underlying entry point
176 	 * may be larger than our structure definition, so reset smbe_elen
177 	 * to our internal size and recompute good checksums for our copy.
178 	 */
179 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
180 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
181 	shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
182 	smbios_checksum(shp, &shp->sh_ent);
183 
184 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
185 
186 	return (0);
187 }
188 
189 static uint_t
190 smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
191 {
192 	uint_t i = 0;
193 	char *dmi;
194 	smb_header_t *hdr;
195 
196 	if (smbe_staddr == NULL)
197 		return (i);
198 
199 	for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
200 		hdr = (smb_header_t *)dmi;
201 		dmi += hdr->smbh_len;
202 		/*
203 		 * Search for the end of the string area.
204 		 */
205 		while (dmi + 1 < smbe_staddr + smbe_stlen &&
206 		    dmi[0] != '\0' && dmi[1] != '\0') {
207 			dmi++;
208 		}
209 		dmi += 2;
210 	}
211 	return (i);
212 }
213 
214 smbios_hdl_t *
215 smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
216     int version, int flags, int *errp)
217 {
218 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
219 	const smb_header_t *hp, *nhp;
220 	const uchar_t *p, *q, *s;
221 	uint_t i, h;
222 	int err;
223 
224 	switch (version) {
225 	case SMB_VERSION_23:
226 	case SMB_VERSION_24:
227 	case SMB_VERSION_25:
228 	case SMB_VERSION_26:
229 	case SMB_VERSION_27:
230 	case SMB_VERSION_28:
231 	case SMB_VERSION_30:
232 	case SMB_VERSION_31:
233 	case SMB_VERSION_32:
234 	case SMB_VERSION_33:
235 	case SMB_VERSION_34:
236 	case SMB_VERSION_35:
237 	case SMB_VERSION_36:
238 		break;
239 	default:
240 		return (smb_open_error(shp, errp, ESMB_VERSION));
241 	}
242 
243 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
244 		return (smb_open_error(shp, errp, ESMB_INVAL));
245 
246 	if (shp == NULL)
247 		return (smb_open_error(shp, errp, ESMB_NOMEM));
248 
249 	if (_smb_debug)
250 		shp->sh_flags |= SMB_FL_DEBUG;
251 
252 	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
253 	if (err != 0) {
254 		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
255 		if (err != 0)
256 			return (smb_open_error(shp, errp, err));
257 		shp->sh_ent_stnum =
258 		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
259 	}
260 
261 	shp->sh_buf = buf;
262 	shp->sh_buflen = len;
263 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
264 	shp->sh_nstructs = 0;
265 	shp->sh_hashlen = _smb_hashlen;
266 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
267 	shp->sh_libvers = version;
268 
269 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
270 		return (smb_open_error(shp, errp, ESMB_NOMEM));
271 
272 	hp = shp->sh_buf;
273 	switch (shp->sh_ent_type) {
274 	case SMBIOS_ENTRY_POINT_21:
275 		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
276 		break;
277 	case SMBIOS_ENTRY_POINT_30:
278 		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
279 		break;
280 	default:
281 		return (smb_open_error(shp, errp, ESMB_VERSION));
282 	}
283 
284 	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
285 		smb_struct_t *stp = &shp->sh_structs[i];
286 		uint_t n = 0;
287 
288 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
289 			shp->sh_flags |= SMB_FL_TRUNC;
290 			break;
291 		}
292 
293 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
294 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
295 
296 		if (hp->smbh_type == SMB_TYPE_EOT)
297 			break; /* ignore any entries beyond end-of-table */
298 
299 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
300 			shp->sh_flags |= SMB_FL_TRUNC;
301 			break;
302 		}
303 
304 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
305 		p = s = (const uchar_t *)hp + hp->smbh_len;
306 
307 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
308 			if (*p++ == '\0')
309 				n++; /* count strings until \0\0 delimiter */
310 		}
311 
312 		if (p > q - 2) {
313 			shp->sh_flags |= SMB_FL_TRUNC;
314 			break;
315 		}
316 
317 		if (p > s)
318 			n++; /* add one for final string in string table */
319 
320 		stp->smbst_hdr = hp;
321 		stp->smbst_str = s;
322 		stp->smbst_end = p;
323 		stp->smbst_next = shp->sh_hash[h];
324 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
325 		stp->smbst_strtablen = n;
326 
327 		if (n != 0 && stp->smbst_strtab == NULL)
328 			return (smb_open_error(shp, errp, ESMB_NOMEM));
329 
330 		shp->sh_hash[h] = stp;
331 		nhp = (void *)(p + 2);
332 		shp->sh_nstructs++;
333 
334 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
335 			if (*p == '\0') {
336 				stp->smbst_strtab[n++] =
337 				    (uint16_t)(s - stp->smbst_str);
338 				s = p + 1;
339 			}
340 		}
341 	}
342 
343 	/* error out if we couldn't find any complete entries in the table */
344 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
345 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
346 
347 	if (flags & SMB_O_ZIDS)
348 		smb_strip(shp);
349 
350 	return (shp);
351 }
352 
353 void
354 smbios_close(smbios_hdl_t *shp)
355 {
356 	uint_t i;
357 
358 	for (i = 0; i < shp->sh_nstructs; i++) {
359 		smb_free(shp->sh_structs[i].smbst_strtab,
360 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
361 	}
362 
363 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
364 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
365 
366 	if (shp->sh_flags & SMB_FL_BUFALLOC)
367 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
368 
369 	smb_free(shp, sizeof (smbios_hdl_t));
370 }
371 
372 /*
373  * Recompute the values of the entry point checksums based upon the content
374  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
375  * anyway in case future versioning requires variations in the algorithm.
376  */
377 /*ARGSUSED*/
378 void
379 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
380 {
381 	uchar_t *p, *q = (uchar_t *)ep;
382 	uint8_t esum = 0, isum = 0;
383 
384 	switch (shp->sh_ent_type) {
385 	case SMBIOS_ENTRY_POINT_21:
386 		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
387 
388 		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
389 		    p < q + sizeof (*ep); p++) {
390 			isum += *p;
391 		}
392 
393 		ep->ep21.smbe_icksum = -isum;
394 
395 		for (p = q; p < q + ep->ep21.smbe_elen; p++)
396 			esum += *p;
397 
398 		ep->ep21.smbe_ecksum = -esum;
399 		break;
400 	case SMBIOS_ENTRY_POINT_30:
401 		ep->ep30.smbe_ecksum = 0;
402 		for (p = q; p < q + ep->ep30.smbe_elen; p++)
403 			esum += *p;
404 
405 		ep->ep30.smbe_ecksum = -esum;
406 		break;
407 	default:
408 		break;
409 	}
410 }
411 
412 const void *
413 smbios_buf(smbios_hdl_t *shp)
414 {
415 	return (shp->sh_buf);
416 }
417 
418 size_t
419 smbios_buflen(smbios_hdl_t *shp)
420 {
421 	return (shp->sh_buflen);
422 }
423 
424 static smbios_struct_t *
425 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
426 {
427 	const smb_header_t *hdr = stp->smbst_hdr;
428 
429 	sp->smbstr_id = hdr->smbh_hdl;
430 	sp->smbstr_type = hdr->smbh_type;
431 	sp->smbstr_data = hdr;
432 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
433 
434 	return (sp);
435 }
436 
437 int
438 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
439 {
440 	const smb_struct_t *stp = smb_lookup_id(shp, id);
441 
442 	if (stp == NULL)
443 		return (-1); /* errno is set for us */
444 
445 	if (sp != NULL)
446 		(void) smb_export(stp, sp);
447 
448 	return (0);
449 }
450 
451 int
452 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
453 {
454 	const smb_struct_t *stp = smb_lookup_type(shp, type);
455 
456 	if (stp == NULL)
457 		return (-1); /* errno is set for us */
458 
459 	if (sp != NULL)
460 		(void) smb_export(stp, sp);
461 
462 	return (0);
463 }
464 
465 int
466 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
467 {
468 	const smb_struct_t *sp = shp->sh_structs;
469 	smbios_struct_t s;
470 	int i, rv = 0;
471 
472 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
473 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
474 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
475 			break;
476 	}
477 
478 	return (rv);
479 }
480 
481 const smb_struct_t *
482 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
483 {
484 	uint_t i;
485 
486 	for (i = 0; i < shp->sh_nstructs; i++) {
487 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
488 			return (&shp->sh_structs[i]);
489 	}
490 
491 	(void) smb_set_errno(shp, ESMB_NOENT);
492 	return (NULL);
493 }
494 
495 const smb_struct_t *
496 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
497 {
498 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
499 
500 	switch (id) {
501 	case SMB_ID_NOTSUP:
502 		(void) smb_set_errno(shp, ESMB_NOTSUP);
503 		return (NULL);
504 	case SMB_ID_NONE:
505 		(void) smb_set_errno(shp, ESMB_NOENT);
506 		return (NULL);
507 	}
508 
509 	for (; stp != NULL; stp = stp->smbst_next) {
510 		if (stp->smbst_hdr->smbh_hdl == id)
511 			break;
512 	}
513 
514 	if (stp == NULL)
515 		(void) smb_set_errno(shp, ESMB_NOENT);
516 
517 	return (stp);
518 }
519 
520 const char *
521 smb_strptr(const smb_struct_t *stp, uint_t i)
522 {
523 	if (i == 0 || i > stp->smbst_strtablen)
524 		return (_smb_emptystr);
525 	else
526 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
527 }
528 
529 int
530 smb_libgteq(smbios_hdl_t *shp, int version)
531 {
532 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
533 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
534 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
535 }
536 
537 int
538 smb_gteq(smbios_hdl_t *shp, int version)
539 {
540 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
541 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
542 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
543 }
544 
545 boolean_t
546 smbios_truncated(smbios_hdl_t *shp)
547 {
548 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
549 }
550