2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <alloca.h>
2N/A#include <picl.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <stdarg.h>
2N/A#include <stdio.h>
2N/A#include "picldefs.h"
2N/A#include "fru_data.h"
2N/A
2N/A#include "libfruds.h"
2N/A#include "libfrup.h"
2N/A
2N/A/* ========================================================================= */
2N/A#define TREEHDL_TO_PICLHDL(treehdl) ((picl_nodehdl_t)treehdl)
2N/A#define PICLHDL_TO_TREEHDL(piclhdl) ((fru_treehdl_t)piclhdl)
2N/A
2N/A#define TREESEGHDL_TO_PICLHDL(treeseghdl) ((picl_nodehdl_t)treeseghdl)
2N/A#define PICLHDL_TO_TREESEGHDL(piclhdl) ((fru_treeseghdl_t)piclhdl)
2N/A
2N/A/* Cache of the root node for quick checks */
2N/Astatic picl_nodehdl_t picl_root_node;
2N/A
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * Map the PICL errors the plugin would give me to FRU errors
2N/A */
2N/Astatic fru_errno_t
2N/Amap_plugin_err(int picl_err)
2N/A{
2N/A switch (picl_err) {
2N/A case PICL_SUCCESS:
2N/A return (FRU_SUCCESS);
2N/A case PICL_PERMDENIED:
2N/A return (FRU_INVALPERM);
2N/A case PICL_PROPEXISTS:
2N/A return (FRU_DUPSEG);
2N/A case PICL_NOSPACE:
2N/A return (FRU_NOSPACE);
2N/A case PICL_NORESPONSE:
2N/A return (FRU_NORESPONSE);
2N/A case PICL_PROPNOTFOUND:
2N/A return (FRU_NODENOTFOUND);
2N/A case PICL_ENDOFLIST:
2N/A return (FRU_DATANOTFOUND);
2N/A }
2N/A return (FRU_IOERROR);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * cause a refresh of the sub-nodes by writing anything to the container
2N/A * property of the node.
2N/A */
2N/Astatic fru_errno_t
2N/Aupdate_data_nodes(picl_nodehdl_t handle)
2N/A{
2N/A uint32_t container = FRUDATA_DELETE_TAG_KEY;
2N/A int picl_err = PICL_SUCCESS;
2N/A
2N/A if ((picl_err = picl_set_propval_by_name(handle,
2N/A PICL_PROP_CONTAINER, (void *)&container,
2N/A sizeof (container))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * picl like function which gets a string property with the proper length
2N/A * NOTE: returns picl errno values NOT fru_errno_t
2N/A */
2N/Astatic int
2N/Aget_strprop_by_name(picl_nodehdl_t handle, char *prop_name, char **string)
2N/A{
2N/A int picl_err = PICL_SUCCESS;
2N/A picl_prophdl_t proph;
2N/A picl_propinfo_t prop_info;
2N/A char *tmp_buf = NULL;
2N/A
2N/A if ((picl_err = picl_get_propinfo_by_name(handle, prop_name,
2N/A &prop_info, &proph)) != PICL_SUCCESS) {
2N/A return (picl_err);
2N/A }
2N/A
2N/A tmp_buf = malloc((sizeof (*tmp_buf) * prop_info.size));
2N/A if (tmp_buf == NULL) {
2N/A return (PICL_FAILURE);
2N/A }
2N/A
2N/A if ((picl_err = picl_get_propval(proph, tmp_buf, prop_info.size))
2N/A != PICL_SUCCESS) {
2N/A free(tmp_buf);
2N/A return (picl_err);
2N/A }
2N/A
2N/A *string = tmp_buf;
2N/A return (PICL_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_name_from_hdl(fru_treehdl_t node, char **name)
2N/A{
2N/A int picl_err = PICL_SUCCESS;
2N/A char *tmp_name = NULL;
2N/A char *label = NULL;
2N/A picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
2N/A
2N/A /* get the name */
2N/A if ((picl_err = get_strprop_by_name(handle, PICL_PROP_NAME,
2N/A &tmp_name)) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A /* get the label, if any */
2N/A if ((picl_err = get_strprop_by_name(handle, PICL_PROP_LABEL,
2N/A &label)) != PICL_SUCCESS) {
2N/A if (picl_err != PICL_PROPNOTFOUND) {
2N/A free(tmp_name);
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A /* else PICL_PROPNOTFOUND is OK because not all nodes */
2N/A /* will have a label. */
2N/A }
2N/A
2N/A /* construct the name as nessecary */
2N/A if (label == NULL) {
2N/A *name = strdup(tmp_name);
2N/A } else {
2N/A#define FRU_LABEL_PADDING 10
2N/A size_t buf_size = strlen(tmp_name) + strlen(label) +
2N/A FRU_LABEL_PADDING;
2N/A char *tmp = malloc(buf_size);
2N/A if (tmp == NULL) {
2N/A free(tmp_name);
2N/A free(label);
2N/A return (FRU_FAILURE);
2N/A }
2N/A snprintf(tmp, buf_size, "%s?%s=%s", tmp_name,
2N/A PICL_PROP_LABEL, label);
2N/A *name = tmp;
2N/A }
2N/A
2N/A free(tmp_name);
2N/A free(label);
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* compare the node name to the name passed */
2N/Astatic fru_errno_t
2N/Acmp_node_name(picl_nodehdl_t node, const char *name)
2N/A{
2N/A char *node_name = NULL;
2N/A
2N/A if (get_strprop_by_name(node, PICL_PROP_NAME, &node_name)
2N/A != PICL_SUCCESS) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A if (strcmp(node_name, name) == 0) {
2N/A free(node_name);
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A free(node_name);
2N/A return (FRU_FAILURE);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* compare the node class name to the name passed */
2N/Astatic fru_errno_t
2N/Acmp_class_name(picl_nodehdl_t node, const char *name)
2N/A{
2N/A char *class_name = NULL;
2N/A
2N/A if (get_strprop_by_name(node, PICL_PROP_CLASSNAME, &class_name)
2N/A != PICL_SUCCESS) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A if (strcmp(class_name, name) == 0) {
2N/A free(class_name);
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A free(class_name);
2N/A return (FRU_FAILURE);
2N/A}
2N/A
2N/A
2N/A/* ========================================================================= */
2N/A/* get the "frutree" root node */
2N/Astatic fru_errno_t
2N/Afpt_get_root(fru_treehdl_t *node)
2N/A{
2N/A picl_nodehdl_t picl_node;
2N/A int picl_err = PICL_SUCCESS;
2N/A
2N/A picl_err = picl_get_root(&picl_node);
2N/A if ((picl_err = picl_get_propval_by_name(picl_node, PICL_PROP_CHILD,
2N/A (void *)&picl_node, sizeof (picl_node)))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A while (cmp_node_name(picl_node, PICL_NODE_FRUTREE)
2N/A != FRU_SUCCESS) {
2N/A
2N/A if ((picl_err = picl_get_propval_by_name(picl_node,
2N/A PICL_PROP_PEER, (void *)&picl_node,
2N/A sizeof (picl_node))) == PICL_PROPNOTFOUND) {
2N/A return (FRU_NODENOTFOUND);
2N/A } else if (picl_err != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A }
2N/A
2N/A picl_root_node = picl_node;
2N/A *node = PICLHDL_TO_TREEHDL(picl_node);
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_peer(fru_treehdl_t sibling, fru_treehdl_t *peer)
2N/A{
2N/A int rc = PICL_SUCCESS;
2N/A picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(sibling);
2N/A picl_nodehdl_t picl_peer;
2N/A
2N/A rc = picl_get_propval_by_name(handle, PICL_PROP_PEER,
2N/A (void *)&picl_peer, sizeof (picl_peer));
2N/A if (rc != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A *peer = PICLHDL_TO_TREEHDL(picl_peer);
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_child(fru_treehdl_t handle, fru_treehdl_t *child)
2N/A{
2N/A picl_nodehdl_t p_child;
2N/A int rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2N/A PICL_PROP_CHILD, (void *)&p_child, sizeof (p_child));
2N/A if (rc != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A *child = PICLHDL_TO_TREEHDL(p_child);
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_parent(fru_treehdl_t handle, fru_treehdl_t *parent)
2N/A{
2N/A int rc = PICL_SUCCESS;
2N/A picl_nodehdl_t p_parent;
2N/A
2N/A /* do not allow the libfru users to see the parent of the root */
2N/A if (TREEHDL_TO_PICLHDL(handle) == picl_root_node) {
2N/A return (FRU_NODENOTFOUND);
2N/A }
2N/A
2N/A rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2N/A PICL_PROP_PARENT, (void *)&p_parent, sizeof (p_parent));
2N/A if (rc != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A *parent = PICLHDL_TO_TREEHDL(p_parent);
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_node_type(fru_treehdl_t node, fru_node_t *type)
2N/A{
2N/A int rc = PICL_SUCCESS;
2N/A char picl_class[PICL_PROPNAMELEN_MAX];
2N/A picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
2N/A
2N/A if ((rc = picl_get_propval_by_name(handle, PICL_PROP_CLASSNAME,
2N/A picl_class, sizeof (picl_class))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A if (strcmp(picl_class, PICL_CLASS_LOCATION) == 0) {
2N/A *type = FRU_NODE_LOCATION;
2N/A return (FRU_SUCCESS);
2N/A } else if (strcmp(picl_class, PICL_CLASS_FRU) == 0) {
2N/A picl_prophdl_t proph;
2N/A
2N/A /* check for the CONTAINER_PROP property which indicates */
2N/A /* there is data for this node. (ie fru is a container) */
2N/A if (picl_get_prop_by_name(handle,
2N/A PICL_PROP_CONTAINER, &proph) == PICL_SUCCESS) {
2N/A *type = FRU_NODE_CONTAINER;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A *type = FRU_NODE_FRU;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A *type = FRU_NODE_UNKNOWN;
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* find the next section or return NODENOTFOUND */
2N/Astatic fru_errno_t
2N/Afind_next_section(picl_nodehdl_t current, picl_nodehdl_t *next)
2N/A{
2N/A picl_nodehdl_t rc_next;
2N/A
2N/A if (picl_get_propval_by_name(current, PICL_PROP_PEER,
2N/A (void *)&rc_next, sizeof (rc_next)) != PICL_SUCCESS) {
2N/A return (FRU_NODENOTFOUND);
2N/A }
2N/A
2N/A /* Make sure this is a "Section" node */
2N/A if (cmp_class_name(rc_next, PICL_CLASS_SECTION)
2N/A == FRU_SUCCESS) {
2N/A *next = rc_next;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A /* and if this is not good keep trying to find a peer which */
2N/A /* is a section */
2N/A return (find_next_section(rc_next, next));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* find the first section or return NODENOTFOUND */
2N/Astatic fru_errno_t
2N/Afind_first_section(picl_nodehdl_t parent, picl_nodehdl_t *section)
2N/A{
2N/A picl_nodehdl_t rc_section;
2N/A
2N/A if (picl_get_propval_by_name(parent, PICL_PROP_CHILD,
2N/A (void *)&rc_section, sizeof (rc_section)) != PICL_SUCCESS) {
2N/A return (FRU_NODENOTFOUND);
2N/A }
2N/A
2N/A /* Make sure this is a "Section" node */
2N/A if (cmp_class_name(rc_section, PICL_CLASS_SECTION)
2N/A == FRU_SUCCESS) {
2N/A *section = rc_section;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A /* and if this is not good keep trying to find a peer which */
2N/A /* is a section */
2N/A return (find_next_section(rc_section, section));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * Find the handle of the segment node "segment".
2N/A * also returns the hardware description of this segment. (read from the
2N/A * section this was found in.)
2N/A * If the ign_cor_flg is set this will still succeed even if the segment is
2N/A * corrupt, otherwise it will return FRU_SEGCORRUPT for corrupt segments
2N/A */
2N/A#define IGN_CORRUPT_YES 1
2N/A#define IGN_CORRUPT_NO 0
2N/Astatic fru_errno_t
2N/Aget_segment_node(picl_nodehdl_t handle, const char *segment,
2N/A picl_nodehdl_t *seg_hdl, fru_seg_hwdesc_t *hw_desc, int ign_cor_flg)
2N/A{
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A picl_nodehdl_t sect_node;
2N/A
2N/A if ((err = update_data_nodes(handle)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A if ((err = find_first_section(handle, &sect_node)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A /* while there are sections. */
2N/A while (err == FRU_SUCCESS) {
2N/A uint32_t num_segs = 0;
2N/A int rc = PICL_SUCCESS;
2N/A picl_nodehdl_t seg_node;
2N/A
2N/A /* do this just in case the Segments have not been built. */
2N/A if ((rc = picl_get_propval_by_name(sect_node,
2N/A PICL_PROP_NUM_SEGMENTS,
2N/A (void *)&num_segs,
2N/A sizeof (num_segs))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A /* while there are segments. */
2N/A rc = picl_get_propval_by_name(sect_node, PICL_PROP_CHILD,
2N/A (void *)&seg_node, sizeof (seg_node));
2N/A while (rc == PICL_SUCCESS) {
2N/A char name[PICL_PROPNAMELEN_MAX];
2N/A picl_get_propval_by_name(seg_node, PICL_PROP_NAME,
2N/A name, sizeof (name));
2N/A if (strcmp(segment, name) == 0) {
2N/A int dummy = 0;
2N/A int protection = 0;
2N/A /* NUM_TAGS prop exists iff segment is OK */
2N/A if ((ign_cor_flg == IGN_CORRUPT_NO) &&
2N/A (picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_NUM_TAGS,
2N/A (void *)&dummy,
2N/A sizeof (dummy)) != PICL_SUCCESS)) {
2N/A return (FRU_SEGCORRUPT);
2N/A }
2N/A /* get the HW protections of this section. */
2N/A if ((rc = picl_get_propval_by_name(sect_node,
2N/A PICL_PROP_PROTECTED,
2N/A (void *)&protection,
2N/A sizeof (protection)))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A hw_desc->all_bits = 0;
2N/A hw_desc->field.read_only = protection;
2N/A
2N/A *seg_hdl = seg_node;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A rc = picl_get_propval_by_name(seg_node, PICL_PROP_PEER,
2N/A (void *)&seg_node, sizeof (seg_node));
2N/A }
2N/A
2N/A /* Peer property not found is ok */
2N/A if (rc != PICL_PROPNOTFOUND) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A err = find_next_section(sect_node, &sect_node);
2N/A }
2N/A
2N/A return (FRU_INVALSEG);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * For the section handle passed add to list all the segment names found.
2N/A * Also incriments total by the number found.
2N/A */
2N/Astatic fru_errno_t
2N/Aadd_segs_for_section(picl_nodehdl_t section, fru_strlist_t *list)
2N/A{
2N/A uint32_t num_segments = 0;
2N/A int rc = PICL_SUCCESS;
2N/A
2N/A if ((rc = picl_get_propval_by_name(section,
2N/A PICL_PROP_NUM_SEGMENTS,
2N/A (void *)&num_segments,
2N/A sizeof (num_segments))) != PICL_SUCCESS) {
2N/A fru_destroy_strlist(list);
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A if (num_segments != 0) {
2N/A picl_nodehdl_t seg_node;
2N/A int total_space = list->num + num_segments;
2N/A
2N/A list->strs = realloc(list->strs,
2N/A (sizeof (*(list->strs)) * (total_space)));
2N/A if (list->strs == NULL) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A /* get the first segment */
2N/A rc = picl_get_propval_by_name(section,
2N/A PICL_PROP_CHILD, (void *)&seg_node,
2N/A sizeof (seg_node));
2N/A
2N/A /* while there are more segments. */
2N/A while (rc == PICL_SUCCESS) {
2N/A char name[FRU_SEGNAMELEN +1];
2N/A
2N/A if ((rc = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_NAME, name,
2N/A sizeof (name))) != PICL_SUCCESS) {
2N/A break;
2N/A }
2N/A
2N/A /* check array bounds */
2N/A if (list->num >= total_space) {
2N/A /* PICL reported incorrect number of segs */
2N/A return (FRU_IOERROR);
2N/A }
2N/A list->strs[(list->num)++] = strdup(name);
2N/A
2N/A rc = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_PEER, (void *)&seg_node,
2N/A sizeof (seg_node));
2N/A }
2N/A
2N/A /* Peer property not found is ok */
2N/A if (rc != PICL_PROPNOTFOUND) {
2N/A return (map_plugin_err(rc));
2N/A }
2N/A
2N/A }
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_seg_list(fru_treehdl_t handle, fru_strlist_t *list)
2N/A{
2N/A fru_errno_t err;
2N/A picl_nodehdl_t sect_node;
2N/A fru_strlist_t rc_list;
2N/A rc_list.num = 0;
2N/A rc_list.strs = NULL;
2N/A
2N/A if ((err = update_data_nodes(TREEHDL_TO_PICLHDL(handle)))
2N/A != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &sect_node))
2N/A != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A /* while there are sections. */
2N/A while (err == FRU_SUCCESS) {
2N/A if ((err = add_segs_for_section(sect_node, &rc_list))
2N/A != FRU_SUCCESS) {
2N/A fru_destroy_strlist(&rc_list);
2N/A return (err);
2N/A }
2N/A err = find_next_section(sect_node, &sect_node);
2N/A }
2N/A
2N/A list->num = rc_list.num;
2N/A list->strs = rc_list.strs;
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_seg_def(fru_treehdl_t handle, const char *seg_name, fru_segdef_t *def)
2N/A{
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A picl_nodehdl_t seg_node;
2N/A fru_seg_hwdesc_t hw_desc;
2N/A
2N/A fru_segdesc_t desc;
2N/A uint32_t size;
2N/A uint32_t address;
2N/A /* LINTED */
2N/A int picl_err = PICL_SUCCESS;
2N/A
2N/A if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A &seg_node, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS)
2N/A return (err);
2N/A
2N/A if ((picl_err = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_DESCRIPTOR,
2N/A &desc, sizeof (desc))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A if ((picl_err = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_LENGTH,
2N/A &size, sizeof (size))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A if ((picl_err = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_OFFSET,
2N/A &address, sizeof (address))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A def->version = LIBFRU_VERSION;
2N/A strlcpy(def->name, seg_name, FRU_SEGNAMELEN+1);
2N/A def->desc = desc;
2N/A def->size = size;
2N/A def->address = address;
2N/A def->hw_desc = hw_desc;
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_add_seg(fru_treehdl_t handle, fru_segdef_t *def)
2N/A{
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A int picl_err = PICL_SUCCESS;
2N/A picl_nodehdl_t section;
2N/A
2N/A/*
2N/A * for every section which has a ADD_SEGMENT_PROP try and add the segment
2N/A */
2N/A if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &section))
2N/A != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A do {
2N/A fru_segdef_t dummy;
2N/A if ((picl_err = picl_get_propval_by_name(section,
2N/A PICL_PROP_ADD_SEGMENT, &dummy, sizeof (dummy)))
2N/A == PICL_SUCCESS) {
2N/A
2N/A picl_err = picl_set_propval_by_name(section,
2N/A PICL_PROP_ADD_SEGMENT, def, sizeof (*def));
2N/A
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A } while (find_next_section(section, &section) == FRU_SUCCESS);
2N/A
2N/A return (map_plugin_err(picl_err));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_delete_seg(fru_treehdl_t handle, const char *seg_name)
2N/A{
2N/A picl_nodehdl_t seg_hdl;
2N/A fru_seg_hwdesc_t hw_desc;
2N/A fru_errno_t err;
2N/A
2N/A int dead_flag = FRUDATA_DELETE_TAG_KEY;
2N/A int rc = PICL_SUCCESS;
2N/A
2N/A if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A &seg_hdl, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A rc = picl_set_propval_by_name(seg_hdl, PICL_PROP_DELETE_SEGMENT,
2N/A &dead_flag, sizeof (dead_flag));
2N/A return (map_plugin_err(rc));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_add_tag_to_seg(fru_treehdl_t handle, const char *seg_name,
2N/A fru_tag_t tag, uint8_t *data, size_t data_len)
2N/A{
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A picl_nodehdl_t segHdl;
2N/A fru_seg_hwdesc_t hw_desc;
2N/A int picl_err = PICL_SUCCESS;
2N/A size_t buf_size = 0;
2N/A uint8_t *buffer = NULL;
2N/A picl_prophdl_t add_prop;
2N/A picl_propinfo_t add_prop_info;
2N/A
2N/A if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A &segHdl, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A /* get the length of the buffer required. */
2N/A if ((picl_err = picl_get_prop_by_name(segHdl,
2N/A PICL_PROP_ADD_PACKET,
2N/A &add_prop)) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A if ((picl_err = picl_get_propinfo(add_prop, &add_prop_info))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A buf_size = add_prop_info.size;
2N/A if (data_len >= (buf_size - get_tag_size(get_tag_type(&tag)))) {
2N/A return (FRU_NOSPACE);
2N/A }
2N/A
2N/A buffer = malloc(buf_size);
2N/A if (buffer == NULL) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A /* write the tag and data into the buffer */
2N/A memcpy(buffer, &tag, get_tag_size(get_tag_type(&tag)));
2N/A memcpy((void *)(buffer+get_tag_size(get_tag_type(&tag))),
2N/A data, data_len);
2N/A
2N/A picl_err = picl_set_propval(add_prop, buffer, buf_size);
2N/A free(buffer);
2N/A return (map_plugin_err(picl_err));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_tag_list(fru_treehdl_t handle, const char *seg_name,
2N/A fru_tag_t **tags, int *number)
2N/A{
2N/A picl_nodehdl_t seg_node;
2N/A fru_seg_hwdesc_t hw_desc;
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A picl_prophdl_t tagTable;
2N/A int picl_err = PICL_SUCCESS;
2N/A unsigned int total_tags = 0;
2N/A
2N/A /* return variables */
2N/A fru_tag_t *rc_tags = NULL;
2N/A unsigned int rc_num = 0;
2N/A
2N/A if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A &seg_node, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A /* get the number of tags and allocate array for them */
2N/A if ((picl_err = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_NUM_TAGS,
2N/A (void *)&total_tags,
2N/A sizeof (total_tags))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A if (total_tags == 0) {
2N/A *tags = rc_tags;
2N/A *number = rc_num;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A
2N/A rc_tags = malloc((sizeof (*rc_tags) * total_tags));
2N/A if (rc_tags == NULL) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A /* go through the tagTable and fill in the array */
2N/A if ((picl_err = picl_get_propval_by_name(seg_node,
2N/A PICL_PROP_PACKET_TABLE,
2N/A &tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
2N/A free(rc_tags);
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A picl_err = picl_get_next_by_col(tagTable, &tagTable);
2N/A while (picl_err == PICL_SUCCESS) {
2N/A /* check array bounds */
2N/A if (rc_num >= total_tags) {
2N/A free(rc_tags);
2N/A return (FRU_FAILURE);
2N/A }
2N/A /* fill in the array */
2N/A if ((picl_err = picl_get_propval(tagTable,
2N/A (void *)&(rc_tags[rc_num++]),
2N/A sizeof (fru_tag_t))) != PICL_SUCCESS) {
2N/A free(rc_tags);
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A /* get the next tag */
2N/A picl_err = picl_get_next_by_col(tagTable, &tagTable);
2N/A }
2N/A
2N/A if (picl_err == PICL_ENDOFLIST) {
2N/A *tags = rc_tags;
2N/A *number = rc_num;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A return (map_plugin_err(picl_err));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/*
2N/A * From the handle, segment name, tag, and instance of the tag get me:
2N/A * segHdl: The segment handle for this segment.
2N/A * tagHdl: tag property handle in the tag table for this instance "tag"
2N/A */
2N/Astatic fru_errno_t
2N/Aget_tag_handle(picl_nodehdl_t handle, const char *segment,
2N/A fru_tag_t tag, int instance,
2N/A picl_nodehdl_t *segHdl,
2N/A picl_prophdl_t *tagHdl)
2N/A{
2N/A fru_seg_hwdesc_t hw_desc;
2N/A fru_errno_t err;
2N/A picl_prophdl_t tagTable = 0;
2N/A int picl_err = PICL_SUCCESS;
2N/A picl_nodehdl_t tmp_seg;
2N/A
2N/A fru_tag_t foundTag;
2N/A
2N/A if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), segment,
2N/A &tmp_seg, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A foundTag.raw_data = 0;
2N/A if ((picl_err = picl_get_propval_by_name(tmp_seg,
2N/A PICL_PROP_PACKET_TABLE,
2N/A &tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A picl_err = picl_get_next_by_col(tagTable, &tagTable);
2N/A while ((picl_err != PICL_ENDOFLIST) &&
2N/A (picl_err == PICL_SUCCESS)) {
2N/A if ((picl_err = picl_get_propval(tagTable, (void *)&foundTag,
2N/A sizeof (foundTag))) != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A if ((tags_equal(tag, foundTag) == 1) && (instance-- == 0)) {
2N/A *segHdl = tmp_seg;
2N/A *tagHdl = tagTable;
2N/A return (FRU_SUCCESS);
2N/A }
2N/A picl_err = picl_get_next_by_col(tagTable, &tagTable);
2N/A }
2N/A
2N/A return (map_plugin_err(picl_err));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_tag_data(fru_treehdl_t handle, const char *seg_name,
2N/A fru_tag_t tag, int instance,
2N/A uint8_t **data, size_t *data_len)
2N/A{
2N/A fru_errno_t err = FRU_SUCCESS;
2N/A int picl_err = PICL_SUCCESS;
2N/A uint8_t *buffer;
2N/A int buf_len = 0;
2N/A
2N/A picl_nodehdl_t seg;
2N/A picl_prophdl_t tagHdl;
2N/A
2N/A if ((err = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
2N/A return (err);
2N/A }
2N/A
2N/A if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A buf_len = get_payload_length(&tag);
2N/A buffer = malloc(buf_len);
2N/A if (buffer == NULL) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A if ((picl_err = picl_get_propval(tagHdl, buffer, buf_len))
2N/A != PICL_SUCCESS) {
2N/A free(buffer);
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A *data = buffer;
2N/A *data_len = buf_len;
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_set_tag_data(fru_treehdl_t handle, const char *seg_name,
2N/A fru_tag_t tag, int instance,
2N/A uint8_t *data, size_t data_len)
2N/A{
2N/A fru_errno_t rc = FRU_SUCCESS;
2N/A int picl_err = PICL_SUCCESS;
2N/A
2N/A picl_nodehdl_t seg;
2N/A picl_prophdl_t tagHdl;
2N/A
2N/A if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
2N/A return (rc);
2N/A }
2N/A
2N/A if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A if ((picl_err = picl_set_propval(tagHdl, data, data_len))
2N/A != PICL_SUCCESS) {
2N/A return (map_plugin_err(picl_err));
2N/A }
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_delete_tag(fru_treehdl_t handle, const char *seg_name, fru_tag_t tag,
2N/A int instance)
2N/A{
2N/A fru_errno_t rc = FRU_SUCCESS;
2N/A int picl_err = PICL_SUCCESS;
2N/A
2N/A picl_nodehdl_t segHdl;
2N/A picl_prophdl_t tagHdl;
2N/A
2N/A /* get tag handle */
2N/A if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
2N/A tag, instance, &segHdl, &tagHdl)) != FRU_SUCCESS) {
2N/A return (rc);
2N/A }
2N/A
2N/A /* set up key */
2N/A tag.raw_data &= FRUDATA_DELETE_TAG_MASK;
2N/A tag.raw_data |= FRUDATA_DELETE_TAG_KEY;
2N/A
2N/A /* Write back */
2N/A picl_err = picl_set_propval(tagHdl, (void *)&(tag.raw_data),
2N/A sizeof (tag.raw_data));
2N/A return (map_plugin_err(picl_err));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_for_each_segment(fru_treehdl_t treenode,
2N/A int (*function)(fru_treeseghdl_t segment, void *args),
2N/A void *args)
2N/A{
2N/A int num_segments = 0, status;
2N/A
2N/A fru_errno_t saved_status = FRU_SUCCESS;
2N/A
2N/A picl_nodehdl_t container = TREEHDL_TO_PICLHDL(treenode),
2N/A section, segment;
2N/A
2N/A
2N/A if ((status = update_data_nodes(container)) != FRU_SUCCESS)
2N/A return (status);
2N/A
2N/A /* process each section */
2N/A for (status = picl_get_propval_by_name(container, PICL_PROP_CHILD,
2N/A &section, sizeof (section));
2N/A status == PICL_SUCCESS;
2N/A status = picl_get_propval_by_name(section, PICL_PROP_PEER,
2N/A &section,
2N/A sizeof (section))) {
2N/A
2N/A if (cmp_class_name(section, PICL_CLASS_SECTION) != FRU_SUCCESS)
2N/A continue;
2N/A
2N/A if ((status = picl_get_propval_by_name(section,
2N/A PICL_PROP_NUM_SEGMENTS,
2N/A &num_segments,
2N/A sizeof (num_segments)))
2N/A == PICL_PROPNOTFOUND) {
2N/A continue;
2N/A } else if (status != PICL_SUCCESS) {
2N/A saved_status = map_plugin_err(status);
2N/A continue;
2N/A } else if (num_segments == 0) {
2N/A continue;
2N/A }
2N/A
2N/A /* process each segment */
2N/A for (status = picl_get_propval_by_name(section,
2N/A PICL_PROP_CHILD,
2N/A &segment,
2N/A sizeof (segment));
2N/A status == PICL_SUCCESS;
2N/A status = picl_get_propval_by_name(segment,
2N/A PICL_PROP_PEER,
2N/A &segment,
2N/A sizeof (segment))) {
2N/A
2N/A if (cmp_class_name(segment, PICL_CLASS_SEGMENT)
2N/A != FRU_SUCCESS) continue;
2N/A
2N/A if ((status = function(PICLHDL_TO_TREESEGHDL(segment),
2N/A args))
2N/A != FRU_SUCCESS) return (status);
2N/A }
2N/A
2N/A if (status != PICL_PROPNOTFOUND)
2N/A saved_status = map_plugin_err(status);
2N/A }
2N/A
2N/A if (status != PICL_PROPNOTFOUND)
2N/A saved_status = map_plugin_err(status);
2N/A
2N/A return (saved_status);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_get_segment_name(fru_treeseghdl_t segment, char **name)
2N/A{
2N/A char *propval;
2N/A
2N/A int status;
2N/A
2N/A picl_prophdl_t proph = 0;
2N/A
2N/A picl_propinfo_t propinfo;
2N/A
2N/A
2N/A if ((status = picl_get_propinfo_by_name(TREESEGHDL_TO_PICLHDL(segment),
2N/A PICL_PROP_NAME, &propinfo, &proph))
2N/A != PICL_SUCCESS)
2N/A return (map_plugin_err(status));
2N/A
2N/A if (propinfo.size == 0)
2N/A return (FRU_INVALDATASIZE);
2N/A
2N/A if ((propval = malloc(propinfo.size)) == NULL)
2N/A return (FRU_NOSPACE);
2N/A
2N/A if ((status = picl_get_propval(proph, propval, propinfo.size))
2N/A != PICL_SUCCESS) {
2N/A free(propval);
2N/A return (map_plugin_err(status));
2N/A }
2N/A
2N/A *name = propval;
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Afpt_for_each_packet(fru_treeseghdl_t treesegment,
2N/A int (*function)(fru_tag_t *tag, uint8_t *payload,
2N/A size_t length,
2N/A void *args),
2N/A void *args)
2N/A{
2N/A int status;
2N/A
2N/A uint8_t *payload;
2N/A
2N/A picl_nodehdl_t segment = TREESEGHDL_TO_PICLHDL(treesegment);
2N/A
2N/A picl_prophdl_t packet, payloadh = 0;
2N/A
2N/A picl_propinfo_t propinfo;
2N/A
2N/A fru_segdesc_t descriptor;
2N/A
2N/A fru_tag_t tag;
2N/A
2N/A
2N/A if ((status = picl_get_propval_by_name(segment, PICL_PROP_DESCRIPTOR,
2N/A &descriptor,
2N/A sizeof (descriptor)))
2N/A != PICL_SUCCESS) return (map_plugin_err(status));
2N/A
2N/A if (descriptor.field.opaque)
2N/A return (FRU_SUCCESS);
2N/A
2N/A if (descriptor.field.encrypted && (encrypt_func == NULL))
2N/A return (FRU_SUCCESS);
2N/A
2N/A if ((status = picl_get_propval_by_name(segment, PICL_PROP_PACKET_TABLE,
2N/A &packet, sizeof (packet)))
2N/A == PICL_PROPNOTFOUND)
2N/A return (FRU_SUCCESS);
2N/A else if (status != PICL_SUCCESS)
2N/A return (map_plugin_err(status));
2N/A
2N/A while ((status = picl_get_next_by_col(packet, &packet))
2N/A == PICL_SUCCESS) {
2N/A if (((status = picl_get_propval(packet, &tag, sizeof (tag)))
2N/A != PICL_SUCCESS) ||
2N/A ((status = picl_get_next_by_row(packet, &payloadh))
2N/A != PICL_SUCCESS) ||
2N/A ((status = picl_get_propinfo(payloadh, &propinfo))
2N/A != PICL_SUCCESS))
2N/A return (map_plugin_err(status));
2N/A
2N/A if (propinfo.size > 0) {
2N/A payload = alloca(propinfo.size);
2N/A if ((status = picl_get_propval(payloadh, payload,
2N/A propinfo.size))
2N/A != PICL_SUCCESS) return (map_plugin_err(status));
2N/A } else {
2N/A payload = NULL;
2N/A }
2N/A
2N/A if ((descriptor.field.encrypted) &&
2N/A ((status = encrypt_func(FRU_DECRYPT, payload,
2N/A propinfo.size))
2N/A != FRU_SUCCESS)) return status;
2N/A
2N/A if ((status = function(&tag, payload, propinfo.size, args))
2N/A != FRU_SUCCESS) return (status);
2N/A }
2N/A
2N/A if (status == PICL_ENDOFLIST)
2N/A return (FRU_SUCCESS);
2N/A else
2N/A return (map_plugin_err(status));
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* ARGSUSED0 */
2N/Astatic fru_errno_t
2N/Ainitialize(int argc, char **argv)
2N/A{
2N/A /* LINTED */
2N/A int rc = PICL_SUCCESS;
2N/A if ((rc = picl_initialize()) != PICL_SUCCESS) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/Astatic fru_errno_t
2N/Ashutdown(void)
2N/A{
2N/A if (picl_shutdown() != PICL_SUCCESS) {
2N/A return (FRU_FAILURE);
2N/A }
2N/A return (FRU_SUCCESS);
2N/A}
2N/A
2N/A/* ========================================================================= */
2N/A/* object for libfru to link to */
2N/Afru_datasource_t data_source =
2N/A{
2N/A LIBFRU_DS_VER,
2N/A initialize,
2N/A shutdown,
2N/A fpt_get_root,
2N/A fpt_get_child,
2N/A fpt_get_peer,
2N/A fpt_get_parent,
2N/A fpt_get_name_from_hdl,
2N/A fpt_get_node_type,
2N/A fpt_get_seg_list,
2N/A fpt_get_seg_def,
2N/A fpt_add_seg,
2N/A fpt_delete_seg,
2N/A fpt_for_each_segment,
2N/A fpt_get_segment_name,
2N/A fpt_add_tag_to_seg,
2N/A fpt_get_tag_list,
2N/A fpt_get_tag_data,
2N/A fpt_set_tag_data,
2N/A fpt_delete_tag,
2N/A fpt_for_each_packet
2N/A};