xref: /illumos-gate/usr/src/common/smbios/smb_info.c (revision e0731422366620894c16c1ee6515551c5f00733d)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * SMBIOS Information Routines
29  *
30  * The routines in this file are used to convert from the SMBIOS data format to
31  * a more reasonable and stable set of structures offered as part of our ABI.
32  * These functions take the general form:
33  *
34  *	stp = smb_lookup_type(shp, foo);
35  *	smb_foo_t foo;
36  *
37  *	smb_info_bcopy(stp->smbst_hdr, &foo, sizeof (foo));
38  *      bzero(caller's struct);
39  *
40  *	copy/convert foo members into caller's struct
41  *
42  * We copy the internal structure on to an automatic variable so as to avoid
43  * checks everywhere for structures that the BIOS has improperly truncated, and
44  * also to automatically handle the case of a structure that has been extended.
45  * When necessary, this code can use smb_gteq() to determine whether the SMBIOS
46  * data is of a particular revision that is supposed to contain a new field.
47  */
48 
49 #include <sys/smbios_impl.h>
50 
51 #ifdef _KERNEL
52 #include <sys/sunddi.h>
53 #else
54 #include <fcntl.h>
55 #include <unistd.h>
56 #include <string.h>
57 #endif
58 
59 /*
60  * A large number of SMBIOS structures contain a set of common strings used to
61  * describe a h/w component's serial number, manufacturer, etc.  These fields
62  * helpfully have different names and offsets and sometimes aren't consistent.
63  * To simplify life for our clients, we factor these common things out into
64  * smbios_info_t, which can be retrieved for any structure.  The following
65  * table describes the mapping from a given structure to the smbios_info_t.
66  * Multiple SMBIOS stuctures' contained objects are also handled here.
67  */
68 static const struct smb_infospec {
69 	uint8_t is_type;		/* structure type */
70 	uint8_t is_manu;		/* manufacturer offset */
71 	uint8_t is_product;		/* product name offset */
72 	uint8_t is_version;		/* version offset */
73 	uint8_t is_serial;		/* serial number offset */
74 	uint8_t is_asset;		/* asset tag offset */
75 	uint8_t is_location;		/* location string offset */
76 	uint8_t is_part;		/* part number offset */
77 	uint8_t is_contc;		/* contained count */
78 	uint8_t is_contsz;		/* contained size */
79 	uint8_t is_contv;		/* contained objects */
80 } _smb_infospecs[] = {
81 	{ SMB_TYPE_SYSTEM,
82 		offsetof(smb_system_t, smbsi_manufacturer),
83 		offsetof(smb_system_t, smbsi_product),
84 		offsetof(smb_system_t, smbsi_version),
85 		offsetof(smb_system_t, smbsi_serial),
86 		0,
87 		0,
88 		0,
89 		0,
90 		0,
91 		0 },
92 	{ SMB_TYPE_BASEBOARD,
93 		offsetof(smb_bboard_t, smbbb_manufacturer),
94 		offsetof(smb_bboard_t, smbbb_product),
95 		offsetof(smb_bboard_t, smbbb_version),
96 		offsetof(smb_bboard_t, smbbb_serial),
97 		offsetof(smb_bboard_t, smbbb_asset),
98 		offsetof(smb_bboard_t, smbbb_location),
99 		0,
100 		offsetof(smb_bboard_t, smbbb_cn),
101 		SMB_CONT_WORD,
102 		offsetof(smb_bboard_t, smbbb_cv) },
103 	{ SMB_TYPE_CHASSIS,
104 		offsetof(smb_chassis_t, smbch_manufacturer),
105 		0,
106 		offsetof(smb_chassis_t, smbch_version),
107 		offsetof(smb_chassis_t, smbch_serial),
108 		offsetof(smb_chassis_t, smbch_asset),
109 		0,
110 		0,
111 		offsetof(smb_chassis_t, smbch_cn),
112 		SMB_CONT_BYTE,
113 		offsetof(smb_chassis_t, smbch_cv) },
114 	{ SMB_TYPE_PROCESSOR,
115 		offsetof(smb_processor_t, smbpr_manufacturer),
116 		0,
117 		offsetof(smb_processor_t, smbpr_version),
118 		offsetof(smb_processor_t, smbpr_serial),
119 		offsetof(smb_processor_t, smbpr_asset),
120 		offsetof(smb_processor_t, smbpr_socket),
121 		offsetof(smb_processor_t, smbpr_part),
122 		0,
123 		0,
124 		0 },
125 	{ SMB_TYPE_CACHE,
126 		0,
127 		0,
128 		0,
129 		0,
130 		0,
131 		offsetof(smb_cache_t, smbca_socket),
132 		0,
133 		0,
134 		0,
135 		0 },
136 	{ SMB_TYPE_PORT,
137 		0,
138 		0,
139 		0,
140 		0,
141 		0,
142 		offsetof(smb_port_t, smbpo_iref),
143 		0,
144 		0,
145 		0,
146 		0 },
147 	{ SMB_TYPE_SLOT,
148 		0,
149 		0,
150 		0,
151 		0,
152 		0,
153 		offsetof(smb_slot_t, smbsl_name),
154 		0,
155 		0,
156 		0,
157 		0 },
158 	{ SMB_TYPE_MEMDEVICE,
159 		offsetof(smb_memdevice_t, smbmdev_manufacturer),
160 		0,
161 		0,
162 		offsetof(smb_memdevice_t, smbmdev_serial),
163 		offsetof(smb_memdevice_t, smbmdev_asset),
164 		offsetof(smb_memdevice_t, smbmdev_dloc),
165 		offsetof(smb_memdevice_t, smbmdev_part),
166 		0,
167 		0,
168 		0 },
169 	{ SMB_TYPE_POWERSUP,
170 		offsetof(smb_powersup_t, smbpsup_manufacturer),
171 		offsetof(smb_powersup_t, smbpsup_devname),
172 		offsetof(smb_powersup_t, smbpsup_rev),
173 		offsetof(smb_powersup_t, smbpsup_serial),
174 		offsetof(smb_powersup_t, smbpsup_asset),
175 		offsetof(smb_powersup_t, smbpsup_loc),
176 		offsetof(smb_powersup_t, smbpsup_part),
177 		0,
178 		0,
179 		0 },
180 	{ SMB_TYPE_EOT }
181 };
182 
183 static const char *
184 smb_info_strptr(const smb_struct_t *stp, uint8_t off, int *n)
185 {
186 	const uint8_t *sp = (const uint8_t *)(uintptr_t)stp->smbst_hdr;
187 
188 	if (off != 0 && sp + off < stp->smbst_end) {
189 		(*n)++; /* indicate success for caller */
190 		return (smb_strptr(stp, sp[off]));
191 	}
192 
193 	return (smb_strptr(stp, 0));
194 }
195 
196 static void
197 smb_info_bcopy(const smb_header_t *hp, void *dst, size_t dstlen)
198 {
199 	if (dstlen > hp->smbh_len) {
200 		bcopy(hp, dst, hp->smbh_len);
201 		bzero((char *)dst + hp->smbh_len, dstlen - hp->smbh_len);
202 	} else
203 		bcopy(hp, dst, dstlen);
204 }
205 
206 void
207 smbios_info_smbios(smbios_hdl_t *shp, smbios_entry_t *ep)
208 {
209 	bcopy(&shp->sh_ent, ep, sizeof (smbios_entry_t));
210 }
211 
212 #ifndef _KERNEL
213 static char smbios_product_override[256];
214 static boolean_t smbios_product_checked;
215 #endif
216 
217 int
218 smbios_info_common(smbios_hdl_t *shp, id_t id, smbios_info_t *ip)
219 {
220 	const smb_struct_t *stp = smb_lookup_id(shp, id);
221 	const struct smb_infospec *isp;
222 	int n = 0;
223 
224 	if (stp == NULL)
225 		return (-1); /* errno is set for us */
226 
227 	for (isp = _smb_infospecs; isp->is_type != SMB_TYPE_EOT; isp++) {
228 		if (isp->is_type == stp->smbst_hdr->smbh_type)
229 			break;
230 	}
231 
232 	ip->smbi_manufacturer = smb_info_strptr(stp, isp->is_manu, &n);
233 	ip->smbi_product = smb_info_strptr(stp, isp->is_product, &n);
234 	ip->smbi_version = smb_info_strptr(stp, isp->is_version, &n);
235 	ip->smbi_serial = smb_info_strptr(stp, isp->is_serial, &n);
236 	ip->smbi_asset = smb_info_strptr(stp, isp->is_asset, &n);
237 	ip->smbi_location = smb_info_strptr(stp, isp->is_location, &n);
238 	ip->smbi_part = smb_info_strptr(stp, isp->is_part, &n);
239 
240 	/*
241 	 * This private file allows developers to experiment with reporting
242 	 * different platform strings from SMBIOS.  It is not a supported
243 	 * mechanism in the long term, and does not work in the kernel.
244 	 */
245 #ifndef _KERNEL
246 	if (isp->is_type == SMB_TYPE_SYSTEM) {
247 		if (!smbios_product_checked) {
248 			int fd = open("/etc/smbios_product", O_RDONLY);
249 			if (fd >= 0) {
250 				(void) read(fd, smbios_product_override,
251 				    sizeof (smbios_product_override) - 1);
252 				(void) close(fd);
253 			}
254 			smbios_product_checked = B_TRUE;
255 		}
256 
257 		if (smbios_product_override[0] != '\0')
258 			ip->smbi_product = smbios_product_override;
259 	}
260 #endif
261 
262 	/*
263 	 * If we have a port with an empty internal reference designator string
264 	 * try using the external reference designator string instead.
265 	 */
266 	if (isp->is_type == SMB_TYPE_PORT && ip->smbi_location[0] == '\0') {
267 		ip->smbi_location = smb_info_strptr(stp,
268 		    offsetof(smb_port_t, smbpo_eref), &n);
269 	}
270 
271 	return (n ? 0 : smb_set_errno(shp, ESMB_NOINFO));
272 }
273 
274 /*
275  * Returns the actual number of contained objects.
276  *
277  * idc - number of contained objects
278  * idv - returned array of contained objects
279  */
280 int
281 smbios_info_contains(smbios_hdl_t *shp, id_t id, uint_t idc, id_t *idv)
282 {
283 	const smb_struct_t *stp = smb_lookup_id(shp, id);
284 	const struct smb_infospec *isp;
285 	id_t *cp;
286 	uint_t size;
287 	uint8_t cnt;
288 	int i, n;
289 
290 	if (stp == NULL) {
291 		return (-1); /* errno is set for us */
292 	}
293 
294 	for (isp = _smb_infospecs; isp->is_type != SMB_TYPE_EOT; isp++) {
295 		if (isp->is_type == stp->smbst_hdr->smbh_type)
296 			break;
297 	}
298 	if (isp->is_type == SMB_TYPE_EOT)
299 		return (smb_set_errno(shp, ESMB_TYPE));
300 
301 	size = isp->is_contsz;
302 	cnt = *((uint8_t *)(uintptr_t)stp->smbst_hdr + isp->is_contc);
303 	cp = (id_t *)((uintptr_t)stp->smbst_hdr + isp->is_contv);
304 
305 	n = MIN(cnt, idc);
306 	for (i = 0; i < n; i++) {
307 		if (size == SMB_CONT_WORD)
308 			idv[i] = *((uint8_t *)(uintptr_t)cp + (i * 2));
309 		else if (size == SMB_CONT_BYTE)
310 			idv[i] = *((uint8_t *)(uintptr_t)cp + (i * 3));
311 		else
312 			return (smb_set_errno(shp, ESMB_INVAL));
313 	}
314 
315 	return (cnt);
316 }
317 
318 id_t
319 smbios_info_bios(smbios_hdl_t *shp, smbios_bios_t *bp)
320 {
321 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BIOS);
322 	const smb_bios_t *bip;
323 
324 	if (stp == NULL)
325 		return (-1); /* errno is set for us */
326 
327 	if (stp->smbst_hdr->smbh_len < sizeof (smb_bios_t) - sizeof (uint8_t))
328 		return (smb_set_errno(shp, ESMB_CORRUPT));
329 
330 	bip = (smb_bios_t *)(uintptr_t)stp->smbst_hdr;
331 	bzero(bp, sizeof (smbios_bios_t));
332 
333 	bp->smbb_vendor = smb_strptr(stp, bip->smbbi_vendor);
334 	bp->smbb_version = smb_strptr(stp, bip->smbbi_version);
335 	bp->smbb_segment = bip->smbbi_segment;
336 	bp->smbb_reldate = smb_strptr(stp, bip->smbbi_reldate);
337 	bp->smbb_romsize = 64 * 1024 * ((uint32_t)bip->smbbi_romsize + 1);
338 	bp->smbb_runsize = 16 * (0x10000 - (uint32_t)bip->smbbi_segment);
339 	bp->smbb_cflags = bip->smbbi_cflags;
340 
341 	/*
342 	 * If one or more extension bytes are present, reset smbb_xcflags to
343 	 * point to them.  Otherwise leave this member set to NULL.
344 	 */
345 	if (stp->smbst_hdr->smbh_len >= sizeof (smb_bios_t)) {
346 		bp->smbb_xcflags = bip->smbbi_xcflags;
347 		bp->smbb_nxcflags = stp->smbst_hdr->smbh_len -
348 		    sizeof (smb_bios_t) + 1;
349 
350 		if (bp->smbb_nxcflags > SMB_BIOSXB_ECFW_MIN &&
351 		    smb_gteq(shp, SMB_VERSION_24)) {
352 			bp->smbb_biosv.smbv_major =
353 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MAJ];
354 			bp->smbb_biosv.smbv_minor =
355 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MIN];
356 			bp->smbb_ecfwv.smbv_major =
357 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MAJ];
358 			bp->smbb_ecfwv.smbv_minor =
359 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MIN];
360 		}
361 	}
362 
363 	return (stp->smbst_hdr->smbh_hdl);
364 }
365 
366 id_t
367 smbios_info_system(smbios_hdl_t *shp, smbios_system_t *sip)
368 {
369 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM);
370 	smb_system_t si;
371 
372 	if (stp == NULL)
373 		return (-1); /* errno is set for us */
374 
375 	smb_info_bcopy(stp->smbst_hdr, &si, sizeof (si));
376 	bzero(sip, sizeof (smbios_system_t));
377 
378 	sip->smbs_uuid = ((smb_system_t *)stp->smbst_hdr)->smbsi_uuid;
379 	sip->smbs_uuidlen = sizeof (si.smbsi_uuid);
380 	sip->smbs_wakeup = si.smbsi_wakeup;
381 	sip->smbs_sku = smb_strptr(stp, si.smbsi_sku);
382 	sip->smbs_family = smb_strptr(stp, si.smbsi_family);
383 
384 	return (stp->smbst_hdr->smbh_hdl);
385 }
386 
387 int
388 smbios_info_bboard(smbios_hdl_t *shp, id_t id, smbios_bboard_t *bbp)
389 {
390 	const smb_struct_t *stp = smb_lookup_id(shp, id);
391 	smb_bboard_t bb;
392 
393 	if (stp == NULL)
394 		return (-1); /* errno is set for us */
395 
396 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_BASEBOARD)
397 		return (smb_set_errno(shp, ESMB_TYPE));
398 
399 	smb_info_bcopy(stp->smbst_hdr, &bb, sizeof (bb));
400 	bzero(bbp, sizeof (smbios_bboard_t));
401 
402 	bbp->smbb_chassis = bb.smbbb_chassis;
403 	bbp->smbb_flags = bb.smbbb_flags;
404 	bbp->smbb_type = bb.smbbb_type;
405 	bbp->smbb_contn = bb.smbbb_cn;
406 
407 	return (0);
408 }
409 
410 int
411 smbios_info_chassis(smbios_hdl_t *shp, id_t id, smbios_chassis_t *chp)
412 {
413 	const smb_struct_t *stp = smb_lookup_id(shp, id);
414 	smb_chassis_t ch;
415 
416 	if (stp == NULL)
417 		return (-1); /* errno is set for us */
418 
419 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CHASSIS)
420 		return (smb_set_errno(shp, ESMB_TYPE));
421 
422 	smb_info_bcopy(stp->smbst_hdr, &ch, sizeof (ch));
423 	bzero(chp, sizeof (smbios_chassis_t));
424 
425 	chp->smbc_oemdata = ch.smbch_oemdata;
426 	chp->smbc_lock = (ch.smbch_type & SMB_CHT_LOCK) != 0;
427 	chp->smbc_type = ch.smbch_type & ~SMB_CHT_LOCK;
428 	chp->smbc_bustate = ch.smbch_bustate;
429 	chp->smbc_psstate = ch.smbch_psstate;
430 	chp->smbc_thstate = ch.smbch_thstate;
431 	chp->smbc_security = ch.smbch_security;
432 	chp->smbc_uheight = ch.smbch_uheight;
433 	chp->smbc_cords = ch.smbch_cords;
434 	chp->smbc_elems = ch.smbch_cn;
435 	chp->smbc_elemlen = ch.smbch_cm;
436 
437 	return (0);
438 }
439 
440 int
441 smbios_info_processor(smbios_hdl_t *shp, id_t id, smbios_processor_t *pp)
442 {
443 	const smb_struct_t *stp = smb_lookup_id(shp, id);
444 	smb_processor_t p;
445 
446 	if (stp == NULL)
447 		return (-1); /* errno is set for us */
448 
449 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PROCESSOR)
450 		return (smb_set_errno(shp, ESMB_TYPE));
451 
452 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
453 	bzero(pp, sizeof (smbios_processor_t));
454 
455 	pp->smbp_cpuid = p.smbpr_cpuid;
456 	pp->smbp_type = p.smbpr_type;
457 	pp->smbp_family = p.smbpr_family;
458 	pp->smbp_voltage = p.smbpr_voltage;
459 	pp->smbp_maxspeed = p.smbpr_maxspeed;
460 	pp->smbp_curspeed = p.smbpr_curspeed;
461 	pp->smbp_status = p.smbpr_status;
462 	pp->smbp_upgrade = p.smbpr_upgrade;
463 	pp->smbp_l1cache = p.smbpr_l1cache;
464 	pp->smbp_l2cache = p.smbpr_l2cache;
465 	pp->smbp_l3cache = p.smbpr_l3cache;
466 
467 	return (0);
468 }
469 
470 int
471 smbios_info_cache(smbios_hdl_t *shp, id_t id, smbios_cache_t *cap)
472 {
473 	const smb_struct_t *stp = smb_lookup_id(shp, id);
474 	smb_cache_t c;
475 
476 	if (stp == NULL)
477 		return (-1); /* errno is set for us */
478 
479 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CACHE)
480 		return (smb_set_errno(shp, ESMB_TYPE));
481 
482 	smb_info_bcopy(stp->smbst_hdr, &c, sizeof (c));
483 	bzero(cap, sizeof (smbios_cache_t));
484 
485 	cap->smba_maxsize = SMB_CACHE_SIZE(c.smbca_maxsize);
486 	cap->smba_size = SMB_CACHE_SIZE(c.smbca_size);
487 	cap->smba_stype = c.smbca_stype;
488 	cap->smba_ctype = c.smbca_ctype;
489 	cap->smba_speed = c.smbca_speed;
490 	cap->smba_etype = c.smbca_etype;
491 	cap->smba_ltype = c.smbca_ltype;
492 	cap->smba_assoc = c.smbca_assoc;
493 	cap->smba_level = SMB_CACHE_CFG_LEVEL(c.smbca_config);
494 	cap->smba_mode = SMB_CACHE_CFG_MODE(c.smbca_config);
495 	cap->smba_location = SMB_CACHE_CFG_LOCATION(c.smbca_config);
496 
497 	if (SMB_CACHE_CFG_ENABLED(c.smbca_config))
498 		cap->smba_flags |= SMB_CAF_ENABLED;
499 
500 	if (SMB_CACHE_CFG_SOCKETED(c.smbca_config))
501 		cap->smba_flags |= SMB_CAF_SOCKETED;
502 
503 	return (0);
504 }
505 
506 int
507 smbios_info_port(smbios_hdl_t *shp, id_t id, smbios_port_t *pop)
508 {
509 	const smb_struct_t *stp = smb_lookup_id(shp, id);
510 	smb_port_t p;
511 
512 	if (stp == NULL)
513 		return (-1); /* errno is set for us */
514 
515 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PORT)
516 		return (smb_set_errno(shp, ESMB_TYPE));
517 
518 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
519 	bzero(pop, sizeof (smbios_port_t));
520 
521 	pop->smbo_iref = smb_strptr(stp, p.smbpo_iref);
522 	pop->smbo_eref = smb_strptr(stp, p.smbpo_eref);
523 
524 	pop->smbo_itype = p.smbpo_itype;
525 	pop->smbo_etype = p.smbpo_etype;
526 	pop->smbo_ptype = p.smbpo_ptype;
527 
528 	return (0);
529 }
530 
531 int
532 smbios_info_slot(smbios_hdl_t *shp, id_t id, smbios_slot_t *sp)
533 {
534 	const smb_struct_t *stp = smb_lookup_id(shp, id);
535 	smb_slot_t s;
536 
537 	if (stp == NULL)
538 		return (-1); /* errno is set for us */
539 
540 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_SLOT)
541 		return (smb_set_errno(shp, ESMB_TYPE));
542 
543 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
544 	bzero(sp, sizeof (smbios_slot_t));
545 
546 	sp->smbl_name = smb_strptr(stp, s.smbsl_name);
547 	sp->smbl_type = s.smbsl_type;
548 	sp->smbl_width = s.smbsl_width;
549 	sp->smbl_usage = s.smbsl_usage;
550 	sp->smbl_length = s.smbsl_length;
551 	sp->smbl_id = s.smbsl_id;
552 	sp->smbl_ch1 = s.smbsl_ch1;
553 	sp->smbl_ch2 = s.smbsl_ch2;
554 	sp->smbl_sg = s.smbsl_sg;
555 	sp->smbl_bus = s.smbsl_bus;
556 	sp->smbl_df = s.smbsl_df;
557 
558 	return (0);
559 }
560 
561 int
562 smbios_info_obdevs_ext(smbios_hdl_t *shp, id_t id, smbios_obdev_ext_t *oep)
563 {
564 	const smb_struct_t *stp = smb_lookup_id(shp, id);
565 	smb_obdev_ext_t obe;
566 
567 	if (stp == NULL)
568 		return (-1); /* errno is set for us */
569 
570 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVEXT)
571 		return (smb_set_errno(shp, ESMB_TYPE));
572 
573 	smb_info_bcopy(stp->smbst_hdr, &obe, sizeof (obe));
574 	bzero(oep, sizeof (smbios_obdev_ext_t));
575 
576 	oep->smboe_name = smb_strptr(stp, obe.smbobe_name);
577 	oep->smboe_dtype = obe.smbobe_dtype;
578 	oep->smboe_dti = obe.smbobe_dti;
579 	oep->smboe_sg = obe.smbobe_sg;
580 	oep->smboe_bus = obe.smbobe_bus;
581 	oep->smboe_df = obe.smbobe_df;
582 
583 	return (0);
584 }
585 
586 int
587 smbios_info_obdevs(smbios_hdl_t *shp, id_t id, int obc, smbios_obdev_t *obp)
588 {
589 	const smb_struct_t *stp = smb_lookup_id(shp, id);
590 	const smb_obdev_t *op;
591 	int i, m, n;
592 
593 	if (stp == NULL)
594 		return (-1); /* errno is set for us */
595 
596 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVS)
597 		return (smb_set_errno(shp, ESMB_TYPE));
598 
599 	op = (smb_obdev_t *)((uintptr_t)stp->smbst_hdr + sizeof (smb_header_t));
600 	m = (stp->smbst_hdr->smbh_len - sizeof (smb_header_t)) / sizeof (*op);
601 	n = MIN(m, obc);
602 
603 	for (i = 0; i < n; i++, op++, obp++) {
604 		obp->smbd_name = smb_strptr(stp, op->smbob_name);
605 		obp->smbd_type = op->smbob_type & ~SMB_OBT_ENABLED;
606 		obp->smbd_enabled = (op->smbob_type & SMB_OBT_ENABLED) != 0;
607 	}
608 
609 	return (m);
610 }
611 
612 /*
613  * The implementation structures for OEMSTR, SYSCONFSTR, and LANG all use the
614  * first byte to indicate the size of a string table at the end of the record.
615  * Therefore, smbios_info_strtab() can be used to retrieve the table size and
616  * strings for any of these underlying record types.
617  */
618 int
619 smbios_info_strtab(smbios_hdl_t *shp, id_t id, int argc, const char *argv[])
620 {
621 	const smb_struct_t *stp = smb_lookup_id(shp, id);
622 	smb_strtab_t s;
623 	int i, n;
624 
625 	if (stp == NULL)
626 		return (-1); /* errno is set for us */
627 
628 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR &&
629 	    stp->smbst_hdr->smbh_type != SMB_TYPE_SYSCONFSTR &&
630 	    stp->smbst_hdr->smbh_type != SMB_TYPE_LANG)
631 		return (smb_set_errno(shp, ESMB_TYPE));
632 
633 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
634 	n = MIN(s.smbtb_count, argc);
635 
636 	for (i = 0; i < n; i++)
637 		argv[i] = smb_strptr(stp, i + 1);
638 
639 	return (s.smbtb_count);
640 }
641 
642 id_t
643 smbios_info_lang(smbios_hdl_t *shp, smbios_lang_t *lp)
644 {
645 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_LANG);
646 	smb_lang_t l;
647 
648 	if (stp == NULL)
649 		return (-1); /* errno is set for us */
650 
651 	smb_info_bcopy(stp->smbst_hdr, &l, sizeof (l));
652 	bzero(lp, sizeof (smbios_lang_t));
653 
654 	lp->smbla_cur = smb_strptr(stp, l.smblang_cur);
655 	lp->smbla_fmt = l.smblang_flags & 1;
656 	lp->smbla_num = l.smblang_num;
657 
658 	return (stp->smbst_hdr->smbh_hdl);
659 }
660 
661 id_t
662 smbios_info_eventlog(smbios_hdl_t *shp, smbios_evlog_t *evp)
663 {
664 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_EVENTLOG);
665 	const smb_sel_t *sel;
666 	size_t len;
667 
668 	if (stp == NULL)
669 		return (-1); /* errno is set for us */
670 
671 	if (stp->smbst_hdr->smbh_len < sizeof (smb_sel_t) - sizeof (uint8_t))
672 		return (smb_set_errno(shp, ESMB_CORRUPT));
673 
674 	sel = (smb_sel_t *)(uintptr_t)stp->smbst_hdr;
675 	len = stp->smbst_hdr->smbh_len - sizeof (smb_sel_t) + sizeof (uint8_t);
676 	bzero(evp, sizeof (smbios_evlog_t));
677 
678 	if (len < sel->smbsel_typec * sel->smbsel_typesz)
679 		return (smb_set_errno(shp, ESMB_CORRUPT));
680 
681 	evp->smbev_size = sel->smbsel_len;
682 	evp->smbev_hdr = sel->smbsel_hdroff;
683 	evp->smbev_data = sel->smbsel_dataoff;
684 	evp->smbev_method = sel->smbsel_method;
685 	evp->smbev_flags = sel->smbsel_status;
686 	evp->smbev_format = sel->smbsel_format;
687 	evp->smbev_token = sel->smbsel_token;
688 	evp->smbev_addr.eva_addr = sel->smbsel_addr;
689 
690 	if (sel->smbsel_typesz == sizeof (smbios_evtype_t)) {
691 		evp->smbev_typec = sel->smbsel_typec;
692 		evp->smbev_typev = (void *)(uintptr_t)sel->smbsel_typev;
693 	}
694 
695 	return (stp->smbst_hdr->smbh_hdl);
696 }
697 
698 int
699 smbios_info_memarray(smbios_hdl_t *shp, id_t id, smbios_memarray_t *map)
700 {
701 	const smb_struct_t *stp = smb_lookup_id(shp, id);
702 	smb_memarray_t m;
703 
704 	if (stp == NULL)
705 		return (-1); /* errno is set for us */
706 
707 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAY)
708 		return (smb_set_errno(shp, ESMB_TYPE));
709 
710 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
711 	bzero(map, sizeof (smbios_memarray_t));
712 
713 	map->smbma_location = m.smbmarr_loc;
714 	map->smbma_use = m.smbmarr_use;
715 	map->smbma_ecc = m.smbmarr_ecc;
716 	map->smbma_ndevs = m.smbmarr_ndevs;
717 	map->smbma_err = m.smbmarr_err;
718 
719 	if (m.smbmarr_cap != 0x80000000)
720 		map->smbma_size = (uint64_t)m.smbmarr_cap * 1024;
721 	else
722 		map->smbma_size = 0; /* unknown */
723 
724 	return (0);
725 }
726 
727 int
728 smbios_info_memarrmap(smbios_hdl_t *shp, id_t id, smbios_memarrmap_t *map)
729 {
730 	const smb_struct_t *stp = smb_lookup_id(shp, id);
731 	smb_memarrmap_t m;
732 
733 	if (stp == NULL)
734 		return (-1); /* errno is set for us */
735 
736 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAYMAP)
737 		return (smb_set_errno(shp, ESMB_TYPE));
738 
739 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
740 	bzero(map, sizeof (smbios_memarrmap_t));
741 
742 	map->smbmam_array = m.smbamap_array;
743 	map->smbmam_width = m.smbamap_width;
744 	map->smbmam_addr = (uint64_t)m.smbamap_start * 1024;
745 	map->smbmam_size = (uint64_t)
746 	    (m.smbamap_end - m.smbamap_start + 1) * 1024;
747 
748 	return (0);
749 }
750 
751 int
752 smbios_info_memdevice(smbios_hdl_t *shp, id_t id, smbios_memdevice_t *mdp)
753 {
754 	const smb_struct_t *stp = smb_lookup_id(shp, id);
755 	smb_memdevice_t m;
756 
757 	if (stp == NULL)
758 		return (-1); /* errno is set for us */
759 
760 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICE)
761 		return (smb_set_errno(shp, ESMB_TYPE));
762 
763 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
764 	bzero(mdp, sizeof (smbios_memdevice_t));
765 
766 	mdp->smbmd_array = m.smbmdev_array;
767 	mdp->smbmd_error = m.smbmdev_error;
768 	mdp->smbmd_twidth = m.smbmdev_twidth == 0xFFFF ? -1U : m.smbmdev_twidth;
769 	mdp->smbmd_dwidth = m.smbmdev_dwidth == 0xFFFF ? -1U : m.smbmdev_dwidth;
770 
771 	if (mdp->smbmd_size != 0xFFFF) {
772 		mdp->smbmd_size = (uint64_t)(m.smbmdev_size & ~SMB_MDS_KBYTES);
773 		if (m.smbmdev_size & SMB_MDS_KBYTES)
774 			mdp->smbmd_size *= 1024;
775 		else
776 			mdp->smbmd_size *= 1024 * 1024;
777 	} else
778 		mdp->smbmd_size = -1ULL; /* size unknown */
779 
780 	mdp->smbmd_form = m.smbmdev_form;
781 	mdp->smbmd_set = m.smbmdev_set;
782 	mdp->smbmd_type = m.smbmdev_type;
783 	mdp->smbmd_flags = m.smbmdev_flags;
784 	mdp->smbmd_dloc = smb_strptr(stp, m.smbmdev_dloc);
785 	mdp->smbmd_bloc = smb_strptr(stp, m.smbmdev_bloc);
786 
787 	if (m.smbmdev_speed != 0)
788 		mdp->smbmd_speed = 1000 / m.smbmdev_speed; /* MHz -> nsec */
789 
790 	return (0);
791 }
792 
793 int
794 smbios_info_memdevmap(smbios_hdl_t *shp, id_t id, smbios_memdevmap_t *mdp)
795 {
796 	const smb_struct_t *stp = smb_lookup_id(shp, id);
797 	smb_memdevmap_t m;
798 
799 	if (stp == NULL)
800 		return (-1); /* errno is set for us */
801 
802 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICEMAP)
803 		return (smb_set_errno(shp, ESMB_TYPE));
804 
805 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
806 	bzero(mdp, sizeof (smbios_memdevmap_t));
807 
808 	mdp->smbmdm_device = m.smbdmap_device;
809 	mdp->smbmdm_arrmap = m.smbdmap_array;
810 	mdp->smbmdm_addr = (uint64_t)m.smbdmap_start * 1024;
811 	mdp->smbmdm_size = (uint64_t)
812 	    (m.smbdmap_end - m.smbdmap_start + 1) * 1024;
813 	mdp->smbmdm_rpos = m.smbdmap_rpos;
814 	mdp->smbmdm_ipos = m.smbdmap_ipos;
815 	mdp->smbmdm_idepth = m.smbdmap_idepth;
816 
817 	return (0);
818 }
819 
820 id_t
821 smbios_info_hwsec(smbios_hdl_t *shp, smbios_hwsec_t *hsp)
822 {
823 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SECURITY);
824 	smb_hwsec_t hs;
825 
826 	if (stp == NULL)
827 		return (-1); /* errno is set for us */
828 
829 	smb_info_bcopy(stp->smbst_hdr, &hs, sizeof (hs));
830 	bzero(hsp, sizeof (smbios_hwsec_t));
831 
832 	hsp->smbh_pwr_ps = SMB_HWS_PWR_PS(hs.smbhs_settings);
833 	hsp->smbh_kbd_ps = SMB_HWS_KBD_PS(hs.smbhs_settings);
834 	hsp->smbh_adm_ps = SMB_HWS_ADM_PS(hs.smbhs_settings);
835 	hsp->smbh_pan_ps = SMB_HWS_PAN_PS(hs.smbhs_settings);
836 
837 	return (stp->smbst_hdr->smbh_hdl);
838 }
839 
840 id_t
841 smbios_info_boot(smbios_hdl_t *shp, smbios_boot_t *bp)
842 {
843 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BOOT);
844 	const smb_boot_t *b = (smb_boot_t *)(uintptr_t)stp->smbst_hdr;
845 
846 	if (stp == NULL)
847 		return (-1); /* errno is set for us */
848 
849 	bzero(bp, sizeof (smbios_boot_t));
850 
851 	bp->smbt_status = b->smbbo_status[0];
852 	bp->smbt_size = stp->smbst_hdr->smbh_len - sizeof (smb_boot_t);
853 	bp->smbt_data = bp->smbt_size ? &b->smbbo_status[1] : NULL;
854 
855 	return (stp->smbst_hdr->smbh_hdl);
856 }
857 
858 id_t
859 smbios_info_ipmi(smbios_hdl_t *shp, smbios_ipmi_t *ip)
860 {
861 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_IPMIDEV);
862 	smb_ipmi_t i;
863 
864 	if (stp == NULL)
865 		return (-1); /* errno is set for us */
866 
867 	smb_info_bcopy(stp->smbst_hdr, &i, sizeof (i));
868 	bzero(ip, sizeof (smbios_ipmi_t));
869 
870 	ip->smbip_type = i.smbipm_type;
871 	ip->smbip_vers.smbv_major = SMB_IPM_SPEC_MAJOR(i.smbipm_spec);
872 	ip->smbip_vers.smbv_minor = SMB_IPM_SPEC_MINOR(i.smbipm_spec);
873 	ip->smbip_i2c = i.smbipm_i2c;
874 	ip->smbip_addr = i.smbipm_addr & ~SMB_IPM_ADDR_IO;
875 	ip->smbip_intr = i.smbipm_intr;
876 
877 	if (i.smbipm_bus != (uint8_t)-1)
878 		ip->smbip_bus = i.smbipm_bus;
879 	else
880 		ip->smbip_bus = -1u;
881 
882 	if (SMB_IPM_INFO_LSB(i.smbipm_info))
883 		ip->smbip_addr |= 1; /* turn on least-significant bit of addr */
884 
885 	if (i.smbipm_addr & SMB_IPM_ADDR_IO) {
886 		switch (SMB_IPM_INFO_REGS(i.smbipm_info)) {
887 		case SMB_IPM_REGS_1B:
888 			ip->smbip_regspacing = 1;
889 			break;
890 		case SMB_IPM_REGS_4B:
891 			ip->smbip_regspacing = 4;
892 			break;
893 		case SMB_IPM_REGS_16B:
894 			ip->smbip_regspacing = 16;
895 			break;
896 		default:
897 			ip->smbip_regspacing = 1;
898 		}
899 		ip->smbip_flags |= SMB_IPMI_F_IOADDR;
900 	}
901 
902 	if (SMB_IPM_INFO_ISPEC(i.smbipm_info))
903 		ip->smbip_flags |= SMB_IPMI_F_INTRSPEC;
904 
905 	if (SMB_IPM_INFO_IPOL(i.smbipm_info) == SMB_IPM_IPOL_HI)
906 		ip->smbip_flags |= SMB_IPMI_F_INTRHIGH;
907 
908 	if (SMB_IPM_INFO_IMODE(i.smbipm_info) == SMB_IPM_IMODE_EDGE)
909 		ip->smbip_flags |= SMB_IPMI_F_INTREDGE;
910 
911 	return (stp->smbst_hdr->smbh_hdl);
912 }
913 
914 static boolean_t
915 smbios_has_oemstr(smbios_hdl_t *shp, const char *oemstr)
916 {
917 	const smb_struct_t *stp = shp->sh_structs;
918 	smb_strtab_t s;
919 	int i, j;
920 
921 	for (i = 0; i < shp->sh_nstructs; i++, stp++) {
922 		if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR)
923 			continue;
924 
925 		smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
926 		for (j = 0; j < s.smbtb_count; j++)
927 			if (strcmp(smb_strptr(stp, j + 1), oemstr) == 0)
928 				return (B_TRUE);
929 	}
930 
931 	return (B_FALSE);
932 }
933 
934 static const char *
935 smb_serial_valid(const char *serial)
936 {
937 	char buf[MAXNAMELEN];
938 	int i = 0;
939 
940 	if (serial == NULL)
941 		return (NULL);
942 
943 	(void) strlcpy(buf, serial, sizeof (buf));
944 
945 	while (buf[i] != '\0' && buf[i] == ' ')
946 		i++;
947 
948 	if (buf[i] == '\0' || strstr(buf, SMB_DEFAULT1) != NULL ||
949 	    strstr(buf, SMB_DEFAULT2) != NULL)
950 		return (NULL);
951 
952 	return (serial);
953 }
954 
955 /*
956  * Get chassis SN or product SN
957  */
958 static int
959 smb_get_sn(smbios_hdl_t *shp, const char **psnp, const char **csnp)
960 {
961 	const smb_struct_t *stp;
962 	smbios_info_t s1, s3;
963 
964 	if (psnp == NULL || csnp == NULL)
965 		return (smb_set_errno(shp, ESMB_INVAL));
966 
967 	*psnp = *csnp = NULL;
968 
969 	/*
970 	 * If SMBIOS meets Sun's PRMS requirements, retrieve product SN
971 	 * from type 1 structure, and chassis SN from type 3 structure.
972 	 * Otherwise return SN in type 1 structure as chassis SN.
973 	 */
974 
975 	/* Get type 1 SN */
976 	if ((stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM)) == NULL ||
977 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s1) == SMB_ERR)
978 		s1.smbi_serial = NULL;
979 
980 	/* Get type 3 SN */
981 	if ((stp = smb_lookup_type(shp, SMB_TYPE_CHASSIS)) == NULL ||
982 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s3) == SMB_ERR)
983 		s3.smbi_serial = NULL;
984 
985 	if (smbios_has_oemstr(shp, SMB_PRMS1)) {
986 		*psnp = smb_serial_valid(s1.smbi_serial);
987 		*csnp = smb_serial_valid(s3.smbi_serial);
988 	} else {
989 		*csnp = smb_serial_valid(s1.smbi_serial);
990 	}
991 
992 	return (0);
993 }
994 
995 const char *
996 smbios_psn(smbios_hdl_t *shp)
997 {
998 	const char *psn, *csn;
999 
1000 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : psn);
1001 }
1002 
1003 const char *
1004 smbios_csn(smbios_hdl_t *shp)
1005 {
1006 	const char *psn, *csn;
1007 
1008 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : csn);
1009 }
1010 
1011 int
1012 smbios_info_extprocessor(smbios_hdl_t *shp, id_t id,
1013     smbios_processor_ext_t *epp)
1014 {
1015 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1016 	smb_processor_ext_t *exp;
1017 
1018 	if (stp == NULL)
1019 		return (-1); /* errno is set for us */
1020 
1021 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_PROCESSOR)
1022 		return (smb_set_errno(shp, ESMB_TYPE));
1023 
1024 	exp = (smb_processor_ext_t *)(uintptr_t)stp->smbst_hdr;
1025 	bzero(epp, sizeof (smbios_processor_ext_t));
1026 
1027 	epp->smbpe_processor = exp->smbpre_processor;
1028 	epp->smbpe_fru = exp->smbpre_fru;
1029 	epp->smbpe_n = exp->smbpre_n;
1030 	epp->smbpe_apicid = exp->smbpre_apicid;
1031 
1032 	return (0);
1033 }
1034 
1035 int
1036 smbios_info_extport(smbios_hdl_t *shp, id_t id, smbios_port_ext_t *eportp)
1037 {
1038 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1039 	smb_port_ext_t *ep;
1040 
1041 	if (stp == NULL)
1042 		return (-1); /* errno is set for us */
1043 
1044 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_PORT)
1045 		return (smb_set_errno(shp, ESMB_TYPE));
1046 
1047 	ep = (smb_port_ext_t *)(uintptr_t)stp->smbst_hdr;
1048 	bzero(eportp, sizeof (smbios_port_ext_t));
1049 
1050 	eportp->smbporte_chassis = ep->smbpoe_chassis;
1051 	eportp->smbporte_port = ep->smbpoe_port;
1052 	eportp->smbporte_dtype = ep->smbpoe_dtype;
1053 	eportp->smbporte_devhdl = ep->smbpoe_devhdl;
1054 	eportp->smbporte_phy = ep->smbpoe_phy;
1055 
1056 	return (0);
1057 }
1058 
1059 int
1060 smbios_info_pciexrc(smbios_hdl_t *shp, id_t id,
1061     smbios_pciexrc_t *rcp)
1062 {
1063 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1064 	smb_pciexrc_t rc;
1065 
1066 	if (stp == NULL)
1067 		return (-1); /* errno is set for us */
1068 
1069 	if (stp->smbst_hdr->smbh_type != SUN_OEM_PCIEXRC)
1070 		return (smb_set_errno(shp, ESMB_TYPE));
1071 
1072 	smb_info_bcopy(stp->smbst_hdr, &rc, sizeof (rc));
1073 	bzero(rcp, sizeof (smbios_pciexrc_t));
1074 
1075 	rcp->smbpcie_bb = rc.smbpciexrc_bboard;
1076 	rcp->smbpcie_bdf = rc.smbpciexrc_bdf;
1077 
1078 	return (0);
1079 }
1080 
1081 int
1082 smbios_info_extmemarray(smbios_hdl_t *shp, id_t id, smbios_memarray_ext_t *emap)
1083 {
1084 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1085 	smb_memarray_ext_t exma;
1086 
1087 	if (stp == NULL)
1088 		return (-1); /* errno is set for us */
1089 
1090 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMARRAY)
1091 		return (smb_set_errno(shp, ESMB_TYPE));
1092 
1093 	smb_info_bcopy(stp->smbst_hdr, &exma, sizeof (exma));
1094 	bzero(emap, sizeof (smbios_memarray_ext_t));
1095 
1096 	emap->smbmae_ma = exma.smbmarre_ma;
1097 	emap->smbmae_comp = exma.smbmarre_component;
1098 	emap->smbmae_bdf = exma.smbmarre_bdf;
1099 
1100 	return (0);
1101 }
1102 
1103 int
1104 smbios_info_extmemdevice(smbios_hdl_t *shp, id_t id,
1105     smbios_memdevice_ext_t *emdp)
1106 {
1107 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1108 	smb_memdevice_ext_t exmd;
1109 
1110 	if (stp == NULL)
1111 		return (-1); /* errno is set for us */
1112 
1113 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMDEVICE)
1114 		return (smb_set_errno(shp, ESMB_TYPE));
1115 
1116 	smb_info_bcopy(stp->smbst_hdr, &exmd, sizeof (exmd));
1117 	bzero(emdp, sizeof (smbios_memdevice_ext_t));
1118 
1119 	emdp->smbmdeve_md = exmd.smbmdeve_mdev;
1120 	emdp->smbmdeve_drch = exmd.smbmdeve_dchan;
1121 	emdp->smbmdeve_ncs  = exmd.smbmdeve_ncs;
1122 	emdp->smbmdeve_cs = exmd.smbmdeve_cs;
1123 
1124 	return (0);
1125 }
1126