xref: /titanic_52/usr/src/common/smbios/smb_open.c (revision e802abbda8c322f24d47835734f4a793ef15ddc8)
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 2006 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 	/*
142 	 * Copy the entry point into our handle.  The underlying entry point
143 	 * may be larger than our structure definition, so reset smbe_elen
144 	 * to our internal size and recompute good checksums for our copy.
145 	 */
146 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
147 	shp->sh_ent.smbe_elen = sizeof (smbios_entry_t);
148 	smbios_checksum(shp, &shp->sh_ent);
149 
150 	shp->sh_buf = buf;
151 	shp->sh_buflen = len;
152 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
153 	shp->sh_nstructs = 0;
154 	shp->sh_hashlen = _smb_hashlen;
155 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
156 	shp->sh_libvers = version;
157 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
158 
159 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
160 		return (smb_open_error(shp, errp, ESMB_NOMEM));
161 
162 	hp = shp->sh_buf;
163 	q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
164 
165 	for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
166 		smb_struct_t *stp = &shp->sh_structs[i];
167 		uint_t n = 0;
168 
169 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q)
170 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
171 
172 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
173 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
174 
175 		if (hp->smbh_type == SMB_TYPE_EOT)
176 			break; /* ignore any entries beyond end-of-table */
177 
178 		if ((const uchar_t *)hp + hp->smbh_len > q - 2)
179 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
180 
181 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
182 		p = s = (const uchar_t *)hp + hp->smbh_len;
183 
184 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
185 			if (*p++ == '\0')
186 				n++; /* count strings until \0\0 delimiter */
187 		}
188 
189 		if (p > q - 2)
190 			return (smb_open_error(shp, errp, ESMB_CORRUPT));
191 
192 		if (p > s)
193 			n++; /* add one for final string in string table */
194 
195 		stp->smbst_hdr = hp;
196 		stp->smbst_str = s;
197 		stp->smbst_end = p;
198 		stp->smbst_next = shp->sh_hash[h];
199 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
200 		stp->smbst_strtablen = n;
201 
202 		if (n != 0 && stp->smbst_strtab == NULL)
203 			return (smb_open_error(shp, errp, ESMB_NOMEM));
204 
205 		shp->sh_hash[h] = stp;
206 		nhp = (void *)(p + 2);
207 		shp->sh_nstructs++;
208 
209 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
210 			if (*p == '\0') {
211 				stp->smbst_strtab[n++] =
212 				    (uint16_t)(s - stp->smbst_str);
213 				s = p + 1;
214 			}
215 		}
216 	}
217 
218 	if (flags & SMB_O_ZIDS)
219 		smb_strip(shp);
220 
221 	return (shp);
222 }
223 
224 void
225 smbios_close(smbios_hdl_t *shp)
226 {
227 	const smbios_entry_t *ep = &shp->sh_ent;
228 	uint_t i;
229 
230 	for (i = 0; i < shp->sh_nstructs; i++) {
231 		smb_free(shp->sh_structs[i].smbst_strtab,
232 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
233 	}
234 
235 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
236 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
237 
238 	if (shp->sh_flags & SMB_FL_BUFALLOC)
239 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
240 
241 	smb_free(shp, sizeof (smbios_hdl_t));
242 }
243 
244 /*
245  * Recompute the values of the entry point checksums based upon the content
246  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
247  * anyway in case future versioning requires variations in the algorithm.
248  */
249 /*ARGSUSED*/
250 void
251 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
252 {
253 	uchar_t *p, *q = (uchar_t *)ep;
254 	uint8_t esum = 0, isum = 0;
255 
256 	ep->smbe_ecksum = ep->smbe_icksum = 0;
257 
258 	for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
259 		isum += *p;
260 
261 	ep->smbe_icksum = -isum;
262 
263 	for (p = q; p < q + ep->smbe_elen; p++)
264 		esum += *p;
265 
266 	ep->smbe_ecksum = -esum;
267 }
268 
269 const void *
270 smbios_buf(smbios_hdl_t *shp)
271 {
272 	return (shp->sh_buf);
273 }
274 
275 size_t
276 smbios_buflen(smbios_hdl_t *shp)
277 {
278 	return (shp->sh_buflen);
279 }
280 
281 static smbios_struct_t *
282 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
283 {
284 	const smb_header_t *hdr = stp->smbst_hdr;
285 
286 	sp->smbstr_id = hdr->smbh_hdl;
287 	sp->smbstr_type = hdr->smbh_type;
288 	sp->smbstr_data = hdr;
289 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
290 
291 	return (sp);
292 }
293 
294 int
295 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
296 {
297 	const smb_struct_t *stp = smb_lookup_id(shp, id);
298 
299 	if (stp == NULL)
300 		return (-1); /* errno is set for us */
301 
302 	if (sp != NULL)
303 		(void) smb_export(stp, sp);
304 
305 	return (0);
306 }
307 
308 int
309 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
310 {
311 	const smb_struct_t *sp = shp->sh_structs;
312 	smbios_struct_t s;
313 	int i, rv = 0;
314 
315 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
316 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
317 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
318 			break;
319 	}
320 
321 	return (rv);
322 }
323 
324 const smb_struct_t *
325 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
326 {
327 	uint_t i;
328 
329 	for (i = 0; i < shp->sh_nstructs; i++) {
330 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
331 			return (&shp->sh_structs[i]);
332 	}
333 
334 	(void) smb_set_errno(shp, ESMB_NOENT);
335 	return (NULL);
336 }
337 
338 const smb_struct_t *
339 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
340 {
341 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
342 
343 	switch (id) {
344 	case SMB_ID_NOTSUP:
345 		(void) smb_set_errno(shp, ESMB_NOTSUP);
346 		return (NULL);
347 	case SMB_ID_NONE:
348 		(void) smb_set_errno(shp, ESMB_NOENT);
349 		return (NULL);
350 	}
351 
352 	for (; stp != NULL; stp = stp->smbst_next) {
353 		if (stp->smbst_hdr->smbh_hdl == id)
354 			break;
355 	}
356 
357 	if (stp == NULL)
358 		(void) smb_set_errno(shp, ESMB_NOENT);
359 
360 	return (stp);
361 }
362 
363 const char *
364 smb_strptr(const smb_struct_t *stp, uint_t i)
365 {
366 	if (i == 0 || i > stp->smbst_strtablen)
367 		return (_smb_emptystr);
368 	else
369 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
370 }
371 
372 int
373 smb_gteq(smbios_hdl_t *shp, int version)
374 {
375 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
376 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
377 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
378 }
379