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
5af3025fdSdt  * Common Development and Distribution License (the "License").
6af3025fdSdt  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22af3025fdSdt  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <limits.h>
277c478bd9Sstevel@tonic-gate #include <alloca.h>
287c478bd9Sstevel@tonic-gate #include "fru_access_impl.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma init(initialize_fruaccess)	/* .init section */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate static	hash_obj_t	*hash_table[TABLE_SIZE];
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * seeprom is the driver_name for the SEEPROM device drivers in excalibur
367c478bd9Sstevel@tonic-gate  * Define the devfsadm command to load the seeprom drivers if open fails.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static	char	devfsadm_cmd[] = "/usr/sbin/devfsadm -i seeprom";
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /* this routine initialize the hash table. */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static void
initialize_fruaccess(void)447c478bd9Sstevel@tonic-gate initialize_fruaccess(void)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	int	count;
477c478bd9Sstevel@tonic-gate 	for (count = 0; count < TABLE_SIZE; count++) {
487c478bd9Sstevel@tonic-gate 		hash_table[count] = NULL;
497c478bd9Sstevel@tonic-gate 	}
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * called to lookup hash object for specified handle in the hash table.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static hash_obj_t *
lookup_handle_object(handle_t handle,int object_type)587c478bd9Sstevel@tonic-gate lookup_handle_object(handle_t	handle, int object_type)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	handle_t	index_to_hash;
617c478bd9Sstevel@tonic-gate 	hash_obj_t	*first_hash_obj;
627c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash_obj;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	index_to_hash	= (handle % TABLE_SIZE);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	first_hash_obj = hash_table[index_to_hash];
677c478bd9Sstevel@tonic-gate 	for (next_hash_obj = first_hash_obj; next_hash_obj != NULL;
687c478bd9Sstevel@tonic-gate 	    next_hash_obj = next_hash_obj->next) {
697c478bd9Sstevel@tonic-gate 		if ((handle == next_hash_obj->obj_hdl) &&
707c478bd9Sstevel@tonic-gate 		    (object_type == next_hash_obj->object_type)) {
717c478bd9Sstevel@tonic-gate 			return (next_hash_obj);
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	return (NULL);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* called to allocate container hash object */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static hash_obj_t *
create_container_hash_object(void)807c478bd9Sstevel@tonic-gate create_container_hash_object(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
837c478bd9Sstevel@tonic-gate 	container_obj_t		*cont_obj;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	cont_obj	= malloc(sizeof (container_obj_t));
867c478bd9Sstevel@tonic-gate 	if (cont_obj == NULL) {
877c478bd9Sstevel@tonic-gate 		return (NULL);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
917c478bd9Sstevel@tonic-gate 	if (hash_obj == NULL) {
927c478bd9Sstevel@tonic-gate 		free(cont_obj);
937c478bd9Sstevel@tonic-gate 		return (NULL);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	cont_obj->sec_obj_list	= NULL;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	hash_obj->object_type	= CONTAINER_TYPE;
997c478bd9Sstevel@tonic-gate 	hash_obj->u.cont_obj	= cont_obj;
1007c478bd9Sstevel@tonic-gate 	hash_obj->next	= NULL;
1017c478bd9Sstevel@tonic-gate 	hash_obj->prev	= NULL;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return (hash_obj);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* called to allocate section hash object */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static hash_obj_t *
create_section_hash_object(void)1097c478bd9Sstevel@tonic-gate create_section_hash_object(void)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
1127c478bd9Sstevel@tonic-gate 	section_obj_t		*sec_obj;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	sec_obj	= malloc(sizeof (section_obj_t));
1157c478bd9Sstevel@tonic-gate 	if (sec_obj == NULL) {
1167c478bd9Sstevel@tonic-gate 		return (NULL);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
1207c478bd9Sstevel@tonic-gate 	if (hash_obj == NULL) {
1217c478bd9Sstevel@tonic-gate 		free(sec_obj);
1227c478bd9Sstevel@tonic-gate 		return (NULL);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	sec_obj->next		= NULL;
1267c478bd9Sstevel@tonic-gate 	sec_obj->seg_obj_list	= NULL;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	hash_obj->u.sec_obj	= sec_obj;
1297c478bd9Sstevel@tonic-gate 	hash_obj->object_type	= SECTION_TYPE;
1307c478bd9Sstevel@tonic-gate 	hash_obj->next		= NULL;
1317c478bd9Sstevel@tonic-gate 	hash_obj->prev		= NULL;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (hash_obj);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* called to allocate segment hash object */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static hash_obj_t *
create_segment_hash_object(void)1397c478bd9Sstevel@tonic-gate create_segment_hash_object(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
1427c478bd9Sstevel@tonic-gate 	segment_obj_t		*seg_obj;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	seg_obj	= malloc(sizeof (segment_obj_t));
1457c478bd9Sstevel@tonic-gate 	if (seg_obj == NULL) {
1467c478bd9Sstevel@tonic-gate 		return (NULL);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
1507c478bd9Sstevel@tonic-gate 	if (hash_obj == NULL) {
1517c478bd9Sstevel@tonic-gate 		free(seg_obj);
1527c478bd9Sstevel@tonic-gate 		return (NULL);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	seg_obj->next		= NULL;
1567c478bd9Sstevel@tonic-gate 	seg_obj->pkt_obj_list	= NULL;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	hash_obj->object_type	= SEGMENT_TYPE;
1597c478bd9Sstevel@tonic-gate 	hash_obj->u.seg_obj	= seg_obj;
1607c478bd9Sstevel@tonic-gate 	hash_obj->next		= NULL;
1617c478bd9Sstevel@tonic-gate 	hash_obj->prev		= NULL;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return (hash_obj);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /* called to allocate packet hash object */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static hash_obj_t *
create_packet_hash_object(void)1697c478bd9Sstevel@tonic-gate create_packet_hash_object(void)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
1727c478bd9Sstevel@tonic-gate 	packet_obj_t		*pkt_obj;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	pkt_obj	= malloc(sizeof (packet_obj_t));
1757c478bd9Sstevel@tonic-gate 	if (pkt_obj == NULL) {
1767c478bd9Sstevel@tonic-gate 		return (NULL);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	hash_obj	= malloc(sizeof (hash_obj_t));
1807c478bd9Sstevel@tonic-gate 	if (hash_obj == NULL) {
1817c478bd9Sstevel@tonic-gate 		free(pkt_obj);
1827c478bd9Sstevel@tonic-gate 		return (NULL);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	pkt_obj->next		= NULL;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	hash_obj->object_type	= PACKET_TYPE;
1887c478bd9Sstevel@tonic-gate 	hash_obj->u.pkt_obj	= pkt_obj;
1897c478bd9Sstevel@tonic-gate 	hash_obj->next		= NULL;
1907c478bd9Sstevel@tonic-gate 	hash_obj->prev		= NULL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	return (hash_obj);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* called to add allocated hash object into the hash table */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static void
add_hashobject_to_hashtable(hash_obj_t * hash_obj)1987c478bd9Sstevel@tonic-gate add_hashobject_to_hashtable(hash_obj_t *hash_obj)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	handle_t		index_to_hash;
2017c478bd9Sstevel@tonic-gate 	static	uint64_t	handle_count	= 0;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	hash_obj->obj_hdl = ++handle_count;	/* store the handle */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* where to add ? */
2067c478bd9Sstevel@tonic-gate 	index_to_hash	= ((hash_obj->obj_hdl) % TABLE_SIZE);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	hash_obj->next	= hash_table[index_to_hash];
2097c478bd9Sstevel@tonic-gate 	hash_table[index_to_hash] = hash_obj;	/* hash obj. added */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (hash_obj->next != NULL) {
2127c478bd9Sstevel@tonic-gate 		hash_obj->next->prev = hash_obj;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /* called to add section object list into the section list */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate static void
add_to_sec_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)2197c478bd9Sstevel@tonic-gate add_to_sec_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	child_obj->u.sec_obj->cont_hdl = parent_obj->obj_hdl;
2247c478bd9Sstevel@tonic-gate 	if (parent_obj->u.cont_obj->sec_obj_list == NULL) {
2257c478bd9Sstevel@tonic-gate 		parent_obj->u.cont_obj->sec_obj_list = child_obj;
2267c478bd9Sstevel@tonic-gate 		return;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	for (next_hash = parent_obj->u.cont_obj->sec_obj_list;
2307c478bd9Sstevel@tonic-gate 	    next_hash->u.sec_obj->next != NULL;
2317c478bd9Sstevel@tonic-gate 	    next_hash = next_hash->u.sec_obj->next) {
2327c478bd9Sstevel@tonic-gate 		;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	next_hash->u.sec_obj->next	= child_obj;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /* called to add segment object list into segment list */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static void
add_to_seg_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)2417c478bd9Sstevel@tonic-gate add_to_seg_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	child_obj->u.seg_obj->section_hdl = parent_obj->obj_hdl;
2467c478bd9Sstevel@tonic-gate 	if (parent_obj->u.sec_obj->seg_obj_list == NULL) {
2477c478bd9Sstevel@tonic-gate 		parent_obj->u.sec_obj->seg_obj_list = child_obj;
2487c478bd9Sstevel@tonic-gate 		return;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	for (next_hash = parent_obj->u.sec_obj->seg_obj_list;
2527c478bd9Sstevel@tonic-gate 	    next_hash->u.seg_obj->next != NULL;
2537c478bd9Sstevel@tonic-gate 	    next_hash = next_hash->u.seg_obj->next) {
2547c478bd9Sstevel@tonic-gate 		;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	next_hash->u.seg_obj->next	= child_obj;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /* called to add packet object list into packet list */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static void
add_to_pkt_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)2637c478bd9Sstevel@tonic-gate add_to_pkt_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* add the packet object in the end of list */
2687c478bd9Sstevel@tonic-gate 	child_obj->u.pkt_obj->segment_hdl = parent_obj->obj_hdl;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (parent_obj->u.seg_obj->pkt_obj_list == NULL) {
2717c478bd9Sstevel@tonic-gate 		parent_obj->u.seg_obj->pkt_obj_list = child_obj;
2727c478bd9Sstevel@tonic-gate 		return;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	for (next_hash = parent_obj->u.seg_obj->pkt_obj_list;
2767c478bd9Sstevel@tonic-gate 	    next_hash->u.pkt_obj->next != NULL;
2777c478bd9Sstevel@tonic-gate 	    next_hash = next_hash->u.pkt_obj->next) {
2787c478bd9Sstevel@tonic-gate 		;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	next_hash->u.pkt_obj->next = child_obj;
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static void
copy_segment_layout(segment_t * seghdr,void * layout)2857c478bd9Sstevel@tonic-gate copy_segment_layout(segment_t	*seghdr, void	*layout)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	segment_layout_t	*seg_layout;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	seg_layout	= (segment_layout_t *)layout;
2907c478bd9Sstevel@tonic-gate 	(void) memcpy(seghdr->name, &seg_layout->name, SEG_NAME_LEN);
2917c478bd9Sstevel@tonic-gate 	seghdr->descriptor = GET_SEGMENT_DESCRIPTOR;
2927c478bd9Sstevel@tonic-gate 	seghdr->offset	= seg_layout->offset;
2937c478bd9Sstevel@tonic-gate 	seghdr->length	= seg_layout->length;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static hash_obj_t *
get_container_hash_object(int object_type,handle_t handle)2977c478bd9Sstevel@tonic-gate get_container_hash_object(int	object_type, handle_t	handle)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	hash_obj_t	*hash_obj;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	switch (object_type) {
3027c478bd9Sstevel@tonic-gate 	case	CONTAINER_TYPE	:
3037c478bd9Sstevel@tonic-gate 		break;
3047c478bd9Sstevel@tonic-gate 	case	SECTION_TYPE	:
3057c478bd9Sstevel@tonic-gate 		hash_obj = lookup_handle_object(handle, CONTAINER_TYPE);
3067c478bd9Sstevel@tonic-gate 		if (hash_obj == NULL) {
3077c478bd9Sstevel@tonic-gate 			return (NULL);
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 		break;
3107c478bd9Sstevel@tonic-gate 	case	SEGMENT_TYPE	:
3117c478bd9Sstevel@tonic-gate 		hash_obj = lookup_handle_object(handle, SECTION_TYPE);
3127c478bd9Sstevel@tonic-gate 		if (hash_obj == NULL) {
3137c478bd9Sstevel@tonic-gate 			return (NULL);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		hash_obj = lookup_handle_object(hash_obj->u.sec_obj->cont_hdl,
3167c478bd9Sstevel@tonic-gate 		    CONTAINER_TYPE);
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 	case	PACKET_TYPE	:
3197c478bd9Sstevel@tonic-gate 		break;
3207c478bd9Sstevel@tonic-gate 	default	:
3217c478bd9Sstevel@tonic-gate 		return (NULL);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	return (hash_obj);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static void
sort_offsettbl(int segcnt,seg_info_t * offset_tbl)3287c478bd9Sstevel@tonic-gate sort_offsettbl(int	segcnt, seg_info_t	*offset_tbl)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	int		cntx;
3317c478bd9Sstevel@tonic-gate 	int		cnty;
3327c478bd9Sstevel@tonic-gate 	seg_info_t	tmp;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	for (cntx = 0; cntx < segcnt+2; cntx++) {
3357c478bd9Sstevel@tonic-gate 		for (cnty = cntx+1; cnty < segcnt + 2; cnty++) {
3367c478bd9Sstevel@tonic-gate 			if (offset_tbl[cntx].offset >
3377c478bd9Sstevel@tonic-gate 			    offset_tbl[cnty].offset) {
3387c478bd9Sstevel@tonic-gate 				(void) memcpy(&tmp, &offset_tbl[cnty],
3397c478bd9Sstevel@tonic-gate 				    sizeof (seg_info_t));
3407c478bd9Sstevel@tonic-gate 				(void) memcpy(&offset_tbl[cnty],
3417c478bd9Sstevel@tonic-gate 				    &offset_tbl[cntx], sizeof (seg_info_t));
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 				(void) memcpy(&offset_tbl[cntx], &tmp,
3447c478bd9Sstevel@tonic-gate 				    sizeof (seg_info_t));
3457c478bd9Sstevel@tonic-gate 			}
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Description : move_segment_data() reads the segment data and writes it
3527c478bd9Sstevel@tonic-gate  *      back to the new segment offset.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate static void
move_segment_data(void * seghdr,int newoffset,container_hdl_t contfd)3567c478bd9Sstevel@tonic-gate move_segment_data(void *seghdr, int newoffset, container_hdl_t contfd)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	int			ret;
3597c478bd9Sstevel@tonic-gate 	char			*buffer;
3607c478bd9Sstevel@tonic-gate 	segment_layout_t	*segment;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	segment	= (segment_layout_t *)seghdr;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	buffer = alloca(segment->length);
3657c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
3667c478bd9Sstevel@tonic-gate 		return;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	ret = pread(contfd, buffer, segment->length, segment->offset);
3707c478bd9Sstevel@tonic-gate 	if (ret != segment->length) {
3717c478bd9Sstevel@tonic-gate 		return;
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	segment->offset = newoffset;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	ret = pwrite(contfd, buffer, segment->length, segment->offset);
3777c478bd9Sstevel@tonic-gate 	if (ret != segment->length) {
3787c478bd9Sstevel@tonic-gate 		return;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Description : pack_segment_data() moves the segment data if there is
3847c478bd9Sstevel@tonic-gate  *              a hole between two segments.
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static void
pack_segment_data(char * seghdr,int segcnt,container_hdl_t contfd,seg_info_t * offset_tbl)3887c478bd9Sstevel@tonic-gate pack_segment_data(char *seghdr, int segcnt, container_hdl_t contfd,
3897c478bd9Sstevel@tonic-gate     seg_info_t *offset_tbl)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	int	cnt;
3927c478bd9Sstevel@tonic-gate 	int	diff;
3937c478bd9Sstevel@tonic-gate 	int	newoffset;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	for (cnt = segcnt + 1; cnt > 0; cnt--) {
3967c478bd9Sstevel@tonic-gate 		if (!offset_tbl[cnt - 1].fixed) {
3977c478bd9Sstevel@tonic-gate 			if (offset_tbl[cnt].offset -
3987c478bd9Sstevel@tonic-gate 			    (offset_tbl[cnt -1 ].offset +
3997c478bd9Sstevel@tonic-gate 			    offset_tbl[cnt - 1].length) > 0) {
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 				diff = offset_tbl[cnt].offset -
4027c478bd9Sstevel@tonic-gate 				    (offset_tbl[cnt - 1].offset +
4037c478bd9Sstevel@tonic-gate 				    offset_tbl[cnt - 1].length);
4047c478bd9Sstevel@tonic-gate 				newoffset = offset_tbl[cnt - 1].offset + diff;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 				move_segment_data(seghdr, newoffset, contfd);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 				offset_tbl[cnt - 1].offset = newoffset;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 				sort_offsettbl(segcnt, offset_tbl);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Description : build_offset_tbl() builds the offset table by reading all the
4187c478bd9Sstevel@tonic-gate  *              segment header. it makes two more entry into the table one for
4197c478bd9Sstevel@tonic-gate  *              section size and another with start of the section after the
4207c478bd9Sstevel@tonic-gate  *              segment header.
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate static int
build_offset_tbl(void * seghdr,int segcnt,int secsize,seg_info_t * offset_tbl)4247c478bd9Sstevel@tonic-gate build_offset_tbl(void   *seghdr, int segcnt, int secsize,
4257c478bd9Sstevel@tonic-gate     seg_info_t *offset_tbl)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	int			cnt;
4287c478bd9Sstevel@tonic-gate 	fru_segdesc_t		segdesc;
4297c478bd9Sstevel@tonic-gate 	segment_layout_t	*segment;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < segcnt; cnt++) {
4327c478bd9Sstevel@tonic-gate 		segment	= (segment_layout_t *)(seghdr) + cnt;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		(void) memcpy(&segdesc, &segment->descriptor,
4357c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t));
4367c478bd9Sstevel@tonic-gate 		offset_tbl[cnt].segnum = cnt;
4377c478bd9Sstevel@tonic-gate 		offset_tbl[cnt].offset = segment->offset;
4387c478bd9Sstevel@tonic-gate 		offset_tbl[cnt].length = segment->length;
4397c478bd9Sstevel@tonic-gate 		offset_tbl[cnt].fixed = segdesc.field.fixed;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/* upper boundary of segment area (lower address bytes) */
4437c478bd9Sstevel@tonic-gate 	offset_tbl[cnt].segnum = -1;
4447c478bd9Sstevel@tonic-gate 	offset_tbl[cnt].offset = sizeof (section_layout_t) +
4457c478bd9Sstevel@tonic-gate 	    ((cnt + 1) * sizeof (segment_layout_t));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	offset_tbl[cnt].length = 0;
4487c478bd9Sstevel@tonic-gate 	offset_tbl[cnt].fixed  = 1;
4497c478bd9Sstevel@tonic-gate 	/* lower boundary of segment area (higher address bytes) */
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	offset_tbl[cnt+1].segnum = -1;
4527c478bd9Sstevel@tonic-gate 	offset_tbl[cnt+1].offset = secsize;
4537c478bd9Sstevel@tonic-gate 	offset_tbl[cnt+1].length = 0;
4547c478bd9Sstevel@tonic-gate 	offset_tbl[cnt+1].fixed = 1;
4557c478bd9Sstevel@tonic-gate 	return (0);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static int
hole_discovery(int bytes,int segcnt,int * totsize,seg_info_t * offset_tbl)4597c478bd9Sstevel@tonic-gate hole_discovery(int bytes, int segcnt, int *totsize, seg_info_t *offset_tbl)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int cnt = 0;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	*totsize = 0;
4647c478bd9Sstevel@tonic-gate 	for (cnt = segcnt + 1; cnt > 0; cnt--) {
4657c478bd9Sstevel@tonic-gate 		if (bytes <= offset_tbl[cnt].offset -
4667c478bd9Sstevel@tonic-gate 		    (offset_tbl[cnt - 1].offset +
4677c478bd9Sstevel@tonic-gate 		    offset_tbl[cnt - 1].length)) {
4687c478bd9Sstevel@tonic-gate 			return (offset_tbl[cnt].offset - bytes);
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		*totsize += offset_tbl[cnt].offset -
4727c478bd9Sstevel@tonic-gate 		    (offset_tbl[cnt - 1].offset + offset_tbl[cnt - 1].length);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	return (0);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * Description : segment_hdr_present() verify space for new segment header to
4807c478bd9Sstevel@tonic-gate  *              be added.
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static int
segment_hdr_present(int segoffset,int size,seg_info_t * offset_tbl)4847c478bd9Sstevel@tonic-gate segment_hdr_present(int segoffset, int size, seg_info_t *offset_tbl)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	if ((segoffset + size) <= offset_tbl[0].offset)
4877c478bd9Sstevel@tonic-gate 		return (0);
4887c478bd9Sstevel@tonic-gate 	else
4897c478bd9Sstevel@tonic-gate 		return (-1);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * Description : find_offset() is called from fru_add_segment routine to find
4947c478bd9Sstevel@tonic-gate  *              a valid offset.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate static int
find_offset(char * seghdr,int segcnt,int secsize,int * sectionoffset,int segsize,int fix,container_hdl_t contfd)4987c478bd9Sstevel@tonic-gate find_offset(char *seghdr, int segcnt, int secsize, int *sectionoffset,
4997c478bd9Sstevel@tonic-gate     int segsize, int fix, container_hdl_t contfd)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	int		ret;
5027c478bd9Sstevel@tonic-gate 	int		newoffset;
5037c478bd9Sstevel@tonic-gate 	int		totsize = 0;
5047c478bd9Sstevel@tonic-gate 	seg_info_t	*offset_tbl;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (segcnt == 0) {
5077c478bd9Sstevel@tonic-gate 		if (!fix) {	/* if not fixed segment */
5087c478bd9Sstevel@tonic-gate 			*sectionoffset = secsize - segsize;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 		return (0);
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * two extra segment info structure are allocated for start of segment
5157c478bd9Sstevel@tonic-gate 	 * and other end of segment. first segment offset is first available
5167c478bd9Sstevel@tonic-gate 	 * space and length is 0. second segment offset is is segment length and
5177c478bd9Sstevel@tonic-gate 	 * offset is 0. build_offset_tbl() explains how upper boundary and lower
5187c478bd9Sstevel@tonic-gate 	 * boudary segment area are initialized in seg_info_t table.
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	offset_tbl    = malloc((segcnt + 2) * sizeof (seg_info_t));
5227c478bd9Sstevel@tonic-gate 	if (offset_tbl == NULL) {
5237c478bd9Sstevel@tonic-gate 		return (-1);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/* read all the segment header to make offset table */
5277c478bd9Sstevel@tonic-gate 	ret = build_offset_tbl(seghdr, segcnt, secsize, offset_tbl);
5287c478bd9Sstevel@tonic-gate 	if (ret != 0) {
5297c478bd9Sstevel@tonic-gate 		free(offset_tbl);
5307c478bd9Sstevel@tonic-gate 		return (-1);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/* sort the table */
5347c478bd9Sstevel@tonic-gate 	sort_offsettbl(segcnt, offset_tbl);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/* new segment header offset */
5377c478bd9Sstevel@tonic-gate 	newoffset = sizeof (section_layout_t) + segcnt *
5387c478bd9Sstevel@tonic-gate 	    sizeof (segment_layout_t);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* do? new segment header overlap any existing data */
5417c478bd9Sstevel@tonic-gate 	ret = segment_hdr_present(newoffset, sizeof (segment_layout_t),
5427c478bd9Sstevel@tonic-gate 	    offset_tbl);
5437c478bd9Sstevel@tonic-gate 	if (ret != 0) { /* make room for new segment if possible */
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* look for hole in order to move segment data */
5467c478bd9Sstevel@tonic-gate 		if (offset_tbl[0].fixed == SEGMENT_FIXED) { /* fixed segment */
5477c478bd9Sstevel@tonic-gate 			free(offset_tbl);
5487c478bd9Sstevel@tonic-gate 			return (-1);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		newoffset = hole_discovery(offset_tbl[0].length, segcnt,
5527c478bd9Sstevel@tonic-gate 		    &totsize, offset_tbl);
5537c478bd9Sstevel@tonic-gate 		if (newoffset != 0) { /* found new offset */
5547c478bd9Sstevel@tonic-gate 				/* now new offset */
5557c478bd9Sstevel@tonic-gate 			offset_tbl[0].offset = newoffset;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			/* move the segment data */
5587c478bd9Sstevel@tonic-gate 			move_segment_data(seghdr, newoffset, contfd);
5597c478bd9Sstevel@tonic-gate 			/* again sort the offset table */
5607c478bd9Sstevel@tonic-gate 			sort_offsettbl(segcnt, offset_tbl);
5617c478bd9Sstevel@tonic-gate 		} else {
5627c478bd9Sstevel@tonic-gate 			/* pack the existing hole */
5637c478bd9Sstevel@tonic-gate 			if (totsize > offset_tbl[0].length) {
5647c478bd9Sstevel@tonic-gate 				pack_segment_data(seghdr, segcnt, contfd,
5657c478bd9Sstevel@tonic-gate 				    offset_tbl);
5667c478bd9Sstevel@tonic-gate 			} else {
5677c478bd9Sstevel@tonic-gate 				free(offset_tbl);
5687c478bd9Sstevel@tonic-gate 				return (-1);
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	totsize = 0;
5747c478bd9Sstevel@tonic-gate 	newoffset = hole_discovery(segsize, segcnt, &totsize, offset_tbl);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (newoffset == 0) { /* No hole found */
5777c478bd9Sstevel@tonic-gate 		if (totsize >= segsize) {
5787c478bd9Sstevel@tonic-gate 			pack_segment_data(seghdr, segcnt, contfd, offset_tbl);
5797c478bd9Sstevel@tonic-gate 			newoffset = hole_discovery(segsize, segcnt, &totsize,
5807c478bd9Sstevel@tonic-gate 			    offset_tbl);
5817c478bd9Sstevel@tonic-gate 			if (newoffset != 0) {
5827c478bd9Sstevel@tonic-gate 				*sectionoffset = newoffset;
5837c478bd9Sstevel@tonic-gate 				free(offset_tbl);
5847c478bd9Sstevel@tonic-gate 				return (0);
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	} else {
5887c478bd9Sstevel@tonic-gate 		*sectionoffset = newoffset;
5897c478bd9Sstevel@tonic-gate 		free(offset_tbl);
5907c478bd9Sstevel@tonic-gate 		return (0);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	free(offset_tbl);
5937c478bd9Sstevel@tonic-gate 	return (-1);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate static char *
tokenizer(char * buf,char * separator,char ** nextBuf,char * matched)5977c478bd9Sstevel@tonic-gate tokenizer(char *buf, char *separator, char **nextBuf, char *matched)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	int i = 0;
6007c478bd9Sstevel@tonic-gate 	int j = 0;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	for (i = 0; buf[i] != '\0'; i++) {
6037c478bd9Sstevel@tonic-gate 		for (j = 0; j < strlen(separator); j++) {
6047c478bd9Sstevel@tonic-gate 			if (buf[i] == separator[j]) {
6057c478bd9Sstevel@tonic-gate 				buf[i] = '\0';
6067c478bd9Sstevel@tonic-gate 				*nextBuf = &(buf[i+1]);
6077c478bd9Sstevel@tonic-gate 				*matched = separator[j];
6087c478bd9Sstevel@tonic-gate 				return (buf);
6097c478bd9Sstevel@tonic-gate 			}
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	*nextBuf = buf;
6147c478bd9Sstevel@tonic-gate 	*matched = '\0';
6157c478bd9Sstevel@tonic-gate 	return (NULL);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static int
get_container_info(const char * def_file,const char * cont_desc_str,container_info_t * cont_info)6197c478bd9Sstevel@tonic-gate get_container_info(const char *def_file, const char *cont_desc_str,
6207c478bd9Sstevel@tonic-gate     container_info_t *cont_info)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	char	*item;
6237c478bd9Sstevel@tonic-gate 	char	*token;
6247c478bd9Sstevel@tonic-gate 	char	*field;
6257c478bd9Sstevel@tonic-gate 	char	matched;
6267c478bd9Sstevel@tonic-gate 	char	buf[1024];
6277c478bd9Sstevel@tonic-gate 	int	foundIt = 0;
6287c478bd9Sstevel@tonic-gate 	int	ro_tok;
6297c478bd9Sstevel@tonic-gate 	int	index;
6307c478bd9Sstevel@tonic-gate 	FILE	*file = fopen(def_file, "r");
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (file == NULL)
6337c478bd9Sstevel@tonic-gate 		return (-1);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	cont_info->num_sections = 0;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), file) != NULL) {
6387c478bd9Sstevel@tonic-gate 		/* ignore all comments */
6397c478bd9Sstevel@tonic-gate 		token = tokenizer(buf, "#", &field, &matched);
6407c478bd9Sstevel@tonic-gate 		/* find the names */
6417c478bd9Sstevel@tonic-gate 		token = tokenizer(buf, ":", &field, &matched);
642*93d26c05SToomas Soome 		if (token != NULL) {
6437c478bd9Sstevel@tonic-gate 			token = tokenizer(token, "|", &item, &matched);
644*93d26c05SToomas Soome 			while (token != NULL) {
6457c478bd9Sstevel@tonic-gate 				if (strcmp(token, cont_desc_str) == 0) {
6467c478bd9Sstevel@tonic-gate 					foundIt = 1;
6477c478bd9Sstevel@tonic-gate 					goto found;
6487c478bd9Sstevel@tonic-gate 				}
6497c478bd9Sstevel@tonic-gate 				token = tokenizer(item, "|", &item, &matched);
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 			/* check the last remaining item */
652*93d26c05SToomas Soome 			if ((item != NULL) &&
6537c478bd9Sstevel@tonic-gate 			    (strcmp(item, cont_desc_str) == 0)) {
6547c478bd9Sstevel@tonic-gate 				foundIt = 1;
6557c478bd9Sstevel@tonic-gate 				goto found;
6567c478bd9Sstevel@tonic-gate 			}
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate found :
6617c478bd9Sstevel@tonic-gate 	if (foundIt == 1) {
6627c478bd9Sstevel@tonic-gate 		token = tokenizer(field, ":", &field, &matched);
663*93d26c05SToomas Soome 		if (token == NULL) {
6647c478bd9Sstevel@tonic-gate 			(void) fclose(file);
6657c478bd9Sstevel@tonic-gate 			return (-1);
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		cont_info->header_ver = (headerrev_t)atoi(token);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		token = tokenizer(field, ":\n", &field, &matched);
670*93d26c05SToomas Soome 		while (token != NULL) {
6717c478bd9Sstevel@tonic-gate 			token = tokenizer(token, ",", &item, &matched);
672*93d26c05SToomas Soome 			if (token == NULL) {
6737c478bd9Sstevel@tonic-gate 				(void) fclose(file);
6747c478bd9Sstevel@tonic-gate 				return (-1);
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			ro_tok = atoi(token);
6777c478bd9Sstevel@tonic-gate 			index = cont_info->num_sections;
6787c478bd9Sstevel@tonic-gate 			cont_info->section_info[index].encoding = ENC_STANDARD;
6797c478bd9Sstevel@tonic-gate 			if (ro_tok == 1) {
6807c478bd9Sstevel@tonic-gate 				cont_info->section_info[index].description.
6817c478bd9Sstevel@tonic-gate 				    field.read_only = 1;
6827c478bd9Sstevel@tonic-gate 			} else if (ro_tok == 0) {
6837c478bd9Sstevel@tonic-gate 				cont_info->section_info[index].description.
6847c478bd9Sstevel@tonic-gate 				    field.read_only = 0;
6857c478bd9Sstevel@tonic-gate 			} else if (ro_tok == 2) {
6867c478bd9Sstevel@tonic-gate 				/*
6877c478bd9Sstevel@tonic-gate 				 * a value of 2 in the read-only token means
6887c478bd9Sstevel@tonic-gate 				 * that the data in this section needs
6897c478bd9Sstevel@tonic-gate 				 * re-interpreting
6907c478bd9Sstevel@tonic-gate 				 */
6917c478bd9Sstevel@tonic-gate 				cont_info->section_info[index].description.
6927c478bd9Sstevel@tonic-gate 				    field.read_only = 1;
6937c478bd9Sstevel@tonic-gate 			} else {
6947c478bd9Sstevel@tonic-gate 				(void) fclose(file);
6957c478bd9Sstevel@tonic-gate 				return (-1);
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			token = tokenizer(item, ",", &item, &matched);
699*93d26c05SToomas Soome 			if (token == NULL) {
7007c478bd9Sstevel@tonic-gate 				(void) fclose(file);
7017c478bd9Sstevel@tonic-gate 				return (-1);
7027c478bd9Sstevel@tonic-gate 			}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			cont_info->section_info[index].address = atoi(token);
7057c478bd9Sstevel@tonic-gate 			if (ro_tok == 2) {
7067c478bd9Sstevel@tonic-gate 				/*
7077c478bd9Sstevel@tonic-gate 				 * expect an extra parameter to define the
7087c478bd9Sstevel@tonic-gate 				 * data interpreter
7097c478bd9Sstevel@tonic-gate 				 */
7107c478bd9Sstevel@tonic-gate 				token = tokenizer(item, ",", &item, &matched);
711*93d26c05SToomas Soome 				if (token == NULL) {
7127c478bd9Sstevel@tonic-gate 					(void) fclose(file);
7137c478bd9Sstevel@tonic-gate 					return (-1);
7147c478bd9Sstevel@tonic-gate 				}
7157c478bd9Sstevel@tonic-gate 			}
716*93d26c05SToomas Soome 			if (*item == '\0') {
7177c478bd9Sstevel@tonic-gate 				(void) fclose(file);
7187c478bd9Sstevel@tonic-gate 				return (-1);
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 			cont_info->section_info[index].size =
7217c478bd9Sstevel@tonic-gate 			    ro_tok == 2 ? atoi(token) : atoi(item);
7227c478bd9Sstevel@tonic-gate 			if (ro_tok == 2) {
7237c478bd9Sstevel@tonic-gate 				if (strcmp(item, "SPD") == 0)
7247c478bd9Sstevel@tonic-gate 					cont_info->section_info[index].
7257c478bd9Sstevel@tonic-gate 					    encoding = ENC_SPD;
7267c478bd9Sstevel@tonic-gate 				else {
7277c478bd9Sstevel@tonic-gate 					(void) fclose(file);
7287c478bd9Sstevel@tonic-gate 					return (-1);
7297c478bd9Sstevel@tonic-gate 				}
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 			(cont_info->num_sections)++;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 			token = tokenizer(field, ":\n ", &field, &matched);
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 	(void) fclose(file);
7377c478bd9Sstevel@tonic-gate 	return (0);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * Description :fru_open_container() opens the container associated with a fru.
7427c478bd9Sstevel@tonic-gate  *              it's called by data plugin module before creating container
7437c478bd9Sstevel@tonic-gate  *              property.  it calls picltree library routine to get the
7447c478bd9Sstevel@tonic-gate  *              device path and driver binding name for the fru to get the
7457c478bd9Sstevel@tonic-gate  *              corresponding fru name that describe the fru layout.
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * Arguments   :picl_hdl_t      fru
7487c478bd9Sstevel@tonic-gate  *              A handle for PICL tree node of class "fru" representing the
7497c478bd9Sstevel@tonic-gate  *              FRU with the container to open.
7507c478bd9Sstevel@tonic-gate  *
7517c478bd9Sstevel@tonic-gate  * Return      :
7527c478bd9Sstevel@tonic-gate  *              On Success, a Positive integer container handle. is returned
7537c478bd9Sstevel@tonic-gate  *              for use in subsequent fru operations;on error, 0 is returned
7547c478bd9Sstevel@tonic-gate  *              and "errno" is set appropriately.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate container_hdl_t
fru_open_container(picl_nodehdl_t fruhdl)7587c478bd9Sstevel@tonic-gate fru_open_container(picl_nodehdl_t fruhdl)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	int			retval;
7617c478bd9Sstevel@tonic-gate 	int			count;
762c5805b0bSps 	int			device_fd;
763c5805b0bSps 	uchar_t			first_byte;
7647c478bd9Sstevel@tonic-gate 	char			*bname;
7657c478bd9Sstevel@tonic-gate 	char			devpath[PATH_MAX];
7667c478bd9Sstevel@tonic-gate 	char			nmbuf[SYS_NMLN];
7677c478bd9Sstevel@tonic-gate 	hash_obj_t		*cont_hash_obj;
7687c478bd9Sstevel@tonic-gate 	hash_obj_t		*sec_hash_obj;
7697c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		tmphdl;
7707c478bd9Sstevel@tonic-gate 	picl_prophdl_t		prophdl;
7717c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
7727c478bd9Sstevel@tonic-gate 	container_info_t	cont_info;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/* Get property handle of _seeprom_source under fru node */
7757c478bd9Sstevel@tonic-gate 	retval = ptree_get_propval_by_name(fruhdl, PICL_REFPROP_SEEPROM_SRC,
7767c478bd9Sstevel@tonic-gate 	    &tmphdl, sizeof (tmphdl));
7777c478bd9Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
778ada2da53SToomas Soome 		return (0);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/* Get the device path of the fru */
7827c478bd9Sstevel@tonic-gate 	retval = ptree_get_propval_by_name(tmphdl, PICL_PROP_DEVICEPATH,
7837c478bd9Sstevel@tonic-gate 	    devpath, PATH_MAX);
7847c478bd9Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
785ada2da53SToomas Soome 		return (0);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	retval = ptree_get_prop_by_name(tmphdl, PICL_PROP_BINDING_NAME,
7897c478bd9Sstevel@tonic-gate 	    &prophdl);
7907c478bd9Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
791ada2da53SToomas Soome 		return (0);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	retval = ptree_get_propinfo(prophdl, &propinfo);
7957c478bd9Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
796ada2da53SToomas Soome 		return (0);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	bname = alloca(propinfo.piclinfo.size);
8007c478bd9Sstevel@tonic-gate 	if (bname == NULL) {
801ada2da53SToomas Soome 		return (0);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/* get the driver binding name */
8057c478bd9Sstevel@tonic-gate 	retval = ptree_get_propval(prophdl, bname, propinfo.piclinfo.size);
8067c478bd9Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
807ada2da53SToomas Soome 		return (0);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	cont_hash_obj	= create_container_hash_object();
8117c478bd9Sstevel@tonic-gate 	if (cont_hash_obj == NULL) {
812ada2da53SToomas Soome 		return (0);
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	add_hashobject_to_hashtable(cont_hash_obj);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	(void) strlcpy(cont_hash_obj->u.cont_obj->device_pathname, devpath,
8187c478bd9Sstevel@tonic-gate 	    sizeof (devpath));
8197c478bd9Sstevel@tonic-gate 
820c5805b0bSps 	/* check for sun or non-sun type fru */
821c5805b0bSps 	if (strcmp(bname, "i2c-at34c02") == 0) {
822c5805b0bSps 		device_fd = open(devpath, O_RDONLY);
823c5805b0bSps 		if (device_fd < 0) {
824ada2da53SToomas Soome 			return (0);
825c5805b0bSps 		}
826c5805b0bSps 		first_byte = 0x00;
827c5805b0bSps 
828c5805b0bSps 		retval = pread(device_fd, &first_byte, sizeof (first_byte), 0);
829af3025fdSdt 		(void) close(device_fd);
830af3025fdSdt 		switch (first_byte) {
831af3025fdSdt 			case 0x08:
832af3025fdSdt 				(void) strcpy(bname, "i2c-at34cps");
833af3025fdSdt 				break;
834af3025fdSdt 			case 0x80:
835af3025fdSdt 				(void) strcpy(bname, "i2c-at34c02");
836af3025fdSdt 				break;
837af3025fdSdt 			default:
838af3025fdSdt 				(void) strcpy(bname, "i2c-at34cuk");
839af3025fdSdt 				break;
840af3025fdSdt 		}
841c5805b0bSps 	}
842c5805b0bSps 
8437c478bd9Sstevel@tonic-gate 	/* if there's a platform-specific conf file, use that */
8447c478bd9Sstevel@tonic-gate 	retval = -1;
8457c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
8467c478bd9Sstevel@tonic-gate 		(void) snprintf(devpath, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF,
8477c478bd9Sstevel@tonic-gate 		    nmbuf);
8487c478bd9Sstevel@tonic-gate 		(void) strlcat(devpath, FRU_CONTAINER_CONF, PATH_MAX);
8497c478bd9Sstevel@tonic-gate 		retval = access(devpath, R_OK);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 	if (retval != 0) {
8527c478bd9Sstevel@tonic-gate 		/* nothing for the platform, try the base name */
8537c478bd9Sstevel@tonic-gate 		(void) snprintf(devpath, PATH_MAX, "%s/%s",
8547c478bd9Sstevel@tonic-gate 		    CONTAINER_DIR, FRU_CONTAINER_CONF);
8557c478bd9Sstevel@tonic-gate 		retval = access(devpath, R_OK);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 	/* matches driver binding name to get container information */
8587c478bd9Sstevel@tonic-gate 	if (retval == 0) {
8597c478bd9Sstevel@tonic-gate 		retval = get_container_info(devpath, bname, &cont_info);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 	if (retval < 0) {
862ada2da53SToomas Soome 		return (0);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	cont_hash_obj->u.cont_obj->num_of_section =  cont_info.num_sections;
8667c478bd9Sstevel@tonic-gate 	cont_hash_obj->u.cont_obj->sec_obj_list = NULL;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	for (count = 0; count < cont_info.num_sections; count++) {
8697c478bd9Sstevel@tonic-gate 		sec_hash_obj = create_section_hash_object();
8707c478bd9Sstevel@tonic-gate 		if (sec_hash_obj == NULL) {
871ada2da53SToomas Soome 			return (0);
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		add_hashobject_to_hashtable(sec_hash_obj);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.offset =
8777c478bd9Sstevel@tonic-gate 		    cont_info.section_info[count].address;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.protection =
8807c478bd9Sstevel@tonic-gate 		    cont_info.section_info[count].description.field.read_only;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.length =
8837c478bd9Sstevel@tonic-gate 		    cont_info.section_info[count].size;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.version = cont_info.header_ver;
8867c478bd9Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->encoding =
8877c478bd9Sstevel@tonic-gate 		    cont_info.section_info[count].encoding;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		add_to_sec_object_list(cont_hash_obj, sec_hash_obj);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	return (cont_hash_obj->obj_hdl);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate static int
verify_header_crc8(headerrev_t head_ver,unsigned char * bytes,int length)8957c478bd9Sstevel@tonic-gate verify_header_crc8(headerrev_t head_ver, unsigned char *bytes, int length)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	int		crc_offset = 0;
8987c478bd9Sstevel@tonic-gate 	unsigned char	orig_crc8 = 0;
8997c478bd9Sstevel@tonic-gate 	unsigned char	calc_crc8 = 0;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	switch (head_ver) {
9027c478bd9Sstevel@tonic-gate 		case SECTION_HDR_VER:
9037c478bd9Sstevel@tonic-gate 			crc_offset = 4;
9047c478bd9Sstevel@tonic-gate 			break;
9057c478bd9Sstevel@tonic-gate 		default:
9067c478bd9Sstevel@tonic-gate 			errno = EINVAL;
9077c478bd9Sstevel@tonic-gate 			return (0);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	orig_crc8 = bytes[crc_offset];
9117c478bd9Sstevel@tonic-gate 	bytes[crc_offset] = 0x00; /* clear for calc */
9127c478bd9Sstevel@tonic-gate 	calc_crc8 = compute_crc8(bytes, length);
9137c478bd9Sstevel@tonic-gate 	bytes[crc_offset] = orig_crc8; /* restore */
9147c478bd9Sstevel@tonic-gate 	return (orig_crc8 == calc_crc8);
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * Description	:
9197c478bd9Sstevel@tonic-gate  *		fru_get_num_sections() returns number of sections in a
9207c478bd9Sstevel@tonic-gate  *		container. it calls get_container_index() to get the container
9217c478bd9Sstevel@tonic-gate  *		index number in the container list.
9227c478bd9Sstevel@tonic-gate  *
9237c478bd9Sstevel@tonic-gate  * Arguments	:
9247c478bd9Sstevel@tonic-gate  *		container_hdl_t	: container handle.
9257c478bd9Sstevel@tonic-gate  *
9267c478bd9Sstevel@tonic-gate  * Return	:
9277c478bd9Sstevel@tonic-gate  *		int
9287c478bd9Sstevel@tonic-gate  *		On success, returns number of sections in a container.
9297c478bd9Sstevel@tonic-gate  *
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /* ARGSUSED */
9337c478bd9Sstevel@tonic-gate int
fru_get_num_sections(container_hdl_t container,door_cred_t * cred)9347c478bd9Sstevel@tonic-gate fru_get_num_sections(container_hdl_t container, door_cred_t *cred)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	hash_obj_t		*hash_object;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	hash_object	= lookup_handle_object(container, CONTAINER_TYPE);
9397c478bd9Sstevel@tonic-gate 	if (hash_object == NULL) {
9407c478bd9Sstevel@tonic-gate 		return (-1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	return (hash_object->u.cont_obj->num_of_section);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * called from fru_get_sections()
9487c478bd9Sstevel@tonic-gate  */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate static void
get_section(int fd,hash_obj_t * sec_hash,section_t * section)9517c478bd9Sstevel@tonic-gate get_section(int fd, hash_obj_t *sec_hash, section_t *section)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	int			retval;
9547c478bd9Sstevel@tonic-gate 	int			size;
9557c478bd9Sstevel@tonic-gate 	int			count;
9567c478bd9Sstevel@tonic-gate 	uint16_t		hdrver;
9577c478bd9Sstevel@tonic-gate 	hash_obj_t		*seg_hash;
9587c478bd9Sstevel@tonic-gate 	unsigned char		*buffer;
9597c478bd9Sstevel@tonic-gate 	section_obj_t		*sec_obj;
9607c478bd9Sstevel@tonic-gate 	section_layout_t	sec_hdr;
9617c478bd9Sstevel@tonic-gate 	segment_layout_t	*seg_hdr;
9627c478bd9Sstevel@tonic-gate 	segment_layout_t	*seg_buf;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	sec_obj	= sec_hash->u.sec_obj;
9657c478bd9Sstevel@tonic-gate 	if (sec_obj == NULL) {
9667c478bd9Sstevel@tonic-gate 		return;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/* populate section_t */
9707c478bd9Sstevel@tonic-gate 	section->handle = sec_hash->obj_hdl;
9717c478bd9Sstevel@tonic-gate 	section->offset = sec_obj->section.offset;
9727c478bd9Sstevel@tonic-gate 	section->length = sec_obj->section.length;
9737c478bd9Sstevel@tonic-gate 	section->protection = sec_obj->section.protection;
9747c478bd9Sstevel@tonic-gate 	section->version = sec_obj->section.version;
9757c478bd9Sstevel@tonic-gate 	sec_obj->num_of_segment	= 0;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	switch (sec_obj->encoding) {
9787c478bd9Sstevel@tonic-gate 	case ENC_STANDARD:
9797c478bd9Sstevel@tonic-gate 		/* read section header layout */
9807c478bd9Sstevel@tonic-gate 		retval = pread(fd, &sec_hdr, sizeof (sec_hdr),
9817c478bd9Sstevel@tonic-gate 		    sec_obj->section.offset);
9827c478bd9Sstevel@tonic-gate 		break;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	case ENC_SPD:
9857c478bd9Sstevel@tonic-gate 		retval = get_sp_sec_hdr(&sec_hdr, sizeof (sec_hdr));
9867c478bd9Sstevel@tonic-gate 		break;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	default:
9897c478bd9Sstevel@tonic-gate 		return;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	if (retval != sizeof (sec_hdr)) {
9937c478bd9Sstevel@tonic-gate 		return;
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	hdrver	= GET_SECTION_HDR_VERSION;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if ((sec_hdr.headertag != SECTION_HDR_TAG) &&
9997c478bd9Sstevel@tonic-gate 	    (hdrver != section->version)) {
10007c478bd9Sstevel@tonic-gate 		return;
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	/* size = section layout + total sizeof segment header */
10047c478bd9Sstevel@tonic-gate 	size	= sizeof (sec_hdr) + ((sec_hdr.segmentcount) *
10057c478bd9Sstevel@tonic-gate 	    sizeof (segment_layout_t));
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	buffer	= alloca(size);
10087c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
10097c478bd9Sstevel@tonic-gate 		return;
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/* segment header buffer */
10137c478bd9Sstevel@tonic-gate 	seg_buf = alloca(size - sizeof (sec_hdr));
10147c478bd9Sstevel@tonic-gate 	if (seg_buf == NULL) {
10157c478bd9Sstevel@tonic-gate 		return;
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	switch (sec_obj->encoding) {
10197c478bd9Sstevel@tonic-gate 	case ENC_STANDARD:
10207c478bd9Sstevel@tonic-gate 		/* read segment header */
10217c478bd9Sstevel@tonic-gate 		retval = pread(fd, seg_buf, size - sizeof (sec_hdr),
10227c478bd9Sstevel@tonic-gate 		    sec_obj->section.offset + sizeof (sec_hdr));
10237c478bd9Sstevel@tonic-gate 		break;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	case ENC_SPD:
10267c478bd9Sstevel@tonic-gate 		retval =
10277c478bd9Sstevel@tonic-gate 		    get_sp_seg_hdr(seg_buf, size - sizeof (sec_hdr));
10287c478bd9Sstevel@tonic-gate 		break;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	default:
10317c478bd9Sstevel@tonic-gate 		return;
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (retval != (size - sizeof (sec_hdr))) {
10357c478bd9Sstevel@tonic-gate 		return;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/* copy section header layout */
10397c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, &sec_hdr, sizeof (sec_hdr));
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/* copy segment header layout */
10427c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer + sizeof (sec_hdr), seg_buf, size -
10437c478bd9Sstevel@tonic-gate 	    sizeof (sec_hdr));
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/* verify crc8 */
10467c478bd9Sstevel@tonic-gate 	retval = verify_header_crc8(hdrver, buffer, size);
10477c478bd9Sstevel@tonic-gate 	if (retval != TRUE) {
10487c478bd9Sstevel@tonic-gate 		return;
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	section->version = hdrver;
10527c478bd9Sstevel@tonic-gate 	sec_obj->section.version = hdrver;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	seg_hdr	= (segment_layout_t *)seg_buf;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	for (count = 0; count < sec_hdr.segmentcount; count++, seg_hdr++) {
10577c478bd9Sstevel@tonic-gate 		seg_hash = create_segment_hash_object();
10587c478bd9Sstevel@tonic-gate 		if (seg_hash == NULL) {
10597c478bd9Sstevel@tonic-gate 			return;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		add_hashobject_to_hashtable(seg_hash);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 		copy_segment_layout(&seg_hash->u.seg_obj->segment, seg_hdr);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		add_to_seg_object_list(sec_hash, seg_hash);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 		sec_obj->num_of_segment++;
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate static int
call_devfsadm(void)10747c478bd9Sstevel@tonic-gate call_devfsadm(void)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	char		*phys_path;
10777c478bd9Sstevel@tonic-gate 	di_node_t	root_node;
10787c478bd9Sstevel@tonic-gate 	di_node_t	prom_node;
10797c478bd9Sstevel@tonic-gate 	di_node_t	f_node;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
10827c478bd9Sstevel@tonic-gate 		return (-1);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	f_node = di_drv_first_node(PICL_CLASS_SEEPROM, root_node);
10867c478bd9Sstevel@tonic-gate 	if (f_node != DI_NODE_NIL) {
10877c478bd9Sstevel@tonic-gate 		phys_path = di_devfs_path(f_node);
10887c478bd9Sstevel@tonic-gate 		if ((prom_node = di_init(phys_path, DINFOMINOR)) !=
10897c478bd9Sstevel@tonic-gate 		    DI_NODE_NIL) {
10907c478bd9Sstevel@tonic-gate 			di_fini(prom_node);
10917c478bd9Sstevel@tonic-gate 			di_fini(root_node);
10927c478bd9Sstevel@tonic-gate 			(void) pclose(popen(devfsadm_cmd, "r"));
10937c478bd9Sstevel@tonic-gate 			return (0);
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	di_fini(root_node);
10977c478bd9Sstevel@tonic-gate 	return (-1);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate /*
11017c478bd9Sstevel@tonic-gate  * Description	:
1102ada2da53SToomas Soome  *		fru_get_sections() fills an array of section structures passed
11037c478bd9Sstevel@tonic-gate  *		as an argument.
11047c478bd9Sstevel@tonic-gate  *
11057c478bd9Sstevel@tonic-gate  * Arguments	:
11067c478bd9Sstevel@tonic-gate  *		container_hdl_t : container handle(device descriptor).
11077c478bd9Sstevel@tonic-gate  *		section_t	: array of section structure.
11087c478bd9Sstevel@tonic-gate  *		int		: maximum number of section in a container.
11097c478bd9Sstevel@tonic-gate  *
11107c478bd9Sstevel@tonic-gate  * Returns	:
1111ada2da53SToomas Soome  *		int
1112ada2da53SToomas Soome  *		On success,the number of section structures written is returned;
1113ada2da53SToomas Soome  *		on error, -1 is returned and "errno" is set appropriately.
11147c478bd9Sstevel@tonic-gate  *
11157c478bd9Sstevel@tonic-gate  */
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /* ARGSUSED */
11187c478bd9Sstevel@tonic-gate int
fru_get_sections(container_hdl_t container,section_t * section,int maxsec,door_cred_t * cred)11197c478bd9Sstevel@tonic-gate fru_get_sections(container_hdl_t container, section_t *section, int maxsec,
1120ada2da53SToomas Soome     door_cred_t *cred)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	int		device_fd;
11237c478bd9Sstevel@tonic-gate 	int		retrys = 1;
11247c478bd9Sstevel@tonic-gate 	int		count;
11257c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_object;
11267c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	cont_object = lookup_handle_object(container, CONTAINER_TYPE);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (cont_object == NULL) {
11317c478bd9Sstevel@tonic-gate 		return (-1);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (cont_object->u.cont_obj->num_of_section > maxsec) {
11357c478bd9Sstevel@tonic-gate 		return (-1);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	sec_hash = cont_object->u.cont_obj->sec_obj_list;
11397c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
11407c478bd9Sstevel@tonic-gate 		return (-1);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	do {
11447c478bd9Sstevel@tonic-gate 		device_fd =
11457c478bd9Sstevel@tonic-gate 		    open(cont_object->u.cont_obj->device_pathname, O_RDONLY);
11467c478bd9Sstevel@tonic-gate 		if (device_fd >= 0) {
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 	} while ((retrys-- > 0) && (call_devfsadm() == 0));
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (device_fd < 0) {
11527c478bd9Sstevel@tonic-gate 		return (-1);
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	for (count = 0; count < cont_object->u.cont_obj->num_of_section;
11567c478bd9Sstevel@tonic-gate 	    count++, section++) {
11577c478bd9Sstevel@tonic-gate 		section->version = -1;
11587c478bd9Sstevel@tonic-gate 		/* populate section_t */
11597c478bd9Sstevel@tonic-gate 		get_section(device_fd, sec_hash, section);
11607c478bd9Sstevel@tonic-gate 		sec_hash = sec_hash->u.sec_obj->next;
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9