xref: /onnv-gate/usr/src/common/smbios/smb_open.c (revision 10942:eaa343de0d06)
1437Smws /*
2437Smws  * CDDL HEADER START
3437Smws  *
4437Smws  * The contents of this file are subject to the terms of the
5*10942STom.Pothier@Sun.COM  * Common Development and Distribution License (the "License").
6*10942STom.Pothier@Sun.COM  * You may not use this file except in compliance with the License.
7437Smws  *
8437Smws  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9437Smws  * or http://www.opensolaris.org/os/licensing.
10437Smws  * See the License for the specific language governing permissions
11437Smws  * and limitations under the License.
12437Smws  *
13437Smws  * When distributing Covered Code, include this CDDL HEADER in each
14437Smws  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15437Smws  * If applicable, add the following below this CDDL HEADER, with the
16437Smws  * fields enclosed by brackets "[]" replaced with your own identifying
17437Smws  * information: Portions Copyright [yyyy] [name of copyright owner]
18437Smws  *
19437Smws  * CDDL HEADER END
20437Smws  */
21437Smws 
22437Smws /*
23*10942STom.Pothier@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24437Smws  * Use is subject to license terms.
25437Smws  */
26437Smws 
27437Smws #include <sys/smbios_impl.h>
28437Smws 
29437Smws static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
30437Smws static const char _smb_emptystr[] = "";		/* empty string to return */
31437Smws int _smb_debug = 0;				/* default debug mode */
32437Smws 
33437Smws /*
34437Smws  * Strip out identification information for you privacy weenies.  This is quite
35437Smws  * simple using our smbios_info_common() abstraction: we just locate any serial
36437Smws  * numbers and asset tags for each record, and then zero out those strings.
37437Smws  * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
38437Smws  * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
39437Smws  * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
40437Smws  * the SMBIOS string table has a special meaning (denotes end-of-record).
41437Smws  */
42437Smws static void
smb_strip(smbios_hdl_t * shp)43437Smws smb_strip(smbios_hdl_t *shp)
44437Smws {
45437Smws 	uint_t i;
46437Smws 
47437Smws 	for (i = 0; i < shp->sh_nstructs; i++) {
48437Smws 		const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
49437Smws 		smbios_info_t info;
50437Smws 		char *p;
51437Smws 
52437Smws 		if (hp->smbh_type == SMB_TYPE_SYSTEM &&
53437Smws 		    hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
54437Smws 			smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
55437Smws 			bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
56437Smws 		}
57437Smws 
58437Smws 		if (hp->smbh_type == SMB_TYPE_BATTERY &&
59437Smws 		    hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
60437Smws 			smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
61437Smws 			bp->smbbat_ssn = 0;
62437Smws 		}
63437Smws 
64437Smws 		if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
65437Smws 			for (p = (char *)info.smbi_serial; *p != '\0'; p++)
66437Smws 				*p = '0';
67437Smws 			for (p = (char *)info.smbi_asset; *p != '\0'; p++)
68437Smws 				*p = '0';
69437Smws 		}
70437Smws 	}
71437Smws }
72437Smws 
73437Smws smbios_hdl_t *
smbios_bufopen(const smbios_entry_t * ep,const void * buf,size_t len,int version,int flags,int * errp)74437Smws smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
75437Smws     int version, int flags, int *errp)
76437Smws {
77437Smws 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
78437Smws 	const smb_header_t *hp, *nhp;
79437Smws 	const uchar_t *p, *q, *s;
80437Smws 	uint_t i, h;
81437Smws 
82437Smws 	switch (version) {
83437Smws 	case SMB_VERSION_23:
84437Smws 	case SMB_VERSION_24:
85437Smws 		break;
86437Smws 	default:
87437Smws 		return (smb_open_error(shp, errp, ESMB_VERSION));
88437Smws 	}
89437Smws 
90437Smws 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
91437Smws 		return (smb_open_error(shp, errp, ESMB_INVAL));
92437Smws 
93437Smws 	if (shp == NULL)
94437Smws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
95437Smws 
96437Smws 	if (_smb_debug)
97437Smws 		shp->sh_flags |= SMB_FL_DEBUG;
98437Smws 
99437Smws 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
100437Smws 		return (smb_open_error(shp, errp, ESMB_HEADER));
101437Smws 
102437Smws 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
103437Smws 		return (smb_open_error(shp, errp, ESMB_HEADER));
104437Smws 
105437Smws 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
106437Smws 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
107437Smws 
108437Smws 	if (!(flags & SMB_O_NOVERS)) {
109437Smws 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
110437Smws 			return (smb_open_error(shp, errp, ESMB_NEW));
111437Smws 
112437Smws 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
113437Smws 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
114437Smws 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
115437Smws 			return (smb_open_error(shp, errp, ESMB_OLD));
116437Smws 	}
117437Smws 
118437Smws 	if (len < sizeof (smb_header_t) ||
119437Smws 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
120437Smws 		return (smb_open_error(shp, errp, ESMB_SHORT));
121437Smws 
122437Smws 	if (!(flags & SMB_O_NOCKSUM)) {
123437Smws 		uint8_t esum = 0, isum = 0;
124437Smws 		q = (uchar_t *)ep;
125437Smws 
126437Smws 		for (p = q; p < q + ep->smbe_elen; p++)
127437Smws 			esum += *p;
128437Smws 
129437Smws 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
130437Smws 			isum += *p;
131437Smws 
132437Smws 		if (esum != 0 || isum != 0) {
133437Smws 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
134437Smws 			return (smb_open_error(shp, errp, ESMB_CKSUM));
135437Smws 		}
136437Smws 	}
137437Smws 
1381222Smws 	/*
1391222Smws 	 * Copy the entry point into our handle.  The underlying entry point
1401222Smws 	 * may be larger than our structure definition, so reset smbe_elen
1411222Smws 	 * to our internal size and recompute good checksums for our copy.
1421222Smws 	 */
143437Smws 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
1441222Smws 	shp->sh_ent.smbe_elen = sizeof (smbios_entry_t);
1451222Smws 	smbios_checksum(shp, &shp->sh_ent);
1461222Smws 
147437Smws 	shp->sh_buf = buf;
148437Smws 	shp->sh_buflen = len;
149437Smws 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
150437Smws 	shp->sh_nstructs = 0;
151437Smws 	shp->sh_hashlen = _smb_hashlen;
152437Smws 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
153437Smws 	shp->sh_libvers = version;
154437Smws 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
155437Smws 
156437Smws 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
157437Smws 		return (smb_open_error(shp, errp, ESMB_NOMEM));
158437Smws 
159437Smws 	hp = shp->sh_buf;
160437Smws 	q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
161437Smws 
162437Smws 	for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
163437Smws 		smb_struct_t *stp = &shp->sh_structs[i];
164437Smws 		uint_t n = 0;
165437Smws 
166437Smws 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q)
167437Smws 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
168437Smws 
169437Smws 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
170437Smws 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
171437Smws 
1721222Smws 		if (hp->smbh_type == SMB_TYPE_EOT)
1731222Smws 			break; /* ignore any entries beyond end-of-table */
1741222Smws 
175437Smws 		if ((const uchar_t *)hp + hp->smbh_len > q - 2)
176437Smws 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
177437Smws 
178437Smws 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
179437Smws 		p = s = (const uchar_t *)hp + hp->smbh_len;
180437Smws 
181437Smws 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
182437Smws 			if (*p++ == '\0')
183437Smws 				n++; /* count strings until \0\0 delimiter */
184437Smws 		}
185437Smws 
186437Smws 		if (p > q - 2)
187437Smws 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
188437Smws 
189437Smws 		if (p > s)
190437Smws 			n++; /* add one for final string in string table */
191437Smws 
192437Smws 		stp->smbst_hdr = hp;
193437Smws 		stp->smbst_str = s;
194437Smws 		stp->smbst_end = p;
195437Smws 		stp->smbst_next = shp->sh_hash[h];
196437Smws 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
197437Smws 		stp->smbst_strtablen = n;
198437Smws 
199437Smws 		if (n != 0 && stp->smbst_strtab == NULL)
200437Smws 			return (smb_open_error(shp, errp, ESMB_NOMEM));
201437Smws 
202437Smws 		shp->sh_hash[h] = stp;
203437Smws 		nhp = (void *)(p + 2);
204437Smws 		shp->sh_nstructs++;
205437Smws 
206437Smws 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
207437Smws 			if (*p == '\0') {
208437Smws 				stp->smbst_strtab[n++] =
209437Smws 				    (uint16_t)(s - stp->smbst_str);
210437Smws 				s = p + 1;
211437Smws 			}
212437Smws 		}
213437Smws 	}
214437Smws 
215437Smws 	if (flags & SMB_O_ZIDS)
216437Smws 		smb_strip(shp);
217437Smws 
218437Smws 	return (shp);
219437Smws }
220437Smws 
221437Smws void
smbios_close(smbios_hdl_t * shp)222437Smws smbios_close(smbios_hdl_t *shp)
223437Smws {
224437Smws 	const smbios_entry_t *ep = &shp->sh_ent;
225437Smws 	uint_t i;
226437Smws 
227437Smws 	for (i = 0; i < shp->sh_nstructs; i++) {
228437Smws 		smb_free(shp->sh_structs[i].smbst_strtab,
229437Smws 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
230437Smws 	}
231437Smws 
232437Smws 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
233437Smws 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
234437Smws 
235437Smws 	if (shp->sh_flags & SMB_FL_BUFALLOC)
236437Smws 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
237437Smws 
238437Smws 	smb_free(shp, sizeof (smbios_hdl_t));
239437Smws }
240437Smws 
241437Smws /*
242437Smws  * Recompute the values of the entry point checksums based upon the content
243437Smws  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
244437Smws  * anyway in case future versioning requires variations in the algorithm.
245437Smws  */
246437Smws /*ARGSUSED*/
247437Smws void
smbios_checksum(smbios_hdl_t * shp,smbios_entry_t * ep)248437Smws smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
249437Smws {
250437Smws 	uchar_t *p, *q = (uchar_t *)ep;
251437Smws 	uint8_t esum = 0, isum = 0;
252437Smws 
253437Smws 	ep->smbe_ecksum = ep->smbe_icksum = 0;
254437Smws 
255437Smws 	for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
256437Smws 		isum += *p;
257437Smws 
258437Smws 	ep->smbe_icksum = -isum;
259437Smws 
260437Smws 	for (p = q; p < q + ep->smbe_elen; p++)
261437Smws 		esum += *p;
262437Smws 
263437Smws 	ep->smbe_ecksum = -esum;
264437Smws }
265437Smws 
266437Smws const void *
smbios_buf(smbios_hdl_t * shp)267437Smws smbios_buf(smbios_hdl_t *shp)
268437Smws {
269437Smws 	return (shp->sh_buf);
270437Smws }
271437Smws 
272437Smws size_t
smbios_buflen(smbios_hdl_t * shp)273437Smws smbios_buflen(smbios_hdl_t *shp)
274437Smws {
275437Smws 	return (shp->sh_buflen);
276437Smws }
277437Smws 
278437Smws static smbios_struct_t *
smb_export(const smb_struct_t * stp,smbios_struct_t * sp)279437Smws smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
280437Smws {
281437Smws 	const smb_header_t *hdr = stp->smbst_hdr;
282437Smws 
283437Smws 	sp->smbstr_id = hdr->smbh_hdl;
284437Smws 	sp->smbstr_type = hdr->smbh_type;
285437Smws 	sp->smbstr_data = hdr;
286437Smws 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
287437Smws 
288437Smws 	return (sp);
289437Smws }
290437Smws 
291437Smws int
smbios_lookup_id(smbios_hdl_t * shp,id_t id,smbios_struct_t * sp)292437Smws smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
293437Smws {
294437Smws 	const smb_struct_t *stp = smb_lookup_id(shp, id);
295437Smws 
296437Smws 	if (stp == NULL)
297437Smws 		return (-1); /* errno is set for us */
298437Smws 
299437Smws 	if (sp != NULL)
300437Smws 		(void) smb_export(stp, sp);
301437Smws 
302437Smws 	return (0);
303437Smws }
304437Smws 
305437Smws int
smbios_lookup_type(smbios_hdl_t * shp,uint_t type,smbios_struct_t * sp)306*10942STom.Pothier@Sun.COM smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
307*10942STom.Pothier@Sun.COM {
308*10942STom.Pothier@Sun.COM 	const smb_struct_t *stp = smb_lookup_type(shp, type);
309*10942STom.Pothier@Sun.COM 
310*10942STom.Pothier@Sun.COM 	if (stp == NULL)
311*10942STom.Pothier@Sun.COM 		return (-1); /* errno is set for us */
312*10942STom.Pothier@Sun.COM 
313*10942STom.Pothier@Sun.COM 	if (sp != NULL)
314*10942STom.Pothier@Sun.COM 		(void) smb_export(stp, sp);
315*10942STom.Pothier@Sun.COM 
316*10942STom.Pothier@Sun.COM 	return (0);
317*10942STom.Pothier@Sun.COM }
318*10942STom.Pothier@Sun.COM 
319*10942STom.Pothier@Sun.COM int
smbios_iter(smbios_hdl_t * shp,smbios_struct_f * func,void * data)320437Smws smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
321437Smws {
322437Smws 	const smb_struct_t *sp = shp->sh_structs;
323437Smws 	smbios_struct_t s;
324437Smws 	int i, rv = 0;
325437Smws 
326437Smws 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
327437Smws 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
328437Smws 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
329437Smws 			break;
330437Smws 	}
331437Smws 
332437Smws 	return (rv);
333437Smws }
334437Smws 
335437Smws const smb_struct_t *
smb_lookup_type(smbios_hdl_t * shp,uint_t type)336437Smws smb_lookup_type(smbios_hdl_t *shp, uint_t type)
337437Smws {
338437Smws 	uint_t i;
339437Smws 
340437Smws 	for (i = 0; i < shp->sh_nstructs; i++) {
341437Smws 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
342437Smws 			return (&shp->sh_structs[i]);
343437Smws 	}
344437Smws 
345437Smws 	(void) smb_set_errno(shp, ESMB_NOENT);
346437Smws 	return (NULL);
347437Smws }
348437Smws 
349437Smws const smb_struct_t *
smb_lookup_id(smbios_hdl_t * shp,uint_t id)350437Smws smb_lookup_id(smbios_hdl_t *shp, uint_t id)
351437Smws {
352437Smws 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
353437Smws 
354437Smws 	switch (id) {
355437Smws 	case SMB_ID_NOTSUP:
356437Smws 		(void) smb_set_errno(shp, ESMB_NOTSUP);
357437Smws 		return (NULL);
358437Smws 	case SMB_ID_NONE:
359437Smws 		(void) smb_set_errno(shp, ESMB_NOENT);
360437Smws 		return (NULL);
361437Smws 	}
362437Smws 
363437Smws 	for (; stp != NULL; stp = stp->smbst_next) {
364437Smws 		if (stp->smbst_hdr->smbh_hdl == id)
365437Smws 			break;
366437Smws 	}
367437Smws 
368437Smws 	if (stp == NULL)
369437Smws 		(void) smb_set_errno(shp, ESMB_NOENT);
370437Smws 
371437Smws 	return (stp);
372437Smws }
373437Smws 
374437Smws const char *
smb_strptr(const smb_struct_t * stp,uint_t i)375437Smws smb_strptr(const smb_struct_t *stp, uint_t i)
376437Smws {
377437Smws 	if (i == 0 || i > stp->smbst_strtablen)
378437Smws 		return (_smb_emptystr);
379437Smws 	else
380437Smws 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
381437Smws }
382437Smws 
383437Smws int
smb_gteq(smbios_hdl_t * shp,int version)384437Smws smb_gteq(smbios_hdl_t *shp, int version)
385437Smws {
386437Smws 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
387437Smws 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
388437Smws 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
389437Smws }
390