xref: /titanic_41/usr/src/cmd/fruadm/fruadm.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
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 2006 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 void
764 update_node_data(fru_nodehdl_t node)
765 {
766 	int  i;
767 
768 	if (service_mode) {
769 		for (i = 0; i < svcargc; i += 2)
770 			(void) update_field(node, svcargv[i], svcargv[i + 1]);
771 	} else {
772 		(void) update_field(node, "/Customer_DataR/Cust_Data",
773 							customer_data);
774 	}
775 }
776 
777 static void
778 walk_tree(fru_nodehdl_t node, const char *prior_path, int process_tree)
779 {
780 	char	*name, path[PATH_MAX];
781 	int	process_self = process_tree, status;
782 	fru_nodehdl_t	 next_node;
783 	fru_node_t	type;
784 
785 	if ((status = fru_get_node_type(node, &type)) != FRU_SUCCESS) {
786 		(void) fprintf(stderr,
787 		    gettext("Error getting FRU tree node type:  %s\n"),
788 		    fru_strerror(status));
789 		exit(1);
790 	}
791 
792 	if ((status = fru_get_name_from_hdl(node, &name)) != FRU_SUCCESS) {
793 		(void) fprintf(stderr,
794 		    gettext("Error getting name of FRU tree node:  %s\n"),
795 		    fru_strerror(status));
796 		exit(1);
797 	}
798 
799 
800 	/*
801 	 * Build the current path
802 	 */
803 	if (snprintf(path, sizeof (path), "%s/%s", prior_path, name)
804 	    >= sizeof (path)) {
805 		(void) fprintf(stderr,
806 			gettext("FRU tree path would overflow buffer\n"));
807 		exit(1);
808 	}
809 
810 	free(name);
811 
812 	/*
813 	 * Process the node
814 	 */
815 	if (list_only) {
816 		(void) printf("%s%s\n", path, ((type == FRU_NODE_FRU) ?
817 			" (fru)" : ((type == FRU_NODE_CONTAINER) ?
818 			" (container)" : "")));
819 	} else if ((process_tree || (process_self = pathmatch(path))) &&
820 			(type == FRU_NODE_CONTAINER)) {
821 		(void) printf("%s\n", path);
822 		if (update) update_node_data(node);
823 		print_node_data(node);
824 		if (!recursive) exit(0);
825 	} else if (process_self && !recursive) {
826 		(void) fprintf(stderr,
827 		    gettext("\"%s\" is not a container\n"), path);
828 		exit(1);
829 	}
830 
831 
832 	/*
833 	 * Recurse
834 	 */
835 	if (fru_get_child(node, &next_node) == FRU_SUCCESS)
836 		walk_tree(next_node, path, process_self);
837 
838 	if (fru_get_peer(node, &next_node) == FRU_SUCCESS)
839 		walk_tree(next_node, prior_path, process_tree);
840 }
841 
842 static int
843 has_system_controller()
844 {
845 	char platform[PATH_MAX];
846 
847 	int size;
848 
849 	if (((size = sysinfo(SI_PLATFORM, platform, sizeof (platform)))
850 		< 0) || (size > sizeof (platform)))
851 		return (-1);
852 
853 	if ((strcmp("SUNW,Sun-Fire", platform) == 0) ||
854 		(strcmp("SUNW,Sun-Fire-15000", platform) == 0)) {
855 		return (1);
856 	}
857 
858 	return (0);
859 }
860 
861 int
862 main(int argc, char *argv[])
863 {
864 	int	process_tree = 0, option, status;
865 
866 	fru_nodehdl_t  root;
867 
868 
869 	command = argv[0];
870 
871 	opterr = 0;	/*  "getopt" should not print to "stderr"  */
872 	while ((option = getopt(argc, argv, "lrs")) != EOF) {
873 	switch (option) {
874 		case 'l':
875 			list_only = 1;
876 			break;
877 		case 'r':
878 			recursive = 1;
879 			break;
880 		case 's':
881 			service_mode = 1;
882 			break;
883 		default:
884 			usage();
885 			return (1);
886 		}
887 	}
888 
889 	argc -= optind;
890 	argv += optind;
891 
892 	if (argc == 0) {
893 		process_tree   = 1;
894 		recursive = 1;
895 	} else {
896 		if (list_only) {
897 			usage();
898 			return (1);
899 		}
900 
901 		frupath = argv[0];
902 		if (*frupath == 0) {
903 			usage();
904 			(void) fprintf(stderr,
905 			    gettext("\"frupath\" should not be empty\n"));
906 			return (1);
907 		}
908 
909 		argc--;
910 		argv++;
911 
912 		if (argc > 0) {
913 			update = 1;
914 			if (service_mode) {
915 				if ((argc % 2) != 0) {
916 					(void) fprintf(stderr,
917 					    gettext("Must specify "
918 						"field-value pairs "
919 						"for update\n"));
920 					return (1);
921 				}
922 
923 				if (validate_fieldnames(argc, argv) != 0) {
924 					return (1);
925 				}
926 
927 				svcargc = argc;
928 				svcargv = argv;
929 			} else if (argc == 1)
930 				customer_data = argv[0];
931 			else {
932 				usage();
933 				return (1);
934 			}
935 		}
936 	}
937 
938 	if ((status = fru_open_data_source("picl", NULL)) != FRU_SUCCESS) {
939 		(void) fprintf(stderr,
940 		    gettext("Unable to access FRU data source: 	%s\n"),
941 		    fru_strerror(status));
942 		return (1);
943 	}
944 
945 	if ((status = fru_get_root(&root)) == FRU_NODENOTFOUND) {
946 		if (has_system_controller() == 1) {
947 			(void) fprintf(stderr,
948 				gettext("Access FRUs from the "
949 					"System Controller\n"));
950 		} else {
951 			(void) fprintf(stderr,
952 				gettext("This system does not provide "
953 					"FRU ID data\n"));
954 
955 		}
956 		return (1);
957 	} else if (status != FRU_SUCCESS) {
958 		(void) fprintf(stderr,
959 		    gettext("Unable to access FRU ID data "
960 			"due to data source error\n"));
961 		return (1);
962 	}
963 
964 	walk_tree(root, "", process_tree);
965 
966 	if ((frupath != NULL) && (!found_frupath)) {
967 		(void) fprintf(stderr,
968 		    gettext("\"%s\" not found\n"),
969 		    frupath);
970 		return (1);
971 	}
972 
973 	return (0);
974 }
975