xref: /illumos-gate/usr/src/cmd/fruadm/fruadm.c (revision 7c478bd9)
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 2002-2003 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 = 0x8000000000000000;
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 *)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 
502 	seg_desc.field.field_perm = 0x6;
503 	seg_desc.field.operations_perm = 0x6;
504 	seg_desc.field.engineering_perm = 0x6;
505 	seg_desc.field.repair_perm = 0x6;
506 
507 	def.address = 0;
508 	def.desc.raw_data = seg_desc.raw_data;
509 	def.hw_desc.all_bits = 0;
510 
511 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
512 		(void) strncpy(def.name, segment_name[cnt], SEGMENT_NAME_SIZE);
513 		if (cnt == 0) {
514 			def.size = FD_SEGMENT_SIZE;
515 		}
516 		if ((fru_create_segment(nodehdl, &def)) != FRU_SUCCESS) {
517 			continue;
518 		}
519 		return (cnt);
520 	}
521 	return (1);
522 }
523 
524 /*
525  * called from update_field() when service flag is ON. currently
526  * supported iterated record is InstallationR and fields supported for
527  * update are Geo_North, Geo_East, Geo_Alt, Geo_Location.
528  */
529 
530 static int
531 updateiter_record(fru_nodehdl_t nodehdl, int cnt, char **ptr,
532 			char *field_name, char  *field_value)
533 {
534 	int	iter_cnt  = 0;
535 	char	rec_name[512];
536 	void	*data = NULL;
537 	char	*tmpptr = NULL;
538 	size_t	dataLen = 0;
539 	char	**elem_ptr;
540 	int	found = 0;
541 	int	index;
542 	int	total_cnt;
543 
544 	static  char    *elem_list[] = {"/Geo_North", "/Geo_East",\
545 				"/Geo_Alt", "/Geo_Location"};
546 
547 	elem_ptr = elem_list;
548 	total_cnt = sizeof (elem_list)/sizeof (*elem_list);
549 
550 	for (index = 0; index < total_cnt; index++) {
551 		tmpptr = strrchr(field_name, '/');
552 		if (tmpptr == NULL) {
553 			(void) fprintf(stderr,
554 			    gettext("Error:  Data Element not known\n"));
555 			return (1);
556 		}
557 		if ((strncmp(*elem_ptr, tmpptr, strlen(*elem_ptr)) != 0)) {
558 			elem_ptr++;
559 			continue;
560 		}
561 		found = 1;
562 		break;
563 	}
564 
565 	if (found == 0) {
566 		(void) fprintf(stderr,
567 		    gettext("Error:  Update not allowed for field:  %s\n"),
568 		    field_name);
569 		return (1);
570 	}
571 
572 	if ((fru_get_num_iterations(nodehdl, &segment_name[cnt], 0,
573 			*ptr, &iter_cnt, NULL)) != FRU_SUCCESS) {
574 		return (1);
575 	}
576 
577 	/* add a new Iterated Record if complete path is not given */
578 	if (iter_cnt == 0) {
579 		(void) snprintf(rec_name, sizeof (rec_name), "/%s[+]", *ptr);
580 		if ((fru_update_field(nodehdl, segment_name[cnt], 0,
581 			rec_name, data, dataLen)) != FRU_SUCCESS) {
582 			return (1);
583 		}
584 
585 		iter_cnt = 1;
586 	}
587 
588 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]%s",
589 	    *ptr, iter_cnt-1, strrchr(field_name, '/'));
590 
591 	if ((convert_update(nodehdl, segment_name[cnt], rec_name,
592 				field_value)) != 0) {
593 		return (1);
594 	}
595 
596 	/* update success  now update the unix timestamp */
597 
598 	(void) snprintf(rec_name, sizeof (rec_name), "/%s[%d]",
599 	    *ptr, iter_cnt-1);
600 	tmpptr = rec_name;
601 
602 	/* update UNIX_Timestamp32 with creation time */
603 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
604 				&tmpptr)) != 0) {
605 		return (1);
606 	}
607 
608 	return (0);
609 }
610 
611 static int
612 update_field(fru_nodehdl_t nodehdl, char *field_name, char *field_value)
613 {
614 	fru_elemdef_t	def;
615 	unsigned char	*data;
616 	size_t	dataLen;
617 	char	*found_path = NULL;
618 	int	cnt;
619 	char	**ptr;
620 	fru_strlist_t	elem;
621 	int	elem_cnt;
622 	int	add_flag = 1;
623 	int	total_cnt;
624 
625 	if (service_mode) {
626 		ptr = serv_data_list;
627 		total_cnt = sizeof (serv_data_list)/sizeof (*serv_data_list);
628 
629 		for (cnt = 0; cnt < total_cnt; cnt++) {
630 			if ((strncmp(*ptr, &field_name[1], strlen(*ptr)) \
631 				!= 0) && (strncmp(*ptr, &field_name[0],
632 						strlen(*ptr)) != 0)) {
633 				ptr++;
634 				add_flag = 0;
635 				continue;
636 			}
637 			add_flag = 1;
638 			break;
639 		}
640 	} else {
641 		ptr = cust_data_list;
642 	}
643 
644 	/* look for the field in either of the segment if found update it */
645 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
646 		if ((fru_read_field(nodehdl, &segment_name[cnt], 0, field_name,
647 		(void **) &data, &dataLen, &found_path)) != FRU_SUCCESS) {
648 			continue;
649 		}
650 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
651 			if (def.iteration_count != 0) {
652 				if ((updateiter_record(nodehdl, cnt, ptr,
653 					field_name, field_value)) != 0) {
654 					return (1);
655 				}
656 				return (0);
657 			}
658 		}
659 
660 		if ((convert_update(nodehdl, segment_name[cnt],
661 				field_name, field_value)) != 0) {
662 			return (1);
663 		}
664 
665 		/* update UNIX_Timestamp32 with update time */
666 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
667 						ptr)) != 0) {
668 			return (1);
669 		}
670 		return (0);
671 	}
672 
673 	elem.num = 0;
674 
675 	/* field not found add the the record in one of the segment */
676 	for (cnt = 0; cnt < NUM_OF_SEGMENT; cnt++) {
677 		fru_list_elems_in(nodehdl, segment_name[cnt], &elem);
678 		for (elem_cnt = 0; elem_cnt < elem.num; elem_cnt++) {
679 			if ((strcmp(*ptr, elem.strs[elem_cnt])) == 0) {
680 				add_flag = 0;
681 			}
682 		}
683 
684 		if (add_flag) {
685 			if ((fru_add_element(nodehdl, segment_name[cnt],
686 							*ptr)) != FRU_SUCCESS) {
687 				continue;
688 			}
689 		}
690 
691 		if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
692 			if (def.iteration_count != 0) {
693 				if ((updateiter_record(nodehdl, cnt, ptr,
694 					field_name, field_value)) != 0) {
695 					return (1);
696 				}
697 				return (0);
698 			}
699 		}
700 
701 		/* update UNIX_Timestamp32 with creation time */
702 		if ((update_unixtimestamp(nodehdl, segment_name[cnt],
703 							ptr)) != 0) {
704 			return (1);
705 		}
706 
707 		/* record added update the field with the value */
708 		if ((convert_update(nodehdl, segment_name[cnt], field_name,
709 						field_value)) != 0) {
710 			return (1);
711 		}
712 		return (0);
713 	}
714 
715 	/* segment not present, create one and add the record */
716 	cnt = create_segment(nodehdl);
717 	if (cnt == 1) {
718 		return (1);
719 	}
720 
721 	if ((fru_add_element(nodehdl, segment_name[cnt], *ptr))
722 						!= FRU_SUCCESS) {
723 		return (1);
724 	}
725 
726 	if ((fru_get_definition(*ptr, &def)) == FRU_SUCCESS) {
727 		if (def.iteration_count != 0) {
728 			if ((updateiter_record(nodehdl,  cnt, ptr,
729 				field_name, field_value)) != 0) {
730 				return (1);
731 			}
732 			return (0);
733 		}
734 	}
735 
736 	/* update UNIX_Timestamp32 with creation time */
737 	if ((update_unixtimestamp(nodehdl, segment_name[cnt],
738 					ptr)) != 0) {
739 		return (1);
740 	}
741 
742 	if ((convert_update(nodehdl, segment_name[cnt], field_name,
743 					field_value)) != 0) {
744 		return (1);
745 	}
746 	return (0);
747 }
748 
749 static void
750 update_node_data(fru_nodehdl_t node)
751 {
752 	int  i;
753 
754 	if (service_mode) {
755 		for (i = 0; i < svcargc; i += 2)
756 			(void) update_field(node, svcargv[i], svcargv[i + 1]);
757 	} else {
758 		(void) update_field(node, "/Customer_DataR/Cust_Data",
759 							customer_data);
760 	}
761 }
762 
763 static void
764 walk_tree(fru_nodehdl_t node, const char *prior_path, int process_tree)
765 {
766 	char	*name, path[PATH_MAX];
767 	int	process_self = process_tree, status;
768 	fru_nodehdl_t	 next_node;
769 	fru_node_t	type;
770 
771 	if ((status = fru_get_node_type(node, &type)) != FRU_SUCCESS) {
772 		(void) fprintf(stderr,
773 		    gettext("Error getting FRU tree node type:  %s\n"),
774 		    fru_strerror(status));
775 		exit(1);
776 	}
777 
778 	if ((status = fru_get_name_from_hdl(node, &name)) != FRU_SUCCESS) {
779 		(void) fprintf(stderr,
780 		    gettext("Error getting name of FRU tree node:  %s\n"),
781 		    fru_strerror(status));
782 		exit(1);
783 	}
784 
785 
786 	/*
787 	 * Build the current path
788 	 */
789 	if (snprintf(path, sizeof (path), "%s/%s", prior_path, name)
790 	    >= sizeof (path)) {
791 		(void) fprintf(stderr,
792 			gettext("FRU tree path would overflow buffer\n"));
793 		exit(1);
794 	}
795 
796 	free(name);
797 
798 	/*
799 	 * Process the node
800 	 */
801 	if (list_only) {
802 		(void) printf("%s%s\n", path, ((type == FRU_NODE_FRU) ?
803 			" (fru)" : ((type == FRU_NODE_CONTAINER) ?
804 			" (container)" : "")));
805 	} else if ((process_tree || (process_self = pathmatch(path))) &&
806 			(type == FRU_NODE_CONTAINER)) {
807 		(void) printf("%s\n", path);
808 		if (update) update_node_data(node);
809 		print_node_data(node);
810 		if (!recursive) exit(0);
811 	} else if (process_self && !recursive) {
812 		(void) fprintf(stderr,
813 		    gettext("\"%s\" is not a container\n"), path);
814 		exit(1);
815 	}
816 
817 
818 	/*
819 	 * Recurse
820 	 */
821 	if (fru_get_child(node, &next_node) == FRU_SUCCESS)
822 		walk_tree(next_node, path, process_self);
823 
824 	if (fru_get_peer(node, &next_node) == FRU_SUCCESS)
825 		walk_tree(next_node, prior_path, process_tree);
826 }
827 
828 static int
829 has_system_controller()
830 {
831 	char platform[PATH_MAX];
832 
833 	int size;
834 
835 	if (((size = sysinfo(SI_PLATFORM, platform, sizeof (platform)))
836 		< 0) || (size > sizeof (platform)))
837 		return (-1);
838 
839 	if ((strcmp("SUNW,Sun-Fire", platform) == 0) ||
840 		(strcmp("SUNW,Sun-Fire-15000", platform) == 0)) {
841 		return (1);
842 	}
843 
844 	return (0);
845 }
846 
847 int
848 main(int argc, char *argv[])
849 {
850 	int	process_tree = 0, option, status;
851 
852 	fru_nodehdl_t  root;
853 
854 
855 	command = argv[0];
856 
857 	opterr = 0;	/*  "getopt" should not print to "stderr"  */
858 	while ((option = getopt(argc, argv, "lrs")) != EOF) {
859 	switch (option) {
860 		case 'l':
861 			list_only = 1;
862 			break;
863 		case 'r':
864 			recursive = 1;
865 			break;
866 		case 's':
867 			service_mode = 1;
868 			break;
869 		default:
870 			usage();
871 			return (1);
872 		}
873 	}
874 
875 	argc -= optind;
876 	argv += optind;
877 
878 	if (argc == 0) {
879 		process_tree   = 1;
880 		recursive = 1;
881 	} else {
882 		if (list_only) {
883 			usage();
884 			return (1);
885 		}
886 
887 		frupath = argv[0];
888 		if (*frupath == 0) {
889 			usage();
890 			(void) fprintf(stderr,
891 			    gettext("\"frupath\" should not be empty\n"));
892 			return (1);
893 		}
894 
895 		argc--;
896 		argv++;
897 
898 		if (argc > 0) {
899 			update = 1;
900 			if (service_mode) {
901 				if ((argc % 2) != 0) {
902 					(void) fprintf(stderr,
903 					    gettext("Must specify "
904 						"field-value pairs "
905 						"for update\n"));
906 					return (1);
907 				}
908 
909 				if (validate_fieldnames(argc, argv) != 0) {
910 					return (1);
911 				}
912 
913 				svcargc = argc;
914 				svcargv = argv;
915 			} else if (argc == 1)
916 				customer_data = argv[0];
917 			else {
918 				usage();
919 				return (1);
920 			}
921 		}
922 	}
923 
924 	if ((status = fru_open_data_source("picl", NULL)) != FRU_SUCCESS) {
925 		(void) fprintf(stderr,
926 		    gettext("Unable to access FRU data source: 	%s\n"),
927 		    fru_strerror(status));
928 		return (1);
929 	}
930 
931 	if ((status = fru_get_root(&root)) == FRU_NODENOTFOUND) {
932 		if (has_system_controller() == 1) {
933 			(void) fprintf(stderr,
934 				gettext("Access FRUs from the "
935 					"System Controller\n"));
936 		} else {
937 			(void) fprintf(stderr,
938 				gettext("This system does not provide "
939 					"FRU ID data\n"));
940 
941 		}
942 		return (1);
943 	} else if (status != FRU_SUCCESS) {
944 		(void) fprintf(stderr,
945 		    gettext("Unable to access FRU ID data "
946 			"due to data source error\n"));
947 		return (1);
948 	}
949 
950 	walk_tree(root, "", process_tree);
951 
952 	if ((frupath != NULL) && (!found_frupath)) {
953 		(void) fprintf(stderr,
954 		    gettext("\"%s\" not found\n"),
955 		    frupath);
956 		return (1);
957 	}
958 
959 	return (0);
960 }
961