xref: /titanic_51/usr/src/common/smbios/smb_open.c (revision c325726f85b6b96c35eff565c37aa401218c37d8)
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.
2438d76b18SRobert Mustacchi  * Copyright 2016 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 
75*c325726fSToomas Soome static int
76*c325726fSToomas Soome smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
77*c325726fSToomas Soome     int flags)
78*c325726fSToomas Soome {
79*c325726fSToomas Soome 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
80*c325726fSToomas Soome 		return (ESMB_HEADER);
81*c325726fSToomas Soome 
82*c325726fSToomas Soome 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
83*c325726fSToomas Soome 		return (ESMB_HEADER);
84*c325726fSToomas Soome 
85*c325726fSToomas Soome 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
86*c325726fSToomas Soome 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
87*c325726fSToomas Soome 
88*c325726fSToomas Soome 	if (!(flags & SMB_O_NOVERS)) {
89*c325726fSToomas Soome 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
90*c325726fSToomas Soome 			return (ESMB_NEW);
91*c325726fSToomas Soome 
92*c325726fSToomas Soome 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
93*c325726fSToomas Soome 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
94*c325726fSToomas Soome 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
95*c325726fSToomas Soome 			return (ESMB_OLD);
96*c325726fSToomas Soome 	}
97*c325726fSToomas Soome 
98*c325726fSToomas Soome 	if (len < sizeof (smb_header_t) ||
99*c325726fSToomas Soome 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
100*c325726fSToomas Soome 		return (ESMB_SHORT);
101*c325726fSToomas Soome 
102*c325726fSToomas Soome 	if (!(flags & SMB_O_NOCKSUM)) {
103*c325726fSToomas Soome 		uint8_t esum = 0, isum = 0;
104*c325726fSToomas Soome 		const uchar_t *p, *q;
105*c325726fSToomas Soome 		q = (uchar_t *)ep;
106*c325726fSToomas Soome 
107*c325726fSToomas Soome 		for (p = q; p < q + ep->smbe_elen; p++)
108*c325726fSToomas Soome 			esum += *p;
109*c325726fSToomas Soome 
110*c325726fSToomas Soome 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
111*c325726fSToomas Soome 			isum += *p;
112*c325726fSToomas Soome 
113*c325726fSToomas Soome 		if (esum != 0 || isum != 0) {
114*c325726fSToomas Soome 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
115*c325726fSToomas Soome 			return (ESMB_CKSUM);
116*c325726fSToomas Soome 		}
117*c325726fSToomas Soome 	}
118*c325726fSToomas Soome 
119*c325726fSToomas Soome 	/*
120*c325726fSToomas Soome 	 * Copy the entry point into our handle.  The underlying entry point
121*c325726fSToomas Soome 	 * may be larger than our structure definition, so reset smbe_elen
122*c325726fSToomas Soome 	 * to our internal size and recompute good checksums for our copy.
123*c325726fSToomas Soome 	 */
124*c325726fSToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
125*c325726fSToomas Soome 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
126*c325726fSToomas Soome 	shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
127*c325726fSToomas Soome 	smbios_checksum(shp, &shp->sh_ent);
128*c325726fSToomas Soome 
129*c325726fSToomas Soome 	shp->sh_ent_stnum = ep->smbe_stnum;
130*c325726fSToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
131*c325726fSToomas Soome 	return (0);
132*c325726fSToomas Soome }
133*c325726fSToomas Soome 
134*c325726fSToomas Soome static int
135*c325726fSToomas Soome smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
136*c325726fSToomas Soome     int flags)
137*c325726fSToomas Soome {
138*c325726fSToomas Soome 	if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
139*c325726fSToomas Soome 	    SMB3_ENTRY_EANCHORLEN))
140*c325726fSToomas Soome 		return (ESMB_HEADER);
141*c325726fSToomas Soome 
142*c325726fSToomas Soome 	smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
143*c325726fSToomas Soome 	    ep->smbe_major, ep->smbe_minor);
144*c325726fSToomas Soome 
145*c325726fSToomas Soome 	if (!(flags & SMB_O_NOVERS)) {
146*c325726fSToomas Soome 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
147*c325726fSToomas Soome 			return (ESMB_NEW);
148*c325726fSToomas Soome 
149*c325726fSToomas Soome 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
150*c325726fSToomas Soome 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
151*c325726fSToomas Soome 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
152*c325726fSToomas Soome 			return (ESMB_OLD);
153*c325726fSToomas Soome 	}
154*c325726fSToomas Soome 
155*c325726fSToomas Soome 	if (len < sizeof (smb_header_t) ||
156*c325726fSToomas Soome 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
157*c325726fSToomas Soome 		return (ESMB_SHORT);
158*c325726fSToomas Soome 
159*c325726fSToomas Soome 	if (!(flags & SMB_O_NOCKSUM)) {
160*c325726fSToomas Soome 		uint8_t esum = 0;
161*c325726fSToomas Soome 		const uchar_t *p, *q;
162*c325726fSToomas Soome 		q = (uchar_t *)ep;
163*c325726fSToomas Soome 
164*c325726fSToomas Soome 		for (p = q; p < q + ep->smbe_elen; p++)
165*c325726fSToomas Soome 			esum += *p;
166*c325726fSToomas Soome 
167*c325726fSToomas Soome 		if (esum != 0) {
168*c325726fSToomas Soome 			smb_dprintf(shp, "bad cksum: e=%x\n", esum);
169*c325726fSToomas Soome 			return (ESMB_CKSUM);
170*c325726fSToomas Soome 		}
171*c325726fSToomas Soome 	}
172*c325726fSToomas Soome 
173*c325726fSToomas Soome 	/*
174*c325726fSToomas Soome 	 * Copy the entry point into our handle.  The underlying entry point
175*c325726fSToomas Soome 	 * may be larger than our structure definition, so reset smbe_elen
176*c325726fSToomas Soome 	 * to our internal size and recompute good checksums for our copy.
177*c325726fSToomas Soome 	 */
178*c325726fSToomas Soome 	shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
179*c325726fSToomas Soome 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
180*c325726fSToomas Soome 	shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
181*c325726fSToomas Soome 	smbios_checksum(shp, &shp->sh_ent);
182*c325726fSToomas Soome 
183*c325726fSToomas Soome 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
184*c325726fSToomas Soome 
185*c325726fSToomas Soome 	return (0);
186*c325726fSToomas Soome }
187*c325726fSToomas Soome 
188*c325726fSToomas Soome static uint_t
189*c325726fSToomas Soome smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
190*c325726fSToomas Soome {
191*c325726fSToomas Soome 	uint_t i = 0;
192*c325726fSToomas Soome 	char *dmi;
193*c325726fSToomas Soome 	smb_header_t *hdr;
194*c325726fSToomas Soome 
195*c325726fSToomas Soome 	if (smbe_staddr == NULL)
196*c325726fSToomas Soome 		return (i);
197*c325726fSToomas Soome 
198*c325726fSToomas Soome 	for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
199*c325726fSToomas Soome 		hdr = (smb_header_t *)dmi;
200*c325726fSToomas Soome 		dmi += hdr->smbh_len;
201*c325726fSToomas Soome 		/*
202*c325726fSToomas Soome 		 * Search for the end of the string area.
203*c325726fSToomas Soome 		 */
204*c325726fSToomas Soome 		while (dmi + 1 < smbe_staddr + smbe_stlen &&
205*c325726fSToomas Soome 		    dmi[0] != '\0' && dmi[1] != '\0') {
206*c325726fSToomas Soome 			dmi++;
207*c325726fSToomas Soome 		}
208*c325726fSToomas Soome 		dmi += 2;
209*c325726fSToomas Soome 	}
210*c325726fSToomas Soome 	return (i);
211*c325726fSToomas Soome }
212*c325726fSToomas 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;
221*c325726fSToomas 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:
23284ab085aSmws 		break;
23384ab085aSmws 	default:
23484ab085aSmws 		return (smb_open_error(shp, errp, ESMB_VERSION));
23584ab085aSmws 	}
23684ab085aSmws 
23784ab085aSmws 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
23884ab085aSmws 		return (smb_open_error(shp, errp, ESMB_INVAL));
23984ab085aSmws 
24084ab085aSmws 	if (shp == NULL)
24184ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
24284ab085aSmws 
24384ab085aSmws 	if (_smb_debug)
24484ab085aSmws 		shp->sh_flags |= SMB_FL_DEBUG;
24584ab085aSmws 
246*c325726fSToomas Soome 	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
247*c325726fSToomas Soome 	if (err != 0) {
248*c325726fSToomas Soome 		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
249*c325726fSToomas Soome 		if (err != 0)
250*c325726fSToomas Soome 			return (smb_open_error(shp, errp, err));
251*c325726fSToomas Soome 		shp->sh_ent_stnum =
252*c325726fSToomas Soome 		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
25384ab085aSmws 	}
25484ab085aSmws 
25584ab085aSmws 	shp->sh_buf = buf;
25684ab085aSmws 	shp->sh_buflen = len;
257*c325726fSToomas Soome 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
25884ab085aSmws 	shp->sh_nstructs = 0;
25984ab085aSmws 	shp->sh_hashlen = _smb_hashlen;
26084ab085aSmws 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
26184ab085aSmws 	shp->sh_libvers = version;
26284ab085aSmws 
26384ab085aSmws 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
26484ab085aSmws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
26584ab085aSmws 
26684ab085aSmws 	hp = shp->sh_buf;
267*c325726fSToomas Soome 	switch (shp->sh_ent_type) {
268*c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_21:
269*c325726fSToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
270*c325726fSToomas Soome 		break;
271*c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_30:
272*c325726fSToomas Soome 		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
273*c325726fSToomas Soome 		break;
274*c325726fSToomas Soome 	default:
275*c325726fSToomas Soome 		return (smb_open_error(shp, errp, ESMB_VERSION));
276*c325726fSToomas Soome 	}
27784ab085aSmws 
278*c325726fSToomas Soome 	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
27984ab085aSmws 		smb_struct_t *stp = &shp->sh_structs[i];
28084ab085aSmws 		uint_t n = 0;
28184ab085aSmws 
282b60ae21dSJonathan Matthew 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
283b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
284b60ae21dSJonathan Matthew 			break;
285b60ae21dSJonathan Matthew 		}
28684ab085aSmws 
28784ab085aSmws 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
28884ab085aSmws 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
28984ab085aSmws 
29084ab085aSmws 		if (hp->smbh_type == SMB_TYPE_EOT)
29184ab085aSmws 			break; /* ignore any entries beyond end-of-table */
29284ab085aSmws 
293b60ae21dSJonathan Matthew 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
294b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
295b60ae21dSJonathan Matthew 			break;
296b60ae21dSJonathan Matthew 		}
297e4586ebfSmws 
29884ab085aSmws 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
29984ab085aSmws 		p = s = (const uchar_t *)hp + hp->smbh_len;
30084ab085aSmws 
30184ab085aSmws 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
30284ab085aSmws 			if (*p++ == '\0')
30384ab085aSmws 				n++; /* count strings until \0\0 delimiter */
30484ab085aSmws 		}
30584ab085aSmws 
306b60ae21dSJonathan Matthew 		if (p > q - 2) {
307b60ae21dSJonathan Matthew 			shp->sh_flags |= SMB_FL_TRUNC;
308b60ae21dSJonathan Matthew 			break;
309b60ae21dSJonathan Matthew 		}
31084ab085aSmws 
31184ab085aSmws 		if (p > s)
31284ab085aSmws 			n++; /* add one for final string in string table */
31384ab085aSmws 
31484ab085aSmws 		stp->smbst_hdr = hp;
31584ab085aSmws 		stp->smbst_str = s;
31684ab085aSmws 		stp->smbst_end = p;
31784ab085aSmws 		stp->smbst_next = shp->sh_hash[h];
31884ab085aSmws 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
31984ab085aSmws 		stp->smbst_strtablen = n;
32084ab085aSmws 
32184ab085aSmws 		if (n != 0 && stp->smbst_strtab == NULL)
32284ab085aSmws 			return (smb_open_error(shp, errp, ESMB_NOMEM));
32384ab085aSmws 
32484ab085aSmws 		shp->sh_hash[h] = stp;
32584ab085aSmws 		nhp = (void *)(p + 2);
32684ab085aSmws 		shp->sh_nstructs++;
32784ab085aSmws 
32884ab085aSmws 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
32984ab085aSmws 			if (*p == '\0') {
33084ab085aSmws 				stp->smbst_strtab[n++] =
33184ab085aSmws 				    (uint16_t)(s - stp->smbst_str);
33284ab085aSmws 				s = p + 1;
33384ab085aSmws 			}
33484ab085aSmws 		}
33584ab085aSmws 	}
33684ab085aSmws 
337b60ae21dSJonathan Matthew 	/* error out if we couldn't find any complete entries in the table */
338b60ae21dSJonathan Matthew 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
339b60ae21dSJonathan Matthew 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
340b60ae21dSJonathan Matthew 
34184ab085aSmws 	if (flags & SMB_O_ZIDS)
34284ab085aSmws 		smb_strip(shp);
34384ab085aSmws 
34484ab085aSmws 	return (shp);
34584ab085aSmws }
34684ab085aSmws 
34784ab085aSmws void
34884ab085aSmws smbios_close(smbios_hdl_t *shp)
34984ab085aSmws {
35084ab085aSmws 	uint_t i;
35184ab085aSmws 
35284ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
35384ab085aSmws 		smb_free(shp->sh_structs[i].smbst_strtab,
35484ab085aSmws 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
35584ab085aSmws 	}
35684ab085aSmws 
357*c325726fSToomas Soome 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
35884ab085aSmws 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
35984ab085aSmws 
36084ab085aSmws 	if (shp->sh_flags & SMB_FL_BUFALLOC)
36184ab085aSmws 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
36284ab085aSmws 
36384ab085aSmws 	smb_free(shp, sizeof (smbios_hdl_t));
36484ab085aSmws }
36584ab085aSmws 
36684ab085aSmws /*
36784ab085aSmws  * Recompute the values of the entry point checksums based upon the content
36884ab085aSmws  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
36984ab085aSmws  * anyway in case future versioning requires variations in the algorithm.
37084ab085aSmws  */
37184ab085aSmws /*ARGSUSED*/
37284ab085aSmws void
37384ab085aSmws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
37484ab085aSmws {
37584ab085aSmws 	uchar_t *p, *q = (uchar_t *)ep;
37684ab085aSmws 	uint8_t esum = 0, isum = 0;
37784ab085aSmws 
378*c325726fSToomas Soome 	switch (shp->sh_ent_type) {
379*c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_21:
380*c325726fSToomas Soome 		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
38184ab085aSmws 
382*c325726fSToomas Soome 		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
383*c325726fSToomas Soome 		    p < q + sizeof (*ep); p++) {
38484ab085aSmws 			isum += *p;
385*c325726fSToomas Soome 		}
38684ab085aSmws 
387*c325726fSToomas Soome 		ep->ep21.smbe_icksum = -isum;
38884ab085aSmws 
389*c325726fSToomas Soome 		for (p = q; p < q + ep->ep21.smbe_elen; p++)
39084ab085aSmws 			esum += *p;
39184ab085aSmws 
392*c325726fSToomas Soome 		ep->ep21.smbe_ecksum = -esum;
393*c325726fSToomas Soome 		break;
394*c325726fSToomas Soome 	case SMBIOS_ENTRY_POINT_30:
395*c325726fSToomas Soome 		ep->ep30.smbe_ecksum = 0;
396*c325726fSToomas Soome 		for (p = q; p < q + ep->ep30.smbe_elen; p++)
397*c325726fSToomas Soome 			esum += *p;
398*c325726fSToomas Soome 
399*c325726fSToomas Soome 		ep->ep30.smbe_ecksum = -esum;
400*c325726fSToomas Soome 		break;
401*c325726fSToomas Soome 	default:
402*c325726fSToomas Soome 		break;
403*c325726fSToomas Soome 	}
40484ab085aSmws }
40584ab085aSmws 
40684ab085aSmws const void *
40784ab085aSmws smbios_buf(smbios_hdl_t *shp)
40884ab085aSmws {
40984ab085aSmws 	return (shp->sh_buf);
41084ab085aSmws }
41184ab085aSmws 
41284ab085aSmws size_t
41384ab085aSmws smbios_buflen(smbios_hdl_t *shp)
41484ab085aSmws {
41584ab085aSmws 	return (shp->sh_buflen);
41684ab085aSmws }
41784ab085aSmws 
41884ab085aSmws static smbios_struct_t *
41984ab085aSmws smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
42084ab085aSmws {
42184ab085aSmws 	const smb_header_t *hdr = stp->smbst_hdr;
42284ab085aSmws 
42384ab085aSmws 	sp->smbstr_id = hdr->smbh_hdl;
42484ab085aSmws 	sp->smbstr_type = hdr->smbh_type;
42584ab085aSmws 	sp->smbstr_data = hdr;
42684ab085aSmws 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
42784ab085aSmws 
42884ab085aSmws 	return (sp);
42984ab085aSmws }
43084ab085aSmws 
43184ab085aSmws int
43284ab085aSmws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
43384ab085aSmws {
43484ab085aSmws 	const smb_struct_t *stp = smb_lookup_id(shp, id);
43584ab085aSmws 
43684ab085aSmws 	if (stp == NULL)
43784ab085aSmws 		return (-1); /* errno is set for us */
43884ab085aSmws 
43984ab085aSmws 	if (sp != NULL)
44084ab085aSmws 		(void) smb_export(stp, sp);
44184ab085aSmws 
44284ab085aSmws 	return (0);
44384ab085aSmws }
44484ab085aSmws 
44584ab085aSmws int
446074bb90dSTom Pothier smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
447074bb90dSTom Pothier {
448074bb90dSTom Pothier 	const smb_struct_t *stp = smb_lookup_type(shp, type);
449074bb90dSTom Pothier 
450074bb90dSTom Pothier 	if (stp == NULL)
451074bb90dSTom Pothier 		return (-1); /* errno is set for us */
452074bb90dSTom Pothier 
453074bb90dSTom Pothier 	if (sp != NULL)
454074bb90dSTom Pothier 		(void) smb_export(stp, sp);
455074bb90dSTom Pothier 
456074bb90dSTom Pothier 	return (0);
457074bb90dSTom Pothier }
458074bb90dSTom Pothier 
459074bb90dSTom Pothier int
46084ab085aSmws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
46184ab085aSmws {
46284ab085aSmws 	const smb_struct_t *sp = shp->sh_structs;
46384ab085aSmws 	smbios_struct_t s;
46484ab085aSmws 	int i, rv = 0;
46584ab085aSmws 
46684ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
46784ab085aSmws 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
46884ab085aSmws 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
46984ab085aSmws 			break;
47084ab085aSmws 	}
47184ab085aSmws 
47284ab085aSmws 	return (rv);
47384ab085aSmws }
47484ab085aSmws 
47584ab085aSmws const smb_struct_t *
47684ab085aSmws smb_lookup_type(smbios_hdl_t *shp, uint_t type)
47784ab085aSmws {
47884ab085aSmws 	uint_t i;
47984ab085aSmws 
48084ab085aSmws 	for (i = 0; i < shp->sh_nstructs; i++) {
48184ab085aSmws 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
48284ab085aSmws 			return (&shp->sh_structs[i]);
48384ab085aSmws 	}
48484ab085aSmws 
48584ab085aSmws 	(void) smb_set_errno(shp, ESMB_NOENT);
48684ab085aSmws 	return (NULL);
48784ab085aSmws }
48884ab085aSmws 
48984ab085aSmws const smb_struct_t *
49084ab085aSmws smb_lookup_id(smbios_hdl_t *shp, uint_t id)
49184ab085aSmws {
49284ab085aSmws 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
49384ab085aSmws 
49484ab085aSmws 	switch (id) {
49584ab085aSmws 	case SMB_ID_NOTSUP:
49684ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOTSUP);
49784ab085aSmws 		return (NULL);
49884ab085aSmws 	case SMB_ID_NONE:
49984ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
50084ab085aSmws 		return (NULL);
50184ab085aSmws 	}
50284ab085aSmws 
50384ab085aSmws 	for (; stp != NULL; stp = stp->smbst_next) {
50484ab085aSmws 		if (stp->smbst_hdr->smbh_hdl == id)
50584ab085aSmws 			break;
50684ab085aSmws 	}
50784ab085aSmws 
50884ab085aSmws 	if (stp == NULL)
50984ab085aSmws 		(void) smb_set_errno(shp, ESMB_NOENT);
51084ab085aSmws 
51184ab085aSmws 	return (stp);
51284ab085aSmws }
51384ab085aSmws 
51484ab085aSmws const char *
51584ab085aSmws smb_strptr(const smb_struct_t *stp, uint_t i)
51684ab085aSmws {
51784ab085aSmws 	if (i == 0 || i > stp->smbst_strtablen)
51884ab085aSmws 		return (_smb_emptystr);
51984ab085aSmws 	else
52084ab085aSmws 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
52184ab085aSmws }
52284ab085aSmws 
52384ab085aSmws int
52438d76b18SRobert Mustacchi smb_libgteq(smbios_hdl_t *shp, int version)
52538d76b18SRobert Mustacchi {
52638d76b18SRobert Mustacchi 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
52738d76b18SRobert Mustacchi 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
52838d76b18SRobert Mustacchi 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
52938d76b18SRobert Mustacchi }
53038d76b18SRobert Mustacchi 
53138d76b18SRobert Mustacchi int
53284ab085aSmws smb_gteq(smbios_hdl_t *shp, int version)
53384ab085aSmws {
53484ab085aSmws 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
53584ab085aSmws 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
53684ab085aSmws 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
53784ab085aSmws }
538b60ae21dSJonathan Matthew 
539b60ae21dSJonathan Matthew boolean_t
540b60ae21dSJonathan Matthew smbios_truncated(smbios_hdl_t *shp)
541b60ae21dSJonathan Matthew {
542b60ae21dSJonathan Matthew 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
543b60ae21dSJonathan Matthew }
544