xref: /illumos-gate/usr/src/cmd/fruadm/fruadm.c (revision bc54f855)
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
506e46062Sjbeloro  * Common Development and Distribution License (the "License").
606e46062Sjbeloro  * 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  */
2106e46062Sjbeloro 
227c478bd9Sstevel@tonic-gate /*
23ee169c7eSGary Mills  * Copyright (c) 2014 Gary Mills
24ee169c7eSGary Mills  *
2580148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*bc54f855SJohn Levon  *
28*bc54f855SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <libfru.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <math.h>
397c478bd9Sstevel@tonic-gate #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <assert.h>
417c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	NUM_OF_SEGMENT	1
447c478bd9Sstevel@tonic-gate #define	SEGMENT_NAME_SIZE	2
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	FD_SEGMENT_SIZE	2949
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static char  *command, *customer_data = NULL, *frupath = NULL, **svcargv;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* DataElement supported in the customer operation */
517c478bd9Sstevel@tonic-gate static  char    *cust_data_list[] = {"Customer_DataR"};
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* DataElement supported in the service operation */
547c478bd9Sstevel@tonic-gate static  char    *serv_data_list[] = {"InstallationR", "ECO_CurrentR"};
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* currently supported segment name */
577c478bd9Sstevel@tonic-gate static  char    *segment_name[] = {"FD"};
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int   found_frupath = 0, list_only = 0, recursive = 0,
607c478bd9Sstevel@tonic-gate     service_mode = 0, svcargc, update = 0;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void
usage(void)647c478bd9Sstevel@tonic-gate usage(void)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
67af3025fdSdt 	    gettext("Usage:  %s [ -l ] | [ [ -r ] frupath [ text ] ]\n"),
68af3025fdSdt 	    command);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int
validate_fieldnames(int argc,char * argv[])727c478bd9Sstevel@tonic-gate validate_fieldnames(int argc, char *argv[])
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	static int	num = sizeof (serv_data_list)/sizeof (*serv_data_list);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	char		*fieldname;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	int		i, j, match, status;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	fru_elemdef_t	definition;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i += 2) {
847c478bd9Sstevel@tonic-gate 		if (argv[i][0] == '/') {
857c478bd9Sstevel@tonic-gate 			fieldname = &argv[i][1];
867c478bd9Sstevel@tonic-gate 		} else {
877c478bd9Sstevel@tonic-gate 			fieldname = &argv[i][0];
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		match = 0;
917c478bd9Sstevel@tonic-gate 		for (j = 0; j < num; j++) {
927c478bd9Sstevel@tonic-gate 			if (strncmp(fieldname, serv_data_list[j],
93af3025fdSdt 			    strlen(serv_data_list[j])) == 0) {
947c478bd9Sstevel@tonic-gate 				match = 1;
957c478bd9Sstevel@tonic-gate 			}
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		if (!match) {
987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
997c478bd9Sstevel@tonic-gate 			    gettext("\"%s\" is not a supported field\n"),
1007c478bd9Sstevel@tonic-gate 			    argv[i]);
1017c478bd9Sstevel@tonic-gate 			return (1);
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		if ((status = fru_get_definition(argv[i], &definition))
1057c478bd9Sstevel@tonic-gate 		    != FRU_SUCCESS) {
1067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("\"%s\":  %s\n"),
1077c478bd9Sstevel@tonic-gate 			    argv[i],
1087c478bd9Sstevel@tonic-gate 			    fru_strerror(status));
1097c478bd9Sstevel@tonic-gate 			return (1);
1107c478bd9Sstevel@tonic-gate 		} else if ((definition.data_type == FDTYPE_Record) ||
1117c478bd9Sstevel@tonic-gate 		    (definition.data_type == FDTYPE_UNDEFINED)) {
1127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1137c478bd9Sstevel@tonic-gate 			    gettext("\"%s\" is not a field\n"), argv[i]);
1147c478bd9Sstevel@tonic-gate 			return (1);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	return (0);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static int
pathmatch(const char * path)1227c478bd9Sstevel@tonic-gate pathmatch(const char *path)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	char  *match;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if ((frupath != NULL) &&
1277c478bd9Sstevel@tonic-gate 	    ((match = strstr(path, frupath)) != NULL) &&
1287c478bd9Sstevel@tonic-gate 	    ((match + strlen(frupath)) == (path + strlen(path))) &&
1297c478bd9Sstevel@tonic-gate 	    ((match == path) || (*(match - 1) == '/'))) {
1307c478bd9Sstevel@tonic-gate 		found_frupath = 1;
1317c478bd9Sstevel@tonic-gate 		return (1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static void
displayBinary(unsigned char * data,size_t length,fru_elemdef_t * def)1377c478bd9Sstevel@tonic-gate displayBinary(unsigned char *data, size_t length, fru_elemdef_t *def)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	int	i = 0;
1407c478bd9Sstevel@tonic-gate 	uint64_t	lldata;
1417c478bd9Sstevel@tonic-gate 	uint64_t	mask;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (def->disp_type == FDISP_Hex) {
1447c478bd9Sstevel@tonic-gate 		for (i = 0; i < length; i++) {
1457c478bd9Sstevel@tonic-gate 			(void) printf("%02X", data[i]);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		return;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	(void) memcpy(&lldata, data, sizeof (lldata));
1517c478bd9Sstevel@tonic-gate 	switch (def->disp_type) {
1527c478bd9Sstevel@tonic-gate 		case FDISP_Binary:
1537c478bd9Sstevel@tonic-gate 		{
15434e5f34eSkmohan 			mask = 0x8000000000000000ULL;
1557c478bd9Sstevel@tonic-gate 			for (i = 0; i < (sizeof (uint64_t) *8); i++) {
1567c478bd9Sstevel@tonic-gate 				if (lldata & (mask >> i)) {
1577c478bd9Sstevel@tonic-gate 					(void) printf("1");
1587c478bd9Sstevel@tonic-gate 				} else {
1597c478bd9Sstevel@tonic-gate 					(void) printf("0");
1607c478bd9Sstevel@tonic-gate 				}
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 			return;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		case FDISP_Octal:
1657c478bd9Sstevel@tonic-gate 		{
1667c478bd9Sstevel@tonic-gate 			(void) printf("%llo", lldata);
1677c478bd9Sstevel@tonic-gate 			return;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		case FDISP_Decimal:
1707c478bd9Sstevel@tonic-gate 		{
1717c478bd9Sstevel@tonic-gate 			(void) printf("%lld", lldata);
1727c478bd9Sstevel@tonic-gate 			return;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 		case FDISP_Time:
1757c478bd9Sstevel@tonic-gate 		{
1767c478bd9Sstevel@tonic-gate 			char buffer[PATH_MAX];
1777c478bd9Sstevel@tonic-gate 			time_t time;
1787c478bd9Sstevel@tonic-gate 			time = (time_t)lldata;
179ee169c7eSGary Mills 			(void) strftime(buffer, PATH_MAX, "%+",
1807c478bd9Sstevel@tonic-gate 			    localtime(&time));
1817c478bd9Sstevel@tonic-gate 			(void) printf("%s", buffer);
1827c478bd9Sstevel@tonic-gate 			return;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static void
displayBAasBinary(unsigned char * data,size_t length)1887c478bd9Sstevel@tonic-gate displayBAasBinary(unsigned char *data, size_t length)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int i;
1917c478bd9Sstevel@tonic-gate 	unsigned char mask;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (i = 0; i < length; i++) {
1947c478bd9Sstevel@tonic-gate 		/*
1957c478bd9Sstevel@tonic-gate 		 * make a mask for the high order bit and adjust down through
1967c478bd9Sstevel@tonic-gate 		 * all the bits.
1977c478bd9Sstevel@tonic-gate 		 */
1987c478bd9Sstevel@tonic-gate 		for (mask = 0x80; mask > 0; mask /= 2) {
1997c478bd9Sstevel@tonic-gate 			if ((data[i] & mask) != 0) /* bit must be on */
2007c478bd9Sstevel@tonic-gate 				(void) printf("1");
2017c478bd9Sstevel@tonic-gate 			else /* bit is off... */
2027c478bd9Sstevel@tonic-gate 				(void) printf("0");
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	(void) printf("\n");
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
display_data(unsigned char * data,size_t length,fru_elemdef_t * def)2097c478bd9Sstevel@tonic-gate display_data(unsigned char *data, size_t length, fru_elemdef_t *def)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	int i = 0;
2127c478bd9Sstevel@tonic-gate 	uint64_t	lldata;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (data == 0x00) {
2157c478bd9Sstevel@tonic-gate 		(void) printf("\n");
2167c478bd9Sstevel@tonic-gate 		return;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	switch (def->data_type) {
2207c478bd9Sstevel@tonic-gate 	case FDTYPE_Binary:
2217c478bd9Sstevel@tonic-gate 	{
2227c478bd9Sstevel@tonic-gate 		displayBinary(data, length, def);
2237c478bd9Sstevel@tonic-gate 		return;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	case FDTYPE_ByteArray:
2277c478bd9Sstevel@tonic-gate 	{
2287c478bd9Sstevel@tonic-gate 		switch (def->disp_type) {
2297c478bd9Sstevel@tonic-gate 		case FDISP_Binary:
2307c478bd9Sstevel@tonic-gate 			displayBAasBinary(data, length);
2317c478bd9Sstevel@tonic-gate 			return;
2327c478bd9Sstevel@tonic-gate 		case FDISP_Hex:
2337c478bd9Sstevel@tonic-gate 			for (i = 0; i < length; i++) {
2347c478bd9Sstevel@tonic-gate 				(void) printf("%02X", data[i]);
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			return;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 		return;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	case FDTYPE_Unicode:
2417c478bd9Sstevel@tonic-gate 		assert(gettext("Unicode not yet supported") == 0);
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 	case FDTYPE_ASCII:
2447c478bd9Sstevel@tonic-gate 	{
2457c478bd9Sstevel@tonic-gate 		char *disp_str = (char *)alloca(length+1);
2467c478bd9Sstevel@tonic-gate 		for (i = 0; i < length; i++)
2477c478bd9Sstevel@tonic-gate 			disp_str[i] = data[i];
248*bc54f855SJohn Levon 		disp_str[i] = '\0';
249*bc54f855SJohn Levon 		(void) printf("%s", disp_str);
250*bc54f855SJohn Levon 		return;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	case FDTYPE_Enumeration:
2547c478bd9Sstevel@tonic-gate 	{
2557c478bd9Sstevel@tonic-gate 		lldata = strtoull((const char *)data, NULL, 0);
2567c478bd9Sstevel@tonic-gate 		for (i = 0; i < def->enum_count; i++) {
2577c478bd9Sstevel@tonic-gate 			if (def->enum_table[i].value == lldata) {
2587c478bd9Sstevel@tonic-gate 			/* strdup such that map_... can realloc if necessary. */
2597c478bd9Sstevel@tonic-gate 				char *tmp = strdup(def->enum_table[i].text);
2607c478bd9Sstevel@tonic-gate 				(void) printf("%s", tmp);
2617c478bd9Sstevel@tonic-gate 				free(tmp);
2627c478bd9Sstevel@tonic-gate 				return;
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Unrecognized Value:  0x"));
2667c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (uint64_t); i++)
2677c478bd9Sstevel@tonic-gate 			(void) printf("%02X", data[i]);
2687c478bd9Sstevel@tonic-gate 		break;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	default:
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static void
print_node_data(fru_nodehdl_t cont_hdl)2767c478bd9Sstevel@tonic-gate print_node_data(fru_nodehdl_t cont_hdl)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	int	iter_cnt = 0;
2797c478bd9Sstevel@tonic-gate 	int	iter;
2807c478bd9Sstevel@tonic-gate 	int	numseg;
2817c478bd9Sstevel@tonic-gate 	int	list_cnt;
2827c478bd9Sstevel@tonic-gate 	unsigned char	*data;
2837c478bd9Sstevel@tonic-gate 	size_t	dataLen;
2847c478bd9Sstevel@tonic-gate 	int	total_cnt;
2857c478bd9Sstevel@tonic-gate 	char	*found_path = NULL;
2867c478bd9Sstevel@tonic-gate 	fru_elemdef_t	 def, def1;
2877c478bd9Sstevel@tonic-gate 	int	instance = 0;
2887c478bd9Sstevel@tonic-gate 	char	**ptr;
2897c478bd9Sstevel@tonic-gate 	char	**tmp_ptr;
2907c478bd9Sstevel@tonic-gate 	int	count = 0;
2917c478bd9Sstevel@tonic-gate 	char	elem_name[PATH_MAX];
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (service_mode) {
2947c478bd9Sstevel@tonic-gate 		total_cnt = sizeof (serv_data_list)/sizeof (*serv_data_list);
2957c478bd9Sstevel@tonic-gate 		ptr = serv_data_list;
2967c478bd9Sstevel@tonic-gate 	} else {
2977c478bd9Sstevel@tonic-gate 		total_cnt = sizeof (cust_data_list)/sizeof (*cust_data_list);
2987c478bd9Sstevel@tonic-gate 		ptr = cust_data_list;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	tmp_ptr = ptr;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (numseg = 0; numseg < NUM_OF_SEGMENT; numseg++) {
3037c478bd9Sstevel@tonic-gate 		ptr = tmp_ptr;
3047c478bd9Sstevel@tonic-gate 		for (list_cnt = 0; list_cnt < total_cnt; list_cnt++) {
3057c478bd9Sstevel@tonic-gate 			if ((fru_get_definition(*ptr, &def)) != FRU_SUCCESS) {
3067c478bd9Sstevel@tonic-gate 				continue;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			if ((fru_get_num_iterations(cont_hdl,
309af3025fdSdt 			    &segment_name[numseg], 0, *ptr,
310af3025fdSdt 			    &iter_cnt, NULL)) != FRU_SUCCESS) {
3117c478bd9Sstevel@tonic-gate 				iter_cnt = 0;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			iter = 0;
3147c478bd9Sstevel@tonic-gate 			do {
3157c478bd9Sstevel@tonic-gate 				for (count = 0; count < def.enum_count;
316af3025fdSdt 				    count++) {
3177c478bd9Sstevel@tonic-gate 					if (def.iteration_type !=
318af3025fdSdt 					    FRU_NOT_ITERATED) {
3197c478bd9Sstevel@tonic-gate 						(void) snprintf(elem_name,
3207c478bd9Sstevel@tonic-gate 						    sizeof (elem_name),
3217c478bd9Sstevel@tonic-gate 			"/%s[%d]/%s", *ptr, iter, def.enum_table[count].text);
3227c478bd9Sstevel@tonic-gate 					} else {
3237c478bd9Sstevel@tonic-gate 						(void) snprintf(elem_name,
3247c478bd9Sstevel@tonic-gate 						    sizeof (elem_name),
3257c478bd9Sstevel@tonic-gate 			"/%s/%s", *ptr, def.enum_table[count].text);
3267c478bd9Sstevel@tonic-gate 					}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 					if ((fru_read_field(cont_hdl,
329af3025fdSdt 					    &segment_name[numseg], instance,
330af3025fdSdt 					    elem_name, (void**)&data, &dataLen,
331af3025fdSdt 					    &found_path)) != FRU_SUCCESS) {
3327c478bd9Sstevel@tonic-gate 						break;
3337c478bd9Sstevel@tonic-gate 					}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 					if ((fru_get_definition(
3367c478bd9Sstevel@tonic-gate 			def.enum_table[count].text, &def1)) != FRU_SUCCESS) {
3377c478bd9Sstevel@tonic-gate 						break;
3387c478bd9Sstevel@tonic-gate 					}
3397c478bd9Sstevel@tonic-gate 					(void) printf("	%s:  ",\
340af3025fdSdt 					    elem_name);
3417c478bd9Sstevel@tonic-gate 					display_data(data, dataLen, &def1);
3427c478bd9Sstevel@tonic-gate 					(void) printf("\n");
3437c478bd9Sstevel@tonic-gate 				}
3447c478bd9Sstevel@tonic-gate 				iter ++;
3457c478bd9Sstevel@tonic-gate 			} while (iter < iter_cnt);
3467c478bd9Sstevel@tonic-gate 			ptr++;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static char *
convertBinaryToDecimal(char * ptr)3527c478bd9Sstevel@tonic-gate convertBinaryToDecimal(char *ptr)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	int	cnt = 0;
3557c478bd9Sstevel@tonic-gate 	char	*data;
3567c478bd9Sstevel@tonic-gate 	int	str_len;
3577c478bd9Sstevel@tonic-gate 	char	*ret = NULL;
3587c478bd9Sstevel@tonic-gate 	uint64_t	result = 0;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	str_len = strlen(ptr);
3617c478bd9Sstevel@tonic-gate 	data = ptr;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	while (str_len >= 1) {
3647c478bd9Sstevel@tonic-gate 		str_len -= 1;
3657c478bd9Sstevel@tonic-gate 		if (data[str_len] == '0') {
3667c478bd9Sstevel@tonic-gate 			result += (0 * pow(2, cnt));
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		if (data[str_len] == '1') {
3697c478bd9Sstevel@tonic-gate 			result += (1 * pow(2, cnt));
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		cnt++;
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	ret = (char *)lltostr(result, "\n");
3747c478bd9Sstevel@tonic-gate 	return (ret);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * called update_field() to update the field with specific field value.
3797c478bd9Sstevel@tonic-gate  * nodehdl represents the fru, segment represents the segment name in the fru.
3807c478bd9Sstevel@tonic-gate  * field_name represents the field to be updated with the value field_value.
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static int
convert_update(fru_nodehdl_t nodehdl,char * segment,char * field_name,char * field_value)3847c478bd9Sstevel@tonic-gate convert_update(fru_nodehdl_t nodehdl, char *segment, char *field_name,
3857c478bd9Sstevel@tonic-gate 							char *field_value)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	uint64_t num = 0;
3887c478bd9Sstevel@tonic-gate 	fru_elemdef_t def;
3897c478bd9Sstevel@tonic-gate 	fru_errno_t	err;
3907c478bd9Sstevel@tonic-gate 	void    *data = NULL;
3917c478bd9Sstevel@tonic-gate 	size_t  dataLen = 0;
3927c478bd9Sstevel@tonic-gate 	int	i;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if ((err = fru_get_definition(field_name, &def)) != FRU_SUCCESS) {
3957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3967c478bd9Sstevel@tonic-gate 		    gettext("Failed to get definition %s:  %s\n"),
3977c478bd9Sstevel@tonic-gate 		    field_name, fru_strerror(err));
3987c478bd9Sstevel@tonic-gate 		return (1);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (field_value == NULL) {
4027c478bd9Sstevel@tonic-gate 		return (1);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	switch (def.data_type) {
4067c478bd9Sstevel@tonic-gate 		case    FDTYPE_Binary:
4077c478bd9Sstevel@tonic-gate 			if (def.disp_type != FDISP_Time) {
4087c478bd9Sstevel@tonic-gate 				if (field_value[0] == 'b') {
4097c478bd9Sstevel@tonic-gate 					field_value =
410af3025fdSdt 					    convertBinaryToDecimal((field_value
411af3025fdSdt 					    +1));
4127c478bd9Sstevel@tonic-gate 				}
4137c478bd9Sstevel@tonic-gate 				num = strtoll(field_value, (char **)NULL, 0);
4147c478bd9Sstevel@tonic-gate 				if ((num == 0) && (errno == 0)) {
4157c478bd9Sstevel@tonic-gate 					return (1);
4167c478bd9Sstevel@tonic-gate 				}
4177c478bd9Sstevel@tonic-gate 				data = (void*)&num;
4187c478bd9Sstevel@tonic-gate 				dataLen = sizeof (uint64_t);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 		case    FDTYPE_ByteArray:
4227c478bd9Sstevel@tonic-gate 			return (1);
4237c478bd9Sstevel@tonic-gate 		case    FDTYPE_Unicode:
4247c478bd9Sstevel@tonic-gate 			return (1);
4257c478bd9Sstevel@tonic-gate 		case    FDTYPE_ASCII:
4267c478bd9Sstevel@tonic-gate 			data = (void *) field_value;
4277c478bd9Sstevel@tonic-gate 			dataLen = strlen(field_value);
4287c478bd9Sstevel@tonic-gate 			if (dataLen < def.data_length) {
4297c478bd9Sstevel@tonic-gate 				dataLen++;
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 		case    FDTYPE_Enumeration:
4337c478bd9Sstevel@tonic-gate 			for (i = 0; i < def.enum_count; i++) {
4347c478bd9Sstevel@tonic-gate 				if (strcmp(def.enum_table[i].text,
435af3025fdSdt 				    field_value) == 0) {
43634e5f34eSkmohan 					data = (void *)(uintptr_t)
437af3025fdSdt 					    def.enum_table[i].value;
4387c478bd9Sstevel@tonic-gate 					dataLen = sizeof (uint64_t);
4397c478bd9Sstevel@tonic-gate 					break;
4407c478bd9Sstevel@tonic-gate 				}
4417c478bd9Sstevel@tonic-gate 			}
4427c478bd9Sstevel@tonic-gate 			return (1);
4437c478bd9Sstevel@tonic-gate 		case    FDTYPE_Record:
4447c478bd9Sstevel@tonic-gate 			if (def.iteration_count == 0) {
4457c478bd9Sstevel@tonic-gate 				return (1);
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 			data = NULL;
4487c478bd9Sstevel@tonic-gate 			dataLen = 0;
4497c478bd9Sstevel@tonic-gate 			break;
4507c478bd9Sstevel@tonic-gate 		case    FDTYPE_UNDEFINED:
4517c478bd9Sstevel@tonic-gate 			return (1);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if ((err = fru_update_field(nodehdl, segment, 0, field_name, data,
455af3025fdSdt 	    dataLen)) != FRU_SUCCESS) {
4567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("fru_update_field():  %s\n"),
457af3025fdSdt 		    fru_strerror(err));
4587c478bd9Sstevel@tonic-gate 		return (1);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * called by update_field() when a new data element is created.
4647c478bd9Sstevel@tonic-gate  * it updates the UNIX_Timestamp32 field with the current system time.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate static int
update_unixtimestamp(fru_nodehdl_t nodehdl,char * segment,char ** ptr)4687c478bd9Sstevel@tonic-gate update_unixtimestamp(fru_nodehdl_t nodehdl, char *segment, char **ptr)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	char	*field_name;
4717c478bd9Sstevel@tonic-gate 	time_t	clock;
4727c478bd9Sstevel@tonic-gate 	struct	tm *sp_tm;
4737c478bd9Sstevel@tonic-gate 	fru_errno_t	err = FRU_SUCCESS;
4747c478bd9Sstevel@tonic-gate 	uint64_t	time_data;
4757c478bd9Sstevel@tonic-gate 	size_t		len;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	len = strlen(*ptr) + strlen("UNIX_Timestamp32") + 3;
4787c478bd9Sstevel@tonic-gate 	field_name = alloca(len);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	(void) snprintf(field_name, len, "/%s/UNIX_Timestamp32", *ptr);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	clock = time(NULL);
4837c478bd9Sstevel@tonic-gate 	sp_tm = localtime(&clock);
4847c478bd9Sstevel@tonic-gate 	time_data = (uint64_t)mktime(sp_tm);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if ((err = fru_update_field(nodehdl, segment, 0, field_name,
487af3025fdSdt 	    (void *)&time_data, sizeof (time_data))) != FRU_SUCCESS) {
4887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("fru_update_field():  %s\n"),
489af3025fdSdt 		    fru_strerror(err));
4907c478bd9Sstevel@tonic-gate 		return (1);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	return (0);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * create segment on the specified fru represented by nodehdl.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static int
create_segment(fru_nodehdl_t nodehdl)5007c478bd9Sstevel@tonic-gate create_segment(fru_nodehdl_t nodehdl)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	fru_segdesc_t	seg_desc;
5037c478bd9Sstevel@tonic-gate 	fru_segdef_t	def;
5047c478bd9Sstevel@tonic-gate 	int	cnt;
505821a83dbSps 	int	status;
5067c478bd9Sstevel@tonic-gate 
50706e46062Sjbeloro 	(void) memset(&seg_desc, 0, sizeof (seg_desc));
5087c478bd9Sstevel@tonic-gate 	seg_desc.field.field_perm = 0x6;
5097c478bd9Sstevel@tonic-gate 	seg_desc.field.operations_perm = 0x6;
5107c478bd9Sstevel@tonic-gate 	seg_desc.field.engineering_perm = 0x6;
5117c478bd9Sstevel@tonic-gate 	seg_desc.field.repair_perm = 0x6;
5127c478bd9Sstevel@tonic-gate 
51306e46062Sjbeloro 	(void) memset(&def, 0, sizeof (def));
5147c478bd9Sstevel@tonic-gate 	def.address = 0;
5157c478bd9Sstevel@tonic-gate 	def.desc.raw_data = seg_desc.raw_data;
5167c478bd9Sstevel@tonic-gate 	def.hw_desc.all_bits = 0;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
5197c478bd9Sstevel@tonic-gate 		(void) strncpy(def.name, segment_name[cnt], SEGMENT_NAME_SIZE);
5207c478bd9Sstevel@tonic-gate 		if (cnt == 0) {
5217c478bd9Sstevel@tonic-gate 			def.size = FD_SEGMENT_SIZE;
5227c478bd9Sstevel@tonic-gate 		}
523821a83dbSps 		if ((status = fru_create_segment(nodehdl, &def))
524af3025fdSdt 		    != FRU_SUCCESS) {
5257c478bd9Sstevel@tonic-gate 			continue;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 		return (cnt);
5287c478bd9Sstevel@tonic-gate 	}
529821a83dbSps 	if (status != FRU_SUCCESS)
530821a83dbSps 		(void) fprintf(stderr, gettext("fru_create_segment():  %s\n"),
531af3025fdSdt 		    fru_strerror(status));
5327c478bd9Sstevel@tonic-gate 	return (1);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * called from update_field() when service flag is ON. currently
5377c478bd9Sstevel@tonic-gate  * supported iterated record is InstallationR and fields supported for
5387c478bd9Sstevel@tonic-gate  * update are Geo_North, Geo_East, Geo_Alt, Geo_Location.
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate static int
updateiter_record(fru_nodehdl_t nodehdl,int cnt,char ** ptr,char * field_name,char * field_value)5427c478bd9Sstevel@tonic-gate updateiter_record(fru_nodehdl_t nodehdl, int cnt, char **ptr,
5437c478bd9Sstevel@tonic-gate 			char *field_name, char  *field_value)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	int	iter_cnt  = 0;
5467c478bd9Sstevel@tonic-gate 	char	rec_name[512];
5477c478bd9Sstevel@tonic-gate 	void	*data = NULL;
5487c478bd9Sstevel@tonic-gate 	char	*tmpptr = NULL;
5497c478bd9Sstevel@tonic-gate 	size_t	dataLen = 0;
5507c478bd9Sstevel@tonic-gate 	char	**elem_ptr;
5517c478bd9Sstevel@tonic-gate 	int	found = 0;
5527c478bd9Sstevel@tonic-gate 	int	index;
5537c478bd9Sstevel@tonic-gate 	int	total_cnt;
554821a83dbSps 	fru_errno_t	err;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	static  char    *elem_list[] = {"/Geo_North", "/Geo_East",\
5577c478bd9Sstevel@tonic-gate 				"/Geo_Alt", "/Geo_Location"};
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	elem_ptr = elem_list;
5607c478bd9Sstevel@tonic-gate 	total_cnt = sizeof (elem_list)/sizeof (*elem_list);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	for (index = 0; index < total_cnt; index++) {
5637c478bd9Sstevel@tonic-gate 		tmpptr = strrchr(field_name, '/');
5647c478bd9Sstevel@tonic-gate 		if (tmpptr == NULL) {
5657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5667c478bd9Sstevel@tonic-gate 			    gettext("Error:  Data Element not known\n"));
5677c478bd9Sstevel@tonic-gate 			return (1);
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 		if ((strncmp(*elem_ptr, tmpptr, strlen(*elem_ptr)) != 0)) {
5707c478bd9Sstevel@tonic-gate 			elem_ptr++;
5717c478bd9Sstevel@tonic-gate 			continue;
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		found = 1;
5747c478bd9Sstevel@tonic-gate 		break;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (found == 0) {
5787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5797c478bd9Sstevel@tonic-gate 		    gettext("Error:  Update not allowed for field:  %s\n"),
5807c478bd9Sstevel@tonic-gate 		    field_name);
5817c478bd9Sstevel@tonic-gate 		return (1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if ((fru_get_num_iterations(nodehdl, &segment_name[cnt], 0,
585af3025fdSdt 	    *ptr, &iter_cnt, NULL)) != FRU_SUCCESS) {
5867c478bd9Sstevel@tonic-gate 		return (1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/* add a new Iterated Record if complete path is not given */
5907c478bd9Sstevel@tonic-gate 	if (iter_cnt == 0) {
5917c478bd9Sstevel@tonic-gate 		(void) snprintf(rec_name, sizeof (rec_name), "/%s[+]", *ptr);
592821a83dbSps 		if ((err = fru_update_field(nodehdl, segment_name[cnt], 0,
593af3025fdSdt 		    rec_name, data, dataLen)) != FRU_SUCCESS) {
594821a83dbSps 			(void) fprintf(stderr,
595821a83dbSps 			gettext("fru_update_field():  %s\n"),
596af3025fdSdt 			    fru_strerror(err));
597*bc54f855SJohn Levon 			return (1);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		iter_cnt = 1;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]%s",
6047c478bd9Sstevel@tonic-gate 	    *ptr, iter_cnt-1, strrchr(field_name, '/'));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if ((convert_update(nodehdl, segment_name[cnt], rec_name,
607af3025fdSdt 	    field_value)) != 0) {
6087c478bd9Sstevel@tonic-gate 		return (1);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/* update success  now update the unix timestamp */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]",
6147c478bd9Sstevel@tonic-gate 	    *ptr, iter_cnt-1);
6157c478bd9Sstevel@tonic-gate 	tmpptr = rec_name;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/* update UNIX_Timestamp32 with creation time */
6187c478bd9Sstevel@tonic-gate 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
619af3025fdSdt 	    &tmpptr)) != 0) {
6207c478bd9Sstevel@tonic-gate 		return (1);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (0);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate static int
update_field(fru_nodehdl_t nodehdl,char * field_name,char * field_value)6277c478bd9Sstevel@tonic-gate update_field(fru_nodehdl_t nodehdl, char *field_name, char *field_value)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	fru_elemdef_t	def;
6307c478bd9Sstevel@tonic-gate 	unsigned char	*data;
6317c478bd9Sstevel@tonic-gate 	size_t	dataLen;
6327c478bd9Sstevel@tonic-gate 	char	*found_path = NULL;
6337c478bd9Sstevel@tonic-gate 	int	cnt;
6347c478bd9Sstevel@tonic-gate 	char	**ptr;
6357c478bd9Sstevel@tonic-gate 	fru_strlist_t	elem;
6367c478bd9Sstevel@tonic-gate 	int	elem_cnt;
6377c478bd9Sstevel@tonic-gate 	int	add_flag = 1;
6387c478bd9Sstevel@tonic-gate 	int	total_cnt;
639821a83dbSps 	int	status;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (service_mode) {
6427c478bd9Sstevel@tonic-gate 		ptr = serv_data_list;
6437c478bd9Sstevel@tonic-gate 		total_cnt = sizeof (serv_data_list)/sizeof (*serv_data_list);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < total_cnt; cnt++) {
6467c478bd9Sstevel@tonic-gate 			if ((strncmp(*ptr, &field_name[1], strlen(*ptr)) \
647af3025fdSdt 			    != 0) && (strncmp(*ptr, &field_name[0],
648af3025fdSdt 			    strlen(*ptr)) != 0)) {
6497c478bd9Sstevel@tonic-gate 				ptr++;
6507c478bd9Sstevel@tonic-gate 				add_flag = 0;
6517c478bd9Sstevel@tonic-gate 				continue;
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			add_flag = 1;
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 	} else {
6577c478bd9Sstevel@tonic-gate 		ptr = cust_data_list;
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/* look for the field in either of the segment if found update it */
6617c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
6627c478bd9Sstevel@tonic-gate 		if ((fru_read_field(nodehdl, &segment_name[cnt], 0, field_name,
663af3025fdSdt 		    (void **) &data, &dataLen, &found_path)) != FRU_SUCCESS) {
6647c478bd9Sstevel@tonic-gate 			continue;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
6677c478bd9Sstevel@tonic-gate 			if (def.iteration_count != 0) {
6687c478bd9Sstevel@tonic-gate 				if ((updateiter_record(nodehdl, cnt, ptr,
669af3025fdSdt 				    field_name, field_value)) != 0) {
6707c478bd9Sstevel@tonic-gate 					return (1);
6717c478bd9Sstevel@tonic-gate 				}
6727c478bd9Sstevel@tonic-gate 				return (0);
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		if ((convert_update(nodehdl, segment_name[cnt],
677af3025fdSdt 		    field_name, field_value)) != 0) {
6787c478bd9Sstevel@tonic-gate 			return (1);
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		/* update UNIX_Timestamp32 with update time */
6827c478bd9Sstevel@tonic-gate 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
683af3025fdSdt 		    ptr)) != 0) {
6847c478bd9Sstevel@tonic-gate 			return (1);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		return (0);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	elem.num = 0;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/* field not found add the the record in one of the segment */
6927c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
69380148899SSurya Prakki 		(void) fru_list_elems_in(nodehdl, segment_name[cnt], &elem);
6947c478bd9Sstevel@tonic-gate 		for (elem_cnt = 0; elem_cnt < elem.num; elem_cnt++) {
6957c478bd9Sstevel@tonic-gate 			if ((strcmp(*ptr, elem.strs[elem_cnt])) == 0) {
6967c478bd9Sstevel@tonic-gate 				add_flag = 0;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		if (add_flag) {
7017c478bd9Sstevel@tonic-gate 			if ((fru_add_element(nodehdl, segment_name[cnt],
702af3025fdSdt 			    *ptr)) != FRU_SUCCESS) {
7037c478bd9Sstevel@tonic-gate 				continue;
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
7087c478bd9Sstevel@tonic-gate 			if (def.iteration_count != 0) {
7097c478bd9Sstevel@tonic-gate 				if ((updateiter_record(nodehdl, cnt, ptr,
710af3025fdSdt 				    field_name, field_value)) != 0) {
7117c478bd9Sstevel@tonic-gate 					return (1);
7127c478bd9Sstevel@tonic-gate 				}
7137c478bd9Sstevel@tonic-gate 				return (0);
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		/* update UNIX_Timestamp32 with creation time */
7187c478bd9Sstevel@tonic-gate 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
719af3025fdSdt 		    ptr)) != 0) {
7207c478bd9Sstevel@tonic-gate 			return (1);
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/* record added update the field with the value */
7247c478bd9Sstevel@tonic-gate 		if ((convert_update(nodehdl, segment_name[cnt], field_name,
725af3025fdSdt 		    field_value)) != 0) {
7267c478bd9Sstevel@tonic-gate 			return (1);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		return (0);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/* segment not present, create one and add the record */
7327c478bd9Sstevel@tonic-gate 	cnt = create_segment(nodehdl);
7337c478bd9Sstevel@tonic-gate 	if (cnt == 1) {
7347c478bd9Sstevel@tonic-gate 		return (1);
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
737821a83dbSps 	if ((status = fru_add_element(nodehdl, segment_name[cnt], *ptr))
738af3025fdSdt 	    != FRU_SUCCESS) {
739821a83dbSps 		(void) fprintf(stderr, gettext("fru_add_element():  %s\n"),
740af3025fdSdt 		    fru_strerror(status));
7417c478bd9Sstevel@tonic-gate 		return (1);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
7457c478bd9Sstevel@tonic-gate 		if (def.iteration_count != 0) {
7467c478bd9Sstevel@tonic-gate 			if ((updateiter_record(nodehdl,  cnt, ptr,
747af3025fdSdt 			    field_name, field_value)) != 0) {
7487c478bd9Sstevel@tonic-gate 				return (1);
7497c478bd9Sstevel@tonic-gate 			}
7507c478bd9Sstevel@tonic-gate 			return (0);
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/* update UNIX_Timestamp32 with creation time */
7557c478bd9Sstevel@tonic-gate 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
756af3025fdSdt 	    ptr)) != 0) {
7577c478bd9Sstevel@tonic-gate 		return (1);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if ((convert_update(nodehdl, segment_name[cnt], field_name,
761af3025fdSdt 	    field_value)) != 0) {
7627c478bd9Sstevel@tonic-gate 		return (1);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677513a25aSdt static int
update_node_data(fru_nodehdl_t node)7687c478bd9Sstevel@tonic-gate update_node_data(fru_nodehdl_t node)
7697c478bd9Sstevel@tonic-gate {
7707513a25aSdt 	int	i;
771af3025fdSdt 	int	status = 0;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (service_mode) {
7747c478bd9Sstevel@tonic-gate 		for (i = 0; i < svcargc; i += 2)
7757513a25aSdt 			if (update_field(node, svcargv[i], svcargv[i + 1])) {
7767513a25aSdt 				status = 1;
7777513a25aSdt 			}
7787c478bd9Sstevel@tonic-gate 	} else {
7797513a25aSdt 		status = update_field(node, "/Customer_DataR/Cust_Data",
780af3025fdSdt 		    customer_data);
7817c478bd9Sstevel@tonic-gate 	}
7827513a25aSdt 	return (status);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate static void
walk_tree(fru_nodehdl_t node,const char * prior_path,int process_tree)7867c478bd9Sstevel@tonic-gate walk_tree(fru_nodehdl_t node, const char *prior_path, int process_tree)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	char	*name, path[PATH_MAX];
7897513a25aSdt 	int	process_self = process_tree, status, update_status = 0;
7907c478bd9Sstevel@tonic-gate 	fru_nodehdl_t	 next_node;
7917c478bd9Sstevel@tonic-gate 	fru_node_t	type;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	if ((status = fru_get_node_type(node, &type)) != FRU_SUCCESS) {
7947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7957c478bd9Sstevel@tonic-gate 		    gettext("Error getting FRU tree node type:  %s\n"),
7967c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
7977c478bd9Sstevel@tonic-gate 		exit(1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if ((status = fru_get_name_from_hdl(node, &name)) != FRU_SUCCESS) {
8017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8027c478bd9Sstevel@tonic-gate 		    gettext("Error getting name of FRU tree node:  %s\n"),
8037c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
8047c478bd9Sstevel@tonic-gate 		exit(1);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * Build the current path
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s/%s", prior_path, name)
8127c478bd9Sstevel@tonic-gate 	    >= sizeof (path)) {
8137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
814af3025fdSdt 		    gettext("FRU tree path would overflow buffer\n"));
8157c478bd9Sstevel@tonic-gate 		exit(1);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	free(name);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * Process the node
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	if (list_only) {
8247c478bd9Sstevel@tonic-gate 		(void) printf("%s%s\n", path, ((type == FRU_NODE_FRU) ?
825af3025fdSdt 		    " (fru)" : ((type == FRU_NODE_CONTAINER) ?
826af3025fdSdt 		    " (container)" : "")));
8277c478bd9Sstevel@tonic-gate 	} else if ((process_tree || (process_self = pathmatch(path))) &&
828af3025fdSdt 	    (type == FRU_NODE_CONTAINER)) {
8297c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", path);
8307513a25aSdt 		if (update) {
8317513a25aSdt 			status = update_node_data(node);
8327513a25aSdt 			update_status = status;
8337513a25aSdt 		}
8347c478bd9Sstevel@tonic-gate 		print_node_data(node);
8357513a25aSdt 		if (!recursive) {
8367513a25aSdt 			exit(status);
8377513a25aSdt 		}
8387c478bd9Sstevel@tonic-gate 	} else if (process_self && !recursive) {
8397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8407c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" is not a container\n"), path);
8417c478bd9Sstevel@tonic-gate 		exit(1);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/*
8467c478bd9Sstevel@tonic-gate 	 * Recurse
8477c478bd9Sstevel@tonic-gate 	 */
8487c478bd9Sstevel@tonic-gate 	if (fru_get_child(node, &next_node) == FRU_SUCCESS)
8497c478bd9Sstevel@tonic-gate 		walk_tree(next_node, path, process_self);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if (fru_get_peer(node, &next_node) == FRU_SUCCESS)
8527c478bd9Sstevel@tonic-gate 		walk_tree(next_node, prior_path, process_tree);
8537513a25aSdt 
8547513a25aSdt 	/*
8557513a25aSdt 	 * when update_node_data failed, need to exit with return value 1
8567513a25aSdt 	 */
8577513a25aSdt 	if (update_status)
8587513a25aSdt 		exit(1);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])8627c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	int	process_tree = 0, option, status;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	fru_nodehdl_t  root;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	command = argv[0];
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	opterr = 0;	/*  "getopt" should not print to "stderr"  */
8727c478bd9Sstevel@tonic-gate 	while ((option = getopt(argc, argv, "lrs")) != EOF) {
8737c478bd9Sstevel@tonic-gate 	switch (option) {
8747c478bd9Sstevel@tonic-gate 		case 'l':
8757c478bd9Sstevel@tonic-gate 			list_only = 1;
8767c478bd9Sstevel@tonic-gate 			break;
8777c478bd9Sstevel@tonic-gate 		case 'r':
8787c478bd9Sstevel@tonic-gate 			recursive = 1;
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		case 's':
8817c478bd9Sstevel@tonic-gate 			service_mode = 1;
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 		default:
8847c478bd9Sstevel@tonic-gate 			usage();
8857c478bd9Sstevel@tonic-gate 			return (1);
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	argc -= optind;
8907c478bd9Sstevel@tonic-gate 	argv += optind;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (argc == 0) {
8937c478bd9Sstevel@tonic-gate 		process_tree   = 1;
8947c478bd9Sstevel@tonic-gate 		recursive = 1;
8957c478bd9Sstevel@tonic-gate 	} else {
8967c478bd9Sstevel@tonic-gate 		if (list_only) {
8977c478bd9Sstevel@tonic-gate 			usage();
8987c478bd9Sstevel@tonic-gate 			return (1);
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		frupath = argv[0];
9027c478bd9Sstevel@tonic-gate 		if (*frupath == 0) {
9037c478bd9Sstevel@tonic-gate 			usage();
9047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9057c478bd9Sstevel@tonic-gate 			    gettext("\"frupath\" should not be empty\n"));
9067c478bd9Sstevel@tonic-gate 			return (1);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		argc--;
9107c478bd9Sstevel@tonic-gate 		argv++;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		if (argc > 0) {
9137c478bd9Sstevel@tonic-gate 			update = 1;
9147c478bd9Sstevel@tonic-gate 			if (service_mode) {
9157c478bd9Sstevel@tonic-gate 				if ((argc % 2) != 0) {
9167c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
9177c478bd9Sstevel@tonic-gate 					    gettext("Must specify "
918af3025fdSdt 					    "field-value pairs "
919af3025fdSdt 					    "for update\n"));
9207c478bd9Sstevel@tonic-gate 					return (1);
9217c478bd9Sstevel@tonic-gate 				}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 				if (validate_fieldnames(argc, argv) != 0) {
9247c478bd9Sstevel@tonic-gate 					return (1);
9257c478bd9Sstevel@tonic-gate 				}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 				svcargc = argc;
9287c478bd9Sstevel@tonic-gate 				svcargv = argv;
9297c478bd9Sstevel@tonic-gate 			} else if (argc == 1)
9307c478bd9Sstevel@tonic-gate 				customer_data = argv[0];
9317c478bd9Sstevel@tonic-gate 			else {
9327c478bd9Sstevel@tonic-gate 				usage();
9337c478bd9Sstevel@tonic-gate 				return (1);
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if ((status = fru_open_data_source("picl", NULL)) != FRU_SUCCESS) {
9397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9407c478bd9Sstevel@tonic-gate 		    gettext("Unable to access FRU data source: 	%s\n"),
9417c478bd9Sstevel@tonic-gate 		    fru_strerror(status));
9427c478bd9Sstevel@tonic-gate 		return (1);
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	if ((status = fru_get_root(&root)) == FRU_NODENOTFOUND) {
946043728a3Sdt 		(void) fprintf(stderr,
947043728a3Sdt 		    gettext("This system does not support PICL "
948043728a3Sdt 		    "infrastructure to provide FRUID data\n"
949043728a3Sdt 		    "Please use the platform SP to access the FRUID "
950043728a3Sdt 		    "information\n"));
9517c478bd9Sstevel@tonic-gate 		return (1);
9527c478bd9Sstevel@tonic-gate 	} else if (status != FRU_SUCCESS) {
9537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9547c478bd9Sstevel@tonic-gate 		    gettext("Unable to access FRU ID data "
955af3025fdSdt 		    "due to data source error\n"));
9567c478bd9Sstevel@tonic-gate 		return (1);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	walk_tree(root, "", process_tree);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if ((frupath != NULL) && (!found_frupath)) {
9627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9637c478bd9Sstevel@tonic-gate 		    gettext("\"%s\" not found\n"),
9647c478bd9Sstevel@tonic-gate 		    frupath);
9657c478bd9Sstevel@tonic-gate 		return (1);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	return (0);
9697c478bd9Sstevel@tonic-gate }
970