xref: /illumos-gate/usr/src/common/smbios/smb_open.c (revision 8ad60789b9913d5b2118fe76d8cd7a661d8ae8b6)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 smbios_hdl_t *
77 smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
78     int version, int flags, int *errp)
79 {
80 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
81 	const smb_header_t *hp, *nhp;
82 	const uchar_t *p, *q, *s;
83 	uint_t i, h;
84 
85 	switch (version) {
86 	case SMB_VERSION_23:
87 	case SMB_VERSION_24:
88 		break;
89 	default:
90 		return (smb_open_error(shp, errp, ESMB_VERSION));
91 	}
92 
93 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
94 		return (smb_open_error(shp, errp, ESMB_INVAL));
95 
96 	if (shp == NULL)
97 		return (smb_open_error(shp, errp, ESMB_NOMEM));
98 
99 	if (_smb_debug)
100 		shp->sh_flags |= SMB_FL_DEBUG;
101 
102 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
103 		return (smb_open_error(shp, errp, ESMB_HEADER));
104 
105 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
106 		return (smb_open_error(shp, errp, ESMB_HEADER));
107 
108 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
109 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
110 
111 	if (!(flags & SMB_O_NOVERS)) {
112 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
113 			return (smb_open_error(shp, errp, ESMB_NEW));
114 
115 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
116 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
117 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
118 			return (smb_open_error(shp, errp, ESMB_OLD));
119 	}
120 
121 	if (len < sizeof (smb_header_t) ||
122 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
123 		return (smb_open_error(shp, errp, ESMB_SHORT));
124 
125 	if (!(flags & SMB_O_NOCKSUM)) {
126 		uint8_t esum = 0, isum = 0;
127 		q = (uchar_t *)ep;
128 
129 		for (p = q; p < q + ep->smbe_elen; p++)
130 			esum += *p;
131 
132 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
133 			isum += *p;
134 
135 		if (esum != 0 || isum != 0) {
136 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
137 			return (smb_open_error(shp, errp, ESMB_CKSUM));
138 		}
139 	}
140 
141 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
142 	shp->sh_buf = buf;
143 	shp->sh_buflen = len;
144 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
145 	shp->sh_nstructs = 0;
146 	shp->sh_hashlen = _smb_hashlen;
147 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
148 	shp->sh_libvers = version;
149 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
150 
151 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
152 		return (smb_open_error(shp, errp, ESMB_NOMEM));
153 
154 	hp = shp->sh_buf;
155 	q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
156 
157 	for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
158 		smb_struct_t *stp = &shp->sh_structs[i];
159 		uint_t n = 0;
160 
161 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q)
162 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
163 
164 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
165 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
166 
167 		if ((const uchar_t *)hp + hp->smbh_len > q - 2)
168 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
169 
170 		if (hp->smbh_type == SMB_TYPE_EOT)
171 			break; /* ignore any entries beyond end-of-table */
172 
173 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
174 		p = s = (const uchar_t *)hp + hp->smbh_len;
175 
176 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
177 			if (*p++ == '\0')
178 				n++; /* count strings until \0\0 delimiter */
179 		}
180 
181 		if (p > q - 2)
182 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
183 
184 		if (p > s)
185 			n++; /* add one for final string in string table */
186 
187 		stp->smbst_hdr = hp;
188 		stp->smbst_str = s;
189 		stp->smbst_end = p;
190 		stp->smbst_next = shp->sh_hash[h];
191 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
192 		stp->smbst_strtablen = n;
193 
194 		if (n != 0 && stp->smbst_strtab == NULL)
195 			return (smb_open_error(shp, errp, ESMB_NOMEM));
196 
197 		shp->sh_hash[h] = stp;
198 		nhp = (void *)(p + 2);
199 		shp->sh_nstructs++;
200 
201 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
202 			if (*p == '\0') {
203 				stp->smbst_strtab[n++] =
204 				    (uint16_t)(s - stp->smbst_str);
205 				s = p + 1;
206 			}
207 		}
208 	}
209 
210 	if (flags & SMB_O_ZIDS)
211 		smb_strip(shp);
212 
213 	return (shp);
214 }
215 
216 void
217 smbios_close(smbios_hdl_t *shp)
218 {
219 	const smbios_entry_t *ep = &shp->sh_ent;
220 	uint_t i;
221 
222 	for (i = 0; i < shp->sh_nstructs; i++) {
223 		smb_free(shp->sh_structs[i].smbst_strtab,
224 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
225 	}
226 
227 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
228 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
229 
230 	if (shp->sh_flags & SMB_FL_BUFALLOC)
231 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
232 
233 	smb_free(shp, sizeof (smbios_hdl_t));
234 }
235 
236 /*
237  * Recompute the values of the entry point checksums based upon the content
238  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
239  * anyway in case future versioning requires variations in the algorithm.
240  */
241 /*ARGSUSED*/
242 void
243 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
244 {
245 	uchar_t *p, *q = (uchar_t *)ep;
246 	uint8_t esum = 0, isum = 0;
247 
248 	ep->smbe_ecksum = ep->smbe_icksum = 0;
249 
250 	for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
251 		isum += *p;
252 
253 	ep->smbe_icksum = -isum;
254 
255 	for (p = q; p < q + ep->smbe_elen; p++)
256 		esum += *p;
257 
258 	ep->smbe_ecksum = -esum;
259 }
260 
261 const void *
262 smbios_buf(smbios_hdl_t *shp)
263 {
264 	return (shp->sh_buf);
265 }
266 
267 size_t
268 smbios_buflen(smbios_hdl_t *shp)
269 {
270 	return (shp->sh_buflen);
271 }
272 
273 static smbios_struct_t *
274 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
275 {
276 	const smb_header_t *hdr = stp->smbst_hdr;
277 
278 	sp->smbstr_id = hdr->smbh_hdl;
279 	sp->smbstr_type = hdr->smbh_type;
280 	sp->smbstr_data = hdr;
281 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
282 
283 	return (sp);
284 }
285 
286 int
287 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
288 {
289 	const smb_struct_t *stp = smb_lookup_id(shp, id);
290 
291 	if (stp == NULL)
292 		return (-1); /* errno is set for us */
293 
294 	if (sp != NULL)
295 		(void) smb_export(stp, sp);
296 
297 	return (0);
298 }
299 
300 int
301 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
302 {
303 	const smb_struct_t *sp = shp->sh_structs;
304 	smbios_struct_t s;
305 	int i, rv = 0;
306 
307 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
308 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
309 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
310 			break;
311 	}
312 
313 	return (rv);
314 }
315 
316 const smb_struct_t *
317 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
318 {
319 	uint_t i;
320 
321 	for (i = 0; i < shp->sh_nstructs; i++) {
322 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
323 			return (&shp->sh_structs[i]);
324 	}
325 
326 	(void) smb_set_errno(shp, ESMB_NOENT);
327 	return (NULL);
328 }
329 
330 const smb_struct_t *
331 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
332 {
333 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
334 
335 	switch (id) {
336 	case SMB_ID_NOTSUP:
337 		(void) smb_set_errno(shp, ESMB_NOTSUP);
338 		return (NULL);
339 	case SMB_ID_NONE:
340 		(void) smb_set_errno(shp, ESMB_NOENT);
341 		return (NULL);
342 	}
343 
344 	for (; stp != NULL; stp = stp->smbst_next) {
345 		if (stp->smbst_hdr->smbh_hdl == id)
346 			break;
347 	}
348 
349 	if (stp == NULL)
350 		(void) smb_set_errno(shp, ESMB_NOENT);
351 
352 	return (stp);
353 }
354 
355 const char *
356 smb_strptr(const smb_struct_t *stp, uint_t i)
357 {
358 	if (i == 0 || i > stp->smbst_strtablen)
359 		return (_smb_emptystr);
360 	else
361 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
362 }
363 
364 int
365 smb_gteq(smbios_hdl_t *shp, int version)
366 {
367 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
368 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
369 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
370 }
371