xref: /illumos-gate/usr/src/common/smbios/smb_info.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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 2009 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 
555 	return (0);
556 }
557 
558 int
559 smbios_info_obdevs(smbios_hdl_t *shp, id_t id, int obc, smbios_obdev_t *obp)
560 {
561 	const smb_struct_t *stp = smb_lookup_id(shp, id);
562 	const smb_obdev_t *op;
563 	int i, m, n;
564 
565 	if (stp == NULL)
566 		return (-1); /* errno is set for us */
567 
568 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVS)
569 		return (smb_set_errno(shp, ESMB_TYPE));
570 
571 	op = (smb_obdev_t *)((uintptr_t)stp->smbst_hdr + sizeof (smb_header_t));
572 	m = (stp->smbst_hdr->smbh_len - sizeof (smb_header_t)) / sizeof (*op);
573 	n = MIN(m, obc);
574 
575 	for (i = 0; i < n; i++, op++, obp++) {
576 		obp->smbd_name = smb_strptr(stp, op->smbob_name);
577 		obp->smbd_type = op->smbob_type & ~SMB_OBT_ENABLED;
578 		obp->smbd_enabled = (op->smbob_type & SMB_OBT_ENABLED) != 0;
579 	}
580 
581 	return (m);
582 }
583 
584 /*
585  * The implementation structures for OEMSTR, SYSCONFSTR, and LANG all use the
586  * first byte to indicate the size of a string table at the end of the record.
587  * Therefore, smbios_info_strtab() can be used to retrieve the table size and
588  * strings for any of these underlying record types.
589  */
590 int
591 smbios_info_strtab(smbios_hdl_t *shp, id_t id, int argc, const char *argv[])
592 {
593 	const smb_struct_t *stp = smb_lookup_id(shp, id);
594 	smb_strtab_t s;
595 	int i, n;
596 
597 	if (stp == NULL)
598 		return (-1); /* errno is set for us */
599 
600 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR &&
601 	    stp->smbst_hdr->smbh_type != SMB_TYPE_SYSCONFSTR &&
602 	    stp->smbst_hdr->smbh_type != SMB_TYPE_LANG)
603 		return (smb_set_errno(shp, ESMB_TYPE));
604 
605 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
606 	n = MIN(s.smbtb_count, argc);
607 
608 	for (i = 0; i < n; i++)
609 		argv[i] = smb_strptr(stp, i + 1);
610 
611 	return (s.smbtb_count);
612 }
613 
614 id_t
615 smbios_info_lang(smbios_hdl_t *shp, smbios_lang_t *lp)
616 {
617 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_LANG);
618 	smb_lang_t l;
619 
620 	if (stp == NULL)
621 		return (-1); /* errno is set for us */
622 
623 	smb_info_bcopy(stp->smbst_hdr, &l, sizeof (l));
624 	bzero(lp, sizeof (smbios_lang_t));
625 
626 	lp->smbla_cur = smb_strptr(stp, l.smblang_cur);
627 	lp->smbla_fmt = l.smblang_flags & 1;
628 	lp->smbla_num = l.smblang_num;
629 
630 	return (stp->smbst_hdr->smbh_hdl);
631 }
632 
633 id_t
634 smbios_info_eventlog(smbios_hdl_t *shp, smbios_evlog_t *evp)
635 {
636 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_EVENTLOG);
637 	const smb_sel_t *sel;
638 	size_t len;
639 
640 	if (stp == NULL)
641 		return (-1); /* errno is set for us */
642 
643 	if (stp->smbst_hdr->smbh_len < sizeof (smb_sel_t) - sizeof (uint8_t))
644 		return (smb_set_errno(shp, ESMB_CORRUPT));
645 
646 	sel = (smb_sel_t *)(uintptr_t)stp->smbst_hdr;
647 	len = stp->smbst_hdr->smbh_len - sizeof (smb_sel_t) + sizeof (uint8_t);
648 	bzero(evp, sizeof (smbios_evlog_t));
649 
650 	if (len < sel->smbsel_typec * sel->smbsel_typesz)
651 		return (smb_set_errno(shp, ESMB_CORRUPT));
652 
653 	evp->smbev_size = sel->smbsel_len;
654 	evp->smbev_hdr = sel->smbsel_hdroff;
655 	evp->smbev_data = sel->smbsel_dataoff;
656 	evp->smbev_method = sel->smbsel_method;
657 	evp->smbev_flags = sel->smbsel_status;
658 	evp->smbev_format = sel->smbsel_format;
659 	evp->smbev_token = sel->smbsel_token;
660 	evp->smbev_addr.eva_addr = sel->smbsel_addr;
661 
662 	if (sel->smbsel_typesz == sizeof (smbios_evtype_t)) {
663 		evp->smbev_typec = sel->smbsel_typec;
664 		evp->smbev_typev = (void *)(uintptr_t)sel->smbsel_typev;
665 	}
666 
667 	return (stp->smbst_hdr->smbh_hdl);
668 }
669 
670 int
671 smbios_info_memarray(smbios_hdl_t *shp, id_t id, smbios_memarray_t *map)
672 {
673 	const smb_struct_t *stp = smb_lookup_id(shp, id);
674 	smb_memarray_t m;
675 
676 	if (stp == NULL)
677 		return (-1); /* errno is set for us */
678 
679 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAY)
680 		return (smb_set_errno(shp, ESMB_TYPE));
681 
682 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
683 	bzero(map, sizeof (smbios_memarray_t));
684 
685 	map->smbma_location = m.smbmarr_loc;
686 	map->smbma_use = m.smbmarr_use;
687 	map->smbma_ecc = m.smbmarr_ecc;
688 	map->smbma_ndevs = m.smbmarr_ndevs;
689 	map->smbma_err = m.smbmarr_err;
690 
691 	if (m.smbmarr_cap != 0x80000000)
692 		map->smbma_size = (uint64_t)m.smbmarr_cap * 1024;
693 	else
694 		map->smbma_size = 0; /* unknown */
695 
696 	return (0);
697 }
698 
699 int
700 smbios_info_memarrmap(smbios_hdl_t *shp, id_t id, smbios_memarrmap_t *map)
701 {
702 	const smb_struct_t *stp = smb_lookup_id(shp, id);
703 	smb_memarrmap_t m;
704 
705 	if (stp == NULL)
706 		return (-1); /* errno is set for us */
707 
708 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAYMAP)
709 		return (smb_set_errno(shp, ESMB_TYPE));
710 
711 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
712 	bzero(map, sizeof (smbios_memarrmap_t));
713 
714 	map->smbmam_array = m.smbamap_array;
715 	map->smbmam_width = m.smbamap_width;
716 	map->smbmam_addr = (uint64_t)m.smbamap_start * 1024;
717 	map->smbmam_size = (uint64_t)
718 	    (m.smbamap_end - m.smbamap_start + 1) * 1024;
719 
720 	return (0);
721 }
722 
723 int
724 smbios_info_memdevice(smbios_hdl_t *shp, id_t id, smbios_memdevice_t *mdp)
725 {
726 	const smb_struct_t *stp = smb_lookup_id(shp, id);
727 	smb_memdevice_t m;
728 
729 	if (stp == NULL)
730 		return (-1); /* errno is set for us */
731 
732 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICE)
733 		return (smb_set_errno(shp, ESMB_TYPE));
734 
735 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
736 	bzero(mdp, sizeof (smbios_memdevice_t));
737 
738 	mdp->smbmd_array = m.smbmdev_array;
739 	mdp->smbmd_error = m.smbmdev_error;
740 	mdp->smbmd_twidth = m.smbmdev_twidth == 0xFFFF ? -1U : m.smbmdev_twidth;
741 	mdp->smbmd_dwidth = m.smbmdev_dwidth == 0xFFFF ? -1U : m.smbmdev_dwidth;
742 
743 	if (mdp->smbmd_size != 0xFFFF) {
744 		mdp->smbmd_size = (uint64_t)(m.smbmdev_size & ~SMB_MDS_KBYTES);
745 		if (m.smbmdev_size & SMB_MDS_KBYTES)
746 			mdp->smbmd_size *= 1024;
747 		else
748 			mdp->smbmd_size *= 1024 * 1024;
749 	} else
750 		mdp->smbmd_size = -1ULL; /* size unknown */
751 
752 	mdp->smbmd_form = m.smbmdev_form;
753 	mdp->smbmd_set = m.smbmdev_set;
754 	mdp->smbmd_type = m.smbmdev_type;
755 	mdp->smbmd_flags = m.smbmdev_flags;
756 	mdp->smbmd_dloc = smb_strptr(stp, m.smbmdev_dloc);
757 	mdp->smbmd_bloc = smb_strptr(stp, m.smbmdev_bloc);
758 
759 	if (m.smbmdev_speed != 0)
760 		mdp->smbmd_speed = 1000 / m.smbmdev_speed; /* MHz -> nsec */
761 
762 	return (0);
763 }
764 
765 int
766 smbios_info_memdevmap(smbios_hdl_t *shp, id_t id, smbios_memdevmap_t *mdp)
767 {
768 	const smb_struct_t *stp = smb_lookup_id(shp, id);
769 	smb_memdevmap_t m;
770 
771 	if (stp == NULL)
772 		return (-1); /* errno is set for us */
773 
774 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICEMAP)
775 		return (smb_set_errno(shp, ESMB_TYPE));
776 
777 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
778 	bzero(mdp, sizeof (smbios_memdevmap_t));
779 
780 	mdp->smbmdm_device = m.smbdmap_device;
781 	mdp->smbmdm_arrmap = m.smbdmap_array;
782 	mdp->smbmdm_addr = (uint64_t)m.smbdmap_start * 1024;
783 	mdp->smbmdm_size = (uint64_t)
784 	    (m.smbdmap_end - m.smbdmap_start + 1) * 1024;
785 	mdp->smbmdm_rpos = m.smbdmap_rpos;
786 	mdp->smbmdm_ipos = m.smbdmap_ipos;
787 	mdp->smbmdm_idepth = m.smbdmap_idepth;
788 
789 	return (0);
790 }
791 
792 id_t
793 smbios_info_hwsec(smbios_hdl_t *shp, smbios_hwsec_t *hsp)
794 {
795 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SECURITY);
796 	smb_hwsec_t hs;
797 
798 	if (stp == NULL)
799 		return (-1); /* errno is set for us */
800 
801 	smb_info_bcopy(stp->smbst_hdr, &hs, sizeof (hs));
802 	bzero(hsp, sizeof (smbios_hwsec_t));
803 
804 	hsp->smbh_pwr_ps = SMB_HWS_PWR_PS(hs.smbhs_settings);
805 	hsp->smbh_kbd_ps = SMB_HWS_KBD_PS(hs.smbhs_settings);
806 	hsp->smbh_adm_ps = SMB_HWS_ADM_PS(hs.smbhs_settings);
807 	hsp->smbh_pan_ps = SMB_HWS_PAN_PS(hs.smbhs_settings);
808 
809 	return (stp->smbst_hdr->smbh_hdl);
810 }
811 
812 id_t
813 smbios_info_boot(smbios_hdl_t *shp, smbios_boot_t *bp)
814 {
815 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BOOT);
816 	const smb_boot_t *b = (smb_boot_t *)(uintptr_t)stp->smbst_hdr;
817 
818 	if (stp == NULL)
819 		return (-1); /* errno is set for us */
820 
821 	bzero(bp, sizeof (smbios_boot_t));
822 
823 	bp->smbt_status = b->smbbo_status[0];
824 	bp->smbt_size = stp->smbst_hdr->smbh_len - sizeof (smb_boot_t);
825 	bp->smbt_data = bp->smbt_size ? &b->smbbo_status[1] : NULL;
826 
827 	return (stp->smbst_hdr->smbh_hdl);
828 }
829 
830 id_t
831 smbios_info_ipmi(smbios_hdl_t *shp, smbios_ipmi_t *ip)
832 {
833 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_IPMIDEV);
834 	smb_ipmi_t i;
835 
836 	if (stp == NULL)
837 		return (-1); /* errno is set for us */
838 
839 	smb_info_bcopy(stp->smbst_hdr, &i, sizeof (i));
840 	bzero(ip, sizeof (smbios_ipmi_t));
841 
842 	ip->smbip_type = i.smbipm_type;
843 	ip->smbip_vers.smbv_major = SMB_IPM_SPEC_MAJOR(i.smbipm_spec);
844 	ip->smbip_vers.smbv_minor = SMB_IPM_SPEC_MINOR(i.smbipm_spec);
845 	ip->smbip_i2c = i.smbipm_i2c;
846 	ip->smbip_addr = i.smbipm_addr & ~SMB_IPM_ADDR_IO;
847 	ip->smbip_intr = i.smbipm_intr;
848 
849 	if (i.smbipm_bus != (uint8_t)-1)
850 		ip->smbip_bus = i.smbipm_bus;
851 	else
852 		ip->smbip_bus = -1u;
853 
854 	if (SMB_IPM_INFO_LSB(i.smbipm_info))
855 		ip->smbip_addr |= 1; /* turn on least-significant bit of addr */
856 
857 	if (i.smbipm_addr & SMB_IPM_ADDR_IO) {
858 		switch (SMB_IPM_INFO_REGS(i.smbipm_info)) {
859 		case SMB_IPM_REGS_1B:
860 			ip->smbip_regspacing = 1;
861 			break;
862 		case SMB_IPM_REGS_4B:
863 			ip->smbip_regspacing = 4;
864 			break;
865 		case SMB_IPM_REGS_16B:
866 			ip->smbip_regspacing = 16;
867 			break;
868 		default:
869 			ip->smbip_regspacing = 1;
870 		}
871 		ip->smbip_flags |= SMB_IPMI_F_IOADDR;
872 	}
873 
874 	if (SMB_IPM_INFO_ISPEC(i.smbipm_info))
875 		ip->smbip_flags |= SMB_IPMI_F_INTRSPEC;
876 
877 	if (SMB_IPM_INFO_IPOL(i.smbipm_info) == SMB_IPM_IPOL_HI)
878 		ip->smbip_flags |= SMB_IPMI_F_INTRHIGH;
879 
880 	if (SMB_IPM_INFO_IMODE(i.smbipm_info) == SMB_IPM_IMODE_EDGE)
881 		ip->smbip_flags |= SMB_IPMI_F_INTREDGE;
882 
883 	return (stp->smbst_hdr->smbh_hdl);
884 }
885 
886 static boolean_t
887 smbios_has_oemstr(smbios_hdl_t *shp, const char *oemstr)
888 {
889 	const smb_struct_t *stp = shp->sh_structs;
890 	smb_strtab_t s;
891 	int i, j;
892 
893 	for (i = 0; i < shp->sh_nstructs; i++, stp++) {
894 		if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR)
895 			continue;
896 
897 		smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
898 		for (j = 0; j < s.smbtb_count; j++)
899 			if (strcmp(smb_strptr(stp, j + 1), oemstr) == 0)
900 				return (B_TRUE);
901 	}
902 
903 	return (B_FALSE);
904 }
905 
906 static const char *
907 smb_serial_valid(const char *serial)
908 {
909 	char buf[MAXNAMELEN];
910 	int i = 0;
911 
912 	if (serial == NULL)
913 		return (NULL);
914 
915 	(void) strlcpy(buf, serial, sizeof (buf));
916 
917 	while (buf[i] != '\0' && buf[i] == ' ')
918 		i++;
919 
920 	if (buf[i] == '\0' || strstr(buf, SMB_DEFAULT1) != NULL ||
921 	    strstr(buf, SMB_DEFAULT2) != NULL)
922 		return (NULL);
923 
924 	return (serial);
925 }
926 
927 /*
928  * Get chassis SN or product SN
929  */
930 static int
931 smb_get_sn(smbios_hdl_t *shp, const char **psnp, const char **csnp)
932 {
933 	const smb_struct_t *stp;
934 	smbios_info_t s1, s3;
935 
936 	if (psnp == NULL || csnp == NULL)
937 		return (smb_set_errno(shp, ESMB_INVAL));
938 
939 	*psnp = *csnp = NULL;
940 
941 	/*
942 	 * If SMBIOS meets Sun's PRMS requirements, retrieve product SN
943 	 * from type 1 structure, and chassis SN from type 3 structure.
944 	 * Otherwise return SN in type 1 structure as chassis SN.
945 	 */
946 
947 	/* Get type 1 SN */
948 	if ((stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM)) == NULL ||
949 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s1) == SMB_ERR)
950 		s1.smbi_serial = NULL;
951 
952 	/* Get type 3 SN */
953 	if ((stp = smb_lookup_type(shp, SMB_TYPE_CHASSIS)) == NULL ||
954 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s3) == SMB_ERR)
955 		s3.smbi_serial = NULL;
956 
957 	if (smbios_has_oemstr(shp, SMB_PRMS1)) {
958 		*psnp = smb_serial_valid(s1.smbi_serial);
959 		*csnp = smb_serial_valid(s3.smbi_serial);
960 	} else {
961 		*csnp = smb_serial_valid(s1.smbi_serial);
962 	}
963 
964 	return (0);
965 }
966 
967 const char *
968 smbios_psn(smbios_hdl_t *shp)
969 {
970 	const char *psn, *csn;
971 
972 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : psn);
973 }
974 
975 const char *
976 smbios_csn(smbios_hdl_t *shp)
977 {
978 	const char *psn, *csn;
979 
980 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : csn);
981 }
982 
983 int
984 smbios_info_extprocessor(smbios_hdl_t *shp, id_t id,
985     smbios_processor_ext_t *epp)
986 {
987 	const smb_struct_t *stp = smb_lookup_id(shp, id);
988 	smb_processor_ext_t *exp;
989 
990 	if (stp == NULL)
991 		return (-1); /* errno is set for us */
992 
993 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_PROCESSOR)
994 		return (smb_set_errno(shp, ESMB_TYPE));
995 
996 	exp = (smb_processor_ext_t *)(uintptr_t)stp->smbst_hdr;
997 	bzero(epp, sizeof (smbios_processor_ext_t));
998 
999 	epp->smbpe_processor = exp->smbpre_processor;
1000 	epp->smbpe_fru = exp->smbpre_fru;
1001 	epp->smbpe_n = exp->smbpre_n;
1002 	epp->smbpe_apicid = exp->smbpre_apicid;
1003 
1004 	return (0);
1005 }
1006 
1007 int
1008 smbios_info_pciexrc(smbios_hdl_t *shp, id_t id,
1009     smbios_pciexrc_t *rcp)
1010 {
1011 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1012 	smb_pciexrc_t rc;
1013 
1014 	if (stp == NULL)
1015 		return (-1); /* errno is set for us */
1016 
1017 	if (stp->smbst_hdr->smbh_type != SUN_OEM_PCIEXRC)
1018 		return (smb_set_errno(shp, ESMB_TYPE));
1019 
1020 	smb_info_bcopy(stp->smbst_hdr, &rc, sizeof (rc));
1021 	bzero(rcp, sizeof (smbios_pciexrc_t));
1022 
1023 	rcp->smbpcie_bb = rc.smbpciexrc_bboard;
1024 	rcp->smbpcie_bdf = rc.smbpciexrc_bdf;
1025 
1026 	return (0);
1027 }
1028 
1029 int
1030 smbios_info_extmemarray(smbios_hdl_t *shp, id_t id, smbios_memarray_ext_t *emap)
1031 {
1032 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1033 	smb_memarray_ext_t exma;
1034 
1035 	if (stp == NULL)
1036 		return (-1); /* errno is set for us */
1037 
1038 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMARRAY)
1039 		return (smb_set_errno(shp, ESMB_TYPE));
1040 
1041 	smb_info_bcopy(stp->smbst_hdr, &exma, sizeof (exma));
1042 	bzero(emap, sizeof (smbios_memarray_ext_t));
1043 
1044 	emap->smbmae_ma = exma.smbmarre_ma;
1045 	emap->smbmae_comp = exma.smbmarre_component;
1046 	emap->smbmae_bdf = exma.smbmarre_bdf;
1047 
1048 	return (0);
1049 }
1050 
1051 int
1052 smbios_info_extmemdevice(smbios_hdl_t *shp, id_t id,
1053     smbios_memdevice_ext_t *emdp)
1054 {
1055 	const smb_struct_t *stp = smb_lookup_id(shp, id);
1056 	smb_memdevice_ext_t exmd;
1057 
1058 	if (stp == NULL)
1059 		return (-1); /* errno is set for us */
1060 
1061 	if (stp->smbst_hdr->smbh_type != SUN_OEM_EXT_MEMDEVICE)
1062 		return (smb_set_errno(shp, ESMB_TYPE));
1063 
1064 	smb_info_bcopy(stp->smbst_hdr, &exmd, sizeof (exmd));
1065 	bzero(emdp, sizeof (smbios_memdevice_ext_t));
1066 
1067 	emdp->smbmdeve_md = exmd.smbmdeve_mdev;
1068 	emdp->smbmdeve_drch = exmd.smbmdeve_dchan;
1069 	emdp->smbmdeve_ncs  = exmd.smbmdeve_ncs;
1070 	emdp->smbmdeve_cs = exmd.smbmdeve_cs;
1071 
1072 	return (0);
1073 }
1074