xref: /titanic_41/usr/src/common/smbios/smb_info.c (revision fbbfbc6ee66f60ad88ebd18c6c030797335354f4)
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  */
67 static const struct smb_infospec {
68 	uint8_t is_type;		/* structure type */
69 	uint8_t is_manu;		/* manufacturer offset */
70 	uint8_t is_product;		/* product name offset */
71 	uint8_t is_version;		/* version offset */
72 	uint8_t is_serial;		/* serial number offset */
73 	uint8_t is_asset;		/* asset tag offset */
74 	uint8_t is_location;		/* location string offset */
75 	uint8_t is_part;		/* part number offset */
76 } _smb_infospecs[] = {
77 	{ SMB_TYPE_SYSTEM,
78 		offsetof(smb_system_t, smbsi_manufacturer),
79 		offsetof(smb_system_t, smbsi_product),
80 		offsetof(smb_system_t, smbsi_version),
81 		offsetof(smb_system_t, smbsi_serial),
82 		0,
83 		0,
84 		0 },
85 	{ SMB_TYPE_BASEBOARD,
86 		offsetof(smb_bboard_t, smbbb_manufacturer),
87 		offsetof(smb_bboard_t, smbbb_product),
88 		offsetof(smb_bboard_t, smbbb_version),
89 		offsetof(smb_bboard_t, smbbb_serial),
90 		offsetof(smb_bboard_t, smbbb_asset),
91 		offsetof(smb_bboard_t, smbbb_location),
92 		0 },
93 	{ SMB_TYPE_CHASSIS,
94 		offsetof(smb_chassis_t, smbch_manufacturer),
95 		0,
96 		offsetof(smb_chassis_t, smbch_version),
97 		offsetof(smb_chassis_t, smbch_serial),
98 		offsetof(smb_chassis_t, smbch_asset),
99 		0,
100 		0 },
101 	{ SMB_TYPE_PROCESSOR,
102 		offsetof(smb_processor_t, smbpr_manufacturer),
103 		0,
104 		offsetof(smb_processor_t, smbpr_version),
105 		offsetof(smb_processor_t, smbpr_serial),
106 		offsetof(smb_processor_t, smbpr_asset),
107 		offsetof(smb_processor_t, smbpr_socket),
108 		offsetof(smb_processor_t, smbpr_part) },
109 	{ SMB_TYPE_CACHE,
110 		0,
111 		0,
112 		0,
113 		0,
114 		0,
115 		offsetof(smb_cache_t, smbca_socket),
116 		0 },
117 	{ SMB_TYPE_PORT,
118 		0,
119 		0,
120 		0,
121 		0,
122 		0,
123 		offsetof(smb_port_t, smbpo_iref),
124 		0 },
125 	{ SMB_TYPE_SLOT,
126 		0,
127 		0,
128 		0,
129 		0,
130 		0,
131 		offsetof(smb_slot_t, smbsl_name),
132 		0 },
133 	{ SMB_TYPE_MEMDEVICE,
134 		offsetof(smb_memdevice_t, smbmdev_manufacturer),
135 		0,
136 		0,
137 		offsetof(smb_memdevice_t, smbmdev_serial),
138 		offsetof(smb_memdevice_t, smbmdev_asset),
139 		offsetof(smb_memdevice_t, smbmdev_dloc),
140 		offsetof(smb_memdevice_t, smbmdev_part) },
141 	{ SMB_TYPE_POWERSUP,
142 		offsetof(smb_powersup_t, smbpsup_manufacturer),
143 		offsetof(smb_powersup_t, smbpsup_devname),
144 		offsetof(smb_powersup_t, smbpsup_rev),
145 		offsetof(smb_powersup_t, smbpsup_serial),
146 		offsetof(smb_powersup_t, smbpsup_asset),
147 		offsetof(smb_powersup_t, smbpsup_loc),
148 		offsetof(smb_powersup_t, smbpsup_part) },
149 	{ SMB_TYPE_EOT }
150 };
151 
152 static const char *
153 smb_info_strptr(const smb_struct_t *stp, uint8_t off, int *n)
154 {
155 	const uint8_t *sp = (const uint8_t *)(uintptr_t)stp->smbst_hdr;
156 
157 	if (off != 0 && sp + off < stp->smbst_end) {
158 		(*n)++; /* indicate success for caller */
159 		return (smb_strptr(stp, sp[off]));
160 	}
161 
162 	return (smb_strptr(stp, 0));
163 }
164 
165 static void
166 smb_info_bcopy(const smb_header_t *hp, void *dst, size_t dstlen)
167 {
168 	if (dstlen > hp->smbh_len) {
169 		bcopy(hp, dst, hp->smbh_len);
170 		bzero((char *)dst + hp->smbh_len, dstlen - hp->smbh_len);
171 	} else
172 		bcopy(hp, dst, dstlen);
173 }
174 
175 void
176 smbios_info_smbios(smbios_hdl_t *shp, smbios_entry_t *ep)
177 {
178 	bcopy(&shp->sh_ent, ep, sizeof (smbios_entry_t));
179 }
180 
181 #ifndef _KERNEL
182 static char smbios_product_override[256];
183 static boolean_t smbios_product_checked;
184 #endif
185 
186 int
187 smbios_info_common(smbios_hdl_t *shp, id_t id, smbios_info_t *ip)
188 {
189 	const smb_struct_t *stp = smb_lookup_id(shp, id);
190 	const struct smb_infospec *isp;
191 	int n = 0;
192 
193 	if (stp == NULL)
194 		return (-1); /* errno is set for us */
195 
196 	for (isp = _smb_infospecs; isp->is_type != SMB_TYPE_EOT; isp++) {
197 		if (isp->is_type == stp->smbst_hdr->smbh_type)
198 			break;
199 	}
200 
201 	ip->smbi_manufacturer = smb_info_strptr(stp, isp->is_manu, &n);
202 	ip->smbi_product = smb_info_strptr(stp, isp->is_product, &n);
203 	ip->smbi_version = smb_info_strptr(stp, isp->is_version, &n);
204 	ip->smbi_serial = smb_info_strptr(stp, isp->is_serial, &n);
205 	ip->smbi_asset = smb_info_strptr(stp, isp->is_asset, &n);
206 	ip->smbi_location = smb_info_strptr(stp, isp->is_location, &n);
207 	ip->smbi_part = smb_info_strptr(stp, isp->is_part, &n);
208 
209 	/*
210 	 * This private file allows developers to experiment with reporting
211 	 * different platform strings from SMBIOS.  It is not a supported
212 	 * mechanism in the long term, and does not work in the kernel.
213 	 */
214 #ifndef _KERNEL
215 	if (isp->is_type == SMB_TYPE_SYSTEM) {
216 		if (!smbios_product_checked) {
217 			int fd = open("/etc/smbios_product", O_RDONLY);
218 			if (fd >= 0) {
219 				(void) read(fd, smbios_product_override,
220 				    sizeof (smbios_product_override) - 1);
221 				(void) close(fd);
222 			}
223 			smbios_product_checked = B_TRUE;
224 		}
225 
226 		if (smbios_product_override[0] != '\0')
227 			ip->smbi_product = smbios_product_override;
228 	}
229 #endif
230 
231 	/*
232 	 * If we have a port with an empty internal reference designator string
233 	 * try using the external reference designator string instead.
234 	 */
235 	if (isp->is_type == SMB_TYPE_PORT && ip->smbi_location[0] == '\0') {
236 		ip->smbi_location = smb_info_strptr(stp,
237 		    offsetof(smb_port_t, smbpo_eref), &n);
238 	}
239 
240 	return (n ? 0 : smb_set_errno(shp, ESMB_NOINFO));
241 }
242 
243 id_t
244 smbios_info_bios(smbios_hdl_t *shp, smbios_bios_t *bp)
245 {
246 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BIOS);
247 	const smb_bios_t *bip;
248 
249 	if (stp == NULL)
250 		return (-1); /* errno is set for us */
251 
252 	if (stp->smbst_hdr->smbh_len < sizeof (smb_bios_t) - sizeof (uint8_t))
253 		return (smb_set_errno(shp, ESMB_CORRUPT));
254 
255 	bip = (smb_bios_t *)(uintptr_t)stp->smbst_hdr;
256 	bzero(bp, sizeof (smbios_bios_t));
257 
258 	bp->smbb_vendor = smb_strptr(stp, bip->smbbi_vendor);
259 	bp->smbb_version = smb_strptr(stp, bip->smbbi_version);
260 	bp->smbb_segment = bip->smbbi_segment;
261 	bp->smbb_reldate = smb_strptr(stp, bip->smbbi_reldate);
262 	bp->smbb_romsize = 64 * 1024 * ((uint32_t)bip->smbbi_romsize + 1);
263 	bp->smbb_runsize = 16 * (0x10000 - (uint32_t)bip->smbbi_segment);
264 	bp->smbb_cflags = bip->smbbi_cflags;
265 
266 	/*
267 	 * If one or more extension bytes are present, reset smbb_xcflags to
268 	 * point to them.  Otherwise leave this member set to NULL.
269 	 */
270 	if (stp->smbst_hdr->smbh_len >= sizeof (smb_bios_t)) {
271 		bp->smbb_xcflags = bip->smbbi_xcflags;
272 		bp->smbb_nxcflags = stp->smbst_hdr->smbh_len -
273 		    sizeof (smb_bios_t) + 1;
274 
275 		if (bp->smbb_nxcflags > SMB_BIOSXB_ECFW_MIN &&
276 		    smb_gteq(shp, SMB_VERSION_24)) {
277 			bp->smbb_biosv.smbv_major =
278 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MAJ];
279 			bp->smbb_biosv.smbv_minor =
280 			    bip->smbbi_xcflags[SMB_BIOSXB_BIOS_MIN];
281 			bp->smbb_ecfwv.smbv_major =
282 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MAJ];
283 			bp->smbb_ecfwv.smbv_minor =
284 			    bip->smbbi_xcflags[SMB_BIOSXB_ECFW_MIN];
285 		}
286 	}
287 
288 	return (stp->smbst_hdr->smbh_hdl);
289 }
290 
291 id_t
292 smbios_info_system(smbios_hdl_t *shp, smbios_system_t *sip)
293 {
294 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM);
295 	smb_system_t si;
296 
297 	if (stp == NULL)
298 		return (-1); /* errno is set for us */
299 
300 	smb_info_bcopy(stp->smbst_hdr, &si, sizeof (si));
301 	bzero(sip, sizeof (smbios_system_t));
302 
303 	sip->smbs_uuid = ((smb_system_t *)stp->smbst_hdr)->smbsi_uuid;
304 	sip->smbs_uuidlen = sizeof (si.smbsi_uuid);
305 	sip->smbs_wakeup = si.smbsi_wakeup;
306 	sip->smbs_sku = smb_strptr(stp, si.smbsi_sku);
307 	sip->smbs_family = smb_strptr(stp, si.smbsi_family);
308 
309 	return (stp->smbst_hdr->smbh_hdl);
310 }
311 
312 int
313 smbios_info_bboard(smbios_hdl_t *shp, id_t id, smbios_bboard_t *bbp)
314 {
315 	const smb_struct_t *stp = smb_lookup_id(shp, id);
316 	smb_bboard_t bb;
317 
318 	if (stp == NULL)
319 		return (-1); /* errno is set for us */
320 
321 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_BASEBOARD)
322 		return (smb_set_errno(shp, ESMB_TYPE));
323 
324 	smb_info_bcopy(stp->smbst_hdr, &bb, sizeof (bb));
325 	bzero(bbp, sizeof (smbios_bboard_t));
326 
327 	/*
328 	 * At present, we do not provide support for the contained object
329 	 * handles portion of the Base Board structure, as none of the 2.3+
330 	 * BIOSes commonly in use appear to implement it at present.
331 	 */
332 	bbp->smbb_chassis = bb.smbbb_chassis;
333 	bbp->smbb_flags = bb.smbbb_flags;
334 	bbp->smbb_type = bb.smbbb_type;
335 
336 	return (0);
337 }
338 
339 int
340 smbios_info_chassis(smbios_hdl_t *shp, id_t id, smbios_chassis_t *chp)
341 {
342 	const smb_struct_t *stp = smb_lookup_id(shp, id);
343 	smb_chassis_t ch;
344 
345 	if (stp == NULL)
346 		return (-1); /* errno is set for us */
347 
348 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CHASSIS)
349 		return (smb_set_errno(shp, ESMB_TYPE));
350 
351 	smb_info_bcopy(stp->smbst_hdr, &ch, sizeof (ch));
352 	bzero(chp, sizeof (smbios_chassis_t));
353 
354 	/*
355 	 * At present, we do not provide support for the contained object
356 	 * handles portion of the Chassis structure, as none of the 2.3+
357 	 * BIOSes commonly in use appear to implement it at present.
358 	 */
359 	chp->smbc_oemdata = ch.smbch_oemdata;
360 	chp->smbc_lock = (ch.smbch_type & SMB_CHT_LOCK) != 0;
361 	chp->smbc_type = ch.smbch_type & ~SMB_CHT_LOCK;
362 	chp->smbc_bustate = ch.smbch_bustate;
363 	chp->smbc_psstate = ch.smbch_psstate;
364 	chp->smbc_thstate = ch.smbch_thstate;
365 	chp->smbc_security = ch.smbch_security;
366 	chp->smbc_uheight = ch.smbch_uheight;
367 	chp->smbc_cords = ch.smbch_cords;
368 	chp->smbc_elems = ch.smbch_cn;
369 
370 	return (0);
371 }
372 
373 int
374 smbios_info_processor(smbios_hdl_t *shp, id_t id, smbios_processor_t *pp)
375 {
376 	const smb_struct_t *stp = smb_lookup_id(shp, id);
377 	smb_processor_t p;
378 
379 	if (stp == NULL)
380 		return (-1); /* errno is set for us */
381 
382 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PROCESSOR)
383 		return (smb_set_errno(shp, ESMB_TYPE));
384 
385 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
386 	bzero(pp, sizeof (smbios_processor_t));
387 
388 	pp->smbp_cpuid = p.smbpr_cpuid;
389 	pp->smbp_type = p.smbpr_type;
390 	pp->smbp_family = p.smbpr_family;
391 	pp->smbp_voltage = p.smbpr_voltage;
392 	pp->smbp_maxspeed = p.smbpr_maxspeed;
393 	pp->smbp_curspeed = p.smbpr_curspeed;
394 	pp->smbp_status = p.smbpr_status;
395 	pp->smbp_upgrade = p.smbpr_upgrade;
396 	pp->smbp_l1cache = p.smbpr_l1cache;
397 	pp->smbp_l2cache = p.smbpr_l2cache;
398 	pp->smbp_l3cache = p.smbpr_l3cache;
399 
400 	return (0);
401 }
402 
403 int
404 smbios_info_cache(smbios_hdl_t *shp, id_t id, smbios_cache_t *cap)
405 {
406 	const smb_struct_t *stp = smb_lookup_id(shp, id);
407 	smb_cache_t c;
408 
409 	if (stp == NULL)
410 		return (-1); /* errno is set for us */
411 
412 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_CACHE)
413 		return (smb_set_errno(shp, ESMB_TYPE));
414 
415 	smb_info_bcopy(stp->smbst_hdr, &c, sizeof (c));
416 	bzero(cap, sizeof (smbios_cache_t));
417 
418 	cap->smba_maxsize = SMB_CACHE_SIZE(c.smbca_maxsize);
419 	cap->smba_size = SMB_CACHE_SIZE(c.smbca_size);
420 	cap->smba_stype = c.smbca_stype;
421 	cap->smba_ctype = c.smbca_ctype;
422 	cap->smba_speed = c.smbca_speed;
423 	cap->smba_etype = c.smbca_etype;
424 	cap->smba_ltype = c.smbca_ltype;
425 	cap->smba_assoc = c.smbca_assoc;
426 	cap->smba_level = SMB_CACHE_CFG_LEVEL(c.smbca_config);
427 	cap->smba_mode = SMB_CACHE_CFG_MODE(c.smbca_config);
428 	cap->smba_location = SMB_CACHE_CFG_LOCATION(c.smbca_config);
429 
430 	if (SMB_CACHE_CFG_ENABLED(c.smbca_config))
431 		cap->smba_flags |= SMB_CAF_ENABLED;
432 
433 	if (SMB_CACHE_CFG_SOCKETED(c.smbca_config))
434 		cap->smba_flags |= SMB_CAF_SOCKETED;
435 
436 	return (0);
437 }
438 
439 int
440 smbios_info_port(smbios_hdl_t *shp, id_t id, smbios_port_t *pop)
441 {
442 	const smb_struct_t *stp = smb_lookup_id(shp, id);
443 	smb_port_t p;
444 
445 	if (stp == NULL)
446 		return (-1); /* errno is set for us */
447 
448 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_PORT)
449 		return (smb_set_errno(shp, ESMB_TYPE));
450 
451 	smb_info_bcopy(stp->smbst_hdr, &p, sizeof (p));
452 	bzero(pop, sizeof (smbios_port_t));
453 
454 	pop->smbo_iref = smb_strptr(stp, p.smbpo_iref);
455 	pop->smbo_eref = smb_strptr(stp, p.smbpo_eref);
456 
457 	pop->smbo_itype = p.smbpo_itype;
458 	pop->smbo_etype = p.smbpo_etype;
459 	pop->smbo_ptype = p.smbpo_ptype;
460 
461 	return (0);
462 }
463 
464 int
465 smbios_info_slot(smbios_hdl_t *shp, id_t id, smbios_slot_t *sp)
466 {
467 	const smb_struct_t *stp = smb_lookup_id(shp, id);
468 	smb_slot_t s;
469 
470 	if (stp == NULL)
471 		return (-1); /* errno is set for us */
472 
473 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_SLOT)
474 		return (smb_set_errno(shp, ESMB_TYPE));
475 
476 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
477 	bzero(sp, sizeof (smbios_slot_t));
478 
479 	sp->smbl_name = smb_strptr(stp, s.smbsl_name);
480 	sp->smbl_type = s.smbsl_type;
481 	sp->smbl_width = s.smbsl_width;
482 	sp->smbl_usage = s.smbsl_usage;
483 	sp->smbl_length = s.smbsl_length;
484 	sp->smbl_id = s.smbsl_id;
485 	sp->smbl_ch1 = s.smbsl_ch1;
486 	sp->smbl_ch2 = s.smbsl_ch2;
487 
488 	return (0);
489 }
490 
491 int
492 smbios_info_obdevs(smbios_hdl_t *shp, id_t id, int obc, smbios_obdev_t *obp)
493 {
494 	const smb_struct_t *stp = smb_lookup_id(shp, id);
495 	const smb_obdev_t *op;
496 	int i, m, n;
497 
498 	if (stp == NULL)
499 		return (-1); /* errno is set for us */
500 
501 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OBDEVS)
502 		return (smb_set_errno(shp, ESMB_TYPE));
503 
504 	op = (smb_obdev_t *)((uintptr_t)stp->smbst_hdr + sizeof (smb_header_t));
505 	m = (stp->smbst_hdr->smbh_len - sizeof (smb_header_t)) / sizeof (*op);
506 	n = MIN(m, obc);
507 
508 	for (i = 0; i < n; i++, op++, obp++) {
509 		obp->smbd_name = smb_strptr(stp, op->smbob_name);
510 		obp->smbd_type = op->smbob_type & ~SMB_OBT_ENABLED;
511 		obp->smbd_enabled = (op->smbob_type & SMB_OBT_ENABLED) != 0;
512 	}
513 
514 	return (m);
515 }
516 
517 /*
518  * The implementation structures for OEMSTR, SYSCONFSTR, and LANG all use the
519  * first byte to indicate the size of a string table at the end of the record.
520  * Therefore, smbios_info_strtab() can be used to retrieve the table size and
521  * strings for any of these underlying record types.
522  */
523 int
524 smbios_info_strtab(smbios_hdl_t *shp, id_t id, int argc, const char *argv[])
525 {
526 	const smb_struct_t *stp = smb_lookup_id(shp, id);
527 	smb_strtab_t s;
528 	int i, n;
529 
530 	if (stp == NULL)
531 		return (-1); /* errno is set for us */
532 
533 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR &&
534 	    stp->smbst_hdr->smbh_type != SMB_TYPE_SYSCONFSTR &&
535 	    stp->smbst_hdr->smbh_type != SMB_TYPE_LANG)
536 		return (smb_set_errno(shp, ESMB_TYPE));
537 
538 	smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
539 	n = MIN(s.smbtb_count, argc);
540 
541 	for (i = 0; i < n; i++)
542 		argv[i] = smb_strptr(stp, i + 1);
543 
544 	return (s.smbtb_count);
545 }
546 
547 id_t
548 smbios_info_lang(smbios_hdl_t *shp, smbios_lang_t *lp)
549 {
550 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_LANG);
551 	smb_lang_t l;
552 
553 	if (stp == NULL)
554 		return (-1); /* errno is set for us */
555 
556 	smb_info_bcopy(stp->smbst_hdr, &l, sizeof (l));
557 	bzero(lp, sizeof (smbios_lang_t));
558 
559 	lp->smbla_cur = smb_strptr(stp, l.smblang_cur);
560 	lp->smbla_fmt = l.smblang_flags & 1;
561 	lp->smbla_num = l.smblang_num;
562 
563 	return (stp->smbst_hdr->smbh_hdl);
564 }
565 
566 id_t
567 smbios_info_eventlog(smbios_hdl_t *shp, smbios_evlog_t *evp)
568 {
569 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_EVENTLOG);
570 	const smb_sel_t *sel;
571 	size_t len;
572 
573 	if (stp == NULL)
574 		return (-1); /* errno is set for us */
575 
576 	if (stp->smbst_hdr->smbh_len < sizeof (smb_sel_t) - sizeof (uint8_t))
577 		return (smb_set_errno(shp, ESMB_CORRUPT));
578 
579 	sel = (smb_sel_t *)(uintptr_t)stp->smbst_hdr;
580 	len = stp->smbst_hdr->smbh_len - sizeof (smb_sel_t) + sizeof (uint8_t);
581 	bzero(evp, sizeof (smbios_evlog_t));
582 
583 	if (len < sel->smbsel_typec * sel->smbsel_typesz)
584 		return (smb_set_errno(shp, ESMB_CORRUPT));
585 
586 	evp->smbev_size = sel->smbsel_len;
587 	evp->smbev_hdr = sel->smbsel_hdroff;
588 	evp->smbev_data = sel->smbsel_dataoff;
589 	evp->smbev_method = sel->smbsel_method;
590 	evp->smbev_flags = sel->smbsel_status;
591 	evp->smbev_format = sel->smbsel_format;
592 	evp->smbev_token = sel->smbsel_token;
593 	evp->smbev_addr.eva_addr = sel->smbsel_addr;
594 
595 	if (sel->smbsel_typesz == sizeof (smbios_evtype_t)) {
596 		evp->smbev_typec = sel->smbsel_typec;
597 		evp->smbev_typev = (void *)(uintptr_t)sel->smbsel_typev;
598 	}
599 
600 	return (stp->smbst_hdr->smbh_hdl);
601 }
602 
603 int
604 smbios_info_memarray(smbios_hdl_t *shp, id_t id, smbios_memarray_t *map)
605 {
606 	const smb_struct_t *stp = smb_lookup_id(shp, id);
607 	smb_memarray_t m;
608 
609 	if (stp == NULL)
610 		return (-1); /* errno is set for us */
611 
612 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAY)
613 		return (smb_set_errno(shp, ESMB_TYPE));
614 
615 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
616 	bzero(map, sizeof (smbios_memarray_t));
617 
618 	map->smbma_location = m.smbmarr_loc;
619 	map->smbma_use = m.smbmarr_use;
620 	map->smbma_ecc = m.smbmarr_ecc;
621 	map->smbma_ndevs = m.smbmarr_ndevs;
622 	map->smbma_err = m.smbmarr_err;
623 
624 	if (m.smbmarr_cap != 0x80000000)
625 		map->smbma_size = (uint64_t)m.smbmarr_cap * 1024;
626 	else
627 		map->smbma_size = 0; /* unknown */
628 
629 	return (0);
630 }
631 
632 int
633 smbios_info_memarrmap(smbios_hdl_t *shp, id_t id, smbios_memarrmap_t *map)
634 {
635 	const smb_struct_t *stp = smb_lookup_id(shp, id);
636 	smb_memarrmap_t m;
637 
638 	if (stp == NULL)
639 		return (-1); /* errno is set for us */
640 
641 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMARRAYMAP)
642 		return (smb_set_errno(shp, ESMB_TYPE));
643 
644 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
645 	bzero(map, sizeof (smbios_memarrmap_t));
646 
647 	map->smbmam_array = m.smbamap_array;
648 	map->smbmam_width = m.smbamap_width;
649 	map->smbmam_addr = (uint64_t)m.smbamap_start * 1024;
650 	map->smbmam_size = (uint64_t)
651 	    (m.smbamap_end - m.smbamap_start + 1) * 1024;
652 
653 	return (0);
654 }
655 
656 int
657 smbios_info_memdevice(smbios_hdl_t *shp, id_t id, smbios_memdevice_t *mdp)
658 {
659 	const smb_struct_t *stp = smb_lookup_id(shp, id);
660 	smb_memdevice_t m;
661 
662 	if (stp == NULL)
663 		return (-1); /* errno is set for us */
664 
665 	if (stp->smbst_hdr->smbh_type != SMB_TYPE_MEMDEVICE)
666 		return (smb_set_errno(shp, ESMB_TYPE));
667 
668 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
669 	bzero(mdp, sizeof (smbios_memdevice_t));
670 
671 	mdp->smbmd_array = m.smbmdev_array;
672 	mdp->smbmd_error = m.smbmdev_error;
673 	mdp->smbmd_twidth = m.smbmdev_twidth == 0xFFFF ? -1U : m.smbmdev_twidth;
674 	mdp->smbmd_dwidth = m.smbmdev_dwidth == 0xFFFF ? -1U : m.smbmdev_dwidth;
675 
676 	if (mdp->smbmd_size != 0xFFFF) {
677 		mdp->smbmd_size = (uint64_t)(m.smbmdev_size & ~SMB_MDS_KBYTES);
678 		if (m.smbmdev_size & SMB_MDS_KBYTES)
679 			mdp->smbmd_size *= 1024;
680 		else
681 			mdp->smbmd_size *= 1024 * 1024;
682 	} else
683 		mdp->smbmd_size = -1ULL; /* size unknown */
684 
685 	mdp->smbmd_form = m.smbmdev_form;
686 	mdp->smbmd_set = m.smbmdev_set;
687 	mdp->smbmd_type = m.smbmdev_type;
688 	mdp->smbmd_flags = m.smbmdev_flags;
689 	mdp->smbmd_dloc = smb_strptr(stp, m.smbmdev_dloc);
690 	mdp->smbmd_bloc = smb_strptr(stp, m.smbmdev_bloc);
691 
692 	if (m.smbmdev_speed != 0)
693 		mdp->smbmd_speed = 1000 / m.smbmdev_speed; /* MHz -> nsec */
694 
695 	return (0);
696 }
697 
698 int
699 smbios_info_memdevmap(smbios_hdl_t *shp, id_t id, smbios_memdevmap_t *mdp)
700 {
701 	const smb_struct_t *stp = smb_lookup_id(shp, id);
702 	smb_memdevmap_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_MEMDEVICEMAP)
708 		return (smb_set_errno(shp, ESMB_TYPE));
709 
710 	smb_info_bcopy(stp->smbst_hdr, &m, sizeof (m));
711 	bzero(mdp, sizeof (smbios_memdevmap_t));
712 
713 	mdp->smbmdm_device = m.smbdmap_device;
714 	mdp->smbmdm_arrmap = m.smbdmap_array;
715 	mdp->smbmdm_addr = (uint64_t)m.smbdmap_start * 1024;
716 	mdp->smbmdm_size = (uint64_t)
717 	    (m.smbdmap_end - m.smbdmap_start + 1) * 1024;
718 	mdp->smbmdm_rpos = m.smbdmap_rpos;
719 	mdp->smbmdm_ipos = m.smbdmap_ipos;
720 	mdp->smbmdm_idepth = m.smbdmap_idepth;
721 
722 	return (0);
723 }
724 
725 id_t
726 smbios_info_hwsec(smbios_hdl_t *shp, smbios_hwsec_t *hsp)
727 {
728 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_SECURITY);
729 	smb_hwsec_t hs;
730 
731 	if (stp == NULL)
732 		return (-1); /* errno is set for us */
733 
734 	smb_info_bcopy(stp->smbst_hdr, &hs, sizeof (hs));
735 	bzero(hsp, sizeof (smbios_hwsec_t));
736 
737 	hsp->smbh_pwr_ps = SMB_HWS_PWR_PS(hs.smbhs_settings);
738 	hsp->smbh_kbd_ps = SMB_HWS_KBD_PS(hs.smbhs_settings);
739 	hsp->smbh_adm_ps = SMB_HWS_ADM_PS(hs.smbhs_settings);
740 	hsp->smbh_pan_ps = SMB_HWS_PAN_PS(hs.smbhs_settings);
741 
742 	return (stp->smbst_hdr->smbh_hdl);
743 }
744 
745 id_t
746 smbios_info_boot(smbios_hdl_t *shp, smbios_boot_t *bp)
747 {
748 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_BOOT);
749 	const smb_boot_t *b = (smb_boot_t *)(uintptr_t)stp->smbst_hdr;
750 
751 	if (stp == NULL)
752 		return (-1); /* errno is set for us */
753 
754 	bzero(bp, sizeof (smbios_boot_t));
755 
756 	bp->smbt_status = b->smbbo_status[0];
757 	bp->smbt_size = stp->smbst_hdr->smbh_len - sizeof (smb_boot_t);
758 	bp->smbt_data = bp->smbt_size ? &b->smbbo_status[1] : NULL;
759 
760 	return (stp->smbst_hdr->smbh_hdl);
761 }
762 
763 id_t
764 smbios_info_ipmi(smbios_hdl_t *shp, smbios_ipmi_t *ip)
765 {
766 	const smb_struct_t *stp = smb_lookup_type(shp, SMB_TYPE_IPMIDEV);
767 	smb_ipmi_t i;
768 
769 	if (stp == NULL)
770 		return (-1); /* errno is set for us */
771 
772 	smb_info_bcopy(stp->smbst_hdr, &i, sizeof (i));
773 	bzero(ip, sizeof (smbios_ipmi_t));
774 
775 	ip->smbip_type = i.smbipm_type;
776 	ip->smbip_vers.smbv_major = SMB_IPM_SPEC_MAJOR(i.smbipm_spec);
777 	ip->smbip_vers.smbv_minor = SMB_IPM_SPEC_MINOR(i.smbipm_spec);
778 	ip->smbip_i2c = i.smbipm_i2c;
779 	ip->smbip_addr = i.smbipm_addr & ~SMB_IPM_ADDR_IO;
780 	ip->smbip_intr = i.smbipm_intr;
781 
782 	if (i.smbipm_bus != (uint8_t)-1)
783 		ip->smbip_bus = i.smbipm_bus;
784 	else
785 		ip->smbip_bus = -1u;
786 
787 	if (SMB_IPM_INFO_LSB(i.smbipm_info))
788 		ip->smbip_addr |= 1; /* turn on least-significant bit of addr */
789 
790 	if (i.smbipm_addr & SMB_IPM_ADDR_IO) {
791 		switch (SMB_IPM_INFO_REGS(i.smbipm_info)) {
792 		case SMB_IPM_REGS_1B:
793 			ip->smbip_regspacing = 1;
794 			break;
795 		case SMB_IPM_REGS_4B:
796 			ip->smbip_regspacing = 4;
797 			break;
798 		case SMB_IPM_REGS_16B:
799 			ip->smbip_regspacing = 16;
800 			break;
801 		default:
802 			ip->smbip_regspacing = 1;
803 		}
804 		ip->smbip_flags |= SMB_IPMI_F_IOADDR;
805 	}
806 
807 	if (SMB_IPM_INFO_ISPEC(i.smbipm_info))
808 		ip->smbip_flags |= SMB_IPMI_F_INTRSPEC;
809 
810 	if (SMB_IPM_INFO_IPOL(i.smbipm_info) == SMB_IPM_IPOL_HI)
811 		ip->smbip_flags |= SMB_IPMI_F_INTRHIGH;
812 
813 	if (SMB_IPM_INFO_IMODE(i.smbipm_info) == SMB_IPM_IMODE_EDGE)
814 		ip->smbip_flags |= SMB_IPMI_F_INTREDGE;
815 
816 	return (stp->smbst_hdr->smbh_hdl);
817 }
818 
819 static boolean_t
820 smbios_has_oemstr(smbios_hdl_t *shp, const char *oemstr)
821 {
822 	const smb_struct_t *stp = shp->sh_structs;
823 	smb_strtab_t s;
824 	int i, j;
825 
826 	for (i = 0; i < shp->sh_nstructs; i++, stp++) {
827 		if (stp->smbst_hdr->smbh_type != SMB_TYPE_OEMSTR)
828 			continue;
829 
830 		smb_info_bcopy(stp->smbst_hdr, &s, sizeof (s));
831 		for (j = 0; j < s.smbtb_count; j++)
832 			if (strcmp(smb_strptr(stp, j + 1), oemstr) == 0)
833 				return (B_TRUE);
834 	}
835 
836 	return (B_FALSE);
837 }
838 
839 static const char *
840 smb_serial_valid(const char *serial)
841 {
842 	char buf[MAXNAMELEN];
843 	int i = 0;
844 
845 	if (serial == NULL)
846 		return (NULL);
847 
848 	(void) strlcpy(buf, serial, sizeof (buf));
849 
850 	while (buf[i] != '\0' && buf[i] == ' ')
851 		i++;
852 
853 	if (buf[i] == '\0' || strstr(buf, SMB_DEFAULT1) != NULL ||
854 	    strstr(buf, SMB_DEFAULT2) != NULL)
855 		return (NULL);
856 
857 	return (serial);
858 }
859 
860 /*
861  * Get chassis SN or product SN
862  */
863 static int
864 smb_get_sn(smbios_hdl_t *shp, const char **psnp, const char **csnp)
865 {
866 	const smb_struct_t *stp;
867 	smbios_info_t s1, s3;
868 
869 	if (psnp == NULL || csnp == NULL)
870 		return (smb_set_errno(shp, ESMB_INVAL));
871 
872 	*psnp = *csnp = NULL;
873 
874 	/*
875 	 * If SMBIOS meets Sun's PRMS requirements, retrieve product SN
876 	 * from type 1 structure, and chassis SN from type 3 structure.
877 	 * Otherwise return SN in type 1 structure as chassis SN.
878 	 */
879 
880 	/* Get type 1 SN */
881 	if ((stp = smb_lookup_type(shp, SMB_TYPE_SYSTEM)) == NULL ||
882 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s1) == SMB_ERR)
883 		s1.smbi_serial = NULL;
884 
885 	/* Get type 3 SN */
886 	if ((stp = smb_lookup_type(shp, SMB_TYPE_CHASSIS)) == NULL ||
887 	    smbios_info_common(shp, stp->smbst_hdr->smbh_hdl, &s3) == SMB_ERR)
888 		s3.smbi_serial = NULL;
889 
890 	if (smbios_has_oemstr(shp, SMB_PRMS1)) {
891 		*psnp = smb_serial_valid(s1.smbi_serial);
892 		*csnp = smb_serial_valid(s3.smbi_serial);
893 	} else {
894 		*csnp = smb_serial_valid(s1.smbi_serial);
895 	}
896 
897 	return (0);
898 }
899 
900 const char *
901 smbios_psn(smbios_hdl_t *shp)
902 {
903 	const char *psn, *csn;
904 
905 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : psn);
906 }
907 
908 const char *
909 smbios_csn(smbios_hdl_t *shp)
910 {
911 	const char *psn, *csn;
912 
913 	return (smb_get_sn(shp, &psn, &csn) == SMB_ERR ? NULL : csn);
914 }
915