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