xref: /titanic_53/usr/src/cmd/prtfru/prtfru.c (revision 46a7ef8aa64ee52c612c9b1b02ca19542a7a2e89)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
594bc7577Sps154918  * Common Development and Distribution License (the "License").
694bc7577Sps154918  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f3af4981Sdt207653  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <alloca.h>
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
36f3af4981Sdt207653 #include <ctype.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "fru_tag.h"
397c478bd9Sstevel@tonic-gate #include "libfrup.h"
407c478bd9Sstevel@tonic-gate #include "libfrureg.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	NUM_ITER_BYTES 4
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	HEAD_ITER 0
467c478bd9Sstevel@tonic-gate #define	TAIL_ITER 1	/*  not used  */
477c478bd9Sstevel@tonic-gate #define	NUM_ITER  2
487c478bd9Sstevel@tonic-gate #define	MAX_ITER  3
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	INDENT 3
517c478bd9Sstevel@tonic-gate #define	TIMESTRINGLEN 128
5294bc7577Sps154918 #define	TEMPERATURE_OFFSET 73
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void	(*print_node)(fru_node_t fru_type, const char *path,
557c478bd9Sstevel@tonic-gate 				const char *name, end_node_fp_t *end_node,
567c478bd9Sstevel@tonic-gate 				void **end_args);
577c478bd9Sstevel@tonic-gate 
58f3af4981Sdt207653 static void	print_element(const uint8_t *data, const fru_regdef_t *def,
59f3af4981Sdt207653 const char *parent_path, int indent);
60f3af4981Sdt207653 
617c478bd9Sstevel@tonic-gate static char	tagname[sizeof ("?_0123456789_0123456789_0123456789")];
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static int	containers_only = 0, list_only = 0, saved_status = 0, xml = 0;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static FILE	*errlog;
667c478bd9Sstevel@tonic-gate 
67f3af4981Sdt207653 int iterglobal = 0;
68f3af4981Sdt207653 int FMAmessageR = -1;
69*46a7ef8aSdt207653 int Fault_Install_DataR_flag = 0;
70*46a7ef8aSdt207653 int Power_On_DataR_flag = 0;
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Definition for data elements found in devices but not found in
737c478bd9Sstevel@tonic-gate  * the system's version of libfrureg
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static fru_regdef_t  unknown = {
767c478bd9Sstevel@tonic-gate 	REGDEF_VERSION,
777c478bd9Sstevel@tonic-gate 	tagname,
787c478bd9Sstevel@tonic-gate 	-1,
797c478bd9Sstevel@tonic-gate 	-1,
807c478bd9Sstevel@tonic-gate 	-1,
817c478bd9Sstevel@tonic-gate 	-1,
827c478bd9Sstevel@tonic-gate 	FDTYPE_ByteArray,
837c478bd9Sstevel@tonic-gate 	FDISP_Hex,
847c478bd9Sstevel@tonic-gate 	FRU_WHICH_UNDEFINED,
857c478bd9Sstevel@tonic-gate 	FRU_WHICH_UNDEFINED,
867c478bd9Sstevel@tonic-gate 	0,
877c478bd9Sstevel@tonic-gate 	NULL,
887c478bd9Sstevel@tonic-gate 	0,
897c478bd9Sstevel@tonic-gate 	FRU_NOT_ITERATED,
907c478bd9Sstevel@tonic-gate 	NULL
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Write message to standard error and possibly the error log buffer
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static void
987c478bd9Sstevel@tonic-gate error(const char *format, ...)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	va_list	args;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/* make relevant output appear before error message */
1047c478bd9Sstevel@tonic-gate 	if (fflush(stdout) == EOF) {
1057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Error flushing output:  %s\n",
1067c478bd9Sstevel@tonic-gate 		    strerror(errno));
1077c478bd9Sstevel@tonic-gate 		exit(1);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	va_start(args, format);
1117c478bd9Sstevel@tonic-gate 	if (vfprintf(stderr, format, args) < 0) exit(1);
1127c478bd9Sstevel@tonic-gate 	if (errlog && (vfprintf(errlog, format, args) < 0)) exit(1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Write message to standard output
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static void
1197c478bd9Sstevel@tonic-gate output(const char *format, ...)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	va_list   args;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	va_start(args, format);
1257c478bd9Sstevel@tonic-gate 	if (vfprintf(stdout, format, args) < 0) {
1267c478bd9Sstevel@tonic-gate 		error(gettext("Error writing output:  %s\n"),
1277c478bd9Sstevel@tonic-gate 		    strerror(errno));
1287c478bd9Sstevel@tonic-gate 		exit(1);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Safe wrapper for putchar()
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate static void
1367c478bd9Sstevel@tonic-gate voidputchar(int c)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	if (putchar(c) == EOF) {
1397c478bd9Sstevel@tonic-gate 		error(gettext("Error writing output:  %s\n"),
1407c478bd9Sstevel@tonic-gate 		    strerror(errno));
1417c478bd9Sstevel@tonic-gate 		exit(1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static void  (*safeputchar)(int c) = voidputchar;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Safe wrapper for puts()
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate static void
1517c478bd9Sstevel@tonic-gate voidputs(const char *s)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	if (fputs(s, stdout) == EOF) {
1547c478bd9Sstevel@tonic-gate 		error(gettext("Error writing output:  %s\n"),
1557c478bd9Sstevel@tonic-gate 		    strerror(errno));
1567c478bd9Sstevel@tonic-gate 		exit(1);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static void  (*safeputs)(const char *s) = voidputs;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * XML-safe wrapper for putchar():  quotes XML-special characters
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static void
1667c478bd9Sstevel@tonic-gate xputchar(int c)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	switch (c) {
1697c478bd9Sstevel@tonic-gate 	case '<':
1707c478bd9Sstevel@tonic-gate 		c = fputs("&lt;", stdout);
1717c478bd9Sstevel@tonic-gate 		break;
1727c478bd9Sstevel@tonic-gate 	case '>':
1737c478bd9Sstevel@tonic-gate 		c = fputs("&gt;", stdout);
1747c478bd9Sstevel@tonic-gate 		break;
1757c478bd9Sstevel@tonic-gate 	case '&':
1767c478bd9Sstevel@tonic-gate 		c = fputs("&amp;", stdout);
1777c478bd9Sstevel@tonic-gate 		break;
1787c478bd9Sstevel@tonic-gate 	case '"':
1797c478bd9Sstevel@tonic-gate 		c = fputs("&quot;", stdout);
1807c478bd9Sstevel@tonic-gate 		break;
1817c478bd9Sstevel@tonic-gate 	default:
1827c478bd9Sstevel@tonic-gate 		c = putchar(c);
1837c478bd9Sstevel@tonic-gate 		break;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (c == EOF) {
1877c478bd9Sstevel@tonic-gate 		error(gettext("Error writing output:  %s\n"),
1887c478bd9Sstevel@tonic-gate 		    strerror(errno));
1897c478bd9Sstevel@tonic-gate 		exit(1);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * XML-safe analog of puts():  quotes XML-special characters
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate static void
1977c478bd9Sstevel@tonic-gate xputs(const char *s)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	char c;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	for (/* */; ((c = *s) != 0); s++)
2027c478bd9Sstevel@tonic-gate 		xputchar(c);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * Output the XML DTD derived from the registry provided by libfrureg
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate int
2097c478bd9Sstevel@tonic-gate output_dtd(void)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	char			**element;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	unsigned int		i, j, num_elements = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	uint8_t			*tagged;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	const fru_regdef_t	*def;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (((element = fru_reg_list_entries(&num_elements)) == NULL) ||
2217c478bd9Sstevel@tonic-gate 	    (num_elements == 0)) {
2227c478bd9Sstevel@tonic-gate 		error(gettext("No FRU ID Registry elements"));
2237c478bd9Sstevel@tonic-gate 		return (1);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((tagged = calloc(num_elements, sizeof (*tagged))) == NULL) {
2277c478bd9Sstevel@tonic-gate 		error(gettext("Unable to get memory for tagged element list"),
2287c478bd9Sstevel@tonic-gate 		    strerror(errno));
2297c478bd9Sstevel@tonic-gate 		return (1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Output the DTD preamble
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	output("<!ELEMENT FRUID_XML_Tree (Parameter*, "
2367c478bd9Sstevel@tonic-gate 	    "(Fru | Location | Container)*,\n"
2377c478bd9Sstevel@tonic-gate 	    "                          Parameter*, ErrorLog?, Parameter*)>\n"
2387c478bd9Sstevel@tonic-gate 	    "<!ATTLIST FRUID_XML_Tree>\n"
2397c478bd9Sstevel@tonic-gate 	    "\n"
2407c478bd9Sstevel@tonic-gate 	    "<!ELEMENT Parameter EMPTY>\n"
2417c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Parameter type CDATA #REQUIRED>\n"
2427c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Parameter name CDATA #REQUIRED>\n"
2437c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Parameter value CDATA #REQUIRED>\n"
2447c478bd9Sstevel@tonic-gate 	    "\n"
2457c478bd9Sstevel@tonic-gate 	    "<!ELEMENT Fru (Fru | Location | Container)*>\n"
2467c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Fru name CDATA #REQUIRED>\n"
2477c478bd9Sstevel@tonic-gate 	    "\n"
2487c478bd9Sstevel@tonic-gate 	    "<!ELEMENT Location (Fru | Location | Container)*>\n"
24994bc7577Sps154918 	    "<!ATTLIST Location\n"
25094bc7577Sps154918 	    "	name CDATA #IMPLIED\n"
25194bc7577Sps154918 	    "	value CDATA #IMPLIED\n"
25294bc7577Sps154918 	    ">\n"
2537c478bd9Sstevel@tonic-gate 	    "\n"
2547c478bd9Sstevel@tonic-gate 	    "<!ELEMENT Container (ContainerData?, "
2557c478bd9Sstevel@tonic-gate 	    "(Fru | Location | Container)*)>\n"
2567c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Container name CDATA #REQUIRED>\n"
2577c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Container imagefile CDATA #IMPLIED>\n"
2587c478bd9Sstevel@tonic-gate 	    "\n"
2597c478bd9Sstevel@tonic-gate 	    "<!ELEMENT ContainerData (Segment*)>\n"
2607c478bd9Sstevel@tonic-gate 	    "<!ATTLIST ContainerData>\n"
2617c478bd9Sstevel@tonic-gate 	    "\n"
2627c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Segment name CDATA #REQUIRED>\n"
2637c478bd9Sstevel@tonic-gate 	    "\n"
2647c478bd9Sstevel@tonic-gate 	    "<!ELEMENT Index EMPTY>\n"
2657c478bd9Sstevel@tonic-gate 	    "<!ATTLIST Index value CDATA #REQUIRED>\n"
2667c478bd9Sstevel@tonic-gate 	    "\n"
2677c478bd9Sstevel@tonic-gate 	    "<!ELEMENT ErrorLog (#PCDATA)>\n"
2687c478bd9Sstevel@tonic-gate 	    "<!ATTLIST ErrorLog>\n"
2697c478bd9Sstevel@tonic-gate 	    "\n");
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Output the definition for each element
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_elements; i++) {
2757c478bd9Sstevel@tonic-gate 		assert(element[i] != NULL);
27694bc7577Sps154918 		/* Prevent incompatible duplicate defn. from FRUID Registry. */
27794bc7577Sps154918 		if ((strcmp("Location", element[i])) == 0) continue;
2787c478bd9Sstevel@tonic-gate 		if ((def = fru_reg_lookup_def_by_name(element[i])) == NULL) {
2797c478bd9Sstevel@tonic-gate 			error(gettext("Error looking up registry "
2807c478bd9Sstevel@tonic-gate 			    "definition for \"%s\"\n"),
2817c478bd9Sstevel@tonic-gate 			    element[i]);
2827c478bd9Sstevel@tonic-gate 			return (1);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		if (def->tagType != FRU_X) tagged[i] = 1;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		if (def->dataType == FDTYPE_Record) {
2887c478bd9Sstevel@tonic-gate 			if (def->iterationType == FRU_NOT_ITERATED)
2897c478bd9Sstevel@tonic-gate 				output("<!ELEMENT %s (%s", element[i],
2907c478bd9Sstevel@tonic-gate 				    def->enumTable[0].text);
2917c478bd9Sstevel@tonic-gate 			else
2927c478bd9Sstevel@tonic-gate 				output("<!ELEMENT %s (Index_%s*)>\n"
2937c478bd9Sstevel@tonic-gate 				    "<!ATTLIST Index_%s>\n"
2947c478bd9Sstevel@tonic-gate 				    "<!ELEMENT Index_%s (%s",
2957c478bd9Sstevel@tonic-gate 				    element[i], element[i], element[i],
2967c478bd9Sstevel@tonic-gate 				    element[i], def->enumTable[0].text);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			for (j = 1; j < def->enumCount; j++)
2997c478bd9Sstevel@tonic-gate 				output(",\n\t%s", def->enumTable[j].text);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			output(")>\n");
3027c478bd9Sstevel@tonic-gate 		} else if (def->iterationType == FRU_NOT_ITERATED) {
3037c478bd9Sstevel@tonic-gate 			output("<!ELEMENT %s EMPTY>\n"
3047c478bd9Sstevel@tonic-gate 			    "<!ATTLIST %s value CDATA #REQUIRED>\n",
3057c478bd9Sstevel@tonic-gate 			    element[i], element[i]);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 			if (def->dataType == FDTYPE_Enumeration) {
3087c478bd9Sstevel@tonic-gate 				output("<!-- %s valid enumeration values\n");
3097c478bd9Sstevel@tonic-gate 				for (j = 0; j < def->enumCount; j++) {
3107c478bd9Sstevel@tonic-gate 					output("\t\"");
3117c478bd9Sstevel@tonic-gate 					xputs(def->enumTable[j].text);
3127c478bd9Sstevel@tonic-gate 					output("\"\n");
3137c478bd9Sstevel@tonic-gate 				}
3147c478bd9Sstevel@tonic-gate 				output("-->\n");
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 		else
3187c478bd9Sstevel@tonic-gate 			output("<!ELEMENT %s (Index*)>\n", element[i]);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		output("\n");
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/* Provide for returning the tag for an "unknown" element */
3247c478bd9Sstevel@tonic-gate 	output("<!ATTLIST UNKNOWN tag CDATA \"UNKNOWN\">\n\n");
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * List all data elements as possible members of "Segment"
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	output("<!ELEMENT Segment ((UNKNOWN");
3317c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_elements; i++) {
3327c478bd9Sstevel@tonic-gate 		if (tagged[i]) output("\n\t| %s", element[i]);
3337c478bd9Sstevel@tonic-gate 		free(element[i]);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	output(")*)>\n");
3367c478bd9Sstevel@tonic-gate 	free(element);
3377c478bd9Sstevel@tonic-gate 	free(tagged);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	return (0);
3407c478bd9Sstevel@tonic-gate }
341f3af4981Sdt207653 /*
342f3af4981Sdt207653  * Function to convert bcd to binary to correct the SPD_Manufacturer_Week
343f3af4981Sdt207653  *
344f3af4981Sdt207653  */
345f3af4981Sdt207653 static void convertbcdtobinary(int *val)
346f3af4981Sdt207653 {
347f3af4981Sdt207653 	int newval, tmpval, rem, origval, poweroften;
348f3af4981Sdt207653 	int i;
349f3af4981Sdt207653 	tmpval = 0;
350f3af4981Sdt207653 	newval = 0;
351f3af4981Sdt207653 	i = 0;
352f3af4981Sdt207653 	rem = 0;
353f3af4981Sdt207653 	poweroften = 1;
354f3af4981Sdt207653 	origval = (int)(*val);
355f3af4981Sdt207653 	tmpval = (int)(*val);
356f3af4981Sdt207653 	while (tmpval != 0) {
357f3af4981Sdt207653 		if (i >= 1)
358f3af4981Sdt207653 			poweroften = poweroften * 10;
359f3af4981Sdt207653 		origval = tmpval;
360f3af4981Sdt207653 		tmpval = (int)(tmpval/16);
361f3af4981Sdt207653 		rem = origval - (tmpval * 16);
362f3af4981Sdt207653 		newval = newval +(int)(poweroften * rem);
363f3af4981Sdt207653 		i ++;
364f3af4981Sdt207653 	}
365f3af4981Sdt207653 	*val = newval;
366f3af4981Sdt207653 }
367f3af4981Sdt207653 
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * Safely pretty-print the value of a field
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate static void
3737c478bd9Sstevel@tonic-gate print_field(const uint8_t *field, const fru_regdef_t *def)
3747c478bd9Sstevel@tonic-gate {
375f3af4981Sdt207653 	char		*errmsg = NULL, timestring[TIMESTRINGLEN], path[16384];
3767c478bd9Sstevel@tonic-gate 
377f3af4981Sdt207653 	int		i, valueint;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	uint64_t	value;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	time_t		timefield;
3827c478bd9Sstevel@tonic-gate 
383f3af4981Sdt207653 	struct tm	*tm;
3847c478bd9Sstevel@tonic-gate 
385f3af4981Sdt207653 	uchar_t		first_byte, data[128];
386f3af4981Sdt207653 
387f3af4981Sdt207653 	const fru_regdef_t	*new_def;
388f3af4981Sdt207653 
389f3af4981Sdt207653 	const char 	*elem_name = NULL;
390f3af4981Sdt207653 	const char	*parent_path;
3917c478bd9Sstevel@tonic-gate 	switch (def->dataType) {
3927c478bd9Sstevel@tonic-gate 	case FDTYPE_Binary:
3937c478bd9Sstevel@tonic-gate 		assert(def->payloadLen <= sizeof (value));
3947c478bd9Sstevel@tonic-gate 		switch (def->dispType) {
3957c478bd9Sstevel@tonic-gate 		case FDISP_Binary:
3967c478bd9Sstevel@tonic-gate 			for (i = 0; i < def->payloadLen; i++)
3977c478bd9Sstevel@tonic-gate 				output("%c%c%c%c%c%c%c%c",
3987c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x80) ? '1' : '0'),
3997c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x40) ? '1' : '0'),
4007c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x20) ? '1' : '0'),
4017c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x10) ? '1' : '0'),
4027c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x08) ? '1' : '0'),
4037c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x04) ? '1' : '0'),
4047c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x02) ? '1' : '0'),
4057c478bd9Sstevel@tonic-gate 				    ((field[i] & 0x01) ? '1' : '0'));
4067c478bd9Sstevel@tonic-gate 			return;
4077c478bd9Sstevel@tonic-gate 		case FDISP_Octal:
4087c478bd9Sstevel@tonic-gate 		case FDISP_Decimal:
4097c478bd9Sstevel@tonic-gate 			value = 0;
410f3af4981Sdt207653 			valueint = 0;
4117c478bd9Sstevel@tonic-gate 			(void) memcpy((((uint8_t *)&value) +
4127c478bd9Sstevel@tonic-gate 			    sizeof (value) - def->payloadLen),
4137c478bd9Sstevel@tonic-gate 			    field, def->payloadLen);
41494bc7577Sps154918 			if ((value != 0) &&
415f3af4981Sdt207653 			    (strcmp(def->name, "SPD_Manufacture_Week") == 0)) {
416f3af4981Sdt207653 				valueint = (int)value;
417f3af4981Sdt207653 				convertbcdtobinary(&valueint);
418f3af4981Sdt207653 				output("%d", valueint);
419f3af4981Sdt207653 				return;
420f3af4981Sdt207653 			}
421f3af4981Sdt207653 			if ((value != 0) &&
42294bc7577Sps154918 			    ((strcmp(def->name, "Lowest") == 0) ||
42394bc7577Sps154918 			    (strcmp(def->name, "Highest") == 0) ||
42494bc7577Sps154918 			    (strcmp(def->name, "Latest") == 0)))
4251e3549a6Sps154918 				output((def->dispType == FDISP_Octal) ?
4261e3549a6Sps154918 				"%llo" : "%lld (%lld degrees C)",
4271e3549a6Sps154918 				    value, (value - TEMPERATURE_OFFSET));
4281e3549a6Sps154918 			else
4297c478bd9Sstevel@tonic-gate 				output((def->dispType == FDISP_Octal) ?
4307c478bd9Sstevel@tonic-gate 				"%llo" : "%lld", value);
4317c478bd9Sstevel@tonic-gate 			return;
4327c478bd9Sstevel@tonic-gate 		case FDISP_Time:
4337c478bd9Sstevel@tonic-gate 			if (def->payloadLen > sizeof (timefield)) {
4347c478bd9Sstevel@tonic-gate 				errmsg = "time value too large for formatting";
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			}
437f3af4981Sdt207653 			timefield = 0;
438f3af4981Sdt207653 			(void) memcpy((((uint8_t *)&timefield) +
439f3af4981Sdt207653 			    sizeof (timefield) - def->payloadLen),
440f3af4981Sdt207653 			    field, def->payloadLen);
441f3af4981Sdt207653 			if (timefield == 0) {
442f3af4981Sdt207653 				errmsg = "No Value Recorded";
443f3af4981Sdt207653 				break;
444f3af4981Sdt207653 			}
445f3af4981Sdt207653 			if ((tm = localtime(&timefield)) == NULL) {
446f3af4981Sdt207653 				errmsg = "cannot convert time value";
447f3af4981Sdt207653 				break;
448f3af4981Sdt207653 			}
449f3af4981Sdt207653 			if (strftime(timestring, sizeof (timestring), "%C", tm)
450f3af4981Sdt207653 			    == 0) {
4517c478bd9Sstevel@tonic-gate 				errmsg = "formatted time would overflow buffer";
4527c478bd9Sstevel@tonic-gate 				break;
4537c478bd9Sstevel@tonic-gate 			}
4547c478bd9Sstevel@tonic-gate 			safeputs(timestring);
4557c478bd9Sstevel@tonic-gate 			return;
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		break;
4587c478bd9Sstevel@tonic-gate 	case FDTYPE_ASCII:
459f3af4981Sdt207653 		if (!xml) {
460f3af4981Sdt207653 			if (strcmp(def->name, "Message") == 0) {
461f3af4981Sdt207653 				if (FMAmessageR == 0)
462f3af4981Sdt207653 					elem_name = "FMA_Event_DataR";
463f3af4981Sdt207653 				else if (FMAmessageR == 1)
464f3af4981Sdt207653 					elem_name = "FMA_MessageR";
465f3af4981Sdt207653 				if (elem_name != NULL) {
466f3af4981Sdt207653 					(void) memcpy(data, field,
467f3af4981Sdt207653 					    def->payloadLen);
468f3af4981Sdt207653 					new_def =
469*46a7ef8aSdt207653 					    fru_reg_lookup_def_by_name
470*46a7ef8aSdt207653 					    (elem_name);
471aad09a1cSdt207653 					(void) snprintf(path, sizeof (path),
472f3af4981Sdt207653 					"/Status_EventsR[%d]/Message(FMA)",
473f3af4981Sdt207653 					    iterglobal);
474f3af4981Sdt207653 					parent_path = path;
475f3af4981Sdt207653 					output("\n");
476f3af4981Sdt207653 					print_element(data, new_def,
477f3af4981Sdt207653 					    parent_path, 2*INDENT);
478f3af4981Sdt207653 					return;
479f3af4981Sdt207653 				}
480f3af4981Sdt207653 			}
481f3af4981Sdt207653 		}
4827c478bd9Sstevel@tonic-gate 		for (i = 0; i < def->payloadLen && field[i]; i++)
4837c478bd9Sstevel@tonic-gate 			safeputchar(field[i]);
4847c478bd9Sstevel@tonic-gate 		return;
4857c478bd9Sstevel@tonic-gate 	case FDTYPE_Enumeration:
4867c478bd9Sstevel@tonic-gate 		value = 0;
4877c478bd9Sstevel@tonic-gate 		(void) memcpy((((uint8_t *)&value) + sizeof (value)
4887c478bd9Sstevel@tonic-gate 		    - def->payloadLen),
4897c478bd9Sstevel@tonic-gate 		    field, def->payloadLen);
4907c478bd9Sstevel@tonic-gate 		for (i = 0; i < def->enumCount; i++)
4917c478bd9Sstevel@tonic-gate 			if (def->enumTable[i].value == value) {
492f3af4981Sdt207653 				if (strcmp(def->name, "Event_Code") == 0) {
493f3af4981Sdt207653 					if (strcmp(def->enumTable[i].text,
494f3af4981Sdt207653 "FMA Message R") == 0)
495f3af4981Sdt207653 						FMAmessageR = 1;
496f3af4981Sdt207653 				else
497f3af4981Sdt207653 					if (strcmp(def->enumTable[i].text,
498f3af4981Sdt207653 "FMA Event Data R") == 0)
499f3af4981Sdt207653 						FMAmessageR = 0;
500f3af4981Sdt207653 				}
5017c478bd9Sstevel@tonic-gate 				safeputs(def->enumTable[i].text);
5027c478bd9Sstevel@tonic-gate 				return;
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		errmsg = "unrecognized value";
5067c478bd9Sstevel@tonic-gate 		break;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* If nothing matched above, print the field in hex */
510f3af4981Sdt207653 	switch (def->dispType) {
511f3af4981Sdt207653 		case FDISP_MSGID:
512f3af4981Sdt207653 			(void) memcpy((uchar_t *)&first_byte, field, 1);
513f3af4981Sdt207653 			if (isprint(first_byte)) {
514f3af4981Sdt207653 				for (i = 0; i < def->payloadLen && field[i];
515f3af4981Sdt207653 				    i++)
516f3af4981Sdt207653 					safeputchar(field[i]);
517f3af4981Sdt207653 			}
518f3af4981Sdt207653 			break;
519f3af4981Sdt207653 		case FDISP_UUID:
520f3af4981Sdt207653 			for (i = 0; i < def->payloadLen; i++) {
521f3af4981Sdt207653 				if ((i == 4) || (i == 6) ||
522f3af4981Sdt207653 				    (i == 8) || (i == 10))
523f3af4981Sdt207653 				output("-");
524f3af4981Sdt207653 				output("%2.2x", field[i]);
525f3af4981Sdt207653 			}
526f3af4981Sdt207653 			break;
527f3af4981Sdt207653 		default:
5287c478bd9Sstevel@tonic-gate 			for (i = 0; i < def->payloadLen; i++)
5297c478bd9Sstevel@tonic-gate 				output("%2.2X", field[i]);
530f3af4981Sdt207653 			break;
531f3af4981Sdt207653 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* Safely print any error message associated with the field */
5347c478bd9Sstevel@tonic-gate 	if (errmsg) {
535f3af4981Sdt207653 		if (strcmp(def->name, "Fault_Diag_Secs") != 0) {
5367c478bd9Sstevel@tonic-gate 			output(" (");
5377c478bd9Sstevel@tonic-gate 			safeputs(errmsg);
538f3af4981Sdt207653 			output(")");
539f3af4981Sdt207653 		}
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * Recursively print the contents of a data element
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static void
5477c478bd9Sstevel@tonic-gate print_element(const uint8_t *data, const fru_regdef_t *def,
5487c478bd9Sstevel@tonic-gate     const char *parent_path, int indent)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	char	*path;
5517c478bd9Sstevel@tonic-gate 	size_t	len;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	int	bytes = 0, i;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	indent = (xml) ? (indent + INDENT) : (2*INDENT);
557*46a7ef8aSdt207653 	if (strcmp(def->name, "Sun_SPD_DataR") == 0) {
558*46a7ef8aSdt207653 		Fault_Install_DataR_flag = indent;
559*46a7ef8aSdt207653 		Power_On_DataR_flag = indent;
560*46a7ef8aSdt207653 	}
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * Construct the path, or, for XML, the name, for the current
5637c478bd9Sstevel@tonic-gate 	 * data element
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 	if ((def->iterationCount == 0) &&
5667c478bd9Sstevel@tonic-gate 	    (def->iterationType != FRU_NOT_ITERATED)) {
5677c478bd9Sstevel@tonic-gate 		if (xml) {
5687c478bd9Sstevel@tonic-gate 			if (def->dataType == FDTYPE_Record) {
5697c478bd9Sstevel@tonic-gate 				len = strlen("Index_") + strlen(def->name) + 1;
5707c478bd9Sstevel@tonic-gate 				path = alloca(len);
5717c478bd9Sstevel@tonic-gate 				(void) snprintf(path, len,
5727c478bd9Sstevel@tonic-gate 				    "Index_%s", def->name);
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 			else
5757c478bd9Sstevel@tonic-gate 				path = "Index";
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		else
5787c478bd9Sstevel@tonic-gate 			path = (char *)parent_path;
5797c478bd9Sstevel@tonic-gate 	} else {
5807c478bd9Sstevel@tonic-gate 		if (xml)
5817c478bd9Sstevel@tonic-gate 			path = (char *)def->name;
5827c478bd9Sstevel@tonic-gate 		else {
5837c478bd9Sstevel@tonic-gate 			len = strlen(parent_path) + sizeof ("/") +
5847c478bd9Sstevel@tonic-gate 			    strlen(def->name) +
5857c478bd9Sstevel@tonic-gate 			    (def->iterationCount ? sizeof ("[255]") : 0);
5867c478bd9Sstevel@tonic-gate 			path = alloca(len);
5877c478bd9Sstevel@tonic-gate 			bytes = snprintf(path, len,
5887c478bd9Sstevel@tonic-gate 			    "%s/%s", parent_path, def->name);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
592*46a7ef8aSdt207653 	if ((Fault_Install_DataR_flag) &&
593*46a7ef8aSdt207653 	    (strcmp(path, "E_1_46") == 0) || (strcmp(path, "/E_1_46") == 0)) {
594*46a7ef8aSdt207653 		int cnt;
595*46a7ef8aSdt207653 		char timestring[128];
596*46a7ef8aSdt207653 		time_t timefield = 0;
597*46a7ef8aSdt207653 		struct tm *tm;
598*46a7ef8aSdt207653 		indent = Fault_Install_DataR_flag;
599*46a7ef8aSdt207653 		(void) memcpy((uint8_t *)&timefield, data, 4);
600*46a7ef8aSdt207653 		if (timefield == 0) {
601*46a7ef8aSdt207653 			(void) sprintf(timestring,
602*46a7ef8aSdt207653 			    "00000000 (No Value Recorded)\"");
603*46a7ef8aSdt207653 		} else {
604*46a7ef8aSdt207653 			if ((tm = localtime(&timefield)) == NULL)
605*46a7ef8aSdt207653 				(void) sprintf(timestring,
606*46a7ef8aSdt207653 				    "cannot convert time value");
607*46a7ef8aSdt207653 			if (strftime(timestring,
608*46a7ef8aSdt207653 			    sizeof (timestring), "%C", tm) == 0)
609*46a7ef8aSdt207653 				(void) sprintf(timestring,
610*46a7ef8aSdt207653 				    "formatted time would overflow buffer");
611*46a7ef8aSdt207653 		}
612*46a7ef8aSdt207653 		if (xml) {
613*46a7ef8aSdt207653 			(void) sprintf(path, "Fault_Install_DataR");
614*46a7ef8aSdt207653 			output("%*s<%s>\n", indent, "", path);
615*46a7ef8aSdt207653 			indent = Fault_Install_DataR_flag + INDENT;
616*46a7ef8aSdt207653 			(void) sprintf(path, "UNIX_Timestamp32");
617*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
618*46a7ef8aSdt207653 			/*CSTYLED*/
619*46a7ef8aSdt207653 			output("%s\"/>\n", timestring);
620*46a7ef8aSdt207653 			(void) sprintf(path, "MACADDR");
621*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
622*46a7ef8aSdt207653 			for (cnt = 4; cnt < 4 + 6; cnt++) {
623*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
624*46a7ef8aSdt207653 				if (cnt < 4 + 6 - 1)
625*46a7ef8aSdt207653 					output(":");
626*46a7ef8aSdt207653 			}
627*46a7ef8aSdt207653 			/*CSTYLED*/
628*46a7ef8aSdt207653 			output("\"/>\n");
629*46a7ef8aSdt207653 			(void) sprintf(path, "Status");
630*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
631*46a7ef8aSdt207653 			/*CSTYLED*/
632*46a7ef8aSdt207653 			output("%2.2x\"/>\n", data[10]);
633*46a7ef8aSdt207653 			(void) sprintf(path, "Initiator");
634*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
635*46a7ef8aSdt207653 			/*CSTYLED*/
636*46a7ef8aSdt207653 			output("%2.2x\"/>\n", data[11]);
637*46a7ef8aSdt207653 			(void) sprintf(path, "Message_Type");
638*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
639*46a7ef8aSdt207653 			/*CSTYLED*/
640*46a7ef8aSdt207653 			output("%2.2x\"/>\n", data[12]);
641*46a7ef8aSdt207653 			(void) sprintf(path, "Message_32");
642*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
643*46a7ef8aSdt207653 			for (cnt = 13; cnt < 13 + 32; cnt++)
644*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
645*46a7ef8aSdt207653 			/*CSTYLED*/
646*46a7ef8aSdt207653 			output("\"/>\n");
647*46a7ef8aSdt207653 			indent = Fault_Install_DataR_flag;
648*46a7ef8aSdt207653 			(void) sprintf(path, "Fault_Install_DataR");
649*46a7ef8aSdt207653 			output("%*s</%s>\n", indent, "", path);
650*46a7ef8aSdt207653 		} else {
651*46a7ef8aSdt207653 			(void) sprintf(path, "/Fault_Install_DataR");
652*46a7ef8aSdt207653 			output("%*s%s\n", indent, "", path);
653*46a7ef8aSdt207653 			(void) sprintf(path,
654*46a7ef8aSdt207653 			    "/Fault_Install_DataR/UNIX_Timestamp32");
655*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
656*46a7ef8aSdt207653 			output("%s\n", timestring);
657*46a7ef8aSdt207653 			(void) sprintf(path, "/Fault_Install_DataR/MACADDR");
658*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
659*46a7ef8aSdt207653 			for (cnt = 4; cnt < 4 + 6; cnt++) {
660*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
661*46a7ef8aSdt207653 				if (cnt < 4 + 6 - 1)
662*46a7ef8aSdt207653 					output(":");
663*46a7ef8aSdt207653 			}
664*46a7ef8aSdt207653 			output("\n");
665*46a7ef8aSdt207653 			(void) sprintf(path, "/Fault_Install_DataR/Status");
666*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
667*46a7ef8aSdt207653 			output("%2.2x\n", data[10]);
668*46a7ef8aSdt207653 			(void) sprintf(path, "/Fault_Install_DataR/Initiator");
669*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
670*46a7ef8aSdt207653 			output("%2.2x\n", data[11]);
671*46a7ef8aSdt207653 			(void) sprintf(path,
672*46a7ef8aSdt207653 			    "/Fault_Install_DataR/Message_Type");
673*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
674*46a7ef8aSdt207653 			output("%2.2x\n", data[12]);
675*46a7ef8aSdt207653 			(void) sprintf(path, "/Fault_Install_DataR/Message_32");
676*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
677*46a7ef8aSdt207653 			for (cnt = 13; cnt < 13 + 32; cnt++)
678*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
679*46a7ef8aSdt207653 			output("\n");
680*46a7ef8aSdt207653 		}
681*46a7ef8aSdt207653 		Fault_Install_DataR_flag = 0;
682*46a7ef8aSdt207653 		return;
683*46a7ef8aSdt207653 	} else if ((Power_On_DataR_flag) && (
684*46a7ef8aSdt207653 	    strcmp(path, "C_10_8") == 0 ||
685*46a7ef8aSdt207653 	    (strcmp(path, "/C_10_8") == 0))) {
686*46a7ef8aSdt207653 		int cnt;
687*46a7ef8aSdt207653 		char timestring[128];
688*46a7ef8aSdt207653 		time_t timefield = 0;
689*46a7ef8aSdt207653 		struct tm *tm;
690*46a7ef8aSdt207653 		indent = Power_On_DataR_flag;
691*46a7ef8aSdt207653 		(void) memcpy((uint8_t *)&timefield, data, 4);
692*46a7ef8aSdt207653 		if (timefield == 0) {
693*46a7ef8aSdt207653 			(void) sprintf(timestring,
694*46a7ef8aSdt207653 			    "00000000 (No Value Recorded)");
695*46a7ef8aSdt207653 		} else {
696*46a7ef8aSdt207653 			if ((tm = localtime(&timefield)) == NULL)
697*46a7ef8aSdt207653 				(void) sprintf(timestring,
698*46a7ef8aSdt207653 				    "cannot convert time value");
699*46a7ef8aSdt207653 			if (strftime(timestring,
700*46a7ef8aSdt207653 			    sizeof (timestring), "%C", tm) == 0)
701*46a7ef8aSdt207653 				(void) sprintf(timestring,
702*46a7ef8aSdt207653 				    "formatted time would overflow buffer");
703*46a7ef8aSdt207653 		}
704*46a7ef8aSdt207653 		if (xml) {
705*46a7ef8aSdt207653 			(void) sprintf(path, "Power_On_DataR");
706*46a7ef8aSdt207653 			output("%*s<%s>\n", indent, "", path);
707*46a7ef8aSdt207653 			indent = Power_On_DataR_flag + INDENT;
708*46a7ef8aSdt207653 			(void) sprintf(path, "UNIX_Timestamp32");
709*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
710*46a7ef8aSdt207653 			/*CSTYLED*/
711*46a7ef8aSdt207653 			output("%s\"/>\n", timestring);
712*46a7ef8aSdt207653 			(void) sprintf(path, "Power_On_Minutes");
713*46a7ef8aSdt207653 			output("%*s<%s value=\"", indent, "", path);
714*46a7ef8aSdt207653 			for (cnt = 4; cnt < 4 + 4; cnt++)
715*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
716*46a7ef8aSdt207653 			/*CSTYLED*/
717*46a7ef8aSdt207653 			output("\"/>\n");
718*46a7ef8aSdt207653 			indent = Power_On_DataR_flag;
719*46a7ef8aSdt207653 			(void) sprintf(path, "Power_On_DataR");
720*46a7ef8aSdt207653 			output("%*s</%s>\n", indent, "", path);
721*46a7ef8aSdt207653 		} else {
722*46a7ef8aSdt207653 			(void) sprintf(path, "/Power_On_DataR");
723*46a7ef8aSdt207653 			output("%*s%s\n", indent, "", path);
724*46a7ef8aSdt207653 			(void) sprintf(path,
725*46a7ef8aSdt207653 			    "/Power_On_DataR/UNIX_Timestamp32");
726*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
727*46a7ef8aSdt207653 			output("%s\n", timestring);
728*46a7ef8aSdt207653 			(void) sprintf(path,
729*46a7ef8aSdt207653 			    "/Power_On_DataR/Power_On_Minutes");
730*46a7ef8aSdt207653 			output("%*s%s: ", indent, "", path);
731*46a7ef8aSdt207653 			for (cnt = 4; cnt < 4 + 4; cnt++)
732*46a7ef8aSdt207653 				output("%2.2x", data[cnt]);
733*46a7ef8aSdt207653 			output("\n");
734*46a7ef8aSdt207653 		}
735*46a7ef8aSdt207653 		Power_On_DataR_flag = 0;
736*46a7ef8aSdt207653 		return;
737*46a7ef8aSdt207653 	}
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Handle the various categories of data elements:  iteration,
7407c478bd9Sstevel@tonic-gate 	 * record, and field
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	if (def->iterationCount) {
7437c478bd9Sstevel@tonic-gate 		int		iterlen = (def->payloadLen - NUM_ITER_BYTES)/
7447c478bd9Sstevel@tonic-gate 		    def->iterationCount,
7457c478bd9Sstevel@tonic-gate 		    n, valid = 1;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		uint8_t		head, num;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		fru_regdef_t	newdef;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * Make a new element definition to describe the components
7547c478bd9Sstevel@tonic-gate 		 * of the iteration
7557c478bd9Sstevel@tonic-gate 		 */
7567c478bd9Sstevel@tonic-gate 		(void) memcpy(&newdef, def, sizeof (newdef));
7577c478bd9Sstevel@tonic-gate 		newdef.iterationCount = 0;
7587c478bd9Sstevel@tonic-gate 		newdef.payloadLen = iterlen;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		/*
7617c478bd9Sstevel@tonic-gate 		 * Validate the contents of the iteration control bytes
7627c478bd9Sstevel@tonic-gate 		 */
7637c478bd9Sstevel@tonic-gate 		if (data[HEAD_ITER] >= def->iterationCount) {
7647c478bd9Sstevel@tonic-gate 			valid = 0;
7657c478bd9Sstevel@tonic-gate 			error(gettext("%s:  Invalid iteration head:  %d "
7667c478bd9Sstevel@tonic-gate 			    "(should be less than %d)\n"),
7677c478bd9Sstevel@tonic-gate 			    path, data[HEAD_ITER], def->iterationCount);
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		if (data[NUM_ITER] > def->iterationCount) {
7717c478bd9Sstevel@tonic-gate 			valid = 0;
7727c478bd9Sstevel@tonic-gate 			error(gettext("%s:  Invalid iteration count:  %d "
7737c478bd9Sstevel@tonic-gate 			    "(should not be greater than %d)\n"),
7747c478bd9Sstevel@tonic-gate 			    path, data[NUM_ITER], def->iterationCount);
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		if (data[MAX_ITER] != def->iterationCount) {
7787c478bd9Sstevel@tonic-gate 			valid = 0;
7797c478bd9Sstevel@tonic-gate 			error(gettext("%s:  Invalid iteration maximum:  %d "
7807c478bd9Sstevel@tonic-gate 			    "(should equal %d)\n"),
7817c478bd9Sstevel@tonic-gate 			    path, data[MAX_ITER], def->iterationCount);
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		if (valid) {
7857c478bd9Sstevel@tonic-gate 			head = data[HEAD_ITER];
7867c478bd9Sstevel@tonic-gate 			num  = data[NUM_ITER];
7877c478bd9Sstevel@tonic-gate 		} else {
7887c478bd9Sstevel@tonic-gate 			head = 0;
7897c478bd9Sstevel@tonic-gate 			num  = def->iterationCount;
7907c478bd9Sstevel@tonic-gate 			error(gettext("%s:  Showing all iterations\n"), path);
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		if (xml)
7947c478bd9Sstevel@tonic-gate 			output("%*s<%s>\n", indent, "", path);
7957c478bd9Sstevel@tonic-gate 		else
7967c478bd9Sstevel@tonic-gate 			output("%*s%s (%d iterations)\n", indent, "", path,
7977c478bd9Sstevel@tonic-gate 			    num);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		/*
8007c478bd9Sstevel@tonic-gate 		 * Print each component of the iteration
8017c478bd9Sstevel@tonic-gate 		 */
8027c478bd9Sstevel@tonic-gate 		for (i = head, n = 0, data += 4;
8037c478bd9Sstevel@tonic-gate 		    n < num;
8047c478bd9Sstevel@tonic-gate 		    i = ((i + 1) % def->iterationCount), n++) {
8057c478bd9Sstevel@tonic-gate 			if (!xml) (void) sprintf((path + bytes), "[%d]", n);
806f3af4981Sdt207653 			iterglobal = n;
8077c478bd9Sstevel@tonic-gate 			print_element((data + i*iterlen), &newdef, path,
8087c478bd9Sstevel@tonic-gate 			    indent);
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		if (xml) output("%*s</%s>\n", indent, "", path);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	} else if (def->dataType == FDTYPE_Record) {
8147c478bd9Sstevel@tonic-gate 		const fru_regdef_t  *component;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		if (xml)
8177c478bd9Sstevel@tonic-gate 			output("%*s<%s>\n", indent, "", path);
8187c478bd9Sstevel@tonic-gate 		else
8197c478bd9Sstevel@tonic-gate 			output("%*s%s\n", indent, "", path);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * Print each component of the record
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		for (i = 0; i < def->enumCount;
8257c478bd9Sstevel@tonic-gate 		    i++, data += component->payloadLen) {
8267c478bd9Sstevel@tonic-gate 			component = fru_reg_lookup_def_by_name(
8277c478bd9Sstevel@tonic-gate 			    def->enumTable[i].text);
8287c478bd9Sstevel@tonic-gate 			assert(component != NULL);
8297c478bd9Sstevel@tonic-gate 			print_element(data, component, path, indent);
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if (xml) output("%*s</%s>\n", indent, "", path);
8337c478bd9Sstevel@tonic-gate 	} else if (xml) {
8347c478bd9Sstevel@tonic-gate 		/*
8357c478bd9Sstevel@tonic-gate 		 * Base case:  print the field formatted for XML
8367c478bd9Sstevel@tonic-gate 		 */
8377c478bd9Sstevel@tonic-gate 		char  *format = ((def == &unknown)
8387c478bd9Sstevel@tonic-gate 		    ? "%*s<UNKNOWN tag=\"%s\" value=\""
8397c478bd9Sstevel@tonic-gate 		    : "%*s<%s value=\"");
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		output(format, indent, "", path);
8427c478bd9Sstevel@tonic-gate 		print_field(data, def);
8437c478bd9Sstevel@tonic-gate 		/*CSTYLED*/
8447c478bd9Sstevel@tonic-gate 		output("\"/>\n");	/* \" confuses cstyle */
845f3af4981Sdt207653 
846f3af4981Sdt207653 		if ((strcmp(def->name, "Message") == 0) &&
847f3af4981Sdt207653 		    ((FMAmessageR == 0) || (FMAmessageR == 1))) {
848f3af4981Sdt207653 			const char	*elem_name = NULL;
849f3af4981Sdt207653 			const char	*parent_path;
850f3af4981Sdt207653 			uchar_t		tmpdata[128];
851f3af4981Sdt207653 			char		path[16384];
852f3af4981Sdt207653 			const fru_regdef_t	*new_def;
853f3af4981Sdt207653 
854f3af4981Sdt207653 			if (FMAmessageR == 0)
855f3af4981Sdt207653 				elem_name = "FMA_Event_DataR";
856f3af4981Sdt207653 			else if (FMAmessageR == 1)
857f3af4981Sdt207653 				elem_name = "FMA_MessageR";
858f3af4981Sdt207653 			if (elem_name != NULL) {
859f3af4981Sdt207653 				(void) memcpy(tmpdata, data, def->payloadLen);
860f3af4981Sdt207653 				new_def = fru_reg_lookup_def_by_name(elem_name);
86177acf672Sdt207653 				(void) snprintf(path, sizeof (path),
862f3af4981Sdt207653 				"/Status_EventsR[%d]/Message(FMA)", iterglobal);
863f3af4981Sdt207653 				parent_path = path;
864f3af4981Sdt207653 				print_element(tmpdata, new_def,
865f3af4981Sdt207653 				    parent_path, 2*INDENT);
866f3af4981Sdt207653 				FMAmessageR = -1;
867f3af4981Sdt207653 			}
868f3af4981Sdt207653 		}
869f3af4981Sdt207653 
8707c478bd9Sstevel@tonic-gate 	} else {
8717c478bd9Sstevel@tonic-gate 		/*
8727c478bd9Sstevel@tonic-gate 		 * Base case:  print the field
8737c478bd9Sstevel@tonic-gate 		 */
8747c478bd9Sstevel@tonic-gate 		output("%*s%s: ", indent, "", path);
8757c478bd9Sstevel@tonic-gate 		print_field(data, def);
8767c478bd9Sstevel@tonic-gate 		output("\n");
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate /*
8817c478bd9Sstevel@tonic-gate  * Print the contents of a packet (i.e., a tagged data element)
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate /* ARGSUSED */
8847c478bd9Sstevel@tonic-gate static int
8857c478bd9Sstevel@tonic-gate print_packet(fru_tag_t *tag, uint8_t *payload, size_t length, void *args)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	int			tag_type = get_tag_type(tag);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	size_t			payload_length = 0;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	const fru_regdef_t	*def;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * Build a definition for unrecognized tags (e.g., not in libfrureg)
8957c478bd9Sstevel@tonic-gate 	 */
8967c478bd9Sstevel@tonic-gate 	if ((tag_type == -1) ||
8977c478bd9Sstevel@tonic-gate 	    ((payload_length = get_payload_length(tag)) != length)) {
8987c478bd9Sstevel@tonic-gate 		def = &unknown;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		unknown.tagType    = -1;
9017c478bd9Sstevel@tonic-gate 		unknown.tagDense   = -1;
9027c478bd9Sstevel@tonic-gate 		unknown.payloadLen = length;
9037c478bd9Sstevel@tonic-gate 		unknown.dataLength = unknown.payloadLen;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		if (tag_type == -1)
9067c478bd9Sstevel@tonic-gate 			(void) snprintf(tagname, sizeof (tagname), "INVALID");
9077c478bd9Sstevel@tonic-gate 		else
9087c478bd9Sstevel@tonic-gate 			(void) snprintf(tagname, sizeof (tagname),
9097c478bd9Sstevel@tonic-gate 			    "%s_%u_%u_%u", get_tagtype_str(tag_type),
9107c478bd9Sstevel@tonic-gate 			    get_tag_dense(tag), payload_length, length);
9117c478bd9Sstevel@tonic-gate 	} else if ((def = fru_reg_lookup_def_by_tag(*tag)) == NULL) {
9127c478bd9Sstevel@tonic-gate 		def = &unknown;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		unknown.tagType    = tag_type;
9157c478bd9Sstevel@tonic-gate 		unknown.tagDense   = get_tag_dense(tag);
9167c478bd9Sstevel@tonic-gate 		unknown.payloadLen = payload_length;
9177c478bd9Sstevel@tonic-gate 		unknown.dataLength = unknown.payloadLen;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 		(void) snprintf(tagname, sizeof (tagname), "%s_%u_%u",
9207c478bd9Sstevel@tonic-gate 		    get_tagtype_str(unknown.tagType),
9217c478bd9Sstevel@tonic-gate 		    unknown.tagDense, payload_length);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * Print the defined element
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	print_element(payload, def, "", INDENT);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Print a segment's name and the contents of each data element in the segment
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate static int
9377c478bd9Sstevel@tonic-gate print_packets_in_segment(fru_seghdl_t segment, void *args)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	char	*name;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	int	status;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if ((status = fru_get_segment_name(segment, &name)) != FRU_SUCCESS) {
9457c478bd9Sstevel@tonic-gate 		saved_status = status;
9467c478bd9Sstevel@tonic-gate 		name = "";
9477c478bd9Sstevel@tonic-gate 		error(gettext("Error getting segment name:  %s\n"),
9487c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (xml)
9537c478bd9Sstevel@tonic-gate 		output("%*s<Segment name=\"%s\">\n", INDENT, "", name);
9547c478bd9Sstevel@tonic-gate 	else
9557c478bd9Sstevel@tonic-gate 		output("%*sSEGMENT: %s\n", INDENT, "", name);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/* Iterate over the packets in the segment, printing the contents */
9587c478bd9Sstevel@tonic-gate 	if ((status = fru_for_each_packet(segment, print_packet, args))
9597c478bd9Sstevel@tonic-gate 	    != FRU_SUCCESS) {
9607c478bd9Sstevel@tonic-gate 		saved_status = status;
9617c478bd9Sstevel@tonic-gate 		error(gettext("Error processing data in segment \"%s\":  %s\n"),
9627c478bd9Sstevel@tonic-gate 		    name, fru_strerror(status));
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (xml) output("%*s</Segment>\n", INDENT, "");
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	free(name);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /* ARGSUSED */
9737c478bd9Sstevel@tonic-gate static void
9747c478bd9Sstevel@tonic-gate print_node_path(fru_node_t fru_type, const char *path, const char *name,
9757c478bd9Sstevel@tonic-gate     end_node_fp_t *end_node, void **end_args)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	output("%s%s\n", path,
9787c478bd9Sstevel@tonic-gate 	    ((fru_type == FRU_NODE_CONTAINER) ? " (container)"
9797c478bd9Sstevel@tonic-gate 	    : ((fru_type == FRU_NODE_FRU) ? " (fru)" : "")));
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * Close the XML element for a "location" node
9847c478bd9Sstevel@tonic-gate  */
9857c478bd9Sstevel@tonic-gate /* ARGSUSED */
9867c478bd9Sstevel@tonic-gate static void
9877c478bd9Sstevel@tonic-gate end_location_xml(fru_nodehdl_t node, const char *path, const char *name,
9887c478bd9Sstevel@tonic-gate     void *args)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	assert(args != NULL);
9917c478bd9Sstevel@tonic-gate 	output("</Location> <!-- %s -->\n", args);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * Close the XML element for a "fru" node
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate /* ARGSUSED */
9987c478bd9Sstevel@tonic-gate static void
9997c478bd9Sstevel@tonic-gate end_fru_xml(fru_nodehdl_t node, const char *path, const char *name, void *args)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	assert(args != NULL);
10027c478bd9Sstevel@tonic-gate 	output("</Fru> <!-- %s -->\n", args);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * Close the XML element for a "container" node
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate /* ARGSUSED */
10097c478bd9Sstevel@tonic-gate static void
10107c478bd9Sstevel@tonic-gate end_container_xml(fru_nodehdl_t node, const char *path, const char *name,
10117c478bd9Sstevel@tonic-gate     void *args)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	assert(args != NULL);
10147c478bd9Sstevel@tonic-gate 	output("</Container> <!-- %s -->\n", args);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate  * Introduce a node in XML and set the appropriate node-closing function
10197c478bd9Sstevel@tonic-gate  */
10207c478bd9Sstevel@tonic-gate /* ARGSUSED */
10217c478bd9Sstevel@tonic-gate static void
10227c478bd9Sstevel@tonic-gate print_node_xml(fru_node_t fru_type, const char *path, const char *name,
10237c478bd9Sstevel@tonic-gate     end_node_fp_t *end_node, void **end_args)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	switch (fru_type) {
10267c478bd9Sstevel@tonic-gate 	case FRU_NODE_FRU:
10277c478bd9Sstevel@tonic-gate 		output("<Fru name=\"%s\">\n", name);
10287c478bd9Sstevel@tonic-gate 		*end_node = end_fru_xml;
10297c478bd9Sstevel@tonic-gate 		break;
10307c478bd9Sstevel@tonic-gate 	case FRU_NODE_CONTAINER:
10317c478bd9Sstevel@tonic-gate 		output("<Container name=\"%s\">\n", name);
10327c478bd9Sstevel@tonic-gate 		*end_node = end_container_xml;
10337c478bd9Sstevel@tonic-gate 		break;
10347c478bd9Sstevel@tonic-gate 	default:
10357c478bd9Sstevel@tonic-gate 		output("<Location name=\"%s\">\n", name);
10367c478bd9Sstevel@tonic-gate 		*end_node = end_location_xml;
10377c478bd9Sstevel@tonic-gate 		break;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	*end_args = (void *) name;
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * Print node info and, where appropriate, node contents
10457c478bd9Sstevel@tonic-gate  */
10467c478bd9Sstevel@tonic-gate /* ARGSUSED */
10477c478bd9Sstevel@tonic-gate static fru_errno_t
10487c478bd9Sstevel@tonic-gate process_node(fru_nodehdl_t node, const char *path, const char *name,
10497c478bd9Sstevel@tonic-gate 		void *args, end_node_fp_t *end_node, void **end_args)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	int		status;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	fru_node_t	fru_type = FRU_NODE_UNKNOWN;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if ((status = fru_get_node_type(node, &fru_type)) != FRU_SUCCESS) {
10577c478bd9Sstevel@tonic-gate 		saved_status = status;
10587c478bd9Sstevel@tonic-gate 		error(gettext("Error getting node type:  %s\n"),
10597c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if (containers_only) {
10637c478bd9Sstevel@tonic-gate 		if (fru_type != FRU_NODE_CONTAINER)
10647c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
10657c478bd9Sstevel@tonic-gate 		name = path;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	/* Introduce the node */
10697c478bd9Sstevel@tonic-gate 	assert(print_node != NULL);
10707c478bd9Sstevel@tonic-gate 	print_node(fru_type, path, name, end_node, end_args);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (list_only)
10737c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	/* Print the contents of each packet in each segment of a container */
10767c478bd9Sstevel@tonic-gate 	if (fru_type == FRU_NODE_CONTAINER) {
10777c478bd9Sstevel@tonic-gate 		if (xml) output("<ContainerData>\n");
10787c478bd9Sstevel@tonic-gate 		if ((status =
10797c478bd9Sstevel@tonic-gate 		    fru_for_each_segment(node, print_packets_in_segment,
10807c478bd9Sstevel@tonic-gate 		    NULL))
10817c478bd9Sstevel@tonic-gate 		    != FRU_SUCCESS) {
10827c478bd9Sstevel@tonic-gate 			saved_status = status;
10837c478bd9Sstevel@tonic-gate 			error(gettext("Error  processing node \"%s\":  %s\n"),
10847c478bd9Sstevel@tonic-gate 			    name, fru_strerror(status));
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 		if (xml) output("</ContainerData>\n");
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate  * Process the node if its path matches the search path in "args"
10947c478bd9Sstevel@tonic-gate  */
10957c478bd9Sstevel@tonic-gate /* ARGSUSED */
10967c478bd9Sstevel@tonic-gate static fru_errno_t
10977c478bd9Sstevel@tonic-gate process_matching_node(fru_nodehdl_t node, const char *path, const char *name,
10987c478bd9Sstevel@tonic-gate     void *args, end_node_fp_t *end_node, void **end_args)
10997c478bd9Sstevel@tonic-gate 	{
11007c478bd9Sstevel@tonic-gate 	int  status;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (!fru_pathmatch(path, args))
11047c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	status = process_node(node, path, path, args, end_node, end_args);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	return ((status == FRU_SUCCESS) ? FRU_WALK_TERMINATE : status);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate /*
11127c478bd9Sstevel@tonic-gate  * Write the trailer required for well-formed DTD-compliant XML
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate static void
11157c478bd9Sstevel@tonic-gate terminate_xml()
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	errno = 0;
11187c478bd9Sstevel@tonic-gate 	if (ftell(errlog) > 0) {
11197c478bd9Sstevel@tonic-gate 		char  c;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		output("<ErrorLog>\n");
11227c478bd9Sstevel@tonic-gate 		rewind(errlog);
11237c478bd9Sstevel@tonic-gate 		if (!errno)
11247c478bd9Sstevel@tonic-gate 			while ((c = getc(errlog)) != EOF)
11257c478bd9Sstevel@tonic-gate 				xputchar(c);
11267c478bd9Sstevel@tonic-gate 		output("</ErrorLog>\n");
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (errno) {
11307c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
11317c478bd9Sstevel@tonic-gate 		errlog = NULL;
11327c478bd9Sstevel@tonic-gate 		error(gettext("Error copying error messages to \"ErrorLog\""),
11337c478bd9Sstevel@tonic-gate 		    strerror(errno));
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	output("</FRUID_XML_Tree>\n");
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /*
11407c478bd9Sstevel@tonic-gate  * Print available FRU ID information
11417c478bd9Sstevel@tonic-gate  */
11427c478bd9Sstevel@tonic-gate int
11437c478bd9Sstevel@tonic-gate prtfru(const char *searchpath, int containers_only_flag, int list_only_flag,
11447c478bd9Sstevel@tonic-gate 	int xml_flag)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	fru_errno_t    status;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	fru_nodehdl_t  frutree = 0;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/* Copy parameter flags to global flags */
11527c478bd9Sstevel@tonic-gate 	containers_only	= containers_only_flag;
11537c478bd9Sstevel@tonic-gate 	list_only	= list_only_flag;
11547c478bd9Sstevel@tonic-gate 	xml		= xml_flag;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/* Help arrange for correct, efficient interleaving of output */
11587c478bd9Sstevel@tonic-gate 	(void) setvbuf(stderr, NULL, _IOLBF, 0);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	/* Initialize for XML--or not */
11627c478bd9Sstevel@tonic-gate 	if (xml) {
11637c478bd9Sstevel@tonic-gate 		safeputchar = xputchar;
11647c478bd9Sstevel@tonic-gate 		safeputs    = xputs;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		print_node  = print_node_xml;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if ((errlog = tmpfile()) == NULL) {
11697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11707c478bd9Sstevel@tonic-gate 			    "Error creating error log file:  %s\n",
11717c478bd9Sstevel@tonic-gate 			    strerror(errno));
11727c478bd9Sstevel@tonic-gate 			return (1);
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		/* Output the XML preamble */
11767c478bd9Sstevel@tonic-gate 		output("<?xml version=\"1.0\" ?>\n"
11777c478bd9Sstevel@tonic-gate 		    "<!--\n"
11787c478bd9Sstevel@tonic-gate 		    " Copyright 2000-2002 Sun Microsystems, Inc.  "
11797c478bd9Sstevel@tonic-gate 		    "All rights reserved.\n"
11807c478bd9Sstevel@tonic-gate 		    " Use is subject to license terms.\n"
11817c478bd9Sstevel@tonic-gate 		    "-->\n\n"
11827c478bd9Sstevel@tonic-gate 		    "<!DOCTYPE FRUID_XML_Tree SYSTEM \"prtfrureg.dtd\">\n\n"
11837c478bd9Sstevel@tonic-gate 		    "<FRUID_XML_Tree>\n");
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		/* Arrange to always properly terminate XML */
11867c478bd9Sstevel@tonic-gate 		if (atexit(terminate_xml))
11877c478bd9Sstevel@tonic-gate 			error(gettext("Warning:  XML will not be terminated:  "
11887c478bd9Sstevel@tonic-gate 			    "%s\n"), strerror(errno));
11897c478bd9Sstevel@tonic-gate 	} else
11907c478bd9Sstevel@tonic-gate 		print_node = print_node_path;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	/* Get the root node */
11947c478bd9Sstevel@tonic-gate 	if ((status = fru_get_root(&frutree)) == FRU_NODENOTFOUND) {
11957c478bd9Sstevel@tonic-gate 		error(gettext("This system does not provide FRU ID data\n"));
11967c478bd9Sstevel@tonic-gate 		return (1);
11977c478bd9Sstevel@tonic-gate 	} else if (status != FRU_SUCCESS) {
11987c478bd9Sstevel@tonic-gate 		error(gettext("Unable to access FRU ID data:  %s\n"),
11997c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
12007c478bd9Sstevel@tonic-gate 		return (1);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	/* Process the tree */
12047c478bd9Sstevel@tonic-gate 	if (searchpath == NULL) {
12057c478bd9Sstevel@tonic-gate 		status = fru_walk_tree(frutree, "", process_node, NULL);
12067c478bd9Sstevel@tonic-gate 	} else {
12077c478bd9Sstevel@tonic-gate 		status = fru_walk_tree(frutree, "", process_matching_node,
12087c478bd9Sstevel@tonic-gate 		    (void *)searchpath);
12097c478bd9Sstevel@tonic-gate 		if (status == FRU_WALK_TERMINATE) {
12107c478bd9Sstevel@tonic-gate 			status = FRU_SUCCESS;
12117c478bd9Sstevel@tonic-gate 		} else if (status == FRU_SUCCESS) {
12127c478bd9Sstevel@tonic-gate 			error(gettext("\"%s\" not found\n"), searchpath);
12137c478bd9Sstevel@tonic-gate 			return (1);
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (status != FRU_SUCCESS)
12187c478bd9Sstevel@tonic-gate 		error(gettext("Error processing FRU tree:  %s\n"),
12197c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	return (((status == FRU_SUCCESS) && (saved_status == 0)) ? 0 : 1);
12227c478bd9Sstevel@tonic-gate }
1223