xref: /titanic_52/usr/src/cmd/smbios/smbios.c (revision 55f5292c612446ce6f93ddd248c0019b5974618b)
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 #include <sys/sysmacros.h>
28 #include <sys/param.h>
29 
30 #include <smbios.h>
31 #include <alloca.h>
32 #include <limits.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <ctype.h>
41 
42 #define	SMBIOS_SUCCESS	0
43 #define	SMBIOS_ERROR	1
44 #define	SMBIOS_USAGE	2
45 
46 static const char *g_pname;
47 static int g_hdr;
48 
49 static int opt_e;
50 static int opt_i = -1;
51 static int opt_O;
52 static int opt_s;
53 static int opt_t = -1;
54 static int opt_x;
55 
56 /*PRINTFLIKE2*/
57 static void
58 oprintf(FILE *fp, const char *format, ...)
59 {
60 	va_list ap;
61 
62 	va_start(ap, format);
63 	(void) vfprintf(fp, format, ap);
64 	va_end(ap);
65 }
66 
67 /*PRINTFLIKE3*/
68 static void
69 desc_printf(const char *d, FILE *fp, const char *format, ...)
70 {
71 	va_list ap;
72 
73 	va_start(ap, format);
74 	(void) vfprintf(fp, format, ap);
75 	va_end(ap);
76 
77 	if (d != NULL)
78 		(void) fprintf(fp, " (%s)\n", d);
79 	else
80 		(void) fprintf(fp, "\n");
81 }
82 
83 static void
84 flag_printf(FILE *fp, const char *s, uint_t flags, size_t bits,
85     const char *(*flag_name)(uint_t), const char *(*flag_desc)(uint_t))
86 {
87 	size_t i;
88 
89 	oprintf(fp, "  %s: 0x%x\n", s, flags);
90 
91 	for (i = 0; i < bits; i++) {
92 		uint_t f = 1 << i;
93 		const char *n;
94 
95 		if (!(flags & f))
96 			continue;
97 
98 		if ((n = flag_name(f)) != NULL)
99 			desc_printf(flag_desc(f), fp, "\t%s", n);
100 		else
101 			desc_printf(flag_desc(f), fp, "\t0x%x", f);
102 	}
103 }
104 
105 static void
106 flag64_printf(FILE *fp, const char *s, uint64_t flags, size_t bits,
107     const char *(*flag_name)(uint64_t), const char *(*flag_desc)(uint64_t))
108 {
109 	size_t i;
110 
111 	oprintf(fp, "  %s: 0x%llx\n", s, (u_longlong_t)flags);
112 
113 	for (i = 0; i < bits; i++) {
114 		u_longlong_t f = 1ULL << i;
115 		const char *n;
116 
117 		if (!(flags & f))
118 			continue;
119 
120 		if ((n = flag_name(f)) != NULL)
121 			desc_printf(flag_desc(f), fp, "\t%s", n);
122 		else
123 			desc_printf(flag_desc(f), fp, "\t0x%llx", f);
124 	}
125 }
126 
127 static void
128 id_printf(FILE *fp, const char *s, id_t id)
129 {
130 	switch (id) {
131 	case SMB_ID_NONE:
132 		oprintf(fp, "%sNone\n", s);
133 		break;
134 	case SMB_ID_NOTSUP:
135 		oprintf(fp, "%sNot Supported\n", s);
136 		break;
137 	default:
138 		oprintf(fp, "%s%u\n", s, (uint_t)id);
139 	}
140 }
141 
142 static void
143 print_smbios(smbios_hdl_t *shp, FILE *fp)
144 {
145 	smbios_entry_t ep;
146 	int i;
147 
148 	smbios_info_smbios(shp, &ep);
149 
150 	oprintf(fp, "Entry Point Anchor Tag: %*.*s\n",
151 	    (int)sizeof (ep.smbe_eanchor), (int)sizeof (ep.smbe_eanchor),
152 	    ep.smbe_eanchor);
153 
154 	oprintf(fp, "Entry Point Checksum: 0x%x\n", ep.smbe_ecksum);
155 	oprintf(fp, "Entry Point Length: %u\n", ep.smbe_elen);
156 	oprintf(fp, "Entry Point Version: %u.%u\n",
157 	    ep.smbe_major, ep.smbe_minor);
158 	oprintf(fp, "Max Structure Size: %u\n", ep.smbe_maxssize);
159 	oprintf(fp, "Entry Point Revision: 0x%x\n", ep.smbe_revision);
160 
161 	oprintf(fp, "Entry Point Revision Data:");
162 	for (i = 0; i < sizeof (ep.smbe_format); i++)
163 		oprintf(fp, " 0x%02x", ep.smbe_format[i]);
164 	oprintf(fp, "\n");
165 
166 	oprintf(fp, "Intermediate Anchor Tag: %*.*s\n",
167 	    (int)sizeof (ep.smbe_ianchor), (int)sizeof (ep.smbe_ianchor),
168 	    ep.smbe_ianchor);
169 
170 	oprintf(fp, "Intermediate Checksum: 0x%x\n", ep.smbe_icksum);
171 	oprintf(fp, "Structure Table Length: %u\n", ep.smbe_stlen);
172 	oprintf(fp, "Structure Table Address: 0x%x\n", ep.smbe_staddr);
173 	oprintf(fp, "Structure Table Entries: %u\n", ep.smbe_stnum);
174 	oprintf(fp, "DMI BCD Revision: 0x%x\n", ep.smbe_bcdrev);
175 }
176 
177 static void
178 print_common(const smbios_info_t *ip, FILE *fp)
179 {
180 	if (ip->smbi_manufacturer[0] != '\0')
181 		oprintf(fp, "  Manufacturer: %s\n", ip->smbi_manufacturer);
182 	if (ip->smbi_product[0] != '\0')
183 		oprintf(fp, "  Product: %s\n", ip->smbi_product);
184 	if (ip->smbi_version[0] != '\0')
185 		oprintf(fp, "  Version: %s\n", ip->smbi_version);
186 	if (ip->smbi_serial[0] != '\0')
187 		oprintf(fp, "  Serial Number: %s\n", ip->smbi_serial);
188 	if (ip->smbi_asset[0] != '\0')
189 		oprintf(fp, "  Asset Tag: %s\n", ip->smbi_asset);
190 	if (ip->smbi_location[0] != '\0')
191 		oprintf(fp, "  Location Tag: %s\n", ip->smbi_location);
192 	if (ip->smbi_part[0] != '\0')
193 		oprintf(fp, "  Part Number: %s\n", ip->smbi_part);
194 }
195 
196 static void
197 print_bios(smbios_hdl_t *shp, FILE *fp)
198 {
199 	smbios_bios_t b;
200 
201 	(void) smbios_info_bios(shp, &b);
202 
203 	oprintf(fp, "  Vendor: %s\n", b.smbb_vendor);
204 	oprintf(fp, "  Version String: %s\n", b.smbb_version);
205 	oprintf(fp, "  Release Date: %s\n", b.smbb_reldate);
206 	oprintf(fp, "  Address Segment: 0x%x\n", b.smbb_segment);
207 	oprintf(fp, "  ROM Size: %u bytes\n", b.smbb_romsize);
208 	oprintf(fp, "  Image Size: %u bytes\n", b.smbb_runsize);
209 
210 	flag64_printf(fp, "Characteristics",
211 	    b.smbb_cflags, sizeof (b.smbb_cflags) * NBBY,
212 	    smbios_bios_flag_name, smbios_bios_flag_desc);
213 
214 	if (b.smbb_nxcflags > SMB_BIOSXB_1) {
215 		flag_printf(fp, "Characteristics Extension Byte 1",
216 		    b.smbb_xcflags[SMB_BIOSXB_1],
217 		    sizeof (b.smbb_xcflags[SMB_BIOSXB_1]) * NBBY,
218 		    smbios_bios_xb1_name, smbios_bios_xb1_desc);
219 	}
220 
221 	if (b.smbb_nxcflags > SMB_BIOSXB_2) {
222 		flag_printf(fp, "Characteristics Extension Byte 2",
223 		    b.smbb_xcflags[SMB_BIOSXB_2],
224 		    sizeof (b.smbb_xcflags[SMB_BIOSXB_2]) * NBBY,
225 		    smbios_bios_xb2_name, smbios_bios_xb2_desc);
226 	}
227 
228 	if (b.smbb_nxcflags > SMB_BIOSXB_BIOS_MIN) {
229 		oprintf(fp, "  Version Number: %u.%u\n",
230 		    b.smbb_biosv.smbv_major, b.smbb_biosv.smbv_minor);
231 	}
232 
233 	if (b.smbb_nxcflags > SMB_BIOSXB_ECFW_MIN) {
234 		oprintf(fp, "  Embedded Ctlr Firmware Version Number: %u.%u\n",
235 		    b.smbb_ecfwv.smbv_major, b.smbb_ecfwv.smbv_minor);
236 	}
237 }
238 
239 static void
240 print_system(smbios_hdl_t *shp, FILE *fp)
241 {
242 	smbios_system_t s;
243 	uint_t i;
244 
245 	(void) smbios_info_system(shp, &s);
246 
247 	oprintf(fp, "  UUID: ");
248 	for (i = 0; i < s.smbs_uuidlen; i++) {
249 		oprintf(fp, "%02x", s.smbs_uuid[i]);
250 		if (i == 3 || i == 5 || i == 7 || i == 9)
251 			oprintf(fp, "-");
252 	}
253 	oprintf(fp, "\n");
254 
255 	desc_printf(smbios_system_wakeup_desc(s.smbs_wakeup),
256 	    fp, "  Wake-Up Event: 0x%x", s.smbs_wakeup);
257 
258 	oprintf(fp, "  SKU Number: %s\n", s.smbs_sku);
259 	oprintf(fp, "  Family: %s\n", s.smbs_family);
260 }
261 
262 static void
263 print_bboard(smbios_hdl_t *shp, id_t id, FILE *fp)
264 {
265 	smbios_bboard_t b;
266 	int chdl_cnt;
267 
268 	(void) smbios_info_bboard(shp, id, &b);
269 
270 	oprintf(fp, "  Chassis: %u\n", (uint_t)b.smbb_chassis);
271 
272 	flag_printf(fp, "Flags", b.smbb_flags, sizeof (b.smbb_flags) * NBBY,
273 	    smbios_bboard_flag_name, smbios_bboard_flag_desc);
274 
275 	desc_printf(smbios_bboard_type_desc(b.smbb_type),
276 	    fp, "  Board Type: 0x%x", b.smbb_type);
277 
278 	chdl_cnt = b.smbb_contn;
279 	if (chdl_cnt != 0) {
280 		id_t *chdl;
281 		uint16_t hdl;
282 		int i, n, cnt;
283 
284 		chdl = alloca(chdl_cnt * sizeof (id_t));
285 		cnt = smbios_info_contains(shp, id, chdl_cnt, chdl);
286 		if (cnt > SMB_CONT_MAX)
287 			return;
288 		n = MIN(chdl_cnt, cnt);
289 
290 		oprintf(fp, "\n");
291 		for (i = 0; i < n; i++) {
292 			hdl = (uint16_t)chdl[i];
293 			oprintf(fp, "  Contained Handle: %u\n", hdl);
294 		}
295 	}
296 }
297 
298 static void
299 print_chassis(smbios_hdl_t *shp, id_t id, FILE *fp)
300 {
301 	smbios_chassis_t c;
302 	int elem_cnt;
303 
304 	(void) smbios_info_chassis(shp, id, &c);
305 
306 	oprintf(fp, "  OEM Data: 0x%x\n", c.smbc_oemdata);
307 	oprintf(fp, "  Lock Present: %s\n", c.smbc_lock ? "Y" : "N");
308 
309 	desc_printf(smbios_chassis_type_desc(c.smbc_type),
310 	    fp, "  Chassis Type: 0x%x", c.smbc_type);
311 
312 	desc_printf(smbios_chassis_state_desc(c.smbc_bustate),
313 	    fp, "  Boot-Up State: 0x%x", c.smbc_bustate);
314 
315 	desc_printf(smbios_chassis_state_desc(c.smbc_psstate),
316 	    fp, "  Power Supply State: 0x%x", c.smbc_psstate);
317 
318 	desc_printf(smbios_chassis_state_desc(c.smbc_thstate),
319 	    fp, "  Thermal State: 0x%x", c.smbc_thstate);
320 
321 	oprintf(fp, "  Chassis Height: %uu\n", c.smbc_uheight);
322 	oprintf(fp, "  Power Cords: %u\n", c.smbc_cords);
323 
324 	elem_cnt = c.smbc_elems;
325 	oprintf(fp, "  Element Records: %u\n", elem_cnt);
326 
327 	if (elem_cnt > 0) {
328 		id_t *elems;
329 		uint8_t type;
330 		int i, n, cnt;
331 
332 		elems = alloca(c.smbc_elems * sizeof (id_t));
333 		cnt = smbios_info_contains(shp, id, elem_cnt, elems);
334 		if (cnt > SMB_CONT_MAX)
335 			return;
336 		n = MIN(elem_cnt, cnt);
337 
338 		oprintf(fp, "\n");
339 		for (i = 0; i < n; i++) {
340 			type = (uint8_t)elems[i];
341 			if (type & 0x80) {
342 				/* SMBIOS structrure Type */
343 				desc_printf(smbios_type_name(type & 0x7f), fp,
344 				    "  Contained SMBIOS structure Type: %u",
345 				    type & 0x80);
346 			} else {
347 				/* SMBIOS Base Board Type */
348 				desc_printf(smbios_bboard_type_desc(type), fp,
349 				    "  Contained SMBIOS Base Board Type: 0x%x",
350 				    type);
351 			}
352 		}
353 	}
354 }
355 
356 static void
357 print_processor(smbios_hdl_t *shp, id_t id, FILE *fp)
358 {
359 	smbios_processor_t p;
360 	uint_t status;
361 
362 	(void) smbios_info_processor(shp, id, &p);
363 	status = SMB_PRSTATUS_STATUS(p.smbp_status);
364 
365 	desc_printf(smbios_processor_family_desc(p.smbp_family),
366 	    fp, "  Family: %u", p.smbp_family);
367 
368 	oprintf(fp, "  CPUID: 0x%llx\n", (u_longlong_t)p.smbp_cpuid);
369 
370 	desc_printf(smbios_processor_type_desc(p.smbp_type),
371 	    fp, "  Type: %u", p.smbp_type);
372 
373 	desc_printf(smbios_processor_upgrade_desc(p.smbp_upgrade),
374 	    fp, "  Socket Upgrade: %u", p.smbp_upgrade);
375 
376 	oprintf(fp, "  Socket Status: %s\n",
377 	    SMB_PRSTATUS_PRESENT(p.smbp_status) ?
378 	    "Populated" : "Not Populated");
379 
380 	desc_printf(smbios_processor_status_desc(status),
381 	    fp, "  Processor Status: %u", status);
382 
383 	if (SMB_PRV_LEGACY(p.smbp_voltage)) {
384 		oprintf(fp, "  Supported Voltages:");
385 		switch (p.smbp_voltage) {
386 		case SMB_PRV_5V:
387 			oprintf(fp, " 5.0V");
388 			break;
389 		case SMB_PRV_33V:
390 			oprintf(fp, " 3.3V");
391 			break;
392 		case SMB_PRV_29V:
393 			oprintf(fp, " 2.9V");
394 			break;
395 		}
396 		oprintf(fp, "\n");
397 	} else {
398 		oprintf(fp, "  Supported Voltages: %.1fV\n",
399 		    (float)SMB_PRV_VOLTAGE(p.smbp_voltage) / 10);
400 	}
401 
402 	if (p.smbp_clkspeed != 0)
403 		oprintf(fp, "  External Clock Speed: %uMHz\n", p.smbp_clkspeed);
404 	else
405 		oprintf(fp, "  External Clock Speed: Unknown\n");
406 
407 	if (p.smbp_maxspeed != 0)
408 		oprintf(fp, "  Maximum Speed: %uMHz\n", p.smbp_maxspeed);
409 	else
410 		oprintf(fp, "  Maximum Speed: Unknown\n");
411 
412 	if (p.smbp_curspeed != 0)
413 		oprintf(fp, "  Current Speed: %uMHz\n", p.smbp_curspeed);
414 	else
415 		oprintf(fp, "  Current Speed: Unknown\n");
416 
417 	id_printf(fp, "  L1 Cache: ", p.smbp_l1cache);
418 	id_printf(fp, "  L2 Cache: ", p.smbp_l2cache);
419 	id_printf(fp, "  L3 Cache: ", p.smbp_l3cache);
420 }
421 
422 static void
423 print_cache(smbios_hdl_t *shp, id_t id, FILE *fp)
424 {
425 	smbios_cache_t c;
426 
427 	(void) smbios_info_cache(shp, id, &c);
428 
429 	oprintf(fp, "  Level: %u\n", c.smba_level);
430 	oprintf(fp, "  Maximum Installed Size: %u bytes\n", c.smba_maxsize);
431 
432 	if (c.smba_size != 0)
433 		oprintf(fp, "  Installed Size: %u bytes\n", c.smba_size);
434 	else
435 		oprintf(fp, "  Installed Size: Not Installed\n");
436 
437 	if (c.smba_speed != 0)
438 		oprintf(fp, "  Speed: %uns\n", c.smba_speed);
439 	else
440 		oprintf(fp, "  Speed: Unknown\n");
441 
442 	flag_printf(fp, "Supported SRAM Types",
443 	    c.smba_stype, sizeof (c.smba_stype) * NBBY,
444 	    smbios_cache_ctype_name, smbios_cache_ctype_desc);
445 
446 	desc_printf(smbios_cache_ctype_desc(c.smba_ctype),
447 	    fp, "  Current SRAM Type: 0x%x", c.smba_ctype);
448 
449 	desc_printf(smbios_cache_ecc_desc(c.smba_etype),
450 	    fp, "  Error Correction Type: %u", c.smba_etype);
451 
452 	desc_printf(smbios_cache_logical_desc(c.smba_ltype),
453 	    fp, "  Logical Cache Type: %u", c.smba_ltype);
454 
455 	desc_printf(smbios_cache_assoc_desc(c.smba_assoc),
456 	    fp, "  Associativity: %u", c.smba_assoc);
457 
458 	desc_printf(smbios_cache_mode_desc(c.smba_mode),
459 	    fp, "  Mode: %u", c.smba_mode);
460 
461 	desc_printf(smbios_cache_loc_desc(c.smba_location),
462 	    fp, "  Location: %u", c.smba_location);
463 
464 	flag_printf(fp, "Flags", c.smba_flags, sizeof (c.smba_flags) * NBBY,
465 	    smbios_cache_flag_name, smbios_cache_flag_desc);
466 }
467 
468 static void
469 print_port(smbios_hdl_t *shp, id_t id, FILE *fp)
470 {
471 	smbios_port_t p;
472 
473 	(void) smbios_info_port(shp, id, &p);
474 
475 	oprintf(fp, "  Internal Reference Designator: %s\n", p.smbo_iref);
476 	oprintf(fp, "  External Reference Designator: %s\n", p.smbo_eref);
477 
478 	desc_printf(smbios_port_conn_desc(p.smbo_itype),
479 	    fp, "  Internal Connector Type: %u", p.smbo_itype);
480 
481 	desc_printf(smbios_port_conn_desc(p.smbo_etype),
482 	    fp, "  External Connector Type: %u", p.smbo_etype);
483 
484 	desc_printf(smbios_port_type_desc(p.smbo_ptype),
485 	    fp, "  Port Type: %u", p.smbo_ptype);
486 }
487 
488 static void
489 print_slot(smbios_hdl_t *shp, id_t id, FILE *fp)
490 {
491 	smbios_slot_t s;
492 
493 	(void) smbios_info_slot(shp, id, &s);
494 
495 	oprintf(fp, "  Reference Designator: %s\n", s.smbl_name);
496 	oprintf(fp, "  Slot ID: 0x%x\n", s.smbl_id);
497 
498 	desc_printf(smbios_slot_type_desc(s.smbl_type),
499 	    fp, "  Type: 0x%x", s.smbl_type);
500 
501 	desc_printf(smbios_slot_width_desc(s.smbl_width),
502 	    fp, "  Width: 0x%x", s.smbl_width);
503 
504 	desc_printf(smbios_slot_usage_desc(s.smbl_usage),
505 	    fp, "  Usage: 0x%x", s.smbl_usage);
506 
507 	desc_printf(smbios_slot_length_desc(s.smbl_length),
508 	    fp, "  Length: 0x%x", s.smbl_length);
509 
510 	flag_printf(fp, "Slot Characteristics 1",
511 	    s.smbl_ch1, sizeof (s.smbl_ch1) * NBBY,
512 	    smbios_slot_ch1_name, smbios_slot_ch1_desc);
513 
514 	flag_printf(fp, "Slot Characteristics 2",
515 	    s.smbl_ch2, sizeof (s.smbl_ch2) * NBBY,
516 	    smbios_slot_ch2_name, smbios_slot_ch2_desc);
517 }
518 
519 static void
520 print_obdevs(smbios_hdl_t *shp, id_t id, FILE *fp)
521 {
522 	smbios_obdev_t *argv;
523 	int i, argc;
524 
525 	if ((argc = smbios_info_obdevs(shp, id, 0, NULL)) > 0) {
526 		argv = alloca(sizeof (smbios_obdev_t) * argc);
527 		(void) smbios_info_obdevs(shp, id, argc, argv);
528 		for (i = 0; i < argc; i++)
529 			oprintf(fp, "  %s\n", argv[i].smbd_name);
530 	}
531 }
532 
533 static void
534 print_strtab(smbios_hdl_t *shp, id_t id, FILE *fp)
535 {
536 	const char **argv;
537 	int i, argc;
538 
539 	if ((argc = smbios_info_strtab(shp, id, 0, NULL)) > 0) {
540 		argv = alloca(sizeof (char *) * argc);
541 		(void) smbios_info_strtab(shp, id, argc, argv);
542 		for (i = 0; i < argc; i++)
543 			oprintf(fp, "  %s\n", argv[i]);
544 	}
545 }
546 
547 static void
548 print_lang(smbios_hdl_t *shp, id_t id, FILE *fp)
549 {
550 	smbios_lang_t l;
551 
552 	(void) smbios_info_lang(shp, &l);
553 
554 	oprintf(fp, "  Current Language: %s\n", l.smbla_cur);
555 	oprintf(fp, "  Language String Format: %u\n", l.smbla_fmt);
556 	oprintf(fp, "  Number of Installed Languages: %u\n", l.smbla_num);
557 	oprintf(fp, "  Installed Languages:\n");
558 
559 	print_strtab(shp, id, fp);
560 }
561 
562 /*ARGSUSED*/
563 static void
564 print_evlog(smbios_hdl_t *shp, id_t id, FILE *fp)
565 {
566 	smbios_evlog_t ev;
567 	uint32_t i;
568 
569 	(void) smbios_info_eventlog(shp, &ev);
570 
571 	oprintf(fp, "  Log Area Size: %lu bytes\n", (ulong_t)ev.smbev_size);
572 	oprintf(fp, "  Header Offset: %lu\n", (ulong_t)ev.smbev_hdr);
573 	oprintf(fp, "  Data Offset: %lu\n", (ulong_t)ev.smbev_data);
574 
575 	desc_printf(smbios_evlog_method_desc(ev.smbev_method),
576 	    fp, "  Data Access Method: %u", ev.smbev_method);
577 
578 	flag_printf(fp, "Log Flags",
579 	    ev.smbev_flags, sizeof (ev.smbev_flags) * NBBY,
580 	    smbios_evlog_flag_name, smbios_evlog_flag_desc);
581 
582 	desc_printf(smbios_evlog_format_desc(ev.smbev_format),
583 	    fp, "  Log Header Format: %u", ev.smbev_format);
584 
585 	oprintf(fp, "  Update Token: 0x%x\n", ev.smbev_token);
586 	oprintf(fp, "  Data Access Address: ");
587 
588 	switch (ev.smbev_method) {
589 	case SMB_EVM_1x1i_1x1d:
590 	case SMB_EVM_2x1i_1x1d:
591 	case SMB_EVM_1x2i_1x1d:
592 		oprintf(fp, "Index Address 0x%x, Data Address 0x%x\n",
593 		    ev.smbev_addr.eva_io.evi_iaddr,
594 		    ev.smbev_addr.eva_io.evi_daddr);
595 		break;
596 	case SMB_EVM_GPNV:
597 		oprintf(fp, "0x%x\n", ev.smbev_addr.eva_gpnv);
598 		break;
599 	default:
600 		oprintf(fp, "0x%x\n", ev.smbev_addr.eva_addr);
601 	}
602 
603 	oprintf(fp, "  Type Descriptors:\n");
604 
605 	for (i = 0; i < ev.smbev_typec; i++) {
606 		oprintf(fp, "  %u: Log Type 0x%x, Data Type 0x%x\n", i,
607 		    ev.smbev_typev[i].smbevt_ltype,
608 		    ev.smbev_typev[i].smbevt_dtype);
609 	}
610 }
611 
612 static void
613 print_bytes(const uint8_t *data, size_t size, FILE *fp)
614 {
615 	size_t row, rows = P2ROUNDUP(size, 16) / 16;
616 	size_t col, cols;
617 
618 	char buf[17];
619 	uint8_t x;
620 
621 	oprintf(fp, "\n  offset:   0 1 2 3  4 5 6 7  8 9 a b  c d e f  "
622 	    "0123456789abcdef\n");
623 
624 	for (row = 0; row < rows; row++) {
625 		oprintf(fp, "    %#4lx: ", (ulong_t)row * 16);
626 		cols = MIN(size - row * 16, 16);
627 
628 		for (col = 0; col < cols; col++) {
629 			if (col % 4 == 0)
630 				oprintf(fp, " ");
631 			x = *data++;
632 			oprintf(fp, "%02x", x);
633 			buf[col] = x <= ' ' || x > '~' ? '.' : x;
634 		}
635 
636 		for (; col < 16; col++) {
637 			if (col % 4 == 0)
638 				oprintf(fp, " ");
639 			oprintf(fp, "  ");
640 			buf[col] = ' ';
641 		}
642 
643 		buf[col] = '\0';
644 		oprintf(fp, "  %s\n", buf);
645 	}
646 
647 	oprintf(fp, "\n");
648 }
649 
650 static void
651 print_memarray(smbios_hdl_t *shp, id_t id, FILE *fp)
652 {
653 	smbios_memarray_t ma;
654 
655 	(void) smbios_info_memarray(shp, id, &ma);
656 
657 	desc_printf(smbios_memarray_loc_desc(ma.smbma_location),
658 	    fp, "  Location: %u", ma.smbma_location);
659 
660 	desc_printf(smbios_memarray_use_desc(ma.smbma_use),
661 	    fp, "  Use: %u", ma.smbma_use);
662 
663 	desc_printf(smbios_memarray_ecc_desc(ma.smbma_ecc),
664 	    fp, "  ECC: %u", ma.smbma_ecc);
665 
666 	oprintf(fp, "  Number of Slots/Sockets: %u\n", ma.smbma_ndevs);
667 	id_printf(fp, "  Memory Error Data: ", ma.smbma_err);
668 	oprintf(fp, "  Max Capacity: %llu bytes\n",
669 	    (u_longlong_t)ma.smbma_size);
670 }
671 
672 static void
673 print_memdevice(smbios_hdl_t *shp, id_t id, FILE *fp)
674 {
675 	smbios_memdevice_t md;
676 
677 	(void) smbios_info_memdevice(shp, id, &md);
678 
679 	id_printf(fp, "  Physical Memory Array: ", md.smbmd_array);
680 	id_printf(fp, "  Memory Error Data: ", md.smbmd_error);
681 
682 	if (md.smbmd_twidth != -1u)
683 		oprintf(fp, "  Total Width: %u bits\n", md.smbmd_twidth);
684 	else
685 		oprintf(fp, "  Total Width: Unknown\n");
686 
687 	if (md.smbmd_dwidth != -1u)
688 		oprintf(fp, "  Data Width: %u bits\n", md.smbmd_dwidth);
689 	else
690 		oprintf(fp, "  Data Width: Unknown\n");
691 
692 	switch (md.smbmd_size) {
693 	case -1ull:
694 		oprintf(fp, "  Size: Unknown\n");
695 		break;
696 	case 0:
697 		oprintf(fp, "  Size: Not Populated\n");
698 		break;
699 	default:
700 		oprintf(fp, "  Size: %llu bytes\n",
701 		    (u_longlong_t)md.smbmd_size);
702 	}
703 
704 	desc_printf(smbios_memdevice_form_desc(md.smbmd_form),
705 	    fp, "  Form Factor: %u", md.smbmd_form);
706 
707 	if (md.smbmd_set == 0)
708 		oprintf(fp, "  Set: None\n");
709 	else if (md.smbmd_set == (uint8_t)-1u)
710 		oprintf(fp, "  Set: Unknown\n");
711 	else
712 		oprintf(fp, "  Set: %u\n", md.smbmd_set);
713 
714 	desc_printf(smbios_memdevice_type_desc(md.smbmd_type),
715 	    fp, "  Memory Type: %u", md.smbmd_type);
716 
717 	flag_printf(fp, "Flags", md.smbmd_flags, sizeof (md.smbmd_flags) * NBBY,
718 	    smbios_memdevice_flag_name, smbios_memdevice_flag_desc);
719 
720 	if (md.smbmd_speed != 0)
721 		oprintf(fp, "  Speed: %uns\n", md.smbmd_speed);
722 	else
723 		oprintf(fp, "  Speed: Unknown\n");
724 
725 	oprintf(fp, "  Device Locator: %s\n", md.smbmd_dloc);
726 	oprintf(fp, "  Bank Locator: %s\n", md.smbmd_bloc);
727 }
728 
729 static void
730 print_memarrmap(smbios_hdl_t *shp, id_t id, FILE *fp)
731 {
732 	smbios_memarrmap_t ma;
733 
734 	(void) smbios_info_memarrmap(shp, id, &ma);
735 
736 	id_printf(fp, "  Physical Memory Array: ", ma.smbmam_array);
737 	oprintf(fp, "  Devices per Row: %u\n", ma.smbmam_width);
738 
739 	oprintf(fp, "  Physical Address: 0x%llx\n  Size: %llu bytes\n",
740 	    (u_longlong_t)ma.smbmam_addr, (u_longlong_t)ma.smbmam_size);
741 }
742 
743 static void
744 print_memdevmap(smbios_hdl_t *shp, id_t id, FILE *fp)
745 {
746 	smbios_memdevmap_t md;
747 
748 	(void) smbios_info_memdevmap(shp, id, &md);
749 
750 	id_printf(fp, "  Memory Device: ", md.smbmdm_device);
751 	id_printf(fp, "  Memory Array Mapped Address: ", md.smbmdm_arrmap);
752 
753 	oprintf(fp, "  Physical Address: 0x%llx\n  Size: %llu bytes\n",
754 	    (u_longlong_t)md.smbmdm_addr, (u_longlong_t)md.smbmdm_size);
755 
756 	oprintf(fp, "  Partition Row Position: %u\n", md.smbmdm_rpos);
757 	oprintf(fp, "  Interleave Position: %u\n", md.smbmdm_ipos);
758 	oprintf(fp, "  Interleave Data Depth: %u\n", md.smbmdm_idepth);
759 }
760 
761 static void
762 print_hwsec(smbios_hdl_t *shp, FILE *fp)
763 {
764 	smbios_hwsec_t h;
765 
766 	(void) smbios_info_hwsec(shp, &h);
767 
768 	desc_printf(smbios_hwsec_desc(h.smbh_pwr_ps),
769 	    fp, "  Power-On Password Status: %u", h.smbh_pwr_ps);
770 	desc_printf(smbios_hwsec_desc(h.smbh_kbd_ps),
771 	    fp, "  Keyboard Password Status: %u", h.smbh_kbd_ps);
772 	desc_printf(smbios_hwsec_desc(h.smbh_adm_ps),
773 	    fp, "  Administrator Password Status: %u", h.smbh_adm_ps);
774 	desc_printf(smbios_hwsec_desc(h.smbh_pan_ps),
775 	    fp, "  Front Panel Reset Status: %u", h.smbh_pan_ps);
776 }
777 
778 static void
779 print_boot(smbios_hdl_t *shp, FILE *fp)
780 {
781 	smbios_boot_t b;
782 
783 	(void) smbios_info_boot(shp, &b);
784 
785 	desc_printf(smbios_boot_desc(b.smbt_status),
786 	    fp, "  Boot Status Code: 0x%x", b.smbt_status);
787 
788 	if (b.smbt_size != 0) {
789 		oprintf(fp, "  Boot Data (%lu bytes):\n", (ulong_t)b.smbt_size);
790 		print_bytes(b.smbt_data, b.smbt_size, fp);
791 	}
792 }
793 
794 static void
795 print_ipmi(smbios_hdl_t *shp, FILE *fp)
796 {
797 	smbios_ipmi_t i;
798 
799 	(void) smbios_info_ipmi(shp, &i);
800 
801 	desc_printf(smbios_ipmi_type_desc(i.smbip_type),
802 	    fp, "  Type: %u", i.smbip_type);
803 
804 	oprintf(fp, "  BMC IPMI Version: %u.%u\n",
805 	    i.smbip_vers.smbv_major, i.smbip_vers.smbv_minor);
806 
807 	oprintf(fp, "  i2c Bus Slave Address: 0x%x\n", i.smbip_i2c);
808 	oprintf(fp, "  NV Storage Device Bus ID: 0x%x\n", i.smbip_bus);
809 	oprintf(fp, "  BMC Base Address: 0x%llx\n", (u_longlong_t)i.smbip_addr);
810 	oprintf(fp, "  Interrupt Number: %u\n", i.smbip_intr);
811 	oprintf(fp, "  Register Spacing: %u\n", i.smbip_regspacing);
812 
813 	flag_printf(fp, "Flags", i.smbip_flags, sizeof (i.smbip_flags) * NBBY,
814 	    smbios_ipmi_flag_name, smbios_ipmi_flag_desc);
815 }
816 
817 static int
818 check_oem(smbios_hdl_t *shp)
819 {
820 	int i;
821 	int cnt;
822 	int rv;
823 	id_t oem_id;
824 	smbios_struct_t s;
825 	const char **oem_str;
826 
827 	rv = smbios_lookup_type(shp, SMB_TYPE_OEMSTR, &s);
828 	if (rv != 0) {
829 		return (-1);
830 	}
831 
832 	oem_id = s.smbstr_id;
833 
834 	cnt = smbios_info_strtab(shp, oem_id, 0, NULL);
835 	if (cnt > 0) {
836 		oem_str =  alloca(sizeof (char *) * cnt);
837 		(void) smbios_info_strtab(shp, oem_id, cnt, oem_str);
838 
839 		for (i = 0; i < cnt; i++) {
840 			if (strncmp(oem_str[i], SMB_PRMS1,
841 			    strlen(SMB_PRMS1) + 1) == 0) {
842 				return (0);
843 			}
844 		}
845 	}
846 
847 	return (-1);
848 }
849 
850 static void
851 print_extprocessor(smbios_hdl_t *shp, id_t id, FILE *fp)
852 {
853 	int i;
854 	smbios_processor_ext_t ep;
855 
856 	if (check_oem(shp) != 0)
857 		return;
858 
859 	(void) smbios_info_extprocessor(shp, id, &ep);
860 
861 	oprintf(fp, "  Processor: %u\n", ep.smbpe_processor);
862 	oprintf(fp, "  FRU: %u\n", ep.smbpe_fru);
863 	oprintf(fp, "  Initial APIC ID count: %u\n\n", ep.smbpe_n);
864 
865 	for (i = 0; i < ep.smbpe_n; i++) {
866 		oprintf(fp, "  Logical Strand %u: Initial APIC ID: %u\n", i,
867 		    ep.smbpe_apicid[i]);
868 	}
869 }
870 
871 static void
872 print_pciexrc(smbios_hdl_t *shp, id_t id, FILE *fp)
873 {
874 	smbios_pciexrc_t pcie;
875 
876 	if (check_oem(shp) != 0)
877 		return;
878 
879 	(void) smbios_info_pciexrc(shp, id, &pcie);
880 
881 	oprintf(fp, "  Component ID: %u\n", pcie.smbpcie_bb);
882 	oprintf(fp, "  BDF: 0x%x\n", pcie.smbpcie_bdf);
883 }
884 
885 static void
886 print_extmemarray(smbios_hdl_t *shp, id_t id, FILE *fp)
887 {
888 	smbios_memarray_ext_t em;
889 
890 	if (check_oem(shp) != 0)
891 		return;
892 
893 	(void) smbios_info_extmemarray(shp, id, &em);
894 
895 	oprintf(fp, "  Physical Memory Array Handle: %u\n", em.smbmae_ma);
896 	oprintf(fp, "  Component Parent Handle: %u\n", em.smbmae_comp);
897 	oprintf(fp, "  BDF: 0x%x\n", em.smbmae_bdf);
898 }
899 
900 static void
901 print_extmemdevice(smbios_hdl_t *shp, id_t id, FILE *fp)
902 {
903 	int i;
904 	smbios_memdevice_ext_t emd;
905 
906 	if (check_oem(shp) != 0)
907 		return;
908 
909 	(void) smbios_info_extmemdevice(shp, id, &emd);
910 
911 	oprintf(fp, "  Memory Device Handle: %u\n", emd.smbmdeve_md);
912 	oprintf(fp, "  DRAM Channel: %u\n", emd.smbmdeve_drch);
913 	oprintf(fp, "  Number of Chip Selects: %u\n", emd.smbmdeve_ncs);
914 
915 	for (i = 0; i < emd.smbmdeve_ncs; i++) {
916 		oprintf(fp, "  Chip Select: %u\n", emd.smbmdeve_cs[i]);
917 	}
918 }
919 
920 static int
921 print_struct(smbios_hdl_t *shp, const smbios_struct_t *sp, void *fp)
922 {
923 	smbios_info_t info;
924 	int hex = opt_x;
925 	const char *s;
926 
927 	if (opt_t != -1 && opt_t != sp->smbstr_type)
928 		return (0); /* skip struct if type doesn't match -t */
929 
930 	if (!opt_O && (sp->smbstr_type == SMB_TYPE_MEMCTL ||
931 	    sp->smbstr_type == SMB_TYPE_MEMMOD))
932 		return (0); /* skip struct if type is obsolete */
933 
934 	if (g_hdr++ == 0 || !opt_s)
935 		oprintf(fp, "%-5s %-4s %s\n", "ID", "SIZE", "TYPE");
936 
937 	oprintf(fp, "%-5u %-4lu",
938 	    (uint_t)sp->smbstr_id, (ulong_t)sp->smbstr_size);
939 
940 	if ((s = smbios_type_name(sp->smbstr_type)) != NULL)
941 		oprintf(fp, " %s", s);
942 	else if (sp->smbstr_type > SMB_TYPE_OEM_LO &&
943 	    sp->smbstr_type < SMB_TYPE_OEM_HI)
944 		oprintf(fp, " %s+%u", "SMB_TYPE_OEM_LO",
945 		    sp->smbstr_type - SMB_TYPE_OEM_LO);
946 	else
947 		oprintf(fp, " %u", sp->smbstr_type);
948 
949 	if ((s = smbios_type_desc(sp->smbstr_type)) != NULL)
950 		oprintf(fp, " (%s)\n", s);
951 	else
952 		oprintf(fp, "\n");
953 
954 	if (opt_s)
955 		return (0); /* only print header line if -s specified */
956 
957 	if (smbios_info_common(shp, sp->smbstr_id, &info) == 0) {
958 		oprintf(fp, "\n");
959 		print_common(&info, fp);
960 	}
961 
962 	switch (sp->smbstr_type) {
963 	case SMB_TYPE_BIOS:
964 		oprintf(fp, "\n");
965 		print_bios(shp, fp);
966 		break;
967 	case SMB_TYPE_SYSTEM:
968 		oprintf(fp, "\n");
969 		print_system(shp, fp);
970 		break;
971 	case SMB_TYPE_BASEBOARD:
972 		oprintf(fp, "\n");
973 		print_bboard(shp, sp->smbstr_id, fp);
974 		break;
975 	case SMB_TYPE_CHASSIS:
976 		oprintf(fp, "\n");
977 		print_chassis(shp, sp->smbstr_id, fp);
978 		break;
979 	case SMB_TYPE_PROCESSOR:
980 		oprintf(fp, "\n");
981 		print_processor(shp, sp->smbstr_id, fp);
982 		break;
983 	case SMB_TYPE_CACHE:
984 		oprintf(fp, "\n");
985 		print_cache(shp, sp->smbstr_id, fp);
986 		break;
987 	case SMB_TYPE_PORT:
988 		oprintf(fp, "\n");
989 		print_port(shp, sp->smbstr_id, fp);
990 		break;
991 	case SMB_TYPE_SLOT:
992 		oprintf(fp, "\n");
993 		print_slot(shp, sp->smbstr_id, fp);
994 		break;
995 	case SMB_TYPE_OBDEVS:
996 		oprintf(fp, "\n");
997 		print_obdevs(shp, sp->smbstr_id, fp);
998 		break;
999 	case SMB_TYPE_OEMSTR:
1000 	case SMB_TYPE_SYSCONFSTR:
1001 		oprintf(fp, "\n");
1002 		print_strtab(shp, sp->smbstr_id, fp);
1003 		break;
1004 	case SMB_TYPE_LANG:
1005 		oprintf(fp, "\n");
1006 		print_lang(shp, sp->smbstr_id, fp);
1007 		break;
1008 	case SMB_TYPE_EVENTLOG:
1009 		oprintf(fp, "\n");
1010 		print_evlog(shp, sp->smbstr_id, fp);
1011 		break;
1012 	case SMB_TYPE_MEMARRAY:
1013 		oprintf(fp, "\n");
1014 		print_memarray(shp, sp->smbstr_id, fp);
1015 		break;
1016 	case SMB_TYPE_MEMDEVICE:
1017 		oprintf(fp, "\n");
1018 		print_memdevice(shp, sp->smbstr_id, fp);
1019 		break;
1020 	case SMB_TYPE_MEMARRAYMAP:
1021 		oprintf(fp, "\n");
1022 		print_memarrmap(shp, sp->smbstr_id, fp);
1023 		break;
1024 	case SMB_TYPE_MEMDEVICEMAP:
1025 		oprintf(fp, "\n");
1026 		print_memdevmap(shp, sp->smbstr_id, fp);
1027 		break;
1028 	case SMB_TYPE_SECURITY:
1029 		oprintf(fp, "\n");
1030 		print_hwsec(shp, fp);
1031 		break;
1032 	case SMB_TYPE_BOOT:
1033 		oprintf(fp, "\n");
1034 		print_boot(shp, fp);
1035 		break;
1036 	case SMB_TYPE_IPMIDEV:
1037 		oprintf(fp, "\n");
1038 		print_ipmi(shp, fp);
1039 		break;
1040 	case SUN_OEM_EXT_PROCESSOR:
1041 		oprintf(fp, "\n");
1042 		print_extprocessor(shp, sp->smbstr_id, fp);
1043 		break;
1044 	case SUN_OEM_PCIEXRC:
1045 		oprintf(fp, "\n");
1046 		print_pciexrc(shp, sp->smbstr_id, fp);
1047 		break;
1048 	case SUN_OEM_EXT_MEMARRAY:
1049 		oprintf(fp, "\n");
1050 		print_extmemarray(shp, sp->smbstr_id, fp);
1051 		break;
1052 	case SUN_OEM_EXT_MEMDEVICE:
1053 		oprintf(fp, "\n");
1054 		print_extmemdevice(shp, sp->smbstr_id, fp);
1055 		break;
1056 	default:
1057 		hex++;
1058 	}
1059 
1060 	if (hex)
1061 		print_bytes(sp->smbstr_data, sp->smbstr_size, fp);
1062 	else
1063 		oprintf(fp, "\n");
1064 
1065 	return (0);
1066 }
1067 
1068 static uint16_t
1069 getu16(const char *name, const char *s)
1070 {
1071 	u_longlong_t val;
1072 	char *p;
1073 
1074 	errno = 0;
1075 	val = strtoull(s, &p, 0);
1076 
1077 	if (errno != 0 || p == s || *p != '\0' || val > UINT16_MAX) {
1078 		(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
1079 		    g_pname, name, s);
1080 		exit(SMBIOS_USAGE);
1081 	}
1082 
1083 	return ((uint16_t)val);
1084 }
1085 
1086 static uint16_t
1087 getstype(const char *name, const char *s)
1088 {
1089 	const char *ts;
1090 	uint16_t t;
1091 
1092 	for (t = 0; t < SMB_TYPE_OEM_LO; t++) {
1093 		if ((ts = smbios_type_name(t)) != NULL && strcmp(s, ts) == 0)
1094 			return (t);
1095 	}
1096 
1097 	(void) fprintf(stderr, "%s: invalid %s argument -- %s\n",
1098 	    g_pname, name, s);
1099 
1100 	exit(SMBIOS_USAGE);
1101 	/*NOTREACHED*/
1102 }
1103 
1104 static int
1105 usage(FILE *fp)
1106 {
1107 	(void) fprintf(fp, "Usage: %s "
1108 	    "[-BeOsx] [-i id] [-t type] [-w file] [file]\n\n", g_pname);
1109 
1110 	(void) fprintf(fp,
1111 	    "\t-B disable header validation for broken BIOSes\n"
1112 	    "\t-e display SMBIOS entry point information\n"
1113 	    "\t-i display only the specified structure\n"
1114 	    "\t-O display obsolete structure types\n"
1115 	    "\t-s display only a summary of structure identifiers and types\n"
1116 	    "\t-t display only the specified structure type\n"
1117 	    "\t-w write the raw data to the specified file\n"
1118 	    "\t-x display raw data for structures\n");
1119 
1120 	return (SMBIOS_USAGE);
1121 }
1122 
1123 int
1124 main(int argc, char *argv[])
1125 {
1126 	const char *ifile = NULL;
1127 	const char *ofile = NULL;
1128 	int oflags = 0;
1129 
1130 	smbios_hdl_t *shp;
1131 	smbios_struct_t s;
1132 	int err, fd, c;
1133 	char *p;
1134 
1135 	if ((p = strrchr(argv[0], '/')) == NULL)
1136 		g_pname = argv[0];
1137 	else
1138 		g_pname = p + 1;
1139 
1140 	while (optind < argc) {
1141 		while ((c = getopt(argc, argv, "Bei:Ost:w:xZ")) != EOF) {
1142 			switch (c) {
1143 			case 'B':
1144 				oflags |= SMB_O_NOCKSUM | SMB_O_NOVERS;
1145 				break;
1146 			case 'e':
1147 				opt_e++;
1148 				break;
1149 			case 'i':
1150 				opt_i = getu16("struct ID", optarg);
1151 				break;
1152 			case 'O':
1153 				opt_O++;
1154 				break;
1155 			case 's':
1156 				opt_s++;
1157 				break;
1158 			case 't':
1159 				if (isdigit(optarg[0]))
1160 					opt_t = getu16("struct type", optarg);
1161 				else
1162 					opt_t = getstype("struct type", optarg);
1163 				break;
1164 			case 'w':
1165 				ofile = optarg;
1166 				break;
1167 			case 'x':
1168 				opt_x++;
1169 				break;
1170 			case 'Z':
1171 				oflags |= SMB_O_ZIDS; /* undocumented */
1172 				break;
1173 			default:
1174 				return (usage(stderr));
1175 			}
1176 		}
1177 
1178 		if (optind < argc) {
1179 			if (ifile != NULL) {
1180 				(void) fprintf(stderr, "%s: illegal "
1181 				    "argument -- %s\n", g_pname, argv[optind]);
1182 				return (SMBIOS_USAGE);
1183 			}
1184 			ifile = argv[optind++];
1185 		}
1186 	}
1187 
1188 	if ((shp = smbios_open(ifile, SMB_VERSION, oflags, &err)) == NULL) {
1189 		(void) fprintf(stderr, "%s: failed to load SMBIOS: %s\n",
1190 		    g_pname, smbios_errmsg(err));
1191 		return (SMBIOS_ERROR);
1192 	}
1193 
1194 	if (ofile != NULL) {
1195 		if ((fd = open(ofile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
1196 			(void) fprintf(stderr, "%s: failed to open %s: %s\n",
1197 			    g_pname, ofile, strerror(errno));
1198 			err = SMBIOS_ERROR;
1199 		} else if (smbios_write(shp, fd) != 0) {
1200 			(void) fprintf(stderr, "%s: failed to write %s: %s\n",
1201 			    g_pname, ofile, smbios_errmsg(smbios_errno(shp)));
1202 			err = SMBIOS_ERROR;
1203 		}
1204 		smbios_close(shp);
1205 		return (err);
1206 	}
1207 
1208 	if (opt_e) {
1209 		print_smbios(shp, stdout);
1210 		smbios_close(shp);
1211 		return (SMBIOS_SUCCESS);
1212 	}
1213 
1214 	if (opt_O && (opt_i != -1 || opt_t != -1))
1215 		opt_O++; /* -i or -t imply displaying obsolete records */
1216 
1217 	if (opt_i != -1)
1218 		err = smbios_lookup_id(shp, opt_i, &s);
1219 	else
1220 		err = smbios_iter(shp, print_struct, stdout);
1221 
1222 	if (err != 0) {
1223 		(void) fprintf(stderr, "%s: failed to access SMBIOS: %s\n",
1224 		    g_pname, smbios_errmsg(smbios_errno(shp)));
1225 		smbios_close(shp);
1226 		return (SMBIOS_ERROR);
1227 	}
1228 
1229 	if (opt_i != -1)
1230 		(void) print_struct(shp, &s, stdout);
1231 
1232 	smbios_close(shp);
1233 	return (SMBIOS_SUCCESS);
1234 }
1235