xref: /illumos-gate/usr/src/common/smbios/smb_open.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
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 2023 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 	case SMB_VERSION_37:
239 		break;
240 	default:
241 		return (smb_open_error(shp, errp, ESMB_VERSION));
242 	}
243 
244 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
245 		return (smb_open_error(shp, errp, ESMB_INVAL));
246 
247 	if (shp == NULL)
248 		return (smb_open_error(shp, errp, ESMB_NOMEM));
249 
250 	if (_smb_debug)
251 		shp->sh_flags |= SMB_FL_DEBUG;
252 
253 	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
254 	if (err != 0) {
255 		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
256 		if (err != 0)
257 			return (smb_open_error(shp, errp, err));
258 		shp->sh_ent_stnum =
259 		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
260 	}
261 
262 	shp->sh_buf = buf;
263 	shp->sh_buflen = len;
264 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
265 	shp->sh_nstructs = 0;
266 	shp->sh_hashlen = _smb_hashlen;
267 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
268 	shp->sh_libvers = version;
269 
270 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
271 		return (smb_open_error(shp, errp, ESMB_NOMEM));
272 
273 	hp = shp->sh_buf;
274 	switch (shp->sh_ent_type) {
275 	case SMBIOS_ENTRY_POINT_21:
276 		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
277 		break;
278 	case SMBIOS_ENTRY_POINT_30:
279 		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
280 		break;
281 	default:
282 		return (smb_open_error(shp, errp, ESMB_VERSION));
283 	}
284 
285 	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
286 		smb_struct_t *stp = &shp->sh_structs[i];
287 		uint_t n = 0;
288 
289 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
290 			shp->sh_flags |= SMB_FL_TRUNC;
291 			break;
292 		}
293 
294 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
295 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
296 
297 		if (hp->smbh_type == SMB_TYPE_EOT)
298 			break; /* ignore any entries beyond end-of-table */
299 
300 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
301 			shp->sh_flags |= SMB_FL_TRUNC;
302 			break;
303 		}
304 
305 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
306 		p = s = (const uchar_t *)hp + hp->smbh_len;
307 
308 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
309 			if (*p++ == '\0')
310 				n++; /* count strings until \0\0 delimiter */
311 		}
312 
313 		if (p > q - 2) {
314 			shp->sh_flags |= SMB_FL_TRUNC;
315 			break;
316 		}
317 
318 		if (p > s)
319 			n++; /* add one for final string in string table */
320 
321 		stp->smbst_hdr = hp;
322 		stp->smbst_str = s;
323 		stp->smbst_end = p;
324 		stp->smbst_next = shp->sh_hash[h];
325 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
326 		stp->smbst_strtablen = n;
327 
328 		if (n != 0 && stp->smbst_strtab == NULL)
329 			return (smb_open_error(shp, errp, ESMB_NOMEM));
330 
331 		shp->sh_hash[h] = stp;
332 		nhp = (void *)(p + 2);
333 		shp->sh_nstructs++;
334 
335 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
336 			if (*p == '\0') {
337 				stp->smbst_strtab[n++] =
338 				    (uint16_t)(s - stp->smbst_str);
339 				s = p + 1;
340 			}
341 		}
342 	}
343 
344 	/* error out if we couldn't find any complete entries in the table */
345 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
346 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
347 
348 	if (flags & SMB_O_ZIDS)
349 		smb_strip(shp);
350 
351 	return (shp);
352 }
353 
354 void
355 smbios_close(smbios_hdl_t *shp)
356 {
357 	uint_t i;
358 
359 	for (i = 0; i < shp->sh_nstructs; i++) {
360 		smb_free(shp->sh_structs[i].smbst_strtab,
361 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
362 	}
363 
364 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
365 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
366 
367 	if (shp->sh_flags & SMB_FL_BUFALLOC)
368 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
369 
370 	smb_free(shp, sizeof (smbios_hdl_t));
371 }
372 
373 /*
374  * Recompute the values of the entry point checksums based upon the content
375  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
376  * anyway in case future versioning requires variations in the algorithm.
377  */
378 /*ARGSUSED*/
379 void
380 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
381 {
382 	uchar_t *p, *q = (uchar_t *)ep;
383 	uint8_t esum = 0, isum = 0;
384 
385 	switch (shp->sh_ent_type) {
386 	case SMBIOS_ENTRY_POINT_21:
387 		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
388 
389 		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
390 		    p < q + sizeof (*ep); p++) {
391 			isum += *p;
392 		}
393 
394 		ep->ep21.smbe_icksum = -isum;
395 
396 		for (p = q; p < q + ep->ep21.smbe_elen; p++)
397 			esum += *p;
398 
399 		ep->ep21.smbe_ecksum = -esum;
400 		break;
401 	case SMBIOS_ENTRY_POINT_30:
402 		ep->ep30.smbe_ecksum = 0;
403 		for (p = q; p < q + ep->ep30.smbe_elen; p++)
404 			esum += *p;
405 
406 		ep->ep30.smbe_ecksum = -esum;
407 		break;
408 	default:
409 		break;
410 	}
411 }
412 
413 const void *
414 smbios_buf(smbios_hdl_t *shp)
415 {
416 	return (shp->sh_buf);
417 }
418 
419 size_t
420 smbios_buflen(smbios_hdl_t *shp)
421 {
422 	return (shp->sh_buflen);
423 }
424 
425 static smbios_struct_t *
426 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
427 {
428 	const smb_header_t *hdr = stp->smbst_hdr;
429 
430 	sp->smbstr_id = hdr->smbh_hdl;
431 	sp->smbstr_type = hdr->smbh_type;
432 	sp->smbstr_data = hdr;
433 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
434 
435 	return (sp);
436 }
437 
438 int
439 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
440 {
441 	const smb_struct_t *stp = smb_lookup_id(shp, id);
442 
443 	if (stp == NULL)
444 		return (-1); /* errno is set for us */
445 
446 	if (sp != NULL)
447 		(void) smb_export(stp, sp);
448 
449 	return (0);
450 }
451 
452 int
453 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
454 {
455 	const smb_struct_t *stp = smb_lookup_type(shp, type);
456 
457 	if (stp == NULL)
458 		return (-1); /* errno is set for us */
459 
460 	if (sp != NULL)
461 		(void) smb_export(stp, sp);
462 
463 	return (0);
464 }
465 
466 int
467 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
468 {
469 	const smb_struct_t *sp = shp->sh_structs;
470 	smbios_struct_t s;
471 	int i, rv = 0;
472 
473 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
474 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
475 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
476 			break;
477 	}
478 
479 	return (rv);
480 }
481 
482 const smb_struct_t *
483 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
484 {
485 	uint_t i;
486 
487 	for (i = 0; i < shp->sh_nstructs; i++) {
488 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
489 			return (&shp->sh_structs[i]);
490 	}
491 
492 	(void) smb_set_errno(shp, ESMB_NOENT);
493 	return (NULL);
494 }
495 
496 const smb_struct_t *
497 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
498 {
499 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
500 
501 	switch (id) {
502 	case SMB_ID_NOTSUP:
503 		(void) smb_set_errno(shp, ESMB_NOTSUP);
504 		return (NULL);
505 	case SMB_ID_NONE:
506 		(void) smb_set_errno(shp, ESMB_NOENT);
507 		return (NULL);
508 	}
509 
510 	for (; stp != NULL; stp = stp->smbst_next) {
511 		if (stp->smbst_hdr->smbh_hdl == id)
512 			break;
513 	}
514 
515 	if (stp == NULL)
516 		(void) smb_set_errno(shp, ESMB_NOENT);
517 
518 	return (stp);
519 }
520 
521 const char *
522 smb_strptr(const smb_struct_t *stp, uint_t i)
523 {
524 	if (i == 0 || i > stp->smbst_strtablen)
525 		return (_smb_emptystr);
526 	else
527 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
528 }
529 
530 boolean_t
531 smb_libgteq(smbios_hdl_t *shp, int version)
532 {
533 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
534 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
535 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
536 }
537 
538 boolean_t
539 smb_gteq(smbios_hdl_t *shp, int version)
540 {
541 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
542 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
543 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
544 }
545 
546 boolean_t
547 smbios_truncated(smbios_hdl_t *shp)
548 {
549 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
550 }
551