xref: /onnv-gate/usr/src/lib/libfru/libfrupicl/frupicl.c (revision 1124:330dd028780c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1124Skmohan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <alloca.h>
300Sstevel@tonic-gate #include <picl.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include "picldefs.h"
360Sstevel@tonic-gate #include "fru_data.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "libfruds.h"
390Sstevel@tonic-gate #include "libfrup.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /* ========================================================================= */
420Sstevel@tonic-gate #define	TREEHDL_TO_PICLHDL(treehdl) ((picl_nodehdl_t)treehdl)
430Sstevel@tonic-gate #define	PICLHDL_TO_TREEHDL(piclhdl) ((fru_treehdl_t)piclhdl)
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	TREESEGHDL_TO_PICLHDL(treeseghdl) ((picl_nodehdl_t)treeseghdl)
460Sstevel@tonic-gate #define	PICLHDL_TO_TREESEGHDL(piclhdl) ((fru_treeseghdl_t)piclhdl)
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /* Cache of the root node for quick checks */
490Sstevel@tonic-gate static picl_nodehdl_t picl_root_node;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /* ========================================================================= */
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Map the PICL errors the plugin would give me to FRU errors
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate static fru_errno_t
map_plugin_err(int picl_err)570Sstevel@tonic-gate map_plugin_err(int picl_err)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	switch (picl_err) {
600Sstevel@tonic-gate 		case PICL_SUCCESS:
610Sstevel@tonic-gate 			return (FRU_SUCCESS);
620Sstevel@tonic-gate 		case PICL_PERMDENIED:
630Sstevel@tonic-gate 			return (FRU_INVALPERM);
640Sstevel@tonic-gate 		case PICL_PROPEXISTS:
650Sstevel@tonic-gate 			return (FRU_DUPSEG);
660Sstevel@tonic-gate 		case PICL_NOSPACE:
670Sstevel@tonic-gate 			return (FRU_NOSPACE);
68*1124Skmohan 		case PICL_NORESPONSE:
69*1124Skmohan 			return (FRU_NORESPONSE);
70*1124Skmohan 		case PICL_PROPNOTFOUND:
71*1124Skmohan 			return (FRU_NODENOTFOUND);
72*1124Skmohan 		case PICL_ENDOFLIST:
73*1124Skmohan 			return (FRU_DATANOTFOUND);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	return (FRU_IOERROR);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /* ========================================================================= */
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * cause a refresh of the sub-nodes by writing anything to the container
810Sstevel@tonic-gate  * property of the node.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static fru_errno_t
update_data_nodes(picl_nodehdl_t handle)840Sstevel@tonic-gate update_data_nodes(picl_nodehdl_t handle)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	uint32_t container = FRUDATA_DELETE_TAG_KEY;
870Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if ((picl_err = picl_set_propval_by_name(handle,
900Sstevel@tonic-gate 		PICL_PROP_CONTAINER, (void *)&container,
910Sstevel@tonic-gate 		sizeof (container))) != PICL_SUCCESS) {
920Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	return (FRU_SUCCESS);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* ========================================================================= */
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * picl like function which gets a string property with the proper length
1010Sstevel@tonic-gate  * NOTE: returns picl errno values NOT fru_errno_t
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static int
get_strprop_by_name(picl_nodehdl_t handle,char * prop_name,char ** string)1040Sstevel@tonic-gate get_strprop_by_name(picl_nodehdl_t handle, char *prop_name, char **string)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1070Sstevel@tonic-gate 	picl_prophdl_t proph;
1080Sstevel@tonic-gate 	picl_propinfo_t prop_info;
1090Sstevel@tonic-gate 	char *tmp_buf = NULL;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if ((picl_err = picl_get_propinfo_by_name(handle, prop_name,
1120Sstevel@tonic-gate 		&prop_info, &proph)) != PICL_SUCCESS) {
1130Sstevel@tonic-gate 		return (picl_err);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	tmp_buf = malloc((sizeof (*tmp_buf) * prop_info.size));
1170Sstevel@tonic-gate 	if (tmp_buf == NULL) {
1180Sstevel@tonic-gate 		return (PICL_FAILURE);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if ((picl_err = picl_get_propval(proph, tmp_buf, prop_info.size))
1220Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1230Sstevel@tonic-gate 		free(tmp_buf);
1240Sstevel@tonic-gate 		return (picl_err);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	*string = tmp_buf;
1280Sstevel@tonic-gate 	return (PICL_SUCCESS);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /* ========================================================================= */
1320Sstevel@tonic-gate static fru_errno_t
fpt_get_name_from_hdl(fru_treehdl_t node,char ** name)1330Sstevel@tonic-gate fpt_get_name_from_hdl(fru_treehdl_t node, char **name)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1360Sstevel@tonic-gate 	char *tmp_name = NULL;
1370Sstevel@tonic-gate 	char *label = NULL;
1380Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/* get the name */
1410Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_NAME,
1420Sstevel@tonic-gate 		&tmp_name)) != PICL_SUCCESS) {
143*1124Skmohan 		return (map_plugin_err(picl_err));
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* get the label, if any */
1470Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_LABEL,
1480Sstevel@tonic-gate 		&label)) != PICL_SUCCESS) {
1490Sstevel@tonic-gate 		if (picl_err != PICL_PROPNOTFOUND) {
1500Sstevel@tonic-gate 			free(tmp_name);
151*1124Skmohan 			return (map_plugin_err(picl_err));
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 		/* else PICL_PROPNOTFOUND is OK because not all nodes */
1540Sstevel@tonic-gate 		/* will have a label. */
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* construct the name as nessecary */
1580Sstevel@tonic-gate 	if (label == NULL) {
1590Sstevel@tonic-gate 		*name = strdup(tmp_name);
1600Sstevel@tonic-gate 	} else {
1610Sstevel@tonic-gate #define	FRU_LABEL_PADDING 10
1620Sstevel@tonic-gate 		size_t buf_size = strlen(tmp_name) + strlen(label) +
1630Sstevel@tonic-gate 			FRU_LABEL_PADDING;
1640Sstevel@tonic-gate 		char *tmp = malloc(buf_size);
1650Sstevel@tonic-gate 		if (tmp == NULL) {
1660Sstevel@tonic-gate 			free(tmp_name);
1670Sstevel@tonic-gate 			free(label);
1680Sstevel@tonic-gate 			return (FRU_FAILURE);
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 		snprintf(tmp, buf_size, "%s?%s=%s", tmp_name,
1710Sstevel@tonic-gate 			PICL_PROP_LABEL, label);
1720Sstevel@tonic-gate 		*name = tmp;
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	free(tmp_name);
1760Sstevel@tonic-gate 	free(label);
1770Sstevel@tonic-gate 	return (FRU_SUCCESS);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* ========================================================================= */
1810Sstevel@tonic-gate /* compare the node name to the name passed */
1820Sstevel@tonic-gate static fru_errno_t
cmp_node_name(picl_nodehdl_t node,const char * name)1830Sstevel@tonic-gate cmp_node_name(picl_nodehdl_t node, const char *name)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	char *node_name = NULL;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_NAME, &node_name)
1880Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1890Sstevel@tonic-gate 		return (FRU_FAILURE);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (strcmp(node_name, name) == 0) {
1930Sstevel@tonic-gate 		free(node_name);
1940Sstevel@tonic-gate 		return (FRU_SUCCESS);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	free(node_name);
1980Sstevel@tonic-gate 	return (FRU_FAILURE);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /* ========================================================================= */
2020Sstevel@tonic-gate /* compare the node class name to the name passed */
2030Sstevel@tonic-gate static fru_errno_t
cmp_class_name(picl_nodehdl_t node,const char * name)2040Sstevel@tonic-gate cmp_class_name(picl_nodehdl_t node, const char *name)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	char *class_name = NULL;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_CLASSNAME, &class_name)
2090Sstevel@tonic-gate 			!= PICL_SUCCESS) {
2100Sstevel@tonic-gate 		return (FRU_FAILURE);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (strcmp(class_name, name) == 0) {
2140Sstevel@tonic-gate 		free(class_name);
2150Sstevel@tonic-gate 		return (FRU_SUCCESS);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	free(class_name);
2190Sstevel@tonic-gate 	return (FRU_FAILURE);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /* ========================================================================= */
2240Sstevel@tonic-gate /* get the "frutree" root node */
2250Sstevel@tonic-gate static fru_errno_t
fpt_get_root(fru_treehdl_t * node)2260Sstevel@tonic-gate fpt_get_root(fru_treehdl_t *node)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	picl_nodehdl_t picl_node;
2290Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	picl_err = picl_get_root(&picl_node);
2320Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(picl_node, PICL_PROP_CHILD,
2330Sstevel@tonic-gate 		(void *)&picl_node, sizeof (picl_node)))
2340Sstevel@tonic-gate 		!= PICL_SUCCESS) {
235*1124Skmohan 		return (map_plugin_err(picl_err));
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	while (cmp_node_name(picl_node, PICL_NODE_FRUTREE)
2390Sstevel@tonic-gate 			!= FRU_SUCCESS) {
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		if ((picl_err = picl_get_propval_by_name(picl_node,
2420Sstevel@tonic-gate 			PICL_PROP_PEER, (void *)&picl_node,
2430Sstevel@tonic-gate 			sizeof (picl_node))) == PICL_PROPNOTFOUND) {
2440Sstevel@tonic-gate 			return (FRU_NODENOTFOUND);
2450Sstevel@tonic-gate 		} else if (picl_err != PICL_SUCCESS) {
246*1124Skmohan 			return (map_plugin_err(picl_err));
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	picl_root_node = picl_node;
2510Sstevel@tonic-gate 	*node = PICLHDL_TO_TREEHDL(picl_node);
2520Sstevel@tonic-gate 	return (FRU_SUCCESS);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /* ========================================================================= */
2560Sstevel@tonic-gate static fru_errno_t
fpt_get_peer(fru_treehdl_t sibling,fru_treehdl_t * peer)2570Sstevel@tonic-gate fpt_get_peer(fru_treehdl_t sibling, fru_treehdl_t *peer)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
2600Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(sibling);
2610Sstevel@tonic-gate 	picl_nodehdl_t picl_peer;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	rc = picl_get_propval_by_name(handle, PICL_PROP_PEER,
2640Sstevel@tonic-gate 		(void *)&picl_peer, sizeof (picl_peer));
2650Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
266*1124Skmohan 		return (map_plugin_err(rc));
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	*peer = PICLHDL_TO_TREEHDL(picl_peer);
2700Sstevel@tonic-gate 	return (FRU_SUCCESS);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /* ========================================================================= */
2740Sstevel@tonic-gate static fru_errno_t
fpt_get_child(fru_treehdl_t handle,fru_treehdl_t * child)2750Sstevel@tonic-gate fpt_get_child(fru_treehdl_t handle, fru_treehdl_t *child)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	picl_nodehdl_t p_child;
2780Sstevel@tonic-gate 	int rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2790Sstevel@tonic-gate 		PICL_PROP_CHILD, (void *)&p_child, sizeof (p_child));
2800Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
281*1124Skmohan 		return (map_plugin_err(rc));
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	*child = PICLHDL_TO_TREEHDL(p_child);
2850Sstevel@tonic-gate 	return (FRU_SUCCESS);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /* ========================================================================= */
2890Sstevel@tonic-gate static fru_errno_t
fpt_get_parent(fru_treehdl_t handle,fru_treehdl_t * parent)2900Sstevel@tonic-gate fpt_get_parent(fru_treehdl_t handle, fru_treehdl_t *parent)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
2930Sstevel@tonic-gate 	picl_nodehdl_t p_parent;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* do not allow the libfru users to see the parent of the root */
2960Sstevel@tonic-gate 	if (TREEHDL_TO_PICLHDL(handle) == picl_root_node) {
2970Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
3010Sstevel@tonic-gate 		PICL_PROP_PARENT, (void *)&p_parent, sizeof (p_parent));
3020Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
303*1124Skmohan 		return (map_plugin_err(rc));
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	*parent = PICLHDL_TO_TREEHDL(p_parent);
3070Sstevel@tonic-gate 	return (FRU_SUCCESS);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /* ========================================================================= */
3110Sstevel@tonic-gate static fru_errno_t
fpt_get_node_type(fru_treehdl_t node,fru_node_t * type)3120Sstevel@tonic-gate fpt_get_node_type(fru_treehdl_t node, fru_node_t *type)
3130Sstevel@tonic-gate {
314*1124Skmohan 	int	rc = PICL_SUCCESS;
3150Sstevel@tonic-gate 	char picl_class[PICL_PROPNAMELEN_MAX];
3160Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
3170Sstevel@tonic-gate 
318*1124Skmohan 	if ((rc = picl_get_propval_by_name(handle, PICL_PROP_CLASSNAME,
319*1124Skmohan 		picl_class, sizeof (picl_class))) != PICL_SUCCESS) {
320*1124Skmohan 		return (map_plugin_err(rc));
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (strcmp(picl_class, PICL_CLASS_LOCATION) == 0)  {
3240Sstevel@tonic-gate 		*type = FRU_NODE_LOCATION;
3250Sstevel@tonic-gate 		return (FRU_SUCCESS);
3260Sstevel@tonic-gate 	} else if (strcmp(picl_class, PICL_CLASS_FRU) == 0) {
3270Sstevel@tonic-gate 		picl_prophdl_t proph;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		/* check for the CONTAINER_PROP property which indicates */
3300Sstevel@tonic-gate 		/* there is data for this node.  (ie fru is a container) */
3310Sstevel@tonic-gate 		if (picl_get_prop_by_name(handle,
3320Sstevel@tonic-gate 			PICL_PROP_CONTAINER, &proph) == PICL_SUCCESS) {
3330Sstevel@tonic-gate 			*type = FRU_NODE_CONTAINER;
3340Sstevel@tonic-gate 			return (FRU_SUCCESS);
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 		*type = FRU_NODE_FRU;
3370Sstevel@tonic-gate 		return (FRU_SUCCESS);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	*type = FRU_NODE_UNKNOWN;
3410Sstevel@tonic-gate 	return (FRU_SUCCESS);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /* ========================================================================= */
3450Sstevel@tonic-gate /* find the next section or return NODENOTFOUND */
3460Sstevel@tonic-gate static fru_errno_t
find_next_section(picl_nodehdl_t current,picl_nodehdl_t * next)3470Sstevel@tonic-gate find_next_section(picl_nodehdl_t current, picl_nodehdl_t *next)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	picl_nodehdl_t rc_next;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (picl_get_propval_by_name(current, PICL_PROP_PEER,
3520Sstevel@tonic-gate 		(void *)&rc_next, sizeof (rc_next)) != PICL_SUCCESS) {
3530Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3570Sstevel@tonic-gate 	if (cmp_class_name(rc_next, PICL_CLASS_SECTION)
3580Sstevel@tonic-gate 			== FRU_SUCCESS) {
3590Sstevel@tonic-gate 		*next = rc_next;
3600Sstevel@tonic-gate 		return (FRU_SUCCESS);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3640Sstevel@tonic-gate 	/* is a section */
3650Sstevel@tonic-gate 	return (find_next_section(rc_next, next));
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /* ========================================================================= */
3690Sstevel@tonic-gate /* find the first section or return NODENOTFOUND */
3700Sstevel@tonic-gate static fru_errno_t
find_first_section(picl_nodehdl_t parent,picl_nodehdl_t * section)3710Sstevel@tonic-gate find_first_section(picl_nodehdl_t parent, picl_nodehdl_t *section)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	picl_nodehdl_t rc_section;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (picl_get_propval_by_name(parent, PICL_PROP_CHILD,
3760Sstevel@tonic-gate 		(void *)&rc_section, sizeof (rc_section)) != PICL_SUCCESS) {
3770Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3810Sstevel@tonic-gate 	if (cmp_class_name(rc_section, PICL_CLASS_SECTION)
3820Sstevel@tonic-gate 			== FRU_SUCCESS) {
3830Sstevel@tonic-gate 		*section = rc_section;
3840Sstevel@tonic-gate 		return (FRU_SUCCESS);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3880Sstevel@tonic-gate 	/* is a section */
3890Sstevel@tonic-gate 	return (find_next_section(rc_section, section));
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /* ========================================================================= */
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * Find the handle of the segment node "segment".
3950Sstevel@tonic-gate  * also returns the hardware description of this segment.  (read from the
3960Sstevel@tonic-gate  * section this was found in.)
3970Sstevel@tonic-gate  * If the ign_cor_flg is set this will still succeed even if the segment is
3980Sstevel@tonic-gate  * corrupt, otherwise it will return FRU_SEGCORRUPT for corrupt segments
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate #define	IGN_CORRUPT_YES 1
4010Sstevel@tonic-gate #define	IGN_CORRUPT_NO 0
4020Sstevel@tonic-gate static fru_errno_t
get_segment_node(picl_nodehdl_t handle,const char * segment,picl_nodehdl_t * seg_hdl,fru_seg_hwdesc_t * hw_desc,int ign_cor_flg)4030Sstevel@tonic-gate get_segment_node(picl_nodehdl_t handle, const char *segment,
4040Sstevel@tonic-gate 	picl_nodehdl_t *seg_hdl, fru_seg_hwdesc_t *hw_desc, int ign_cor_flg)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
4070Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if ((err = update_data_nodes(handle)) != FRU_SUCCESS) {
4100Sstevel@tonic-gate 		return (err);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if ((err = find_first_section(handle, &sect_node)) != FRU_SUCCESS) {
4140Sstevel@tonic-gate 		return (err);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/* while there are sections. */
4180Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
4190Sstevel@tonic-gate 		uint32_t num_segs = 0;
4200Sstevel@tonic-gate 		int rc = PICL_SUCCESS;
4210Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		/* do this just in case the Segments have not been built. */
4240Sstevel@tonic-gate 		if ((rc = picl_get_propval_by_name(sect_node,
4250Sstevel@tonic-gate 			PICL_PROP_NUM_SEGMENTS,
4260Sstevel@tonic-gate 			(void *)&num_segs,
4270Sstevel@tonic-gate 			sizeof (num_segs))) != PICL_SUCCESS) {
4280Sstevel@tonic-gate 			return (map_plugin_err(rc));
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		/* while there are segments. */
4320Sstevel@tonic-gate 		rc = picl_get_propval_by_name(sect_node, PICL_PROP_CHILD,
4330Sstevel@tonic-gate 			(void *)&seg_node, sizeof (seg_node));
4340Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
4350Sstevel@tonic-gate 			char name[PICL_PROPNAMELEN_MAX];
4360Sstevel@tonic-gate 			picl_get_propval_by_name(seg_node, PICL_PROP_NAME,
4370Sstevel@tonic-gate 				name, sizeof (name));
4380Sstevel@tonic-gate 			if (strcmp(segment, name) == 0) {
4390Sstevel@tonic-gate 				int dummy = 0;
4400Sstevel@tonic-gate 				int protection = 0;
4410Sstevel@tonic-gate 				/* NUM_TAGS prop exists iff segment is OK */
4420Sstevel@tonic-gate 				if ((ign_cor_flg == IGN_CORRUPT_NO) &&
4430Sstevel@tonic-gate 					(picl_get_propval_by_name(seg_node,
4440Sstevel@tonic-gate 					PICL_PROP_NUM_TAGS,
4450Sstevel@tonic-gate 					(void *)&dummy,
4460Sstevel@tonic-gate 					sizeof (dummy)) != PICL_SUCCESS)) {
4470Sstevel@tonic-gate 					return (FRU_SEGCORRUPT);
4480Sstevel@tonic-gate 				}
4490Sstevel@tonic-gate 				/* get the HW protections of this section. */
450*1124Skmohan 				if ((rc = picl_get_propval_by_name(sect_node,
4510Sstevel@tonic-gate 					PICL_PROP_PROTECTED,
4520Sstevel@tonic-gate 					(void *)&protection,
453*1124Skmohan 					sizeof (protection)))
454*1124Skmohan 							!= PICL_SUCCESS) {
455*1124Skmohan 					return (map_plugin_err(rc));
4560Sstevel@tonic-gate 				}
4570Sstevel@tonic-gate 				hw_desc->all_bits = 0;
4580Sstevel@tonic-gate 				hw_desc->field.read_only = protection;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 				*seg_hdl = seg_node;
4610Sstevel@tonic-gate 				return (FRU_SUCCESS);
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 			rc = picl_get_propval_by_name(seg_node, PICL_PROP_PEER,
4640Sstevel@tonic-gate 				(void *)&seg_node, sizeof (seg_node));
4650Sstevel@tonic-gate 		}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		/* Peer property not found is ok */
4680Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
469*1124Skmohan 			return (map_plugin_err(rc));
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	return (FRU_INVALSEG);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /* ========================================================================= */
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * For the section handle passed add to list all the segment names found.
4810Sstevel@tonic-gate  * Also incriments total by the number found.
4820Sstevel@tonic-gate  */
4830Sstevel@tonic-gate static fru_errno_t
add_segs_for_section(picl_nodehdl_t section,fru_strlist_t * list)4840Sstevel@tonic-gate add_segs_for_section(picl_nodehdl_t section, fru_strlist_t *list)
4850Sstevel@tonic-gate {
4860Sstevel@tonic-gate 	uint32_t num_segments = 0;
4870Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ((rc = picl_get_propval_by_name(section,
4900Sstevel@tonic-gate 		PICL_PROP_NUM_SEGMENTS,
4910Sstevel@tonic-gate 		(void *)&num_segments,
4920Sstevel@tonic-gate 		sizeof (num_segments))) != PICL_SUCCESS) {
4930Sstevel@tonic-gate 		fru_destroy_strlist(list);
4940Sstevel@tonic-gate 		return (map_plugin_err(rc));
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (num_segments != 0) {
4980Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
4990Sstevel@tonic-gate 		int total_space = list->num + num_segments;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		list->strs = realloc(list->strs,
5020Sstevel@tonic-gate 			(sizeof (*(list->strs)) * (total_space)));
5030Sstevel@tonic-gate 		if (list->strs == NULL) {
5040Sstevel@tonic-gate 			return (FRU_FAILURE);
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		/* get the first segment */
5080Sstevel@tonic-gate 		rc = picl_get_propval_by_name(section,
5090Sstevel@tonic-gate 			PICL_PROP_CHILD, (void *)&seg_node,
5100Sstevel@tonic-gate 			sizeof (seg_node));
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 		/* while there are more segments. */
5130Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
5140Sstevel@tonic-gate 			char name[FRU_SEGNAMELEN +1];
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 			if ((rc = picl_get_propval_by_name(seg_node,
5170Sstevel@tonic-gate 				PICL_PROP_NAME, name,
5180Sstevel@tonic-gate 				sizeof (name))) != PICL_SUCCESS) {
5190Sstevel@tonic-gate 				break;
5200Sstevel@tonic-gate 			}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 			/* check array bounds */
5230Sstevel@tonic-gate 			if (list->num >= total_space) {
5240Sstevel@tonic-gate 				/* PICL reported incorrect number of segs */
5250Sstevel@tonic-gate 				return (FRU_IOERROR);
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			list->strs[(list->num)++] = strdup(name);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 			rc = picl_get_propval_by_name(seg_node,
5300Sstevel@tonic-gate 				PICL_PROP_PEER, (void *)&seg_node,
5310Sstevel@tonic-gate 				sizeof (seg_node));
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		/* Peer property not found is ok */
5350Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
536*1124Skmohan 			return (map_plugin_err(rc));
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	return (FRU_SUCCESS);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /* ========================================================================= */
5440Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_list(fru_treehdl_t handle,fru_strlist_t * list)5450Sstevel@tonic-gate fpt_get_seg_list(fru_treehdl_t handle, fru_strlist_t *list)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	fru_errno_t err;
5480Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
5490Sstevel@tonic-gate 	fru_strlist_t rc_list;
5500Sstevel@tonic-gate 	rc_list.num = 0;
5510Sstevel@tonic-gate 	rc_list.strs = NULL;
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if ((err = update_data_nodes(TREEHDL_TO_PICLHDL(handle)))
5540Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5550Sstevel@tonic-gate 		return (err);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &sect_node))
5590Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5600Sstevel@tonic-gate 		return (err);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/* while there are sections. */
5640Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
5650Sstevel@tonic-gate 		if ((err = add_segs_for_section(sect_node, &rc_list))
5660Sstevel@tonic-gate 				!= FRU_SUCCESS) {
5670Sstevel@tonic-gate 			fru_destroy_strlist(&rc_list);
5680Sstevel@tonic-gate 			return (err);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	list->num = rc_list.num;
5740Sstevel@tonic-gate 	list->strs = rc_list.strs;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	return (FRU_SUCCESS);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /* ========================================================================= */
5800Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_def(fru_treehdl_t handle,const char * seg_name,fru_segdef_t * def)5810Sstevel@tonic-gate fpt_get_seg_def(fru_treehdl_t handle, const char *seg_name, fru_segdef_t *def)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
5840Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
5850Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	fru_segdesc_t desc;
5880Sstevel@tonic-gate 	uint32_t size;
5890Sstevel@tonic-gate 	uint32_t address;
5900Sstevel@tonic-gate 	/* LINTED */
5910Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
5940Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS)
5950Sstevel@tonic-gate 		return (err);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
5980Sstevel@tonic-gate 		PICL_PROP_DESCRIPTOR,
5990Sstevel@tonic-gate 		&desc, sizeof (desc))) != PICL_SUCCESS) {
600*1124Skmohan 		return (map_plugin_err(picl_err));
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
6040Sstevel@tonic-gate 		PICL_PROP_LENGTH,
6050Sstevel@tonic-gate 		&size, sizeof (size))) != PICL_SUCCESS) {
606*1124Skmohan 		return (map_plugin_err(picl_err));
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
6100Sstevel@tonic-gate 		PICL_PROP_OFFSET,
6110Sstevel@tonic-gate 		&address, sizeof (address))) != PICL_SUCCESS) {
612*1124Skmohan 		return (map_plugin_err(picl_err));
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	def->version = LIBFRU_VERSION;
6160Sstevel@tonic-gate 	strlcpy(def->name, seg_name, FRU_SEGNAMELEN+1);
6170Sstevel@tonic-gate 	def->desc = desc;
6180Sstevel@tonic-gate 	def->size = size;
6190Sstevel@tonic-gate 	def->address = address;
6200Sstevel@tonic-gate 	def->hw_desc = hw_desc;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	return (FRU_SUCCESS);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /* ========================================================================= */
6260Sstevel@tonic-gate static fru_errno_t
fpt_add_seg(fru_treehdl_t handle,fru_segdef_t * def)6270Sstevel@tonic-gate fpt_add_seg(fru_treehdl_t handle, fru_segdef_t *def)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6300Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6310Sstevel@tonic-gate 	picl_nodehdl_t section;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate  * for every section which has a ADD_SEGMENT_PROP try and add the segment
6350Sstevel@tonic-gate  */
6360Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &section))
6370Sstevel@tonic-gate 		!= FRU_SUCCESS) {
6380Sstevel@tonic-gate 		return (err);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	do {
6410Sstevel@tonic-gate 		fru_segdef_t dummy;
6420Sstevel@tonic-gate 		if ((picl_err = picl_get_propval_by_name(section,
6430Sstevel@tonic-gate 			PICL_PROP_ADD_SEGMENT, &dummy, sizeof (dummy)))
6440Sstevel@tonic-gate 				== PICL_SUCCESS) {
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 			picl_err = picl_set_propval_by_name(section,
6470Sstevel@tonic-gate 				PICL_PROP_ADD_SEGMENT, def, sizeof (*def));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 			return (map_plugin_err(picl_err));
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 	} while (find_next_section(section, &section) == FRU_SUCCESS);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /* ========================================================================= */
6570Sstevel@tonic-gate static fru_errno_t
fpt_delete_seg(fru_treehdl_t handle,const char * seg_name)6580Sstevel@tonic-gate fpt_delete_seg(fru_treehdl_t handle, const char *seg_name)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate 	picl_nodehdl_t seg_hdl;
6610Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6620Sstevel@tonic-gate 	fru_errno_t err;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	int dead_flag = FRUDATA_DELETE_TAG_KEY;
6650Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6680Sstevel@tonic-gate 		&seg_hdl, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS) {
6690Sstevel@tonic-gate 		return (err);
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	rc = picl_set_propval_by_name(seg_hdl, PICL_PROP_DELETE_SEGMENT,
6730Sstevel@tonic-gate 		&dead_flag, sizeof (dead_flag));
6740Sstevel@tonic-gate 	return (map_plugin_err(rc));
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /* ========================================================================= */
6780Sstevel@tonic-gate static fru_errno_t
fpt_add_tag_to_seg(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,uint8_t * data,size_t data_len)6790Sstevel@tonic-gate fpt_add_tag_to_seg(fru_treehdl_t handle, const char *seg_name,
6800Sstevel@tonic-gate 		fru_tag_t tag, uint8_t *data, size_t data_len)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6830Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
6840Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6850Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6860Sstevel@tonic-gate 	size_t buf_size = 0;
6870Sstevel@tonic-gate 	uint8_t *buffer = NULL;
6880Sstevel@tonic-gate 	picl_prophdl_t add_prop;
6890Sstevel@tonic-gate 	picl_propinfo_t add_prop_info;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6920Sstevel@tonic-gate 		&segHdl, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
6930Sstevel@tonic-gate 		return (err);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/* get the length of the buffer required. */
6970Sstevel@tonic-gate 	if ((picl_err = picl_get_prop_by_name(segHdl,
6980Sstevel@tonic-gate 		PICL_PROP_ADD_PACKET,
6990Sstevel@tonic-gate 		&add_prop)) != PICL_SUCCESS) {
700*1124Skmohan 		return (map_plugin_err(picl_err));
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	if ((picl_err = picl_get_propinfo(add_prop, &add_prop_info))
7030Sstevel@tonic-gate 			!= PICL_SUCCESS) {
704*1124Skmohan 		return (map_plugin_err(picl_err));
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	buf_size = add_prop_info.size;
7080Sstevel@tonic-gate 	if (data_len >= (buf_size - get_tag_size(get_tag_type(&tag)))) {
7090Sstevel@tonic-gate 		return (FRU_NOSPACE);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	buffer = malloc(buf_size);
7130Sstevel@tonic-gate 	if (buffer == NULL) {
7140Sstevel@tonic-gate 		return (FRU_FAILURE);
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	/* write the tag and data into the buffer */
7170Sstevel@tonic-gate 	memcpy(buffer, &tag, get_tag_size(get_tag_type(&tag)));
7180Sstevel@tonic-gate 	memcpy((void *)(buffer+get_tag_size(get_tag_type(&tag))),
7190Sstevel@tonic-gate 		data, data_len);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	picl_err = picl_set_propval(add_prop, buffer, buf_size);
7220Sstevel@tonic-gate 	free(buffer);
7230Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate /* ========================================================================= */
7270Sstevel@tonic-gate static fru_errno_t
fpt_get_tag_list(fru_treehdl_t handle,const char * seg_name,fru_tag_t ** tags,int * number)7280Sstevel@tonic-gate fpt_get_tag_list(fru_treehdl_t handle, const char *seg_name,
7290Sstevel@tonic-gate 		fru_tag_t **tags, int *number)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
7320Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
7330Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
7340Sstevel@tonic-gate 	picl_prophdl_t tagTable;
7350Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
7360Sstevel@tonic-gate 	unsigned int total_tags = 0;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/* return variables */
7390Sstevel@tonic-gate 	fru_tag_t *rc_tags = NULL;
7400Sstevel@tonic-gate 	unsigned int rc_num = 0;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
7430Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
7440Sstevel@tonic-gate 		return (err);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/* get the number of tags and allocate array for them */
7480Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
7490Sstevel@tonic-gate 		PICL_PROP_NUM_TAGS,
7500Sstevel@tonic-gate 		(void *)&total_tags,
7510Sstevel@tonic-gate 		sizeof (total_tags))) != PICL_SUCCESS) {
752*1124Skmohan 		return (map_plugin_err(picl_err));
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (total_tags == 0) {
7560Sstevel@tonic-gate 		*tags = rc_tags;
7570Sstevel@tonic-gate 		*number = rc_num;
7580Sstevel@tonic-gate 		return (FRU_SUCCESS);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	rc_tags = malloc((sizeof (*rc_tags) * total_tags));
7620Sstevel@tonic-gate 	if (rc_tags == NULL) {
7630Sstevel@tonic-gate 		return (FRU_FAILURE);
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/* go through the tagTable and fill in the array */
7670Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
7680Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
7690Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
7700Sstevel@tonic-gate 		free(rc_tags);
771*1124Skmohan 		return (map_plugin_err(picl_err));
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 	picl_err = picl_get_next_by_col(tagTable, &tagTable);
7740Sstevel@tonic-gate 	while (picl_err == PICL_SUCCESS) {
7750Sstevel@tonic-gate 		/* check array bounds */
7760Sstevel@tonic-gate 		if (rc_num >= total_tags) {
7770Sstevel@tonic-gate 			free(rc_tags);
7780Sstevel@tonic-gate 			return (FRU_FAILURE);
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		/* fill in the array */
7810Sstevel@tonic-gate 		if ((picl_err = picl_get_propval(tagTable,
7820Sstevel@tonic-gate 			(void *)&(rc_tags[rc_num++]),
7830Sstevel@tonic-gate 			sizeof (fru_tag_t))) != PICL_SUCCESS) {
7840Sstevel@tonic-gate 			free(rc_tags);
785*1124Skmohan 			return (map_plugin_err(picl_err));
7860Sstevel@tonic-gate 		}
7870Sstevel@tonic-gate 		/* get the next tag */
7880Sstevel@tonic-gate 		picl_err = picl_get_next_by_col(tagTable, &tagTable);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	if (picl_err == PICL_ENDOFLIST) {
7920Sstevel@tonic-gate 		*tags = rc_tags;
7930Sstevel@tonic-gate 		*number = rc_num;
7940Sstevel@tonic-gate 		return (FRU_SUCCESS);
7950Sstevel@tonic-gate 	}
796*1124Skmohan 	return (map_plugin_err(picl_err));
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate /* ========================================================================= */
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate  * From the handle, segment name, tag, and instance of the tag get me:
8020Sstevel@tonic-gate  * segHdl: The segment handle for this segment.
8030Sstevel@tonic-gate  * tagHdl: tag property handle in the tag table for this instance "tag"
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate static fru_errno_t
get_tag_handle(picl_nodehdl_t handle,const char * segment,fru_tag_t tag,int instance,picl_nodehdl_t * segHdl,picl_prophdl_t * tagHdl)8060Sstevel@tonic-gate get_tag_handle(picl_nodehdl_t handle, const char *segment,
8070Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8080Sstevel@tonic-gate 		picl_nodehdl_t *segHdl,
8090Sstevel@tonic-gate 		picl_prophdl_t *tagHdl)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
8120Sstevel@tonic-gate 	fru_errno_t err;
8130Sstevel@tonic-gate 	picl_prophdl_t tagTable = 0;
8140Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8150Sstevel@tonic-gate 	picl_nodehdl_t tmp_seg;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	fru_tag_t foundTag;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), segment,
8200Sstevel@tonic-gate 		&tmp_seg, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
8210Sstevel@tonic-gate 		return (err);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	foundTag.raw_data = 0;
8250Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(tmp_seg,
8260Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
8270Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
828*1124Skmohan 		return (map_plugin_err(picl_err));
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	picl_err = picl_get_next_by_col(tagTable, &tagTable);
8320Sstevel@tonic-gate 	while ((picl_err != PICL_ENDOFLIST) &&
8330Sstevel@tonic-gate 		(picl_err == PICL_SUCCESS)) {
8340Sstevel@tonic-gate 		if ((picl_err = picl_get_propval(tagTable, (void *)&foundTag,
8350Sstevel@tonic-gate 			sizeof (foundTag))) != PICL_SUCCESS) {
836*1124Skmohan 			return (map_plugin_err(picl_err));
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 		if ((tags_equal(tag, foundTag) == 1) && (instance-- == 0)) {
8390Sstevel@tonic-gate 			*segHdl = tmp_seg;
8400Sstevel@tonic-gate 			*tagHdl = tagTable;
8410Sstevel@tonic-gate 			return (FRU_SUCCESS);
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate 		picl_err = picl_get_next_by_col(tagTable, &tagTable);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
846*1124Skmohan 	return (map_plugin_err(picl_err));
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /* ========================================================================= */
8500Sstevel@tonic-gate static fru_errno_t
fpt_get_tag_data(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance,uint8_t ** data,size_t * data_len)8510Sstevel@tonic-gate fpt_get_tag_data(fru_treehdl_t handle, const char *seg_name,
8520Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8530Sstevel@tonic-gate 		uint8_t **data, size_t *data_len)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
8560Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8570Sstevel@tonic-gate 	uint8_t *buffer;
8580Sstevel@tonic-gate 	int buf_len = 0;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	picl_nodehdl_t seg;
8610Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if ((err = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
8640Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
8650Sstevel@tonic-gate 		return (err);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
8690Sstevel@tonic-gate 		!= PICL_SUCCESS) {
870*1124Skmohan 		return (map_plugin_err(picl_err));
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	buf_len = get_payload_length(&tag);
8740Sstevel@tonic-gate 	buffer = malloc(buf_len);
8750Sstevel@tonic-gate 	if (buffer == NULL) {
8760Sstevel@tonic-gate 		return (FRU_FAILURE);
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if ((picl_err = picl_get_propval(tagHdl, buffer, buf_len))
8800Sstevel@tonic-gate 		!= PICL_SUCCESS) {
8810Sstevel@tonic-gate 		free(buffer);
8820Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	*data = buffer;
8860Sstevel@tonic-gate 	*data_len = buf_len;
8870Sstevel@tonic-gate 	return (FRU_SUCCESS);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /* ========================================================================= */
8910Sstevel@tonic-gate static fru_errno_t
fpt_set_tag_data(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance,uint8_t * data,size_t data_len)8920Sstevel@tonic-gate fpt_set_tag_data(fru_treehdl_t handle, const char *seg_name,
8930Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8940Sstevel@tonic-gate 		uint8_t *data, size_t data_len)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
8970Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	picl_nodehdl_t seg;
9000Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9030Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
9040Sstevel@tonic-gate 		return (rc);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
9080Sstevel@tonic-gate 		!= PICL_SUCCESS) {
909*1124Skmohan 		return (map_plugin_err(picl_err));
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if ((picl_err = picl_set_propval(tagHdl, data, data_len))
9130Sstevel@tonic-gate 		!= PICL_SUCCESS) {
9140Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	return (FRU_SUCCESS);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate /* ========================================================================= */
9210Sstevel@tonic-gate static fru_errno_t
fpt_delete_tag(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance)9220Sstevel@tonic-gate fpt_delete_tag(fru_treehdl_t handle, const char *seg_name, fru_tag_t tag,
9230Sstevel@tonic-gate 		int instance)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
9260Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
9290Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/* get tag handle */
9320Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9330Sstevel@tonic-gate 		tag, instance, &segHdl, &tagHdl)) != FRU_SUCCESS) {
9340Sstevel@tonic-gate 		return (rc);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/* set up key */
9380Sstevel@tonic-gate 	tag.raw_data &= FRUDATA_DELETE_TAG_MASK;
9390Sstevel@tonic-gate 	tag.raw_data |= FRUDATA_DELETE_TAG_KEY;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/* Write back */
9420Sstevel@tonic-gate 	picl_err = picl_set_propval(tagHdl, (void *)&(tag.raw_data),
9430Sstevel@tonic-gate 		sizeof (tag.raw_data));
9440Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /* ========================================================================= */
9480Sstevel@tonic-gate static fru_errno_t
fpt_for_each_segment(fru_treehdl_t treenode,int (* function)(fru_treeseghdl_t segment,void * args),void * args)9490Sstevel@tonic-gate fpt_for_each_segment(fru_treehdl_t treenode,
9500Sstevel@tonic-gate 			int (*function)(fru_treeseghdl_t segment, void *args),
9510Sstevel@tonic-gate 			void *args)
9520Sstevel@tonic-gate {
9530Sstevel@tonic-gate 	int		num_segments = 0, status;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	fru_errno_t	saved_status = FRU_SUCCESS;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	picl_nodehdl_t	container = TREEHDL_TO_PICLHDL(treenode),
9580Sstevel@tonic-gate 			section, segment;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	if ((status = update_data_nodes(container)) != FRU_SUCCESS)
9620Sstevel@tonic-gate 		return (status);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/* process each section */
9650Sstevel@tonic-gate 	for (status = picl_get_propval_by_name(container, PICL_PROP_CHILD,
9660Sstevel@tonic-gate 						&section, sizeof (section));
9670Sstevel@tonic-gate 		status == PICL_SUCCESS;
9680Sstevel@tonic-gate 		status = picl_get_propval_by_name(section, PICL_PROP_PEER,
9690Sstevel@tonic-gate 							&section,
9700Sstevel@tonic-gate 							sizeof (section))) {
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		if (cmp_class_name(section, PICL_CLASS_SECTION) != FRU_SUCCESS)
9730Sstevel@tonic-gate 			continue;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		if ((status = picl_get_propval_by_name(section,
9760Sstevel@tonic-gate 							PICL_PROP_NUM_SEGMENTS,
9770Sstevel@tonic-gate 							&num_segments,
9780Sstevel@tonic-gate 							sizeof (num_segments)))
9790Sstevel@tonic-gate 		    == PICL_PROPNOTFOUND) {
9800Sstevel@tonic-gate 			continue;
9810Sstevel@tonic-gate 		} else if (status != PICL_SUCCESS) {
9820Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
9830Sstevel@tonic-gate 			continue;
9840Sstevel@tonic-gate 		} else if (num_segments == 0) {
9850Sstevel@tonic-gate 			continue;
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 		/* process each segment */
9890Sstevel@tonic-gate 		for (status = picl_get_propval_by_name(section,
9900Sstevel@tonic-gate 							PICL_PROP_CHILD,
9910Sstevel@tonic-gate 							&segment,
9920Sstevel@tonic-gate 							sizeof (segment));
9930Sstevel@tonic-gate 			status == PICL_SUCCESS;
9940Sstevel@tonic-gate 			status = picl_get_propval_by_name(segment,
9950Sstevel@tonic-gate 							PICL_PROP_PEER,
9960Sstevel@tonic-gate 							&segment,
9970Sstevel@tonic-gate 							sizeof (segment))) {
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 			if (cmp_class_name(segment, PICL_CLASS_SEGMENT)
10000Sstevel@tonic-gate 			    != FRU_SUCCESS) continue;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 			if ((status = function(PICLHDL_TO_TREESEGHDL(segment),
10030Sstevel@tonic-gate 						args))
10040Sstevel@tonic-gate 			    != FRU_SUCCESS) return (status);
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		if (status != PICL_PROPNOTFOUND)
10080Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	if (status != PICL_PROPNOTFOUND)
10120Sstevel@tonic-gate 		saved_status = map_plugin_err(status);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	return (saved_status);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate /* ========================================================================= */
10180Sstevel@tonic-gate static fru_errno_t
fpt_get_segment_name(fru_treeseghdl_t segment,char ** name)10190Sstevel@tonic-gate fpt_get_segment_name(fru_treeseghdl_t segment, char **name)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate 	char		*propval;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	int		status;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	picl_prophdl_t	proph = 0;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	picl_propinfo_t	propinfo;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 
1030*1124Skmohan 	if ((status = picl_get_propinfo_by_name(TREESEGHDL_TO_PICLHDL(segment),
1031*1124Skmohan 		PICL_PROP_NAME, &propinfo, &proph))
10320Sstevel@tonic-gate 	    != PICL_SUCCESS)
1033*1124Skmohan 		return (map_plugin_err(status));
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	if (propinfo.size == 0)
10360Sstevel@tonic-gate 		return (FRU_INVALDATASIZE);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if ((propval = malloc(propinfo.size)) == NULL)
10390Sstevel@tonic-gate 		return (FRU_NOSPACE);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	if ((status = picl_get_propval(proph, propval, propinfo.size))
10420Sstevel@tonic-gate 	    != PICL_SUCCESS) {
10430Sstevel@tonic-gate 		free(propval);
10440Sstevel@tonic-gate 		return (map_plugin_err(status));
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	*name = propval;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	return (FRU_SUCCESS);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate /* ========================================================================= */
10530Sstevel@tonic-gate static fru_errno_t
fpt_for_each_packet(fru_treeseghdl_t treesegment,int (* function)(fru_tag_t * tag,uint8_t * payload,size_t length,void * args),void * args)10540Sstevel@tonic-gate fpt_for_each_packet(fru_treeseghdl_t treesegment,
10550Sstevel@tonic-gate 			int (*function)(fru_tag_t *tag, uint8_t *payload,
10560Sstevel@tonic-gate 					size_t length,
10570Sstevel@tonic-gate 			void *args),
10580Sstevel@tonic-gate     void *args)
10590Sstevel@tonic-gate {
10600Sstevel@tonic-gate 	int		status;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	uint8_t		*payload;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	picl_nodehdl_t	segment = TREESEGHDL_TO_PICLHDL(treesegment);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	picl_prophdl_t	packet, payloadh = 0;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	picl_propinfo_t	propinfo;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	fru_segdesc_t	descriptor;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	fru_tag_t	tag;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if ((status = picl_get_propval_by_name(segment, PICL_PROP_DESCRIPTOR,
10760Sstevel@tonic-gate 						&descriptor,
10770Sstevel@tonic-gate 						sizeof (descriptor)))
10780Sstevel@tonic-gate 	    != PICL_SUCCESS) return (map_plugin_err(status));
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (descriptor.field.opaque)
10810Sstevel@tonic-gate 		return (FRU_SUCCESS);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (descriptor.field.encrypted && (encrypt_func == NULL))
10840Sstevel@tonic-gate 		return (FRU_SUCCESS);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if ((status = picl_get_propval_by_name(segment, PICL_PROP_PACKET_TABLE,
10870Sstevel@tonic-gate 						&packet, sizeof (packet)))
10880Sstevel@tonic-gate 	    == PICL_PROPNOTFOUND)
10890Sstevel@tonic-gate 		return (FRU_SUCCESS);
10900Sstevel@tonic-gate 	else if (status != PICL_SUCCESS)
10910Sstevel@tonic-gate 		return (map_plugin_err(status));
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	while ((status = picl_get_next_by_col(packet, &packet))
10940Sstevel@tonic-gate 		== PICL_SUCCESS) {
10950Sstevel@tonic-gate 		if (((status = picl_get_propval(packet, &tag, sizeof (tag)))
10960Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
10970Sstevel@tonic-gate 		    ((status = picl_get_next_by_row(packet, &payloadh))
10980Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
10990Sstevel@tonic-gate 		    ((status = picl_get_propinfo(payloadh, &propinfo))
11000Sstevel@tonic-gate 			!= PICL_SUCCESS))
11010Sstevel@tonic-gate 			return (map_plugin_err(status));
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		if (propinfo.size > 0) {
11040Sstevel@tonic-gate 			payload = alloca(propinfo.size);
11050Sstevel@tonic-gate 			if ((status = picl_get_propval(payloadh, payload,
11060Sstevel@tonic-gate 							propinfo.size))
11070Sstevel@tonic-gate 			    != PICL_SUCCESS) return (map_plugin_err(status));
11080Sstevel@tonic-gate 		} else {
11090Sstevel@tonic-gate 			payload = NULL;
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		if ((descriptor.field.encrypted) &&
11130Sstevel@tonic-gate 		    ((status = encrypt_func(FRU_DECRYPT, payload,
11140Sstevel@tonic-gate 						propinfo.size))
11150Sstevel@tonic-gate 			!= FRU_SUCCESS)) return status;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		if ((status = function(&tag, payload, propinfo.size, args))
11180Sstevel@tonic-gate 		    != FRU_SUCCESS) return (status);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (status == PICL_ENDOFLIST)
11220Sstevel@tonic-gate 		return (FRU_SUCCESS);
11230Sstevel@tonic-gate 	else
11240Sstevel@tonic-gate 		return (map_plugin_err(status));
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /* ========================================================================= */
11280Sstevel@tonic-gate /* ARGSUSED0 */
11290Sstevel@tonic-gate static fru_errno_t
initialize(int argc,char ** argv)11300Sstevel@tonic-gate initialize(int argc, char **argv)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	/* LINTED */
11330Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
11340Sstevel@tonic-gate 	if ((rc = picl_initialize()) != PICL_SUCCESS) {
11350Sstevel@tonic-gate 		return (FRU_FAILURE);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	return (FRU_SUCCESS);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate /* ========================================================================= */
11420Sstevel@tonic-gate static fru_errno_t
shutdown(void)11430Sstevel@tonic-gate shutdown(void)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	if (picl_shutdown() != PICL_SUCCESS) {
11460Sstevel@tonic-gate 		return (FRU_FAILURE);
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 	return (FRU_SUCCESS);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /* ========================================================================= */
11520Sstevel@tonic-gate /* object for libfru to link to */
11530Sstevel@tonic-gate fru_datasource_t data_source =
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 	LIBFRU_DS_VER,
11560Sstevel@tonic-gate 	initialize,
11570Sstevel@tonic-gate 	shutdown,
11580Sstevel@tonic-gate 	fpt_get_root,
11590Sstevel@tonic-gate 	fpt_get_child,
11600Sstevel@tonic-gate 	fpt_get_peer,
11610Sstevel@tonic-gate 	fpt_get_parent,
11620Sstevel@tonic-gate 	fpt_get_name_from_hdl,
11630Sstevel@tonic-gate 	fpt_get_node_type,
11640Sstevel@tonic-gate 	fpt_get_seg_list,
11650Sstevel@tonic-gate 	fpt_get_seg_def,
11660Sstevel@tonic-gate 	fpt_add_seg,
11670Sstevel@tonic-gate 	fpt_delete_seg,
11680Sstevel@tonic-gate 	fpt_for_each_segment,
11690Sstevel@tonic-gate 	fpt_get_segment_name,
11700Sstevel@tonic-gate 	fpt_add_tag_to_seg,
11710Sstevel@tonic-gate 	fpt_get_tag_list,
11720Sstevel@tonic-gate 	fpt_get_tag_data,
11730Sstevel@tonic-gate 	fpt_set_tag_data,
11740Sstevel@tonic-gate 	fpt_delete_tag,
11750Sstevel@tonic-gate 	fpt_for_each_packet
11760Sstevel@tonic-gate };
1177