xref: /titanic_51/usr/src/common/smbios/smb_open.c (revision b10a562e3f5e730a20b76a2716d85674fe62a226)
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 2016 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 	case SMB_VERSION_31:
93 		break;
94 	default:
95 		return (smb_open_error(shp, errp, ESMB_VERSION));
96 	}
97 
98 	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
99 		return (smb_open_error(shp, errp, ESMB_INVAL));
100 
101 	if (shp == NULL)
102 		return (smb_open_error(shp, errp, ESMB_NOMEM));
103 
104 	if (_smb_debug)
105 		shp->sh_flags |= SMB_FL_DEBUG;
106 
107 	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
108 		return (smb_open_error(shp, errp, ESMB_HEADER));
109 
110 	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
111 		return (smb_open_error(shp, errp, ESMB_HEADER));
112 
113 	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
114 	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
115 
116 	if (!(flags & SMB_O_NOVERS)) {
117 		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
118 			return (smb_open_error(shp, errp, ESMB_NEW));
119 
120 		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
121 		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
122 		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
123 			return (smb_open_error(shp, errp, ESMB_OLD));
124 	}
125 
126 	if (len < sizeof (smb_header_t) ||
127 	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
128 		return (smb_open_error(shp, errp, ESMB_SHORT));
129 
130 	if (!(flags & SMB_O_NOCKSUM)) {
131 		uint8_t esum = 0, isum = 0;
132 		q = (uchar_t *)ep;
133 
134 		for (p = q; p < q + ep->smbe_elen; p++)
135 			esum += *p;
136 
137 		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
138 			isum += *p;
139 
140 		if (esum != 0 || isum != 0) {
141 			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
142 			return (smb_open_error(shp, errp, ESMB_CKSUM));
143 		}
144 	}
145 
146 	/*
147 	 * Copy the entry point into our handle.  The underlying entry point
148 	 * may be larger than our structure definition, so reset smbe_elen
149 	 * to our internal size and recompute good checksums for our copy.
150 	 */
151 	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
152 	shp->sh_ent.smbe_elen = sizeof (smbios_entry_t);
153 	smbios_checksum(shp, &shp->sh_ent);
154 
155 	shp->sh_buf = buf;
156 	shp->sh_buflen = len;
157 	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * ep->smbe_stnum);
158 	shp->sh_nstructs = 0;
159 	shp->sh_hashlen = _smb_hashlen;
160 	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
161 	shp->sh_libvers = version;
162 	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
163 
164 	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
165 		return (smb_open_error(shp, errp, ESMB_NOMEM));
166 
167 	hp = shp->sh_buf;
168 	q = (const uchar_t *)buf + MIN(ep->smbe_stlen, len);
169 
170 	for (i = 0; i < ep->smbe_stnum; i++, hp = nhp) {
171 		smb_struct_t *stp = &shp->sh_structs[i];
172 		uint_t n = 0;
173 
174 		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
175 			shp->sh_flags |= SMB_FL_TRUNC;
176 			break;
177 		}
178 
179 		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
180 		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
181 
182 		if (hp->smbh_type == SMB_TYPE_EOT)
183 			break; /* ignore any entries beyond end-of-table */
184 
185 		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
186 			shp->sh_flags |= SMB_FL_TRUNC;
187 			break;
188 		}
189 
190 		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
191 		p = s = (const uchar_t *)hp + hp->smbh_len;
192 
193 		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
194 			if (*p++ == '\0')
195 				n++; /* count strings until \0\0 delimiter */
196 		}
197 
198 		if (p > q - 2) {
199 			shp->sh_flags |= SMB_FL_TRUNC;
200 			break;
201 		}
202 
203 		if (p > s)
204 			n++; /* add one for final string in string table */
205 
206 		stp->smbst_hdr = hp;
207 		stp->smbst_str = s;
208 		stp->smbst_end = p;
209 		stp->smbst_next = shp->sh_hash[h];
210 		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
211 		stp->smbst_strtablen = n;
212 
213 		if (n != 0 && stp->smbst_strtab == NULL)
214 			return (smb_open_error(shp, errp, ESMB_NOMEM));
215 
216 		shp->sh_hash[h] = stp;
217 		nhp = (void *)(p + 2);
218 		shp->sh_nstructs++;
219 
220 		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
221 			if (*p == '\0') {
222 				stp->smbst_strtab[n++] =
223 				    (uint16_t)(s - stp->smbst_str);
224 				s = p + 1;
225 			}
226 		}
227 	}
228 
229 	/* error out if we couldn't find any complete entries in the table */
230 	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
231 		return (smb_open_error(shp, errp, ESMB_CORRUPT));
232 
233 	if (flags & SMB_O_ZIDS)
234 		smb_strip(shp);
235 
236 	return (shp);
237 }
238 
239 void
240 smbios_close(smbios_hdl_t *shp)
241 {
242 	const smbios_entry_t *ep = &shp->sh_ent;
243 	uint_t i;
244 
245 	for (i = 0; i < shp->sh_nstructs; i++) {
246 		smb_free(shp->sh_structs[i].smbst_strtab,
247 		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
248 	}
249 
250 	smb_free(shp->sh_structs, sizeof (smb_struct_t) * ep->smbe_stnum);
251 	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
252 
253 	if (shp->sh_flags & SMB_FL_BUFALLOC)
254 		smb_free((void *)shp->sh_buf, shp->sh_buflen);
255 
256 	smb_free(shp, sizeof (smbios_hdl_t));
257 }
258 
259 /*
260  * Recompute the values of the entry point checksums based upon the content
261  * of the specified SMBIOS entry point.  We don't need 'shp' but require it
262  * anyway in case future versioning requires variations in the algorithm.
263  */
264 /*ARGSUSED*/
265 void
266 smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
267 {
268 	uchar_t *p, *q = (uchar_t *)ep;
269 	uint8_t esum = 0, isum = 0;
270 
271 	ep->smbe_ecksum = ep->smbe_icksum = 0;
272 
273 	for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
274 		isum += *p;
275 
276 	ep->smbe_icksum = -isum;
277 
278 	for (p = q; p < q + ep->smbe_elen; p++)
279 		esum += *p;
280 
281 	ep->smbe_ecksum = -esum;
282 }
283 
284 const void *
285 smbios_buf(smbios_hdl_t *shp)
286 {
287 	return (shp->sh_buf);
288 }
289 
290 size_t
291 smbios_buflen(smbios_hdl_t *shp)
292 {
293 	return (shp->sh_buflen);
294 }
295 
296 static smbios_struct_t *
297 smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
298 {
299 	const smb_header_t *hdr = stp->smbst_hdr;
300 
301 	sp->smbstr_id = hdr->smbh_hdl;
302 	sp->smbstr_type = hdr->smbh_type;
303 	sp->smbstr_data = hdr;
304 	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
305 
306 	return (sp);
307 }
308 
309 int
310 smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
311 {
312 	const smb_struct_t *stp = smb_lookup_id(shp, id);
313 
314 	if (stp == NULL)
315 		return (-1); /* errno is set for us */
316 
317 	if (sp != NULL)
318 		(void) smb_export(stp, sp);
319 
320 	return (0);
321 }
322 
323 int
324 smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
325 {
326 	const smb_struct_t *stp = smb_lookup_type(shp, type);
327 
328 	if (stp == NULL)
329 		return (-1); /* errno is set for us */
330 
331 	if (sp != NULL)
332 		(void) smb_export(stp, sp);
333 
334 	return (0);
335 }
336 
337 int
338 smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
339 {
340 	const smb_struct_t *sp = shp->sh_structs;
341 	smbios_struct_t s;
342 	int i, rv = 0;
343 
344 	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
345 		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
346 		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
347 			break;
348 	}
349 
350 	return (rv);
351 }
352 
353 const smb_struct_t *
354 smb_lookup_type(smbios_hdl_t *shp, uint_t type)
355 {
356 	uint_t i;
357 
358 	for (i = 0; i < shp->sh_nstructs; i++) {
359 		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
360 			return (&shp->sh_structs[i]);
361 	}
362 
363 	(void) smb_set_errno(shp, ESMB_NOENT);
364 	return (NULL);
365 }
366 
367 const smb_struct_t *
368 smb_lookup_id(smbios_hdl_t *shp, uint_t id)
369 {
370 	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
371 
372 	switch (id) {
373 	case SMB_ID_NOTSUP:
374 		(void) smb_set_errno(shp, ESMB_NOTSUP);
375 		return (NULL);
376 	case SMB_ID_NONE:
377 		(void) smb_set_errno(shp, ESMB_NOENT);
378 		return (NULL);
379 	}
380 
381 	for (; stp != NULL; stp = stp->smbst_next) {
382 		if (stp->smbst_hdr->smbh_hdl == id)
383 			break;
384 	}
385 
386 	if (stp == NULL)
387 		(void) smb_set_errno(shp, ESMB_NOENT);
388 
389 	return (stp);
390 }
391 
392 const char *
393 smb_strptr(const smb_struct_t *stp, uint_t i)
394 {
395 	if (i == 0 || i > stp->smbst_strtablen)
396 		return (_smb_emptystr);
397 	else
398 		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
399 }
400 
401 int
402 smb_libgteq(smbios_hdl_t *shp, int version)
403 {
404 	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
405 	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
406 	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
407 }
408 
409 int
410 smb_gteq(smbios_hdl_t *shp, int version)
411 {
412 	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
413 	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
414 	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
415 }
416 
417 boolean_t
418 smbios_truncated(smbios_hdl_t *shp)
419 {
420 	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
421 }
422