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 2018 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
31static const uint_t _smb_hashlen = 64;		/* hash length (must be Pof2) */
32static const char _smb_emptystr[] = "";		/* empty string to return */
33int _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 */
44static void
45smb_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
75static int
76smbios_bufopen_21(smbios_hdl_t *shp, const smbios_21_entry_t *ep, size_t len,
77    int flags)
78{
79	if (strncmp(ep->smbe_eanchor, SMB_ENTRY_EANCHOR, SMB_ENTRY_EANCHORLEN))
80		return (ESMB_HEADER);
81
82	if (strncmp(ep->smbe_ianchor, SMB_ENTRY_IANCHOR, SMB_ENTRY_IANCHORLEN))
83		return (ESMB_HEADER);
84
85	smb_dprintf(shp, "opening SMBIOS version %u.%u bcdrev 0x%x\n",
86	    ep->smbe_major, ep->smbe_minor, ep->smbe_bcdrev);
87
88	if (!(flags & SMB_O_NOVERS)) {
89		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
90			return (ESMB_NEW);
91
92		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
93		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
94		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
95			return (ESMB_OLD);
96	}
97
98	if (len < sizeof (smb_header_t) ||
99	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
100		return (ESMB_SHORT);
101
102	if (!(flags & SMB_O_NOCKSUM)) {
103		uint8_t esum = 0, isum = 0;
104		const uchar_t *p, *q;
105		q = (uchar_t *)ep;
106
107		for (p = q; p < q + ep->smbe_elen; p++)
108			esum += *p;
109
110		for (p = (uchar_t *)ep->smbe_ianchor; p < q + sizeof (*ep); p++)
111			isum += *p;
112
113		if (esum != 0 || isum != 0) {
114			smb_dprintf(shp, "bad cksum: e=%x i=%x\n", esum, isum);
115			return (ESMB_CKSUM);
116		}
117	}
118
119	/*
120	 * Copy the entry point into our handle.  The underlying entry point
121	 * may be larger than our structure definition, so reset smbe_elen
122	 * to our internal size and recompute good checksums for our copy.
123	 */
124	shp->sh_ent_type = SMBIOS_ENTRY_POINT_21;
125	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
126	shp->sh_ent.ep21.smbe_elen = sizeof (smbios_entry_t);
127	smbios_checksum(shp, &shp->sh_ent);
128
129	shp->sh_ent_stnum = ep->smbe_stnum;
130	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
131	return (0);
132}
133
134static int
135smbios_bufopen_30(smbios_hdl_t *shp, const smbios_30_entry_t *ep, size_t len,
136    int flags)
137{
138	if (strncmp(ep->smbe_eanchor, SMB3_ENTRY_EANCHOR,
139	    SMB3_ENTRY_EANCHORLEN))
140		return (ESMB_HEADER);
141
142	smb_dprintf(shp, "opening SMBIOS version %u.%u\n",
143	    ep->smbe_major, ep->smbe_minor);
144
145	if (!(flags & SMB_O_NOVERS)) {
146		if (ep->smbe_major > SMB_MAJOR(SMB_VERSION))
147			return (ESMB_NEW);
148
149		if (ep->smbe_major < SMB_MAJOR(SMB_VERSION_23) || (
150		    ep->smbe_major == SMB_MAJOR(SMB_VERSION_23) &&
151		    ep->smbe_minor < SMB_MINOR(SMB_VERSION_23)))
152			return (ESMB_OLD);
153	}
154
155	if (len < sizeof (smb_header_t) ||
156	    ep->smbe_stlen < sizeof (smb_header_t) || len < ep->smbe_stlen)
157		return (ESMB_SHORT);
158
159	if (!(flags & SMB_O_NOCKSUM)) {
160		uint8_t esum = 0;
161		const uchar_t *p, *q;
162		q = (uchar_t *)ep;
163
164		for (p = q; p < q + ep->smbe_elen; p++)
165			esum += *p;
166
167		if (esum != 0) {
168			smb_dprintf(shp, "bad cksum: e=%x\n", esum);
169			return (ESMB_CKSUM);
170		}
171	}
172
173	/*
174	 * Copy the entry point into our handle.  The underlying entry point
175	 * may be larger than our structure definition, so reset smbe_elen
176	 * to our internal size and recompute good checksums for our copy.
177	 */
178	shp->sh_ent_type = SMBIOS_ENTRY_POINT_30;
179	bcopy(ep, &shp->sh_ent, sizeof (smbios_entry_t));
180	shp->sh_ent.ep30.smbe_elen = sizeof (smbios_entry_t);
181	smbios_checksum(shp, &shp->sh_ent);
182
183	shp->sh_smbvers = SMB_MAJMIN(ep->smbe_major, ep->smbe_minor);
184
185	return (0);
186}
187
188static uint_t
189smbios_table_nentries(const char *smbe_staddr, uint32_t smbe_stlen)
190{
191	uint_t i = 0;
192	char *dmi;
193	smb_header_t *hdr;
194
195	if (smbe_staddr == NULL)
196		return (i);
197
198	for (dmi = (char *)smbe_staddr; dmi < smbe_staddr + smbe_stlen; i++) {
199		hdr = (smb_header_t *)dmi;
200		dmi += hdr->smbh_len;
201		/*
202		 * Search for the end of the string area.
203		 */
204		while (dmi + 1 < smbe_staddr + smbe_stlen &&
205		    dmi[0] != '\0' && dmi[1] != '\0') {
206			dmi++;
207		}
208		dmi += 2;
209	}
210	return (i);
211}
212
213smbios_hdl_t *
214smbios_bufopen(const smbios_entry_t *ep, const void *buf, size_t len,
215    int version, int flags, int *errp)
216{
217	smbios_hdl_t *shp = smb_zalloc(sizeof (smbios_hdl_t));
218	const smb_header_t *hp, *nhp;
219	const uchar_t *p, *q, *s;
220	uint_t i, h;
221	int err;
222
223	switch (version) {
224	case SMB_VERSION_23:
225	case SMB_VERSION_24:
226	case SMB_VERSION_25:
227	case SMB_VERSION_26:
228	case SMB_VERSION_27:
229	case SMB_VERSION_28:
230	case SMB_VERSION_30:
231	case SMB_VERSION_31:
232	case SMB_VERSION_32:
233	case SMB_VERSION_33:
234		break;
235	default:
236		return (smb_open_error(shp, errp, ESMB_VERSION));
237	}
238
239	if (ep == NULL || buf == NULL || len == 0 || (flags & ~SMB_O_MASK))
240		return (smb_open_error(shp, errp, ESMB_INVAL));
241
242	if (shp == NULL)
243		return (smb_open_error(shp, errp, ESMB_NOMEM));
244
245	if (_smb_debug)
246		shp->sh_flags |= SMB_FL_DEBUG;
247
248	err = smbios_bufopen_21(shp, &ep->ep21, len, flags);
249	if (err != 0) {
250		err = smbios_bufopen_30(shp, &ep->ep30, len, flags);
251		if (err != 0)
252			return (smb_open_error(shp, errp, err));
253		shp->sh_ent_stnum =
254		    smbios_table_nentries(buf, ep->ep30.smbe_stlen);
255	}
256
257	shp->sh_buf = buf;
258	shp->sh_buflen = len;
259	shp->sh_structs = smb_alloc(sizeof (smb_struct_t) * shp->sh_ent_stnum);
260	shp->sh_nstructs = 0;
261	shp->sh_hashlen = _smb_hashlen;
262	shp->sh_hash = smb_zalloc(sizeof (smb_struct_t *) * shp->sh_hashlen);
263	shp->sh_libvers = version;
264
265	if (shp->sh_structs == NULL || shp->sh_hash == NULL)
266		return (smb_open_error(shp, errp, ESMB_NOMEM));
267
268	hp = shp->sh_buf;
269	switch (shp->sh_ent_type) {
270	case SMBIOS_ENTRY_POINT_21:
271		q = (const uchar_t *)buf + MIN(ep->ep21.smbe_stlen, len);
272		break;
273	case SMBIOS_ENTRY_POINT_30:
274		q = (const uchar_t *)buf + MIN(ep->ep30.smbe_stlen, len);
275		break;
276	default:
277		return (smb_open_error(shp, errp, ESMB_VERSION));
278	}
279
280	for (i = 0; i < shp->sh_ent_stnum; i++, hp = nhp) {
281		smb_struct_t *stp = &shp->sh_structs[i];
282		uint_t n = 0;
283
284		if ((const uchar_t *)hp + sizeof (smb_header_t) > q) {
285			shp->sh_flags |= SMB_FL_TRUNC;
286			break;
287		}
288
289		smb_dprintf(shp, "struct [%u] type %u len %u hdl %u at %p\n",
290		    i, hp->smbh_type, hp->smbh_len, hp->smbh_hdl, (void *)hp);
291
292		if (hp->smbh_type == SMB_TYPE_EOT)
293			break; /* ignore any entries beyond end-of-table */
294
295		if ((const uchar_t *)hp + hp->smbh_len > q - 2) {
296			shp->sh_flags |= SMB_FL_TRUNC;
297			break;
298		}
299
300		h = hp->smbh_hdl & (shp->sh_hashlen - 1);
301		p = s = (const uchar_t *)hp + hp->smbh_len;
302
303		while (p <= q - 2 && (p[0] != '\0' || p[1] != '\0')) {
304			if (*p++ == '\0')
305				n++; /* count strings until \0\0 delimiter */
306		}
307
308		if (p > q - 2) {
309			shp->sh_flags |= SMB_FL_TRUNC;
310			break;
311		}
312
313		if (p > s)
314			n++; /* add one for final string in string table */
315
316		stp->smbst_hdr = hp;
317		stp->smbst_str = s;
318		stp->smbst_end = p;
319		stp->smbst_next = shp->sh_hash[h];
320		stp->smbst_strtab = smb_alloc(sizeof (uint16_t) * n);
321		stp->smbst_strtablen = n;
322
323		if (n != 0 && stp->smbst_strtab == NULL)
324			return (smb_open_error(shp, errp, ESMB_NOMEM));
325
326		shp->sh_hash[h] = stp;
327		nhp = (void *)(p + 2);
328		shp->sh_nstructs++;
329
330		for (n = 0, p = s; n < stp->smbst_strtablen; p++) {
331			if (*p == '\0') {
332				stp->smbst_strtab[n++] =
333				    (uint16_t)(s - stp->smbst_str);
334				s = p + 1;
335			}
336		}
337	}
338
339	/* error out if we couldn't find any complete entries in the table */
340	if ((shp->sh_flags & SMB_FL_TRUNC) && i == 0)
341		return (smb_open_error(shp, errp, ESMB_CORRUPT));
342
343	if (flags & SMB_O_ZIDS)
344		smb_strip(shp);
345
346	return (shp);
347}
348
349void
350smbios_close(smbios_hdl_t *shp)
351{
352	uint_t i;
353
354	for (i = 0; i < shp->sh_nstructs; i++) {
355		smb_free(shp->sh_structs[i].smbst_strtab,
356		    sizeof (uint16_t) * shp->sh_structs[i].smbst_strtablen);
357	}
358
359	smb_free(shp->sh_structs, sizeof (smb_struct_t) * shp->sh_ent_stnum);
360	smb_free(shp->sh_hash, sizeof (smb_struct_t *) * shp->sh_hashlen);
361
362	if (shp->sh_flags & SMB_FL_BUFALLOC)
363		smb_free((void *)shp->sh_buf, shp->sh_buflen);
364
365	smb_free(shp, sizeof (smbios_hdl_t));
366}
367
368/*
369 * Recompute the values of the entry point checksums based upon the content
370 * of the specified SMBIOS entry point.  We don't need 'shp' but require it
371 * anyway in case future versioning requires variations in the algorithm.
372 */
373/*ARGSUSED*/
374void
375smbios_checksum(smbios_hdl_t *shp, smbios_entry_t *ep)
376{
377	uchar_t *p, *q = (uchar_t *)ep;
378	uint8_t esum = 0, isum = 0;
379
380	switch (shp->sh_ent_type) {
381	case SMBIOS_ENTRY_POINT_21:
382		ep->ep21.smbe_ecksum = ep->ep21.smbe_icksum = 0;
383
384		for (p = (uchar_t *)ep->ep21.smbe_ianchor;
385		    p < q + sizeof (*ep); p++) {
386			isum += *p;
387		}
388
389		ep->ep21.smbe_icksum = -isum;
390
391		for (p = q; p < q + ep->ep21.smbe_elen; p++)
392			esum += *p;
393
394		ep->ep21.smbe_ecksum = -esum;
395		break;
396	case SMBIOS_ENTRY_POINT_30:
397		ep->ep30.smbe_ecksum = 0;
398		for (p = q; p < q + ep->ep30.smbe_elen; p++)
399			esum += *p;
400
401		ep->ep30.smbe_ecksum = -esum;
402		break;
403	default:
404		break;
405	}
406}
407
408const void *
409smbios_buf(smbios_hdl_t *shp)
410{
411	return (shp->sh_buf);
412}
413
414size_t
415smbios_buflen(smbios_hdl_t *shp)
416{
417	return (shp->sh_buflen);
418}
419
420static smbios_struct_t *
421smb_export(const smb_struct_t *stp, smbios_struct_t *sp)
422{
423	const smb_header_t *hdr = stp->smbst_hdr;
424
425	sp->smbstr_id = hdr->smbh_hdl;
426	sp->smbstr_type = hdr->smbh_type;
427	sp->smbstr_data = hdr;
428	sp->smbstr_size = (size_t)(stp->smbst_end - (uchar_t *)hdr);
429
430	return (sp);
431}
432
433int
434smbios_lookup_id(smbios_hdl_t *shp, id_t id, smbios_struct_t *sp)
435{
436	const smb_struct_t *stp = smb_lookup_id(shp, id);
437
438	if (stp == NULL)
439		return (-1); /* errno is set for us */
440
441	if (sp != NULL)
442		(void) smb_export(stp, sp);
443
444	return (0);
445}
446
447int
448smbios_lookup_type(smbios_hdl_t *shp, uint_t type, smbios_struct_t *sp)
449{
450	const smb_struct_t *stp = smb_lookup_type(shp, type);
451
452	if (stp == NULL)
453		return (-1); /* errno is set for us */
454
455	if (sp != NULL)
456		(void) smb_export(stp, sp);
457
458	return (0);
459}
460
461int
462smbios_iter(smbios_hdl_t *shp, smbios_struct_f *func, void *data)
463{
464	const smb_struct_t *sp = shp->sh_structs;
465	smbios_struct_t s;
466	int i, rv = 0;
467
468	for (i = 0; i < shp->sh_nstructs; i++, sp++) {
469		if (sp->smbst_hdr->smbh_type != SMB_TYPE_INACTIVE &&
470		    (rv = func(shp, smb_export(sp, &s), data)) != 0)
471			break;
472	}
473
474	return (rv);
475}
476
477const smb_struct_t *
478smb_lookup_type(smbios_hdl_t *shp, uint_t type)
479{
480	uint_t i;
481
482	for (i = 0; i < shp->sh_nstructs; i++) {
483		if (shp->sh_structs[i].smbst_hdr->smbh_type == type)
484			return (&shp->sh_structs[i]);
485	}
486
487	(void) smb_set_errno(shp, ESMB_NOENT);
488	return (NULL);
489}
490
491const smb_struct_t *
492smb_lookup_id(smbios_hdl_t *shp, uint_t id)
493{
494	const smb_struct_t *stp = shp->sh_hash[id & (shp->sh_hashlen - 1)];
495
496	switch (id) {
497	case SMB_ID_NOTSUP:
498		(void) smb_set_errno(shp, ESMB_NOTSUP);
499		return (NULL);
500	case SMB_ID_NONE:
501		(void) smb_set_errno(shp, ESMB_NOENT);
502		return (NULL);
503	}
504
505	for (; stp != NULL; stp = stp->smbst_next) {
506		if (stp->smbst_hdr->smbh_hdl == id)
507			break;
508	}
509
510	if (stp == NULL)
511		(void) smb_set_errno(shp, ESMB_NOENT);
512
513	return (stp);
514}
515
516const char *
517smb_strptr(const smb_struct_t *stp, uint_t i)
518{
519	if (i == 0 || i > stp->smbst_strtablen)
520		return (_smb_emptystr);
521	else
522		return ((char *)stp->smbst_str + stp->smbst_strtab[i - 1]);
523}
524
525int
526smb_libgteq(smbios_hdl_t *shp, int version)
527{
528	return (SMB_MAJOR(shp->sh_libvers) > SMB_MAJOR(version) || (
529	    SMB_MAJOR(shp->sh_libvers) == SMB_MAJOR(version) &&
530	    SMB_MINOR(shp->sh_libvers) >= SMB_MINOR(version)));
531}
532
533int
534smb_gteq(smbios_hdl_t *shp, int version)
535{
536	return (SMB_MAJOR(shp->sh_smbvers) > SMB_MAJOR(version) || (
537	    SMB_MAJOR(shp->sh_smbvers) == SMB_MAJOR(version) &&
538	    SMB_MINOR(shp->sh_smbvers) >= SMB_MINOR(version)));
539}
540
541boolean_t
542smbios_truncated(smbios_hdl_t *shp)
543{
544	return ((shp->sh_flags & SMB_FL_TRUNC) != 0);
545}
546