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 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	(void) close(device_fd);
11647c478bd9Sstevel@tonic-gate 	return (count);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Description	:
1169ada2da53SToomas Soome  *		fru_get_num_segments() returns the current number of segments
11707c478bd9Sstevel@tonic-gate  *		in a section.
11717c478bd9Sstevel@tonic-gate  *
11727c478bd9Sstevel@tonic-gate  * Arguments	:
11737c478bd9Sstevel@tonic-gate  *		section_hdl_t : section header holding section information.
11747c478bd9Sstevel@tonic-gate  *
11757c478bd9Sstevel@tonic-gate  * Return	:
1176ada2da53SToomas Soome  *		int
1177ada2da53SToomas Soome  *		On success, the number of segments in the argument section is
1178ada2da53SToomas Soome  *		returned; on error -1 is returned.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /* ARGSUSED */
11827c478bd9Sstevel@tonic-gate int
fru_get_num_segments(section_hdl_t section,door_cred_t * cred)11837c478bd9Sstevel@tonic-gate fru_get_num_segments(section_hdl_t section, door_cred_t *cred)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_object;
11867c478bd9Sstevel@tonic-gate 	section_obj_t	*sec_obj;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	sec_object	= lookup_handle_object(section, SECTION_TYPE);
11897c478bd9Sstevel@tonic-gate 	if (sec_object == NULL) {
11907c478bd9Sstevel@tonic-gate 		return (-1);
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	sec_obj	= sec_object->u.sec_obj;
11947c478bd9Sstevel@tonic-gate 	if (sec_obj == NULL) {
11957c478bd9Sstevel@tonic-gate 		return (-1);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	return (sec_obj->num_of_segment);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate  * Description	:
12037c478bd9Sstevel@tonic-gate  *		fru_get_segments() fills an array of structures representing the
12047c478bd9Sstevel@tonic-gate  *		segments in a section.
12057c478bd9Sstevel@tonic-gate  *
12067c478bd9Sstevel@tonic-gate  * Arguments	:
12077c478bd9Sstevel@tonic-gate  *		section_hdl_t : holds section number.
12087c478bd9Sstevel@tonic-gate  *		segment_t : on success will hold segment information.
12097c478bd9Sstevel@tonic-gate  *		int	: maximum number of segment.
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  * Return	:
12127c478bd9Sstevel@tonic-gate  *		int
12137c478bd9Sstevel@tonic-gate  *		On success, the number of segment structures written is
12147c478bd9Sstevel@tonic-gate  *		returned; on errno -1 is returned.
12157c478bd9Sstevel@tonic-gate  */
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate /* ARGSUSED */
12187c478bd9Sstevel@tonic-gate int
fru_get_segments(section_hdl_t section,segment_t * segment,int maxseg,door_cred_t * cred)12197c478bd9Sstevel@tonic-gate fru_get_segments(section_hdl_t section, segment_t *segment, int maxseg,
12207c478bd9Sstevel@tonic-gate     door_cred_t *cred)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	int		count;
12237c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_object;
12247c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_object;
12257c478bd9Sstevel@tonic-gate 	section_obj_t	*sec_obj;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	sec_object = lookup_handle_object(section, SECTION_TYPE);
12287c478bd9Sstevel@tonic-gate 	if (sec_object == NULL) {
12297c478bd9Sstevel@tonic-gate 		return (-1);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	sec_obj	= sec_object->u.sec_obj;
12337c478bd9Sstevel@tonic-gate 	if (sec_obj == NULL) {
12347c478bd9Sstevel@tonic-gate 		return (-1);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (sec_obj->num_of_segment > maxseg) {
12387c478bd9Sstevel@tonic-gate 		return (-1);
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	seg_object	= sec_object->u.sec_obj->seg_obj_list;
12427c478bd9Sstevel@tonic-gate 	if (seg_object == NULL) {
12437c478bd9Sstevel@tonic-gate 		return (-1);
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	for (count = 0; count < sec_obj->num_of_segment; count++) {
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/* populate segment_t */
12497c478bd9Sstevel@tonic-gate 		segment->handle = seg_object->obj_hdl;
12507c478bd9Sstevel@tonic-gate 		(void) memcpy(segment->name,
12517c478bd9Sstevel@tonic-gate 		    seg_object->u.seg_obj->segment.name, SEG_NAME_LEN);
12527c478bd9Sstevel@tonic-gate 		segment->descriptor = seg_object->u.seg_obj->segment.descriptor;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		segment->offset	= seg_object->u.seg_obj->segment.offset;
12557c478bd9Sstevel@tonic-gate 		segment->length	= seg_object->u.seg_obj->segment.length;
12567c478bd9Sstevel@tonic-gate 		seg_object = seg_object->u.seg_obj->next;
12577c478bd9Sstevel@tonic-gate 		segment++;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	return (0);
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate  * Description	:
12647c478bd9Sstevel@tonic-gate  *		fru_add_segment() adds a segment to a section.
12657c478bd9Sstevel@tonic-gate  *
12667c478bd9Sstevel@tonic-gate  * Arguments	:
12677c478bd9Sstevel@tonic-gate  *		section_hdl_t section
12687c478bd9Sstevel@tonic-gate  *		A handle for the section in which to add the segment.
12697c478bd9Sstevel@tonic-gate  *
12707c478bd9Sstevel@tonic-gate  *		segment_t *segment
12717c478bd9Sstevel@tonic-gate  *		On entry, the "handle" component of "segment" is ignored and the
12727c478bd9Sstevel@tonic-gate  *		remaining components specify the parameters of the segment to be
12737c478bd9Sstevel@tonic-gate  *		added.  On return, the "handle" component is set to the handle
12747c478bd9Sstevel@tonic-gate  *		for the added segment. The segment offset is mandatory for FIXED
12757c478bd9Sstevel@tonic-gate  *		segments; otherwise, the offset is advisory.
12767c478bd9Sstevel@tonic-gate  *
12777c478bd9Sstevel@tonic-gate  * Return	:
12787c478bd9Sstevel@tonic-gate  *		int
12797c478bd9Sstevel@tonic-gate  *		On success, 0 is returned; on error -1 is returned.
12807c478bd9Sstevel@tonic-gate  *
12817c478bd9Sstevel@tonic-gate  */
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate int
fru_add_segment(section_hdl_t section,segment_t * segment,section_hdl_t * newsection,door_cred_t * cred)12847c478bd9Sstevel@tonic-gate fru_add_segment(section_hdl_t section, segment_t *segment,
1285ada2da53SToomas Soome     section_hdl_t *newsection, door_cred_t *cred)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	int		fd;
12887c478bd9Sstevel@tonic-gate 	int		retval;
12897c478bd9Sstevel@tonic-gate 	int		offset;
12907c478bd9Sstevel@tonic-gate 	int		sec_size;
12917c478bd9Sstevel@tonic-gate 	int		seg_cnt;
12927c478bd9Sstevel@tonic-gate 	int		bufsize;
12937c478bd9Sstevel@tonic-gate 	int		new_seg_offset;
12947c478bd9Sstevel@tonic-gate 	int		new_seg_length;
12957c478bd9Sstevel@tonic-gate 	int		fixed_segment;
12967c478bd9Sstevel@tonic-gate 	char		trailer[]	= { 0x0c, 0x00, 0x00, 0x00, 0x00 };
12977c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
12987c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
12997c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
13007c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*new_seg_desc;
1301ada2da53SToomas Soome 	unsigned char	*crcbuf;
13027c478bd9Sstevel@tonic-gate 	section_layout_t sec_layout;
13037c478bd9Sstevel@tonic-gate 	segment_layout_t *seg_layout;
13047c478bd9Sstevel@tonic-gate 	segment_layout_t *segment_buf;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/* check the effective uid of the client */
13077c478bd9Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
13087c478bd9Sstevel@tonic-gate 		errno = EPERM;
13097c478bd9Sstevel@tonic-gate 		return (-1);	/* not a root */
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/* section hash */
13137c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(section, SECTION_TYPE);
13147c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
13157c478bd9Sstevel@tonic-gate 		return (-1);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	/* check for read-only section */
13197c478bd9Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
13207c478bd9Sstevel@tonic-gate 		errno = EPERM;
13217c478bd9Sstevel@tonic-gate 		return (-1);
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	/* look for duplicate segment */
13257c478bd9Sstevel@tonic-gate 	seg_hash = sec_hash->u.sec_obj->seg_obj_list;
13267c478bd9Sstevel@tonic-gate 	while (seg_hash != NULL) {
13277c478bd9Sstevel@tonic-gate 		if (strncmp(segment->name, seg_hash->u.seg_obj->segment.name,
13287c478bd9Sstevel@tonic-gate 		    SEG_NAME_LEN) == 0) {
13297c478bd9Sstevel@tonic-gate 			errno = EEXIST;
13307c478bd9Sstevel@tonic-gate 			return (-1); /* can't add duplicate segment */
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 		seg_hash = seg_hash->u.seg_obj->next;
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	/* get the container hash */
13367c478bd9Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
13377c478bd9Sstevel@tonic-gate 	    CONTAINER_TYPE);
13387c478bd9Sstevel@tonic-gate 	if (cont_hash == NULL) {
13397c478bd9Sstevel@tonic-gate 		return (-1);
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/* open the container */
13437c478bd9Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
13447c478bd9Sstevel@tonic-gate 	if (fd < 0) {
13457c478bd9Sstevel@tonic-gate 		return (-1);
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/* section start here */
13497c478bd9Sstevel@tonic-gate 	offset	= sec_hash->u.sec_obj->section.offset;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/* read section header layout */
13527c478bd9Sstevel@tonic-gate 	retval = pread(fd, &sec_layout, sizeof (sec_layout), offset);
13537c478bd9Sstevel@tonic-gate 	if (retval != sizeof (sec_layout)) {
13547c478bd9Sstevel@tonic-gate 		(void) close(fd);
13557c478bd9Sstevel@tonic-gate 		return (-1);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/* check for valid section header */
13597c478bd9Sstevel@tonic-gate 	if (sec_layout.headertag != SECTION_HDR_TAG) {
13607c478bd9Sstevel@tonic-gate 		/* write a new one */
13617c478bd9Sstevel@tonic-gate 		sec_layout.headertag		= SECTION_HDR_TAG;
13627c478bd9Sstevel@tonic-gate 		sec_layout.headerversion[0]	= SECTION_HDR_VER_BIT0;
13637c478bd9Sstevel@tonic-gate 		sec_layout.headerversion[1]	= SECTION_HDR_VER_BIT1;
13647c478bd9Sstevel@tonic-gate 		sec_layout.headerlength		= sizeof (sec_layout);
13657c478bd9Sstevel@tonic-gate 		sec_layout.segmentcount		= 0;
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/* section size */
13697c478bd9Sstevel@tonic-gate 	sec_size	= sec_hash->u.sec_obj->section.length;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/* number of segment in the section */
13727c478bd9Sstevel@tonic-gate 	seg_cnt	= sec_layout.segmentcount;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/* total sizeof segment + new segment */
13757c478bd9Sstevel@tonic-gate 	bufsize	=	sizeof (segment_layout_t) * (seg_cnt + 1);
13767c478bd9Sstevel@tonic-gate 	segment_buf = alloca(bufsize);
13777c478bd9Sstevel@tonic-gate 	if (segment_buf == NULL) {
13787c478bd9Sstevel@tonic-gate 		return (-1);
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/* read entire segment header */
13827c478bd9Sstevel@tonic-gate 	retval = pread(fd, segment_buf,  (bufsize - sizeof (segment_layout_t)),
13837c478bd9Sstevel@tonic-gate 	    offset + sizeof (section_layout_t));
13847c478bd9Sstevel@tonic-gate 	if (retval != (bufsize - sizeof (segment_layout_t))) {
13857c478bd9Sstevel@tonic-gate 		(void) close(fd);
13867c478bd9Sstevel@tonic-gate 		return (-1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	new_seg_offset	= segment->offset; /* new segment offset */
13907c478bd9Sstevel@tonic-gate 	new_seg_length	= segment->length; /* new segment length */
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	new_seg_desc	= (fru_segdesc_t *)&segment->descriptor;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	fixed_segment	= new_seg_desc->field.fixed;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	/* get new offset for new segment to be addedd */
13977c478bd9Sstevel@tonic-gate 	retval = find_offset((char *)segment_buf, seg_cnt, sec_size,
13987c478bd9Sstevel@tonic-gate 	    &new_seg_offset, new_seg_length, fixed_segment, fd);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (retval != 0)	{
14017c478bd9Sstevel@tonic-gate 		(void) close(fd);
14027c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
14037c478bd9Sstevel@tonic-gate 		return (-1);
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	/* copy new segment data in segment layout */
14077c478bd9Sstevel@tonic-gate 	seg_layout	= (segment_layout_t *)(segment_buf + seg_cnt);
14087c478bd9Sstevel@tonic-gate 	(void) memcpy(&seg_layout->name, segment->name, SEG_NAME_LEN);
14097c478bd9Sstevel@tonic-gate 	(void) memcpy(seg_layout->descriptor, &segment->descriptor,
14107c478bd9Sstevel@tonic-gate 	    sizeof (uint32_t));
14117c478bd9Sstevel@tonic-gate 	seg_layout->length	= segment->length;
14127c478bd9Sstevel@tonic-gate 	seg_layout->offset	= new_seg_offset; /* new segment offset */
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	sec_layout.segmentcount += 1;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	crcbuf	= alloca(sizeof (section_layout_t) + bufsize);
14177c478bd9Sstevel@tonic-gate 	if (crcbuf == NULL) {
14187c478bd9Sstevel@tonic-gate 		(void) close(fd);
14197c478bd9Sstevel@tonic-gate 		return (-1);
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	sec_layout.headercrc8 = 0;
14237c478bd9Sstevel@tonic-gate 	sec_layout.headerlength += sizeof (segment_layout_t);
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	(void) memcpy(crcbuf, (char *)&sec_layout, sizeof (section_layout_t));
14267c478bd9Sstevel@tonic-gate 	(void) memcpy(crcbuf + sizeof (section_layout_t), segment_buf, bufsize);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	sec_layout.headercrc8 = compute_crc8(crcbuf, bufsize +
14297c478bd9Sstevel@tonic-gate 	    sizeof (section_layout_t));
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	/* write section header */
14327c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, &sec_layout, sizeof (section_layout_t), offset);
14337c478bd9Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
14347c478bd9Sstevel@tonic-gate 		(void) close(fd);
14357c478bd9Sstevel@tonic-gate 		return (-1);
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/* write segment header */
14397c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, segment_buf, bufsize, offset +
14407c478bd9Sstevel@tonic-gate 	    sizeof (section_layout_t));
14417c478bd9Sstevel@tonic-gate 	if (retval != bufsize) {
14427c478bd9Sstevel@tonic-gate 		(void) close(fd);
14437c478bd9Sstevel@tonic-gate 		return (-1);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/* write segment trailer */
14477c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, &trailer, sizeof (trailer), new_seg_offset);
14487c478bd9Sstevel@tonic-gate 	if (retval != sizeof (trailer)) {
14497c478bd9Sstevel@tonic-gate 		(void) close(fd);
14507c478bd9Sstevel@tonic-gate 		return (-1);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	(void) close(fd);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	/* create new segment hash object */
14567c478bd9Sstevel@tonic-gate 	seg_hash	= create_segment_hash_object();
14577c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
14587c478bd9Sstevel@tonic-gate 		return (-1);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	add_hashobject_to_hashtable(seg_hash);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	copy_segment_layout(&seg_hash->u.seg_obj->segment, seg_layout);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	add_to_seg_object_list(sec_hash, seg_hash);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	sec_hash->u.sec_obj->num_of_segment += 1;
14687c478bd9Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = new_seg_offset;
14697c478bd9Sstevel@tonic-gate 	*newsection	= section; /* return the new section handle */
14707c478bd9Sstevel@tonic-gate 	return (0);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate static void
free_pkt_object_list(hash_obj_t * hash_obj)14747c478bd9Sstevel@tonic-gate free_pkt_object_list(hash_obj_t	*hash_obj)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_obj;
14777c478bd9Sstevel@tonic-gate 	hash_obj_t	*free_obj;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	next_obj = hash_obj->u.seg_obj->pkt_obj_list;
14807c478bd9Sstevel@tonic-gate 	while (next_obj != NULL) {
14817c478bd9Sstevel@tonic-gate 		free_obj = next_obj;
14827c478bd9Sstevel@tonic-gate 		next_obj = next_obj->u.pkt_obj->next;
14837c478bd9Sstevel@tonic-gate 		/* if prev is NULL it's the first object in the list */
14847c478bd9Sstevel@tonic-gate 		if (free_obj->prev == NULL) {
14857c478bd9Sstevel@tonic-gate 			hash_table[(free_obj->obj_hdl % TABLE_SIZE)] =
14867c478bd9Sstevel@tonic-gate 			    free_obj->next;
14877c478bd9Sstevel@tonic-gate 			if (free_obj->next != NULL) {
14887c478bd9Sstevel@tonic-gate 				free_obj->next->prev = free_obj->prev;
14897c478bd9Sstevel@tonic-gate 			}
14907c478bd9Sstevel@tonic-gate 		} else {
14917c478bd9Sstevel@tonic-gate 			free_obj->prev->next = free_obj->next;
14927c478bd9Sstevel@tonic-gate 			if (free_obj->next != NULL) {
14937c478bd9Sstevel@tonic-gate 				free_obj->next->prev = free_obj->prev;
14947c478bd9Sstevel@tonic-gate 			}
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 		free(free_obj->u.pkt_obj->payload);
14987c478bd9Sstevel@tonic-gate 		free(free_obj->u.pkt_obj);
14997c478bd9Sstevel@tonic-gate 		free(free_obj);
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	hash_obj->u.seg_obj->pkt_obj_list = NULL;
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate static void
free_segment_hash(handle_t handle,hash_obj_t * sec_hash)15067c478bd9Sstevel@tonic-gate free_segment_hash(handle_t	handle, hash_obj_t	*sec_hash)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
15097c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	seg_hash	= sec_hash->u.sec_obj->seg_obj_list;
15127c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
15137c478bd9Sstevel@tonic-gate 		return;
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (seg_hash->obj_hdl == handle) {
15177c478bd9Sstevel@tonic-gate 		sec_hash->u.sec_obj->seg_obj_list = seg_hash->u.seg_obj->next;
15187c478bd9Sstevel@tonic-gate 	} else {
15197c478bd9Sstevel@tonic-gate 		while (seg_hash->obj_hdl != handle) {
15207c478bd9Sstevel@tonic-gate 			next_hash	= seg_hash;
15217c478bd9Sstevel@tonic-gate 			seg_hash = seg_hash->u.seg_obj->next;
15227c478bd9Sstevel@tonic-gate 			if (seg_hash == NULL) {
15237c478bd9Sstevel@tonic-gate 				return;
15247c478bd9Sstevel@tonic-gate 			}
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 		next_hash->u.seg_obj->next = seg_hash->u.seg_obj->next;
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	if (seg_hash->prev == NULL) {
15307c478bd9Sstevel@tonic-gate 		hash_table[(seg_hash->obj_hdl % TABLE_SIZE)] = seg_hash->next;
15317c478bd9Sstevel@tonic-gate 		if (seg_hash->next != NULL) {
15327c478bd9Sstevel@tonic-gate 			seg_hash->next->prev = NULL;
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 	} else {
15357c478bd9Sstevel@tonic-gate 		seg_hash->prev->next = seg_hash->next;
15367c478bd9Sstevel@tonic-gate 		if (seg_hash->next != NULL) {
15377c478bd9Sstevel@tonic-gate 			seg_hash->next->prev = seg_hash->prev;
15387c478bd9Sstevel@tonic-gate 		}
15397c478bd9Sstevel@tonic-gate 	}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	free_pkt_object_list(seg_hash);
15427c478bd9Sstevel@tonic-gate 	free(seg_hash->u.seg_obj);
15437c478bd9Sstevel@tonic-gate 	free(seg_hash);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /*
15477c478bd9Sstevel@tonic-gate  * Description	:
15487c478bd9Sstevel@tonic-gate  *		fru_delete_segment() deletes a segment from a section; the
15497c478bd9Sstevel@tonic-gate  *		associated container data is not altered.
15507c478bd9Sstevel@tonic-gate  *
15517c478bd9Sstevel@tonic-gate  * Arguments	: segment_hdl_t	segment handle.
15527c478bd9Sstevel@tonic-gate  *		  section_hdl_t	new section handle.
15537c478bd9Sstevel@tonic-gate  *
15547c478bd9Sstevel@tonic-gate  * Return	:
15557c478bd9Sstevel@tonic-gate  *		int
15567c478bd9Sstevel@tonic-gate  *		On success, 0 returned; On error -1 is returned.
15577c478bd9Sstevel@tonic-gate  */
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate int
fru_delete_segment(segment_hdl_t segment,section_hdl_t * newsection,door_cred_t * cred)15607c478bd9Sstevel@tonic-gate fru_delete_segment(segment_hdl_t segment, section_hdl_t *newsection,
1561ada2da53SToomas Soome     door_cred_t *cred)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate 	int			num_of_seg;
15647c478bd9Sstevel@tonic-gate 	int			bufsize;
15657c478bd9Sstevel@tonic-gate 	int			count;
15667c478bd9Sstevel@tonic-gate 	int			retval;
15677c478bd9Sstevel@tonic-gate 	int			fd;
15687c478bd9Sstevel@tonic-gate 	int			segnum;
15697c478bd9Sstevel@tonic-gate 	hash_obj_t		*seg_hash;
15707c478bd9Sstevel@tonic-gate 	hash_obj_t		*sec_hash;
15717c478bd9Sstevel@tonic-gate 	hash_obj_t		*cont_hash;
15727c478bd9Sstevel@tonic-gate 	hash_obj_t		*tmp_hash;
15737c478bd9Sstevel@tonic-gate 	unsigned char		*buffer;
15747c478bd9Sstevel@tonic-gate 	fru_segdesc_t		*desc;
15757c478bd9Sstevel@tonic-gate 	segment_layout_t	*seg_buf;
15767c478bd9Sstevel@tonic-gate 	section_layout_t	*sec_layout;
15777c478bd9Sstevel@tonic-gate 	segment_layout_t	*seg_layout;
15787c478bd9Sstevel@tonic-gate 	segment_layout_t	*next_layout;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	/* check the effective uid of the client */
15817c478bd9Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
15827c478bd9Sstevel@tonic-gate 		errno = EPERM;
15837c478bd9Sstevel@tonic-gate 		return (-1);	/* not a root */
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
15877c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
15887c478bd9Sstevel@tonic-gate 		return (-1);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
15927c478bd9Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_DELETE)) {
15937c478bd9Sstevel@tonic-gate 		errno = EPERM;
15947c478bd9Sstevel@tonic-gate 		return (-1); /* can't delete this segment */
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
15987c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
15997c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
16007c478bd9Sstevel@tonic-gate 		return (-1);
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
16047c478bd9Sstevel@tonic-gate 		errno = EPERM;
16057c478bd9Sstevel@tonic-gate 		return (-1);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	num_of_seg	= sec_hash->u.sec_obj->num_of_segment;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	bufsize	= (sizeof (segment_layout_t) * num_of_seg);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	seg_buf	= alloca(bufsize);
16137c478bd9Sstevel@tonic-gate 	if (seg_buf == NULL) {
16147c478bd9Sstevel@tonic-gate 		return (-1);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	segnum	= 0;
16187c478bd9Sstevel@tonic-gate 	for (tmp_hash = sec_hash->u.sec_obj->seg_obj_list; tmp_hash != NULL;
16197c478bd9Sstevel@tonic-gate 	    tmp_hash = tmp_hash->u.seg_obj->next) {
16207c478bd9Sstevel@tonic-gate 		if (tmp_hash->obj_hdl == segment) {
16217c478bd9Sstevel@tonic-gate 			break;
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 		segnum++;
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
16277c478bd9Sstevel@tonic-gate 	    CONTAINER_TYPE);
16287c478bd9Sstevel@tonic-gate 	if (cont_hash == NULL) {
16297c478bd9Sstevel@tonic-gate 		return (-1);
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	fd  = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
16337c478bd9Sstevel@tonic-gate 	if (fd < 0) {
16347c478bd9Sstevel@tonic-gate 		return (-1);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	sec_layout	= alloca(sizeof (section_layout_t));
16387c478bd9Sstevel@tonic-gate 	if (sec_layout == NULL) {
16397c478bd9Sstevel@tonic-gate 		(void) close(fd);
16407c478bd9Sstevel@tonic-gate 		return (-1);
16417c478bd9Sstevel@tonic-gate 	}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/* read section layout header */
16447c478bd9Sstevel@tonic-gate 	retval = pread(fd, sec_layout, sizeof (section_layout_t),
16457c478bd9Sstevel@tonic-gate 	    sec_hash->u.sec_obj->section.offset);
16467c478bd9Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
16477c478bd9Sstevel@tonic-gate 		(void) close(fd);
16487c478bd9Sstevel@tonic-gate 		return (-1);
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	/* read segment header layout */
16527c478bd9Sstevel@tonic-gate 	retval = pread(fd, seg_buf, bufsize,
16537c478bd9Sstevel@tonic-gate 	    sec_hash->u.sec_obj->section.offset + sizeof (section_layout_t));
16547c478bd9Sstevel@tonic-gate 	if (retval != bufsize) {
16557c478bd9Sstevel@tonic-gate 		(void) close(fd);
16567c478bd9Sstevel@tonic-gate 		return (-1);
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	seg_layout = (segment_layout_t *)(seg_buf + segnum);
16607c478bd9Sstevel@tonic-gate 	next_layout	= seg_layout;
16617c478bd9Sstevel@tonic-gate 	for (count = segnum;
16627c478bd9Sstevel@tonic-gate 	    count < sec_hash->u.sec_obj->num_of_segment - 1; count++) {
16637c478bd9Sstevel@tonic-gate 		next_layout++;
16647c478bd9Sstevel@tonic-gate 		(void) memcpy(seg_layout, next_layout,
16657c478bd9Sstevel@tonic-gate 		    sizeof (segment_layout_t));
16667c478bd9Sstevel@tonic-gate 		seg_layout++;
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	(void) memset(seg_layout, '\0', sizeof (segment_layout_t));
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	sec_layout->headercrc8 = 0;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	sec_layout->headerlength -= sizeof (segment_layout_t);
16747c478bd9Sstevel@tonic-gate 	sec_layout->segmentcount -= 1;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	buffer = alloca(sec_layout->headerlength);
16777c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
16787c478bd9Sstevel@tonic-gate 		(void) close(fd);
16797c478bd9Sstevel@tonic-gate 		return (-1);
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, sec_layout, sizeof (section_layout_t));
16837c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer + sizeof (section_layout_t), seg_buf, bufsize -
16847c478bd9Sstevel@tonic-gate 	    sizeof (segment_layout_t));
16857c478bd9Sstevel@tonic-gate 	sec_layout->headercrc8 = compute_crc8(buffer, sec_layout->headerlength);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	/* write section header with update crc8 and header length */
16887c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, sec_layout, sizeof (section_layout_t),
16897c478bd9Sstevel@tonic-gate 	    sec_hash->u.sec_obj->section.offset);
16907c478bd9Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
16917c478bd9Sstevel@tonic-gate 		(void) close(fd);
16927c478bd9Sstevel@tonic-gate 		return (-1);
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	/* write the update segment header */
16967c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, seg_buf, bufsize,
16977c478bd9Sstevel@tonic-gate 	    sec_hash->u.sec_obj->section.offset + sizeof (section_layout_t));
16987c478bd9Sstevel@tonic-gate 	(void) close(fd);
16997c478bd9Sstevel@tonic-gate 	if (retval != bufsize) {
17007c478bd9Sstevel@tonic-gate 		return (-1);
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	free_segment_hash(segment, sec_hash);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	*newsection	= sec_hash->obj_hdl;
17067c478bd9Sstevel@tonic-gate 	sec_hash->u.sec_obj->num_of_segment = sec_layout->segmentcount;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	return (0);
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate /*
17127c478bd9Sstevel@tonic-gate  * Description	:
1713ada2da53SToomas Soome  *		fru_read_segment() reads the raw contents of a segment.
17147c478bd9Sstevel@tonic-gate  *
17157c478bd9Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
17167c478bd9Sstevel@tonic-gate  *		 void *	: buffer containing segment data when function returns.
17177c478bd9Sstevel@tonic-gate  *		size_t :number of bytes.
17187c478bd9Sstevel@tonic-gate  *
17197c478bd9Sstevel@tonic-gate  * Return	:
1720ada2da53SToomas Soome  *		int
17217c478bd9Sstevel@tonic-gate  *		On success, the number of bytes read is returned;
17227c478bd9Sstevel@tonic-gate  *
17237c478bd9Sstevel@tonic-gate  * Notes	:
17247c478bd9Sstevel@tonic-gate  *		Segments containing packets can be read in structured fashion
17257c478bd9Sstevel@tonic-gate  *		using the fru_get_packets() and fru_get_payload() primitives;the
17267c478bd9Sstevel@tonic-gate  *		entire byte range of a segment can be read using
17277c478bd9Sstevel@tonic-gate  *		fru_read_segment().
17287c478bd9Sstevel@tonic-gate  */
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate /* ARGSUSED */
17317c478bd9Sstevel@tonic-gate ssize_t
fru_read_segment(segment_hdl_t segment,void * buffer,size_t nbytes,door_cred_t * cred)17327c478bd9Sstevel@tonic-gate fru_read_segment(segment_hdl_t segment, void *buffer, size_t nbytes,
17337c478bd9Sstevel@tonic-gate     door_cred_t *cred)
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate 	int		fd;
17367c478bd9Sstevel@tonic-gate 	int		retval;
17377c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
17387c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
17397c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/* segment hash object */
17427c478bd9Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
17437c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
17447c478bd9Sstevel@tonic-gate 		return (-1);
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	/* section hash object */
17487c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
17497c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
17507c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
17517c478bd9Sstevel@tonic-gate 		return (-1);
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	/* container hash object */
17557c478bd9Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
17567c478bd9Sstevel@tonic-gate 	    CONTAINER_TYPE);
17577c478bd9Sstevel@tonic-gate 	if (cont_hash == NULL) {
17587c478bd9Sstevel@tonic-gate 		return (-1);
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->segment.length < nbytes) {
17627c478bd9Sstevel@tonic-gate 		return (-1);
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDONLY);
17667c478bd9Sstevel@tonic-gate 	if (fd < 0) {
17677c478bd9Sstevel@tonic-gate 		return (-1);
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	switch (sec_hash->u.sec_obj->encoding) {
17717c478bd9Sstevel@tonic-gate 	case ENC_STANDARD:
17727c478bd9Sstevel@tonic-gate 		retval = pread(fd, buffer, nbytes,
17737c478bd9Sstevel@tonic-gate 		    seg_hash->u.seg_obj->segment.offset);
17747c478bd9Sstevel@tonic-gate 		(void) close(fd);
17757c478bd9Sstevel@tonic-gate 		if (retval != nbytes) {
17767c478bd9Sstevel@tonic-gate 			return (-1);
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 		break;
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	case ENC_SPD: {
17817c478bd9Sstevel@tonic-gate 		char	*spd_buf;
17827c478bd9Sstevel@tonic-gate 		uchar_t	*ptr;
17837c478bd9Sstevel@tonic-gate 		size_t	len;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		spd_buf = alloca(sec_hash->u.sec_obj->section.length);
17867c478bd9Sstevel@tonic-gate 		if (spd_buf == NULL)
17877c478bd9Sstevel@tonic-gate 			retval = -1;
17887c478bd9Sstevel@tonic-gate 		else {
17897c478bd9Sstevel@tonic-gate 			retval = get_spd_data(fd, spd_buf,
17907c478bd9Sstevel@tonic-gate 			    sec_hash->u.sec_obj->section.length,
17917c478bd9Sstevel@tonic-gate 			    seg_hash->u.seg_obj->segment.offset);
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 		(void) close(fd);
17947c478bd9Sstevel@tonic-gate 		if (retval != 0) {
17957c478bd9Sstevel@tonic-gate 			return (-1);
17967c478bd9Sstevel@tonic-gate 		}
17977c478bd9Sstevel@tonic-gate 		retval = cvrt_dim_data(spd_buf,
17987c478bd9Sstevel@tonic-gate 		    sec_hash->u.sec_obj->section.length, &ptr, &len);
17997c478bd9Sstevel@tonic-gate 		if (retval != 0) {
18007c478bd9Sstevel@tonic-gate 			return (-1);
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 		if (nbytes > len)
18037c478bd9Sstevel@tonic-gate 			nbytes = len;
18047c478bd9Sstevel@tonic-gate 		(void) memcpy(buffer, ptr, nbytes);
18057c478bd9Sstevel@tonic-gate 		free(ptr);
18067c478bd9Sstevel@tonic-gate 		break;
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	default:
18107c478bd9Sstevel@tonic-gate 		return (-1);
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	return (nbytes);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate /*
18177c478bd9Sstevel@tonic-gate  * Description	:
18187c478bd9Sstevel@tonic-gate  *		fru_write_segment() writes a raw segment.
18197c478bd9Sstevel@tonic-gate  *
18207c478bd9Sstevel@tonic-gate  * Arguments	: segment_hdl_t :segment handle.
18217c478bd9Sstevel@tonic-gate  *		 const void * : data buffer.
18227c478bd9Sstevel@tonic-gate  *		 size_t	: number of bytes.
18237c478bd9Sstevel@tonic-gate  *		 segment_hdl_t : new segment handle.
18247c478bd9Sstevel@tonic-gate  *
18257c478bd9Sstevel@tonic-gate  * Returns	:
18267c478bd9Sstevel@tonic-gate  *		int
18277c478bd9Sstevel@tonic-gate  *		On success, the number of bytes written is returned
18287c478bd9Sstevel@tonic-gate  *
18297c478bd9Sstevel@tonic-gate  */
18307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18317c478bd9Sstevel@tonic-gate int
fru_write_segment(segment_hdl_t segment,const void * data,size_t nbytes,segment_hdl_t * newsegment,door_cred_t * cred)18327c478bd9Sstevel@tonic-gate fru_write_segment(segment_hdl_t segment, const void *data, size_t nbytes,
18337c478bd9Sstevel@tonic-gate     segment_hdl_t *newsegment, door_cred_t *cred)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate static int
get_packet(int device_fd,void * buffer,int size,int offset)18407c478bd9Sstevel@tonic-gate get_packet(int device_fd, void *buffer, int size, int offset)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate 	int	retval;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	retval = pread(device_fd, (char *)buffer, size, offset);
18457c478bd9Sstevel@tonic-gate 	if (retval != -1) {
18467c478bd9Sstevel@tonic-gate 		return (0);
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 	return (-1);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate static uint32_t
get_checksum_crc(hash_obj_t * seg_hash,int data_size)18527c478bd9Sstevel@tonic-gate get_checksum_crc(hash_obj_t	*seg_hash, int data_size)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	int		protection;
18557c478bd9Sstevel@tonic-gate 	int		offset = 0;
18567c478bd9Sstevel@tonic-gate 	uint32_t	crc;
18577c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
18587c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
18597c478bd9Sstevel@tonic-gate 	unsigned char	*buffer;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
18627c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
18637c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
18647c478bd9Sstevel@tonic-gate 		return ((uint32_t)-1);
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	buffer = alloca(data_size);
18687c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
18697c478bd9Sstevel@tonic-gate 		return ((uint32_t)-1);
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/* traverse the packet object list for all the tags and payload */
18737c478bd9Sstevel@tonic-gate 	for (pkt_hash = seg_hash->u.seg_obj->pkt_obj_list;
18747c478bd9Sstevel@tonic-gate 	    pkt_hash != NULL; pkt_hash = pkt_hash->u.pkt_obj->next) {
18757c478bd9Sstevel@tonic-gate 		(void) memcpy(buffer + offset, &pkt_hash->u.pkt_obj->tag,
18767c478bd9Sstevel@tonic-gate 		    pkt_hash->u.pkt_obj->tag_size);
18777c478bd9Sstevel@tonic-gate 		offset += pkt_hash->u.pkt_obj->tag_size;
18787c478bd9Sstevel@tonic-gate 		(void) memcpy(buffer + offset, pkt_hash->u.pkt_obj->payload,
18797c478bd9Sstevel@tonic-gate 		    pkt_hash->u.pkt_obj->paylen);
18807c478bd9Sstevel@tonic-gate 		offset += pkt_hash->u.pkt_obj->paylen;
18817c478bd9Sstevel@tonic-gate 	}
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	protection	= sec_hash->u.sec_obj->section.protection;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if (protection == READ_ONLY_SECTION) { /* read-only section */
18867c478bd9Sstevel@tonic-gate 		crc = compute_crc32(buffer, data_size);
18877c478bd9Sstevel@tonic-gate 	} else {		/* read/write section */
18887c478bd9Sstevel@tonic-gate 		crc = compute_checksum32(buffer, data_size);
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 	return (crc);	/* computed crc */
18917c478bd9Sstevel@tonic-gate }
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate static int
get_dev_or_buffered_packets(hash_obj_t * seg_hash,int device_fd,int offset,int length,const char * buf)18947c478bd9Sstevel@tonic-gate get_dev_or_buffered_packets(hash_obj_t *seg_hash, int device_fd, int offset,
18957c478bd9Sstevel@tonic-gate     int length, const char *buf)
18967c478bd9Sstevel@tonic-gate {
18977c478bd9Sstevel@tonic-gate 	int		tag_size;
18987c478bd9Sstevel@tonic-gate 	int		paylen;
18997c478bd9Sstevel@tonic-gate 	int		retval;
19007c478bd9Sstevel@tonic-gate 	int		seg_limit = 0;
19017c478bd9Sstevel@tonic-gate 	int		pktcnt	= 0;
19027c478bd9Sstevel@tonic-gate 	char		*data;
19037c478bd9Sstevel@tonic-gate 	uint32_t	crc;
19047c478bd9Sstevel@tonic-gate 	uint32_t	origcrc;
19057c478bd9Sstevel@tonic-gate 	fru_tag_t	tag;
19067c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash_obj;
19077c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*segdesc;
19087c478bd9Sstevel@tonic-gate 	fru_tagtype_t	tagtype;
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
19117c478bd9Sstevel@tonic-gate 		retval = get_packet(device_fd, &tag, sizeof (fru_tag_t),
19127c478bd9Sstevel@tonic-gate 		    offset);
19137c478bd9Sstevel@tonic-gate 		if (retval == -1) {
19147c478bd9Sstevel@tonic-gate 			return (-1);
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 	} else if (length - offset < sizeof (fru_tag_t)) {
19177c478bd9Sstevel@tonic-gate 		return (-1);
19187c478bd9Sstevel@tonic-gate 	} else {
19197c478bd9Sstevel@tonic-gate 		(void) memcpy(&tag, buf + offset, sizeof (fru_tag_t));
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = offset;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	data	= (char *)&tag;
19257c478bd9Sstevel@tonic-gate 	while (data[0] != SEG_TRAILER_TAG) {
19267c478bd9Sstevel@tonic-gate 		tagtype	= get_tag_type(&tag); /* verify tag type */
19277c478bd9Sstevel@tonic-gate 		if (tagtype == -1) {
19287c478bd9Sstevel@tonic-gate 			return (-1);
19297c478bd9Sstevel@tonic-gate 		}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 		tag_size = get_tag_size(tagtype);
19327c478bd9Sstevel@tonic-gate 		if (tag_size == -1) {
19337c478bd9Sstevel@tonic-gate 			return (-1);
19347c478bd9Sstevel@tonic-gate 		}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 		seg_limit += tag_size;
19377c478bd9Sstevel@tonic-gate 		if (seg_limit > length) {
19387c478bd9Sstevel@tonic-gate 			return (-1);
19397c478bd9Sstevel@tonic-gate 		}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 		paylen = get_payload_length((void *)&tag);
19427c478bd9Sstevel@tonic-gate 		if (paylen == -1) {
19437c478bd9Sstevel@tonic-gate 			return (-1);
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 		seg_limit += paylen;
19477c478bd9Sstevel@tonic-gate 		if (seg_limit > length) {
19487c478bd9Sstevel@tonic-gate 			return (-1);
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		pkt_hash_obj = create_packet_hash_object();
19527c478bd9Sstevel@tonic-gate 		if (pkt_hash_obj == NULL) {
19537c478bd9Sstevel@tonic-gate 			return (-1);
19547c478bd9Sstevel@tonic-gate 		}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->payload = malloc(paylen);
19577c478bd9Sstevel@tonic-gate 		if (pkt_hash_obj->u.pkt_obj->payload == NULL) {
19587c478bd9Sstevel@tonic-gate 			free(pkt_hash_obj);
19597c478bd9Sstevel@tonic-gate 			return (-1);
19607c478bd9Sstevel@tonic-gate 		}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 		offset += tag_size;
19637c478bd9Sstevel@tonic-gate 		if (buf == NULL) {
19647c478bd9Sstevel@tonic-gate 			retval = pread(device_fd,
19657c478bd9Sstevel@tonic-gate 			    pkt_hash_obj->u.pkt_obj->payload, paylen, offset);
19667c478bd9Sstevel@tonic-gate 		} else if (paylen + offset > length) {
19677c478bd9Sstevel@tonic-gate 			retval = 0;
19687c478bd9Sstevel@tonic-gate 		} else {
19697c478bd9Sstevel@tonic-gate 			(void) memcpy(pkt_hash_obj->u.pkt_obj->payload,
19707c478bd9Sstevel@tonic-gate 			    buf + offset, paylen);
19717c478bd9Sstevel@tonic-gate 			retval = paylen;
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 		if (retval != paylen) {
19747c478bd9Sstevel@tonic-gate 			free(pkt_hash_obj->u.pkt_obj->payload);
19757c478bd9Sstevel@tonic-gate 			free(pkt_hash_obj);
19767c478bd9Sstevel@tonic-gate 			return (-1);
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		/* don't change this */
19807c478bd9Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->tag.raw_data = 0;
19817c478bd9Sstevel@tonic-gate 		(void) memcpy(&pkt_hash_obj->u.pkt_obj->tag, &tag, tag_size);
19827c478bd9Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->paylen = paylen;
19837c478bd9Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->tag_size = tag_size;
19847c478bd9Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->payload_offset = offset;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 		offset += paylen;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 		add_hashobject_to_hashtable(pkt_hash_obj);
19897c478bd9Sstevel@tonic-gate 		add_to_pkt_object_list(seg_hash, pkt_hash_obj);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 		pktcnt++;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 		if (buf == NULL) {
19947c478bd9Sstevel@tonic-gate 			retval = get_packet(device_fd, &tag, sizeof (fru_tag_t),
19957c478bd9Sstevel@tonic-gate 			    offset);
19967c478bd9Sstevel@tonic-gate 			if (retval == -1) {
19977c478bd9Sstevel@tonic-gate 				return (-1);
19987c478bd9Sstevel@tonic-gate 			}
19997c478bd9Sstevel@tonic-gate 		} else if (length - offset < sizeof (fru_tag_t)) {
20007c478bd9Sstevel@tonic-gate 			if (length - offset > 0) {
20017c478bd9Sstevel@tonic-gate 				/*
20027c478bd9Sstevel@tonic-gate 				 * not enough data for a full fru_tag_t
20037c478bd9Sstevel@tonic-gate 				 * just return what there is
20047c478bd9Sstevel@tonic-gate 				 */
20057c478bd9Sstevel@tonic-gate 				(void) memset(&tag, 0, sizeof (fru_tag_t));
20067c478bd9Sstevel@tonic-gate 				(void) memcpy(&tag, buf + offset,
20077c478bd9Sstevel@tonic-gate 				    length - offset);
20087c478bd9Sstevel@tonic-gate 			}
20097c478bd9Sstevel@tonic-gate 		} else {
20107c478bd9Sstevel@tonic-gate 			(void) memcpy(&tag, buf + offset, sizeof (fru_tag_t));
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		data	= (char *)&tag;
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	segdesc	= (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = offset;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	if (!segdesc->field.ignore_checksum)  {
20217c478bd9Sstevel@tonic-gate 		crc = get_checksum_crc(seg_hash, seg_limit);
20227c478bd9Sstevel@tonic-gate 		offset	= seg_hash->u.seg_obj->segment.offset;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 		if (buf == NULL) {
20257c478bd9Sstevel@tonic-gate 			retval = pread(device_fd, &origcrc, sizeof (origcrc),
20267c478bd9Sstevel@tonic-gate 			    offset + seg_limit + 1);
20277c478bd9Sstevel@tonic-gate 			if (retval != sizeof (origcrc)) {
20287c478bd9Sstevel@tonic-gate 				return (-1);
20297c478bd9Sstevel@tonic-gate 			}
20307c478bd9Sstevel@tonic-gate 		} else if (length - offset < sizeof (origcrc)) {
20317c478bd9Sstevel@tonic-gate 			return (-1);
20327c478bd9Sstevel@tonic-gate 		} else {
20337c478bd9Sstevel@tonic-gate 			(void) memcpy(&origcrc, buf + seg_limit + 1,
20347c478bd9Sstevel@tonic-gate 			    sizeof (origcrc));
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 		if (origcrc != crc) {
20387c478bd9Sstevel@tonic-gate 			seg_hash->u.seg_obj->trailer_offset = offset;
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	return (pktcnt);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate static int
get_packets(hash_obj_t * seg_hash,int device_fd,int offset,int length)20467c478bd9Sstevel@tonic-gate get_packets(hash_obj_t *seg_hash, int device_fd, int offset, int length)
20477c478bd9Sstevel@tonic-gate {
20487c478bd9Sstevel@tonic-gate 	return (get_dev_or_buffered_packets(seg_hash, device_fd, offset,
20497c478bd9Sstevel@tonic-gate 	    length, NULL));
20507c478bd9Sstevel@tonic-gate }
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate static int
get_buffered_packets(hash_obj_t * seg_hash,const char * seg_buf,size_t seg_len)20537c478bd9Sstevel@tonic-gate get_buffered_packets(hash_obj_t *seg_hash, const char *seg_buf, size_t seg_len)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	return (get_dev_or_buffered_packets(seg_hash, -1, 0, seg_len, seg_buf));
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * Description	:
20607c478bd9Sstevel@tonic-gate  *		fru_get_num_packets() returns the current number of packets
20617c478bd9Sstevel@tonic-gate  *		in a segment.
20627c478bd9Sstevel@tonic-gate  *
20637c478bd9Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
20647c478bd9Sstevel@tonic-gate  *
20657c478bd9Sstevel@tonic-gate  * Return	:
20667c478bd9Sstevel@tonic-gate  *		int
20677c478bd9Sstevel@tonic-gate  *		On success, the number of packets is returned;
20687c478bd9Sstevel@tonic-gate  *		-1 on failure.
20697c478bd9Sstevel@tonic-gate  */
20707c478bd9Sstevel@tonic-gate int
fru_get_num_packets(segment_hdl_t segment,door_cred_t * cred)20717c478bd9Sstevel@tonic-gate fru_get_num_packets(segment_hdl_t segment, door_cred_t *cred)
20727c478bd9Sstevel@tonic-gate {
20737c478bd9Sstevel@tonic-gate 	int		device_fd;
20747c478bd9Sstevel@tonic-gate 	int		pktcnt;
20757c478bd9Sstevel@tonic-gate 	int		length;
20767c478bd9Sstevel@tonic-gate 	uint16_t	offset;
20777c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash_obj;
20787c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
20797c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
20807c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*segdesc;
20817c478bd9Sstevel@tonic-gate 	segment_obj_t	*segment_object;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	seg_hash	= lookup_handle_object(segment, SEGMENT_TYPE);
20847c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
20857c478bd9Sstevel@tonic-gate 		return (-1);
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	segment_object	= seg_hash->u.seg_obj;
20897c478bd9Sstevel@tonic-gate 	if (segment_object == NULL) {
20907c478bd9Sstevel@tonic-gate 		return (-1);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	segdesc = (fru_segdesc_t *)&segment_object->segment.descriptor;
20947c478bd9Sstevel@tonic-gate 	if (segdesc->field.opaque) {
20957c478bd9Sstevel@tonic-gate 		return (0);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->pkt_obj_list != NULL) {
20997c478bd9Sstevel@tonic-gate 		return (segment_object->num_of_packets);
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	offset = segment_object->segment.offset;
21037c478bd9Sstevel@tonic-gate 	length = segment_object->segment.length;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	/* section hash object */
21067c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
21077c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
21087c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
21097c478bd9Sstevel@tonic-gate 		return (-1);
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	segment_object->num_of_packets = 0;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	switch (sec_hash->u.sec_obj->encoding) {
21157c478bd9Sstevel@tonic-gate 	case ENC_STANDARD:
21167c478bd9Sstevel@tonic-gate 		cont_hash_obj = get_container_hash_object(SEGMENT_TYPE,
21177c478bd9Sstevel@tonic-gate 		    segment_object->section_hdl);
21187c478bd9Sstevel@tonic-gate 		if (cont_hash_obj == NULL) {
21197c478bd9Sstevel@tonic-gate 			return (-1);
21207c478bd9Sstevel@tonic-gate 		}
21217c478bd9Sstevel@tonic-gate 		device_fd = open(cont_hash_obj->u.cont_obj->device_pathname,
21227c478bd9Sstevel@tonic-gate 		    O_RDWR);
21237c478bd9Sstevel@tonic-gate 		if (device_fd < 0) {
21247c478bd9Sstevel@tonic-gate 			return (-1);
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		pktcnt = get_packets(seg_hash, device_fd, offset, length);
21287c478bd9Sstevel@tonic-gate 		(void) close(device_fd);
21297c478bd9Sstevel@tonic-gate 		break;
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	case ENC_SPD: {
21327c478bd9Sstevel@tonic-gate 		ssize_t		spd_seg_len;
21337c478bd9Sstevel@tonic-gate 		size_t		nbytes;
21347c478bd9Sstevel@tonic-gate 		char		*seg_buf;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 		nbytes = segment_object->segment.length;
21377c478bd9Sstevel@tonic-gate 		seg_buf = alloca(nbytes);
21387c478bd9Sstevel@tonic-gate 		if (seg_buf == NULL)
21397c478bd9Sstevel@tonic-gate 			return (-1);
21407c478bd9Sstevel@tonic-gate 		spd_seg_len =
21417c478bd9Sstevel@tonic-gate 		    fru_read_segment(segment, seg_buf, nbytes, cred);
21427c478bd9Sstevel@tonic-gate 		if (spd_seg_len < 0)
2143af3025fdSdt 			return (-1);
21447c478bd9Sstevel@tonic-gate 		pktcnt = get_buffered_packets(seg_hash, seg_buf,
21457c478bd9Sstevel@tonic-gate 		    spd_seg_len);
21467c478bd9Sstevel@tonic-gate 		break;
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	default:
21507c478bd9Sstevel@tonic-gate 		return (-1);
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	if (pktcnt == -1) {
21547c478bd9Sstevel@tonic-gate 		free_pkt_object_list(seg_hash);
21557c478bd9Sstevel@tonic-gate 		seg_hash->u.seg_obj->pkt_obj_list = NULL;
21567c478bd9Sstevel@tonic-gate 	}
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	segment_object->num_of_packets = pktcnt;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	return (segment_object->num_of_packets);
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate /*
21657c478bd9Sstevel@tonic-gate  * Description	:
21667c478bd9Sstevel@tonic-gate  *		fru_get_packets() fills an array of structures representing the
21677c478bd9Sstevel@tonic-gate  *		packets in a segment.
21687c478bd9Sstevel@tonic-gate  *
21697c478bd9Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
21707c478bd9Sstevel@tonic-gate  *		packet_t	: packet buffer.
21717c478bd9Sstevel@tonic-gate  *		int	: maximum number of packets.
21727c478bd9Sstevel@tonic-gate  *
21737c478bd9Sstevel@tonic-gate  * Return	:
21747c478bd9Sstevel@tonic-gate  *		int
21757c478bd9Sstevel@tonic-gate  *		On success, the number of packet structures written is returned;
21767c478bd9Sstevel@tonic-gate  *		On failure -1 is returned;
21777c478bd9Sstevel@tonic-gate  *
21787c478bd9Sstevel@tonic-gate  */
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate /* ARGSUSED */
21817c478bd9Sstevel@tonic-gate int
fru_get_packets(segment_hdl_t segment,packet_t * packet,int maxpackets,door_cred_t * cred)21827c478bd9Sstevel@tonic-gate fru_get_packets(segment_hdl_t segment, packet_t *packet, int maxpackets,
21837c478bd9Sstevel@tonic-gate     door_cred_t *cred)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	int		count;
21867c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash_obj;
21877c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash_obj;
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/* segment hash object */
21907c478bd9Sstevel@tonic-gate 	seg_hash_obj	= lookup_handle_object(segment, SEGMENT_TYPE);
21917c478bd9Sstevel@tonic-gate 	if (seg_hash_obj == NULL) {
21927c478bd9Sstevel@tonic-gate 		return (-1);
21937c478bd9Sstevel@tonic-gate 	}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (seg_hash_obj->u.seg_obj->num_of_packets != maxpackets) {
21967c478bd9Sstevel@tonic-gate 		return (-1);
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	pkt_hash_obj	= seg_hash_obj->u.seg_obj->pkt_obj_list;
22007c478bd9Sstevel@tonic-gate 	if (pkt_hash_obj == NULL) {
22017c478bd9Sstevel@tonic-gate 		return (-1);
22027c478bd9Sstevel@tonic-gate 	}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	for (count = 0; count < maxpackets; count++, packet++) {
22057c478bd9Sstevel@tonic-gate 		packet->handle	= pkt_hash_obj->obj_hdl;
22067c478bd9Sstevel@tonic-gate 		packet->tag = 0;
22077c478bd9Sstevel@tonic-gate 		(void) memcpy(&packet->tag, &pkt_hash_obj->u.pkt_obj->tag,
22087c478bd9Sstevel@tonic-gate 		    pkt_hash_obj->u.pkt_obj->tag_size);
22097c478bd9Sstevel@tonic-gate 		pkt_hash_obj = pkt_hash_obj->u.pkt_obj->next;
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	return (0);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate /*
22167c478bd9Sstevel@tonic-gate  * Description	:
22177c478bd9Sstevel@tonic-gate  *		fru_get_payload() copies the contents of a packet's payload.
22187c478bd9Sstevel@tonic-gate  *
22197c478bd9Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet handle.
22207c478bd9Sstevel@tonic-gate  *		void *	: payload buffer.
22217c478bd9Sstevel@tonic-gate  *		size_t	: sizeof the buffer.
22227c478bd9Sstevel@tonic-gate  *
22237c478bd9Sstevel@tonic-gate  * Return	:
2224ada2da53SToomas Soome  *		int
2225ada2da53SToomas Soome  *		On success, the number of bytes copied is returned; On error
22267c478bd9Sstevel@tonic-gate  *		-1 returned.
22277c478bd9Sstevel@tonic-gate  */
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate /* ARGSUSED */
22307c478bd9Sstevel@tonic-gate ssize_t
fru_get_payload(packet_hdl_t packet,void * buffer,size_t nbytes,door_cred_t * cred)22317c478bd9Sstevel@tonic-gate fru_get_payload(packet_hdl_t packet, void *buffer, size_t nbytes,
22327c478bd9Sstevel@tonic-gate     door_cred_t *cred)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	hash_obj_t	*packet_hash_obj;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	/* packet hash object */
22377c478bd9Sstevel@tonic-gate 	packet_hash_obj	= lookup_handle_object(packet, PACKET_TYPE);
22387c478bd9Sstevel@tonic-gate 	if (packet_hash_obj == NULL) {
22397c478bd9Sstevel@tonic-gate 		return (-1);
22407c478bd9Sstevel@tonic-gate 	}
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	/* verify payload length */
22437c478bd9Sstevel@tonic-gate 	if (nbytes != packet_hash_obj->u.pkt_obj->paylen) {
22447c478bd9Sstevel@tonic-gate 		return (-1);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, packet_hash_obj->u.pkt_obj->payload, nbytes);
22487c478bd9Sstevel@tonic-gate 	return (nbytes);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate /*
22527c478bd9Sstevel@tonic-gate  * Description	:
2253ada2da53SToomas Soome  *		fru_update_payload() writes the contents of a packet's payload.
22547c478bd9Sstevel@tonic-gate  *
22557c478bd9Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet handle.
22567c478bd9Sstevel@tonic-gate  *		const void * : data buffer.
22577c478bd9Sstevel@tonic-gate  *		size_t	: buffer size.
22587c478bd9Sstevel@tonic-gate  *		packet_hdl_t	: new packet handle.
22597c478bd9Sstevel@tonic-gate  *
22607c478bd9Sstevel@tonic-gate  * Return	:
2261ada2da53SToomas Soome  *		int
22627c478bd9Sstevel@tonic-gate  *		On success, 0 is returned; on failure
22637c478bd9Sstevel@tonic-gate  *		-1 is returned.
22647c478bd9Sstevel@tonic-gate  */
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate int
fru_update_payload(packet_hdl_t packet,const void * data,size_t nbytes,packet_hdl_t * newpacket,door_cred_t * cred)22677c478bd9Sstevel@tonic-gate fru_update_payload(packet_hdl_t packet, const void *data, size_t nbytes,
22687c478bd9Sstevel@tonic-gate     packet_hdl_t *newpacket, door_cred_t *cred)
22697c478bd9Sstevel@tonic-gate {
22707c478bd9Sstevel@tonic-gate 	int		fd;
22717c478bd9Sstevel@tonic-gate 	int		segment_offset;
22727c478bd9Sstevel@tonic-gate 	int		trailer_offset;
22737c478bd9Sstevel@tonic-gate 	int		retval;
22747c478bd9Sstevel@tonic-gate 	uint32_t	crc;
22757c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
22767c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
22777c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
22787c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
22797c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*desc;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	/* check the effective uid of the client */
22827c478bd9Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
22837c478bd9Sstevel@tonic-gate 		errno = EPERM;
22847c478bd9Sstevel@tonic-gate 		return (-1);	/* not a root */
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	/* packet hash object */
22887c478bd9Sstevel@tonic-gate 	pkt_hash = lookup_handle_object(packet,	PACKET_TYPE);
22897c478bd9Sstevel@tonic-gate 	if (pkt_hash == NULL) {
22907c478bd9Sstevel@tonic-gate 		return (-1);
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	/* segment hash object */
22947c478bd9Sstevel@tonic-gate 	seg_hash = lookup_handle_object(pkt_hash->u.pkt_obj->segment_hdl,
22957c478bd9Sstevel@tonic-gate 	    SEGMENT_TYPE);
22967c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
22977c478bd9Sstevel@tonic-gate 		return (-1);
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	/* check for write perm. */
23017c478bd9Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
23027c478bd9Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
23037c478bd9Sstevel@tonic-gate 		errno = EPERM;
23047c478bd9Sstevel@tonic-gate 		return (-1); /* write not allowed */
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
23087c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
23097c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
23107c478bd9Sstevel@tonic-gate 		return (-1);
23117c478bd9Sstevel@tonic-gate 	}
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
23147c478bd9Sstevel@tonic-gate 		errno = EPERM;
23157c478bd9Sstevel@tonic-gate 		return (-1);		/* read-only section */
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
23197c478bd9Sstevel@tonic-gate 	    CONTAINER_TYPE);
23207c478bd9Sstevel@tonic-gate 	if (cont_hash == NULL) {
23217c478bd9Sstevel@tonic-gate 		return (-1);
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (pkt_hash->u.pkt_obj->paylen != nbytes) {
23257c478bd9Sstevel@tonic-gate 		return (-1);
23267c478bd9Sstevel@tonic-gate 	}
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	(void) memcpy(pkt_hash->u.pkt_obj->payload, (char *)data, nbytes);
23297c478bd9Sstevel@tonic-gate 	fd	= open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
23307c478bd9Sstevel@tonic-gate 	if (fd < 0) {
23317c478bd9Sstevel@tonic-gate 		return (-1);
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	trailer_offset	= seg_hash->u.seg_obj->trailer_offset;
23357c478bd9Sstevel@tonic-gate 	segment_offset	= seg_hash->u.seg_obj->segment.offset;
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	crc = get_checksum_crc(seg_hash, (trailer_offset - segment_offset));
23387c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, data, nbytes, pkt_hash->u.pkt_obj->payload_offset);
23397c478bd9Sstevel@tonic-gate 	if (retval != nbytes) {
23407c478bd9Sstevel@tonic-gate 		(void) close(fd);
23417c478bd9Sstevel@tonic-gate 		return (-1);
23427c478bd9Sstevel@tonic-gate 	}
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, &crc, sizeof (crc), trailer_offset + 1);
23457c478bd9Sstevel@tonic-gate 	(void) close(fd);
23467c478bd9Sstevel@tonic-gate 	if (retval != sizeof (crc)) {
23477c478bd9Sstevel@tonic-gate 		return (-1);
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 	*newpacket	= packet;
23507c478bd9Sstevel@tonic-gate 	return (0);
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate  * Description	:
23557c478bd9Sstevel@tonic-gate  *		fru_append_packet() appends a packet to a segment.
23567c478bd9Sstevel@tonic-gate  *
23577c478bd9Sstevel@tonic-gate  * Arguments	:
23587c478bd9Sstevel@tonic-gate  *		segment_hdl_t segment
23597c478bd9Sstevel@tonic-gate  *		A handle for the segment to which the packet will be appended.
23607c478bd9Sstevel@tonic-gate  *
2361ada2da53SToomas Soome  *		packet_t *packet
2362ada2da53SToomas Soome  *		On entry, the "tag" component of "packet" specifies the tag
2363ada2da53SToomas Soome  *		value for the added packet; the "handle" component is ignored.
2364ada2da53SToomas Soome  *		On return, the "handle" component is set to the handle of the
2365ada2da53SToomas Soome  *		appended packet.
23667c478bd9Sstevel@tonic-gate  *
2367ada2da53SToomas Soome  *		const void *payload
2368ada2da53SToomas Soome  *		A pointer to the caller's buffer containing the payload data for
2369ada2da53SToomas Soome  *		the appended packet.
23707c478bd9Sstevel@tonic-gate  *
2371ada2da53SToomas Soome  *		size_t nbytes
2372ada2da53SToomas Soome  *		The size of the caller buffer.
23737c478bd9Sstevel@tonic-gate  *
23747c478bd9Sstevel@tonic-gate  * Return	:
2375ada2da53SToomas Soome  *		int
2376ada2da53SToomas Soome  *		On success, 0 is returned; on error -1 is returned;
23777c478bd9Sstevel@tonic-gate  */
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate int
fru_append_packet(segment_hdl_t segment,packet_t * packet,const void * payload,size_t nbytes,segment_hdl_t * newsegment,door_cred_t * cred)23807c478bd9Sstevel@tonic-gate fru_append_packet(segment_hdl_t segment, packet_t *packet, const void *payload,
23817c478bd9Sstevel@tonic-gate     size_t nbytes, segment_hdl_t *newsegment, door_cred_t *cred)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	int		trailer_offset;
23847c478bd9Sstevel@tonic-gate 	int		tag_size;
23857c478bd9Sstevel@tonic-gate 	int		fd;
23867c478bd9Sstevel@tonic-gate 	int		retval;
23877c478bd9Sstevel@tonic-gate 	char		trailer[] = {0x0c, 0x00, 0x00, 0x00, 0x00};
23887c478bd9Sstevel@tonic-gate 	uint32_t	crc;
23897c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
23907c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
23917c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
23927c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
23937c478bd9Sstevel@tonic-gate 	fru_tagtype_t	tagtype;
23947c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*desc;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	/* check the effective uid of the client */
23977c478bd9Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
23987c478bd9Sstevel@tonic-gate 		errno = EPERM;
23997c478bd9Sstevel@tonic-gate 		return (-1);	/* not a root */
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
24037c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
24047c478bd9Sstevel@tonic-gate 		return (-1);
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/* check for write perm. */
24087c478bd9Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
24097c478bd9Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
24107c478bd9Sstevel@tonic-gate 		errno = EPERM;
24117c478bd9Sstevel@tonic-gate 		return (-1); /* write not allowed */
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
24157c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
24167c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
24177c478bd9Sstevel@tonic-gate 		return (-1);
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
24217c478bd9Sstevel@tonic-gate 		errno = EPERM;
24227c478bd9Sstevel@tonic-gate 		return (-1);		/* read-only section */
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	trailer_offset	= seg_hash->u.seg_obj->trailer_offset;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	/*
24287c478bd9Sstevel@tonic-gate 	 * if trailer offset is 0 than parse the segment data to get the trailer
24297c478bd9Sstevel@tonic-gate 	 * offset to compute the remaining space left in the segment area for
24307c478bd9Sstevel@tonic-gate 	 * new packet to be added.
24317c478bd9Sstevel@tonic-gate 	 */
24327c478bd9Sstevel@tonic-gate 	if (trailer_offset == 0) {
24337c478bd9Sstevel@tonic-gate 		(void) fru_get_num_packets(segment, cred);
24347c478bd9Sstevel@tonic-gate 		trailer_offset  = seg_hash->u.seg_obj->trailer_offset;
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	tagtype	= get_tag_type((void *)&packet->tag);
24387c478bd9Sstevel@tonic-gate 	if (tagtype == -1) {
24397c478bd9Sstevel@tonic-gate 		return (-1);
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	tag_size	= get_tag_size(tagtype);
24437c478bd9Sstevel@tonic-gate 	if (tag_size == -1) {
24447c478bd9Sstevel@tonic-gate 		return (-1);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->segment.length >
24487c478bd9Sstevel@tonic-gate 	    ((trailer_offset - seg_hash->u.seg_obj->segment.offset) +
24497c478bd9Sstevel@tonic-gate 	    tag_size + nbytes + sizeof (char) + sizeof (uint32_t))) {
24507c478bd9Sstevel@tonic-gate 		/* create new packet hash */
24517c478bd9Sstevel@tonic-gate 		pkt_hash = create_packet_hash_object();
24527c478bd9Sstevel@tonic-gate 		if (pkt_hash == NULL) {
24537c478bd9Sstevel@tonic-gate 			return (-1);
24547c478bd9Sstevel@tonic-gate 		}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 		/* tag initialization */
24577c478bd9Sstevel@tonic-gate 		(void) memcpy(&pkt_hash->u.pkt_obj->tag, &packet->tag,
24587c478bd9Sstevel@tonic-gate 		    tag_size);
24597c478bd9Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->tag_size	= tag_size;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 		/* payload inititalization */
24627c478bd9Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->payload	= malloc(nbytes);
24637c478bd9Sstevel@tonic-gate 		if (pkt_hash->u.pkt_obj->payload == NULL) {
24647c478bd9Sstevel@tonic-gate 			free(pkt_hash);
24657c478bd9Sstevel@tonic-gate 			return (-1);
24667c478bd9Sstevel@tonic-gate 		}
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 		(void) memcpy(pkt_hash->u.pkt_obj->payload, payload, nbytes);
24697c478bd9Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->paylen	= nbytes;
24707c478bd9Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->payload_offset = trailer_offset + tag_size;
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 		/* add to hash table */
24737c478bd9Sstevel@tonic-gate 		add_hashobject_to_hashtable(pkt_hash);
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 		add_to_pkt_object_list(seg_hash, pkt_hash);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 		cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
24787c478bd9Sstevel@tonic-gate 		    CONTAINER_TYPE);
24797c478bd9Sstevel@tonic-gate 		if (cont_hash == NULL) {
24807c478bd9Sstevel@tonic-gate 			return (-1);
24817c478bd9Sstevel@tonic-gate 		}
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 		fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
24847c478bd9Sstevel@tonic-gate 		if (fd < 0) {
24857c478bd9Sstevel@tonic-gate 			return (-1);
24867c478bd9Sstevel@tonic-gate 		}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 		/* update the trailer offset  */
24897c478bd9Sstevel@tonic-gate 		trailer_offset += tag_size + nbytes;
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 		/* calculate new checksum */
24927c478bd9Sstevel@tonic-gate 		crc = get_checksum_crc(seg_hash, (trailer_offset -
24937c478bd9Sstevel@tonic-gate 		    seg_hash->u.seg_obj->segment.offset));
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, &packet->tag, tag_size,
24967c478bd9Sstevel@tonic-gate 		    trailer_offset - (tag_size + nbytes));
24977c478bd9Sstevel@tonic-gate 		if (retval != tag_size) {
24987c478bd9Sstevel@tonic-gate 			(void) close(fd);
24997c478bd9Sstevel@tonic-gate 			return (-1);
25007c478bd9Sstevel@tonic-gate 		}
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, payload, nbytes, trailer_offset - nbytes);
25037c478bd9Sstevel@tonic-gate 		if (retval != nbytes) {
25047c478bd9Sstevel@tonic-gate 			(void) close(fd);
25057c478bd9Sstevel@tonic-gate 			return (-1);
25067c478bd9Sstevel@tonic-gate 		}
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, trailer, sizeof (trailer), trailer_offset);
25097c478bd9Sstevel@tonic-gate 		if (retval != sizeof (trailer)) {
25107c478bd9Sstevel@tonic-gate 			(void) close(fd);
25117c478bd9Sstevel@tonic-gate 			return (-1);
25127c478bd9Sstevel@tonic-gate 		}
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, &crc, sizeof (crc), trailer_offset + 1);
25157c478bd9Sstevel@tonic-gate 		(void) close(fd);
25167c478bd9Sstevel@tonic-gate 		if (retval != sizeof (crc)) {
25177c478bd9Sstevel@tonic-gate 			return (-1);
25187c478bd9Sstevel@tonic-gate 		}
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 		seg_hash->u.seg_obj->trailer_offset = trailer_offset;
25217c478bd9Sstevel@tonic-gate 		seg_hash->u.seg_obj->num_of_packets += 1;
25227c478bd9Sstevel@tonic-gate 
2523ada2da53SToomas Soome 		*newsegment = segment;	/* return new segment handle */
25247c478bd9Sstevel@tonic-gate 		return (0);
25257c478bd9Sstevel@tonic-gate 	} else {
25267c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	return (-1);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate static void
adjust_packets(int fd,hash_obj_t * free_obj,hash_obj_t * object_list)25337c478bd9Sstevel@tonic-gate adjust_packets(int	fd, hash_obj_t	*free_obj, hash_obj_t	*object_list)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	int		retval;
25367c478bd9Sstevel@tonic-gate 	uint32_t	new_offset;
25377c478bd9Sstevel@tonic-gate 	hash_obj_t	*hash_ptr;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	new_offset = free_obj->u.pkt_obj->payload_offset -
25407c478bd9Sstevel@tonic-gate 	    free_obj->u.pkt_obj->tag_size;
25417c478bd9Sstevel@tonic-gate 	for (hash_ptr = object_list;
25427c478bd9Sstevel@tonic-gate 	    hash_ptr != NULL; hash_ptr = hash_ptr->u.pkt_obj->next) {
25437c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, &hash_ptr->u.pkt_obj->tag,
25447c478bd9Sstevel@tonic-gate 		    hash_ptr->u.pkt_obj->tag_size, new_offset);
25457c478bd9Sstevel@tonic-gate 		if (retval != hash_ptr->u.pkt_obj->tag_size) {
25467c478bd9Sstevel@tonic-gate 			return;
25477c478bd9Sstevel@tonic-gate 		}
25487c478bd9Sstevel@tonic-gate 		new_offset += hash_ptr->u.pkt_obj->tag_size;
25497c478bd9Sstevel@tonic-gate 		hash_ptr->u.pkt_obj->payload_offset = new_offset;
25507c478bd9Sstevel@tonic-gate 		retval = pwrite(fd, hash_ptr->u.pkt_obj->payload,
25517c478bd9Sstevel@tonic-gate 		    hash_ptr->u.pkt_obj->paylen, new_offset);
25527c478bd9Sstevel@tonic-gate 		if (retval != hash_ptr->u.pkt_obj->paylen) {
25537c478bd9Sstevel@tonic-gate 			return;
25547c478bd9Sstevel@tonic-gate 		}
25557c478bd9Sstevel@tonic-gate 		new_offset += hash_ptr->u.pkt_obj->paylen;
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate }
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate static void
free_packet_object(handle_t handle,hash_obj_t * seg_hash)25607c478bd9Sstevel@tonic-gate free_packet_object(handle_t	handle, hash_obj_t *seg_hash)
25617c478bd9Sstevel@tonic-gate {
25627c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
25637c478bd9Sstevel@tonic-gate 	hash_obj_t	*next_hash;
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	pkt_hash	= seg_hash->u.seg_obj->pkt_obj_list;
25667c478bd9Sstevel@tonic-gate 	if (pkt_hash == NULL) {
25677c478bd9Sstevel@tonic-gate 		return;
25687c478bd9Sstevel@tonic-gate 	}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	if (pkt_hash->obj_hdl == handle) {
25717c478bd9Sstevel@tonic-gate 		seg_hash->u.seg_obj->pkt_obj_list = pkt_hash->u.pkt_obj->next;
25727c478bd9Sstevel@tonic-gate 	} else {
25737c478bd9Sstevel@tonic-gate 		while (pkt_hash->obj_hdl != handle) {
25747c478bd9Sstevel@tonic-gate 			next_hash = pkt_hash;
25757c478bd9Sstevel@tonic-gate 			pkt_hash = pkt_hash->u.pkt_obj->next;
25767c478bd9Sstevel@tonic-gate 			if (pkt_hash == NULL) {
25777c478bd9Sstevel@tonic-gate 				return;
25787c478bd9Sstevel@tonic-gate 			}
25797c478bd9Sstevel@tonic-gate 		}
25807c478bd9Sstevel@tonic-gate 		next_hash->u.pkt_obj->next = pkt_hash->u.pkt_obj->next;
25817c478bd9Sstevel@tonic-gate 	}
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 	if (pkt_hash->prev == NULL) {
25847c478bd9Sstevel@tonic-gate 		hash_table[(pkt_hash->obj_hdl % TABLE_SIZE)] = pkt_hash->next;
25857c478bd9Sstevel@tonic-gate 		if (pkt_hash->next != NULL) {
25867c478bd9Sstevel@tonic-gate 			pkt_hash->next->prev = NULL;
25877c478bd9Sstevel@tonic-gate 		}
25887c478bd9Sstevel@tonic-gate 	} else {
25897c478bd9Sstevel@tonic-gate 		pkt_hash->prev->next = pkt_hash->next;
25907c478bd9Sstevel@tonic-gate 		if (pkt_hash->next != NULL) {
25917c478bd9Sstevel@tonic-gate 			pkt_hash->next->prev = pkt_hash->prev;
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	free(pkt_hash->u.pkt_obj->payload);
25967c478bd9Sstevel@tonic-gate 	free(pkt_hash->u.pkt_obj);
25977c478bd9Sstevel@tonic-gate 	free(pkt_hash);
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate /*
26017c478bd9Sstevel@tonic-gate  * Description	:
2602ada2da53SToomas Soome  *		fru_delete_packet() deletes a packet from a segment.
26037c478bd9Sstevel@tonic-gate  *
26047c478bd9Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet number to be deleted.
26057c478bd9Sstevel@tonic-gate  *		segment_hdl_t : new segment handler.
26067c478bd9Sstevel@tonic-gate  *
26077c478bd9Sstevel@tonic-gate  * Return	:
2608ada2da53SToomas Soome  *		int
2609ada2da53SToomas Soome  *		On success, 0 is returned; on error, -1.
26107c478bd9Sstevel@tonic-gate  *
26117c478bd9Sstevel@tonic-gate  * NOTES
2612ada2da53SToomas Soome  *		Packets are adjacent; thus, deleting a packet requires moving
2613ada2da53SToomas Soome  *		succeeding packets to compact the resulting hole.
26147c478bd9Sstevel@tonic-gate  */
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate int
fru_delete_packet(packet_hdl_t packet,segment_hdl_t * newsegment,door_cred_t * cred)26177c478bd9Sstevel@tonic-gate fru_delete_packet(packet_hdl_t packet, segment_hdl_t *newsegment,
26187c478bd9Sstevel@tonic-gate     door_cred_t *cred)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	int		retval;
26217c478bd9Sstevel@tonic-gate 	int		fd;
26227c478bd9Sstevel@tonic-gate 	char		trailer[] = { 0x0c, 0x00, 0x00, 0x00, 0x00};
26237c478bd9Sstevel@tonic-gate 	uint32_t	crc;
26247c478bd9Sstevel@tonic-gate 	hash_obj_t	*tmp_obj;
26257c478bd9Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
26267c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
26277c478bd9Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
26287c478bd9Sstevel@tonic-gate 	hash_obj_t	*prev_obj;
26297c478bd9Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
26307c478bd9Sstevel@tonic-gate 	fru_segdesc_t	*desc;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	/* check the effective uid of the client */
26337c478bd9Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
26347c478bd9Sstevel@tonic-gate 		errno = EPERM;
26357c478bd9Sstevel@tonic-gate 		return (-1);	/* not a root */
26367c478bd9Sstevel@tonic-gate 	}
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	/* packet hash object */
26397c478bd9Sstevel@tonic-gate 	pkt_hash = lookup_handle_object(packet, PACKET_TYPE);
26407c478bd9Sstevel@tonic-gate 	if (pkt_hash == NULL) {
26417c478bd9Sstevel@tonic-gate 		return (-1);
26427c478bd9Sstevel@tonic-gate 	}
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	/* segment hash object */
26457c478bd9Sstevel@tonic-gate 	seg_hash = lookup_handle_object(pkt_hash->u.pkt_obj->segment_hdl,
26467c478bd9Sstevel@tonic-gate 	    SEGMENT_TYPE);
26477c478bd9Sstevel@tonic-gate 	if (seg_hash == NULL) {
26487c478bd9Sstevel@tonic-gate 		return (-1);
26497c478bd9Sstevel@tonic-gate 	}
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	/* check for write perm. */
26527c478bd9Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
26537c478bd9Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
26547c478bd9Sstevel@tonic-gate 		errno = EPERM;
26557c478bd9Sstevel@tonic-gate 		return (-1); /* write not allowed */
26567c478bd9Sstevel@tonic-gate 	}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	/* section hash object */
26597c478bd9Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
26607c478bd9Sstevel@tonic-gate 	    SECTION_TYPE);
26617c478bd9Sstevel@tonic-gate 	if (sec_hash == NULL) {
26627c478bd9Sstevel@tonic-gate 		return (-1);
26637c478bd9Sstevel@tonic-gate 	}
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
26667c478bd9Sstevel@tonic-gate 		errno = EPERM;
2667ada2da53SToomas Soome 		return (-1);		/* read-only section */
26687c478bd9Sstevel@tonic-gate 	}
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	prev_obj	= seg_hash->u.seg_obj->pkt_obj_list;
26717c478bd9Sstevel@tonic-gate 	if (prev_obj == NULL) {
26727c478bd9Sstevel@tonic-gate 		return (-1);
26737c478bd9Sstevel@tonic-gate 	}
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	/* container hash object */
26767c478bd9Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
26777c478bd9Sstevel@tonic-gate 	    CONTAINER_TYPE);
26787c478bd9Sstevel@tonic-gate 	if (cont_hash == NULL) {
26797c478bd9Sstevel@tonic-gate 		return (-1);
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
26837c478bd9Sstevel@tonic-gate 	if (fd < 0) {
26847c478bd9Sstevel@tonic-gate 		return (-1);
26857c478bd9Sstevel@tonic-gate 	}
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	if (prev_obj->obj_hdl == packet) { /* first object to be deleted */
26887c478bd9Sstevel@tonic-gate 		adjust_packets(fd, prev_obj, prev_obj->u.pkt_obj->next);
26897c478bd9Sstevel@tonic-gate 		seg_hash->u.seg_obj->trailer_offset -=
26907c478bd9Sstevel@tonic-gate 		    (prev_obj->u.pkt_obj->tag_size +
26917c478bd9Sstevel@tonic-gate 		    prev_obj->u.pkt_obj->paylen);
26927c478bd9Sstevel@tonic-gate 		free_packet_object(packet, seg_hash);
26937c478bd9Sstevel@tonic-gate 	} else {
26947c478bd9Sstevel@tonic-gate 		for (tmp_obj = prev_obj;
2695af3025fdSdt 		    tmp_obj != NULL; tmp_obj = tmp_obj->u.pkt_obj->next) {
26967c478bd9Sstevel@tonic-gate 			/* found the object */
26977c478bd9Sstevel@tonic-gate 			if (tmp_obj->obj_hdl == packet) {
26987c478bd9Sstevel@tonic-gate 				adjust_packets(fd, tmp_obj,
26997c478bd9Sstevel@tonic-gate 				    tmp_obj->u.pkt_obj->next);
27007c478bd9Sstevel@tonic-gate 				seg_hash->u.seg_obj->trailer_offset -=
27017c478bd9Sstevel@tonic-gate 				    (tmp_obj->u.pkt_obj->tag_size +
27027c478bd9Sstevel@tonic-gate 				    tmp_obj->u.pkt_obj->paylen);
27037c478bd9Sstevel@tonic-gate 				free_packet_object(packet, seg_hash);
27047c478bd9Sstevel@tonic-gate 			}
27057c478bd9Sstevel@tonic-gate 		}
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	seg_hash->u.seg_obj->num_of_packets -= 1;
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	/* calculate checksum */
27117c478bd9Sstevel@tonic-gate 	crc = get_checksum_crc(seg_hash, (seg_hash->u.seg_obj->trailer_offset -
27127c478bd9Sstevel@tonic-gate 	    seg_hash->u.seg_obj->segment.offset));
27137c478bd9Sstevel@tonic-gate 	/* write trailer at new offset */
27147c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, &trailer, sizeof (trailer),
27157c478bd9Sstevel@tonic-gate 	    seg_hash->u.seg_obj->trailer_offset);
27167c478bd9Sstevel@tonic-gate 	if (retval != sizeof (trailer)) {
27177c478bd9Sstevel@tonic-gate 		(void) close(fd);
27187c478bd9Sstevel@tonic-gate 		return (-1);
27197c478bd9Sstevel@tonic-gate 	}
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	/* write the checksum value */
27227c478bd9Sstevel@tonic-gate 	retval = pwrite(fd, &crc, sizeof (crc),
27237c478bd9Sstevel@tonic-gate 	    seg_hash->u.seg_obj->trailer_offset + 1);
27247c478bd9Sstevel@tonic-gate 	(void) close(fd);
27257c478bd9Sstevel@tonic-gate 	if (retval != sizeof (crc)) {
27267c478bd9Sstevel@tonic-gate 		return (-1);
27277c478bd9Sstevel@tonic-gate 	}
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	*newsegment = seg_hash->obj_hdl; /* return new segment handle */
27307c478bd9Sstevel@tonic-gate 	return (0);
27317c478bd9Sstevel@tonic-gate }
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate /*
27347c478bd9Sstevel@tonic-gate  * Description :
27357c478bd9Sstevel@tonic-gate  *		fru_close_container() removes the association between a
27367c478bd9Sstevel@tonic-gate  *		container and its handle. this routines free's up all the
27377c478bd9Sstevel@tonic-gate  *		hash object contained under container.
27387c478bd9Sstevel@tonic-gate  *
27397c478bd9Sstevel@tonic-gate  * Arguments   :
27407c478bd9Sstevel@tonic-gate  *		container_hdl_t holds the file descriptor of the fru.
27417c478bd9Sstevel@tonic-gate  *
27427c478bd9Sstevel@tonic-gate  * Return      :
27437c478bd9Sstevel@tonic-gate  *		int
27447c478bd9Sstevel@tonic-gate  *		return 0.
27457c478bd9Sstevel@tonic-gate  *
27467c478bd9Sstevel@tonic-gate  */
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate /* ARGSUSED */
27497c478bd9Sstevel@tonic-gate int
fru_close_container(container_hdl_t container)27507c478bd9Sstevel@tonic-gate fru_close_container(container_hdl_t container)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate 	hash_obj_t	*hash_obj;
27537c478bd9Sstevel@tonic-gate 	hash_obj_t	*prev_hash;
27547c478bd9Sstevel@tonic-gate 	hash_obj_t	*sec_hash_obj;
27557c478bd9Sstevel@tonic-gate 	handle_t	obj_hdl;
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	/* lookup for container hash object */
27587c478bd9Sstevel@tonic-gate 	hash_obj = lookup_handle_object(container, CONTAINER_TYPE);
27597c478bd9Sstevel@tonic-gate 	if (hash_obj == NULL) {
27607c478bd9Sstevel@tonic-gate 		return (0);
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	/* points to section object list */
27647c478bd9Sstevel@tonic-gate 	sec_hash_obj = hash_obj->u.cont_obj->sec_obj_list;
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	/* traverse section object list */
27677c478bd9Sstevel@tonic-gate 	while (sec_hash_obj != NULL) {
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 		/* traverse segment hash object in the section */
27707c478bd9Sstevel@tonic-gate 		while (sec_hash_obj->u.sec_obj->seg_obj_list != NULL) {
27717c478bd9Sstevel@tonic-gate 			/* object handle of the segment hash object */
27727c478bd9Sstevel@tonic-gate 			obj_hdl	=
27737c478bd9Sstevel@tonic-gate 			    sec_hash_obj->u.sec_obj->seg_obj_list->obj_hdl;
27747c478bd9Sstevel@tonic-gate 			free_segment_hash(obj_hdl, sec_hash_obj);
27757c478bd9Sstevel@tonic-gate 		}
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 		/* going to free section hash object, relink the hash object */
27787c478bd9Sstevel@tonic-gate 		if (sec_hash_obj->prev == NULL) {
27797c478bd9Sstevel@tonic-gate 			hash_table[(sec_hash_obj->obj_hdl % TABLE_SIZE)] =
27807c478bd9Sstevel@tonic-gate 			    sec_hash_obj->next;
27817c478bd9Sstevel@tonic-gate 			if (sec_hash_obj->next != NULL) {
27827c478bd9Sstevel@tonic-gate 				sec_hash_obj->next->prev = NULL;
27837c478bd9Sstevel@tonic-gate 			}
27847c478bd9Sstevel@tonic-gate 		} else {
27857c478bd9Sstevel@tonic-gate 			sec_hash_obj->prev->next = sec_hash_obj->next;
27867c478bd9Sstevel@tonic-gate 			if (sec_hash_obj->next != NULL) {
27877c478bd9Sstevel@tonic-gate 				sec_hash_obj->next->prev = sec_hash_obj->prev;
27887c478bd9Sstevel@tonic-gate 			}
27897c478bd9Sstevel@tonic-gate 		}
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 		prev_hash = sec_hash_obj;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		sec_hash_obj = sec_hash_obj->u.sec_obj->next;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		free(prev_hash->u.sec_obj); /* free section hash object */
27967c478bd9Sstevel@tonic-gate 		free(prev_hash); /* free section hash */
27977c478bd9Sstevel@tonic-gate 	}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	/* free container hash object */
28007c478bd9Sstevel@tonic-gate 	if (hash_obj->prev == NULL) {
28017c478bd9Sstevel@tonic-gate 		hash_table[(hash_obj->obj_hdl % TABLE_SIZE)] = hash_obj->next;
28027c478bd9Sstevel@tonic-gate 		if (hash_obj->next != NULL) {
28037c478bd9Sstevel@tonic-gate 			hash_obj->next->prev = NULL;
28047c478bd9Sstevel@tonic-gate 		}
28057c478bd9Sstevel@tonic-gate 	} else {
28067c478bd9Sstevel@tonic-gate 		hash_obj->prev->next = hash_obj->next;
28077c478bd9Sstevel@tonic-gate 		if (hash_obj->next != NULL) {
28087c478bd9Sstevel@tonic-gate 			hash_obj->next->prev = hash_obj->prev;
28097c478bd9Sstevel@tonic-gate 		}
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	free(hash_obj->u.cont_obj);
28137c478bd9Sstevel@tonic-gate 	free(hash_obj);
28147c478bd9Sstevel@tonic-gate 	return (0);
28157c478bd9Sstevel@tonic-gate }
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate /*
28187c478bd9Sstevel@tonic-gate  * Description :
28197c478bd9Sstevel@tonic-gate  *		fru_is_data_available() checks to see if the frudata
28207c478bd9Sstevel@tonic-gate  *		is available on a fru.
28217c478bd9Sstevel@tonic-gate  *
28227c478bd9Sstevel@tonic-gate  * Arguments   :
28237c478bd9Sstevel@tonic-gate  *		picl_nodehdl_t holds the picl node handle of the fru.
28247c478bd9Sstevel@tonic-gate  *
28257c478bd9Sstevel@tonic-gate  * Return      :
28267c478bd9Sstevel@tonic-gate  *		int
28277c478bd9Sstevel@tonic-gate  *		return 1: if FRUID information is available
28287c478bd9Sstevel@tonic-gate  *		return 0: if FRUID information is not present
28297c478bd9Sstevel@tonic-gate  *
28307c478bd9Sstevel@tonic-gate  */
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate /* ARGSUSED */
28337c478bd9Sstevel@tonic-gate int
fru_is_data_available(picl_nodehdl_t fru)28347c478bd9Sstevel@tonic-gate fru_is_data_available(picl_nodehdl_t fru)
28357c478bd9Sstevel@tonic-gate {
28367c478bd9Sstevel@tonic-gate 	return (0);
28377c478bd9Sstevel@tonic-gate }
2838