xref: /titanic_51/usr/src/common/smbios/smb_open.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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.
24*174bc649SRobert Mustacchi  * Copyright 2018 Joyent, Inc.
25074bb90dSTom Pothier  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2684ab085aSmws  * Use is subject to license terms.
2784ab085aSmws  */
2884ab085aSmws 
2984ab085aSmws #include <sys/smbios_impl.h>
3084ab085aSmws 
3184ab085aSmws static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
3284ab085aSmws static const char _smb_emptystr[] = "";		/* empty string to return */
3384ab085aSmws int _smb_debug = 0;				/* default debug mode */
3484ab085aSmws 
3584ab085aSmws /*
3684ab085aSmws  * Strip out identification information for you privacy weenies.  This is quite
3784ab085aSmws  * simple using our smbios_info_common() abstraction: we just locate any serial
3884ab085aSmws  * numbers and asset tags for each record, and then zero out those strings.
3984ab085aSmws  * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
4084ab085aSmws  * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
4184ab085aSmws  * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
4284ab085aSmws  * the SMBIOS string table has a special meaning (denotes end-of-record).
4384ab085aSmws  */
4484ab085aSmws static void
4584ab085aSmws smb_strip(smbios_hdl_t *shp)
4684ab085aSmws {
4784ab085aSmws 	uint_t i;
4884ab085aSmws 
4984ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
5084ab085aSmws 		const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
5184ab085aSmws 		smbios_info_t info;
5284ab085aSmws 		char *p;
5384ab085aSmws 
5484ab085aSmws 		if (hp->smbh_type == SMB_TYPE_SYSTEM &&
5584ab085aSmws 		    hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
5684ab085aSmws 			smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
5784ab085aSmws 			bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
5884ab085aSmws 		}
5984ab085aSmws 
6084ab085aSmws 		if (hp->smbh_type == SMB_TYPE_BATTERY &&
6184ab085aSmws 		    hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
6284ab085aSmws 			smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
6384ab085aSmws 			bp->smbbat_ssn = 0;
6484ab085aSmws 		}
6584ab085aSmws 
6684ab085aSmws 		if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
6784ab085aSmws 			for (p = (char *)info.smbi_serial; *p != '\0'; p++)
6884ab085aSmws 				*p = '0';
6984ab085aSmws 			for (p = (char *)info.smbi_asset; *p != '\0'; p++)
7084ab085aSmws 				*p = '0';
7184ab085aSmws 		}
7284ab085aSmws 	}
7384ab085aSmws }
7484ab085aSmws 
75c325726fSToomas Soome static int
76c325726fSToomas Soome smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
77c325726fSToomas Soome     int flags)
78c325726fSToomas Soome {
79c325726fSToomas Soome 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
80c325726fSToomas Soome 		return (ESMB_HEADER);
81c325726fSToomas Soome 
82c325726fSToomas Soome 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
83c325726fSToomas Soome 		return (ESMB_HEADER);
84c325726fSToomas Soome 
85c325726fSToomas Soome 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
86c325726fSToomas Soome 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
87c325726fSToomas Soome 
88c325726fSToomas Soome 	if (!(flags & SMB_O_NOVERS)) {
89c325726fSToomas Soome 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
90c325726fSToomas Soome 			return (ESMB_NEW);
91c325726fSToomas Soome 
92c325726fSToomas Soome 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
93c325726fSToomas Soome 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
94c325726fSToomas Soome 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
95c325726fSToomas Soome 			return (ESMB_OLD);
96c325726fSToomas Soome 	}
97c325726fSToomas Soome 
98c325726fSToomas Soome 	if (len < sizeof (smb_header_t) ||
99c325726fSToomas Soome 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
100c325726fSToomas Soome 		return (ESMB_SHORT);
101c325726fSToomas Soome 
102c325726fSToomas Soome 	if (!(flags & SMB_O_NOCKSUM)) {
103c325726fSToomas Soome 		uint8_t esum = 0, isum = 0;
104c325726fSToomas Soome 		const uchar_t *p, *q;
105c325726fSToomas Soome 		q = (uchar_t *)ep;
106c325726fSToomas Soome 
107c325726fSToomas Soome 		for (p = q; p < q + ep->smbe_elen; p++)
108c325726fSToomas Soome 			esum += *p;
109c325726fSToomas Soome 
110c325726fSToomas Soome 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
111c325726fSToomas Soome 			isum += *p;
112c325726fSToomas Soome 
113c325726fSToomas Soome 		if (esum != 0 || isum != 0) {
114c325726fSToomas Soome 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
115c325726fSToomas Soome 			return (ESMB_CKSUM);
116c325726fSToomas Soome 		}
117c325726fSToomas Soome 	}
118c325726fSToomas Soome 
119c325726fSToomas Soome 	/*
120c325726fSToomas Soome 	 * Copy the entry point into our handle.  The underlying entry point
121c325726fSToomas Soome 	 * may be larger than our structure definition, so reset smbe_elen
122c325726fSToomas Soome 	 * to our internal size and recompute good checksums for our copy.
123c325726fSToomas Soome 	 */
124c325726fSToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
125c325726fSToomas Soome 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
126c325726fSToomas Soome 	shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
127c325726fSToomas Soome 	smbios_checksum(shp, &shp->sh_ent);
128c325726fSToomas Soome 
129c325726fSToomas Soome 	shp->sh_ent_stnum = ep->smbe_stnum;
130c325726fSToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
131c325726fSToomas Soome 	return (0);
132c325726fSToomas Soome }
133c325726fSToomas Soome 
134c325726fSToomas Soome static int
135c325726fSToomas Soome smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
136c325726fSToomas Soome     int flags)
137c325726fSToomas Soome {
138c325726fSToomas Soome 	if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
139c325726fSToomas Soome 	    SMB3_ENTRY_EANCHORLEN))
140c325726fSToomas Soome 		return (ESMB_HEADER);
141c325726fSToomas Soome 
142c325726fSToomas Soome 	smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
143c325726fSToomas Soome 	    ep->smbe_major, ep->smbe_minor);
144c325726fSToomas Soome 
145c325726fSToomas Soome 	if (!(flags & SMB_O_NOVERS)) {
146c325726fSToomas Soome 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
147c325726fSToomas Soome 			return (ESMB_NEW);
148c325726fSToomas Soome 
149c325726fSToomas Soome 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
150c325726fSToomas Soome 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
151c325726fSToomas Soome 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
152c325726fSToomas Soome 			return (ESMB_OLD);
153c325726fSToomas Soome 	}
154c325726fSToomas Soome 
155c325726fSToomas Soome 	if (len < sizeof (smb_header_t) ||
156c325726fSToomas Soome 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
157c325726fSToomas Soome 		return (ESMB_SHORT);
158c325726fSToomas Soome 
159c325726fSToomas Soome 	if (!(flags & SMB_O_NOCKSUM)) {
160c325726fSToomas Soome 		uint8_t esum = 0;
161c325726fSToomas Soome 		const uchar_t *p, *q;
162c325726fSToomas Soome 		q = (uchar_t *)ep;
163c325726fSToomas Soome 
164c325726fSToomas Soome 		for (p = q; p < q + ep->smbe_elen; p++)
165c325726fSToomas Soome 			esum += *p;
166c325726fSToomas Soome 
167c325726fSToomas Soome 		if (esum != 0) {
168c325726fSToomas Soome 			smb_dprintf(shp, "bad cksum: e=%x\n", esum);
169c325726fSToomas Soome 			return (ESMB_CKSUM);
170c325726fSToomas Soome 		}
171c325726fSToomas Soome 	}
172c325726fSToomas Soome 
173c325726fSToomas Soome 	/*
174c325726fSToomas Soome 	 * Copy the entry point into our handle.  The underlying entry point
175c325726fSToomas Soome 	 * may be larger than our structure definition, so reset smbe_elen
176c325726fSToomas Soome 	 * to our internal size and recompute good checksums for our copy.
177c325726fSToomas Soome 	 */
178c325726fSToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
179c325726fSToomas Soome 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
180c325726fSToomas Soome 	shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
181c325726fSToomas Soome 	smbios_checksum(shp, &shp->sh_ent);
182c325726fSToomas Soome 
183c325726fSToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
184c325726fSToomas Soome 
185c325726fSToomas Soome 	return (0);
186c325726fSToomas Soome }
187c325726fSToomas Soome 
188c325726fSToomas Soome static uint_t
189c325726fSToomas Soome smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
190c325726fSToomas Soome {
191c325726fSToomas Soome 	uint_t i = 0;
192c325726fSToomas Soome 	char *dmi;
193c325726fSToomas Soome 	smb_header_t *hdr;
194c325726fSToomas Soome 
195c325726fSToomas Soome 	if (smbe_staddr == NULL)
196c325726fSToomas Soome 		return (i);
197c325726fSToomas Soome 
198c325726fSToomas Soome 	for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
199c325726fSToomas Soome 		hdr = (smb_header_t *)dmi;
200c325726fSToomas Soome 		dmi += hdr->smbh_len;
201c325726fSToomas Soome 		/*
202c325726fSToomas Soome 		 * Search for the end of the string area.
203c325726fSToomas Soome 		 */
204c325726fSToomas Soome 		while (dmi + 1 < smbe_staddr + smbe_stlen &&
205c325726fSToomas Soome 		    dmi[0] != '\0' && dmi[1] != '\0') {
206c325726fSToomas Soome 			dmi++;
207c325726fSToomas Soome 		}
208c325726fSToomas Soome 		dmi += 2;
209c325726fSToomas Soome 	}
210c325726fSToomas Soome 	return (i);
211c325726fSToomas Soome }
212c325726fSToomas Soome 
21384ab085aSmws smbios_hdl_t *
21484ab085aSmws smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
21584ab085aSmws     int version, int flags, int *errp)
21684ab085aSmws {
21784ab085aSmws 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
21884ab085aSmws 	const smb_header_t *hp, *nhp;
21984ab085aSmws 	const uchar_t *p, *q, *s;
22084ab085aSmws 	uint_t i, h;
221c325726fSToomas Soome 	int err;
22284ab085aSmws 
22384ab085aSmws 	switch (version) {
22484ab085aSmws 	case SMB_VERSION_23:
22584ab085aSmws 	case SMB_VERSION_24:
2264e901881SDale Ghent 	case SMB_VERSION_25:
2274e901881SDale Ghent 	case SMB_VERSION_26:
2284e901881SDale Ghent 	case SMB_VERSION_27:
2294e901881SDale Ghent 	case SMB_VERSION_28:
2306734c4b0SRobert Mustacchi 	case SMB_VERSION_30:
23138d76b18SRobert Mustacchi 	case SMB_VERSION_31:
232*174bc649SRobert Mustacchi 	case SMB_VERSION_32:
23384ab085aSmws 		break;
23484ab085aSmws 	default:
23584ab085aSmws 		return (smb_open_error(shp, errp, ESMB_VERSION));
23684ab085aSmws 	}
23784ab085aSmws 
23884ab085aSmws 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
23984ab085aSmws 		return (smb_open_error(shp, errp, ESMB_INVAL));
24084ab085aSmws 
24184ab085aSmws 	if (shp == NULL)
24284ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
24384ab085aSmws 
24484ab085aSmws 	if (_smb_debug)
24584ab085aSmws 		shp->sh_flags |= SMB_FL_DEBUG;
24684ab085aSmws 
247c325726fSToomas Soome 	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
248c325726fSToomas Soome 	if (err != 0) {
249c325726fSToomas Soome 		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
250c325726fSToomas Soome 		if (err != 0)
251c325726fSToomas Soome 			return (smb_open_error(shp, errp, err));
252c325726fSToomas Soome 		shp->sh_ent_stnum =
253c325726fSToomas Soome 		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
25484ab085aSmws 	}
25584ab085aSmws 
25684ab085aSmws 	shp->sh_buf = buf;
25784ab085aSmws 	shp->sh_buflen = len;
258c325726fSToomas Soome 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
25984ab085aSmws 	shp->sh_nstructs = 0;
26084ab085aSmws 	shp->sh_hashlen = _smb_hashlen;
26184ab085aSmws 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
26284ab085aSmws 	shp->sh_libvers = version;
26384ab085aSmws 
26484ab085aSmws 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
26584ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
26684ab085aSmws 
26784ab085aSmws 	hp = shp->sh_buf;
268c325726fSToomas Soome 	switch (shp->sh_ent_type) {
269c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_21:
270c325726fSToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
271c325726fSToomas Soome 		break;
272c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_30:
273c325726fSToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
274c325726fSToomas Soome 		break;
275c325726fSToomas Soome 	default:
276c325726fSToomas Soome 		return (smb_open_error(shp, errp, ESMB_VERSION));
277c325726fSToomas Soome 	}
27884ab085aSmws 
279c325726fSToomas Soome 	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
28084ab085aSmws 		smb_struct_t *stp = &shp->sh_structs[i];
28184ab085aSmws 		uint_t n = 0;
28284ab085aSmws 
283b60ae21dSJonathan Matthew 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
284b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
285b60ae21dSJonathan Matthew 			break;
286b60ae21dSJonathan Matthew 		}
28784ab085aSmws 
28884ab085aSmws 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
28984ab085aSmws 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
29084ab085aSmws 
29184ab085aSmws 		if (hp->smbh_type == SMB_TYPE_EOT)
29284ab085aSmws 			break; /* ignore any entries beyond end-of-table */
29384ab085aSmws 
294b60ae21dSJonathan Matthew 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
295b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
296b60ae21dSJonathan Matthew 			break;
297b60ae21dSJonathan Matthew 		}
298e4586ebfSmws 
29984ab085aSmws 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
30084ab085aSmws 		p = s = (const uchar_t *)hp + hp->smbh_len;
30184ab085aSmws 
30284ab085aSmws 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
30384ab085aSmws 			if (*p++ == '\0')
30484ab085aSmws 				n++; /* count strings until \0\0 delimiter */
30584ab085aSmws 		}
30684ab085aSmws 
307b60ae21dSJonathan Matthew 		if (p > q - 2) {
308b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
309b60ae21dSJonathan Matthew 			break;
310b60ae21dSJonathan Matthew 		}
31184ab085aSmws 
31284ab085aSmws 		if (p > s)
31384ab085aSmws 			n++; /* add one for final string in string table */
31484ab085aSmws 
31584ab085aSmws 		stp->smbst_hdr = hp;
31684ab085aSmws 		stp->smbst_str = s;
31784ab085aSmws 		stp->smbst_end = p;
31884ab085aSmws 		stp->smbst_next = shp->sh_hash[h];
31984ab085aSmws 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
32084ab085aSmws 		stp->smbst_strtablen = n;
32184ab085aSmws 
32284ab085aSmws 		if (n != 0 && stp->smbst_strtab == NULL)
32384ab085aSmws 			return (smb_open_error(shp, errp, ESMB_NOMEM));
32484ab085aSmws 
32584ab085aSmws 		shp->sh_hash[h] = stp;
32684ab085aSmws 		nhp = (void *)(p + 2);
32784ab085aSmws 		shp->sh_nstructs++;
32884ab085aSmws 
32984ab085aSmws 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
33084ab085aSmws 			if (*p == '\0') {
33184ab085aSmws 				stp->smbst_strtab[n++] =
33284ab085aSmws 				    (uint16_t)(s - stp->smbst_str);
33384ab085aSmws 				s = p + 1;
33484ab085aSmws 			}
33584ab085aSmws 		}
33684ab085aSmws 	}
33784ab085aSmws 
338b60ae21dSJonathan Matthew 	/* error out if we couldn't find any complete entries in the table */
339b60ae21dSJonathan Matthew 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
340b60ae21dSJonathan Matthew 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
341b60ae21dSJonathan Matthew 
34284ab085aSmws 	if (flags & SMB_O_ZIDS)
34384ab085aSmws 		smb_strip(shp);
34484ab085aSmws 
34584ab085aSmws 	return (shp);
34684ab085aSmws }
34784ab085aSmws 
34884ab085aSmws void
34984ab085aSmws smbios_close(smbios_hdl_t *shp)
35084ab085aSmws {
35184ab085aSmws 	uint_t i;
35284ab085aSmws 
35384ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
35484ab085aSmws 		smb_free(shp->sh_structs[i].smbst_strtab,
35584ab085aSmws 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
35684ab085aSmws 	}
35784ab085aSmws 
358c325726fSToomas Soome 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
35984ab085aSmws 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
36084ab085aSmws 
36184ab085aSmws 	if (shp->sh_flags & SMB_FL_BUFALLOC)
36284ab085aSmws 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
36384ab085aSmws 
36484ab085aSmws 	smb_free(shp, sizeof (smbios_hdl_t));
36584ab085aSmws }
36684ab085aSmws 
36784ab085aSmws /*
36884ab085aSmws  * Recompute the values of the entry point checksums based upon the content
36984ab085aSmws  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
37084ab085aSmws  * anyway in case future versioning requires variations in the algorithm.
37184ab085aSmws  */
37284ab085aSmws /*ARGSUSED*/
37384ab085aSmws void
37484ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
37584ab085aSmws {
37684ab085aSmws 	uchar_t *p, *q = (uchar_t *)ep;
37784ab085aSmws 	uint8_t esum = 0, isum = 0;
37884ab085aSmws 
379c325726fSToomas Soome 	switch (shp->sh_ent_type) {
380c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_21:
381c325726fSToomas Soome 		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
38284ab085aSmws 
383c325726fSToomas Soome 		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
384c325726fSToomas Soome 		    p < q + sizeof (*ep); p++) {
38584ab085aSmws 			isum += *p;
386c325726fSToomas Soome 		}
38784ab085aSmws 
388c325726fSToomas Soome 		ep->ep21.smbe_icksum = -isum;
38984ab085aSmws 
390c325726fSToomas Soome 		for (p = q; p < q + ep->ep21.smbe_elen; p++)
39184ab085aSmws 			esum += *p;
39284ab085aSmws 
393c325726fSToomas Soome 		ep->ep21.smbe_ecksum = -esum;
394c325726fSToomas Soome 		break;
395c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_30:
396c325726fSToomas Soome 		ep->ep30.smbe_ecksum = 0;
397c325726fSToomas Soome 		for (p = q; p < q + ep->ep30.smbe_elen; p++)
398c325726fSToomas Soome 			esum += *p;
399c325726fSToomas Soome 
400c325726fSToomas Soome 		ep->ep30.smbe_ecksum = -esum;
401c325726fSToomas Soome 		break;
402c325726fSToomas Soome 	default:
403c325726fSToomas Soome 		break;
404c325726fSToomas Soome 	}
40584ab085aSmws }
40684ab085aSmws 
40784ab085aSmws const void *
40884ab085aSmws smbios_buf(smbios_hdl_t *shp)
40984ab085aSmws {
41084ab085aSmws 	return (shp->sh_buf);
41184ab085aSmws }
41284ab085aSmws 
41384ab085aSmws size_t
41484ab085aSmws smbios_buflen(smbios_hdl_t *shp)
41584ab085aSmws {
41684ab085aSmws 	return (shp->sh_buflen);
41784ab085aSmws }
41884ab085aSmws 
41984ab085aSmws static smbios_struct_t *
42084ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
42184ab085aSmws {
42284ab085aSmws 	const smb_header_t *hdr = stp->smbst_hdr;
42384ab085aSmws 
42484ab085aSmws 	sp->smbstr_id = hdr->smbh_hdl;
42584ab085aSmws 	sp->smbstr_type = hdr->smbh_type;
42684ab085aSmws 	sp->smbstr_data = hdr;
42784ab085aSmws 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
42884ab085aSmws 
42984ab085aSmws 	return (sp);
43084ab085aSmws }
43184ab085aSmws 
43284ab085aSmws int
43384ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
43484ab085aSmws {
43584ab085aSmws 	const smb_struct_t *stp = smb_lookup_id(shp, id);
43684ab085aSmws 
43784ab085aSmws 	if (stp == NULL)
43884ab085aSmws 		return (-1); /* errno is set for us */
43984ab085aSmws 
44084ab085aSmws 	if (sp != NULL)
44184ab085aSmws 		(void) smb_export(stp, sp);
44284ab085aSmws 
44384ab085aSmws 	return (0);
44484ab085aSmws }
44584ab085aSmws 
44684ab085aSmws int
447074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
448074bb90dSTom Pothier {
449074bb90dSTom Pothier 	const smb_struct_t *stp = smb_lookup_type(shp, type);
450074bb90dSTom Pothier 
451074bb90dSTom Pothier 	if (stp == NULL)
452074bb90dSTom Pothier 		return (-1); /* errno is set for us */
453074bb90dSTom Pothier 
454074bb90dSTom Pothier 	if (sp != NULL)
455074bb90dSTom Pothier 		(void) smb_export(stp, sp);
456074bb90dSTom Pothier 
457074bb90dSTom Pothier 	return (0);
458074bb90dSTom Pothier }
459074bb90dSTom Pothier 
460074bb90dSTom Pothier int
46184ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
46284ab085aSmws {
46384ab085aSmws 	const smb_struct_t *sp = shp->sh_structs;
46484ab085aSmws 	smbios_struct_t s;
46584ab085aSmws 	int i, rv = 0;
46684ab085aSmws 
46784ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
46884ab085aSmws 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
46984ab085aSmws 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
47084ab085aSmws 			break;
47184ab085aSmws 	}
47284ab085aSmws 
47384ab085aSmws 	return (rv);
47484ab085aSmws }
47584ab085aSmws 
47684ab085aSmws const smb_struct_t *
47784ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type)
47884ab085aSmws {
47984ab085aSmws 	uint_t i;
48084ab085aSmws 
48184ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
48284ab085aSmws 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
48384ab085aSmws 			return (&shp->sh_structs[i]);
48484ab085aSmws 	}
48584ab085aSmws 
48684ab085aSmws 	(void) smb_set_errno(shp, ESMB_NOENT);
48784ab085aSmws 	return (NULL);
48884ab085aSmws }
48984ab085aSmws 
49084ab085aSmws const smb_struct_t *
49184ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id)
49284ab085aSmws {
49384ab085aSmws 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
49484ab085aSmws 
49584ab085aSmws 	switch (id) {
49684ab085aSmws 	case SMB_ID_NOTSUP:
49784ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOTSUP);
49884ab085aSmws 		return (NULL);
49984ab085aSmws 	case SMB_ID_NONE:
50084ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
50184ab085aSmws 		return (NULL);
50284ab085aSmws 	}
50384ab085aSmws 
50484ab085aSmws 	for (; stp != NULL; stp = stp->smbst_next) {
50584ab085aSmws 		if (stp->smbst_hdr->smbh_hdl == id)
50684ab085aSmws 			break;
50784ab085aSmws 	}
50884ab085aSmws 
50984ab085aSmws 	if (stp == NULL)
51084ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
51184ab085aSmws 
51284ab085aSmws 	return (stp);
51384ab085aSmws }
51484ab085aSmws 
51584ab085aSmws const char *
51684ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i)
51784ab085aSmws {
51884ab085aSmws 	if (i == 0 || i > stp->smbst_strtablen)
51984ab085aSmws 		return (_smb_emptystr);
52084ab085aSmws 	else
52184ab085aSmws 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
52284ab085aSmws }
52384ab085aSmws 
52484ab085aSmws int
52538d76b18SRobert Mustacchi smb_libgteq(smbios_hdl_t *shp, int version)
52638d76b18SRobert Mustacchi {
52738d76b18SRobert Mustacchi 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
52838d76b18SRobert Mustacchi 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
52938d76b18SRobert Mustacchi 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
53038d76b18SRobert Mustacchi }
53138d76b18SRobert Mustacchi 
53238d76b18SRobert Mustacchi int
53384ab085aSmws smb_gteq(smbios_hdl_t *shp, int version)
53484ab085aSmws {
53584ab085aSmws 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
53684ab085aSmws 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
53784ab085aSmws 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
53884ab085aSmws }
539b60ae21dSJonathan Matthew 
540b60ae21dSJonathan Matthew boolean_t
541b60ae21dSJonathan Matthew smbios_truncated(smbios_hdl_t *shp)
542b60ae21dSJonathan Matthew {
543b60ae21dSJonathan Matthew 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
544b60ae21dSJonathan Matthew }
545