xref: /illumos-gate/usr/src/common/smbios/smb_open.c (revision 6bc074b1)
184ab085aSmws /*
284ab085aSmws  * CDDL HEADER START
384ab085aSmws  *
484ab085aSmws  * The contents of this file are subject to the terms of the
5074bb90dSTom Pothier  * Common Development and Distribution License (the "License").
6074bb90dSTom Pothier  * You may not use this file except in compliance with the License.
784ab085aSmws  *
884ab085aSmws  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
984ab085aSmws  * or http://www.opensolaris.org/os/licensing.
1084ab085aSmws  * See the License for the specific language governing permissions
1184ab085aSmws  * and limitations under the License.
1284ab085aSmws  *
1384ab085aSmws  * When distributing Covered Code, include this CDDL HEADER in each
1484ab085aSmws  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1584ab085aSmws  * If applicable, add the following below this CDDL HEADER, with the
1684ab085aSmws  * fields enclosed by brackets "[]" replaced with your own identifying
1784ab085aSmws  * information: Portions Copyright [yyyy] [name of copyright owner]
1884ab085aSmws  *
1984ab085aSmws  * CDDL HEADER END
2084ab085aSmws  */
2184ab085aSmws 
2284ab085aSmws /*
234e901881SDale Ghent  * Copyright 2015 OmniTI Computer Consulting, Inc.  All rights reserved.
241566bc34SRobert Mustacchi  * Copyright 2018 Joyent, Inc.
25*6bc074b1SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
26074bb90dSTom Pothier  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2784ab085aSmws  * Use is subject to license terms.
2884ab085aSmws  */
2984ab085aSmws 
3084ab085aSmws #include <sys/smbios_impl.h>
3184ab085aSmws 
3284ab085aSmws static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
3384ab085aSmws static const char _smb_emptystr[] = "";		/* empty string to return */
3484ab085aSmws int _smb_debug = 0;				/* default debug mode */
3584ab085aSmws 
3684ab085aSmws /*
3784ab085aSmws  * Strip out identification information for you privacy weenies.  This is quite
3884ab085aSmws  * simple using our smbios_info_common() abstraction: we just locate any serial
3984ab085aSmws  * numbers and asset tags for each record, and then zero out those strings.
4084ab085aSmws  * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
4184ab085aSmws  * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
4284ab085aSmws  * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
4384ab085aSmws  * the SMBIOS string table has a special meaning (denotes end-of-record).
4484ab085aSmws  */
4584ab085aSmws static void
smb_strip(smbios_hdl_t * shp)4684ab085aSmws smb_strip(smbios_hdl_t *shp)
4784ab085aSmws {
4884ab085aSmws 	uint_t i;
4984ab085aSmws 
5084ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
5184ab085aSmws 		const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
5284ab085aSmws 		smbios_info_t info;
5384ab085aSmws 		char *p;
5484ab085aSmws 
5584ab085aSmws 		if (hp->smbh_type == SMB_TYPE_SYSTEM &&
5684ab085aSmws 		    hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
5784ab085aSmws 			smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
5884ab085aSmws 			bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
5984ab085aSmws 		}
6084ab085aSmws 
6184ab085aSmws 		if (hp->smbh_type == SMB_TYPE_BATTERY &&
6284ab085aSmws 		    hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
6384ab085aSmws 			smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
6484ab085aSmws 			bp->smbbat_ssn = 0;
6584ab085aSmws 		}
6684ab085aSmws 
6784ab085aSmws 		if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
6884ab085aSmws 			for (p = (char *)info.smbi_serial; *p != '\0'; p++)
6984ab085aSmws 				*p = '0';
7084ab085aSmws 			for (p = (char *)info.smbi_asset; *p != '\0'; p++)
7184ab085aSmws 				*p = '0';
7284ab085aSmws 		}
7384ab085aSmws 	}
7484ab085aSmws }
7584ab085aSmws 
761951a933SToomas Soome static int
smbios_bufopen_21(smbios_hdl_t * shp,const smbios_21_entry_t * ep,size_t len,int flags)771951a933SToomas Soome smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
781951a933SToomas Soome     int flags)
7984ab085aSmws {
8084ab085aSmws 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
811951a933SToomas Soome 		return (ESMB_HEADER);
8284ab085aSmws 
8384ab085aSmws 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
841951a933SToomas Soome 		return (ESMB_HEADER);
8584ab085aSmws 
8684ab085aSmws 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
8784ab085aSmws 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
8884ab085aSmws 
8984ab085aSmws 	if (!(flags & SMB_O_NOVERS)) {
9084ab085aSmws 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
911951a933SToomas Soome 			return (ESMB_NEW);
9284ab085aSmws 
9384ab085aSmws 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
9484ab085aSmws 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
9584ab085aSmws 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
961951a933SToomas Soome 			return (ESMB_OLD);
9784ab085aSmws 	}
9884ab085aSmws 
9984ab085aSmws 	if (len < sizeof (smb_header_t) ||
10084ab085aSmws 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
1011951a933SToomas Soome 		return (ESMB_SHORT);
10284ab085aSmws 
10384ab085aSmws 	if (!(flags & SMB_O_NOCKSUM)) {
10484ab085aSmws 		uint8_t esum = 0, isum = 0;
1051951a933SToomas Soome 		const uchar_t *p, *q;
10684ab085aSmws 		q = (uchar_t *)ep;
10784ab085aSmws 
10884ab085aSmws 		for (p = q; p < q + ep->smbe_elen; p++)
10984ab085aSmws 			esum += *p;
11084ab085aSmws 
11184ab085aSmws 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
11284ab085aSmws 			isum += *p;
11384ab085aSmws 
11484ab085aSmws 		if (esum != 0 || isum != 0) {
11584ab085aSmws 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
1161951a933SToomas Soome 			return (ESMB_CKSUM);
1171951a933SToomas Soome 		}
1181951a933SToomas Soome 	}
1191951a933SToomas Soome 
1201951a933SToomas Soome 	/*
1211951a933SToomas Soome 	 * Copy the entry point into our handle.  The underlying entry point
1221951a933SToomas Soome 	 * may be larger than our structure definition, so reset smbe_elen
1231951a933SToomas Soome 	 * to our internal size and recompute good checksums for our copy.
1241951a933SToomas Soome 	 */
1251951a933SToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
1261951a933SToomas Soome 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
1271951a933SToomas Soome 	shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
1281951a933SToomas Soome 	smbios_checksum(shp, &shp->sh_ent);
1291951a933SToomas Soome 
1301951a933SToomas Soome 	shp->sh_ent_stnum = ep->smbe_stnum;
1311951a933SToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
1321951a933SToomas Soome 	return (0);
1331951a933SToomas Soome }
1341951a933SToomas Soome 
1351951a933SToomas Soome static int
smbios_bufopen_30(smbios_hdl_t * shp,const smbios_30_entry_t * ep,size_t len,int flags)1361951a933SToomas Soome smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
1371951a933SToomas Soome     int flags)
1381951a933SToomas Soome {
1391951a933SToomas Soome 	if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
1401951a933SToomas Soome 	    SMB3_ENTRY_EANCHORLEN))
1411951a933SToomas Soome 		return (ESMB_HEADER);
1421951a933SToomas Soome 
1431951a933SToomas Soome 	smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
1441951a933SToomas Soome 	    ep->smbe_major, ep->smbe_minor);
1451951a933SToomas Soome 
1461951a933SToomas Soome 	if (!(flags & SMB_O_NOVERS)) {
1471951a933SToomas Soome 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
1481951a933SToomas Soome 			return (ESMB_NEW);
1491951a933SToomas Soome 
1501951a933SToomas Soome 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
1511951a933SToomas Soome 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
1521951a933SToomas Soome 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
1531951a933SToomas Soome 			return (ESMB_OLD);
1541951a933SToomas Soome 	}
1551951a933SToomas Soome 
1561951a933SToomas Soome 	if (len < sizeof (smb_header_t) ||
1571951a933SToomas Soome 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
1581951a933SToomas Soome 		return (ESMB_SHORT);
1591951a933SToomas Soome 
1601951a933SToomas Soome 	if (!(flags & SMB_O_NOCKSUM)) {
1611951a933SToomas Soome 		uint8_t esum = 0;
1621951a933SToomas Soome 		const uchar_t *p, *q;
1631951a933SToomas Soome 		q = (uchar_t *)ep;
1641951a933SToomas Soome 
1651951a933SToomas Soome 		for (p = q; p < q + ep->smbe_elen; p++)
1661951a933SToomas Soome 			esum += *p;
1671951a933SToomas Soome 
1681951a933SToomas Soome 		if (esum != 0) {
1691951a933SToomas Soome 			smb_dprintf(shp, "bad cksum: e=%x\n", esum);
1701951a933SToomas Soome 			return (ESMB_CKSUM);
17184ab085aSmws 		}
17284ab085aSmws 	}
17384ab085aSmws 
174e4586ebfSmws 	/*
175e4586ebfSmws 	 * Copy the entry point into our handle.  The underlying entry point
176e4586ebfSmws 	 * may be larger than our structure definition, so reset smbe_elen
177e4586ebfSmws 	 * to our internal size and recompute good checksums for our copy.
178e4586ebfSmws 	 */
1791951a933SToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
18084ab085aSmws 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
1811951a933SToomas Soome 	shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
182e4586ebfSmws 	smbios_checksum(shp, &shp->sh_ent);
183e4586ebfSmws 
1841951a933SToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
1851951a933SToomas Soome 
1861951a933SToomas Soome 	return (0);
1871951a933SToomas Soome }
1881951a933SToomas Soome 
1891951a933SToomas Soome static uint_t
smbios_table_nentries(const char * smbe_staddr,uint32_t smbe_stlen)1901951a933SToomas Soome smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
1911951a933SToomas Soome {
1921951a933SToomas Soome 	uint_t i = 0;
1931951a933SToomas Soome 	char *dmi;
1941951a933SToomas Soome 	smb_header_t *hdr;
1951951a933SToomas Soome 
1961951a933SToomas Soome 	if (smbe_staddr == NULL)
1971951a933SToomas Soome 		return (i);
1981951a933SToomas Soome 
1991951a933SToomas Soome 	for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
2001951a933SToomas Soome 		hdr = (smb_header_t *)dmi;
2011951a933SToomas Soome 		dmi += hdr->smbh_len;
2021951a933SToomas Soome 		/*
2031951a933SToomas Soome 		 * Search for the end of the string area.
2041951a933SToomas Soome 		 */
2051951a933SToomas Soome 		while (dmi + 1 < smbe_staddr + smbe_stlen &&
2061951a933SToomas Soome 		    dmi[0] != '\0' && dmi[1] != '\0') {
2071951a933SToomas Soome 			dmi++;
2081951a933SToomas Soome 		}
2091951a933SToomas Soome 		dmi += 2;
2101951a933SToomas Soome 	}
2111951a933SToomas Soome 	return (i);
2121951a933SToomas Soome }
2131951a933SToomas Soome 
2141951a933SToomas Soome smbios_hdl_t *
smbios_bufopen(const smbios_entry_t * ep,const void * buf,size_t len,int version,int flags,int * errp)2151951a933SToomas Soome smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
2161951a933SToomas Soome     int version, int flags, int *errp)
2171951a933SToomas Soome {
2181951a933SToomas Soome 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
2191951a933SToomas Soome 	const smb_header_t *hp, *nhp;
2201951a933SToomas Soome 	const uchar_t *p, *q, *s;
2211951a933SToomas Soome 	uint_t i, h;
2221951a933SToomas Soome 	int err;
2231951a933SToomas Soome 
2241951a933SToomas Soome 	switch (version) {
2251951a933SToomas Soome 	case SMB_VERSION_23:
2261951a933SToomas Soome 	case SMB_VERSION_24:
2271951a933SToomas Soome 	case SMB_VERSION_25:
2281951a933SToomas Soome 	case SMB_VERSION_26:
2291951a933SToomas Soome 	case SMB_VERSION_27:
2301951a933SToomas Soome 	case SMB_VERSION_28:
2311951a933SToomas Soome 	case SMB_VERSION_30:
2321951a933SToomas Soome 	case SMB_VERSION_31:
2331566bc34SRobert Mustacchi 	case SMB_VERSION_32:
234176a9270SRobert Mustacchi 	case SMB_VERSION_33:
235c6795799SRobert Mustacchi 	case SMB_VERSION_34:
236d53cdfabSRobert Mustacchi 	case SMB_VERSION_35:
2371d1fc316SRobert Mustacchi 	case SMB_VERSION_36:
238*6bc074b1SRobert Mustacchi 	case SMB_VERSION_37:
2391951a933SToomas Soome 		break;
2401951a933SToomas Soome 	default:
2411951a933SToomas Soome 		return (smb_open_error(shp, errp, ESMB_VERSION));
2421951a933SToomas Soome 	}
2431951a933SToomas Soome 
2441951a933SToomas Soome 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
2451951a933SToomas Soome 		return (smb_open_error(shp, errp, ESMB_INVAL));
2461951a933SToomas Soome 
2471951a933SToomas Soome 	if (shp == NULL)
2481951a933SToomas Soome 		return (smb_open_error(shp, errp, ESMB_NOMEM));
2491951a933SToomas Soome 
2501951a933SToomas Soome 	if (_smb_debug)
2511951a933SToomas Soome 		shp->sh_flags |= SMB_FL_DEBUG;
2521951a933SToomas Soome 
2531951a933SToomas Soome 	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
2541951a933SToomas Soome 	if (err != 0) {
2551951a933SToomas Soome 		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
2561951a933SToomas Soome 		if (err != 0)
2571951a933SToomas Soome 			return (smb_open_error(shp, errp, err));
2581951a933SToomas Soome 		shp->sh_ent_stnum =
2591951a933SToomas Soome 		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
2601951a933SToomas Soome 	}
2611951a933SToomas Soome 
26284ab085aSmws 	shp->sh_buf = buf;
26384ab085aSmws 	shp->sh_buflen = len;
2641951a933SToomas Soome 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
26584ab085aSmws 	shp->sh_nstructs = 0;
26684ab085aSmws 	shp->sh_hashlen = _smb_hashlen;
26784ab085aSmws 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
26884ab085aSmws 	shp->sh_libvers = version;
26984ab085aSmws 
27084ab085aSmws 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
27184ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
27284ab085aSmws 
27384ab085aSmws 	hp = shp->sh_buf;
2741951a933SToomas Soome 	switch (shp->sh_ent_type) {
2751951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_21:
2761951a933SToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
2771951a933SToomas Soome 		break;
2781951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_30:
2791951a933SToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
2801951a933SToomas Soome 		break;
2811951a933SToomas Soome 	default:
2821951a933SToomas Soome 		return (smb_open_error(shp, errp, ESMB_VERSION));
2831951a933SToomas Soome 	}
28484ab085aSmws 
2851951a933SToomas Soome 	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
28684ab085aSmws 		smb_struct_t *stp = &shp->sh_structs[i];
28784ab085aSmws 		uint_t n = 0;
28884ab085aSmws 
289516627f3SJonathan Matthew 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
290516627f3SJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
291516627f3SJonathan Matthew 			break;
292516627f3SJonathan Matthew 		}
29384ab085aSmws 
29484ab085aSmws 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
29584ab085aSmws 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
29684ab085aSmws 
29784ab085aSmws 		if (hp->smbh_type == SMB_TYPE_EOT)
29884ab085aSmws 			break; /* ignore any entries beyond end-of-table */
29984ab085aSmws 
300516627f3SJonathan Matthew 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
301516627f3SJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
302516627f3SJonathan Matthew 			break;
303516627f3SJonathan Matthew 		}
304e4586ebfSmws 
30584ab085aSmws 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
30684ab085aSmws 		p = s = (const uchar_t *)hp + hp->smbh_len;
30784ab085aSmws 
30884ab085aSmws 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
30984ab085aSmws 			if (*p++ == '\0')
31084ab085aSmws 				n++; /* count strings until \0\0 delimiter */
31184ab085aSmws 		}
31284ab085aSmws 
313516627f3SJonathan Matthew 		if (p > q - 2) {
314516627f3SJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
315516627f3SJonathan Matthew 			break;
316516627f3SJonathan Matthew 		}
31784ab085aSmws 
31884ab085aSmws 		if (p > s)
31984ab085aSmws 			n++; /* add one for final string in string table */
32084ab085aSmws 
32184ab085aSmws 		stp->smbst_hdr = hp;
32284ab085aSmws 		stp->smbst_str = s;
32384ab085aSmws 		stp->smbst_end = p;
32484ab085aSmws 		stp->smbst_next = shp->sh_hash[h];
32584ab085aSmws 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
32684ab085aSmws 		stp->smbst_strtablen = n;
32784ab085aSmws 
32884ab085aSmws 		if (n != 0 && stp->smbst_strtab == NULL)
32984ab085aSmws 			return (smb_open_error(shp, errp, ESMB_NOMEM));
33084ab085aSmws 
33184ab085aSmws 		shp->sh_hash[h] = stp;
33284ab085aSmws 		nhp = (void *)(p + 2);
33384ab085aSmws 		shp->sh_nstructs++;
33484ab085aSmws 
33584ab085aSmws 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
33684ab085aSmws 			if (*p == '\0') {
33784ab085aSmws 				stp->smbst_strtab[n++] =
33884ab085aSmws 				    (uint16_t)(s - stp->smbst_str);
33984ab085aSmws 				s = p + 1;
34084ab085aSmws 			}
34184ab085aSmws 		}
34284ab085aSmws 	}
34384ab085aSmws 
344516627f3SJonathan Matthew 	/* error out if we couldn't find any complete entries in the table */
345516627f3SJonathan Matthew 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
346516627f3SJonathan Matthew 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
347516627f3SJonathan Matthew 
34884ab085aSmws 	if (flags & SMB_O_ZIDS)
34984ab085aSmws 		smb_strip(shp);
35084ab085aSmws 
35184ab085aSmws 	return (shp);
35284ab085aSmws }
35384ab085aSmws 
35484ab085aSmws void
smbios_close(smbios_hdl_t * shp)35584ab085aSmws smbios_close(smbios_hdl_t *shp)
35684ab085aSmws {
35784ab085aSmws 	uint_t i;
35884ab085aSmws 
35984ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
36084ab085aSmws 		smb_free(shp->sh_structs[i].smbst_strtab,
36184ab085aSmws 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
36284ab085aSmws 	}
36384ab085aSmws 
3641951a933SToomas Soome 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
36584ab085aSmws 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
36684ab085aSmws 
36784ab085aSmws 	if (shp->sh_flags & SMB_FL_BUFALLOC)
36884ab085aSmws 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
36984ab085aSmws 
37084ab085aSmws 	smb_free(shp, sizeof (smbios_hdl_t));
37184ab085aSmws }
37284ab085aSmws 
37384ab085aSmws /*
37484ab085aSmws  * Recompute the values of the entry point checksums based upon the content
37584ab085aSmws  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
37684ab085aSmws  * anyway in case future versioning requires variations in the algorithm.
37784ab085aSmws  */
37884ab085aSmws /*ARGSUSED*/
37984ab085aSmws void
smbios_checksum(smbios_hdl_t * shp,smbios_entry_t * ep)38084ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
38184ab085aSmws {
38284ab085aSmws 	uchar_t *p, *q = (uchar_t *)ep;
38384ab085aSmws 	uint8_t esum = 0, isum = 0;
38484ab085aSmws 
3851951a933SToomas Soome 	switch (shp->sh_ent_type) {
3861951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_21:
3871951a933SToomas Soome 		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
38884ab085aSmws 
3891951a933SToomas Soome 		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
3901951a933SToomas Soome 		    p < q + sizeof (*ep); p++) {
3911951a933SToomas Soome 			isum += *p;
3921951a933SToomas Soome 		}
39384ab085aSmws 
3941951a933SToomas Soome 		ep->ep21.smbe_icksum = -isum;
39584ab085aSmws 
3961951a933SToomas Soome 		for (p = q; p < q + ep->ep21.smbe_elen; p++)
3971951a933SToomas Soome 			esum += *p;
39884ab085aSmws 
3991951a933SToomas Soome 		ep->ep21.smbe_ecksum = -esum;
4001951a933SToomas Soome 		break;
4011951a933SToomas Soome 	case SMBIOS_ENTRY_POINT_30:
4021951a933SToomas Soome 		ep->ep30.smbe_ecksum = 0;
4031951a933SToomas Soome 		for (p = q; p < q + ep->ep30.smbe_elen; p++)
4041951a933SToomas Soome 			esum += *p;
4051951a933SToomas Soome 
4061951a933SToomas Soome 		ep->ep30.smbe_ecksum = -esum;
4071951a933SToomas Soome 		break;
4081951a933SToomas Soome 	default:
4091951a933SToomas Soome 		break;
4101951a933SToomas Soome 	}
41184ab085aSmws }
41284ab085aSmws 
41384ab085aSmws const void *
smbios_buf(smbios_hdl_t * shp)41484ab085aSmws smbios_buf(smbios_hdl_t *shp)
41584ab085aSmws {
41684ab085aSmws 	return (shp->sh_buf);
41784ab085aSmws }
41884ab085aSmws 
41984ab085aSmws size_t
smbios_buflen(smbios_hdl_t * shp)42084ab085aSmws smbios_buflen(smbios_hdl_t *shp)
42184ab085aSmws {
42284ab085aSmws 	return (shp->sh_buflen);
42384ab085aSmws }
42484ab085aSmws 
42584ab085aSmws static smbios_struct_t *
smb_export(const smb_struct_t * stp,smbios_struct_t * sp)42684ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
42784ab085aSmws {
42884ab085aSmws 	const smb_header_t *hdr = stp->smbst_hdr;
42984ab085aSmws 
43084ab085aSmws 	sp->smbstr_id = hdr->smbh_hdl;
43184ab085aSmws 	sp->smbstr_type = hdr->smbh_type;
43284ab085aSmws 	sp->smbstr_data = hdr;
43384ab085aSmws 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
43484ab085aSmws 
43584ab085aSmws 	return (sp);
43684ab085aSmws }
43784ab085aSmws 
43884ab085aSmws int
smbios_lookup_id(smbios_hdl_t * shp,id_t id,smbios_struct_t * sp)43984ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
44084ab085aSmws {
44184ab085aSmws 	const smb_struct_t *stp = smb_lookup_id(shp, id);
44284ab085aSmws 
44384ab085aSmws 	if (stp == NULL)
44484ab085aSmws 		return (-1); /* errno is set for us */
44584ab085aSmws 
44684ab085aSmws 	if (sp != NULL)
44784ab085aSmws 		(void) smb_export(stp, sp);
44884ab085aSmws 
44984ab085aSmws 	return (0);
45084ab085aSmws }
45184ab085aSmws 
452074bb90dSTom Pothier int
smbios_lookup_type(smbios_hdl_t * shp,uint_t type,smbios_struct_t * sp)453074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
454074bb90dSTom Pothier {
455074bb90dSTom Pothier 	const smb_struct_t *stp = smb_lookup_type(shp, type);
456074bb90dSTom Pothier 
457074bb90dSTom Pothier 	if (stp == NULL)
458074bb90dSTom Pothier 		return (-1); /* errno is set for us */
459074bb90dSTom Pothier 
460074bb90dSTom Pothier 	if (sp != NULL)
461074bb90dSTom Pothier 		(void) smb_export(stp, sp);
462074bb90dSTom Pothier 
463074bb90dSTom Pothier 	return (0);
464074bb90dSTom Pothier }
465074bb90dSTom Pothier 
46684ab085aSmws int
smbios_iter(smbios_hdl_t * shp,smbios_struct_f * func,void * data)46784ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
46884ab085aSmws {
46984ab085aSmws 	const smb_struct_t *sp = shp->sh_structs;
47084ab085aSmws 	smbios_struct_t s;
47184ab085aSmws 	int i, rv = 0;
47284ab085aSmws 
47384ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
47484ab085aSmws 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
47584ab085aSmws 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
47684ab085aSmws 			break;
47784ab085aSmws 	}
47884ab085aSmws 
47984ab085aSmws 	return (rv);
48084ab085aSmws }
48184ab085aSmws 
48284ab085aSmws const smb_struct_t *
smb_lookup_type(smbios_hdl_t * shp,uint_t type)48384ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type)
48484ab085aSmws {
48584ab085aSmws 	uint_t i;
48684ab085aSmws 
48784ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
48884ab085aSmws 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
48984ab085aSmws 			return (&shp->sh_structs[i]);
49084ab085aSmws 	}
49184ab085aSmws 
49284ab085aSmws 	(void) smb_set_errno(shp, ESMB_NOENT);
49384ab085aSmws 	return (NULL);
49484ab085aSmws }
49584ab085aSmws 
49684ab085aSmws const smb_struct_t *
smb_lookup_id(smbios_hdl_t * shp,uint_t id)49784ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id)
49884ab085aSmws {
49984ab085aSmws 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
50084ab085aSmws 
50184ab085aSmws 	switch (id) {
50284ab085aSmws 	case SMB_ID_NOTSUP:
50384ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOTSUP);
50484ab085aSmws 		return (NULL);
50584ab085aSmws 	case SMB_ID_NONE:
50684ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
50784ab085aSmws 		return (NULL);
50884ab085aSmws 	}
50984ab085aSmws 
51084ab085aSmws 	for (; stp != NULL; stp = stp->smbst_next) {
51184ab085aSmws 		if (stp->smbst_hdr->smbh_hdl == id)
51284ab085aSmws 			break;
51384ab085aSmws 	}
51484ab085aSmws 
51584ab085aSmws 	if (stp == NULL)
51684ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
51784ab085aSmws 
51884ab085aSmws 	return (stp);
51984ab085aSmws }
52084ab085aSmws 
52184ab085aSmws const char *
smb_strptr(const smb_struct_t * stp,uint_t i)52284ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i)
52384ab085aSmws {
52484ab085aSmws 	if (i == 0 || i > stp->smbst_strtablen)
52584ab085aSmws 		return (_smb_emptystr);
52684ab085aSmws 	else
52784ab085aSmws 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
52884ab085aSmws }
52984ab085aSmws 
530*6bc074b1SRobert Mustacchi boolean_t
smb_libgteq(smbios_hdl_t * shp,int version)531e5cce96fSRobert Mustacchi smb_libgteq(smbios_hdl_t *shp, int version)
532e5cce96fSRobert Mustacchi {
533e5cce96fSRobert Mustacchi 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
534e5cce96fSRobert Mustacchi 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
535e5cce96fSRobert Mustacchi 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
536e5cce96fSRobert Mustacchi }
537e5cce96fSRobert Mustacchi 
538*6bc074b1SRobert Mustacchi boolean_t
smb_gteq(smbios_hdl_t * shp,int version)53984ab085aSmws smb_gteq(smbios_hdl_t *shp, int version)
54084ab085aSmws {
54184ab085aSmws 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
54284ab085aSmws 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
54384ab085aSmws 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
54484ab085aSmws }
545516627f3SJonathan Matthew 
546516627f3SJonathan Matthew boolean_t
smbios_truncated(smbios_hdl_t * shp)547516627f3SJonathan Matthew smbios_truncated(smbios_hdl_t *shp)
548516627f3SJonathan Matthew {
549516627f3SJonathan Matthew 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
550516627f3SJonathan Matthew }
551