xref: /illumos-gate/usr/src/lib/libfru/libfrupicltree/frupicltree.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*35551380Skmohan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <alloca.h>
287c478bd9Sstevel@tonic-gate #include <picl.h>
297c478bd9Sstevel@tonic-gate #include <picltree.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "picldefs.h"
377c478bd9Sstevel@tonic-gate #include "fru_data.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "libfruds.h"
407c478bd9Sstevel@tonic-gate #include "libfrup.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	FRU_LABEL_PADDING 10
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* ========================================================================= */
457c478bd9Sstevel@tonic-gate #define	TREEHDL_TO_PICLHDL(treehdl) ((picl_nodehdl_t)treehdl)
467c478bd9Sstevel@tonic-gate #define	PICLHDL_TO_TREEHDL(piclhdl) ((fru_treehdl_t)piclhdl)
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	TREESEGHDL_TO_PICLHDL(treeseghdl) ((picl_nodehdl_t)treeseghdl)
497c478bd9Sstevel@tonic-gate #define	PICLHDL_TO_TREESEGHDL(piclhdl) ((fru_treeseghdl_t)piclhdl)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* Cache of the root node for quick checks */
527c478bd9Sstevel@tonic-gate static picl_nodehdl_t picl_root_node;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* ========================================================================= */
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Map the PICL errors the plugin would give me to FRU errors
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate static fru_errno_t
map_plugin_err(int picl_err)597c478bd9Sstevel@tonic-gate map_plugin_err(int picl_err)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	switch (picl_err) {
627c478bd9Sstevel@tonic-gate 		case PICL_SUCCESS:
637c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
647c478bd9Sstevel@tonic-gate 		case PICL_PERMDENIED:
657c478bd9Sstevel@tonic-gate 			return (FRU_INVALPERM);
667c478bd9Sstevel@tonic-gate 		case PICL_PROPEXISTS:
677c478bd9Sstevel@tonic-gate 			return (FRU_DUPSEG);
687c478bd9Sstevel@tonic-gate 		case PICL_NOSPACE:
697c478bd9Sstevel@tonic-gate 			return (FRU_NOSPACE);
70*35551380Skmohan 		case PICL_NORESPONSE:
71*35551380Skmohan 			return (FRU_NORESPONSE);
72*35551380Skmohan 		case PICL_PROPNOTFOUND:
73*35551380Skmohan 			return (FRU_NODENOTFOUND);
74*35551380Skmohan 		case PICL_ENDOFLIST:
75*35551380Skmohan 			return (FRU_DATANOTFOUND);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	return (FRU_IOERROR);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* ========================================================================= */
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * cause a refresh of the sub-nodes by writing anything to the container
837c478bd9Sstevel@tonic-gate  * property of the node.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static fru_errno_t
update_data_nodes(picl_nodehdl_t handle)867c478bd9Sstevel@tonic-gate update_data_nodes(picl_nodehdl_t handle)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	uint32_t container = FRUDATA_DELETE_TAG_KEY;
897c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_update_propval_by_name(handle,
927c478bd9Sstevel@tonic-gate 		PICL_PROP_CONTAINER, (void *)&container,
937c478bd9Sstevel@tonic-gate 		sizeof (container))) != PICL_SUCCESS) {
947c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* ========================================================================= */
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * picl like function which gets a string property with the proper length
1037c478bd9Sstevel@tonic-gate  * NOTE: returns picl errno values NOT fru_errno_t
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static int
get_strprop_by_name(picl_nodehdl_t handle,char * prop_name,char ** string)1067c478bd9Sstevel@tonic-gate get_strprop_by_name(picl_nodehdl_t handle, char *prop_name, char **string)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1097c478bd9Sstevel@tonic-gate 	picl_prophdl_t proph;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	size_t buf_size = 0;
1127c478bd9Sstevel@tonic-gate 	char *tmp_buf = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	ptree_propinfo_t prop_info;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_prop_by_name(handle, prop_name, &proph))
1177c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1187c478bd9Sstevel@tonic-gate 		return (picl_err);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propinfo(proph, &prop_info))
1217c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1227c478bd9Sstevel@tonic-gate 		return (picl_err);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	buf_size = prop_info.piclinfo.size;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	tmp_buf = malloc((sizeof (*tmp_buf) * buf_size));
1277c478bd9Sstevel@tonic-gate 	if (tmp_buf == NULL) {
1287c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval(proph, tmp_buf, buf_size))
1327c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1337c478bd9Sstevel@tonic-gate 		free(tmp_buf);
1347c478bd9Sstevel@tonic-gate 		return (picl_err);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	*string = tmp_buf;
1387c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /* ========================================================================= */
1427c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_name_from_hdl(fru_treehdl_t node,char ** name)1437c478bd9Sstevel@tonic-gate fpt_get_name_from_hdl(fru_treehdl_t node, char **name)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1467c478bd9Sstevel@tonic-gate 	char *tmp_name = NULL;
1477c478bd9Sstevel@tonic-gate 	char *label = NULL;
1487c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* get the name */
1517c478bd9Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_NAME,
1527c478bd9Sstevel@tonic-gate 		&tmp_name)) != PICL_SUCCESS) {
153*35551380Skmohan 		return (map_plugin_err(picl_err));
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* get the label, if any */
1577c478bd9Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_LABEL,
1587c478bd9Sstevel@tonic-gate 		&label)) != PICL_SUCCESS) {
1597c478bd9Sstevel@tonic-gate 		if (picl_err != PICL_PROPNOTFOUND) {
1607c478bd9Sstevel@tonic-gate 			free(tmp_name);
161*35551380Skmohan 			return (map_plugin_err(picl_err));
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		/* else PICL_PROPNOTFOUND is OK because not all nodes */
1647c478bd9Sstevel@tonic-gate 		/* will have a label. */
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* construct the name as nessecary */
1687c478bd9Sstevel@tonic-gate 	if (label == NULL) {
1697c478bd9Sstevel@tonic-gate 		*name = strdup(tmp_name);
1707c478bd9Sstevel@tonic-gate 	} else {
1717c478bd9Sstevel@tonic-gate 		size_t buf_size = strlen(tmp_name) + strlen(label) +
1727c478bd9Sstevel@tonic-gate 			FRU_LABEL_PADDING;
1737c478bd9Sstevel@tonic-gate 		char *tmp = malloc(buf_size);
1747c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
1757c478bd9Sstevel@tonic-gate 			free(tmp_name);
1767c478bd9Sstevel@tonic-gate 			free(label);
1777c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 		snprintf(tmp, buf_size, "%s?%s=%s", tmp_name,
1807c478bd9Sstevel@tonic-gate 			PICL_PROP_LABEL, label);
1817c478bd9Sstevel@tonic-gate 		*name = tmp;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	free(tmp_name);
1857c478bd9Sstevel@tonic-gate 	free(label);
1867c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* ========================================================================= */
1907c478bd9Sstevel@tonic-gate /* compare the node name to the name passed */
1917c478bd9Sstevel@tonic-gate static fru_errno_t
cmp_node_name(picl_nodehdl_t node,const char * name)1927c478bd9Sstevel@tonic-gate cmp_node_name(picl_nodehdl_t node, const char *name)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	char *node_name = NULL;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_NAME, &node_name)
1977c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1987c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (strcmp(node_name, name) == 0) {
2027c478bd9Sstevel@tonic-gate 		free(node_name);
2037c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	free(node_name);
2077c478bd9Sstevel@tonic-gate 	return (FRU_FAILURE);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /* ========================================================================= */
2117c478bd9Sstevel@tonic-gate /* compare the node class name to the name passed */
2127c478bd9Sstevel@tonic-gate static fru_errno_t
cmp_class_name(picl_nodehdl_t node,const char * name)2137c478bd9Sstevel@tonic-gate cmp_class_name(picl_nodehdl_t node, const char *name)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	char *class_name = NULL;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_CLASSNAME, &class_name)
2187c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
2197c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (strcmp(class_name, name) == 0) {
2237c478bd9Sstevel@tonic-gate 		free(class_name);
2247c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	free(class_name);
2287c478bd9Sstevel@tonic-gate 	return (FRU_FAILURE);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /* ========================================================================= */
2337c478bd9Sstevel@tonic-gate /* get the "frutree" root node */
2347c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_root(fru_treehdl_t * node)2357c478bd9Sstevel@tonic-gate fpt_get_root(fru_treehdl_t *node)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	picl_nodehdl_t picl_node;
2387c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	picl_err = ptree_get_root(&picl_node);
2417c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(picl_node, PICL_PROP_CHILD,
2427c478bd9Sstevel@tonic-gate 		(void *)&picl_node, sizeof (picl_node)))
2437c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
244*35551380Skmohan 		return (map_plugin_err(picl_err));
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	while (cmp_node_name(picl_node, PICL_NODE_FRUTREE)
2487c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		if ((picl_err = ptree_get_propval_by_name(picl_node,
2517c478bd9Sstevel@tonic-gate 			PICL_PROP_PEER, (void *)&picl_node,
2527c478bd9Sstevel@tonic-gate 			sizeof (picl_node))) == PICL_PROPNOTFOUND) {
2537c478bd9Sstevel@tonic-gate 			return (FRU_NODENOTFOUND);
2547c478bd9Sstevel@tonic-gate 		} else if (picl_err != PICL_SUCCESS) {
255*35551380Skmohan 			return (map_plugin_err(picl_err));
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	picl_root_node = picl_node;
2607c478bd9Sstevel@tonic-gate 	*node = PICLHDL_TO_TREEHDL(picl_node);
2617c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /* ========================================================================= */
2657c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_peer(fru_treehdl_t sibling,fru_treehdl_t * peer)2667c478bd9Sstevel@tonic-gate fpt_get_peer(fru_treehdl_t sibling, fru_treehdl_t *peer)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
2697c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(sibling);
2707c478bd9Sstevel@tonic-gate 	picl_nodehdl_t picl_peer;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	rc = ptree_get_propval_by_name(handle, PICL_PROP_PEER,
2737c478bd9Sstevel@tonic-gate 		(void *)&picl_peer, sizeof (picl_peer));
2747c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
275*35551380Skmohan 		return (map_plugin_err(rc));
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	*peer = PICLHDL_TO_TREEHDL(picl_peer);
2797c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /* ========================================================================= */
2837c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_child(fru_treehdl_t handle,fru_treehdl_t * child)2847c478bd9Sstevel@tonic-gate fpt_get_child(fru_treehdl_t handle, fru_treehdl_t *child)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	picl_nodehdl_t p_child;
2877c478bd9Sstevel@tonic-gate 	int rc = ptree_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2887c478bd9Sstevel@tonic-gate 		PICL_PROP_CHILD, (void *)&p_child, sizeof (p_child));
2897c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
290*35551380Skmohan 		return (map_plugin_err(rc));
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	*child = PICLHDL_TO_TREEHDL(p_child);
2947c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /* ========================================================================= */
2987c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_parent(fru_treehdl_t handle,fru_treehdl_t * parent)2997c478bd9Sstevel@tonic-gate fpt_get_parent(fru_treehdl_t handle, fru_treehdl_t *parent)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
3027c478bd9Sstevel@tonic-gate 	picl_nodehdl_t p_parent;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* do not allow the libfru users to see the parent of the root */
3057c478bd9Sstevel@tonic-gate 	if (TREEHDL_TO_PICLHDL(handle) == picl_root_node) {
3067c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	rc = ptree_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
3107c478bd9Sstevel@tonic-gate 		PICL_PROP_PARENT, (void *)&p_parent, sizeof (p_parent));
3117c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
312*35551380Skmohan 		return (map_plugin_err(rc));
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	*parent = PICLHDL_TO_TREEHDL(p_parent);
3167c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /* ========================================================================= */
3207c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_node_type(fru_treehdl_t node,fru_node_t * type)3217c478bd9Sstevel@tonic-gate fpt_get_node_type(fru_treehdl_t node, fru_node_t *type)
3227c478bd9Sstevel@tonic-gate {
323*35551380Skmohan 	int  rc = PICL_SUCCESS;
3247c478bd9Sstevel@tonic-gate 	char picl_class[PICL_PROPNAMELEN_MAX];
3257c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
3267c478bd9Sstevel@tonic-gate 
327*35551380Skmohan 	if ((rc = ptree_get_propval_by_name(handle, PICL_PROP_CLASSNAME,
328*35551380Skmohan 		picl_class, sizeof (picl_class))) != PICL_SUCCESS) {
329*35551380Skmohan 		return (map_plugin_err(rc));
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (strcmp(picl_class, PICL_CLASS_LOCATION) == 0)  {
3337c478bd9Sstevel@tonic-gate 		*type = FRU_NODE_LOCATION;
3347c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3357c478bd9Sstevel@tonic-gate 	} else if (strcmp(picl_class, PICL_CLASS_FRU) == 0) {
3367c478bd9Sstevel@tonic-gate 		picl_prophdl_t proph;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		/* check for the CONTAINER_PROP property which indicates */
3397c478bd9Sstevel@tonic-gate 		/* there is data for this node.  (ie fru is a container) */
3407c478bd9Sstevel@tonic-gate 		if (ptree_get_prop_by_name(handle,
3417c478bd9Sstevel@tonic-gate 			PICL_PROP_CONTAINER, &proph) == PICL_SUCCESS) {
3427c478bd9Sstevel@tonic-gate 			*type = FRU_NODE_CONTAINER;
3437c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		*type = FRU_NODE_FRU;
3467c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	*type = FRU_NODE_UNKNOWN;
3507c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /* ========================================================================= */
3547c478bd9Sstevel@tonic-gate /* find the next section or return NODENOTFOUND */
3557c478bd9Sstevel@tonic-gate static fru_errno_t
find_next_section(picl_nodehdl_t current,picl_nodehdl_t * next)3567c478bd9Sstevel@tonic-gate find_next_section(picl_nodehdl_t current, picl_nodehdl_t *next)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	picl_nodehdl_t rc_next;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (ptree_get_propval_by_name(current, PICL_PROP_PEER,
3617c478bd9Sstevel@tonic-gate 		(void *)&rc_next, sizeof (rc_next)) != PICL_SUCCESS) {
3627c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3667c478bd9Sstevel@tonic-gate 	if (cmp_class_name(rc_next, PICL_CLASS_SECTION)
3677c478bd9Sstevel@tonic-gate 			== FRU_SUCCESS) {
3687c478bd9Sstevel@tonic-gate 		*next = rc_next;
3697c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3737c478bd9Sstevel@tonic-gate 	/* is a section */
3747c478bd9Sstevel@tonic-gate 	return (find_next_section(rc_next, next));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /* ========================================================================= */
3787c478bd9Sstevel@tonic-gate /* find the first section or return NODENOTFOUND */
3797c478bd9Sstevel@tonic-gate static fru_errno_t
find_first_section(picl_nodehdl_t parent,picl_nodehdl_t * section)3807c478bd9Sstevel@tonic-gate find_first_section(picl_nodehdl_t parent, picl_nodehdl_t *section)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	picl_nodehdl_t rc_section;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (ptree_get_propval_by_name(parent, PICL_PROP_CHILD,
3857c478bd9Sstevel@tonic-gate 		(void *)&rc_section, sizeof (rc_section)) != PICL_SUCCESS) {
3867c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3907c478bd9Sstevel@tonic-gate 	if (cmp_class_name(rc_section, PICL_CLASS_SECTION)
3917c478bd9Sstevel@tonic-gate 			== FRU_SUCCESS) {
3927c478bd9Sstevel@tonic-gate 		*section = rc_section;
3937c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3977c478bd9Sstevel@tonic-gate 	/* is a section */
3987c478bd9Sstevel@tonic-gate 	return (find_next_section(rc_section, section));
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /* ========================================================================= */
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Find the handle of the segment node "segment".
4047c478bd9Sstevel@tonic-gate  * also returns the hardware description of this segment.  (read from the
4057c478bd9Sstevel@tonic-gate  * section this was found in.)
4067c478bd9Sstevel@tonic-gate  * If the ign_cor_flg is set this will still succeed even if the segment is
4077c478bd9Sstevel@tonic-gate  * corrupt, otherwise it will return FRU_SEGCORRUPT for corrupt segments
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate #define	IGN_CORRUPT_YES 1
4107c478bd9Sstevel@tonic-gate #define	IGN_CORRUPT_NO 0
4117c478bd9Sstevel@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)4127c478bd9Sstevel@tonic-gate get_segment_node(picl_nodehdl_t handle, const char *segment,
4137c478bd9Sstevel@tonic-gate 	picl_nodehdl_t *seg_hdl, fru_seg_hwdesc_t *hw_desc, int ign_cor_flg)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
4167c478bd9Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if ((err = update_data_nodes(handle)) != FRU_SUCCESS) {
4197c478bd9Sstevel@tonic-gate 		return (err);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(handle, &sect_node)) != FRU_SUCCESS) {
4237c478bd9Sstevel@tonic-gate 		return (err);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/* while there are sections. */
4277c478bd9Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
4287c478bd9Sstevel@tonic-gate 		uint32_t num_segs = 0;
4297c478bd9Sstevel@tonic-gate 		int rc = PICL_SUCCESS;
4307c478bd9Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		/* do this just in case the Segments have not been built. */
4337c478bd9Sstevel@tonic-gate 		if ((rc = ptree_get_propval_by_name(sect_node,
4347c478bd9Sstevel@tonic-gate 			PICL_PROP_NUM_SEGMENTS,
4357c478bd9Sstevel@tonic-gate 			(void *)&num_segs,
4367c478bd9Sstevel@tonic-gate 			sizeof (num_segs))) != PICL_SUCCESS) {
4377c478bd9Sstevel@tonic-gate 			return (map_plugin_err(rc));
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/* while there are segments. */
4417c478bd9Sstevel@tonic-gate 		rc = ptree_get_propval_by_name(sect_node, PICL_PROP_CHILD,
4427c478bd9Sstevel@tonic-gate 			(void *)&seg_node, sizeof (seg_node));
4437c478bd9Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
4447c478bd9Sstevel@tonic-gate 			char name[PICL_PROPNAMELEN_MAX];
4457c478bd9Sstevel@tonic-gate 			ptree_get_propval_by_name(seg_node, PICL_PROP_NAME,
4467c478bd9Sstevel@tonic-gate 				name, sizeof (name));
4477c478bd9Sstevel@tonic-gate 			if (strcmp(segment, name) == 0) {
4487c478bd9Sstevel@tonic-gate 				int dummy = 0;
4497c478bd9Sstevel@tonic-gate 				int protection = 0;
4507c478bd9Sstevel@tonic-gate 				/* NUM_TAGS prop exists iff segment is OK */
4517c478bd9Sstevel@tonic-gate 				if ((ign_cor_flg == IGN_CORRUPT_NO) &&
4527c478bd9Sstevel@tonic-gate 					(ptree_get_propval_by_name(seg_node,
4537c478bd9Sstevel@tonic-gate 					PICL_PROP_NUM_TAGS,
4547c478bd9Sstevel@tonic-gate 					(void *)&dummy,
4557c478bd9Sstevel@tonic-gate 					sizeof (dummy)) != PICL_SUCCESS)) {
4567c478bd9Sstevel@tonic-gate 					return (FRU_SEGCORRUPT);
4577c478bd9Sstevel@tonic-gate 				}
4587c478bd9Sstevel@tonic-gate 				/* get the HW protections of this section. */
459*35551380Skmohan 				if ((rc = ptree_get_propval_by_name(sect_node,
4607c478bd9Sstevel@tonic-gate 					PICL_PROP_PROTECTED,
4617c478bd9Sstevel@tonic-gate 					(void *)&protection,
462*35551380Skmohan 					sizeof (protection)))
463*35551380Skmohan 							!= PICL_SUCCESS) {
464*35551380Skmohan 					return (map_plugin_err(rc));
4657c478bd9Sstevel@tonic-gate 				}
4667c478bd9Sstevel@tonic-gate 				hw_desc->all_bits = 0;
4677c478bd9Sstevel@tonic-gate 				hw_desc->field.read_only = protection;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 				*seg_hdl = seg_node;
4707c478bd9Sstevel@tonic-gate 				return (FRU_SUCCESS);
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 			rc = ptree_get_propval_by_name(seg_node, PICL_PROP_PEER,
4737c478bd9Sstevel@tonic-gate 				(void *)&seg_node, sizeof (seg_node));
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		/* Peer property not found is ok */
4777c478bd9Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
478*35551380Skmohan 			return (map_plugin_err(rc));
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	return (FRU_INVALSEG);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /* ========================================================================= */
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * For the section handle passed add to list all the segment names found.
4907c478bd9Sstevel@tonic-gate  * Also incriments total by the number found.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate static fru_errno_t
add_segs_for_section(picl_nodehdl_t section,fru_strlist_t * list)4937c478bd9Sstevel@tonic-gate add_segs_for_section(picl_nodehdl_t section, fru_strlist_t *list)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	int num_segments = 0;
4967c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if ((rc = ptree_get_propval_by_name(section,
4997c478bd9Sstevel@tonic-gate 		PICL_PROP_NUM_SEGMENTS,
5007c478bd9Sstevel@tonic-gate 		(void *)&num_segments,
5017c478bd9Sstevel@tonic-gate 		sizeof (num_segments))) != PICL_SUCCESS) {
5027c478bd9Sstevel@tonic-gate 		fru_destroy_strlist(list);
5037c478bd9Sstevel@tonic-gate 		return (map_plugin_err(rc));
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (num_segments != 0) {
5077c478bd9Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
5087c478bd9Sstevel@tonic-gate 		int total_space = list->num + num_segments;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		list->strs = realloc(list->strs,
5117c478bd9Sstevel@tonic-gate 			(sizeof (*(list->strs)) * (total_space)));
5127c478bd9Sstevel@tonic-gate 		if (list->strs == NULL) {
5137c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		/* get the first segment */
5177c478bd9Sstevel@tonic-gate 		rc = ptree_get_propval_by_name(section,
5187c478bd9Sstevel@tonic-gate 			PICL_PROP_CHILD, (void *)&seg_node,
5197c478bd9Sstevel@tonic-gate 			sizeof (seg_node));
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/* while there are more segments. */
5227c478bd9Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
5237c478bd9Sstevel@tonic-gate 			char name[FRU_SEGNAMELEN +1];
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			if ((rc = ptree_get_propval_by_name(seg_node,
5267c478bd9Sstevel@tonic-gate 				PICL_PROP_NAME, name,
5277c478bd9Sstevel@tonic-gate 				sizeof (name))) != PICL_SUCCESS) {
5287c478bd9Sstevel@tonic-gate 				break;
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 			/* check array bounds */
5327c478bd9Sstevel@tonic-gate 			if (list->num >= total_space) {
5337c478bd9Sstevel@tonic-gate 				/* PICL reported incorrect number of segs */
5347c478bd9Sstevel@tonic-gate 				return (FRU_IOERROR);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 			list->strs[(list->num)++] = strdup(name);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 			rc = ptree_get_propval_by_name(seg_node,
5397c478bd9Sstevel@tonic-gate 				PICL_PROP_PEER, (void *)&seg_node,
5407c478bd9Sstevel@tonic-gate 				sizeof (seg_node));
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		/* Peer property not found is ok */
5447c478bd9Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
545*35551380Skmohan 			return (map_plugin_err(rc));
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /* ========================================================================= */
5537c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_list(fru_treehdl_t handle,fru_strlist_t * list)5547c478bd9Sstevel@tonic-gate fpt_get_seg_list(fru_treehdl_t handle, fru_strlist_t *list)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	fru_errno_t err;
5577c478bd9Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
5587c478bd9Sstevel@tonic-gate 	fru_strlist_t rc_list;
5597c478bd9Sstevel@tonic-gate 	rc_list.num = 0;
5607c478bd9Sstevel@tonic-gate 	rc_list.strs = NULL;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if ((err = update_data_nodes(TREEHDL_TO_PICLHDL(handle)))
5637c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5647c478bd9Sstevel@tonic-gate 		return (err);
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &sect_node))
5687c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5697c478bd9Sstevel@tonic-gate 		return (err);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/* while there are sections. */
5737c478bd9Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
5747c478bd9Sstevel@tonic-gate 		if ((err = add_segs_for_section(sect_node, &rc_list))
5757c478bd9Sstevel@tonic-gate 				!= FRU_SUCCESS) {
5767c478bd9Sstevel@tonic-gate 			fru_destroy_strlist(&rc_list);
5777c478bd9Sstevel@tonic-gate 			return (err);
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	list->num = rc_list.num;
5837c478bd9Sstevel@tonic-gate 	list->strs = rc_list.strs;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /* ========================================================================= */
5897c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_def(fru_treehdl_t handle,const char * seg_name,fru_segdef_t * def)5907c478bd9Sstevel@tonic-gate fpt_get_seg_def(fru_treehdl_t handle, const char *seg_name, fru_segdef_t *def)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
5937c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
5947c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	fru_segdesc_t desc;
5977c478bd9Sstevel@tonic-gate 	uint32_t size;
5987c478bd9Sstevel@tonic-gate 	uint32_t address;
5997c478bd9Sstevel@tonic-gate 	/* LINTED */
6007c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6037c478bd9Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS)
6047c478bd9Sstevel@tonic-gate 		return (err);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(seg_node,
6077c478bd9Sstevel@tonic-gate 		PICL_PROP_DESCRIPTOR,
6087c478bd9Sstevel@tonic-gate 		&desc, sizeof (desc))) != PICL_SUCCESS) {
609*35551380Skmohan 		return (map_plugin_err(picl_err));
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(seg_node,
6137c478bd9Sstevel@tonic-gate 		PICL_PROP_LENGTH,
6147c478bd9Sstevel@tonic-gate 		&size, sizeof (size))) != PICL_SUCCESS) {
615*35551380Skmohan 		return (map_plugin_err(picl_err));
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(seg_node,
6197c478bd9Sstevel@tonic-gate 		PICL_PROP_OFFSET,
6207c478bd9Sstevel@tonic-gate 		&address, sizeof (address))) != PICL_SUCCESS) {
621*35551380Skmohan 		return (map_plugin_err(picl_err));
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	def->version = LIBFRU_VERSION;
6257c478bd9Sstevel@tonic-gate 	strlcpy(def->name, seg_name, FRU_SEGNAMELEN+1);
6267c478bd9Sstevel@tonic-gate 	def->desc = desc;
6277c478bd9Sstevel@tonic-gate 	def->size = size;
6287c478bd9Sstevel@tonic-gate 	def->address = address;
6297c478bd9Sstevel@tonic-gate 	def->hw_desc = hw_desc;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate /* ========================================================================= */
6357c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_add_seg(fru_treehdl_t handle,fru_segdef_t * def)6367c478bd9Sstevel@tonic-gate fpt_add_seg(fru_treehdl_t handle, fru_segdef_t *def)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6397c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6407c478bd9Sstevel@tonic-gate 	picl_nodehdl_t section;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * for every section which has a ADD_SEGMENT_PROP try and add the segment
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &section))
6467c478bd9Sstevel@tonic-gate 		!= FRU_SUCCESS) {
6477c478bd9Sstevel@tonic-gate 		return (err);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	do {
6507c478bd9Sstevel@tonic-gate 		fru_segdef_t dummy;
6517c478bd9Sstevel@tonic-gate 		if ((picl_err = ptree_get_propval_by_name(section,
6527c478bd9Sstevel@tonic-gate 			PICL_PROP_ADD_SEGMENT, &dummy, sizeof (dummy)))
6537c478bd9Sstevel@tonic-gate 				== PICL_SUCCESS) {
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			picl_err = ptree_update_propval_by_name(section,
6567c478bd9Sstevel@tonic-gate 				PICL_PROP_ADD_SEGMENT, def, sizeof (*def));
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 			return (map_plugin_err(picl_err));
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate 	} while (find_next_section(section, &section) == FRU_SUCCESS);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /* ========================================================================= */
6667c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_delete_seg(fru_treehdl_t handle,const char * seg_name)6677c478bd9Sstevel@tonic-gate fpt_delete_seg(fru_treehdl_t handle, const char *seg_name)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_hdl;
6707c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6717c478bd9Sstevel@tonic-gate 	fru_errno_t err;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	int dead_flag = FRUDATA_DELETE_TAG_KEY;
6747c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6777c478bd9Sstevel@tonic-gate 		&seg_hdl, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS) {
6787c478bd9Sstevel@tonic-gate 		return (err);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	rc = ptree_update_propval_by_name(seg_hdl, PICL_PROP_DELETE_SEGMENT,
6827c478bd9Sstevel@tonic-gate 		&dead_flag, sizeof (dead_flag));
6837c478bd9Sstevel@tonic-gate 	return (map_plugin_err(rc));
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /* ========================================================================= */
6877c478bd9Sstevel@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)6887c478bd9Sstevel@tonic-gate fpt_add_tag_to_seg(fru_treehdl_t handle, const char *seg_name,
6897c478bd9Sstevel@tonic-gate 		fru_tag_t tag, uint8_t *data, size_t data_len)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6927c478bd9Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
6937c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6947c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6957c478bd9Sstevel@tonic-gate 	size_t buf_size = 0;
6967c478bd9Sstevel@tonic-gate 	uint8_t *buffer = NULL;
6977c478bd9Sstevel@tonic-gate 	picl_prophdl_t add_prop;
6987c478bd9Sstevel@tonic-gate 	ptree_propinfo_t add_prop_info;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
7017c478bd9Sstevel@tonic-gate 		&segHdl, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
7027c478bd9Sstevel@tonic-gate 		return (err);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/* get the length of the buffer required. */
7067c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_prop_by_name(segHdl,
7077c478bd9Sstevel@tonic-gate 		PICL_PROP_ADD_PACKET,
7087c478bd9Sstevel@tonic-gate 		&add_prop)) != PICL_SUCCESS) {
709*35551380Skmohan 		return (map_plugin_err(picl_err));
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propinfo(add_prop, &add_prop_info))
7137c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
714*35551380Skmohan 		return (map_plugin_err(picl_err));
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	buf_size = add_prop_info.piclinfo.size;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (data_len >= (buf_size -  get_tag_size(get_tag_type(&tag)))) {
7197c478bd9Sstevel@tonic-gate 		return (FRU_NOSPACE);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	buffer = malloc(buf_size);
7237c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
7247c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 	/* write the tag and data into the buffer */
7277c478bd9Sstevel@tonic-gate 	memcpy(buffer, &tag, get_tag_size(get_tag_type(&tag)));
7287c478bd9Sstevel@tonic-gate 	memcpy((void *)(buffer+get_tag_size(get_tag_type(&tag))),
7297c478bd9Sstevel@tonic-gate 		data, data_len);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	picl_err = ptree_update_propval(add_prop, buffer, buf_size);
7327c478bd9Sstevel@tonic-gate 	free(buffer);
7337c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /* ========================================================================= */
7377c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_tag_list(fru_treehdl_t handle,const char * seg_name,fru_tag_t ** tags,int * number)7387c478bd9Sstevel@tonic-gate fpt_get_tag_list(fru_treehdl_t handle, const char *seg_name,
7397c478bd9Sstevel@tonic-gate 		fru_tag_t **tags, int *number)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
7427c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
7437c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
7447c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagTable;
7457c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
7467c478bd9Sstevel@tonic-gate 	unsigned int total_tags = 0;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/* return variables */
7497c478bd9Sstevel@tonic-gate 	fru_tag_t *rc_tags = NULL;
7507c478bd9Sstevel@tonic-gate 	unsigned int rc_num = 0;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
7537c478bd9Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
7547c478bd9Sstevel@tonic-gate 		return (err);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/* get the number of tags and allocate array for them */
7587c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(seg_node,
7597c478bd9Sstevel@tonic-gate 		PICL_PROP_NUM_TAGS,
7607c478bd9Sstevel@tonic-gate 		(void *)&total_tags,
7617c478bd9Sstevel@tonic-gate 		sizeof (total_tags))) != PICL_SUCCESS) {
762*35551380Skmohan 		return (map_plugin_err(picl_err));
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (total_tags == 0) {
7667c478bd9Sstevel@tonic-gate 		*tags = rc_tags;
7677c478bd9Sstevel@tonic-gate 		*number = rc_num;
7687c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	rc_tags = malloc((sizeof (*rc_tags) * total_tags));
7727c478bd9Sstevel@tonic-gate 	if (rc_tags == NULL) {
7737c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/* go through the tagTable and fill in the array */
7777c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(seg_node,
7787c478bd9Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
7797c478bd9Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
7807c478bd9Sstevel@tonic-gate 		free(rc_tags);
781*35551380Skmohan 		return (map_plugin_err(picl_err));
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 	picl_err = ptree_get_next_by_col(tagTable, &tagTable);
7847c478bd9Sstevel@tonic-gate 	while (picl_err == PICL_SUCCESS) {
7857c478bd9Sstevel@tonic-gate 		/* check array bounds */
7867c478bd9Sstevel@tonic-gate 		if (rc_num >= total_tags) {
7877c478bd9Sstevel@tonic-gate 			free(rc_tags);
7887c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 		/* fill in the array */
7917c478bd9Sstevel@tonic-gate 		if ((picl_err = ptree_get_propval(tagTable,
7927c478bd9Sstevel@tonic-gate 			(void *)&(rc_tags[rc_num++]),
7937c478bd9Sstevel@tonic-gate 			sizeof (fru_tag_t))) != PICL_SUCCESS) {
7947c478bd9Sstevel@tonic-gate 			free(rc_tags);
795*35551380Skmohan 			return (map_plugin_err(picl_err));
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		/* get the next tag */
7987c478bd9Sstevel@tonic-gate 		picl_err = ptree_get_next_by_col(tagTable, &tagTable);
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (picl_err == PICL_ENDOFLIST) {
8027c478bd9Sstevel@tonic-gate 		*tags = rc_tags;
8037c478bd9Sstevel@tonic-gate 		*number = rc_num;
8047c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
8057c478bd9Sstevel@tonic-gate 	}
806*35551380Skmohan 	return (map_plugin_err(picl_err));
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /* ========================================================================= */
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * From the handle, segment name, tag, and instance of the tag get me:
8127c478bd9Sstevel@tonic-gate  * segHdl: The segment handle for this segment.
8137c478bd9Sstevel@tonic-gate  * tagHdl: tag property handle in the tag table for this instance "tag"
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@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)8167c478bd9Sstevel@tonic-gate get_tag_handle(picl_nodehdl_t handle, const char *segment,
8177c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8187c478bd9Sstevel@tonic-gate 		picl_nodehdl_t *segHdl,
8197c478bd9Sstevel@tonic-gate 		picl_prophdl_t *tagHdl)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
8227c478bd9Sstevel@tonic-gate 	fru_errno_t err;
8237c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagTable = 0;
8247c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8257c478bd9Sstevel@tonic-gate 	picl_nodehdl_t tmp_seg;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	fru_tag_t foundTag;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), segment,
8307c478bd9Sstevel@tonic-gate 		&tmp_seg, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
8317c478bd9Sstevel@tonic-gate 		return (err);
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	foundTag.raw_data = 0;
8357c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval_by_name(tmp_seg,
8367c478bd9Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
8377c478bd9Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
838*35551380Skmohan 		return (map_plugin_err(picl_err));
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	picl_err = ptree_get_next_by_col(tagTable, &tagTable);
8427c478bd9Sstevel@tonic-gate 	while ((picl_err != PICL_ENDOFLIST) &&
8437c478bd9Sstevel@tonic-gate 		(picl_err == PICL_SUCCESS)) {
8447c478bd9Sstevel@tonic-gate 		if ((picl_err = ptree_get_propval(tagTable, (void *)&foundTag,
8457c478bd9Sstevel@tonic-gate 			sizeof (foundTag))) != PICL_SUCCESS) {
846*35551380Skmohan 			return (map_plugin_err(picl_err));
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 		if ((tags_equal(tag, foundTag) == 1) && (instance-- == 0)) {
8497c478bd9Sstevel@tonic-gate 			*segHdl = tmp_seg;
8507c478bd9Sstevel@tonic-gate 			*tagHdl = tagTable;
8517c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 		picl_err = ptree_get_next_by_col(tagTable, &tagTable);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
856*35551380Skmohan 	return (map_plugin_err(picl_err));
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /* ========================================================================= */
8607c478bd9Sstevel@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)8617c478bd9Sstevel@tonic-gate fpt_get_tag_data(fru_treehdl_t handle, const char *seg_name,
8627c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8637c478bd9Sstevel@tonic-gate 		uint8_t **data, size_t *data_len)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
8667c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8677c478bd9Sstevel@tonic-gate 	uint8_t *buffer;
8687c478bd9Sstevel@tonic-gate 	int buf_len = 0;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg;
8717c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if ((err = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
8747c478bd9Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
8757c478bd9Sstevel@tonic-gate 		return (err);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_next_by_row(tagHdl, &tagHdl))
8797c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
880*35551380Skmohan 		return (map_plugin_err(picl_err));
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	buf_len = get_payload_length(&tag);
8847c478bd9Sstevel@tonic-gate 	buffer = malloc(buf_len);
8857c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
8867c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_propval(tagHdl, buffer, buf_len))
8907c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
8917c478bd9Sstevel@tonic-gate 		free(buffer);
8927c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	*data = buffer;
8967c478bd9Sstevel@tonic-gate 	*data_len = buf_len;
8977c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate /* ========================================================================= */
9017c478bd9Sstevel@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)9027c478bd9Sstevel@tonic-gate fpt_set_tag_data(fru_treehdl_t handle, const char *seg_name,
9037c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
9047c478bd9Sstevel@tonic-gate 		uint8_t *data, size_t data_len)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
9077c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg;
9107c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9137c478bd9Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
9147c478bd9Sstevel@tonic-gate 		return (rc);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_get_next_by_row(tagHdl, &tagHdl))
9187c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
919*35551380Skmohan 		return (map_plugin_err(picl_err));
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if ((picl_err = ptree_update_propval(tagHdl, data, data_len))
9237c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
9247c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /* ========================================================================= */
9317c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_delete_tag(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance)9327c478bd9Sstevel@tonic-gate fpt_delete_tag(fru_treehdl_t handle, const char *seg_name, fru_tag_t tag,
9337c478bd9Sstevel@tonic-gate 		int instance)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
9367c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
9397c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	/* get tag handle */
9427c478bd9Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9437c478bd9Sstevel@tonic-gate 		tag, instance, &segHdl, &tagHdl)) != FRU_SUCCESS) {
9447c478bd9Sstevel@tonic-gate 		return (rc);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/* set up key */
9487c478bd9Sstevel@tonic-gate 	tag.raw_data &= FRUDATA_DELETE_TAG_MASK;
9497c478bd9Sstevel@tonic-gate 	tag.raw_data |= FRUDATA_DELETE_TAG_KEY;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/* Write back */
9527c478bd9Sstevel@tonic-gate 	picl_err = ptree_update_propval(tagHdl, (void *)&(tag.raw_data),
9537c478bd9Sstevel@tonic-gate 		sizeof (tag.raw_data));
9547c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate /* ========================================================================= */
9587c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_for_each_segment(fru_treehdl_t treenode,int (* function)(fru_treeseghdl_t segment,void * args),void * args)9597c478bd9Sstevel@tonic-gate fpt_for_each_segment(fru_treehdl_t treenode,
9607c478bd9Sstevel@tonic-gate 			int (*function)(fru_treeseghdl_t segment, void *args),
9617c478bd9Sstevel@tonic-gate 			void *args)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	int		num_segments = 0, status;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	fru_errno_t	saved_status = FRU_SUCCESS;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	container = TREEHDL_TO_PICLHDL(treenode),
9687c478bd9Sstevel@tonic-gate 			section, segment;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	if ((status = update_data_nodes(container)) != FRU_SUCCESS)
9727c478bd9Sstevel@tonic-gate 		return (status);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/* process each section */
9757c478bd9Sstevel@tonic-gate 	for (status = ptree_get_propval_by_name(container, PICL_PROP_CHILD,
9767c478bd9Sstevel@tonic-gate 						&section, sizeof (section));
9777c478bd9Sstevel@tonic-gate 		status == PICL_SUCCESS;
9787c478bd9Sstevel@tonic-gate 		status = ptree_get_propval_by_name(section, PICL_PROP_PEER,
9797c478bd9Sstevel@tonic-gate 							&section,
9807c478bd9Sstevel@tonic-gate 							sizeof (section))) {
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 		if (cmp_class_name(section, PICL_CLASS_SECTION) != FRU_SUCCESS)
9837c478bd9Sstevel@tonic-gate 			continue;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		if ((status = ptree_get_propval_by_name(section,
9867c478bd9Sstevel@tonic-gate 							PICL_PROP_NUM_SEGMENTS,
9877c478bd9Sstevel@tonic-gate 							&num_segments,
9887c478bd9Sstevel@tonic-gate 							sizeof (num_segments)))
9897c478bd9Sstevel@tonic-gate 		    == PICL_PROPNOTFOUND) {
9907c478bd9Sstevel@tonic-gate 			continue;
9917c478bd9Sstevel@tonic-gate 		} else if (status != PICL_SUCCESS) {
9927c478bd9Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
9937c478bd9Sstevel@tonic-gate 			continue;
9947c478bd9Sstevel@tonic-gate 		} else if (num_segments == 0) {
9957c478bd9Sstevel@tonic-gate 			continue;
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		/* process each segment */
9997c478bd9Sstevel@tonic-gate 		for (status = ptree_get_propval_by_name(section,
10007c478bd9Sstevel@tonic-gate 							PICL_PROP_CHILD,
10017c478bd9Sstevel@tonic-gate 							&segment,
10027c478bd9Sstevel@tonic-gate 							sizeof (segment));
10037c478bd9Sstevel@tonic-gate 			status == PICL_SUCCESS;
10047c478bd9Sstevel@tonic-gate 			status = ptree_get_propval_by_name(segment,
10057c478bd9Sstevel@tonic-gate 							PICL_PROP_PEER,
10067c478bd9Sstevel@tonic-gate 							&segment,
10077c478bd9Sstevel@tonic-gate 							sizeof (segment))) {
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 			if (cmp_class_name(segment, PICL_CLASS_SEGMENT)
10107c478bd9Sstevel@tonic-gate 			    != FRU_SUCCESS) continue;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 			if ((status = function(PICLHDL_TO_TREESEGHDL(segment),
10137c478bd9Sstevel@tonic-gate 						args))
10147c478bd9Sstevel@tonic-gate 			    != FRU_SUCCESS) return (status);
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		if (status != PICL_PROPNOTFOUND)
10187c478bd9Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (status != PICL_PROPNOTFOUND)
10227c478bd9Sstevel@tonic-gate 		saved_status = map_plugin_err(status);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	return (saved_status);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /* ========================================================================= */
10287c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_segment_name(fru_treeseghdl_t segment,char ** name)10297c478bd9Sstevel@tonic-gate fpt_get_segment_name(fru_treeseghdl_t segment, char **name)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	char			*propval;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	int			status;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph = 0;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 
1040*35551380Skmohan 	if ((status = ptree_get_prop_by_name(TREESEGHDL_TO_PICLHDL(segment),
1041*35551380Skmohan 		PICL_PROP_NAME, &proph))
10427c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS)
1043*35551380Skmohan 		return (map_plugin_err(status));
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (ptree_get_propinfo(proph, &propinfo) != PICL_SUCCESS)
1046*35551380Skmohan 		return (map_plugin_err(status));
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (propinfo.piclinfo.size == 0)
10497c478bd9Sstevel@tonic-gate 		return (FRU_INVALDATASIZE);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if ((propval = malloc(propinfo.piclinfo.size)) == NULL)
10527c478bd9Sstevel@tonic-gate 		return (FRU_NOSPACE);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if ((status = ptree_get_propval(proph, propval, propinfo.piclinfo.size))
10557c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS) {
10567c478bd9Sstevel@tonic-gate 		free(propval);
10577c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	*name = propval;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /* ========================================================================= */
10667c478bd9Sstevel@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)10677c478bd9Sstevel@tonic-gate fpt_for_each_packet(fru_treeseghdl_t treesegment,
10687c478bd9Sstevel@tonic-gate 			int (*function)(fru_tag_t *tag, uint8_t *payload,
10697c478bd9Sstevel@tonic-gate 					size_t length,
10707c478bd9Sstevel@tonic-gate 			void *args),
10717c478bd9Sstevel@tonic-gate     void *args)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	int			status;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	uint8_t			*payload;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		segment = TREESEGHDL_TO_PICLHDL(treesegment);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	picl_prophdl_t		packet, payloadh = 0;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	fru_segdesc_t		descriptor;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	fru_tag_t		tag;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if ((status = ptree_get_propval_by_name(segment, PICL_PROP_DESCRIPTOR,
10897c478bd9Sstevel@tonic-gate 						&descriptor,
10907c478bd9Sstevel@tonic-gate 						sizeof (descriptor)))
10917c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS) return (map_plugin_err(status));
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	if (descriptor.field.opaque)
10947c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (descriptor.field.encrypted && (encrypt_func == NULL))
10977c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if ((status = ptree_get_propval_by_name(segment, PICL_PROP_PACKET_TABLE,
11007c478bd9Sstevel@tonic-gate 						&packet, sizeof (packet)))
11017c478bd9Sstevel@tonic-gate 	    == PICL_PROPNOTFOUND)
11027c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
11037c478bd9Sstevel@tonic-gate 	else if (status != PICL_SUCCESS)
11047c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	while ((status = ptree_get_next_by_col(packet, &packet))
11077c478bd9Sstevel@tonic-gate 		== PICL_SUCCESS) {
11087c478bd9Sstevel@tonic-gate 		if (((status = ptree_get_propval(packet, &tag, sizeof (tag)))
11097c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
11107c478bd9Sstevel@tonic-gate 		    ((status = ptree_get_next_by_row(packet, &payloadh))
11117c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
11127c478bd9Sstevel@tonic-gate 		    ((status = ptree_get_propinfo(payloadh, &propinfo))
11137c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS))
11147c478bd9Sstevel@tonic-gate 			return (map_plugin_err(status));
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		if (propinfo.piclinfo.size > 0) {
11177c478bd9Sstevel@tonic-gate 			payload = alloca(propinfo.piclinfo.size);
11187c478bd9Sstevel@tonic-gate 			if ((status = ptree_get_propval(payloadh, payload,
11197c478bd9Sstevel@tonic-gate 							propinfo.piclinfo.size))
11207c478bd9Sstevel@tonic-gate 			    != PICL_SUCCESS) return (map_plugin_err(status));
11217c478bd9Sstevel@tonic-gate 		} else {
11227c478bd9Sstevel@tonic-gate 			payload = NULL;
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		if ((descriptor.field.encrypted) &&
11267c478bd9Sstevel@tonic-gate 		    ((status = encrypt_func(FRU_DECRYPT, payload,
11277c478bd9Sstevel@tonic-gate 						propinfo.piclinfo.size))
11287c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS)) return status;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		if ((status = function(&tag, payload, propinfo.piclinfo.size,
11317c478bd9Sstevel@tonic-gate 					args))
11327c478bd9Sstevel@tonic-gate 		    != FRU_SUCCESS) return (status);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (status == PICL_ENDOFLIST)
11367c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
11377c478bd9Sstevel@tonic-gate 	else
11387c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate /* ========================================================================= */
11427c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
11437c478bd9Sstevel@tonic-gate static fru_errno_t
initialize(int argc,char ** argv)11447c478bd9Sstevel@tonic-gate initialize(int argc, char **argv)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /* ========================================================================= */
11507c478bd9Sstevel@tonic-gate static fru_errno_t
shutdown(void)11517c478bd9Sstevel@tonic-gate shutdown(void)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /* ========================================================================= */
11577c478bd9Sstevel@tonic-gate /* object for libfru to link to */
11587c478bd9Sstevel@tonic-gate fru_datasource_t data_source =
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	LIBFRU_DS_VER,
11617c478bd9Sstevel@tonic-gate 	initialize,
11627c478bd9Sstevel@tonic-gate 	shutdown,
11637c478bd9Sstevel@tonic-gate 	fpt_get_root,
11647c478bd9Sstevel@tonic-gate 	fpt_get_child,
11657c478bd9Sstevel@tonic-gate 	fpt_get_peer,
11667c478bd9Sstevel@tonic-gate 	fpt_get_parent,
11677c478bd9Sstevel@tonic-gate 	fpt_get_name_from_hdl,
11687c478bd9Sstevel@tonic-gate 	fpt_get_node_type,
11697c478bd9Sstevel@tonic-gate 	fpt_get_seg_list,
11707c478bd9Sstevel@tonic-gate 	fpt_get_seg_def,
11717c478bd9Sstevel@tonic-gate 	fpt_add_seg,
11727c478bd9Sstevel@tonic-gate 	fpt_delete_seg,
11737c478bd9Sstevel@tonic-gate 	fpt_for_each_segment,
11747c478bd9Sstevel@tonic-gate 	fpt_get_segment_name,
11757c478bd9Sstevel@tonic-gate 	fpt_add_tag_to_seg,
11767c478bd9Sstevel@tonic-gate 	fpt_get_tag_list,
11777c478bd9Sstevel@tonic-gate 	fpt_get_tag_data,
11787c478bd9Sstevel@tonic-gate 	fpt_set_tag_data,
11797c478bd9Sstevel@tonic-gate 	fpt_delete_tag,
11807c478bd9Sstevel@tonic-gate 	fpt_for_each_packet
11817c478bd9Sstevel@tonic-gate };
1182