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