xref: /titanic_51/usr/src/cmd/fruadm/fruadm.c (revision fe598cdcd847f8359013532d5c691bb6190378c0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <libintl.h>
34 #include <libfru.h>
35 #include <errno.h>
36 #include <math.h>
37 #include <alloca.h>
38 #include <assert.h>
39 #include <sys/systeminfo.h>
40 
41 #define	NUM_OF_SEGMENT	1
42 #define	SEGMENT_NAME_SIZE	2
43 
44 #define	FD_SEGMENT_SIZE	2949
45 
46 static char  *command, *customer_data = NULL, *frupath = NULL, **svcargv;
47 
48 /* DataElement supported in the customer operation */
49 static  char    *cust_data_list[] = {"Customer_DataR"};
50 
51 /* DataElement supported in the service operation */
52 static  char    *serv_data_list[] = {"InstallationR", "ECO_CurrentR"};
53 
54 /* currently supported segment name */
55 static  char    *segment_name[] = {"FD"};
56 
57 static int   found_frupath = 0, list_only = 0, recursive = 0,
58     service_mode = 0, svcargc, update = 0;
59 
60 
61 static void
62 usage(void)
63 {
64 	(void) fprintf(stderr,
65 		gettext("Usage:  %s [ -l ] | [ [ -r ] frupath [ text ] ]\n"),
66 		command);
67 }
68 
69 static int
70 validate_fieldnames(int argc, char *argv[])
71 {
72 	static int	num = sizeof (serv_data_list)/sizeof (*serv_data_list);
73 
74 	char		*fieldname;
75 
76 	int		i, j, match, status;
77 
78 	fru_elemdef_t	definition;
79 
80 
81 	for (i = 0; i < argc; i += 2) {
82 		if (argv[i][0] == '/') {
83 			fieldname = &argv[i][1];
84 		} else {
85 			fieldname = &argv[i][0];
86 		}
87 
88 		match = 0;
89 		for (j = 0; j < num; j++) {
90 			if (strncmp(fieldname, serv_data_list[j],
91 				strlen(serv_data_list[j])) == 0) {
92 				match = 1;
93 			}
94 		}
95 		if (!match) {
96 			(void) fprintf(stderr,
97 			    gettext("\"%s\" is not a supported field\n"),
98 			    argv[i]);
99 			return (1);
100 		}
101 
102 		if ((status = fru_get_definition(argv[i], &definition))
103 		    != FRU_SUCCESS) {
104 			(void) fprintf(stderr, gettext("\"%s\":  %s\n"),
105 			    argv[i],
106 			    fru_strerror(status));
107 			return (1);
108 		} else if ((definition.data_type == FDTYPE_Record) ||
109 		    (definition.data_type == FDTYPE_UNDEFINED)) {
110 			(void) fprintf(stderr,
111 			    gettext("\"%s\" is not a field\n"), argv[i]);
112 			return (1);
113 		}
114 	}
115 
116 	return (0);
117 }
118 
119 static int
120 pathmatch(const char *path)
121 {
122 	char  *match;
123 
124 	if ((frupath != NULL) &&
125 	    ((match = strstr(path, frupath)) != NULL) &&
126 	    ((match + strlen(frupath)) == (path + strlen(path))) &&
127 	    ((match == path) || (*(match - 1) == '/'))) {
128 		found_frupath = 1;
129 		return (1);
130 	}
131 	return (0);
132 }
133 
134 static void
135 displayBinary(unsigned char *data, size_t length, fru_elemdef_t *def)
136 {
137 	int	i = 0;
138 	uint64_t	lldata;
139 	uint64_t	mask;
140 
141 	if (def->disp_type == FDISP_Hex) {
142 		for (i = 0; i < length; i++) {
143 			(void) printf("%02X", data[i]);
144 		}
145 		return;
146 	}
147 
148 	(void) memcpy(&lldata, data, sizeof (lldata));
149 	switch (def->disp_type) {
150 		case FDISP_Binary:
151 		{
152 			mask = 0x8000000000000000ULL;
153 			for (i = 0; i < (sizeof (uint64_t) *8); i++) {
154 				if (lldata & (mask >> i)) {
155 					(void) printf("1");
156 				} else {
157 					(void) printf("0");
158 				}
159 			}
160 			return;
161 		}
162 		case FDISP_Octal:
163 		{
164 			(void) printf("%llo", lldata);
165 			return;
166 		}
167 		case FDISP_Decimal:
168 		{
169 			(void) printf("%lld", lldata);
170 			return;
171 		}
172 		case FDISP_Time:
173 		{
174 			char buffer[PATH_MAX];
175 			time_t time;
176 			time = (time_t)lldata;
177 			(void) strftime(buffer, PATH_MAX, "%C",
178 			    localtime(&time));
179 			(void) printf("%s", buffer);
180 			return;
181 		}
182 	}
183 }
184 
185 static void
186 displayBAasBinary(unsigned char *data, size_t length)
187 {
188 	int i;
189 	unsigned char mask;
190 
191 	for (i = 0; i < length; i++) {
192 		/*
193 		 * make a mask for the high order bit and adjust down through
194 		 * all the bits.
195 		 */
196 		for (mask = 0x80; mask > 0; mask /= 2) {
197 			if ((data[i] & mask) != 0) /* bit must be on */
198 				(void) printf("1");
199 			else /* bit is off... */
200 				(void) printf("0");
201 		}
202 	}
203 	(void) printf("\n");
204 }
205 
206 static void
207 display_data(unsigned char *data, size_t length, fru_elemdef_t *def)
208 {
209 	int i = 0;
210 	uint64_t	lldata;
211 
212 	if (data == 0x00) {
213 		(void) printf("\n");
214 		return;
215 	}
216 
217 	switch (def->data_type) {
218 	case FDTYPE_Binary:
219 	{
220 		displayBinary(data, length, def);
221 		return;
222 	}
223 
224 	case FDTYPE_ByteArray:
225 	{
226 		switch (def->disp_type) {
227 		case FDISP_Binary:
228 			displayBAasBinary(data, length);
229 			return;
230 		case FDISP_Hex:
231 			for (i = 0; i < length; i++) {
232 				(void) printf("%02X", data[i]);
233 			}
234 			return;
235 		}
236 		return;
237 	}
238 	case FDTYPE_Unicode:
239 		assert(gettext("Unicode not yet supported") == 0);
240 		break;
241 	case FDTYPE_ASCII:
242 	{
243 		char *disp_str = (char *)alloca(length+1);
244 		for (i = 0; i < length; i++)
245 			disp_str[i] = data[i];
246 			disp_str[i] = '\0';
247 			(void) printf("%s", disp_str);
248 			return;
249 	}
250 
251 	case FDTYPE_Enumeration:
252 	{
253 		lldata = strtoull((const char *)data, NULL, 0);
254 		for (i = 0; i < def->enum_count; i++) {
255 			if (def->enum_table[i].value == lldata) {
256 			/* strdup such that map_... can realloc if necessary. */
257 				char *tmp = strdup(def->enum_table[i].text);
258 				(void) printf("%s", tmp);
259 				free(tmp);
260 				return;
261 			}
262 		}
263 		(void) printf(gettext("Unrecognized Value:  0x"));
264 		for (i = 0; i < sizeof (uint64_t); i++)
265 			(void) printf("%02X", data[i]);
266 		break;
267 	}
268 	default:
269 		break;
270 	}
271 }
272 
273 static void
274 print_node_data(fru_nodehdl_t cont_hdl)
275 {
276 	int	iter_cnt = 0;
277 	int	iter;
278 	int	numseg;
279 	int	list_cnt;
280 	unsigned char	*data;
281 	size_t	dataLen;
282 	int	total_cnt;
283 	char	*found_path = NULL;
284 	fru_elemdef_t	 def, def1;
285 	int	instance = 0;
286 	char	**ptr;
287 	char	**tmp_ptr;
288 	int	count = 0;
289 	char	elem_name[PATH_MAX];
290 
291 	if (service_mode) {
292 		total_cnt = sizeof (serv_data_list)/sizeof (*serv_data_list);
293 		ptr = serv_data_list;
294 	} else {
295 		total_cnt = sizeof (cust_data_list)/sizeof (*cust_data_list);
296 		ptr = cust_data_list;
297 	}
298 	tmp_ptr = ptr;
299 
300 	for (numseg = 0; numseg < NUM_OF_SEGMENT; numseg++) {
301 		ptr = tmp_ptr;
302 		for (list_cnt = 0; list_cnt < total_cnt; list_cnt++) {
303 			if ((fru_get_definition(*ptr, &def)) != FRU_SUCCESS) {
304 				continue;
305 			}
306 			if ((fru_get_num_iterations(cont_hdl,
307 				&segment_name[numseg], 0, *ptr,
308 					&iter_cnt, NULL)) != FRU_SUCCESS) {
309 				iter_cnt = 0;
310 			}
311 			iter = 0;
312 			do {
313 				for (count = 0; count < def.enum_count;
314 								count++) {
315 					if (def.iteration_type !=
316 							FRU_NOT_ITERATED) {
317 						(void) snprintf(elem_name,
318 						    sizeof (elem_name),
319 			"/%s[%d]/%s", *ptr, iter, def.enum_table[count].text);
320 					} else {
321 						(void) snprintf(elem_name,
322 						    sizeof (elem_name),
323 			"/%s/%s", *ptr, def.enum_table[count].text);
324 					}
325 
326 					if ((fru_read_field(cont_hdl,
327 		&segment_name[numseg], instance, elem_name, (void**)&data,
328 				&dataLen, &found_path)) != FRU_SUCCESS) {
329 						break;
330 					}
331 
332 					if ((fru_get_definition(
333 			def.enum_table[count].text, &def1)) != FRU_SUCCESS) {
334 						break;
335 					}
336 					(void) printf("	%s:  ",\
337 								elem_name);
338 					display_data(data, dataLen, &def1);
339 					(void) printf("\n");
340 				}
341 				iter ++;
342 			} while (iter < iter_cnt);
343 			ptr++;
344 		}
345 	}
346 }
347 
348 static char *
349 convertBinaryToDecimal(char *ptr)
350 {
351 	int	cnt = 0;
352 	char	*data;
353 	int	str_len;
354 	char	*ret = NULL;
355 	uint64_t	result = 0;
356 
357 	str_len = strlen(ptr);
358 	data = ptr;
359 
360 	while (str_len >= 1) {
361 		str_len -= 1;
362 		if (data[str_len] == '0') {
363 			result += (0 * pow(2, cnt));
364 		}
365 		if (data[str_len] == '1') {
366 			result += (1 * pow(2, cnt));
367 		}
368 		cnt++;
369 	}
370 	ret = (char *)lltostr(result, "\n");
371 	return (ret);
372 }
373 
374 /*
375  * called update_field() to update the field with specific field value.
376  * nodehdl represents the fru, segment represents the segment name in the fru.
377  * field_name represents the field to be updated with the value field_value.
378  */
379 
380 static int
381 convert_update(fru_nodehdl_t nodehdl, char *segment, char *field_name,
382 							char *field_value)
383 {
384 	uint64_t num = 0;
385 	fru_elemdef_t def;
386 	fru_errno_t	err;
387 	void    *data = NULL;
388 	size_t  dataLen = 0;
389 	int	i;
390 
391 	if ((err = fru_get_definition(field_name, &def)) != FRU_SUCCESS) {
392 		(void) fprintf(stderr,
393 		    gettext("Failed to get definition %s:  %s\n"),
394 		    field_name, fru_strerror(err));
395 		return (1);
396 	}
397 
398 	if (field_value == NULL) {
399 		return (1);
400 	}
401 
402 	switch (def.data_type) {
403 		case    FDTYPE_Binary:
404 			if (def.disp_type != FDISP_Time) {
405 				if (field_value[0] == 'b') {
406 					field_value =
407 					convertBinaryToDecimal((field_value+1));
408 				}
409 				num = strtoll(field_value, (char **)NULL, 0);
410 				if ((num == 0) && (errno == 0)) {
411 					return (1);
412 				}
413 				data = (void*)&num;
414 				dataLen = sizeof (uint64_t);
415 			}
416 			break;
417 		case    FDTYPE_ByteArray:
418 			return (1);
419 		case    FDTYPE_Unicode:
420 			return (1);
421 		case    FDTYPE_ASCII:
422 			data = (void *) field_value;
423 			dataLen = strlen(field_value);
424 			if (dataLen < def.data_length) {
425 				dataLen++;
426 			}
427 			break;
428 		case    FDTYPE_Enumeration:
429 			for (i = 0; i < def.enum_count; i++) {
430 				if (strcmp(def.enum_table[i].text,
431 							field_value) == 0) {
432 					data = (void *)(uintptr_t)
433 							def.enum_table[i].value;
434 					dataLen = sizeof (uint64_t);
435 					break;
436 				}
437 			}
438 			return (1);
439 		case    FDTYPE_Record:
440 			if (def.iteration_count == 0) {
441 				return (1);
442 			}
443 			data = NULL;
444 			dataLen = 0;
445 			break;
446 		case    FDTYPE_UNDEFINED:
447 			return (1);
448 	}
449 
450 	if ((err = fru_update_field(nodehdl, segment, 0, field_name, data,
451 						dataLen)) != FRU_SUCCESS) {
452 		(void) fprintf(stderr, gettext("fru_update_field():  %s\n"),
453 						fru_strerror(err));
454 		return (1);
455 	}
456 	return (0);
457 }
458 /*
459  * called by update_field() when a new data element is created.
460  * it updates the UNIX_Timestamp32 field with the current system time.
461  */
462 
463 static int
464 update_unixtimestamp(fru_nodehdl_t nodehdl, char *segment, char **ptr)
465 {
466 	char	*field_name;
467 	time_t	clock;
468 	struct	tm *sp_tm;
469 	fru_errno_t	err = FRU_SUCCESS;
470 	uint64_t	time_data;
471 	size_t		len;
472 
473 	len = strlen(*ptr) + strlen("UNIX_Timestamp32") + 3;
474 	field_name = alloca(len);
475 
476 	(void) snprintf(field_name, len, "/%s/UNIX_Timestamp32", *ptr);
477 
478 	clock = time(NULL);
479 	sp_tm = localtime(&clock);
480 	time_data = (uint64_t)mktime(sp_tm);
481 
482 	if ((err = fru_update_field(nodehdl, segment, 0, field_name,
483 		(void *)&time_data, sizeof (time_data))) != FRU_SUCCESS) {
484 		(void) fprintf(stderr, gettext("fru_update_field():  %s\n"),
485 						fru_strerror(err));
486 		return (1);
487 	}
488 	return (0);
489 }
490 
491 /*
492  * create segment on the specified fru represented by nodehdl.
493  */
494 
495 static int
496 create_segment(fru_nodehdl_t nodehdl)
497 {
498 	fru_segdesc_t	seg_desc;
499 	fru_segdef_t	def;
500 	int	cnt;
501 	int	status;
502 
503 	(void) memset(&seg_desc, 0, sizeof (seg_desc));
504 	seg_desc.field.field_perm = 0x6;
505 	seg_desc.field.operations_perm = 0x6;
506 	seg_desc.field.engineering_perm = 0x6;
507 	seg_desc.field.repair_perm = 0x6;
508 
509 	(void) memset(&def, 0, sizeof (def));
510 	def.address = 0;
511 	def.desc.raw_data = seg_desc.raw_data;
512 	def.hw_desc.all_bits = 0;
513 
514 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
515 		(void) strncpy(def.name, segment_name[cnt], SEGMENT_NAME_SIZE);
516 		if (cnt == 0) {
517 			def.size = FD_SEGMENT_SIZE;
518 		}
519 		if ((status = fru_create_segment(nodehdl, &def))
520 					!= FRU_SUCCESS) {
521 			continue;
522 		}
523 		return (cnt);
524 	}
525 	if (status != FRU_SUCCESS)
526 		(void) fprintf(stderr, gettext("fru_create_segment():  %s\n"),
527 				fru_strerror(status));
528 	return (1);
529 }
530 
531 /*
532  * called from update_field() when service flag is ON. currently
533  * supported iterated record is InstallationR and fields supported for
534  * update are Geo_North, Geo_East, Geo_Alt, Geo_Location.
535  */
536 
537 static int
538 updateiter_record(fru_nodehdl_t nodehdl, int cnt, char **ptr,
539 			char *field_name, char  *field_value)
540 {
541 	int	iter_cnt  = 0;
542 	char	rec_name[512];
543 	void	*data = NULL;
544 	char	*tmpptr = NULL;
545 	size_t	dataLen = 0;
546 	char	**elem_ptr;
547 	int	found = 0;
548 	int	index;
549 	int	total_cnt;
550 	fru_errno_t	err;
551 
552 	static  char    *elem_list[] = {"/Geo_North", "/Geo_East",\
553 				"/Geo_Alt", "/Geo_Location"};
554 
555 	elem_ptr = elem_list;
556 	total_cnt = sizeof (elem_list)/sizeof (*elem_list);
557 
558 	for (index = 0; index < total_cnt; index++) {
559 		tmpptr = strrchr(field_name, '/');
560 		if (tmpptr == NULL) {
561 			(void) fprintf(stderr,
562 			    gettext("Error:  Data Element not known\n"));
563 			return (1);
564 		}
565 		if ((strncmp(*elem_ptr, tmpptr, strlen(*elem_ptr)) != 0)) {
566 			elem_ptr++;
567 			continue;
568 		}
569 		found = 1;
570 		break;
571 	}
572 
573 	if (found == 0) {
574 		(void) fprintf(stderr,
575 		    gettext("Error:  Update not allowed for field:  %s\n"),
576 		    field_name);
577 		return (1);
578 	}
579 
580 	if ((fru_get_num_iterations(nodehdl, &segment_name[cnt], 0,
581 			*ptr, &iter_cnt, NULL)) != FRU_SUCCESS) {
582 		return (1);
583 	}
584 
585 	/* add a new Iterated Record if complete path is not given */
586 	if (iter_cnt == 0) {
587 		(void) snprintf(rec_name, sizeof (rec_name), "/%s[+]", *ptr);
588 		if ((err = fru_update_field(nodehdl, segment_name[cnt], 0,
589 			rec_name, data, dataLen)) != FRU_SUCCESS) {
590 			(void) fprintf(stderr,
591 			gettext("fru_update_field():  %s\n"),
592 			fru_strerror(err));
593 		return (1);
594 		}
595 
596 		iter_cnt = 1;
597 	}
598 
599 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]%s",
600 	    *ptr, iter_cnt-1, strrchr(field_name, '/'));
601 
602 	if ((convert_update(nodehdl, segment_name[cnt], rec_name,
603 				field_value)) != 0) {
604 		return (1);
605 	}
606 
607 	/* update success  now update the unix timestamp */
608 
609 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]",
610 	    *ptr, iter_cnt-1);
611 	tmpptr = rec_name;
612 
613 	/* update UNIX_Timestamp32 with creation time */
614 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
615 				&tmpptr)) != 0) {
616 		return (1);
617 	}
618 
619 	return (0);
620 }
621 
622 static int
623 update_field(fru_nodehdl_t nodehdl, char *field_name, char *field_value)
624 {
625 	fru_elemdef_t	def;
626 	unsigned char	*data;
627 	size_t	dataLen;
628 	char	*found_path = NULL;
629 	int	cnt;
630 	char	**ptr;
631 	fru_strlist_t	elem;
632 	int	elem_cnt;
633 	int	add_flag = 1;
634 	int	total_cnt;
635 	int	status;
636 
637 	if (service_mode) {
638 		ptr = serv_data_list;
639 		total_cnt = sizeof (serv_data_list)/sizeof (*serv_data_list);
640 
641 		for (cnt = 0; cnt < total_cnt; cnt++) {
642 			if ((strncmp(*ptr, &field_name[1], strlen(*ptr)) \
643 				!= 0) && (strncmp(*ptr, &field_name[0],
644 						strlen(*ptr)) != 0)) {
645 				ptr++;
646 				add_flag = 0;
647 				continue;
648 			}
649 			add_flag = 1;
650 			break;
651 		}
652 	} else {
653 		ptr = cust_data_list;
654 	}
655 
656 	/* look for the field in either of the segment if found update it */
657 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
658 		if ((fru_read_field(nodehdl, &segment_name[cnt], 0, field_name,
659 		(void **) &data, &dataLen, &found_path)) != FRU_SUCCESS) {
660 			continue;
661 		}
662 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
663 			if (def.iteration_count != 0) {
664 				if ((updateiter_record(nodehdl, cnt, ptr,
665 					field_name, field_value)) != 0) {
666 					return (1);
667 				}
668 				return (0);
669 			}
670 		}
671 
672 		if ((convert_update(nodehdl, segment_name[cnt],
673 				field_name, field_value)) != 0) {
674 			return (1);
675 		}
676 
677 		/* update UNIX_Timestamp32 with update time */
678 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
679 						ptr)) != 0) {
680 			return (1);
681 		}
682 		return (0);
683 	}
684 
685 	elem.num = 0;
686 
687 	/* field not found add the the record in one of the segment */
688 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
689 		fru_list_elems_in(nodehdl, segment_name[cnt], &elem);
690 		for (elem_cnt = 0; elem_cnt < elem.num; elem_cnt++) {
691 			if ((strcmp(*ptr, elem.strs[elem_cnt])) == 0) {
692 				add_flag = 0;
693 			}
694 		}
695 
696 		if (add_flag) {
697 			if ((fru_add_element(nodehdl, segment_name[cnt],
698 							*ptr)) != FRU_SUCCESS) {
699 				continue;
700 			}
701 		}
702 
703 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
704 			if (def.iteration_count != 0) {
705 				if ((updateiter_record(nodehdl, cnt, ptr,
706 					field_name, field_value)) != 0) {
707 					return (1);
708 				}
709 				return (0);
710 			}
711 		}
712 
713 		/* update UNIX_Timestamp32 with creation time */
714 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
715 							ptr)) != 0) {
716 			return (1);
717 		}
718 
719 		/* record added update the field with the value */
720 		if ((convert_update(nodehdl, segment_name[cnt], field_name,
721 						field_value)) != 0) {
722 			return (1);
723 		}
724 		return (0);
725 	}
726 
727 	/* segment not present, create one and add the record */
728 	cnt = create_segment(nodehdl);
729 	if (cnt == 1) {
730 		return (1);
731 	}
732 
733 	if ((status = fru_add_element(nodehdl, segment_name[cnt], *ptr))
734 						!= FRU_SUCCESS) {
735 		(void) fprintf(stderr, gettext("fru_add_element():  %s\n"),
736 					fru_strerror(status));
737 		return (1);
738 	}
739 
740 	if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
741 		if (def.iteration_count != 0) {
742 			if ((updateiter_record(nodehdl,  cnt, ptr,
743 				field_name, field_value)) != 0) {
744 				return (1);
745 			}
746 			return (0);
747 		}
748 	}
749 
750 	/* update UNIX_Timestamp32 with creation time */
751 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
752 					ptr)) != 0) {
753 		return (1);
754 	}
755 
756 	if ((convert_update(nodehdl, segment_name[cnt], field_name,
757 					field_value)) != 0) {
758 		return (1);
759 	}
760 	return (0);
761 }
762 
763 static int
764 update_node_data(fru_nodehdl_t node)
765 {
766 	int	i;
767 	int	status;
768 
769 	if (service_mode) {
770 		for (i = 0; i < svcargc; i += 2)
771 			if (update_field(node, svcargv[i], svcargv[i + 1])) {
772 				status = 1;
773 			}
774 	} else {
775 		status = update_field(node, "/Customer_DataR/Cust_Data",
776 							customer_data);
777 	}
778 	return (status);
779 }
780 
781 static void
782 walk_tree(fru_nodehdl_t node, const char *prior_path, int process_tree)
783 {
784 	char	*name, path[PATH_MAX];
785 	int	process_self = process_tree, status, update_status = 0;
786 	fru_nodehdl_t	 next_node;
787 	fru_node_t	type;
788 
789 	if ((status = fru_get_node_type(node, &type)) != FRU_SUCCESS) {
790 		(void) fprintf(stderr,
791 		    gettext("Error getting FRU tree node type:  %s\n"),
792 		    fru_strerror(status));
793 		exit(1);
794 	}
795 
796 	if ((status = fru_get_name_from_hdl(node, &name)) != FRU_SUCCESS) {
797 		(void) fprintf(stderr,
798 		    gettext("Error getting name of FRU tree node:  %s\n"),
799 		    fru_strerror(status));
800 		exit(1);
801 	}
802 
803 
804 	/*
805 	 * Build the current path
806 	 */
807 	if (snprintf(path, sizeof (path), "%s/%s", prior_path, name)
808 	    >= sizeof (path)) {
809 		(void) fprintf(stderr,
810 			gettext("FRU tree path would overflow buffer\n"));
811 		exit(1);
812 	}
813 
814 	free(name);
815 
816 	/*
817 	 * Process the node
818 	 */
819 	if (list_only) {
820 		(void) printf("%s%s\n", path, ((type == FRU_NODE_FRU) ?
821 			" (fru)" : ((type == FRU_NODE_CONTAINER) ?
822 			" (container)" : "")));
823 	} else if ((process_tree || (process_self = pathmatch(path))) &&
824 			(type == FRU_NODE_CONTAINER)) {
825 		(void) printf("%s\n", path);
826 		if (update) {
827 			status = update_node_data(node);
828 			update_status = status;
829 		}
830 		print_node_data(node);
831 		if (!recursive) {
832 			exit(status);
833 		}
834 	} else if (process_self && !recursive) {
835 		(void) fprintf(stderr,
836 		    gettext("\"%s\" is not a container\n"), path);
837 		exit(1);
838 	}
839 
840 
841 	/*
842 	 * Recurse
843 	 */
844 	if (fru_get_child(node, &next_node) == FRU_SUCCESS)
845 		walk_tree(next_node, path, process_self);
846 
847 	if (fru_get_peer(node, &next_node) == FRU_SUCCESS)
848 		walk_tree(next_node, prior_path, process_tree);
849 
850 	/*
851 	 * when update_node_data failed, need to exit with return value 1
852 	 */
853 	if (update_status)
854 		exit(1);
855 }
856 
857 static int
858 has_system_controller()
859 {
860 	char platform[PATH_MAX];
861 
862 	int size;
863 
864 	if (((size = sysinfo(SI_PLATFORM, platform, sizeof (platform)))
865 		< 0) || (size > sizeof (platform)))
866 		return (-1);
867 
868 	if ((strcmp("SUNW,Sun-Fire", platform) == 0) ||
869 		(strcmp("SUNW,Sun-Fire-15000", platform) == 0)) {
870 		return (1);
871 	}
872 
873 	return (0);
874 }
875 
876 int
877 main(int argc, char *argv[])
878 {
879 	int	process_tree = 0, option, status;
880 
881 	fru_nodehdl_t  root;
882 
883 
884 	command = argv[0];
885 
886 	opterr = 0;	/*  "getopt" should not print to "stderr"  */
887 	while ((option = getopt(argc, argv, "lrs")) != EOF) {
888 	switch (option) {
889 		case 'l':
890 			list_only = 1;
891 			break;
892 		case 'r':
893 			recursive = 1;
894 			break;
895 		case 's':
896 			service_mode = 1;
897 			break;
898 		default:
899 			usage();
900 			return (1);
901 		}
902 	}
903 
904 	argc -= optind;
905 	argv += optind;
906 
907 	if (argc == 0) {
908 		process_tree   = 1;
909 		recursive = 1;
910 	} else {
911 		if (list_only) {
912 			usage();
913 			return (1);
914 		}
915 
916 		frupath = argv[0];
917 		if (*frupath == 0) {
918 			usage();
919 			(void) fprintf(stderr,
920 			    gettext("\"frupath\" should not be empty\n"));
921 			return (1);
922 		}
923 
924 		argc--;
925 		argv++;
926 
927 		if (argc > 0) {
928 			update = 1;
929 			if (service_mode) {
930 				if ((argc % 2) != 0) {
931 					(void) fprintf(stderr,
932 					    gettext("Must specify "
933 						"field-value pairs "
934 						"for update\n"));
935 					return (1);
936 				}
937 
938 				if (validate_fieldnames(argc, argv) != 0) {
939 					return (1);
940 				}
941 
942 				svcargc = argc;
943 				svcargv = argv;
944 			} else if (argc == 1)
945 				customer_data = argv[0];
946 			else {
947 				usage();
948 				return (1);
949 			}
950 		}
951 	}
952 
953 	if ((status = fru_open_data_source("picl", NULL)) != FRU_SUCCESS) {
954 		(void) fprintf(stderr,
955 		    gettext("Unable to access FRU data source: 	%s\n"),
956 		    fru_strerror(status));
957 		return (1);
958 	}
959 
960 	if ((status = fru_get_root(&root)) == FRU_NODENOTFOUND) {
961 		if (has_system_controller() == 1) {
962 			(void) fprintf(stderr,
963 				gettext("Access FRUs from the "
964 					"System Controller\n"));
965 		} else {
966 			(void) fprintf(stderr,
967 				gettext("This system does not provide "
968 					"FRU ID data\n"));
969 
970 		}
971 		return (1);
972 	} else if (status != FRU_SUCCESS) {
973 		(void) fprintf(stderr,
974 		    gettext("Unable to access FRU ID data "
975 			"due to data source error\n"));
976 		return (1);
977 	}
978 
979 	walk_tree(root, "", process_tree);
980 
981 	if ((frupath != NULL) && (!found_frupath)) {
982 		(void) fprintf(stderr,
983 		    gettext("\"%s\" not found\n"),
984 		    frupath);
985 		return (1);
986 	}
987 
988 	return (0);
989 }
990