xref: /illumos-gate/usr/src/common/smbios/smb_info.c (revision 89574a1f89d2af2d1755c4e854b150d6113e0564)
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 (c) 2018, Joyent, Inc.
25  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 /*
30  * SMBIOS Information Routines
31  *
32  * The routines in this file are used to convert from the SMBIOS data format to
33  * a more reasonable and stable set of structures offered as part of our ABI.
34  * These functions take the general form:
35  *
36  *	stp = smb_lookup_type(shp, foo);
37  *	smb_foo_t foo;
38  *
39  *	smb_info_bcopy(stp->smbst_hdr, &foo, sizeof (foo));
40  *      bzero(caller's struct);
41  *
42  *	copy/convert foo members into caller's struct
43  *
44  * We copy the internal structure on to an automatic variable so as to avoid
45  * checks everywhere for structures that the BIOS has improperly truncated, and
46  * also to automatically handle the case of a structure that has been extended.
47  * When necessary, this code can use smb_gteq() to determine whether the SMBIOS
48  * data is of a particular revision that is supposed to contain a new field.
49  *
50  * Note, when trying to bzero the caller's struct you have to be careful about
51  * versions. One can only bzero the initial version that existed in illumos. In
52  * other words, if someone passes an older library handle that doesn't support a
53  * version you cannot assume that their structures have those additional members
54  * in them. Instead, a 'base' version is introduced for such types that have
55  * differences and instead we only bzero out the base version and then handle
56  * the additional members. In general, because all additional members will be
57  * assigned, there's no reason to zero them out unless they are arrays that
58  * won't be entirely filled in.
59  *
60  * Due to history, anything added after the update from version 2.4, in other
61  * words additions from or after '5094 Update libsmbios with recent items'
62  * (4e901881) is currently being used for this. While we don't allow software
63  * compiling against this to get an older form, this was the first major update
64  * and a good starting point for us to enforce this behavior which is useful for
65  * moving forward to making this more public.
66  */
67 
68 #include <sys/smbios_impl.h>
69 #include <sys/byteorder.h>
70 
71 #ifdef _KERNEL
72 #include <sys/sunddi.h>
73 #else
74 #include <fcntl.h>
75 #include <unistd.h>
76 #include <string.h>
77 #endif
78 
79 /*
80  * A large number of SMBIOS structures contain a set of common strings used to
81  * describe a h/w component's serial number, manufacturer, etc.  These fields
82  * helpfully have different names and offsets and sometimes aren't consistent.
83  * To simplify life for our clients, we factor these common things out into
84  * smbios_info_t, which can be retrieved for any structure.  The following
85  * table describes the mapping from a given structure to the smbios_info_t.
86  * Multiple SMBIOS stuctures' contained objects are also handled here.
87  */
88 static const struct smb_infospec {
89 	uint8_t is_type;		/* structure type */
90 	uint8_t is_manu;		/* manufacturer offset */
91 	uint8_t is_product;		/* product name offset */
92 	uint8_t is_version;		/* version offset */
93 	uint8_t is_serial;		/* serial number offset */
94 	uint8_t is_asset;		/* asset tag offset */
95 	uint8_t is_location;		/* location string offset */
96 	uint8_t is_part;		/* part number offset */
97 	uint8_t is_contc;		/* contained count */
98 	uint8_t is_contsz;		/* contained size */
99 	uint8_t is_contv;		/* contained objects */
100 } _smb_infospecs[] = {
101 	{ SMB_TYPE_SYSTEM,
102 		offsetof(smb_system_t, smbsi_manufacturer),
103 		offsetof(smb_system_t, smbsi_product),
104 		offsetof(smb_system_t, smbsi_version),
105 		offsetof(smb_system_t, smbsi_serial),
106 		0,
107 		0,
108 		0,
109 		0,
110 		0,
111 		0 },
112 	{ SMB_TYPE_BASEBOARD,
113 		offsetof(smb_bboard_t, smbbb_manufacturer),
114 		offsetof(smb_bboard_t, smbbb_product),
115 		offsetof(smb_bboard_t, smbbb_version),
116 		offsetof(smb_bboard_t, smbbb_serial),
117 		offsetof(smb_bboard_t, smbbb_asset),
118 		offsetof(smb_bboard_t, smbbb_location),
119 		0,
120 		offsetof(smb_bboard_t, smbbb_cn),
121 		SMB_CONT_WORD,
122 		offsetof(smb_bboard_t, smbbb_cv) },
123 	{ SMB_TYPE_CHASSIS,
124 		offsetof(smb_chassis_t, smbch_manufacturer),
125 		0,
126 		offsetof(smb_chassis_t, smbch_version),
127 		offsetof(smb_chassis_t, smbch_serial),
128 		offsetof(smb_chassis_t, smbch_asset),
129 		0,
130 		0,
131 		offsetof(smb_chassis_t, smbch_cn),
132 		SMB_CONT_BYTE,
133 		offsetof(smb_chassis_t, smbch_cv) },
134 	{ SMB_TYPE_PROCESSOR,
135 		offsetof(smb_processor_t, smbpr_manufacturer),
136 		0,
137 		offsetof(smb_processor_t, smbpr_version),
138 		offsetof(smb_processor_t, smbpr_serial),
139 		offsetof(smb_processor_t, smbpr_asset),
140 		offsetof(smb_processor_t, smbpr_socket),
141 		offsetof(smb_processor_t, smbpr_part),
142 		0,
143 		0,
144 		0 },
145 	{ SMB_TYPE_CACHE,
146 		0,
147 		0,
148 		0,
149 		0,
150 		0,
151 		offsetof(smb_cache_t, smbca_socket),
152 		0,
153 		0,
154 		0,
155 		0 },
156 	{ SMB_TYPE_PORT,
157 		0,
158 		0,
159 		0,
160 		0,
161 		0,
162 		offsetof(smb_port_t, smbpo_iref),
163 		0,
164 		0,
165 		0,
166 		0 },
167 	{ SMB_TYPE_SLOT,
168 		0,
169 		0,
170 		0,
171 		0,
172 		0,
173 		offsetof(smb_slot_t, smbsl_name),
174 		0,
175 		0,
176 		0,
177 		0 },
178 	{ SMB_TYPE_MEMDEVICE,
179 		offsetof(smb_memdevice_t, smbmdev_manufacturer),
180 		0,
181 		0,
182 		offsetof(smb_memdevice_t, smbmdev_serial),
183 		offsetof(smb_memdevice_t, smbmdev_asset),
184 		offsetof(smb_memdevice_t, smbmdev_dloc),
185 		offsetof(smb_memdevice_t, smbmdev_part),
186 		0,
187 		0,
188 		0 },
189 	{ SMB_TYPE_POWERSUP,
190 		offsetof(smb_powersup_t, smbpsup_manufacturer),
191 		offsetof(smb_powersup_t, smbpsup_devname),
192 		offsetof(smb_powersup_t, smbpsup_rev),
193 		offsetof(smb_powersup_t, smbpsup_serial),
194 		offsetof(smb_powersup_t, smbpsup_asset),
195 		offsetof(smb_powersup_t, smbpsup_loc),
196 		offsetof(smb_powersup_t, smbpsup_part),
197 		0,
198 		0,
199 		0 },
200 	{ SMB_TYPE_EOT }
201 };
202 
203 static const char *
204 smb_info_strptr(const smb_struct_t *stp, uint8_t off, int *n)
205 {
206 	const uint8_t *sp = (const uint8_t *)(uintptr_t)stp->smbst_hdr;
207 
208 	if (off != 0 && sp + off < stp->smbst_end) {
209 		(*n)++; /* indicate success for caller */
210 		return (smb_strptr(stp, sp[off]));
211 	}
212 
213 	return (smb_strptr(stp, 0));
214 }
215 
216 static void
217 smb_info_bcopy(const smb_header_t *hp, void *dst, size_t dstlen)
218 {
219 	if (dstlen > hp->smbh_len) {
220 		bcopy(hp, dst, hp->smbh_len);
221 		bzero((char *)dst + hp->smbh_len, dstlen - hp->smbh_len);
222 	} else
223 		bcopy(hp, dst, dstlen);
224 }
225 
226 smbios_entry_point_t
227 smbios_info_smbios(smbios_hdl_t *shp, smbios_entry_t *ep)
228 {
229 	bcopy(&shp->sh_ent, ep, sizeof (smbios_entry_t));
230 	return (shp->sh_ent_type);
231 }
232 
233 void
234 smbios_info_smbios_version(smbios_hdl_t *shp, smbios_version_t *v)
235 {
236 	v->smbv_major = SMB_MAJOR(shp->sh_smbvers);
237 	v->smbv_minor = SMB_MINOR(shp->sh_smbvers);
238 }
239 
240 #ifndef _KERNEL
241 static char smbios_product_override[256];
242 static boolean_t smbios_product_checked;
243 #endif
244 
245 int
246 smbios_info_common(smbios_hdl_t *shp, id_t id, smbios_info_t *ip)
247 {
248 	const smb_struct_t *stp = smb_lookup_id(shp, id);
249 	const struct smb_infospec *isp;
250 	int n = 0;
251 
252 	if (stp == NULL)
253 		return (-1); /* errno is set for us */
254 
255 	for (isp = _smb_infospecs; isp->is_type != SMB_TYPE_EOT; isp++) {
256 		if (isp->is_type == stp->smbst_hdr->smbh_type)
257 			break;
258 	}
259 
260 	ip->smbi_manufacturer = smb_info_strptr(stp, isp->is_manu, &n);
261 	ip->smbi_product = smb_info_strptr(stp, isp->is_product, &n);
262 	ip->smbi_version = smb_info_strptr(stp, isp->is_version, &n);
263 	ip->smbi_serial = smb_info_strptr(stp, isp->is_serial, &n);
264 	ip->smbi_asset = smb_info_strptr(stp, isp->is_asset, &n);
265 	ip->smbi_location = smb_info_strptr(stp, isp->is_location, &n);
266 	ip->smbi_part = smb_info_strptr(stp, isp->is_part, &n);
267 
268 	/*
269 	 * This private file allows developers to experiment with reporting
270 	 * different platform strings from SMBIOS.  It is not a supported
271 	 * mechanism in the long term, and does not work in the kernel.
272 	 */
273 #ifndef _KERNEL
274 	if (isp->is_type == SMB_TYPE_SYSTEM) {
275 		if (!smbios_product_checked) {
276 			int fd = open("/etc/smbios_product", O_RDONLY);
277 			if (fd >= 0) {
278 				(void) read(fd, smbios_product_override,
279 				    sizeof (smbios_product_override) - 1);
280 				(void) close(fd);
281 			}
282 			smbios_product_checked = B_TRUE;
283 		}
284 
285 		if (smbios_product_override[0] != '\0')
286 			ip->smbi_product = smbios_product_override;
287 	}
288 #endif
289 
290 	/*
291 	 * If we have a port with an empty internal reference designator string
292 	 * try using the external reference designator string instead.
293 	 */
294 	if (isp->is_type == SMB_TYPE_PORT && ip->smbi_location[0] == '\0') {
295 		ip->smbi_location = smb_info_strptr(stp,
296 		    offsetof(smb_port_t, smbpo_eref), &n);
297 	}
298 
299 	return (n ? 0 : smb_set_errno(shp, ESMB_NOINFO));
300 }
301 
302 /*
303  * Returns the actual number of contained objects.
304  *
305  * idc - number of contained objects
306  * idv - returned array of contained objects
307  */
308 int
309 smbios_info_contains(smbios_hdl_t *shp, id_t id, uint_t idc, id_t *idv)
310 {
311 	const smb_struct_t *stp = smb_lookup_id(shp, id);
312 	const struct smb_infospec *isp;
313 	id_t *cp;
314 	uint_t size;
315 	uint8_t cnt;
316 	int i, n;
317 
318 	if (stp == NULL) {
319 		return (-1); /* errno is set for us */
320 	}
321 
322 	for (isp = _smb_infospecs; isp->is_type != SMB_TYPE_EOT; isp++) {
323 		if (isp->is_type == stp->smbst_hdr->smbh_type)
324 			break;
325 	}
326 	if (isp->is_type == SMB_TYPE_EOT)
327 		return (smb_set_errno(shp, ESMB_TYPE));
328 
329 	size = isp->is_contsz;
330 	cnt = *((uint8_t *)(uintptr_t)stp->smbst_hdr + isp->is_contc);
331 	cp = (id_t *)((uintptr_t)stp->smbst_hdr + isp->is_contv);
332 
333 	n = MIN(cnt, idc);
334 	for (i = 0; i < n; i++) {
335 		if (size == SMB_CONT_WORD)
336 			idv[i] = *((uint8_t *)(uintptr_t)cp + (i * 2));
337 		else if (size == SMB_CONT_BYTE)
338 			idv[i] = *((uint8_t *)(uintptr_t)cp + (i * 3));
339 		else
340 			return (smb_set_errno(shp, ESMB_INVAL));
341 	}
342 
343 	return (cnt);
344 }
345 
346 id_t
347 smbios_info_bios(smbios_hdl_t *shp, smbios_bios_t *bp)
348 {
349 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BIOS);
350 	const smb_bios_t *bip;
351 
352 	if (stp == NULL)
353 		return (-1); /* errno is set for us */
354 
355 	if (stp->smbst_hdr->smbh_len < sizeof (smb_bios_t) - sizeof (uint8_t))
356 		return (smb_set_errno(shp, ESMB_CORRUPT));
357 
358 	bip = (smb_bios_t *)(uintptr_t)stp->smbst_hdr;
359 	bzero(bp, sizeof (smb_base_bios_t));
360 	if (smb_libgteq(shp, SMB_VERSION_31)) {
361 		bp->smbb_extromsize = 0;
362 	}
363 
364 	bp->smbb_vendor = smb_strptr(stp, bip->smbbi_vendor);
365 	bp->smbb_version = smb_strptr(stp, bip->smbbi_version);
366 	bp->smbb_segment = bip->smbbi_segment;
367 	bp->smbb_reldate = smb_strptr(stp, bip->smbbi_reldate);
368 	bp->smbb_romsize = 64 * 1024 * ((uint32_t)bip->smbbi_romsize + 1);
369 	bp->smbb_runsize = 16 * (0x10000 - (uint32_t)bip->smbbi_segment);
370 	bp->smbb_cflags = bip->smbbi_cflags;
371 
372 	/*
373 	 * If one or more extension bytes are present, reset smbb_xcflags to
374 	 * point to them.  Otherwise leave this member set to NULL.
375 	 */
376 	if (stp->smbst_hdr->smbh_len >= sizeof (smb_bios_t)) {
377 		bp->smbb_xcflags = bip->smbbi_xcflags;
378 		bp->smbb_nxcflags = stp->smbst_hdr->smbh_len -
379 		    sizeof (smb_bios_t) + 1;
380 
381 		if (bp->smbb_nxcflags > SMB_BIOSXB_ECFW_MIN &&
382 		    smb_gteq(shp, SMB_VERSION_24)) {
383 			bp->smbb_biosv.smbv_major =
384 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MAJ];
385 			bp->smbb_biosv.smbv_minor =
386 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MIN];
387 			bp->smbb_ecfwv.smbv_major =
388 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MAJ];
389 			bp->smbb_ecfwv.smbv_minor =
390 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MIN];
391 		}
392 
393 		if (bp->smbb_nxcflags > SMB_BIOSXB_EXTROM + 1 &&
394 		    smb_gteq(shp, SMB_VERSION_31)) {
395 			uint16_t val;
396 			uint64_t rs;
397 
398 			/*
399 			 * Because of the fact that the extended size is a
400 			 * uint16_t and we'd need to define an explicit
401 			 * endian-aware way to access it, we don't include it in
402 			 * the number of extended flags below and thus subtract
403 			 * its size.
404 			 */
405 			bp->smbb_nxcflags -= sizeof (uint16_t);
406 			bcopy(&bip->smbbi_xcflags[SMB_BIOSXB_EXTROM], &val,
407 			    sizeof (val));
408 			val = LE_16(val);
409 
410 			/*
411 			 * The upper two bits of the extended rom size are used
412 			 * to indicate whether the other 14 bits are in MB or
413 			 * GB.
414 			 */
415 			rs = SMB_BIOS_EXTROM_VALUE_MASK(val);
416 			switch (SMB_BIOS_EXTROM_SHIFT_MASK(val)) {
417 			case 0:
418 				rs *= 1024ULL * 1024ULL;
419 				break;
420 			case 1:
421 				rs *= 1024ULL * 1024ULL * 1024ULL;
422 				break;
423 			default:
424 				rs = 0;
425 				break;
426 			}
427 
428 			if (smb_libgteq(shp, SMB_VERSION_31)) {
429 				bp->smbb_extromsize = rs;
430 			}
431 		}
432 	}
433 
434 	if (smb_libgteq(shp, SMB_VERSION_31) && bp->smbb_extromsize == 0) {
435 		bp->smbb_extromsize = bp->smbb_romsize;
436 	}
437 
438 	return (stp->smbst_hdr->smbh_hdl);
439 }
440 
441 id_t
442 smbios_info_system(smbios_hdl_t *shp, smbios_system_t *sip)
443 {
444 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM);
445 	smb_system_t si;
446 
447 	if (stp == NULL)
448 		return (-1); /* errno is set for us */
449 
450 	smb_info_bcopy(stp->smbst_hdr, &si, sizeof (si));
451 	bzero(sip, sizeof (smbios_system_t));
452 
453 	sip->smbs_uuid = ((smb_system_t *)stp->smbst_hdr)->smbsi_uuid;
454 	sip->smbs_uuidlen = sizeof (si.smbsi_uuid);
455 	sip->smbs_wakeup = si.smbsi_wakeup;
456 	sip->smbs_sku = smb_strptr(stp, si.smbsi_sku);
457 	sip->smbs_family = smb_strptr(stp, si.smbsi_family);
458 
459 	return (stp->smbst_hdr->smbh_hdl);
460 }
461 
462 int
463 smbios_info_bboard(smbios_hdl_t *shp, id_t id, smbios_bboard_t *bbp)
464 {
465 	const smb_struct_t *stp = smb_lookup_id(shp, id);
466 	smb_bboard_t bb;
467 
468 	if (stp == NULL)
469 		return (-1); /* errno is set for us */
470 
471 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_BASEBOARD)
472 		return (smb_set_errno(shp, ESMB_TYPE));
473 
474 	smb_info_bcopy(stp->smbst_hdr, &bb, sizeof (bb));
475 	bzero(bbp, sizeof (smbios_bboard_t));
476 
477 	bbp->smbb_chassis = bb.smbbb_chassis;
478 	bbp->smbb_flags = bb.smbbb_flags;
479 	bbp->smbb_type = bb.smbbb_type;
480 	bbp->smbb_contn = bb.smbbb_cn;
481 
482 	return (0);
483 }
484 
485 int
486 smbios_info_chassis(smbios_hdl_t *shp, id_t id, smbios_chassis_t *chp)
487 {
488 	const smb_struct_t *stp = smb_lookup_id(shp, id);
489 	/* Length is measurable by one byte, so it'll be no more than 255. */
490 	uint8_t buf[256];
491 	smb_chassis_t *ch = (smb_chassis_t *)&buf[0];
492 
493 	if (stp == NULL)
494 		return (-1); /* errno is set for us */
495 
496 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CHASSIS)
497 		return (smb_set_errno(shp, ESMB_TYPE));
498 
499 	smb_info_bcopy(stp->smbst_hdr, ch, sizeof (buf));
500 	bzero(chp, sizeof (smb_base_chassis_t));
501 	if (smb_libgteq(shp, SMB_VERSION_27)) {
502 		bzero(chp->smbc_sku, sizeof (chp->smbc_sku));
503 	}
504 
505 	chp->smbc_oemdata = ch->smbch_oemdata;
506 	chp->smbc_lock = (ch->smbch_type & SMB_CHT_LOCK) != 0;
507 	chp->smbc_type = ch->smbch_type & ~SMB_CHT_LOCK;
508 	chp->smbc_bustate = ch->smbch_bustate;
509 	chp->smbc_psstate = ch->smbch_psstate;
510 	chp->smbc_thstate = ch->smbch_thstate;
511 	chp->smbc_security = ch->smbch_security;
512 	chp->smbc_uheight = ch->smbch_uheight;
513 	chp->smbc_cords = ch->smbch_cords;
514 	chp->smbc_elems = ch->smbch_cn;
515 	chp->smbc_elemlen = ch->smbch_cm;
516 
517 	if (smb_libgteq(shp, SMB_VERSION_27)) {
518 		(void) strlcpy(chp->smbc_sku, SMB_CH_SKU(ch),
519 		    sizeof (chp->smbc_sku));
520 	}
521 
522 	return (0);
523 }
524 
525 int
526 smbios_info_processor(smbios_hdl_t *shp, id_t id, smbios_processor_t *pp)
527 {
528 	const smb_struct_t *stp = smb_lookup_id(shp, id);
529 	smb_processor_t p;
530 
531 	if (stp == NULL)
532 		return (-1); /* errno is set for us */
533 
534 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PROCESSOR)
535 		return (smb_set_errno(shp, ESMB_TYPE));
536 
537 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
538 	bzero(pp, sizeof (smb_base_processor_t));
539 
540 	pp->smbp_cpuid = p.smbpr_cpuid;
541 	pp->smbp_type = p.smbpr_type;
542 	pp->smbp_family = p.smbpr_family;
543 	pp->smbp_voltage = p.smbpr_voltage;
544 	pp->smbp_maxspeed = p.smbpr_maxspeed;
545 	pp->smbp_curspeed = p.smbpr_curspeed;
546 	pp->smbp_status = p.smbpr_status;
547 	pp->smbp_upgrade = p.smbpr_upgrade;
548 	pp->smbp_l1cache = p.smbpr_l1cache;
549 	pp->smbp_l2cache = p.smbpr_l2cache;
550 	pp->smbp_l3cache = p.smbpr_l3cache;
551 
552 	if (smb_libgteq(shp, SMB_VERSION_25)) {
553 		pp->smbp_corecount = p.smbpr_corecount;
554 		pp->smbp_coresenabled = p.smbpr_coresenabled;
555 		pp->smbp_threadcount = p.smbpr_threadcount;
556 		pp->smbp_cflags = p.smbpr_cflags;
557 	}
558 
559 	if (smb_libgteq(shp, SMB_VERSION_26)) {
560 		pp->smbp_family2 = p.smbpr_family2;
561 	}
562 
563 	if (smb_libgteq(shp, SMB_VERSION_30)) {
564 		pp->smbp_corecount2 = p.smbpr_corecount2;
565 		pp->smbp_coresenabled2 = p.smbpr_coresenabled2;
566 		pp->smbp_threadcount2 = p.smbpr_threadcount2;
567 	}
568 
569 	return (0);
570 }
571 
572 int
573 smbios_info_cache(smbios_hdl_t *shp, id_t id, smbios_cache_t *cap)
574 {
575 	const smb_struct_t *stp = smb_lookup_id(shp, id);
576 	smb_cache_t c;
577 
578 	if (stp == NULL)
579 		return (-1); /* errno is set for us */
580 
581 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CACHE)
582 		return (smb_set_errno(shp, ESMB_TYPE));
583 
584 	smb_info_bcopy(stp->smbst_hdr, &c, sizeof (c));
585 	bzero(cap, sizeof (smb_base_cache_t));
586 
587 	cap->smba_maxsize = SMB_CACHE_SIZE(c.smbca_maxsize);
588 	cap->smba_size = SMB_CACHE_SIZE(c.smbca_size);
589 	cap->smba_stype = c.smbca_stype;
590 	cap->smba_ctype = c.smbca_ctype;
591 	cap->smba_speed = c.smbca_speed;
592 	cap->smba_etype = c.smbca_etype;
593 	cap->smba_ltype = c.smbca_ltype;
594 	cap->smba_assoc = c.smbca_assoc;
595 	cap->smba_level = SMB_CACHE_CFG_LEVEL(c.smbca_config);
596 	cap->smba_mode = SMB_CACHE_CFG_MODE(c.smbca_config);
597 	cap->smba_location = SMB_CACHE_CFG_LOCATION(c.smbca_config);
598 
599 	if (SMB_CACHE_CFG_ENABLED(c.smbca_config))
600 		cap->smba_flags |= SMB_CAF_ENABLED;
601 
602 	if (SMB_CACHE_CFG_SOCKETED(c.smbca_config))
603 		cap->smba_flags |= SMB_CAF_SOCKETED;
604 
605 	if (smb_libgteq(shp, SMB_VERSION_31)) {
606 		if (smb_gteq(shp, SMB_VERSION_31)) {
607 			cap->smba_maxsize2 =
608 			    SMB_CACHE_EXT_SIZE(c.smbca_maxsize2);
609 			cap->smba_size2 = SMB_CACHE_EXT_SIZE(c.smbca_size2);
610 		} else {
611 			cap->smba_maxsize2 = cap->smba_maxsize;
612 			cap->smba_size2 = cap->smba_size;
613 		}
614 	}
615 
616 	return (0);
617 }
618 
619 int
620 smbios_info_port(smbios_hdl_t *shp, id_t id, smbios_port_t *pop)
621 {
622 	const smb_struct_t *stp = smb_lookup_id(shp, id);
623 	smb_port_t p;
624 
625 	if (stp == NULL)
626 		return (-1); /* errno is set for us */
627 
628 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PORT)
629 		return (smb_set_errno(shp, ESMB_TYPE));
630 
631 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
632 	bzero(pop, sizeof (smbios_port_t));
633 
634 	pop->smbo_iref = smb_strptr(stp, p.smbpo_iref);
635 	pop->smbo_eref = smb_strptr(stp, p.smbpo_eref);
636 
637 	pop->smbo_itype = p.smbpo_itype;
638 	pop->smbo_etype = p.smbpo_etype;
639 	pop->smbo_ptype = p.smbpo_ptype;
640 
641 	return (0);
642 }
643 
644 int
645 smbios_info_slot(smbios_hdl_t *shp, id_t id, smbios_slot_t *sp)
646 {
647 	const smb_struct_t *stp = smb_lookup_id(shp, id);
648 	smb_slot_t s;
649 
650 	if (stp == NULL)
651 		return (-1); /* errno is set for us */
652 
653 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_SLOT)
654 		return (smb_set_errno(shp, ESMB_TYPE));
655 
656 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
657 	bzero(sp, sizeof (smbios_slot_t));
658 
659 	sp->smbl_name = smb_strptr(stp, s.smbsl_name);
660 	sp->smbl_type = s.smbsl_type;
661 	sp->smbl_width = s.smbsl_width;
662 	sp->smbl_usage = s.smbsl_usage;
663 	sp->smbl_length = s.smbsl_length;
664 	sp->smbl_id = s.smbsl_id;
665 	sp->smbl_ch1 = s.smbsl_ch1;
666 	sp->smbl_ch2 = s.smbsl_ch2;
667 	sp->smbl_sg = s.smbsl_sg;
668 	sp->smbl_bus = s.smbsl_bus;
669 	sp->smbl_df = s.smbsl_df;
670 
671 	return (0);
672 }
673 
674 int
675 smbios_info_obdevs_ext(smbios_hdl_t *shp, id_t id, smbios_obdev_ext_t *oep)
676 {
677 	const smb_struct_t *stp = smb_lookup_id(shp, id);
678 	smb_obdev_ext_t obe;
679 
680 	if (stp == NULL)
681 		return (-1); /* errno is set for us */
682 
683 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVEXT)
684 		return (smb_set_errno(shp, ESMB_TYPE));
685 
686 	smb_info_bcopy(stp->smbst_hdr, &obe, sizeof (obe));
687 	bzero(oep, sizeof (smbios_obdev_ext_t));
688 
689 	oep->smboe_name = smb_strptr(stp, obe.smbobe_name);
690 	oep->smboe_dtype = obe.smbobe_dtype;
691 	oep->smboe_dti = obe.smbobe_dti;
692 	oep->smboe_sg = obe.smbobe_sg;
693 	oep->smboe_bus = obe.smbobe_bus;
694 	oep->smboe_df = obe.smbobe_df;
695 
696 	return (0);
697 }
698 
699 int
700 smbios_info_obdevs(smbios_hdl_t *shp, id_t id, int obc, smbios_obdev_t *obp)
701 {
702 	const smb_struct_t *stp = smb_lookup_id(shp, id);
703 	const smb_obdev_t *op;
704 	int i, m, n;
705 
706 	if (stp == NULL)
707 		return (-1); /* errno is set for us */
708 
709 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVS)
710 		return (smb_set_errno(shp, ESMB_TYPE));
711 
712 	op = (smb_obdev_t *)((uintptr_t)stp->smbst_hdr + sizeof (smb_header_t));
713 	m = (stp->smbst_hdr->smbh_len - sizeof (smb_header_t)) / sizeof (*op);
714 	n = MIN(m, obc);
715 
716 	for (i = 0; i < n; i++, op++, obp++) {
717 		obp->smbd_name = smb_strptr(stp, op->smbob_name);
718 		obp->smbd_type = op->smbob_type & ~SMB_OBT_ENABLED;
719 		obp->smbd_enabled = (op->smbob_type & SMB_OBT_ENABLED) != 0;
720 	}
721 
722 	return (m);
723 }
724 
725 /*
726  * The implementation structures for OEMSTR, SYSCONFSTR, and LANG all use the
727  * first byte to indicate the size of a string table at the end of the record.
728  * Therefore, smbios_info_strtab() can be used to retrieve the table size and
729  * strings for any of these underlying record types.
730  */
731 int
732 smbios_info_strtab(smbios_hdl_t *shp, id_t id, int argc, const char *argv[])
733 {
734 	const smb_struct_t *stp = smb_lookup_id(shp, id);
735 	smb_strtab_t s;
736 	int i, n;
737 
738 	if (stp == NULL)
739 		return (-1); /* errno is set for us */
740 
741 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR &&
742 	    stp->smbst_hdr->smbh_type != SMB_TYPE_SYSCONFSTR &&
743 	    stp->smbst_hdr->smbh_type != SMB_TYPE_LANG)
744 		return (smb_set_errno(shp, ESMB_TYPE));
745 
746 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
747 	n = MIN(s.smbtb_count, argc);
748 
749 	for (i = 0; i < n; i++)
750 		argv[i] = smb_strptr(stp, i + 1);
751 
752 	return (s.smbtb_count);
753 }
754 
755 id_t
756 smbios_info_lang(smbios_hdl_t *shp, smbios_lang_t *lp)
757 {
758 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_LANG);
759 	smb_lang_t l;
760 
761 	if (stp == NULL)
762 		return (-1); /* errno is set for us */
763 
764 	smb_info_bcopy(stp->smbst_hdr, &l, sizeof (l));
765 	bzero(lp, sizeof (smbios_lang_t));
766 
767 	lp->smbla_cur = smb_strptr(stp, l.smblang_cur);
768 	lp->smbla_fmt = l.smblang_flags & 1;
769 	lp->smbla_num = l.smblang_num;
770 
771 	return (stp->smbst_hdr->smbh_hdl);
772 }
773 
774 id_t
775 smbios_info_eventlog(smbios_hdl_t *shp, smbios_evlog_t *evp)
776 {
777 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_EVENTLOG);
778 	const smb_sel_t *sel;
779 	size_t len;
780 
781 	if (stp == NULL)
782 		return (-1); /* errno is set for us */
783 
784 	if (stp->smbst_hdr->smbh_len < sizeof (smb_sel_t) - sizeof (uint8_t))
785 		return (smb_set_errno(shp, ESMB_CORRUPT));
786 
787 	sel = (smb_sel_t *)(uintptr_t)stp->smbst_hdr;
788 	len = stp->smbst_hdr->smbh_len - sizeof (smb_sel_t) + sizeof (uint8_t);
789 	bzero(evp, sizeof (smbios_evlog_t));
790 
791 	if (len < sel->smbsel_typec * sel->smbsel_typesz)
792 		return (smb_set_errno(shp, ESMB_CORRUPT));
793 
794 	evp->smbev_size = sel->smbsel_len;
795 	evp->smbev_hdr = sel->smbsel_hdroff;
796 	evp->smbev_data = sel->smbsel_dataoff;
797 	evp->smbev_method = sel->smbsel_method;
798 	evp->smbev_flags = sel->smbsel_status;
799 	evp->smbev_format = sel->smbsel_format;
800 	evp->smbev_token = sel->smbsel_token;
801 	evp->smbev_addr.eva_addr = sel->smbsel_addr;
802 
803 	if (sel->smbsel_typesz == sizeof (smbios_evtype_t)) {
804 		evp->smbev_typec = sel->smbsel_typec;
805 		evp->smbev_typev = (void *)(uintptr_t)sel->smbsel_typev;
806 	}
807 
808 	return (stp->smbst_hdr->smbh_hdl);
809 }
810 
811 int
812 smbios_info_memarray(smbios_hdl_t *shp, id_t id, smbios_memarray_t *map)
813 {
814 	const smb_struct_t *stp = smb_lookup_id(shp, id);
815 	smb_memarray_t m;
816 
817 	if (stp == NULL)
818 		return (-1); /* errno is set for us */
819 
820 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAY)
821 		return (smb_set_errno(shp, ESMB_TYPE));
822 
823 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
824 	bzero(map, sizeof (smbios_memarray_t));
825 
826 	map->smbma_location = m.smbmarr_loc;
827 	map->smbma_use = m.smbmarr_use;
828 	map->smbma_ecc = m.smbmarr_ecc;
829 	map->smbma_ndevs = m.smbmarr_ndevs;
830 	map->smbma_err = m.smbmarr_err;
831 
832 	if (m.smbmarr_cap != 0x80000000)
833 		map->smbma_size = (uint64_t)m.smbmarr_cap * 1024;
834 	else if (m.smbmarr_extcap != 0)
835 		map->smbma_size = m.smbmarr_extcap;
836 	else
837 		map->smbma_size = 0; /* unknown */
838 
839 	return (0);
840 }
841 
842 int
843 smbios_info_memarrmap(smbios_hdl_t *shp, id_t id, smbios_memarrmap_t *map)
844 {
845 	const smb_struct_t *stp = smb_lookup_id(shp, id);
846 	smb_memarrmap_t m;
847 
848 	if (stp == NULL)
849 		return (-1); /* errno is set for us */
850 
851 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAYMAP)
852 		return (smb_set_errno(shp, ESMB_TYPE));
853 
854 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
855 	bzero(map, sizeof (smbios_memarrmap_t));
856 
857 	map->smbmam_array = m.smbamap_array;
858 	map->smbmam_width = m.smbamap_width;
859 
860 	if (m.smbamap_start != 0xFFFFFFFF && m.smbamap_end != 0xFFFFFFFF) {
861 		map->smbmam_addr = (uint64_t)m.smbamap_start * 1024;
862 		map->smbmam_size = (uint64_t)
863 		    (m.smbamap_end - m.smbamap_start + 1) * 1024;
864 	} else if (m.smbamap_extstart != 0 && m.smbamap_extend != 0) {
865 		map->smbmam_addr = m.smbamap_extstart;
866 		map->smbmam_size = m.smbamap_extend - m.smbamap_extstart + 1;
867 	}
868 
869 	return (0);
870 }
871 
872 int
873 smbios_info_memdevice(smbios_hdl_t *shp, id_t id, smbios_memdevice_t *mdp)
874 {
875 	const smb_struct_t *stp = smb_lookup_id(shp, id);
876 	smb_memdevice_t m;
877 
878 	if (stp == NULL)
879 		return (-1); /* errno is set for us */
880 
881 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICE)
882 		return (smb_set_errno(shp, ESMB_TYPE));
883 
884 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
885 	bzero(mdp, sizeof (smb_base_memdevice_t));
886 
887 	mdp->smbmd_array = m.smbmdev_array;
888 	mdp->smbmd_error = m.smbmdev_error;
889 	mdp->smbmd_twidth = m.smbmdev_twidth == 0xFFFF ? -1U : m.smbmdev_twidth;
890 	mdp->smbmd_dwidth = m.smbmdev_dwidth == 0xFFFF ? -1U : m.smbmdev_dwidth;
891 
892 	if (m.smbmdev_size == 0x7FFF) {
893 		mdp->smbmd_size = (uint64_t)m.smbmdev_extsize;
894 		mdp->smbmd_size *= 1024 * 1024; /* convert MB to bytes */
895 	} else if (m.smbmdev_size != 0xFFFF) {
896 		mdp->smbmd_size = (uint64_t)(m.smbmdev_size & ~SMB_MDS_KBYTES);
897 		if (m.smbmdev_size & SMB_MDS_KBYTES)
898 			mdp->smbmd_size *= 1024;
899 		else
900 			mdp->smbmd_size *= 1024 * 1024;
901 	} else
902 		mdp->smbmd_size = -1ULL; /* size unknown */
903 
904 	mdp->smbmd_form = m.smbmdev_form;
905 	mdp->smbmd_set = m.smbmdev_set;
906 	mdp->smbmd_type = m.smbmdev_type;
907 	mdp->smbmd_speed = m.smbmdev_speed;
908 	mdp->smbmd_flags = m.smbmdev_flags;
909 	mdp->smbmd_dloc = smb_strptr(stp, m.smbmdev_dloc);
910 	mdp->smbmd_bloc = smb_strptr(stp, m.smbmdev_bloc);
911 
912 	if (smb_libgteq(shp, SMB_VERSION_26)) {
913 		mdp->smbmd_rank = m.smbmdev_attrs & 0x0F;
914 	}
915 
916 	if (smb_libgteq(shp, SMB_VERSION_27)) {
917 		mdp->smbmd_clkspeed = m.smbmdev_clkspeed;
918 	}
919 
920 	if (smb_libgteq(shp, SMB_VERSION_28)) {
921 		mdp->smbmd_minvolt = m.smbmdev_minvolt;
922 		mdp->smbmd_maxvolt = m.smbmdev_maxvolt;
923 		mdp->smbmd_confvolt = m.smbmdev_confvolt;
924 	}
925 
926 	return (0);
927 }
928 
929 int
930 smbios_info_memdevmap(smbios_hdl_t *shp, id_t id, smbios_memdevmap_t *mdp)
931 {
932 	const smb_struct_t *stp = smb_lookup_id(shp, id);
933 	smb_memdevmap_t m;
934 
935 	if (stp == NULL)
936 		return (-1); /* errno is set for us */
937 
938 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICEMAP)
939 		return (smb_set_errno(shp, ESMB_TYPE));
940 
941 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
942 	bzero(mdp, sizeof (smbios_memdevmap_t));
943 
944 	mdp->smbmdm_device = m.smbdmap_device;
945 	mdp->smbmdm_arrmap = m.smbdmap_array;
946 	mdp->smbmdm_rpos = m.smbdmap_rpos;
947 	mdp->smbmdm_ipos = m.smbdmap_ipos;
948 	mdp->smbmdm_idepth = m.smbdmap_idepth;
949 
950 	if (m.smbdmap_start != 0xFFFFFFFF && m.smbdmap_end != 0xFFFFFFFF) {
951 		mdp->smbmdm_addr = (uint64_t)m.smbdmap_start * 1024;
952 		mdp->smbmdm_size = (uint64_t)
953 		    (m.smbdmap_end - m.smbdmap_start + 1) * 1024;
954 	} else if (m.smbdmap_extstart != 0 && m.smbdmap_extend != 0) {
955 		mdp->smbmdm_addr = m.smbdmap_extstart;
956 		mdp->smbmdm_size = m.smbdmap_extend - m.smbdmap_extstart + 1;
957 	}
958 
959 	return (0);
960 }
961 
962 id_t
963 smbios_info_hwsec(smbios_hdl_t *shp, smbios_hwsec_t *hsp)
964 {
965 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SECURITY);
966 	smb_hwsec_t hs;
967 
968 	if (stp == NULL)
969 		return (-1); /* errno is set for us */
970 
971 	smb_info_bcopy(stp->smbst_hdr, &hs, sizeof (hs));
972 	bzero(hsp, sizeof (smbios_hwsec_t));
973 
974 	hsp->smbh_pwr_ps = SMB_HWS_PWR_PS(hs.smbhs_settings);
975 	hsp->smbh_kbd_ps = SMB_HWS_KBD_PS(hs.smbhs_settings);
976 	hsp->smbh_adm_ps = SMB_HWS_ADM_PS(hs.smbhs_settings);
977 	hsp->smbh_pan_ps = SMB_HWS_PAN_PS(hs.smbhs_settings);
978 
979 	return (stp->smbst_hdr->smbh_hdl);
980 }
981 
982 id_t
983 smbios_info_boot(smbios_hdl_t *shp, smbios_boot_t *bp)
984 {
985 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BOOT);
986 	const smb_boot_t *b;
987 
988 	if (stp == NULL)
989 		return (-1); /* errno is set for us */
990 
991 	bzero(bp, sizeof (smbios_boot_t));
992 
993 	b = (smb_boot_t *)(uintptr_t)stp->smbst_hdr;
994 
995 	bp->smbt_status = b->smbbo_status[0];
996 	bp->smbt_size = stp->smbst_hdr->smbh_len - sizeof (smb_boot_t);
997 	bp->smbt_data = bp->smbt_size ? &b->smbbo_status[1] : NULL;
998 
999 	return (stp->smbst_hdr->smbh_hdl);
1000 }
1001 
1002 id_t
1003 smbios_info_ipmi(smbios_hdl_t *shp, smbios_ipmi_t *ip)
1004 {
1005 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_IPMIDEV);
1006 	smb_ipmi_t i;
1007 
1008 	if (stp == NULL)
1009 		return (-1); /* errno is set for us */
1010 
1011 	smb_info_bcopy(stp->smbst_hdr, &i, sizeof (i));
1012 	bzero(ip, sizeof (smbios_ipmi_t));
1013 
1014 	ip->smbip_type = i.smbipm_type;
1015 	ip->smbip_vers.smbv_major = SMB_IPM_SPEC_MAJOR(i.smbipm_spec);
1016 	ip->smbip_vers.smbv_minor = SMB_IPM_SPEC_MINOR(i.smbipm_spec);
1017 	ip->smbip_i2c = i.smbipm_i2c;
1018 	ip->smbip_addr = i.smbipm_addr & ~SMB_IPM_ADDR_IO;
1019 	ip->smbip_intr = i.smbipm_intr;
1020 
1021 	if (i.smbipm_bus != (uint8_t)-1)
1022 		ip->smbip_bus = i.smbipm_bus;
1023 	else
1024 		ip->smbip_bus = -1u;
1025 
1026 	if (SMB_IPM_INFO_LSB(i.smbipm_info))
1027 		ip->smbip_addr |= 1; /* turn on least-significant bit of addr */
1028 
1029 	if (i.smbipm_addr & SMB_IPM_ADDR_IO) {
1030 		switch (SMB_IPM_INFO_REGS(i.smbipm_info)) {
1031 		case SMB_IPM_REGS_1B:
1032 			ip->smbip_regspacing = 1;
1033 			break;
1034 		case SMB_IPM_REGS_4B:
1035 			ip->smbip_regspacing = 4;
1036 			break;
1037 		case SMB_IPM_REGS_16B:
1038 			ip->smbip_regspacing = 16;
1039 			break;
1040 		default:
1041 			ip->smbip_regspacing = 1;
1042 		}
1043 		ip->smbip_flags |= SMB_IPMI_F_IOADDR;
1044 	}
1045 
1046 	if (SMB_IPM_INFO_ISPEC(i.smbipm_info))
1047 		ip->smbip_flags |= SMB_IPMI_F_INTRSPEC;
1048 
1049 	if (SMB_IPM_INFO_IPOL(i.smbipm_info) == SMB_IPM_IPOL_HI)
1050 		ip->smbip_flags |= SMB_IPMI_F_INTRHIGH;
1051 
1052 	if (SMB_IPM_INFO_IMODE(i.smbipm_info) == SMB_IPM_IMODE_EDGE)
1053 		ip->smbip_flags |= SMB_IPMI_F_INTREDGE;
1054 
1055 	return (stp->smbst_hdr->smbh_hdl);
1056 }
1057 
1058 static boolean_t
1059 smbios_has_oemstr(smbios_hdl_t *shp, const char *oemstr)
1060 {
1061 	const smb_struct_t *stp = shp->sh_structs;
1062 	smb_strtab_t s;
1063 	int i, j;
1064 
1065 	for (i = 0; i < shp->sh_nstructs; i++, stp++) {
1066 		if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR)
1067 			continue;
1068 
1069 		smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
1070 		for (j = 0; j < s.smbtb_count; j++)
1071 			if (strcmp(smb_strptr(stp, j + 1), oemstr) == 0)
1072 				return (B_TRUE);
1073 	}
1074 
1075 	return (B_FALSE);
1076 }
1077 
1078 static const char *
1079 smb_serial_valid(const char *serial)
1080 {
1081 	char buf[MAXNAMELEN];
1082 	int i = 0;
1083 
1084 	if (serial == NULL)
1085 		return (NULL);
1086 
1087 	(void) strlcpy(buf, serial, sizeof (buf));
1088 
1089 	while (buf[i] != '\0' && buf[i] == ' ')
1090 		i++;
1091 
1092 	if (buf[i] == '\0' || strstr(buf, SMB_DEFAULT1) != NULL ||
1093 	    strstr(buf, SMB_DEFAULT2) != NULL)
1094 		return (NULL);
1095 
1096 	return (serial);
1097 }
1098 
1099 /*
1100  * Get chassis SN or product SN
1101  */
1102 static int
1103 smb_get_sn(smbios_hdl_t *shp, const char **psnp, const char **csnp)
1104 {
1105 	const smb_struct_t *stp;
1106 	smbios_info_t s1, s3;
1107 
1108 	if (psnp == NULL || csnp == NULL)
1109 		return (smb_set_errno(shp, ESMB_INVAL));
1110 
1111 	*psnp = *csnp = NULL;
1112 
1113 	/*
1114 	 * If SMBIOS meets Sun's PRMS requirements, retrieve product SN
1115 	 * from type 1 structure, and chassis SN from type 3 structure.
1116 	 * Otherwise return SN in type 1 structure as chassis SN.
1117 	 */
1118 
1119 	/* Get type 1 SN */
1120 	if ((stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM)) == NULL ||
1121 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s1) == SMB_ERR)
1122 		s1.smbi_serial = NULL;
1123 
1124 	/* Get type 3 SN */
1125 	if ((stp = smb_lookup_type(shp, SMB_TYPE_CHASSIS)) == NULL ||
1126 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s3) == SMB_ERR)
1127 		s3.smbi_serial = NULL;
1128 
1129 	if (smbios_has_oemstr(shp, SMB_PRMS1)) {
1130 		*psnp = smb_serial_valid(s1.smbi_serial);
1131 		*csnp = smb_serial_valid(s3.smbi_serial);
1132 	} else {
1133 		*csnp = smb_serial_valid(s1.smbi_serial);
1134 	}
1135 
1136 	return (0);
1137 }
1138 
1139 const char *
1140 smbios_psn(smbios_hdl_t *shp)
1141 {
1142 	const char *psn, *csn;
1143 
1144 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : psn);
1145 }
1146 
1147 const char *
1148 smbios_csn(smbios_hdl_t *shp)
1149 {
1150 	const char *psn, *csn;
1151 
1152 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : csn);
1153 }
1154 
1155 int
1156 smbios_info_extprocessor(smbios_hdl_t *shp, id_t id,
1157     smbios_processor_ext_t *epp)
1158 {
1159 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1160 	smb_processor_ext_t *exp;
1161 
1162 	if (stp == NULL)
1163 		return (-1); /* errno is set for us */
1164 
1165 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_PROCESSOR)
1166 		return (smb_set_errno(shp, ESMB_TYPE));
1167 
1168 	exp = (smb_processor_ext_t *)(uintptr_t)stp->smbst_hdr;
1169 	bzero(epp, sizeof (smbios_processor_ext_t));
1170 
1171 	epp->smbpe_processor = exp->smbpre_processor;
1172 	epp->smbpe_fru = exp->smbpre_fru;
1173 	epp->smbpe_n = exp->smbpre_n;
1174 	epp->smbpe_apicid = exp->smbpre_apicid;
1175 
1176 	return (0);
1177 }
1178 
1179 int
1180 smbios_info_extport(smbios_hdl_t *shp, id_t id, smbios_port_ext_t *eportp)
1181 {
1182 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1183 	smb_port_ext_t *ep;
1184 
1185 	if (stp == NULL)
1186 		return (-1); /* errno is set for us */
1187 
1188 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_PORT)
1189 		return (smb_set_errno(shp, ESMB_TYPE));
1190 
1191 	ep = (smb_port_ext_t *)(uintptr_t)stp->smbst_hdr;
1192 	bzero(eportp, sizeof (smbios_port_ext_t));
1193 
1194 	eportp->smbporte_chassis = ep->smbpoe_chassis;
1195 	eportp->smbporte_port = ep->smbpoe_port;
1196 	eportp->smbporte_dtype = ep->smbpoe_dtype;
1197 	eportp->smbporte_devhdl = ep->smbpoe_devhdl;
1198 	eportp->smbporte_phy = ep->smbpoe_phy;
1199 
1200 	return (0);
1201 }
1202 
1203 int
1204 smbios_info_pciexrc(smbios_hdl_t *shp, id_t id,
1205     smbios_pciexrc_t *rcp)
1206 {
1207 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1208 	smb_pciexrc_t rc;
1209 
1210 	if (stp == NULL)
1211 		return (-1); /* errno is set for us */
1212 
1213 	if (stp->smbst_hdr->smbh_type != SUN_OEM_PCIEXRC)
1214 		return (smb_set_errno(shp, ESMB_TYPE));
1215 
1216 	smb_info_bcopy(stp->smbst_hdr, &rc, sizeof (rc));
1217 	bzero(rcp, sizeof (smbios_pciexrc_t));
1218 
1219 	rcp->smbpcie_bb = rc.smbpciexrc_bboard;
1220 	rcp->smbpcie_bdf = rc.smbpciexrc_bdf;
1221 
1222 	return (0);
1223 }
1224 
1225 int
1226 smbios_info_extmemarray(smbios_hdl_t *shp, id_t id, smbios_memarray_ext_t *emap)
1227 {
1228 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1229 	smb_memarray_ext_t exma;
1230 
1231 	if (stp == NULL)
1232 		return (-1); /* errno is set for us */
1233 
1234 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMARRAY)
1235 		return (smb_set_errno(shp, ESMB_TYPE));
1236 
1237 	smb_info_bcopy(stp->smbst_hdr, &exma, sizeof (exma));
1238 	bzero(emap, sizeof (smbios_memarray_ext_t));
1239 
1240 	emap->smbmae_ma = exma.smbmarre_ma;
1241 	emap->smbmae_comp = exma.smbmarre_component;
1242 	emap->smbmae_bdf = exma.smbmarre_bdf;
1243 
1244 	return (0);
1245 }
1246 
1247 int
1248 smbios_info_extmemdevice(smbios_hdl_t *shp, id_t id,
1249     smbios_memdevice_ext_t *emdp)
1250 {
1251 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1252 	smb_memdevice_ext_t exmd;
1253 
1254 	if (stp == NULL)
1255 		return (-1); /* errno is set for us */
1256 
1257 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMDEVICE)
1258 		return (smb_set_errno(shp, ESMB_TYPE));
1259 
1260 	smb_info_bcopy(stp->smbst_hdr, &exmd, sizeof (exmd));
1261 	bzero(emdp, sizeof (smbios_memdevice_ext_t));
1262 
1263 	emdp->smbmdeve_md = exmd.smbmdeve_mdev;
1264 	emdp->smbmdeve_drch = exmd.smbmdeve_dchan;
1265 	emdp->smbmdeve_ncs  = exmd.smbmdeve_ncs;
1266 	emdp->smbmdeve_cs = exmd.smbmdeve_cs;
1267 
1268 	return (0);
1269 }
1270 
1271 int
1272 smbios_info_powersup(smbios_hdl_t *shp, id_t id, smbios_powersup_t *psup)
1273 {
1274 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1275 	smb_powersup_t psu;
1276 
1277 	if (stp == NULL)
1278 		return (-1); /* errno is set for us */
1279 
1280 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_POWERSUP)
1281 		return (smb_set_errno(shp, ESMB_TYPE));
1282 
1283 	/* The minimum length required by the spec is 0x10. */
1284 	if (stp->smbst_hdr->smbh_len < 0x10)
1285 		return (smb_set_errno(shp, ESMB_SHORT));
1286 
1287 	bzero(psup, sizeof (*psup));
1288 	smb_info_bcopy(stp->smbst_hdr, &psu, sizeof (psu));
1289 	psup->smbps_group = psu.smbpsup_group;
1290 	psup->smbps_maxout = psu.smbpsup_max;
1291 
1292 	if (SMB_PSU_CHARS_ISHOT(psu.smbpsup_char))
1293 		psup->smbps_flags |= SMB_POWERSUP_F_HOT;
1294 	if (SMB_PSU_CHARS_ISPRES(psu.smbpsup_char))
1295 		psup->smbps_flags |= SMB_POWERSUP_F_PRESENT;
1296 	if (SMB_PSU_CHARS_ISUNPLUG(psu.smbpsup_char))
1297 		psup->smbps_flags |= SMB_POWERSUP_F_UNPLUG;
1298 
1299 	psup->smbps_ivrs = SMB_PSU_CHARS_IVRS(psu.smbpsup_char);
1300 	psup->smbps_status = SMB_PSU_CHARS_STATUS(psu.smbpsup_char);
1301 	psup->smbps_pstype = SMB_PSU_CHARS_TYPE(psu.smbpsup_char);
1302 
1303 	if (stp->smbst_hdr->smbh_len >= 0x12) {
1304 		psup->smbps_vprobe = psu.smbpsup_vprobe;
1305 	} else {
1306 		psup->smbps_vprobe = 0xffff;
1307 	}
1308 
1309 	if (stp->smbst_hdr->smbh_len >= 0x14) {
1310 		psup->smbps_cooldev = psu.smbpsup_cooldev;
1311 	} else {
1312 		psup->smbps_cooldev = 0xffff;
1313 	}
1314 
1315 	if (stp->smbst_hdr->smbh_len >= 0x16) {
1316 		psup->smbps_iprobe = psu.smbpsup_iprobe;
1317 	} else {
1318 		psup->smbps_iprobe = 0xffff;
1319 	}
1320 
1321 	return (0);
1322 }
1323 
1324 int
1325 smbios_info_vprobe(smbios_hdl_t *shp, id_t id, smbios_vprobe_t *vprobe)
1326 {
1327 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1328 	smb_vprobe_t vp;
1329 
1330 	if (stp == NULL)
1331 		return (-1); /* errno is set for us */
1332 
1333 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_VPROBE)
1334 		return (smb_set_errno(shp, ESMB_TYPE));
1335 
1336 	if (stp->smbst_hdr->smbh_len < SMB_VPROBE_MINLEN)
1337 		return (smb_set_errno(shp, ESMB_SHORT));
1338 
1339 	bzero(vprobe, sizeof (*vprobe));
1340 	smb_info_bcopy(stp->smbst_hdr, &vp, sizeof (vp));
1341 	vprobe->smbvp_description = smb_strptr(stp, vp.smbvpr_descr);
1342 	vprobe->smbvp_location = SMB_VPROBE_LOCATION(vp.smbvpr_locstat);
1343 	vprobe->smbvp_status = SMB_VPROBE_STATUS(vp.smbvpr_locstat);
1344 	vprobe->smbvp_maxval = vp.smbvpr_maxval;
1345 	vprobe->smbvp_minval = vp.smbvpr_minval;
1346 	vprobe->smbvp_resolution = vp.smbvpr_resolution;
1347 	vprobe->smbvp_tolerance	= vp.smbvpr_tolerance;
1348 	vprobe->smbvp_accuracy = vp.smbvpr_accuracy;
1349 
1350 	if (stp->smbst_hdr->smbh_len >= SMB_VPROBE_NOMINAL_MINLEN) {
1351 		vprobe->smbvp_nominal = vp.smbvpr_nominal;
1352 	} else {
1353 		vprobe->smbvp_nominal = SMB_PROBE_UNKNOWN_VALUE;
1354 	}
1355 
1356 	return (0);
1357 }
1358 
1359 int
1360 smbios_info_cooldev(smbios_hdl_t *shp, id_t id, smbios_cooldev_t *cooldev)
1361 {
1362 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1363 	smb_cooldev_t cd;
1364 
1365 	if (stp == NULL)
1366 		return (-1); /* errno is set for us */
1367 
1368 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_COOLDEV)
1369 		return (smb_set_errno(shp, ESMB_TYPE));
1370 
1371 	if (stp->smbst_hdr->smbh_len < SMB_COOLDEV_MINLEN)
1372 		return (smb_set_errno(shp, ESMB_SHORT));
1373 
1374 	bzero(cooldev, sizeof (*cooldev));
1375 	smb_info_bcopy(stp->smbst_hdr, &cd, sizeof (cd));
1376 	cooldev->smbcd_tprobe = cd.smbcdev_tprobe;
1377 	cooldev->smbcd_type = SMB_COOLDEV_TYPE(cd.smbcdev_typstat);
1378 	cooldev->smbcd_status = SMB_COOLDEV_STATUS(cd.smbcdev_typstat);
1379 	cooldev->smbcd_group = cd.smbcdev_group;
1380 	cooldev->smbcd_oem = cd.smbcdev_oem;
1381 
1382 	if (stp->smbst_hdr->smbh_len >= SMB_COOLDEV_NOMINAL_MINLEN) {
1383 		cooldev->smbcd_nominal = cd.smbcdev_nominal;
1384 	} else {
1385 		cooldev->smbcd_nominal = SMB_PROBE_UNKNOWN_VALUE;
1386 	}
1387 
1388 	/*
1389 	 * The description field was added in SMBIOS version 2.7. The
1390 	 * SMB_TYPE_COOLDEV support was only added after all of the 2.7+ fields
1391 	 * were added in the spec. So while a user may request an older version,
1392 	 * we don't have to worry about old structures and just simply skip it
1393 	 * if they're not asking for it.
1394 	 */
1395 	if (smb_libgteq(shp, SMB_VERSION_27) &&
1396 	    smb_gteq(shp, SMB_VERSION_27) &&
1397 	    stp->smbst_hdr->smbh_len >= SMB_COOLDEV_DESCR_MINLEN) {
1398 		cooldev->smbcd_descr = smb_strptr(stp, cd.smbcdev_descr);
1399 	} else {
1400 		cooldev->smbcd_descr = NULL;
1401 	}
1402 
1403 	return (0);
1404 }
1405 
1406 int
1407 smbios_info_tprobe(smbios_hdl_t *shp, id_t id, smbios_tprobe_t *tprobe)
1408 {
1409 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1410 	smb_tprobe_t tp;
1411 
1412 	if (stp == NULL)
1413 		return (-1); /* errno is set for us */
1414 
1415 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_TPROBE)
1416 		return (smb_set_errno(shp, ESMB_TYPE));
1417 
1418 	if (stp->smbst_hdr->smbh_len < SMB_TPROBE_MINLEN)
1419 		return (smb_set_errno(shp, ESMB_SHORT));
1420 
1421 	bzero(tprobe, sizeof (*tprobe));
1422 	smb_info_bcopy(stp->smbst_hdr, &tp, sizeof (tp));
1423 	tprobe->smbtp_description = smb_strptr(stp, tp.smbtpr_descr);
1424 	tprobe->smbtp_location = SMB_TPROBE_LOCATION(tp.smbtpr_locstat);
1425 	tprobe->smbtp_status = SMB_TPROBE_STATUS(tp.smbtpr_locstat);
1426 	tprobe->smbtp_maxval = tp.smbtpr_maxval;
1427 	tprobe->smbtp_minval = tp.smbtpr_minval;
1428 	tprobe->smbtp_resolution = tp.smbtpr_resolution;
1429 	tprobe->smbtp_tolerance	= tp.smbtpr_tolerance;
1430 	tprobe->smbtp_accuracy = tp.smbtpr_accuracy;
1431 
1432 	if (stp->smbst_hdr->smbh_len >= SMB_TPROBE_NOMINAL_MINLEN) {
1433 		tprobe->smbtp_nominal = tp.smbtpr_nominal;
1434 	} else {
1435 		tprobe->smbtp_nominal = SMB_PROBE_UNKNOWN_VALUE;
1436 	}
1437 
1438 	return (0);
1439 }
1440 
1441 int
1442 smbios_info_iprobe(smbios_hdl_t *shp, id_t id, smbios_iprobe_t *iprobe)
1443 {
1444 	const smb_struct_t *sip = smb_lookup_id(shp, id);
1445 	smb_iprobe_t ip;
1446 
1447 	if (sip == NULL)
1448 		return (-1); /* errno is set for us */
1449 
1450 	if (sip->smbst_hdr->smbh_type != SMB_TYPE_IPROBE)
1451 		return (smb_set_errno(shp, ESMB_TYPE));
1452 
1453 	if (sip->smbst_hdr->smbh_len < SMB_IPROBE_MINLEN)
1454 		return (smb_set_errno(shp, ESMB_SHORT));
1455 
1456 	bzero(iprobe, sizeof (*iprobe));
1457 	smb_info_bcopy(sip->smbst_hdr, &ip, sizeof (ip));
1458 	iprobe->smbip_description = smb_strptr(sip, ip.smbipr_descr);
1459 	iprobe->smbip_location = SMB_IPROBE_LOCATION(ip.smbipr_locstat);
1460 	iprobe->smbip_status = SMB_IPROBE_STATUS(ip.smbipr_locstat);
1461 	iprobe->smbip_maxval = ip.smbipr_maxval;
1462 	iprobe->smbip_minval = ip.smbipr_minval;
1463 	iprobe->smbip_resolution = ip.smbipr_resolution;
1464 	iprobe->smbip_tolerance	= ip.smbipr_tolerance;
1465 	iprobe->smbip_accuracy = ip.smbipr_accuracy;
1466 
1467 	if (sip->smbst_hdr->smbh_len >= SMB_IPROBE_NOMINAL_MINLEN) {
1468 		iprobe->smbip_nominal = ip.smbipr_nominal;
1469 	} else {
1470 		iprobe->smbip_nominal = SMB_PROBE_UNKNOWN_VALUE;
1471 	}
1472 
1473 	return (0);
1474 }
1475