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