xref: /titanic_51/usr/src/common/smbios/smb_open.c (revision 68c47f65208790c466e5e484f2293d3baed71c6a)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/smbios_impl.h>
28 
29 static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
30 static const char _smb_emptystr[] = "";		/* empty string to return */
31 int _smb_debug = 0;				/* default debug mode */
32 
33 /*
34  * Strip out identification information for you privacy weenies.  This is quite
35  * simple using our smbios_info_common() abstraction: we just locate any serial
36  * numbers and asset tags for each record, and then zero out those strings.
37  * Then we must handle two special cases: SMB_TYPE_SYSTEM holds a 16-byte UUID
38  * and SMB_TYPE_BATTERY stores a Smart Battery Data Spec 16-bit serial number.
39  * We use a literal '0' rather than '\0' for zeroing strings because \0\0 in
40  * the SMBIOS string table has a special meaning (denotes end-of-record).
41  */
42 static void
43 smb_strip(smbios_hdl_t *shp)
44 {
45 	uint_t i;
46 
47 	for (i = 0; i < shp->sh_nstructs; i++) {
48 		const smb_header_t *hp = shp->sh_structs[i].smbst_hdr;
49 		smbios_info_t info;
50 		char *p;
51 
52 		if (hp->smbh_type == SMB_TYPE_SYSTEM &&
53 		    hp->smbh_len >= offsetof(smb_system_t, smbsi_wakeup)) {
54 			smb_system_t *sp = (smb_system_t *)(uintptr_t)hp;
55 			bzero(sp->smbsi_uuid, sizeof (sp->smbsi_uuid));
56 		}
57 
58 		if (hp->smbh_type == SMB_TYPE_BATTERY &&
59 		    hp->smbh_len >= offsetof(smb_battery_t, smbbat_sdate)) {
60 			smb_battery_t *bp = (smb_battery_t *)(uintptr_t)hp;
61 			bp->smbbat_ssn = 0;
62 		}
63 
64 		if (smbios_info_common(shp, hp->smbh_hdl, &info) != SMB_ERR) {
65 			for (p = (char *)info.smbi_serial; *p != '\0'; p++)
66 				*p = '0';
67 			for (p = (char *)info.smbi_asset; *p != '\0'; p++)
68 				*p = '0';
69 		}
70 	}
71 }
72 
73 smbios_hdl_t *
74 smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
75     int version, int flags, int *errp)
76 {
77 	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
78 	const smb_header_t *hp, *nhp;
79 	const uchar_t *p, *q, *s;
80 	uint_t i, h;
81 
82 	switch (version) {
83 	case SMB_VERSION_23:
84 	case SMB_VERSION_24:
85 		break;
86 	default:
87 		return (smb_open_error(shp, errp, ESMB_VERSION));
88 	}
89 
90 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
91 		return (smb_open_error(shp, errp, ESMB_INVAL));
92 
93 	if (shp == NULL)
94 		return (smb_open_error(shp, errp, ESMB_NOMEM));
95 
96 	if (_smb_debug)
97 		shp->sh_flags |= SMB_FL_DEBUG;
98 
99 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
100 		return (smb_open_error(shp, errp, ESMB_HEADER));
101 
102 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
103 		return (smb_open_error(shp, errp, ESMB_HEADER));
104 
105 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
106 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
107 
108 	if (!(flags & SMB_O_NOVERS)) {
109 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
110 			return (smb_open_error(shp, errp, ESMB_NEW));
111 
112 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
113 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
114 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
115 			return (smb_open_error(shp, errp, ESMB_OLD));
116 	}
117 
118 	if (len < sizeof (smb_header_t) ||
119 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
120 		return (smb_open_error(shp, errp, ESMB_SHORT));
121 
122 	if (!(flags & SMB_O_NOCKSUM)) {
123 		uint8_t esum = 0, isum = 0;
124 		q = (uchar_t *)ep;
125 
126 		for (p = q; p < q + ep->smbe_elen; p++)
127 			esum += *p;
128 
129 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
130 			isum += *p;
131 
132 		if (esum != 0 || isum != 0) {
133 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
134 			return (smb_open_error(shp, errp, ESMB_CKSUM));
135 		}
136 	}
137 
138 	/*
139 	 * Copy the entry point into our handle.  The underlying entry point
140 	 * may be larger than our structure definition, so reset smbe_elen
141 	 * to our internal size and recompute good checksums for our copy.
142 	 */
143 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
144 	shp->sh_ent.smbe_elen = sizeof (smbios_entry_t);
145 	smbios_checksum(shp, &shp->sh_ent);
146 
147 	shp->sh_buf = buf;
148 	shp->sh_buflen = len;
149 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
150 	shp->sh_nstructs = 0;
151 	shp->sh_hashlen = _smb_hashlen;
152 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
153 	shp->sh_libvers = version;
154 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
155 
156 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
157 		return (smb_open_error(shp, errp, ESMB_NOMEM));
158 
159 	hp = shp->sh_buf;
160 	q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
161 
162 	for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
163 		smb_struct_t *stp = &shp->sh_structs[i];
164 		uint_t n = 0;
165 
166 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q)
167 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
168 
169 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
170 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
171 
172 		if (hp->smbh_type == SMB_TYPE_EOT)
173 			break; /* ignore any entries beyond end-of-table */
174 
175 		if ((const uchar_t *)hp + hp->smbh_len > q - 2)
176 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
177 
178 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
179 		p = s = (const uchar_t *)hp + hp->smbh_len;
180 
181 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
182 			if (*p++ == '\0')
183 				n++; /* count strings until \0\0 delimiter */
184 		}
185 
186 		if (p > q - 2)
187 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
188 
189 		if (p > s)
190 			n++; /* add one for final string in string table */
191 
192 		stp->smbst_hdr = hp;
193 		stp->smbst_str = s;
194 		stp->smbst_end = p;
195 		stp->smbst_next = shp->sh_hash[h];
196 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
197 		stp->smbst_strtablen = n;
198 
199 		if (n != 0 && stp->smbst_strtab == NULL)
200 			return (smb_open_error(shp, errp, ESMB_NOMEM));
201 
202 		shp->sh_hash[h] = stp;
203 		nhp = (void *)(p + 2);
204 		shp->sh_nstructs++;
205 
206 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
207 			if (*p == '\0') {
208 				stp->smbst_strtab[n++] =
209 				    (uint16_t)(s - stp->smbst_str);
210 				s = p + 1;
211 			}
212 		}
213 	}
214 
215 	if (flags & SMB_O_ZIDS)
216 		smb_strip(shp);
217 
218 	return (shp);
219 }
220 
221 void
222 smbios_close(smbios_hdl_t *shp)
223 {
224 	const smbios_entry_t *ep = &shp->sh_ent;
225 	uint_t i;
226 
227 	for (i = 0; i < shp->sh_nstructs; i++) {
228 		smb_free(shp->sh_structs[i].smbst_strtab,
229 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
230 	}
231 
232 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
233 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
234 
235 	if (shp->sh_flags & SMB_FL_BUFALLOC)
236 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
237 
238 	smb_free(shp, sizeof (smbios_hdl_t));
239 }
240 
241 /*
242  * Recompute the values of the entry point checksums based upon the content
243  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
244  * anyway in case future versioning requires variations in the algorithm.
245  */
246 /*ARGSUSED*/
247 void
248 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
249 {
250 	uchar_t *p, *q = (uchar_t *)ep;
251 	uint8_t esum = 0, isum = 0;
252 
253 	ep->smbe_ecksum = ep->smbe_icksum = 0;
254 
255 	for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
256 		isum += *p;
257 
258 	ep->smbe_icksum = -isum;
259 
260 	for (p = q; p < q + ep->smbe_elen; p++)
261 		esum += *p;
262 
263 	ep->smbe_ecksum = -esum;
264 }
265 
266 const void *
267 smbios_buf(smbios_hdl_t *shp)
268 {
269 	return (shp->sh_buf);
270 }
271 
272 size_t
273 smbios_buflen(smbios_hdl_t *shp)
274 {
275 	return (shp->sh_buflen);
276 }
277 
278 static smbios_struct_t *
279 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
280 {
281 	const smb_header_t *hdr = stp->smbst_hdr;
282 
283 	sp->smbstr_id = hdr->smbh_hdl;
284 	sp->smbstr_type = hdr->smbh_type;
285 	sp->smbstr_data = hdr;
286 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
287 
288 	return (sp);
289 }
290 
291 int
292 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
293 {
294 	const smb_struct_t *stp = smb_lookup_id(shp, id);
295 
296 	if (stp == NULL)
297 		return (-1); /* errno is set for us */
298 
299 	if (sp != NULL)
300 		(void) smb_export(stp, sp);
301 
302 	return (0);
303 }
304 
305 int
306 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
307 {
308 	const smb_struct_t *stp = smb_lookup_type(shp, type);
309 
310 	if (stp == NULL)
311 		return (-1); /* errno is set for us */
312 
313 	if (sp != NULL)
314 		(void) smb_export(stp, sp);
315 
316 	return (0);
317 }
318 
319 int
320 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
321 {
322 	const smb_struct_t *sp = shp->sh_structs;
323 	smbios_struct_t s;
324 	int i, rv = 0;
325 
326 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
327 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
328 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
329 			break;
330 	}
331 
332 	return (rv);
333 }
334 
335 const smb_struct_t *
336 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
337 {
338 	uint_t i;
339 
340 	for (i = 0; i < shp->sh_nstructs; i++) {
341 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
342 			return (&shp->sh_structs[i]);
343 	}
344 
345 	(void) smb_set_errno(shp, ESMB_NOENT);
346 	return (NULL);
347 }
348 
349 const smb_struct_t *
350 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
351 {
352 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
353 
354 	switch (id) {
355 	case SMB_ID_NOTSUP:
356 		(void) smb_set_errno(shp, ESMB_NOTSUP);
357 		return (NULL);
358 	case SMB_ID_NONE:
359 		(void) smb_set_errno(shp, ESMB_NOENT);
360 		return (NULL);
361 	}
362 
363 	for (; stp != NULL; stp = stp->smbst_next) {
364 		if (stp->smbst_hdr->smbh_hdl == id)
365 			break;
366 	}
367 
368 	if (stp == NULL)
369 		(void) smb_set_errno(shp, ESMB_NOENT);
370 
371 	return (stp);
372 }
373 
374 const char *
375 smb_strptr(const smb_struct_t *stp, uint_t i)
376 {
377 	if (i == 0 || i > stp->smbst_strtablen)
378 		return (_smb_emptystr);
379 	else
380 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
381 }
382 
383 int
384 smb_gteq(smbios_hdl_t *shp, int version)
385 {
386 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
387 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
388 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
389 }
390