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