1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <limits.h>
30*0Sstevel@tonic-gate #include <alloca.h>
31*0Sstevel@tonic-gate #include "fru_access_impl.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #pragma init(initialize_fruaccess)	/* .init section */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate static	hash_obj_t	*hash_table[TABLE_SIZE];
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * seeprom is the driver_name for the SEEPROM device drivers in excalibur
39*0Sstevel@tonic-gate  * Define the devfsadm command to load the seeprom drivers if open fails.
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static	char	devfsadm_cmd[] = "/usr/sbin/devfsadm -i seeprom";
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /* this routine initialize the hash table. */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate static void
initialize_fruaccess(void)47*0Sstevel@tonic-gate initialize_fruaccess(void)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	int	count;
50*0Sstevel@tonic-gate 	for (count = 0; count < TABLE_SIZE; count++) {
51*0Sstevel@tonic-gate 		hash_table[count] = NULL;
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * called to lookup hash object for specified handle in the hash table.
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static hash_obj_t *
lookup_handle_object(handle_t handle,int object_type)61*0Sstevel@tonic-gate lookup_handle_object(handle_t	handle, int object_type)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	handle_t	index_to_hash;
64*0Sstevel@tonic-gate 	hash_obj_t	*first_hash_obj;
65*0Sstevel@tonic-gate 	hash_obj_t	*next_hash_obj;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	index_to_hash	= (handle % TABLE_SIZE);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	first_hash_obj = hash_table[index_to_hash];
70*0Sstevel@tonic-gate 	for (next_hash_obj = first_hash_obj; next_hash_obj != NULL;
71*0Sstevel@tonic-gate 				next_hash_obj = next_hash_obj->next) {
72*0Sstevel@tonic-gate 		if ((handle == next_hash_obj->obj_hdl) &&
73*0Sstevel@tonic-gate 				(object_type == next_hash_obj->object_type)) {
74*0Sstevel@tonic-gate 				return (next_hash_obj);
75*0Sstevel@tonic-gate 		}
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate 	return (NULL);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /* called to allocate container hash object */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static hash_obj_t *
create_container_hash_object(void)83*0Sstevel@tonic-gate create_container_hash_object(void)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
86*0Sstevel@tonic-gate 	container_obj_t		*cont_obj;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	cont_obj	= malloc(sizeof (container_obj_t));
89*0Sstevel@tonic-gate 	if (cont_obj == NULL) {
90*0Sstevel@tonic-gate 		return (NULL);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
94*0Sstevel@tonic-gate 	if (hash_obj == NULL) {
95*0Sstevel@tonic-gate 		free(cont_obj);
96*0Sstevel@tonic-gate 		return (NULL);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	cont_obj->sec_obj_list	= NULL;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	hash_obj->object_type	= CONTAINER_TYPE;
102*0Sstevel@tonic-gate 	hash_obj->u.cont_obj	= cont_obj;
103*0Sstevel@tonic-gate 	hash_obj->next	= NULL;
104*0Sstevel@tonic-gate 	hash_obj->prev	= NULL;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	return (hash_obj);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /* called to allocate section hash object */
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static hash_obj_t *
create_section_hash_object(void)112*0Sstevel@tonic-gate create_section_hash_object(void)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
115*0Sstevel@tonic-gate 	section_obj_t		*sec_obj;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	sec_obj	= malloc(sizeof (section_obj_t));
118*0Sstevel@tonic-gate 	if (sec_obj == NULL) {
119*0Sstevel@tonic-gate 		return (NULL);
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
123*0Sstevel@tonic-gate 	if (hash_obj == NULL) {
124*0Sstevel@tonic-gate 		free(sec_obj);
125*0Sstevel@tonic-gate 		return (NULL);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	sec_obj->next		= NULL;
129*0Sstevel@tonic-gate 	sec_obj->seg_obj_list	= NULL;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	hash_obj->u.sec_obj	= sec_obj;
132*0Sstevel@tonic-gate 	hash_obj->object_type	= SECTION_TYPE;
133*0Sstevel@tonic-gate 	hash_obj->next		= NULL;
134*0Sstevel@tonic-gate 	hash_obj->prev		= NULL;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	return (hash_obj);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /* called to allocate segment hash object */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static hash_obj_t *
create_segment_hash_object(void)142*0Sstevel@tonic-gate create_segment_hash_object(void)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
145*0Sstevel@tonic-gate 	segment_obj_t		*seg_obj;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	seg_obj	= malloc(sizeof (segment_obj_t));
148*0Sstevel@tonic-gate 	if (seg_obj == NULL) {
149*0Sstevel@tonic-gate 		return (NULL);
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	hash_obj = malloc(sizeof (hash_obj_t));
153*0Sstevel@tonic-gate 	if (hash_obj == NULL) {
154*0Sstevel@tonic-gate 		free(seg_obj);
155*0Sstevel@tonic-gate 		return (NULL);
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	seg_obj->next		= NULL;
159*0Sstevel@tonic-gate 	seg_obj->pkt_obj_list	= NULL;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	hash_obj->object_type	= SEGMENT_TYPE;
162*0Sstevel@tonic-gate 	hash_obj->u.seg_obj	= seg_obj;
163*0Sstevel@tonic-gate 	hash_obj->next		= NULL;
164*0Sstevel@tonic-gate 	hash_obj->prev		= NULL;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	return (hash_obj);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate /* called to allocate packet hash object */
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate static hash_obj_t *
create_packet_hash_object(void)172*0Sstevel@tonic-gate create_packet_hash_object(void)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	hash_obj_t		*hash_obj;
175*0Sstevel@tonic-gate 	packet_obj_t		*pkt_obj;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	pkt_obj	= malloc(sizeof (packet_obj_t));
178*0Sstevel@tonic-gate 	if (pkt_obj == NULL) {
179*0Sstevel@tonic-gate 		return (NULL);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	hash_obj	= malloc(sizeof (hash_obj_t));
183*0Sstevel@tonic-gate 	if (hash_obj == NULL) {
184*0Sstevel@tonic-gate 		free(pkt_obj);
185*0Sstevel@tonic-gate 		return (NULL);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	pkt_obj->next		= NULL;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	hash_obj->object_type	= PACKET_TYPE;
191*0Sstevel@tonic-gate 	hash_obj->u.pkt_obj	= pkt_obj;
192*0Sstevel@tonic-gate 	hash_obj->next		= NULL;
193*0Sstevel@tonic-gate 	hash_obj->prev		= NULL;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	return (hash_obj);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /* called to add allocated hash object into the hash table */
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static void
add_hashobject_to_hashtable(hash_obj_t * hash_obj)201*0Sstevel@tonic-gate add_hashobject_to_hashtable(hash_obj_t *hash_obj)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	handle_t		index_to_hash;
204*0Sstevel@tonic-gate 	static	uint64_t	handle_count	= 0;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	hash_obj->obj_hdl = ++handle_count;	/* store the handle */
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* where to add ? */
209*0Sstevel@tonic-gate 	index_to_hash	= ((hash_obj->obj_hdl) % TABLE_SIZE);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	hash_obj->next	= hash_table[index_to_hash];
212*0Sstevel@tonic-gate 	hash_table[index_to_hash] = hash_obj;	/* hash obj. added */
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if (hash_obj->next != NULL) {
215*0Sstevel@tonic-gate 		hash_obj->next->prev = hash_obj;
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate /* called to add section object list into the section list */
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate static void
add_to_sec_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)222*0Sstevel@tonic-gate add_to_sec_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
223*0Sstevel@tonic-gate {
224*0Sstevel@tonic-gate 	hash_obj_t	*next_hash;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	child_obj->u.sec_obj->cont_hdl = parent_obj->obj_hdl;
227*0Sstevel@tonic-gate 	if (parent_obj->u.cont_obj->sec_obj_list == NULL) {
228*0Sstevel@tonic-gate 		parent_obj->u.cont_obj->sec_obj_list = child_obj;
229*0Sstevel@tonic-gate 		return;
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	for (next_hash = parent_obj->u.cont_obj->sec_obj_list;
233*0Sstevel@tonic-gate 			next_hash->u.sec_obj->next != NULL;
234*0Sstevel@tonic-gate 			next_hash = next_hash->u.sec_obj->next) {
235*0Sstevel@tonic-gate 		;
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	next_hash->u.sec_obj->next	= child_obj;
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /* called to add segment object list into segment list */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate static void
add_to_seg_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)244*0Sstevel@tonic-gate add_to_seg_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	hash_obj_t	*next_hash;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	child_obj->u.seg_obj->section_hdl = parent_obj->obj_hdl;
249*0Sstevel@tonic-gate 	if (parent_obj->u.sec_obj->seg_obj_list == NULL) {
250*0Sstevel@tonic-gate 		parent_obj->u.sec_obj->seg_obj_list = child_obj;
251*0Sstevel@tonic-gate 		return;
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	for (next_hash = parent_obj->u.sec_obj->seg_obj_list;
255*0Sstevel@tonic-gate 			next_hash->u.seg_obj->next != NULL;
256*0Sstevel@tonic-gate 			next_hash = next_hash->u.seg_obj->next) {
257*0Sstevel@tonic-gate 		;
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	next_hash->u.seg_obj->next	= child_obj;
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate /* called to add packet object list into packet list */
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate static void
add_to_pkt_object_list(hash_obj_t * parent_obj,hash_obj_t * child_obj)266*0Sstevel@tonic-gate add_to_pkt_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	hash_obj_t	*next_hash;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/* add the packet object in the end of list */
271*0Sstevel@tonic-gate 	child_obj->u.pkt_obj->segment_hdl = parent_obj->obj_hdl;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if (parent_obj->u.seg_obj->pkt_obj_list == NULL) {
274*0Sstevel@tonic-gate 		parent_obj->u.seg_obj->pkt_obj_list = child_obj;
275*0Sstevel@tonic-gate 		return;
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	for (next_hash = parent_obj->u.seg_obj->pkt_obj_list;
279*0Sstevel@tonic-gate 			next_hash->u.pkt_obj->next != NULL;
280*0Sstevel@tonic-gate 			next_hash = next_hash->u.pkt_obj->next) {
281*0Sstevel@tonic-gate 			;
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	next_hash->u.pkt_obj->next = child_obj;
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate static void
copy_segment_layout(segment_t * seghdr,void * layout)288*0Sstevel@tonic-gate copy_segment_layout(segment_t	*seghdr, void	*layout)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	segment_layout_t	*seg_layout;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	seg_layout	= (segment_layout_t *)layout;
293*0Sstevel@tonic-gate 	(void) memcpy(seghdr->name, &seg_layout->name, SEG_NAME_LEN);
294*0Sstevel@tonic-gate 	seghdr->descriptor = GET_SEGMENT_DESCRIPTOR;
295*0Sstevel@tonic-gate 	seghdr->offset	= seg_layout->offset;
296*0Sstevel@tonic-gate 	seghdr->length	= seg_layout->length;
297*0Sstevel@tonic-gate }
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate static hash_obj_t *
get_container_hash_object(int object_type,handle_t handle)300*0Sstevel@tonic-gate get_container_hash_object(int	object_type, handle_t	handle)
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate 	hash_obj_t	*hash_obj;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	switch (object_type) {
305*0Sstevel@tonic-gate 	case	CONTAINER_TYPE	:
306*0Sstevel@tonic-gate 		break;
307*0Sstevel@tonic-gate 	case	SECTION_TYPE	:
308*0Sstevel@tonic-gate 		hash_obj = lookup_handle_object(handle, CONTAINER_TYPE);
309*0Sstevel@tonic-gate 		if (hash_obj == NULL) {
310*0Sstevel@tonic-gate 			return (NULL);
311*0Sstevel@tonic-gate 		}
312*0Sstevel@tonic-gate 		break;
313*0Sstevel@tonic-gate 	case	SEGMENT_TYPE	:
314*0Sstevel@tonic-gate 		hash_obj = lookup_handle_object(handle, SECTION_TYPE);
315*0Sstevel@tonic-gate 		if (hash_obj == NULL) {
316*0Sstevel@tonic-gate 			return (NULL);
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 		hash_obj = lookup_handle_object(hash_obj->u.sec_obj->cont_hdl,
319*0Sstevel@tonic-gate 							CONTAINER_TYPE);
320*0Sstevel@tonic-gate 		break;
321*0Sstevel@tonic-gate 	case	PACKET_TYPE	:
322*0Sstevel@tonic-gate 		break;
323*0Sstevel@tonic-gate 	default	:
324*0Sstevel@tonic-gate 		return (NULL);
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 	return (hash_obj);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate static void
sort_offsettbl(int segcnt,seg_info_t * offset_tbl)331*0Sstevel@tonic-gate sort_offsettbl(int	segcnt, seg_info_t	*offset_tbl)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate 	int		cntx;
334*0Sstevel@tonic-gate 	int		cnty;
335*0Sstevel@tonic-gate 	seg_info_t	tmp;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	for (cntx = 0; cntx < segcnt+2; cntx++) {
338*0Sstevel@tonic-gate 		for (cnty = cntx+1; cnty < segcnt + 2; cnty++) {
339*0Sstevel@tonic-gate 			if (offset_tbl[cntx].offset >
340*0Sstevel@tonic-gate 					offset_tbl[cnty].offset) {
341*0Sstevel@tonic-gate 				(void) memcpy(&tmp, &offset_tbl[cnty],
342*0Sstevel@tonic-gate 						sizeof (seg_info_t));
343*0Sstevel@tonic-gate 				(void) memcpy(&offset_tbl[cnty],
344*0Sstevel@tonic-gate 				&offset_tbl[cntx], sizeof (seg_info_t));
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 				(void) memcpy(&offset_tbl[cntx], &tmp,
347*0Sstevel@tonic-gate 						sizeof (seg_info_t));
348*0Sstevel@tonic-gate 			}
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate /*
354*0Sstevel@tonic-gate  * Description : move_segment_data() reads the segment data and writes it
355*0Sstevel@tonic-gate  *      back to the new segment offset.
356*0Sstevel@tonic-gate  */
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate static void
move_segment_data(void * seghdr,int newoffset,container_hdl_t contfd)359*0Sstevel@tonic-gate move_segment_data(void *seghdr, int newoffset, container_hdl_t contfd)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	int			ret;
362*0Sstevel@tonic-gate 	char			*buffer;
363*0Sstevel@tonic-gate 	segment_layout_t	*segment;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	segment	= (segment_layout_t *)seghdr;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	buffer = alloca(segment->length);
368*0Sstevel@tonic-gate 	if (buffer == NULL) {
369*0Sstevel@tonic-gate 		return;
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	ret = pread(contfd, buffer,
373*0Sstevel@tonic-gate 			segment->length, segment->offset);
374*0Sstevel@tonic-gate 	if (ret != segment->length) {
375*0Sstevel@tonic-gate 		return;
376*0Sstevel@tonic-gate 	}
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	segment->offset = newoffset;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	ret = pwrite(contfd, buffer, segment->length, segment->offset);
381*0Sstevel@tonic-gate 	if (ret != segment->length) {
382*0Sstevel@tonic-gate 		return;
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * Description : pack_segment_data() moves the segment data if there is
388*0Sstevel@tonic-gate  *              a hole between two segments.
389*0Sstevel@tonic-gate  */
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate static void
pack_segment_data(char * seghdr,int segcnt,container_hdl_t contfd,seg_info_t * offset_tbl)392*0Sstevel@tonic-gate pack_segment_data(char *seghdr, int segcnt, container_hdl_t contfd,
393*0Sstevel@tonic-gate 						seg_info_t *offset_tbl)
394*0Sstevel@tonic-gate {
395*0Sstevel@tonic-gate 	int	cnt;
396*0Sstevel@tonic-gate 	int	diff;
397*0Sstevel@tonic-gate 	int	newoffset;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	for (cnt = segcnt + 1; cnt > 0; cnt--) {
400*0Sstevel@tonic-gate 		if (!offset_tbl[cnt - 1].fixed) {
401*0Sstevel@tonic-gate 			if (offset_tbl[cnt].offset
402*0Sstevel@tonic-gate 				- (offset_tbl[cnt -1 ].offset
403*0Sstevel@tonic-gate 					+ offset_tbl[cnt - 1].length) > 0) {
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 				diff = offset_tbl[cnt].offset -
406*0Sstevel@tonic-gate 					(offset_tbl[cnt - 1].offset
407*0Sstevel@tonic-gate 					+ offset_tbl[cnt - 1].length);
408*0Sstevel@tonic-gate 				newoffset = offset_tbl[cnt - 1].offset
409*0Sstevel@tonic-gate 								+ diff;
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 				move_segment_data(seghdr, newoffset,
412*0Sstevel@tonic-gate 								contfd);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 				offset_tbl[cnt - 1].offset = newoffset;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 				sort_offsettbl(segcnt, offset_tbl);
417*0Sstevel@tonic-gate 			}
418*0Sstevel@tonic-gate 		}
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate }
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate /*
423*0Sstevel@tonic-gate  * Description : build_offset_tbl() builds the offset table by reading all the
424*0Sstevel@tonic-gate  *              segment header. it makes two more entry into the table one for
425*0Sstevel@tonic-gate  *              section size and another with start of the section after the
426*0Sstevel@tonic-gate  *              segment header.
427*0Sstevel@tonic-gate  */
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate static int
build_offset_tbl(void * seghdr,int segcnt,int secsize,seg_info_t * offset_tbl)430*0Sstevel@tonic-gate build_offset_tbl(void   *seghdr, int segcnt, int secsize,
431*0Sstevel@tonic-gate 						seg_info_t *offset_tbl)
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	int			cnt;
434*0Sstevel@tonic-gate 	fru_segdesc_t		segdesc;
435*0Sstevel@tonic-gate 	segment_layout_t	*segment;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	for (cnt = 0; cnt < segcnt; cnt++) {
438*0Sstevel@tonic-gate 		segment	= (segment_layout_t *)(seghdr) + cnt;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 		(void) memcpy(&segdesc, &segment->descriptor,
441*0Sstevel@tonic-gate 						sizeof (uint32_t));
442*0Sstevel@tonic-gate 		offset_tbl[cnt].segnum = cnt;
443*0Sstevel@tonic-gate 		offset_tbl[cnt].offset = segment->offset;
444*0Sstevel@tonic-gate 		offset_tbl[cnt].length = segment->length;
445*0Sstevel@tonic-gate 		offset_tbl[cnt].fixed = segdesc.field.fixed;
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	/* upper boundary of segment area (lower address bytes) */
449*0Sstevel@tonic-gate 	offset_tbl[cnt].segnum = -1;
450*0Sstevel@tonic-gate 	offset_tbl[cnt].offset = sizeof (section_layout_t) + ((cnt + 1)
451*0Sstevel@tonic-gate 					* sizeof (segment_layout_t));
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	offset_tbl[cnt].length = 0;
454*0Sstevel@tonic-gate 	offset_tbl[cnt].fixed  = 1;
455*0Sstevel@tonic-gate 	/* lower boundary of segment area (higher address bytes) */
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	offset_tbl[cnt+1].segnum = -1;
458*0Sstevel@tonic-gate 	offset_tbl[cnt+1].offset = secsize;
459*0Sstevel@tonic-gate 	offset_tbl[cnt+1].length = 0;
460*0Sstevel@tonic-gate 	offset_tbl[cnt+1].fixed = 1;
461*0Sstevel@tonic-gate 	return (0);
462*0Sstevel@tonic-gate }
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate static int
hole_discovery(int bytes,int segcnt,int * totsize,seg_info_t * offset_tbl)465*0Sstevel@tonic-gate hole_discovery(int bytes, int segcnt, int *totsize, seg_info_t *offset_tbl)
466*0Sstevel@tonic-gate {
467*0Sstevel@tonic-gate 	int cnt = 0;
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	*totsize = 0;
470*0Sstevel@tonic-gate 	for (cnt = segcnt + 1; cnt > 0; cnt--) {
471*0Sstevel@tonic-gate 		if (bytes <= offset_tbl[cnt].offset -
472*0Sstevel@tonic-gate 				(offset_tbl[cnt - 1].offset +
473*0Sstevel@tonic-gate 					offset_tbl[cnt - 1].length)) {
474*0Sstevel@tonic-gate 			return (offset_tbl[cnt].offset - bytes);
475*0Sstevel@tonic-gate 		}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		*totsize += offset_tbl[cnt].offset -
478*0Sstevel@tonic-gate 				(offset_tbl[cnt - 1].offset +
479*0Sstevel@tonic-gate 					offset_tbl[cnt - 1].length);
480*0Sstevel@tonic-gate 	}
481*0Sstevel@tonic-gate 	return (0);
482*0Sstevel@tonic-gate }
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate /*
486*0Sstevel@tonic-gate  * Description : segment_hdr_present() verify space for new segment header to
487*0Sstevel@tonic-gate  *              be added.
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate static int
segment_hdr_present(int segoffset,int size,seg_info_t * offset_tbl)491*0Sstevel@tonic-gate segment_hdr_present(int segoffset, int size, seg_info_t *offset_tbl)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 	if ((segoffset + size) <= offset_tbl[0].offset)
494*0Sstevel@tonic-gate 		return (0);
495*0Sstevel@tonic-gate 	else
496*0Sstevel@tonic-gate 		return (-1);
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate /*
500*0Sstevel@tonic-gate  * Description : find_offset() is called from fru_add_segment routine to find
501*0Sstevel@tonic-gate  *              a valid offset.
502*0Sstevel@tonic-gate  */
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate static int
find_offset(char * seghdr,int segcnt,int secsize,int * sectionoffset,int segsize,int fix,container_hdl_t contfd)505*0Sstevel@tonic-gate find_offset(char *seghdr, int segcnt, int secsize, int *sectionoffset,
506*0Sstevel@tonic-gate 		int segsize, int fix, container_hdl_t contfd)
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate 	int		ret;
509*0Sstevel@tonic-gate 	int		newoffset;
510*0Sstevel@tonic-gate 	int		totsize = 0;
511*0Sstevel@tonic-gate 	seg_info_t	*offset_tbl;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (segcnt == 0) {
514*0Sstevel@tonic-gate 		if (!fix) {	/* if not fixed segment */
515*0Sstevel@tonic-gate 			*sectionoffset = secsize - segsize;
516*0Sstevel@tonic-gate 		}
517*0Sstevel@tonic-gate 		return (0);
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	/*
521*0Sstevel@tonic-gate 	 * two extra segment info structure are allocated for start of segment
522*0Sstevel@tonic-gate 	 * and other end of segment. first segment offset is first available
523*0Sstevel@tonic-gate 	 * space and length is 0. second segment offset is is segment length and
524*0Sstevel@tonic-gate 	 * offset is 0. build_offset_tbl() explains how upper boundary and lower
525*0Sstevel@tonic-gate 	 * boudary segment area are initialized in seg_info_t table.
526*0Sstevel@tonic-gate 	 */
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	offset_tbl    = malloc((segcnt + 2) * sizeof (seg_info_t));
529*0Sstevel@tonic-gate 	if (offset_tbl == NULL) {
530*0Sstevel@tonic-gate 		return (-1);
531*0Sstevel@tonic-gate 	}
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	/* read all the segment header to make offset table */
534*0Sstevel@tonic-gate 	ret = build_offset_tbl(seghdr, segcnt, secsize, offset_tbl);
535*0Sstevel@tonic-gate 	if (ret != 0) {
536*0Sstevel@tonic-gate 		free(offset_tbl);
537*0Sstevel@tonic-gate 		return (-1);
538*0Sstevel@tonic-gate 	}
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	/* sort the table */
541*0Sstevel@tonic-gate 	sort_offsettbl(segcnt, offset_tbl);
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate 	/* new segment header offset */
544*0Sstevel@tonic-gate 	newoffset = sizeof (section_layout_t) + segcnt *
545*0Sstevel@tonic-gate 						sizeof (segment_layout_t);
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 	/* do? new segment header overlap any existing data */
548*0Sstevel@tonic-gate 	ret = segment_hdr_present(newoffset, sizeof (segment_layout_t),
549*0Sstevel@tonic-gate 								offset_tbl);
550*0Sstevel@tonic-gate 	if (ret != 0) { /* make room for new segment if possible */
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	/* look for hole in order to move segment data */
553*0Sstevel@tonic-gate 		if (offset_tbl[0].fixed == SEGMENT_FIXED) { /* fixed segment */
554*0Sstevel@tonic-gate 			free(offset_tbl);
555*0Sstevel@tonic-gate 			return (-1);
556*0Sstevel@tonic-gate 		}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 		newoffset = hole_discovery(offset_tbl[0].length,
559*0Sstevel@tonic-gate 					segcnt, &totsize, offset_tbl);
560*0Sstevel@tonic-gate 		if (newoffset != 0) { /* found new offset */
561*0Sstevel@tonic-gate 				/* now new offset */
562*0Sstevel@tonic-gate 			offset_tbl[0].offset = newoffset;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 			/* move the segment data */
565*0Sstevel@tonic-gate 			move_segment_data(seghdr, newoffset, contfd);
566*0Sstevel@tonic-gate 			/* again sort the offset table */
567*0Sstevel@tonic-gate 			sort_offsettbl(segcnt, offset_tbl);
568*0Sstevel@tonic-gate 		} else {
569*0Sstevel@tonic-gate 			/* pack the existing hole */
570*0Sstevel@tonic-gate 			if (totsize > offset_tbl[0].length) {
571*0Sstevel@tonic-gate 				pack_segment_data(seghdr, segcnt,
572*0Sstevel@tonic-gate 							contfd, offset_tbl);
573*0Sstevel@tonic-gate 			} else {
574*0Sstevel@tonic-gate 				free(offset_tbl);
575*0Sstevel@tonic-gate 				return (-1);
576*0Sstevel@tonic-gate 			}
577*0Sstevel@tonic-gate 		}
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	totsize = 0;
581*0Sstevel@tonic-gate 	newoffset = hole_discovery(segsize, segcnt, &totsize, offset_tbl);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	if (newoffset == 0) { /* No hole found */
584*0Sstevel@tonic-gate 		if (totsize >= segsize) {
585*0Sstevel@tonic-gate 			pack_segment_data(seghdr, segcnt, contfd,
586*0Sstevel@tonic-gate 								offset_tbl);
587*0Sstevel@tonic-gate 			newoffset = hole_discovery(segsize, segcnt,
588*0Sstevel@tonic-gate 							&totsize, offset_tbl);
589*0Sstevel@tonic-gate 			if (newoffset != 0) {
590*0Sstevel@tonic-gate 				*sectionoffset = newoffset;
591*0Sstevel@tonic-gate 				free(offset_tbl);
592*0Sstevel@tonic-gate 				return (0);
593*0Sstevel@tonic-gate 			}
594*0Sstevel@tonic-gate 		}
595*0Sstevel@tonic-gate 	} else {
596*0Sstevel@tonic-gate 		*sectionoffset = newoffset;
597*0Sstevel@tonic-gate 		free(offset_tbl);
598*0Sstevel@tonic-gate 		return (0);
599*0Sstevel@tonic-gate 	}
600*0Sstevel@tonic-gate 	free(offset_tbl);
601*0Sstevel@tonic-gate 	return (-1);
602*0Sstevel@tonic-gate }
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate static char *
tokenizer(char * buf,char * separator,char ** nextBuf,char * matched)605*0Sstevel@tonic-gate tokenizer(char *buf, char *separator, char **nextBuf, char *matched)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate 	int i = 0;
608*0Sstevel@tonic-gate 	int j = 0;
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	for (i = 0; buf[i] != '\0'; i++) {
611*0Sstevel@tonic-gate 		for (j = 0; j < strlen(separator); j++) {
612*0Sstevel@tonic-gate 			if (buf[i] == separator[j]) {
613*0Sstevel@tonic-gate 				buf[i] = '\0';
614*0Sstevel@tonic-gate 				*nextBuf = &(buf[i+1]);
615*0Sstevel@tonic-gate 				*matched = separator[j];
616*0Sstevel@tonic-gate 				return (buf);
617*0Sstevel@tonic-gate 			}
618*0Sstevel@tonic-gate 		}
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	*nextBuf = buf;
622*0Sstevel@tonic-gate 	*matched = '\0';
623*0Sstevel@tonic-gate 	return (NULL);
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate static int
get_container_info(const char * def_file,const char * cont_desc_str,container_info_t * cont_info)627*0Sstevel@tonic-gate get_container_info(const char *def_file, const char *cont_desc_str,
628*0Sstevel@tonic-gate 						container_info_t *cont_info)
629*0Sstevel@tonic-gate {
630*0Sstevel@tonic-gate 	char	*item;
631*0Sstevel@tonic-gate 	char	*token;
632*0Sstevel@tonic-gate 	char	*field;
633*0Sstevel@tonic-gate 	char	matched;
634*0Sstevel@tonic-gate 	char	buf[1024];
635*0Sstevel@tonic-gate 	int	foundIt = 0;
636*0Sstevel@tonic-gate 	FILE	*file = fopen(def_file, "r");
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	if (file == NULL)
639*0Sstevel@tonic-gate 		return (-1);
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	cont_info->num_sections = 0;
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), file) != NULL) {
644*0Sstevel@tonic-gate 		/* ignore all comments */
645*0Sstevel@tonic-gate 		token = tokenizer(buf, "#", &field, &matched);
646*0Sstevel@tonic-gate 		/* find the names */
647*0Sstevel@tonic-gate 		token = tokenizer(buf, ":", &field, &matched);
648*0Sstevel@tonic-gate 		if (token != 0x00) {
649*0Sstevel@tonic-gate 			token = tokenizer(token, "|", &item, &matched);
650*0Sstevel@tonic-gate 			while (token != 0x00) {
651*0Sstevel@tonic-gate 				if (strcmp(token, cont_desc_str) == 0) {
652*0Sstevel@tonic-gate 					foundIt = 1;
653*0Sstevel@tonic-gate 					goto found;
654*0Sstevel@tonic-gate 				}
655*0Sstevel@tonic-gate 				token = tokenizer(item, "|", &item, &matched);
656*0Sstevel@tonic-gate 			}
657*0Sstevel@tonic-gate 			/* check the last remaining item */
658*0Sstevel@tonic-gate 			if ((item != 0x00) &&
659*0Sstevel@tonic-gate 					(strcmp(item, cont_desc_str) == 0)) {
660*0Sstevel@tonic-gate 				foundIt = 1;
661*0Sstevel@tonic-gate 				goto found;
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 		}
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate found :
667*0Sstevel@tonic-gate 	if (foundIt == 1) {
668*0Sstevel@tonic-gate 		token = tokenizer(field, ":", &field, &matched);
669*0Sstevel@tonic-gate 		if (token == 0x00) {
670*0Sstevel@tonic-gate 			(void) fclose(file);
671*0Sstevel@tonic-gate 			return (-1);
672*0Sstevel@tonic-gate 		}
673*0Sstevel@tonic-gate 		cont_info->header_ver = (headerrev_t)atoi(token);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 		token = tokenizer(field, ":\n", &field, &matched);
676*0Sstevel@tonic-gate 		while (token != 0x00) {
677*0Sstevel@tonic-gate 			token = tokenizer(token, ",", &item, &matched);
678*0Sstevel@tonic-gate 			if (token == 0x00) {
679*0Sstevel@tonic-gate 				(void) fclose(file);
680*0Sstevel@tonic-gate 				return (-1);
681*0Sstevel@tonic-gate 			}
682*0Sstevel@tonic-gate 			if (atoi(token) == 1) {
683*0Sstevel@tonic-gate 				cont_info->section_info[cont_info->
684*0Sstevel@tonic-gate 				num_sections].description.field.read_only = 1;
685*0Sstevel@tonic-gate 			} else if (atoi(token) == 0) {
686*0Sstevel@tonic-gate 				cont_info->section_info[cont_info->
687*0Sstevel@tonic-gate 				num_sections].description.field.read_only = 0;
688*0Sstevel@tonic-gate 			} else {
689*0Sstevel@tonic-gate 				(void) fclose(file);
690*0Sstevel@tonic-gate 				return (-1);
691*0Sstevel@tonic-gate 			}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 			token = tokenizer(item, ",", &item, &matched);
694*0Sstevel@tonic-gate 			if (token == 0x00) {
695*0Sstevel@tonic-gate 				(void) fclose(file);
696*0Sstevel@tonic-gate 				return (-1);
697*0Sstevel@tonic-gate 			}
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 			if (atoi(token) == 1) {
700*0Sstevel@tonic-gate 				cont_info->section_info[cont_info->
701*0Sstevel@tonic-gate 				num_sections].description.field.chk_type = 1;
702*0Sstevel@tonic-gate 			} else if (atoi(token) == 0) {
703*0Sstevel@tonic-gate 				cont_info->section_info[cont_info->
704*0Sstevel@tonic-gate 				num_sections].description.field.chk_type = 0;
705*0Sstevel@tonic-gate 			} else {
706*0Sstevel@tonic-gate 				(void) fclose(file);
707*0Sstevel@tonic-gate 				return (-1);
708*0Sstevel@tonic-gate 			}
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 			token = tokenizer(item, ",", &item, &matched);
712*0Sstevel@tonic-gate 			if (token == 0x00) {
713*0Sstevel@tonic-gate 				(void) fclose(file);
714*0Sstevel@tonic-gate 				return (-1);
715*0Sstevel@tonic-gate 			}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 			cont_info->section_info[cont_info->num_sections].
718*0Sstevel@tonic-gate 					address = atoi(token);
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 			if (item == '\0') {
722*0Sstevel@tonic-gate 				(void) fclose(file);
723*0Sstevel@tonic-gate 				return (-1);
724*0Sstevel@tonic-gate 			}
725*0Sstevel@tonic-gate 			cont_info->section_info[cont_info->num_sections].size =
726*0Sstevel@tonic-gate 								atoi(item);
727*0Sstevel@tonic-gate 			(cont_info->num_sections)++;
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 			token = tokenizer(field, ":\n ", &field, &matched);
730*0Sstevel@tonic-gate 		}
731*0Sstevel@tonic-gate 	}
732*0Sstevel@tonic-gate 	(void) fclose(file);
733*0Sstevel@tonic-gate 	return (0);
734*0Sstevel@tonic-gate }
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate /*
737*0Sstevel@tonic-gate  * Description :fru_open_container() opens the container associated with a fru.
738*0Sstevel@tonic-gate  *              it's called by data plugin module before creating container
739*0Sstevel@tonic-gate  *              property.  it calls picltree library routine to get the
740*0Sstevel@tonic-gate  *              device path and driver binding name for the fru to get the
741*0Sstevel@tonic-gate  *              corresponding fru name that describe the fru layout.
742*0Sstevel@tonic-gate  *
743*0Sstevel@tonic-gate  * Arguments   :picl_hdl_t      fru
744*0Sstevel@tonic-gate  *              A handle for PICL tree node of class "fru" representing the
745*0Sstevel@tonic-gate  *              FRU with the container to open.
746*0Sstevel@tonic-gate  *
747*0Sstevel@tonic-gate  * Return      :
748*0Sstevel@tonic-gate  *              On Success, a Positive integer container handle. is returned
749*0Sstevel@tonic-gate  *              for use in subsequent fru operations;on error, 0 is returned
750*0Sstevel@tonic-gate  *              and "errno" is set appropriately.
751*0Sstevel@tonic-gate  */
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate container_hdl_t
fru_open_container(picl_nodehdl_t fruhdl)754*0Sstevel@tonic-gate fru_open_container(picl_nodehdl_t fruhdl)
755*0Sstevel@tonic-gate {
756*0Sstevel@tonic-gate 	int			retval;
757*0Sstevel@tonic-gate 	int			count;
758*0Sstevel@tonic-gate 	char			*bname;
759*0Sstevel@tonic-gate 	char			devpath[PATH_MAX];
760*0Sstevel@tonic-gate 	hash_obj_t		*cont_hash_obj;
761*0Sstevel@tonic-gate 	hash_obj_t		*sec_hash_obj;
762*0Sstevel@tonic-gate 	picl_nodehdl_t		tmphdl;
763*0Sstevel@tonic-gate 	picl_prophdl_t		prophdl;
764*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
765*0Sstevel@tonic-gate 	container_info_t	cont_info;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	/* Get property handle of _seeprom_source under fru node */
768*0Sstevel@tonic-gate 	retval = ptree_get_propval_by_name(fruhdl, PICL_REFPROP_SEEPROM_SRC,
769*0Sstevel@tonic-gate 						&tmphdl, sizeof (tmphdl));
770*0Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
771*0Sstevel@tonic-gate 		return (NULL);
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	/* Get the device path of the fru */
775*0Sstevel@tonic-gate 	retval = ptree_get_propval_by_name(tmphdl, PICL_PROP_DEVICEPATH,
776*0Sstevel@tonic-gate 							devpath, PATH_MAX);
777*0Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
778*0Sstevel@tonic-gate 		return (NULL);
779*0Sstevel@tonic-gate 	}
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	retval = ptree_get_prop_by_name(tmphdl, PICL_PROP_BINDING_NAME,
782*0Sstevel@tonic-gate 								&prophdl);
783*0Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
784*0Sstevel@tonic-gate 		return (NULL);
785*0Sstevel@tonic-gate 	}
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	retval = ptree_get_propinfo(prophdl, &propinfo);
788*0Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
789*0Sstevel@tonic-gate 		return (NULL);
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	bname = alloca(propinfo.piclinfo.size);
793*0Sstevel@tonic-gate 	if (bname == NULL) {
794*0Sstevel@tonic-gate 		return (NULL);
795*0Sstevel@tonic-gate 	}
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/* get the driver binding name */
798*0Sstevel@tonic-gate 	retval = ptree_get_propval(prophdl, bname, propinfo.piclinfo.size);
799*0Sstevel@tonic-gate 	if (retval != PICL_SUCCESS) {
800*0Sstevel@tonic-gate 		return (NULL);
801*0Sstevel@tonic-gate 	}
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	cont_hash_obj	= create_container_hash_object();
804*0Sstevel@tonic-gate 	if (cont_hash_obj == NULL) {
805*0Sstevel@tonic-gate 		return (NULL);
806*0Sstevel@tonic-gate 	}
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	add_hashobject_to_hashtable(cont_hash_obj);
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	(void) strlcpy(cont_hash_obj->u.cont_obj->device_pathname, devpath,
811*0Sstevel@tonic-gate 							sizeof (devpath));
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	/* takes driver binding name as to get container information */
814*0Sstevel@tonic-gate 	retval = get_container_info(CONTAINER_CONF_FILE, bname, &cont_info);
815*0Sstevel@tonic-gate 	if (retval < 0) {
816*0Sstevel@tonic-gate 		return (NULL);
817*0Sstevel@tonic-gate 	}
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 	cont_hash_obj->u.cont_obj->num_of_section =  cont_info.num_sections;
820*0Sstevel@tonic-gate 	cont_hash_obj->u.cont_obj->sec_obj_list = NULL;
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	for (count = 0; count < cont_info.num_sections; count++) {
823*0Sstevel@tonic-gate 		sec_hash_obj = create_section_hash_object();
824*0Sstevel@tonic-gate 		if (sec_hash_obj == NULL) {
825*0Sstevel@tonic-gate 			return (NULL);
826*0Sstevel@tonic-gate 		}
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 		add_hashobject_to_hashtable(sec_hash_obj);
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.offset =
831*0Sstevel@tonic-gate 				cont_info.section_info[count].address;
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.protection =
834*0Sstevel@tonic-gate 		cont_info.section_info[count].description.field.read_only;
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->checksum_method =
837*0Sstevel@tonic-gate 		cont_info.section_info[count].description.field.chk_type;
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.length =
840*0Sstevel@tonic-gate 					cont_info.section_info[count].size;
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 		sec_hash_obj->u.sec_obj->section.version = cont_info.header_ver;
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 		add_to_sec_object_list(cont_hash_obj, sec_hash_obj);
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate 	return (cont_hash_obj->obj_hdl);
847*0Sstevel@tonic-gate }
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate static int
verify_header_crc8(headerrev_t head_ver,unsigned char * bytes,int length)850*0Sstevel@tonic-gate verify_header_crc8(headerrev_t head_ver, unsigned char *bytes, int length)
851*0Sstevel@tonic-gate {
852*0Sstevel@tonic-gate 	int		crc_offset = 0;
853*0Sstevel@tonic-gate 	unsigned char	orig_crc8 = 0;
854*0Sstevel@tonic-gate 	unsigned char	calc_crc8 = 0;
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	switch (head_ver) {
857*0Sstevel@tonic-gate 		case SECTION_HDR_VER:
858*0Sstevel@tonic-gate 			crc_offset = 4;
859*0Sstevel@tonic-gate 			break;
860*0Sstevel@tonic-gate 		default:
861*0Sstevel@tonic-gate 			errno = EINVAL;
862*0Sstevel@tonic-gate 			return (0);
863*0Sstevel@tonic-gate 	}
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	orig_crc8 = bytes[crc_offset];
866*0Sstevel@tonic-gate 	bytes[crc_offset] = 0x00; /* clear for calc */
867*0Sstevel@tonic-gate 	calc_crc8 = compute_crc8(bytes, length);
868*0Sstevel@tonic-gate 	bytes[crc_offset] = orig_crc8; /* restore */
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	return (orig_crc8 == calc_crc8);
871*0Sstevel@tonic-gate }
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate /*
874*0Sstevel@tonic-gate  * Description	:
875*0Sstevel@tonic-gate  *		fru_get_num_sections() returns number of sections in a
876*0Sstevel@tonic-gate  *		container. it calls get_container_index() to get the container
877*0Sstevel@tonic-gate  *		index number in the container list.
878*0Sstevel@tonic-gate  *
879*0Sstevel@tonic-gate  * Arguments	:
880*0Sstevel@tonic-gate  *		container_hdl_t	: container handle.
881*0Sstevel@tonic-gate  *
882*0Sstevel@tonic-gate  * Return	:
883*0Sstevel@tonic-gate  *		int
884*0Sstevel@tonic-gate  *		On success, returns number of sections in a container.
885*0Sstevel@tonic-gate  *
886*0Sstevel@tonic-gate  */
887*0Sstevel@tonic-gate /*ARGSUSED*/
888*0Sstevel@tonic-gate int
fru_get_num_sections(container_hdl_t container,door_cred_t * cred)889*0Sstevel@tonic-gate fru_get_num_sections(container_hdl_t container, door_cred_t *cred)
890*0Sstevel@tonic-gate {
891*0Sstevel@tonic-gate 	hash_obj_t		*hash_object;
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	hash_object	= lookup_handle_object(container, CONTAINER_TYPE);
894*0Sstevel@tonic-gate 	if (hash_object == NULL) {
895*0Sstevel@tonic-gate 		return (-1);
896*0Sstevel@tonic-gate 	}
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	return (hash_object->u.cont_obj->num_of_section);
899*0Sstevel@tonic-gate }
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate /*
902*0Sstevel@tonic-gate  * called from fru_get_sections()
903*0Sstevel@tonic-gate  */
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate static void
get_section(int fd,hash_obj_t * sec_hash,section_t * section)906*0Sstevel@tonic-gate get_section(int fd, hash_obj_t *sec_hash, section_t *section)
907*0Sstevel@tonic-gate {
908*0Sstevel@tonic-gate 	int			retval;
909*0Sstevel@tonic-gate 	int			size;
910*0Sstevel@tonic-gate 	int			count;
911*0Sstevel@tonic-gate 	uint16_t		hdrver;
912*0Sstevel@tonic-gate 	hash_obj_t		*seg_hash;
913*0Sstevel@tonic-gate 	unsigned char		*buffer;
914*0Sstevel@tonic-gate 	section_obj_t		*sec_obj;
915*0Sstevel@tonic-gate 	section_layout_t	sec_hdr;
916*0Sstevel@tonic-gate 	segment_layout_t	*seg_hdr;
917*0Sstevel@tonic-gate 	segment_layout_t	*seg_buf;
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	sec_obj	= sec_hash->u.sec_obj;
920*0Sstevel@tonic-gate 	if (sec_obj == NULL) {
921*0Sstevel@tonic-gate 		return;
922*0Sstevel@tonic-gate 	}
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	/* populate section_t */
925*0Sstevel@tonic-gate 	section->handle = sec_hash->obj_hdl;
926*0Sstevel@tonic-gate 	section->offset = sec_obj->section.offset;
927*0Sstevel@tonic-gate 	section->length = sec_obj->section.length;
928*0Sstevel@tonic-gate 	section->protection = sec_obj->section.protection;
929*0Sstevel@tonic-gate 	section->version = sec_obj->section.version;
930*0Sstevel@tonic-gate 	sec_obj->num_of_segment	= 0;
931*0Sstevel@tonic-gate 
932*0Sstevel@tonic-gate 	/* read section header layout */
933*0Sstevel@tonic-gate 	retval = pread(fd, &sec_hdr, sizeof (sec_hdr),
934*0Sstevel@tonic-gate 						sec_obj->section.offset);
935*0Sstevel@tonic-gate 	if (retval != sizeof (sec_hdr)) {
936*0Sstevel@tonic-gate 		return;
937*0Sstevel@tonic-gate 	}
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	hdrver	= GET_SECTION_HDR_VERSION;
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	if ((sec_hdr.headertag != SECTION_HDR_TAG) &&
942*0Sstevel@tonic-gate 					(hdrver != section->version)) {
943*0Sstevel@tonic-gate 		return;
944*0Sstevel@tonic-gate 	}
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 	/* size = section layout + total sizeof segment header */
947*0Sstevel@tonic-gate 	size	= sizeof (sec_hdr) + ((sec_hdr.segmentcount)
948*0Sstevel@tonic-gate 					* sizeof (segment_layout_t));
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 	buffer	= alloca(size);
951*0Sstevel@tonic-gate 	if (buffer == NULL) {
952*0Sstevel@tonic-gate 		return;
953*0Sstevel@tonic-gate 	}
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 	/* segment header buffer */
956*0Sstevel@tonic-gate 	seg_buf = alloca(size - sizeof (sec_hdr));
957*0Sstevel@tonic-gate 	if (seg_buf == NULL) {
958*0Sstevel@tonic-gate 		return;
959*0Sstevel@tonic-gate 	}
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate 	/* read segment header */
962*0Sstevel@tonic-gate 	retval = pread(fd, seg_buf, size - sizeof (sec_hdr),
963*0Sstevel@tonic-gate 				sec_obj->section.offset + sizeof (sec_hdr));
964*0Sstevel@tonic-gate 	if (retval != (size - sizeof (sec_hdr))) {
965*0Sstevel@tonic-gate 		return;
966*0Sstevel@tonic-gate 	}
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 	/* copy section header layout */
969*0Sstevel@tonic-gate 	(void) memcpy(buffer, &sec_hdr, sizeof (sec_hdr));
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	/* copy segment header layout */
972*0Sstevel@tonic-gate 	(void) memcpy(buffer + sizeof (sec_hdr), seg_buf, size -
973*0Sstevel@tonic-gate 							sizeof (sec_hdr));
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 	/* verify crc8 */
976*0Sstevel@tonic-gate 	retval = verify_header_crc8(hdrver, buffer, size);
977*0Sstevel@tonic-gate 	if (retval != TRUE) {
978*0Sstevel@tonic-gate 		return;
979*0Sstevel@tonic-gate 	}
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	section->version = hdrver;
983*0Sstevel@tonic-gate 	sec_obj->section.version = hdrver;
984*0Sstevel@tonic-gate 
985*0Sstevel@tonic-gate 	seg_hdr	= (segment_layout_t *)seg_buf;
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 	for (count = 0; count < sec_hdr.segmentcount; count++, seg_hdr++) {
988*0Sstevel@tonic-gate 		seg_hash = create_segment_hash_object();
989*0Sstevel@tonic-gate 		if (seg_hash == NULL) {
990*0Sstevel@tonic-gate 			return;
991*0Sstevel@tonic-gate 		}
992*0Sstevel@tonic-gate 
993*0Sstevel@tonic-gate 		add_hashobject_to_hashtable(seg_hash);
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 		copy_segment_layout(&seg_hash->u.seg_obj->segment, seg_hdr);
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 		add_to_seg_object_list(sec_hash, seg_hash);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 		sec_obj->num_of_segment++;
1000*0Sstevel@tonic-gate 	}
1001*0Sstevel@tonic-gate }
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate static int
call_devfsadm(void)1005*0Sstevel@tonic-gate call_devfsadm(void)
1006*0Sstevel@tonic-gate {
1007*0Sstevel@tonic-gate 	char		*phys_path;
1008*0Sstevel@tonic-gate 	di_node_t	root_node;
1009*0Sstevel@tonic-gate 	di_node_t	prom_node;
1010*0Sstevel@tonic-gate 	di_node_t	f_node;
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
1013*0Sstevel@tonic-gate 		return (-1);
1014*0Sstevel@tonic-gate 	}
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	f_node = di_drv_first_node(PICL_CLASS_SEEPROM, root_node);
1017*0Sstevel@tonic-gate 	if (f_node != DI_NODE_NIL) {
1018*0Sstevel@tonic-gate 		phys_path = di_devfs_path(f_node);
1019*0Sstevel@tonic-gate 		if ((prom_node = di_init(phys_path, DINFOMINOR))
1020*0Sstevel@tonic-gate 							!= DI_NODE_NIL) {
1021*0Sstevel@tonic-gate 			di_fini(prom_node);
1022*0Sstevel@tonic-gate 			di_fini(root_node);
1023*0Sstevel@tonic-gate 			(void) pclose(popen(devfsadm_cmd, "r"));
1024*0Sstevel@tonic-gate 			return (0);
1025*0Sstevel@tonic-gate 		}
1026*0Sstevel@tonic-gate 	}
1027*0Sstevel@tonic-gate 	di_fini(root_node);
1028*0Sstevel@tonic-gate 	return (-1);
1029*0Sstevel@tonic-gate }
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate /*
1032*0Sstevel@tonic-gate  * Description	:
1033*0Sstevel@tonic-gate  *   		fru_get_sections() fills an array of section structures passed
1034*0Sstevel@tonic-gate  *		as an argument.
1035*0Sstevel@tonic-gate  *
1036*0Sstevel@tonic-gate  * Arguments	:
1037*0Sstevel@tonic-gate  *		container_hdl_t : container handle(device descriptor).
1038*0Sstevel@tonic-gate  *		section_t	: array of section structure.
1039*0Sstevel@tonic-gate  *		int		: maximum number of section in a container.
1040*0Sstevel@tonic-gate  *
1041*0Sstevel@tonic-gate  * Returns	:
1042*0Sstevel@tonic-gate  *   		int
1043*0Sstevel@tonic-gate  *     		On success,the number of section structures written is returned;
1044*0Sstevel@tonic-gate  *     		on error, -1 is returned and "errno" is set appropriately.
1045*0Sstevel@tonic-gate  *
1046*0Sstevel@tonic-gate  */
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate /* ARGSUSED */
1049*0Sstevel@tonic-gate int
fru_get_sections(container_hdl_t container,section_t * section,int maxsec,door_cred_t * cred)1050*0Sstevel@tonic-gate fru_get_sections(container_hdl_t container, section_t *section, int maxsec,
1051*0Sstevel@tonic-gate 							door_cred_t *cred)
1052*0Sstevel@tonic-gate {
1053*0Sstevel@tonic-gate 	int		device_fd;
1054*0Sstevel@tonic-gate 	int		retrys = 1;
1055*0Sstevel@tonic-gate 	int		count;
1056*0Sstevel@tonic-gate 	hash_obj_t	*cont_object;
1057*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	cont_object	= lookup_handle_object(container, CONTAINER_TYPE);
1060*0Sstevel@tonic-gate 	if (cont_object == NULL) {
1061*0Sstevel@tonic-gate 		return (-1);
1062*0Sstevel@tonic-gate 	}
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate 	if (cont_object->u.cont_obj->num_of_section > maxsec) {
1065*0Sstevel@tonic-gate 		return (-1);
1066*0Sstevel@tonic-gate 	}
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	sec_hash = cont_object->u.cont_obj->sec_obj_list;
1069*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
1070*0Sstevel@tonic-gate 		return (-1);
1071*0Sstevel@tonic-gate 	}
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	do {
1074*0Sstevel@tonic-gate 		device_fd = open(cont_object->u.cont_obj->device_pathname,
1075*0Sstevel@tonic-gate 								O_RDWR);
1076*0Sstevel@tonic-gate 		if (device_fd >= 0) {
1077*0Sstevel@tonic-gate 			break;
1078*0Sstevel@tonic-gate 		}
1079*0Sstevel@tonic-gate 	} while ((retrys-- > 0) && (call_devfsadm() == 0));
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 	if (device_fd < 0) {
1082*0Sstevel@tonic-gate 		return (-1);
1083*0Sstevel@tonic-gate 	}
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	for (count = 0; count < cont_object->u.cont_obj->num_of_section;
1086*0Sstevel@tonic-gate 						count++, section++) {
1087*0Sstevel@tonic-gate 		section->version = -1;
1088*0Sstevel@tonic-gate 		/* populate section_t */
1089*0Sstevel@tonic-gate 		get_section(device_fd, sec_hash, section);
1090*0Sstevel@tonic-gate 		sec_hash = sec_hash->u.sec_obj->next;
1091*0Sstevel@tonic-gate 	}
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	(void) close(device_fd);
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 	return (count);
1096*0Sstevel@tonic-gate }
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate /*
1099*0Sstevel@tonic-gate  * Description	:
1100*0Sstevel@tonic-gate  * 		fru_get_num_segments() returns the current number of segments
1101*0Sstevel@tonic-gate  *		in a section.
1102*0Sstevel@tonic-gate  *
1103*0Sstevel@tonic-gate  * Arguments	:
1104*0Sstevel@tonic-gate  *		section_hdl_t : section header holding section information.
1105*0Sstevel@tonic-gate  *
1106*0Sstevel@tonic-gate  * Return	:
1107*0Sstevel@tonic-gate  * 		int
1108*0Sstevel@tonic-gate  *     		On success, the number of segments in the argument section is
1109*0Sstevel@tonic-gate  *     		returned; on error -1 is returned.
1110*0Sstevel@tonic-gate  */
1111*0Sstevel@tonic-gate /*ARGSUSED*/
1112*0Sstevel@tonic-gate int
fru_get_num_segments(section_hdl_t section,door_cred_t * cred)1113*0Sstevel@tonic-gate fru_get_num_segments(section_hdl_t section, door_cred_t *cred)
1114*0Sstevel@tonic-gate {
1115*0Sstevel@tonic-gate 	hash_obj_t	*sec_object;
1116*0Sstevel@tonic-gate 	section_obj_t	*sec_obj;
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate 	sec_object	= lookup_handle_object(section, SECTION_TYPE);
1119*0Sstevel@tonic-gate 	if (sec_object == NULL) {
1120*0Sstevel@tonic-gate 		return (-1);
1121*0Sstevel@tonic-gate 	}
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 	sec_obj	= sec_object->u.sec_obj;
1124*0Sstevel@tonic-gate 	if (sec_obj == NULL) {
1125*0Sstevel@tonic-gate 		return (-1);
1126*0Sstevel@tonic-gate 	}
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 	return (sec_obj->num_of_segment);
1129*0Sstevel@tonic-gate }
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate /*
1132*0Sstevel@tonic-gate  * Description	:
1133*0Sstevel@tonic-gate  *		fru_get_segments() fills an array of structures representing the
1134*0Sstevel@tonic-gate  *		segments in a section.
1135*0Sstevel@tonic-gate  *
1136*0Sstevel@tonic-gate  * Arguments	:
1137*0Sstevel@tonic-gate  *		section_hdl_t : holds section number.
1138*0Sstevel@tonic-gate  *		segment_t : on success will hold segment information.
1139*0Sstevel@tonic-gate  *		int	: maximum number of segment.
1140*0Sstevel@tonic-gate  *
1141*0Sstevel@tonic-gate  * Return	:
1142*0Sstevel@tonic-gate  *		int
1143*0Sstevel@tonic-gate  *		On success, the number of segment structures written is
1144*0Sstevel@tonic-gate  *		returned; on errno -1 is returned.
1145*0Sstevel@tonic-gate  */
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate /* ARGSUSED */
1148*0Sstevel@tonic-gate int
fru_get_segments(section_hdl_t section,segment_t * segment,int maxseg,door_cred_t * cred)1149*0Sstevel@tonic-gate fru_get_segments(section_hdl_t section, segment_t *segment, int maxseg,
1150*0Sstevel@tonic-gate 						door_cred_t *cred)
1151*0Sstevel@tonic-gate {
1152*0Sstevel@tonic-gate 	int		count;
1153*0Sstevel@tonic-gate 	hash_obj_t	*sec_object;
1154*0Sstevel@tonic-gate 	hash_obj_t	*seg_object;
1155*0Sstevel@tonic-gate 	section_obj_t	*sec_obj;
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate 	sec_object = lookup_handle_object(section, SECTION_TYPE);
1158*0Sstevel@tonic-gate 	if (sec_object == NULL) {
1159*0Sstevel@tonic-gate 		return (-1);
1160*0Sstevel@tonic-gate 	}
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate 	sec_obj	= sec_object->u.sec_obj;
1163*0Sstevel@tonic-gate 	if (sec_obj == NULL) {
1164*0Sstevel@tonic-gate 		return (-1);
1165*0Sstevel@tonic-gate 	}
1166*0Sstevel@tonic-gate 
1167*0Sstevel@tonic-gate 	if (sec_obj->num_of_segment > maxseg) {
1168*0Sstevel@tonic-gate 		return (-1);
1169*0Sstevel@tonic-gate 	}
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 	seg_object	= sec_object->u.sec_obj->seg_obj_list;
1172*0Sstevel@tonic-gate 	if (seg_object == NULL) {
1173*0Sstevel@tonic-gate 		return (-1);
1174*0Sstevel@tonic-gate 	}
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate 	for (count = 0; count < sec_obj->num_of_segment; count++) {
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate 		/* populate segment_t */
1179*0Sstevel@tonic-gate 		segment->handle = seg_object->obj_hdl;
1180*0Sstevel@tonic-gate 		(void) memcpy(segment->name,
1181*0Sstevel@tonic-gate 			seg_object->u.seg_obj->segment.name, SEG_NAME_LEN);
1182*0Sstevel@tonic-gate 		segment->descriptor = seg_object->u.seg_obj->segment.descriptor;
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 		segment->offset	= seg_object->u.seg_obj->segment.offset;
1185*0Sstevel@tonic-gate 		segment->length	= seg_object->u.seg_obj->segment.length;
1186*0Sstevel@tonic-gate 		seg_object = seg_object->u.seg_obj->next;
1187*0Sstevel@tonic-gate 		segment++;
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate 	return (0);
1190*0Sstevel@tonic-gate }
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate /*
1193*0Sstevel@tonic-gate  * Description	:
1194*0Sstevel@tonic-gate  *		fru_add_segment() adds a segment to a section.
1195*0Sstevel@tonic-gate  *
1196*0Sstevel@tonic-gate  * Arguments	:
1197*0Sstevel@tonic-gate  *		section_hdl_t section
1198*0Sstevel@tonic-gate  *		A handle for the section in which to add the segment.
1199*0Sstevel@tonic-gate  *
1200*0Sstevel@tonic-gate  *		segment_t *segment
1201*0Sstevel@tonic-gate  *		On entry, the "handle" component of "segment" is ignored and the
1202*0Sstevel@tonic-gate  *		remaining components specify the parameters of the segment to be
1203*0Sstevel@tonic-gate  *		added.  On return, the "handle" component is set to the handle
1204*0Sstevel@tonic-gate  *		for the added segment. The segment offset is mandatory for FIXED
1205*0Sstevel@tonic-gate  *		segments; otherwise, the offset is advisory.
1206*0Sstevel@tonic-gate  *
1207*0Sstevel@tonic-gate  * Return	:
1208*0Sstevel@tonic-gate  *		int
1209*0Sstevel@tonic-gate  *		On success, 0 is returned; on error -1 is returned.
1210*0Sstevel@tonic-gate  *
1211*0Sstevel@tonic-gate  */
1212*0Sstevel@tonic-gate 
1213*0Sstevel@tonic-gate int
fru_add_segment(section_hdl_t section,segment_t * segment,section_hdl_t * newsection,door_cred_t * cred)1214*0Sstevel@tonic-gate fru_add_segment(section_hdl_t section, segment_t *segment,
1215*0Sstevel@tonic-gate 				section_hdl_t *newsection, door_cred_t *cred)
1216*0Sstevel@tonic-gate {
1217*0Sstevel@tonic-gate 	int		fd;
1218*0Sstevel@tonic-gate 	int		retval;
1219*0Sstevel@tonic-gate 	int		offset;
1220*0Sstevel@tonic-gate 	int		sec_size;
1221*0Sstevel@tonic-gate 	int		seg_cnt;
1222*0Sstevel@tonic-gate 	int		bufsize;
1223*0Sstevel@tonic-gate 	int		new_seg_offset;
1224*0Sstevel@tonic-gate 	int		new_seg_length;
1225*0Sstevel@tonic-gate 	int		fixed_segment;
1226*0Sstevel@tonic-gate 	char		trailer[]	= { 0x0c, 0x00, 0x00, 0x00, 0x00 };
1227*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
1228*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
1229*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
1230*0Sstevel@tonic-gate 	fru_segdesc_t	*new_seg_desc;
1231*0Sstevel@tonic-gate 	unsigned char 	*crcbuf;
1232*0Sstevel@tonic-gate 	section_layout_t sec_layout;
1233*0Sstevel@tonic-gate 	segment_layout_t *seg_layout;
1234*0Sstevel@tonic-gate 	segment_layout_t *segment_buf;
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 	/* check the effective uid of the client */
1237*0Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
1238*0Sstevel@tonic-gate 		errno = EPERM;
1239*0Sstevel@tonic-gate 		return (-1);	/* not a root */
1240*0Sstevel@tonic-gate 	}
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 	/* section hash */
1243*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(section, SECTION_TYPE);
1244*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
1245*0Sstevel@tonic-gate 		return (-1);
1246*0Sstevel@tonic-gate 	}
1247*0Sstevel@tonic-gate 
1248*0Sstevel@tonic-gate 	/* check for read-only section */
1249*0Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
1250*0Sstevel@tonic-gate 		errno = EPERM;
1251*0Sstevel@tonic-gate 		return (-1);
1252*0Sstevel@tonic-gate 	}
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	/* look for duplicate segment */
1255*0Sstevel@tonic-gate 	seg_hash = sec_hash->u.sec_obj->seg_obj_list;
1256*0Sstevel@tonic-gate 	while (seg_hash != NULL) {
1257*0Sstevel@tonic-gate 		if (strncmp(segment->name, seg_hash->u.seg_obj->segment.name,
1258*0Sstevel@tonic-gate 					SEG_NAME_LEN) == 0) {
1259*0Sstevel@tonic-gate 			errno = EEXIST;
1260*0Sstevel@tonic-gate 			return (-1); /* can't add duplicate segment */
1261*0Sstevel@tonic-gate 		}
1262*0Sstevel@tonic-gate 		seg_hash = seg_hash->u.seg_obj->next;
1263*0Sstevel@tonic-gate 	}
1264*0Sstevel@tonic-gate 
1265*0Sstevel@tonic-gate 	/* get the container hash */
1266*0Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
1267*0Sstevel@tonic-gate 							CONTAINER_TYPE);
1268*0Sstevel@tonic-gate 	if (cont_hash == NULL) {
1269*0Sstevel@tonic-gate 		return (-1);
1270*0Sstevel@tonic-gate 	}
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate 	/* open the container */
1273*0Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
1274*0Sstevel@tonic-gate 	if (fd < 0) {
1275*0Sstevel@tonic-gate 		return (-1);
1276*0Sstevel@tonic-gate 	}
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 	/* section start here */
1279*0Sstevel@tonic-gate 	offset	= sec_hash->u.sec_obj->section.offset;
1280*0Sstevel@tonic-gate 
1281*0Sstevel@tonic-gate 	/* read section header layout */
1282*0Sstevel@tonic-gate 	retval = pread(fd, &sec_layout, sizeof (sec_layout), offset);
1283*0Sstevel@tonic-gate 	if (retval != sizeof (sec_layout)) {
1284*0Sstevel@tonic-gate 		(void) close(fd);
1285*0Sstevel@tonic-gate 		return (-1);
1286*0Sstevel@tonic-gate 	}
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 	/* check for valid section header */
1289*0Sstevel@tonic-gate 	if (sec_layout.headertag != SECTION_HDR_TAG) {
1290*0Sstevel@tonic-gate 		/* write a new one */
1291*0Sstevel@tonic-gate 		sec_layout.headertag		= SECTION_HDR_TAG;
1292*0Sstevel@tonic-gate 		sec_layout.headerversion[0]	= SECTION_HDR_VER_BIT0;
1293*0Sstevel@tonic-gate 		sec_layout.headerversion[1]	= SECTION_HDR_VER_BIT1;
1294*0Sstevel@tonic-gate 		sec_layout.headerlength		= sizeof (sec_layout);
1295*0Sstevel@tonic-gate 		sec_layout.segmentcount		= 0;
1296*0Sstevel@tonic-gate 	}
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 	/* section size */
1299*0Sstevel@tonic-gate 	sec_size	= sec_hash->u.sec_obj->section.length;
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 	/* number of segment in the section */
1302*0Sstevel@tonic-gate 	seg_cnt	= sec_layout.segmentcount;
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	/* total sizeof segment + new segment */
1305*0Sstevel@tonic-gate 	bufsize	=	sizeof (segment_layout_t) * (seg_cnt + 1);
1306*0Sstevel@tonic-gate 	segment_buf = alloca(bufsize);
1307*0Sstevel@tonic-gate 	if (segment_buf == NULL) {
1308*0Sstevel@tonic-gate 		return (-1);
1309*0Sstevel@tonic-gate 	}
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate 	/* read entire segment header */
1312*0Sstevel@tonic-gate 	retval = pread(fd, segment_buf,  (bufsize - sizeof (segment_layout_t)),
1313*0Sstevel@tonic-gate 					offset + sizeof (section_layout_t));
1314*0Sstevel@tonic-gate 	if (retval != (bufsize - sizeof (segment_layout_t))) {
1315*0Sstevel@tonic-gate 		(void) close(fd);
1316*0Sstevel@tonic-gate 		return (-1);
1317*0Sstevel@tonic-gate 	}
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	new_seg_offset	= segment->offset; /* new segment offset */
1320*0Sstevel@tonic-gate 	new_seg_length	= segment->length; /* new segment length */
1321*0Sstevel@tonic-gate 
1322*0Sstevel@tonic-gate 	new_seg_desc	= (fru_segdesc_t *)&segment->descriptor;
1323*0Sstevel@tonic-gate 
1324*0Sstevel@tonic-gate 	fixed_segment	= new_seg_desc->field.fixed;
1325*0Sstevel@tonic-gate 
1326*0Sstevel@tonic-gate 	/* get new offset for new segment to be addedd */
1327*0Sstevel@tonic-gate 	retval = find_offset((char *)segment_buf, seg_cnt, sec_size,
1328*0Sstevel@tonic-gate 			&new_seg_offset, new_seg_length, fixed_segment, fd);
1329*0Sstevel@tonic-gate 
1330*0Sstevel@tonic-gate 	if (retval != 0)	{
1331*0Sstevel@tonic-gate 		(void) close(fd);
1332*0Sstevel@tonic-gate 		errno = EAGAIN;
1333*0Sstevel@tonic-gate 		return (-1);
1334*0Sstevel@tonic-gate 	}
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate 	/* copy new segment data in segment layout */
1337*0Sstevel@tonic-gate 	seg_layout	= (segment_layout_t *)(segment_buf + seg_cnt);
1338*0Sstevel@tonic-gate 	(void) memcpy(&seg_layout->name, segment->name, SEG_NAME_LEN);
1339*0Sstevel@tonic-gate 	(void) memcpy(seg_layout->descriptor, &segment->descriptor,
1340*0Sstevel@tonic-gate 						sizeof (uint32_t));
1341*0Sstevel@tonic-gate 	seg_layout->length	= segment->length;
1342*0Sstevel@tonic-gate 	seg_layout->offset	= new_seg_offset; /* new segment offset */
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 	sec_layout.segmentcount += 1;
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate 	crcbuf	= alloca(sizeof (section_layout_t) + bufsize);
1347*0Sstevel@tonic-gate 	if (crcbuf == NULL) {
1348*0Sstevel@tonic-gate 		(void) close(fd);
1349*0Sstevel@tonic-gate 		return (-1);
1350*0Sstevel@tonic-gate 	}
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate 	sec_layout.headercrc8 = 0;
1353*0Sstevel@tonic-gate 	sec_layout.headerlength += sizeof (segment_layout_t);
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate 	(void) memcpy(crcbuf, (char *)&sec_layout, sizeof (section_layout_t));
1356*0Sstevel@tonic-gate 	(void) memcpy(crcbuf + sizeof (section_layout_t), segment_buf, bufsize);
1357*0Sstevel@tonic-gate 
1358*0Sstevel@tonic-gate 	sec_layout.headercrc8 = compute_crc8(crcbuf, bufsize +
1359*0Sstevel@tonic-gate 						sizeof (section_layout_t));
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 	/* write section header */
1362*0Sstevel@tonic-gate 	retval = pwrite(fd, &sec_layout, sizeof (section_layout_t), offset);
1363*0Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
1364*0Sstevel@tonic-gate 		(void) close(fd);
1365*0Sstevel@tonic-gate 		return (-1);
1366*0Sstevel@tonic-gate 	}
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 	/* write segment header */
1369*0Sstevel@tonic-gate 	retval = pwrite(fd, segment_buf, bufsize, offset +
1370*0Sstevel@tonic-gate 						sizeof (section_layout_t));
1371*0Sstevel@tonic-gate 	if (retval != bufsize) {
1372*0Sstevel@tonic-gate 		(void) close(fd);
1373*0Sstevel@tonic-gate 		return (-1);
1374*0Sstevel@tonic-gate 	}
1375*0Sstevel@tonic-gate 
1376*0Sstevel@tonic-gate 	/* write segment trailer */
1377*0Sstevel@tonic-gate 	retval = pwrite(fd, &trailer, sizeof (trailer), new_seg_offset);
1378*0Sstevel@tonic-gate 	if (retval != sizeof (trailer)) {
1379*0Sstevel@tonic-gate 		(void) close(fd);
1380*0Sstevel@tonic-gate 		return (-1);
1381*0Sstevel@tonic-gate 	}
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	(void) close(fd);
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 	/* create new segment hash object */
1386*0Sstevel@tonic-gate 	seg_hash	= create_segment_hash_object();
1387*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
1388*0Sstevel@tonic-gate 		return (-1);
1389*0Sstevel@tonic-gate 	}
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate 	add_hashobject_to_hashtable(seg_hash);
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	copy_segment_layout(&seg_hash->u.seg_obj->segment, seg_layout);
1394*0Sstevel@tonic-gate 
1395*0Sstevel@tonic-gate 	add_to_seg_object_list(sec_hash, seg_hash);
1396*0Sstevel@tonic-gate 
1397*0Sstevel@tonic-gate 	sec_hash->u.sec_obj->num_of_segment += 1;
1398*0Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = new_seg_offset;
1399*0Sstevel@tonic-gate 	*newsection	= section; /* return the new section handle */
1400*0Sstevel@tonic-gate 	return (0);
1401*0Sstevel@tonic-gate }
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate static void
free_pkt_object_list(hash_obj_t * hash_obj)1404*0Sstevel@tonic-gate free_pkt_object_list(hash_obj_t	*hash_obj)
1405*0Sstevel@tonic-gate {
1406*0Sstevel@tonic-gate 	hash_obj_t	*next_obj;
1407*0Sstevel@tonic-gate 	hash_obj_t	*free_obj;
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 	next_obj = hash_obj->u.seg_obj->pkt_obj_list;
1410*0Sstevel@tonic-gate 	while (next_obj != NULL) {
1411*0Sstevel@tonic-gate 		free_obj = next_obj;
1412*0Sstevel@tonic-gate 		next_obj = next_obj->u.pkt_obj->next;
1413*0Sstevel@tonic-gate 		/* if prev is NULL it's the first object in the list */
1414*0Sstevel@tonic-gate 		if (free_obj->prev == NULL) {
1415*0Sstevel@tonic-gate 			hash_table[(free_obj->obj_hdl % TABLE_SIZE)] =
1416*0Sstevel@tonic-gate 					free_obj->next;
1417*0Sstevel@tonic-gate 			if (free_obj->next != NULL) {
1418*0Sstevel@tonic-gate 				free_obj->next->prev = free_obj->prev;
1419*0Sstevel@tonic-gate 			}
1420*0Sstevel@tonic-gate 		} else {
1421*0Sstevel@tonic-gate 			free_obj->prev->next = free_obj->next;
1422*0Sstevel@tonic-gate 			if (free_obj->next != NULL) {
1423*0Sstevel@tonic-gate 				free_obj->next->prev = free_obj->prev;
1424*0Sstevel@tonic-gate 			}
1425*0Sstevel@tonic-gate 		}
1426*0Sstevel@tonic-gate 		free(free_obj->u.pkt_obj);
1427*0Sstevel@tonic-gate 		free(free_obj);
1428*0Sstevel@tonic-gate 	}
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	hash_obj->u.seg_obj->pkt_obj_list = NULL;
1431*0Sstevel@tonic-gate }
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate static void
free_segment_hash(handle_t handle,hash_obj_t * sec_hash)1434*0Sstevel@tonic-gate free_segment_hash(handle_t	handle, hash_obj_t	*sec_hash)
1435*0Sstevel@tonic-gate {
1436*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
1437*0Sstevel@tonic-gate 	hash_obj_t	*next_hash;
1438*0Sstevel@tonic-gate 
1439*0Sstevel@tonic-gate 	seg_hash	= sec_hash->u.sec_obj->seg_obj_list;
1440*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
1441*0Sstevel@tonic-gate 		return;
1442*0Sstevel@tonic-gate 	}
1443*0Sstevel@tonic-gate 
1444*0Sstevel@tonic-gate 	if (seg_hash->obj_hdl == handle) {
1445*0Sstevel@tonic-gate 		sec_hash->u.sec_obj->seg_obj_list = seg_hash->u.seg_obj->next;
1446*0Sstevel@tonic-gate 	} else {
1447*0Sstevel@tonic-gate 		while (seg_hash->obj_hdl != handle) {
1448*0Sstevel@tonic-gate 			next_hash	= seg_hash;
1449*0Sstevel@tonic-gate 			seg_hash = seg_hash->u.seg_obj->next;
1450*0Sstevel@tonic-gate 			if (seg_hash == NULL) {
1451*0Sstevel@tonic-gate 				return;
1452*0Sstevel@tonic-gate 			}
1453*0Sstevel@tonic-gate 		}
1454*0Sstevel@tonic-gate 		next_hash->u.seg_obj->next = seg_hash->u.seg_obj->next;
1455*0Sstevel@tonic-gate 	}
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 	if (seg_hash->prev == NULL) {
1458*0Sstevel@tonic-gate 		hash_table[(seg_hash->obj_hdl % TABLE_SIZE)] = seg_hash->next;
1459*0Sstevel@tonic-gate 		if (seg_hash->next != NULL) {
1460*0Sstevel@tonic-gate 			seg_hash->next->prev = NULL;
1461*0Sstevel@tonic-gate 		}
1462*0Sstevel@tonic-gate 	} else {
1463*0Sstevel@tonic-gate 		seg_hash->prev->next = seg_hash->next;
1464*0Sstevel@tonic-gate 		if (seg_hash->next != NULL) {
1465*0Sstevel@tonic-gate 			seg_hash->next->prev = seg_hash->prev;
1466*0Sstevel@tonic-gate 		}
1467*0Sstevel@tonic-gate 	}
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 	free_pkt_object_list(seg_hash);
1470*0Sstevel@tonic-gate 	free(seg_hash->u.seg_obj);
1471*0Sstevel@tonic-gate 	free(seg_hash);
1472*0Sstevel@tonic-gate }
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate /*
1475*0Sstevel@tonic-gate  * Description	:
1476*0Sstevel@tonic-gate  *		fru_delete_segment() deletes a segment from a section; the
1477*0Sstevel@tonic-gate  *		associated container data is not altered.
1478*0Sstevel@tonic-gate  *
1479*0Sstevel@tonic-gate  * Arguments	: segment_hdl_t	segment handle.
1480*0Sstevel@tonic-gate  *		  section_hdl_t	new section handle.
1481*0Sstevel@tonic-gate  *
1482*0Sstevel@tonic-gate  * Return	:
1483*0Sstevel@tonic-gate  *		int
1484*0Sstevel@tonic-gate  *		On success, 0 returned; On error -1 is returned.
1485*0Sstevel@tonic-gate  */
1486*0Sstevel@tonic-gate 
1487*0Sstevel@tonic-gate int
fru_delete_segment(segment_hdl_t segment,section_hdl_t * newsection,door_cred_t * cred)1488*0Sstevel@tonic-gate fru_delete_segment(segment_hdl_t segment, section_hdl_t *newsection,
1489*0Sstevel@tonic-gate 							door_cred_t *cred)
1490*0Sstevel@tonic-gate {
1491*0Sstevel@tonic-gate 	int			num_of_seg;
1492*0Sstevel@tonic-gate 	int			bufsize;
1493*0Sstevel@tonic-gate 	int			count;
1494*0Sstevel@tonic-gate 	int			retval;
1495*0Sstevel@tonic-gate 	int			fd;
1496*0Sstevel@tonic-gate 	int			segnum;
1497*0Sstevel@tonic-gate 	hash_obj_t		*seg_hash;
1498*0Sstevel@tonic-gate 	hash_obj_t		*sec_hash;
1499*0Sstevel@tonic-gate 	hash_obj_t		*cont_hash;
1500*0Sstevel@tonic-gate 	hash_obj_t		*tmp_hash;
1501*0Sstevel@tonic-gate 	unsigned char		*buffer;
1502*0Sstevel@tonic-gate 	fru_segdesc_t		*desc;
1503*0Sstevel@tonic-gate 	segment_layout_t	*seg_buf;
1504*0Sstevel@tonic-gate 	section_layout_t	*sec_layout;
1505*0Sstevel@tonic-gate 	segment_layout_t	*seg_layout;
1506*0Sstevel@tonic-gate 	segment_layout_t	*next_layout;
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate 	/* check the effective uid of the client */
1509*0Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
1510*0Sstevel@tonic-gate 		errno = EPERM;
1511*0Sstevel@tonic-gate 		return (-1);	/* not a root */
1512*0Sstevel@tonic-gate 	}
1513*0Sstevel@tonic-gate 
1514*0Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
1515*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
1516*0Sstevel@tonic-gate 		return (-1);
1517*0Sstevel@tonic-gate 	}
1518*0Sstevel@tonic-gate 
1519*0Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
1520*0Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_DELETE)) {
1521*0Sstevel@tonic-gate 		errno = EPERM;
1522*0Sstevel@tonic-gate 		return (-1); /* can't delete this segment */
1523*0Sstevel@tonic-gate 	}
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
1526*0Sstevel@tonic-gate 						SECTION_TYPE);
1527*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
1528*0Sstevel@tonic-gate 		return (-1);
1529*0Sstevel@tonic-gate 	}
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
1532*0Sstevel@tonic-gate 		errno = EPERM;
1533*0Sstevel@tonic-gate 		return (-1);
1534*0Sstevel@tonic-gate 	}
1535*0Sstevel@tonic-gate 
1536*0Sstevel@tonic-gate 	num_of_seg	= sec_hash->u.sec_obj->num_of_segment;
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 	bufsize	= (sizeof (segment_layout_t) * num_of_seg);
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 	seg_buf	= alloca(bufsize);
1541*0Sstevel@tonic-gate 	if (seg_buf == NULL) {
1542*0Sstevel@tonic-gate 		return (-1);
1543*0Sstevel@tonic-gate 	}
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate 	segnum	= 0;
1546*0Sstevel@tonic-gate 	for (tmp_hash = sec_hash->u.sec_obj->seg_obj_list; tmp_hash != NULL;
1547*0Sstevel@tonic-gate 			tmp_hash = tmp_hash->u.seg_obj->next) {
1548*0Sstevel@tonic-gate 		if (tmp_hash->obj_hdl == segment) {
1549*0Sstevel@tonic-gate 			break;
1550*0Sstevel@tonic-gate 		}
1551*0Sstevel@tonic-gate 		segnum++;
1552*0Sstevel@tonic-gate 	}
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
1555*0Sstevel@tonic-gate 						CONTAINER_TYPE);
1556*0Sstevel@tonic-gate 	if (cont_hash == NULL) {
1557*0Sstevel@tonic-gate 		return (-1);
1558*0Sstevel@tonic-gate 	}
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	fd  = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
1561*0Sstevel@tonic-gate 	if (fd < 0) {
1562*0Sstevel@tonic-gate 		return (-1);
1563*0Sstevel@tonic-gate 	}
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate 	sec_layout	= alloca(sizeof (section_layout_t));
1566*0Sstevel@tonic-gate 	if (sec_layout == NULL) {
1567*0Sstevel@tonic-gate 		(void) close(fd);
1568*0Sstevel@tonic-gate 		return (-1);
1569*0Sstevel@tonic-gate 	}
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate 	/* read section layout header */
1572*0Sstevel@tonic-gate 	retval = pread(fd, sec_layout, sizeof (section_layout_t),
1573*0Sstevel@tonic-gate 					sec_hash->u.sec_obj->section.offset);
1574*0Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
1575*0Sstevel@tonic-gate 		(void) close(fd);
1576*0Sstevel@tonic-gate 		return (-1);
1577*0Sstevel@tonic-gate 	}
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	/* read segment header layout */
1580*0Sstevel@tonic-gate 	retval = pread(fd, seg_buf, bufsize,
1581*0Sstevel@tonic-gate 			sec_hash->u.sec_obj->section.offset +
1582*0Sstevel@tonic-gate 						sizeof (section_layout_t));
1583*0Sstevel@tonic-gate 	if (retval != bufsize) {
1584*0Sstevel@tonic-gate 		(void) close(fd);
1585*0Sstevel@tonic-gate 		return (-1);
1586*0Sstevel@tonic-gate 	}
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	seg_layout = (segment_layout_t *)(seg_buf + segnum);
1589*0Sstevel@tonic-gate 	next_layout	= seg_layout;
1590*0Sstevel@tonic-gate 	for (count = segnum; count < sec_hash->u.sec_obj->num_of_segment-1;
1591*0Sstevel@tonic-gate 								count++) {
1592*0Sstevel@tonic-gate 		next_layout++;
1593*0Sstevel@tonic-gate 		(void) memcpy(seg_layout, next_layout,
1594*0Sstevel@tonic-gate 						sizeof (segment_layout_t));
1595*0Sstevel@tonic-gate 		seg_layout++;
1596*0Sstevel@tonic-gate 	}
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 	(void) memset(seg_layout, '\0', sizeof (segment_layout_t));
1599*0Sstevel@tonic-gate 
1600*0Sstevel@tonic-gate 	sec_layout->headercrc8 = 0;
1601*0Sstevel@tonic-gate 
1602*0Sstevel@tonic-gate 	sec_layout->headerlength -= sizeof (segment_layout_t);
1603*0Sstevel@tonic-gate 	sec_layout->segmentcount -= 1;
1604*0Sstevel@tonic-gate 
1605*0Sstevel@tonic-gate 	buffer = alloca(sec_layout->headerlength);
1606*0Sstevel@tonic-gate 	if (buffer == NULL) {
1607*0Sstevel@tonic-gate 		(void) close(fd);
1608*0Sstevel@tonic-gate 		return (-1);
1609*0Sstevel@tonic-gate 	}
1610*0Sstevel@tonic-gate 
1611*0Sstevel@tonic-gate 	(void) memcpy(buffer, sec_layout, sizeof (section_layout_t));
1612*0Sstevel@tonic-gate 	(void) memcpy(buffer + sizeof (section_layout_t), seg_buf, bufsize
1613*0Sstevel@tonic-gate 						- sizeof (segment_layout_t));
1614*0Sstevel@tonic-gate 	sec_layout->headercrc8 = compute_crc8(buffer,
1615*0Sstevel@tonic-gate 						sec_layout->headerlength);
1616*0Sstevel@tonic-gate 
1617*0Sstevel@tonic-gate 	/* write section header with update crc8 and header length */
1618*0Sstevel@tonic-gate 	retval = pwrite(fd, sec_layout, sizeof (section_layout_t),
1619*0Sstevel@tonic-gate 					sec_hash->u.sec_obj->section.offset);
1620*0Sstevel@tonic-gate 	if (retval != sizeof (section_layout_t)) {
1621*0Sstevel@tonic-gate 		(void) close(fd);
1622*0Sstevel@tonic-gate 		return (-1);
1623*0Sstevel@tonic-gate 	}
1624*0Sstevel@tonic-gate 
1625*0Sstevel@tonic-gate 	/* write the update segment header */
1626*0Sstevel@tonic-gate 	retval = pwrite(fd, seg_buf, bufsize,
1627*0Sstevel@tonic-gate 		sec_hash->u.sec_obj->section.offset +
1628*0Sstevel@tonic-gate 						sizeof (section_layout_t));
1629*0Sstevel@tonic-gate 	(void) close(fd);
1630*0Sstevel@tonic-gate 	if (retval != bufsize) {
1631*0Sstevel@tonic-gate 		return (-1);
1632*0Sstevel@tonic-gate 	}
1633*0Sstevel@tonic-gate 
1634*0Sstevel@tonic-gate 	free_segment_hash(segment, sec_hash);
1635*0Sstevel@tonic-gate 
1636*0Sstevel@tonic-gate 	*newsection	= sec_hash->obj_hdl;
1637*0Sstevel@tonic-gate 	sec_hash->u.sec_obj->num_of_segment = sec_layout->segmentcount;
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate 	return (0);
1640*0Sstevel@tonic-gate }
1641*0Sstevel@tonic-gate 
1642*0Sstevel@tonic-gate /*
1643*0Sstevel@tonic-gate  * Description	:
1644*0Sstevel@tonic-gate  * 		fru_read_segment() reads the raw contents of a segment.
1645*0Sstevel@tonic-gate  *
1646*0Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
1647*0Sstevel@tonic-gate  *		 void *	: buffer containing segment data when function returns.
1648*0Sstevel@tonic-gate  *		size_t :number of bytes.
1649*0Sstevel@tonic-gate  *
1650*0Sstevel@tonic-gate  * Return	:
1651*0Sstevel@tonic-gate  * 		int
1652*0Sstevel@tonic-gate  *		On success, the number of bytes read is returned;
1653*0Sstevel@tonic-gate  *
1654*0Sstevel@tonic-gate  * Notes	:
1655*0Sstevel@tonic-gate  *		Segments containing packets can be read in structured fashion
1656*0Sstevel@tonic-gate  *		using the fru_get_packets() and fru_get_payload() primitives;the
1657*0Sstevel@tonic-gate  *		entire byte range of a segment can be read using
1658*0Sstevel@tonic-gate  *		fru_read_segment().
1659*0Sstevel@tonic-gate  */
1660*0Sstevel@tonic-gate /*ARGSUSED*/
1661*0Sstevel@tonic-gate ssize_t
fru_read_segment(segment_hdl_t segment,void * buffer,size_t nbytes,door_cred_t * cred)1662*0Sstevel@tonic-gate fru_read_segment(segment_hdl_t segment, void *buffer, size_t nbytes,
1663*0Sstevel@tonic-gate 		door_cred_t *cred)
1664*0Sstevel@tonic-gate {
1665*0Sstevel@tonic-gate 	int		fd;
1666*0Sstevel@tonic-gate 	int		retval;
1667*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
1668*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
1669*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
1670*0Sstevel@tonic-gate 
1671*0Sstevel@tonic-gate 	/* segment hash object */
1672*0Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
1673*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
1674*0Sstevel@tonic-gate 		return (-1);
1675*0Sstevel@tonic-gate 	}
1676*0Sstevel@tonic-gate 
1677*0Sstevel@tonic-gate 	/* section hash object */
1678*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
1679*0Sstevel@tonic-gate 							SECTION_TYPE);
1680*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
1681*0Sstevel@tonic-gate 		return (-1);
1682*0Sstevel@tonic-gate 	}
1683*0Sstevel@tonic-gate 
1684*0Sstevel@tonic-gate 	/* container hash object */
1685*0Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
1686*0Sstevel@tonic-gate 							CONTAINER_TYPE);
1687*0Sstevel@tonic-gate 	if (cont_hash == NULL) {
1688*0Sstevel@tonic-gate 		return (-1);
1689*0Sstevel@tonic-gate 	}
1690*0Sstevel@tonic-gate 
1691*0Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->segment.length < nbytes) {
1692*0Sstevel@tonic-gate 		return (-1);
1693*0Sstevel@tonic-gate 	}
1694*0Sstevel@tonic-gate 
1695*0Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
1696*0Sstevel@tonic-gate 	if (fd < 0) {
1697*0Sstevel@tonic-gate 		return (-1);
1698*0Sstevel@tonic-gate 	}
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate 	retval = pread(fd, buffer, nbytes, seg_hash->u.seg_obj->segment.offset);
1701*0Sstevel@tonic-gate 	(void) close(fd);
1702*0Sstevel@tonic-gate 	if (retval != nbytes) {
1703*0Sstevel@tonic-gate 		return (-1);
1704*0Sstevel@tonic-gate 	}
1705*0Sstevel@tonic-gate 	return (nbytes);
1706*0Sstevel@tonic-gate }
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate /*
1709*0Sstevel@tonic-gate  * Description	:
1710*0Sstevel@tonic-gate  *		fru_write_segment() writes a raw segment.
1711*0Sstevel@tonic-gate  *
1712*0Sstevel@tonic-gate  * Arguments	: segment_hdl_t :segment handle.
1713*0Sstevel@tonic-gate  *		 const void * : data buffer.
1714*0Sstevel@tonic-gate  *		 size_t	: number of bytes.
1715*0Sstevel@tonic-gate  *		 segment_hdl_t : new segment handle.
1716*0Sstevel@tonic-gate  *
1717*0Sstevel@tonic-gate  * Returns	:
1718*0Sstevel@tonic-gate  *		int
1719*0Sstevel@tonic-gate  *		On success, the number of bytes written is returned
1720*0Sstevel@tonic-gate  *
1721*0Sstevel@tonic-gate  */
1722*0Sstevel@tonic-gate /*ARGSUSED*/
1723*0Sstevel@tonic-gate int
fru_write_segment(segment_hdl_t segment,const void * data,size_t nbytes,segment_hdl_t * newsegment,door_cred_t * cred)1724*0Sstevel@tonic-gate fru_write_segment(segment_hdl_t segment, const void *data, size_t nbytes,
1725*0Sstevel@tonic-gate 				segment_hdl_t *newsegment, door_cred_t *cred)
1726*0Sstevel@tonic-gate {
1727*0Sstevel@tonic-gate 	return (ENOTSUP);
1728*0Sstevel@tonic-gate }
1729*0Sstevel@tonic-gate 
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate static int
get_packet(int device_fd,void * buffer,int size,int offset)1732*0Sstevel@tonic-gate get_packet(int device_fd, void *buffer, int size, int offset)
1733*0Sstevel@tonic-gate {
1734*0Sstevel@tonic-gate 	int	retval;
1735*0Sstevel@tonic-gate 
1736*0Sstevel@tonic-gate 	retval = pread(device_fd, (char *)buffer, size, offset);
1737*0Sstevel@tonic-gate 	if (retval != -1) {
1738*0Sstevel@tonic-gate 		return (0);
1739*0Sstevel@tonic-gate 	}
1740*0Sstevel@tonic-gate 	return (-1);
1741*0Sstevel@tonic-gate }
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate /*
1744*0Sstevel@tonic-gate  * Description	:
1745*0Sstevel@tonic-gate  *		get_payload() populates a buffer with the packets payload
1746*0Sstevel@tonic-gate  *
1747*0Sstevel@tonic-gate  * Arguments	: hash_obj_t : packet.
1748*0Sstevel@tonic-gate  *                int  : device file descriptor
1749*0Sstevel@tonic-gate  *                uint8_t* : pointer to a pre allocated buffer
1750*0Sstevel@tonic-gate  *
1751*0Sstevel@tonic-gate  *
1752*0Sstevel@tonic-gate  * Return	:
1753*0Sstevel@tonic-gate  *		int
1754*0Sstevel@tonic-gate  *		On success, 0 is returned; on failure
1755*0Sstevel@tonic-gate  *		-1 returned.
1756*0Sstevel@tonic-gate  */
1757*0Sstevel@tonic-gate int
get_payload(int device_fd,hash_obj_t * packet,uint8_t * payload)1758*0Sstevel@tonic-gate get_payload(int device_fd, hash_obj_t *packet, uint8_t *payload)
1759*0Sstevel@tonic-gate {
1760*0Sstevel@tonic-gate 	int		retval;
1761*0Sstevel@tonic-gate 	packet_obj_t	*packet_object;
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate 
1764*0Sstevel@tonic-gate 	packet_object = packet->u.pkt_obj;
1765*0Sstevel@tonic-gate 	if (packet_object == NULL) {
1766*0Sstevel@tonic-gate 		return (-1);
1767*0Sstevel@tonic-gate 	}
1768*0Sstevel@tonic-gate 
1769*0Sstevel@tonic-gate 	/* Get the data */
1770*0Sstevel@tonic-gate 	retval = pread(device_fd, payload, packet_object->paylen,
1771*0Sstevel@tonic-gate 			packet_object->payload_offset);
1772*0Sstevel@tonic-gate 	if (retval != packet_object->paylen) {
1773*0Sstevel@tonic-gate 	    free(payload);
1774*0Sstevel@tonic-gate 	    return (-1);
1775*0Sstevel@tonic-gate 	}
1776*0Sstevel@tonic-gate 
1777*0Sstevel@tonic-gate 	return (0);
1778*0Sstevel@tonic-gate 
1779*0Sstevel@tonic-gate }
1780*0Sstevel@tonic-gate 
1781*0Sstevel@tonic-gate 
1782*0Sstevel@tonic-gate static uint32_t
get_checksum_crc(int device_fd,hash_obj_t * seg_hash,int data_size)1783*0Sstevel@tonic-gate get_checksum_crc(int device_fd, hash_obj_t	*seg_hash, int data_size)
1784*0Sstevel@tonic-gate {
1785*0Sstevel@tonic-gate 	int		checksum;
1786*0Sstevel@tonic-gate 	int		offset = 0;
1787*0Sstevel@tonic-gate 	int		retval;
1788*0Sstevel@tonic-gate 	uint8_t		*payload;
1789*0Sstevel@tonic-gate 	uint32_t	crc;
1790*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
1791*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
1792*0Sstevel@tonic-gate 	unsigned char	*buffer;
1793*0Sstevel@tonic-gate 
1794*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
1795*0Sstevel@tonic-gate 							SECTION_TYPE);
1796*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
1797*0Sstevel@tonic-gate 		return ((uint32_t)-1);
1798*0Sstevel@tonic-gate 	}
1799*0Sstevel@tonic-gate 
1800*0Sstevel@tonic-gate 	buffer = alloca(data_size);
1801*0Sstevel@tonic-gate 	if (buffer == NULL) {
1802*0Sstevel@tonic-gate 		return ((uint32_t)-1);
1803*0Sstevel@tonic-gate 	}
1804*0Sstevel@tonic-gate 
1805*0Sstevel@tonic-gate 	/* traverse the packet object list for all the tags and payload */
1806*0Sstevel@tonic-gate 	for (pkt_hash = seg_hash->u.seg_obj->pkt_obj_list; pkt_hash != NULL;
1807*0Sstevel@tonic-gate 				pkt_hash = pkt_hash->u.pkt_obj->next) {
1808*0Sstevel@tonic-gate 		(void) memcpy(buffer + offset, &pkt_hash->u.pkt_obj->tag,
1809*0Sstevel@tonic-gate 					pkt_hash->u.pkt_obj->tag_size);
1810*0Sstevel@tonic-gate 		offset += pkt_hash->u.pkt_obj->tag_size;
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate 		/* read packet payload */
1813*0Sstevel@tonic-gate 		payload = malloc(pkt_hash->u.pkt_obj->paylen);
1814*0Sstevel@tonic-gate 		if (payload == NULL) {
1815*0Sstevel@tonic-gate 		    return ((uint32_t)-1);
1816*0Sstevel@tonic-gate 		}
1817*0Sstevel@tonic-gate 		retval = get_payload(device_fd, pkt_hash, payload);
1818*0Sstevel@tonic-gate 		if (retval == -1) {
1819*0Sstevel@tonic-gate 		    free(payload);
1820*0Sstevel@tonic-gate 		    return ((uint32_t)-1);
1821*0Sstevel@tonic-gate 		}
1822*0Sstevel@tonic-gate 		(void) memcpy(buffer + offset, payload,
1823*0Sstevel@tonic-gate 					pkt_hash->u.pkt_obj->paylen);
1824*0Sstevel@tonic-gate 		offset += pkt_hash->u.pkt_obj->paylen;
1825*0Sstevel@tonic-gate 		free(payload);
1826*0Sstevel@tonic-gate 	}
1827*0Sstevel@tonic-gate 
1828*0Sstevel@tonic-gate 	checksum	= sec_hash->u.sec_obj->checksum_method;
1829*0Sstevel@tonic-gate 
1830*0Sstevel@tonic-gate 	if (checksum == CRC32_SECTION) { /* read-only section */
1831*0Sstevel@tonic-gate 		crc = compute_crc32(buffer, data_size);
1832*0Sstevel@tonic-gate 	} else {		/* read/write section */
1833*0Sstevel@tonic-gate 		crc = compute_checksum32(buffer, data_size);
1834*0Sstevel@tonic-gate 	}
1835*0Sstevel@tonic-gate 	return (crc);	/* computed crc */
1836*0Sstevel@tonic-gate }
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate static int
get_packets(hash_obj_t * seg_hash,int device_fd,int offset,int length)1839*0Sstevel@tonic-gate get_packets(hash_obj_t *seg_hash, int device_fd, int offset, int length)
1840*0Sstevel@tonic-gate {
1841*0Sstevel@tonic-gate 	int		tag_size;
1842*0Sstevel@tonic-gate 	int		paylen;
1843*0Sstevel@tonic-gate 	int		retval;
1844*0Sstevel@tonic-gate 	int		seg_limit = 0;
1845*0Sstevel@tonic-gate 	int		pktcnt	= 0;
1846*0Sstevel@tonic-gate 	char		*data;
1847*0Sstevel@tonic-gate 	uint32_t	crc;
1848*0Sstevel@tonic-gate 	uint32_t	origcrc;
1849*0Sstevel@tonic-gate 	fru_tag_t	tag;
1850*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash_obj;
1851*0Sstevel@tonic-gate 	fru_segdesc_t	*segdesc;
1852*0Sstevel@tonic-gate 	fru_tagtype_t	tagtype;
1853*0Sstevel@tonic-gate 
1854*0Sstevel@tonic-gate 	retval = get_packet(device_fd, &tag, sizeof (fru_tag_t), offset);
1855*0Sstevel@tonic-gate 	if (retval == -1) {
1856*0Sstevel@tonic-gate 		return (-1);
1857*0Sstevel@tonic-gate 	}
1858*0Sstevel@tonic-gate 
1859*0Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = offset;
1860*0Sstevel@tonic-gate 
1861*0Sstevel@tonic-gate 	data	= (char *)&tag;
1862*0Sstevel@tonic-gate 	while (data[0] != SEG_TRAILER_TAG) {
1863*0Sstevel@tonic-gate 		tagtype	= get_tag_type(&tag); /* verify tag type */
1864*0Sstevel@tonic-gate 		if (tagtype == -1) {
1865*0Sstevel@tonic-gate 			return (-1);
1866*0Sstevel@tonic-gate 		}
1867*0Sstevel@tonic-gate 
1868*0Sstevel@tonic-gate 		tag_size = get_tag_size(tagtype);
1869*0Sstevel@tonic-gate 		if (tag_size == -1) {
1870*0Sstevel@tonic-gate 			return (-1);
1871*0Sstevel@tonic-gate 		}
1872*0Sstevel@tonic-gate 
1873*0Sstevel@tonic-gate 		seg_limit += tag_size;
1874*0Sstevel@tonic-gate 		if (seg_limit > length) {
1875*0Sstevel@tonic-gate 			return (-1);
1876*0Sstevel@tonic-gate 		}
1877*0Sstevel@tonic-gate 
1878*0Sstevel@tonic-gate 		paylen = get_payload_length((void *)&tag);
1879*0Sstevel@tonic-gate 		if (paylen == -1) {
1880*0Sstevel@tonic-gate 			return (-1);
1881*0Sstevel@tonic-gate 		}
1882*0Sstevel@tonic-gate 
1883*0Sstevel@tonic-gate 		seg_limit += paylen;
1884*0Sstevel@tonic-gate 		if (seg_limit > length) {
1885*0Sstevel@tonic-gate 			return (-1);
1886*0Sstevel@tonic-gate 		}
1887*0Sstevel@tonic-gate 
1888*0Sstevel@tonic-gate 		pkt_hash_obj = create_packet_hash_object();
1889*0Sstevel@tonic-gate 		if (pkt_hash_obj == NULL) {
1890*0Sstevel@tonic-gate 			return (-1);
1891*0Sstevel@tonic-gate 		}
1892*0Sstevel@tonic-gate 
1893*0Sstevel@tonic-gate 		offset += tag_size;
1894*0Sstevel@tonic-gate 
1895*0Sstevel@tonic-gate 		/* don't change this */
1896*0Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->tag.raw_data = 0;
1897*0Sstevel@tonic-gate 		(void) memcpy(&pkt_hash_obj->u.pkt_obj->tag, &tag, tag_size);
1898*0Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->paylen = paylen;
1899*0Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->tag_size = tag_size;
1900*0Sstevel@tonic-gate 		pkt_hash_obj->u.pkt_obj->payload_offset = offset;
1901*0Sstevel@tonic-gate 
1902*0Sstevel@tonic-gate 		offset += paylen;
1903*0Sstevel@tonic-gate 
1904*0Sstevel@tonic-gate 		add_hashobject_to_hashtable(pkt_hash_obj);
1905*0Sstevel@tonic-gate 		add_to_pkt_object_list(seg_hash, pkt_hash_obj);
1906*0Sstevel@tonic-gate 
1907*0Sstevel@tonic-gate 		pktcnt++;
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate 		retval = get_packet(device_fd, &tag, sizeof (fru_tag_t),
1910*0Sstevel@tonic-gate 									offset);
1911*0Sstevel@tonic-gate 		if (retval == -1) {
1912*0Sstevel@tonic-gate 			return (retval);
1913*0Sstevel@tonic-gate 		}
1914*0Sstevel@tonic-gate 
1915*0Sstevel@tonic-gate 		data	= (char *)&tag;
1916*0Sstevel@tonic-gate 	}
1917*0Sstevel@tonic-gate 
1918*0Sstevel@tonic-gate 	segdesc	= (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
1919*0Sstevel@tonic-gate 
1920*0Sstevel@tonic-gate 	seg_hash->u.seg_obj->trailer_offset = offset;
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	if (!segdesc->field.ignore_checksum)  {
1923*0Sstevel@tonic-gate 		crc = get_checksum_crc(device_fd, seg_hash, seg_limit);
1924*0Sstevel@tonic-gate 		offset	= seg_hash->u.seg_obj->segment.offset;
1925*0Sstevel@tonic-gate 
1926*0Sstevel@tonic-gate 		retval = pread(device_fd, &origcrc, sizeof (origcrc),
1927*0Sstevel@tonic-gate 						offset + seg_limit + 1);
1928*0Sstevel@tonic-gate 		if (retval != sizeof (origcrc)) {
1929*0Sstevel@tonic-gate 			return (-1);
1930*0Sstevel@tonic-gate 		}
1931*0Sstevel@tonic-gate 
1932*0Sstevel@tonic-gate 		if (origcrc != crc) {
1933*0Sstevel@tonic-gate 			seg_hash->u.seg_obj->trailer_offset = offset;
1934*0Sstevel@tonic-gate 			return (-1);
1935*0Sstevel@tonic-gate 		}
1936*0Sstevel@tonic-gate 	}
1937*0Sstevel@tonic-gate 
1938*0Sstevel@tonic-gate 	return (pktcnt);
1939*0Sstevel@tonic-gate }
1940*0Sstevel@tonic-gate 
1941*0Sstevel@tonic-gate /*
1942*0Sstevel@tonic-gate  * Description	:
1943*0Sstevel@tonic-gate  *		fru_get_num_packets() returns the current number of packets
1944*0Sstevel@tonic-gate  *		in a segment.
1945*0Sstevel@tonic-gate  *
1946*0Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
1947*0Sstevel@tonic-gate  *
1948*0Sstevel@tonic-gate  * Return	:
1949*0Sstevel@tonic-gate  *		int
1950*0Sstevel@tonic-gate  *		On success, the number of packets is returned;
1951*0Sstevel@tonic-gate  *		-1 on failure.
1952*0Sstevel@tonic-gate  */
1953*0Sstevel@tonic-gate /*ARGSUSED*/
1954*0Sstevel@tonic-gate int
fru_get_num_packets(segment_hdl_t segment,door_cred_t * cred)1955*0Sstevel@tonic-gate fru_get_num_packets(segment_hdl_t segment, door_cred_t *cred)
1956*0Sstevel@tonic-gate {
1957*0Sstevel@tonic-gate 	int		device_fd;
1958*0Sstevel@tonic-gate 	int		pktcnt;
1959*0Sstevel@tonic-gate 	int		length;
1960*0Sstevel@tonic-gate 	uint16_t	offset;
1961*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash_obj;
1962*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
1963*0Sstevel@tonic-gate 	fru_segdesc_t	*segdesc;
1964*0Sstevel@tonic-gate 	segment_obj_t	*segment_object;
1965*0Sstevel@tonic-gate 
1966*0Sstevel@tonic-gate 	seg_hash	= lookup_handle_object(segment, SEGMENT_TYPE);
1967*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
1968*0Sstevel@tonic-gate 		return (-1);
1969*0Sstevel@tonic-gate 	}
1970*0Sstevel@tonic-gate 
1971*0Sstevel@tonic-gate 	segment_object	= seg_hash->u.seg_obj;
1972*0Sstevel@tonic-gate 	if (segment_object == NULL) {
1973*0Sstevel@tonic-gate 		return (-1);
1974*0Sstevel@tonic-gate 	}
1975*0Sstevel@tonic-gate 
1976*0Sstevel@tonic-gate 
1977*0Sstevel@tonic-gate 	segdesc = (fru_segdesc_t *)&segment_object->segment.descriptor;
1978*0Sstevel@tonic-gate 	if (segdesc->field.opaque) {
1979*0Sstevel@tonic-gate 		return (0);
1980*0Sstevel@tonic-gate 	}
1981*0Sstevel@tonic-gate 
1982*0Sstevel@tonic-gate 	offset = segment_object->segment.offset;
1983*0Sstevel@tonic-gate 	length = segment_object->segment.length;
1984*0Sstevel@tonic-gate 
1985*0Sstevel@tonic-gate 	cont_hash_obj	= get_container_hash_object(SEGMENT_TYPE,
1986*0Sstevel@tonic-gate 						segment_object->section_hdl);
1987*0Sstevel@tonic-gate 
1988*0Sstevel@tonic-gate 	if (cont_hash_obj == NULL) {
1989*0Sstevel@tonic-gate 		return (-1);
1990*0Sstevel@tonic-gate 	}
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->pkt_obj_list != NULL) {
1993*0Sstevel@tonic-gate 		return (segment_object->num_of_packets);
1994*0Sstevel@tonic-gate 	}
1995*0Sstevel@tonic-gate 
1996*0Sstevel@tonic-gate 	segment_object->num_of_packets = 0;
1997*0Sstevel@tonic-gate 	device_fd = open(cont_hash_obj->u.cont_obj->device_pathname,
1998*0Sstevel@tonic-gate 								O_RDWR);
1999*0Sstevel@tonic-gate 	if (device_fd < 0) {
2000*0Sstevel@tonic-gate 		return (-1);
2001*0Sstevel@tonic-gate 	}
2002*0Sstevel@tonic-gate 
2003*0Sstevel@tonic-gate 	pktcnt = get_packets(seg_hash, device_fd, offset,
2004*0Sstevel@tonic-gate 							length);
2005*0Sstevel@tonic-gate 	if (pktcnt == -1) {
2006*0Sstevel@tonic-gate 		free_pkt_object_list(seg_hash);
2007*0Sstevel@tonic-gate 		seg_hash->u.seg_obj->pkt_obj_list = NULL;
2008*0Sstevel@tonic-gate 	}
2009*0Sstevel@tonic-gate 
2010*0Sstevel@tonic-gate 	segment_object->num_of_packets = pktcnt;
2011*0Sstevel@tonic-gate 	(void) close(device_fd);
2012*0Sstevel@tonic-gate 
2013*0Sstevel@tonic-gate 	return (segment_object->num_of_packets);
2014*0Sstevel@tonic-gate }
2015*0Sstevel@tonic-gate 
2016*0Sstevel@tonic-gate 
2017*0Sstevel@tonic-gate /*
2018*0Sstevel@tonic-gate  * Description	:
2019*0Sstevel@tonic-gate  *		fru_get_packets() fills an array of structures representing the
2020*0Sstevel@tonic-gate  *		packets in a segment.
2021*0Sstevel@tonic-gate  *
2022*0Sstevel@tonic-gate  * Arguments	: segment_hdl_t : segment handle.
2023*0Sstevel@tonic-gate  *		packet_t	: packet buffer.
2024*0Sstevel@tonic-gate  *		int	: maximum number of packets.
2025*0Sstevel@tonic-gate  *
2026*0Sstevel@tonic-gate  * Return	:
2027*0Sstevel@tonic-gate  *		int
2028*0Sstevel@tonic-gate  *		On success, the number of packet structures written is returned;
2029*0Sstevel@tonic-gate  *		On failure -1 is returned;
2030*0Sstevel@tonic-gate  *
2031*0Sstevel@tonic-gate  */
2032*0Sstevel@tonic-gate /*ARGSUSED*/
2033*0Sstevel@tonic-gate int
fru_get_packets(segment_hdl_t segment,packet_t * packet,int maxpackets,door_cred_t * cred)2034*0Sstevel@tonic-gate fru_get_packets(segment_hdl_t segment, packet_t *packet, int maxpackets,
2035*0Sstevel@tonic-gate 							door_cred_t *cred)
2036*0Sstevel@tonic-gate {
2037*0Sstevel@tonic-gate 	int		count;
2038*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash_obj;
2039*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash_obj;
2040*0Sstevel@tonic-gate 
2041*0Sstevel@tonic-gate 	/* segment hash object */
2042*0Sstevel@tonic-gate 	seg_hash_obj	= lookup_handle_object(segment, SEGMENT_TYPE);
2043*0Sstevel@tonic-gate 	if (seg_hash_obj == NULL) {
2044*0Sstevel@tonic-gate 		return (-1);
2045*0Sstevel@tonic-gate 	}
2046*0Sstevel@tonic-gate 
2047*0Sstevel@tonic-gate 	if (seg_hash_obj->u.seg_obj->num_of_packets != maxpackets) {
2048*0Sstevel@tonic-gate 		return (-1);
2049*0Sstevel@tonic-gate 	}
2050*0Sstevel@tonic-gate 
2051*0Sstevel@tonic-gate 	pkt_hash_obj	= seg_hash_obj->u.seg_obj->pkt_obj_list;
2052*0Sstevel@tonic-gate 	if (pkt_hash_obj == NULL) {
2053*0Sstevel@tonic-gate 		return (-1);
2054*0Sstevel@tonic-gate 	}
2055*0Sstevel@tonic-gate 
2056*0Sstevel@tonic-gate 	for (count = 0; count < maxpackets; count++, packet++) {
2057*0Sstevel@tonic-gate 		packet->handle	= pkt_hash_obj->obj_hdl;
2058*0Sstevel@tonic-gate 		packet->tag = 0;
2059*0Sstevel@tonic-gate 		(void) memcpy(&packet->tag, &pkt_hash_obj->u.pkt_obj->tag,
2060*0Sstevel@tonic-gate 				pkt_hash_obj->u.pkt_obj->tag_size);
2061*0Sstevel@tonic-gate 		pkt_hash_obj = pkt_hash_obj->u.pkt_obj->next;
2062*0Sstevel@tonic-gate 	}
2063*0Sstevel@tonic-gate 
2064*0Sstevel@tonic-gate 	return (0);
2065*0Sstevel@tonic-gate }
2066*0Sstevel@tonic-gate 
2067*0Sstevel@tonic-gate /*
2068*0Sstevel@tonic-gate  * Description	:
2069*0Sstevel@tonic-gate  *		fru_get_payload() copies the contents of a packet's payload.
2070*0Sstevel@tonic-gate  *
2071*0Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet handle.
2072*0Sstevel@tonic-gate  *		void *	: payload buffer.
2073*0Sstevel@tonic-gate  *		size_t	: sizeof the buffer.
2074*0Sstevel@tonic-gate  *
2075*0Sstevel@tonic-gate  * Return	:
2076*0Sstevel@tonic-gate  *    		int
2077*0Sstevel@tonic-gate  *     		On success, the number of bytes copied is returned; On error
2078*0Sstevel@tonic-gate  *		-1 returned.
2079*0Sstevel@tonic-gate  */
2080*0Sstevel@tonic-gate /*ARGSUSED*/
2081*0Sstevel@tonic-gate ssize_t
fru_get_payload(packet_hdl_t packet,void * buffer,size_t nbytes,door_cred_t * cred)2082*0Sstevel@tonic-gate fru_get_payload(packet_hdl_t packet, void *buffer, size_t nbytes,
2083*0Sstevel@tonic-gate 						door_cred_t *cred)
2084*0Sstevel@tonic-gate {
2085*0Sstevel@tonic-gate 	int		retval;
2086*0Sstevel@tonic-gate 	int		device_fd;
2087*0Sstevel@tonic-gate 	uint8_t		*payload;
2088*0Sstevel@tonic-gate 	hash_obj_t	*packet_hash_obj;
2089*0Sstevel@tonic-gate 	hash_obj_t	*segment_hash_obj;
2090*0Sstevel@tonic-gate 	hash_obj_t	*container_hash_obj;
2091*0Sstevel@tonic-gate 
2092*0Sstevel@tonic-gate 	/* packet hash object */
2093*0Sstevel@tonic-gate 	packet_hash_obj	= lookup_handle_object(packet, PACKET_TYPE);
2094*0Sstevel@tonic-gate 	if (packet_hash_obj == NULL) {
2095*0Sstevel@tonic-gate 		return (-1);
2096*0Sstevel@tonic-gate 	}
2097*0Sstevel@tonic-gate 
2098*0Sstevel@tonic-gate 	payload = malloc(packet_hash_obj->u.pkt_obj->paylen);
2099*0Sstevel@tonic-gate 	if (payload == NULL) {
2100*0Sstevel@tonic-gate 	    return (-1);
2101*0Sstevel@tonic-gate 	}
2102*0Sstevel@tonic-gate 
2103*0Sstevel@tonic-gate 	/* lookup the segment hash object */
2104*0Sstevel@tonic-gate 	segment_hash_obj =
2105*0Sstevel@tonic-gate 	    lookup_handle_object(packet_hash_obj->u.pkt_obj->segment_hdl,
2106*0Sstevel@tonic-gate 	    SEGMENT_TYPE);
2107*0Sstevel@tonic-gate 	if (segment_hash_obj == NULL) {
2108*0Sstevel@tonic-gate 	    free(payload);
2109*0Sstevel@tonic-gate 	    return (-1);
2110*0Sstevel@tonic-gate 	}
2111*0Sstevel@tonic-gate 
2112*0Sstevel@tonic-gate 	/* Get the container hash object to get the seeprom device path */
2113*0Sstevel@tonic-gate 	container_hash_obj	= get_container_hash_object(SEGMENT_TYPE,
2114*0Sstevel@tonic-gate 				    segment_hash_obj->u.seg_obj->section_hdl);
2115*0Sstevel@tonic-gate 	if (container_hash_obj == NULL) {
2116*0Sstevel@tonic-gate 	    free(payload);
2117*0Sstevel@tonic-gate 	    return (-1);
2118*0Sstevel@tonic-gate 	}
2119*0Sstevel@tonic-gate 
2120*0Sstevel@tonic-gate 	/* Open the seeprom device */
2121*0Sstevel@tonic-gate 	device_fd = open(container_hash_obj->u.cont_obj->device_pathname,
2122*0Sstevel@tonic-gate 								O_RDWR);
2123*0Sstevel@tonic-gate 	if (device_fd < 0) {
2124*0Sstevel@tonic-gate 	    free(payload);
2125*0Sstevel@tonic-gate 	    return (-1);
2126*0Sstevel@tonic-gate 	}
2127*0Sstevel@tonic-gate 
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate 	/* Call to get the payload  */
2130*0Sstevel@tonic-gate 	retval  = get_payload(device_fd, packet_hash_obj, payload);
2131*0Sstevel@tonic-gate 	if (retval == -1) {
2132*0Sstevel@tonic-gate 	    free(payload);
2133*0Sstevel@tonic-gate 	    (void) close(device_fd);
2134*0Sstevel@tonic-gate 	    return (-1);
2135*0Sstevel@tonic-gate 	}
2136*0Sstevel@tonic-gate 
2137*0Sstevel@tonic-gate 
2138*0Sstevel@tonic-gate 	/* verify payload length */
2139*0Sstevel@tonic-gate 	if (nbytes != packet_hash_obj->u.pkt_obj->paylen) {
2140*0Sstevel@tonic-gate 	    free(payload);
2141*0Sstevel@tonic-gate 	    (void) close(device_fd);
2142*0Sstevel@tonic-gate 	    return (-1);
2143*0Sstevel@tonic-gate 	}
2144*0Sstevel@tonic-gate 
2145*0Sstevel@tonic-gate 	(void) memcpy(buffer, payload, nbytes);
2146*0Sstevel@tonic-gate 	free(payload);
2147*0Sstevel@tonic-gate 	(void) close(device_fd);
2148*0Sstevel@tonic-gate 	return (nbytes);
2149*0Sstevel@tonic-gate }
2150*0Sstevel@tonic-gate 
2151*0Sstevel@tonic-gate /*
2152*0Sstevel@tonic-gate  * Description	:
2153*0Sstevel@tonic-gate  * 		fru_update_payload() writes the contents of a packet's payload.
2154*0Sstevel@tonic-gate  *
2155*0Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet handle.
2156*0Sstevel@tonic-gate  *		const void * : data buffer.
2157*0Sstevel@tonic-gate  *		size_t	: buffer size.
2158*0Sstevel@tonic-gate  *		packet_hdl_t	: new packet handle.
2159*0Sstevel@tonic-gate  *
2160*0Sstevel@tonic-gate  * Return	:
2161*0Sstevel@tonic-gate  * 		int
2162*0Sstevel@tonic-gate  *		On success, 0 is returned; on failure
2163*0Sstevel@tonic-gate  *		-1 is returned.
2164*0Sstevel@tonic-gate  */
2165*0Sstevel@tonic-gate 
2166*0Sstevel@tonic-gate int
fru_update_payload(packet_hdl_t packet,const void * data,size_t nbytes,packet_hdl_t * newpacket,door_cred_t * cred)2167*0Sstevel@tonic-gate fru_update_payload(packet_hdl_t packet, const void *data, size_t nbytes,
2168*0Sstevel@tonic-gate 				packet_hdl_t *newpacket, door_cred_t *cred)
2169*0Sstevel@tonic-gate {
2170*0Sstevel@tonic-gate 	int		fd;
2171*0Sstevel@tonic-gate 	int		segment_offset;
2172*0Sstevel@tonic-gate 	int		trailer_offset;
2173*0Sstevel@tonic-gate 	int		retval;
2174*0Sstevel@tonic-gate 	uint32_t	crc;
2175*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
2176*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
2177*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
2178*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
2179*0Sstevel@tonic-gate 	fru_segdesc_t	*desc;
2180*0Sstevel@tonic-gate 
2181*0Sstevel@tonic-gate 	/* check the effective uid of the client */
2182*0Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
2183*0Sstevel@tonic-gate 		errno = EPERM;
2184*0Sstevel@tonic-gate 		return (-1);	/* not a root */
2185*0Sstevel@tonic-gate 	}
2186*0Sstevel@tonic-gate 
2187*0Sstevel@tonic-gate 	/* packet hash object */
2188*0Sstevel@tonic-gate 	pkt_hash = lookup_handle_object(packet,	PACKET_TYPE);
2189*0Sstevel@tonic-gate 	if (pkt_hash == NULL) {
2190*0Sstevel@tonic-gate 		return (-1);
2191*0Sstevel@tonic-gate 	}
2192*0Sstevel@tonic-gate 
2193*0Sstevel@tonic-gate 	/* segment hash object */
2194*0Sstevel@tonic-gate 	seg_hash = lookup_handle_object(pkt_hash->u.pkt_obj->segment_hdl,
2195*0Sstevel@tonic-gate 						SEGMENT_TYPE);
2196*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
2197*0Sstevel@tonic-gate 		return (-1);
2198*0Sstevel@tonic-gate 	}
2199*0Sstevel@tonic-gate 
2200*0Sstevel@tonic-gate 	/* check for write perm. */
2201*0Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
2202*0Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
2203*0Sstevel@tonic-gate 		errno = EPERM;
2204*0Sstevel@tonic-gate 		return (-1); /* write not allowed */
2205*0Sstevel@tonic-gate 	}
2206*0Sstevel@tonic-gate 
2207*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2208*0Sstevel@tonic-gate 						SECTION_TYPE);
2209*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
2210*0Sstevel@tonic-gate 		return (-1);
2211*0Sstevel@tonic-gate 	}
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
2214*0Sstevel@tonic-gate 		errno = EPERM;
2215*0Sstevel@tonic-gate 		return (-1);		/* read-only section */
2216*0Sstevel@tonic-gate 	}
2217*0Sstevel@tonic-gate 
2218*0Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
2219*0Sstevel@tonic-gate 						CONTAINER_TYPE);
2220*0Sstevel@tonic-gate 	if (cont_hash == NULL) {
2221*0Sstevel@tonic-gate 		return (-1);
2222*0Sstevel@tonic-gate 	}
2223*0Sstevel@tonic-gate 
2224*0Sstevel@tonic-gate 	if (pkt_hash->u.pkt_obj->paylen != nbytes) {
2225*0Sstevel@tonic-gate 		return (-1);
2226*0Sstevel@tonic-gate 	}
2227*0Sstevel@tonic-gate 
2228*0Sstevel@tonic-gate 	fd	= open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
2229*0Sstevel@tonic-gate 	if (fd < 0) {
2230*0Sstevel@tonic-gate 		return (-1);
2231*0Sstevel@tonic-gate 	}
2232*0Sstevel@tonic-gate 
2233*0Sstevel@tonic-gate 	trailer_offset	= seg_hash->u.seg_obj->trailer_offset;
2234*0Sstevel@tonic-gate 	segment_offset	= seg_hash->u.seg_obj->segment.offset;
2235*0Sstevel@tonic-gate 
2236*0Sstevel@tonic-gate 	crc = get_checksum_crc(fd, seg_hash, (trailer_offset - segment_offset));
2237*0Sstevel@tonic-gate 	retval = pwrite(fd, data, nbytes, pkt_hash->u.pkt_obj->payload_offset);
2238*0Sstevel@tonic-gate 	if (retval != nbytes) {
2239*0Sstevel@tonic-gate 		(void) close(fd);
2240*0Sstevel@tonic-gate 		return (-1);
2241*0Sstevel@tonic-gate 	}
2242*0Sstevel@tonic-gate 
2243*0Sstevel@tonic-gate 	retval = pwrite(fd, &crc, sizeof (crc), trailer_offset + 1);
2244*0Sstevel@tonic-gate 	(void) close(fd);
2245*0Sstevel@tonic-gate 	if (retval != sizeof (crc)) {
2246*0Sstevel@tonic-gate 		return (-1);
2247*0Sstevel@tonic-gate 	}
2248*0Sstevel@tonic-gate 	*newpacket	= packet;
2249*0Sstevel@tonic-gate 	return (0);
2250*0Sstevel@tonic-gate }
2251*0Sstevel@tonic-gate 
2252*0Sstevel@tonic-gate /*
2253*0Sstevel@tonic-gate  * Description	:
2254*0Sstevel@tonic-gate  *		fru_append_packet() appends a packet to a segment.
2255*0Sstevel@tonic-gate  *
2256*0Sstevel@tonic-gate  * Arguments	:
2257*0Sstevel@tonic-gate  *		segment_hdl_t segment
2258*0Sstevel@tonic-gate  *		A handle for the segment to which the packet will be appended.
2259*0Sstevel@tonic-gate  *
2260*0Sstevel@tonic-gate  *   		packet_t *packet
2261*0Sstevel@tonic-gate  *     		On entry, the "tag" component of "packet" specifies the tag
2262*0Sstevel@tonic-gate  *     		value for the added packet; the "handle" component is ignored.
2263*0Sstevel@tonic-gate  *     		On return, the "handle" component is set to the handle of the
2264*0Sstevel@tonic-gate  *     		appended packet.
2265*0Sstevel@tonic-gate  *
2266*0Sstevel@tonic-gate  *   		const void *payload
2267*0Sstevel@tonic-gate  *     		A pointer to the caller's buffer containing the payload data for
2268*0Sstevel@tonic-gate  *     		the appended packet.
2269*0Sstevel@tonic-gate  *
2270*0Sstevel@tonic-gate  *   		size_t nbytes
2271*0Sstevel@tonic-gate  *     		The size of the caller buffer.
2272*0Sstevel@tonic-gate  *
2273*0Sstevel@tonic-gate  * Return	:
2274*0Sstevel@tonic-gate  *   		int
2275*0Sstevel@tonic-gate  *     		On success, 0 is returned; on error -1 is returned;
2276*0Sstevel@tonic-gate  */
2277*0Sstevel@tonic-gate 
2278*0Sstevel@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)2279*0Sstevel@tonic-gate fru_append_packet(segment_hdl_t segment, packet_t *packet, const void *payload,
2280*0Sstevel@tonic-gate 		size_t nbytes, segment_hdl_t *newsegment, door_cred_t *cred)
2281*0Sstevel@tonic-gate {
2282*0Sstevel@tonic-gate 	int		trailer_offset;
2283*0Sstevel@tonic-gate 	int		tag_size;
2284*0Sstevel@tonic-gate 	int		fd;
2285*0Sstevel@tonic-gate 	int		retval;
2286*0Sstevel@tonic-gate 	char		trailer[] = {0x0c, 0x00, 0x00, 0x00, 0x00};
2287*0Sstevel@tonic-gate 	uint32_t	crc;
2288*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
2289*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
2290*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
2291*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
2292*0Sstevel@tonic-gate 	fru_tagtype_t	tagtype;
2293*0Sstevel@tonic-gate 	fru_segdesc_t	*desc;
2294*0Sstevel@tonic-gate 
2295*0Sstevel@tonic-gate 	/* check the effective uid of the client */
2296*0Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
2297*0Sstevel@tonic-gate 		errno = EPERM;
2298*0Sstevel@tonic-gate 		return (-1);	/* not a root */
2299*0Sstevel@tonic-gate 	}
2300*0Sstevel@tonic-gate 
2301*0Sstevel@tonic-gate 	seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
2302*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
2303*0Sstevel@tonic-gate 		return (-1);
2304*0Sstevel@tonic-gate 	}
2305*0Sstevel@tonic-gate 
2306*0Sstevel@tonic-gate 	/* check for write perm. */
2307*0Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
2308*0Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
2309*0Sstevel@tonic-gate 		errno = EPERM;
2310*0Sstevel@tonic-gate 		return (-1); /* write not allowed */
2311*0Sstevel@tonic-gate 	}
2312*0Sstevel@tonic-gate 
2313*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2314*0Sstevel@tonic-gate 							SECTION_TYPE);
2315*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
2316*0Sstevel@tonic-gate 		return (-1);
2317*0Sstevel@tonic-gate 	}
2318*0Sstevel@tonic-gate 
2319*0Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
2320*0Sstevel@tonic-gate 		errno = EPERM;
2321*0Sstevel@tonic-gate 		return (-1);		/* read-only section */
2322*0Sstevel@tonic-gate 	}
2323*0Sstevel@tonic-gate 
2324*0Sstevel@tonic-gate 	trailer_offset	= seg_hash->u.seg_obj->trailer_offset;
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate 	/*
2327*0Sstevel@tonic-gate 	 * if trailer offset is 0 than parse the segment data to get the trailer
2328*0Sstevel@tonic-gate 	 * offset to compute the remaining space left in the segment area for
2329*0Sstevel@tonic-gate 	 * new packet to be added.
2330*0Sstevel@tonic-gate 	 */
2331*0Sstevel@tonic-gate 	if (trailer_offset == 0) {
2332*0Sstevel@tonic-gate 		(void) fru_get_num_packets(segment, cred);
2333*0Sstevel@tonic-gate 		trailer_offset  = seg_hash->u.seg_obj->trailer_offset;
2334*0Sstevel@tonic-gate 	}
2335*0Sstevel@tonic-gate 
2336*0Sstevel@tonic-gate 	tagtype	= get_tag_type((void *)&packet->tag);
2337*0Sstevel@tonic-gate 	if (tagtype == -1) {
2338*0Sstevel@tonic-gate 		return (-1);
2339*0Sstevel@tonic-gate 	}
2340*0Sstevel@tonic-gate 
2341*0Sstevel@tonic-gate 	tag_size	= get_tag_size(tagtype);
2342*0Sstevel@tonic-gate 	if (tag_size == -1) {
2343*0Sstevel@tonic-gate 		return (-1);
2344*0Sstevel@tonic-gate 	}
2345*0Sstevel@tonic-gate 
2346*0Sstevel@tonic-gate 	if (seg_hash->u.seg_obj->segment.length >
2347*0Sstevel@tonic-gate 		((trailer_offset - seg_hash->u.seg_obj->segment.offset) +
2348*0Sstevel@tonic-gate 			tag_size + nbytes + sizeof (char)
2349*0Sstevel@tonic-gate 						+ sizeof (uint32_t))) {
2350*0Sstevel@tonic-gate 		/* create new packet hash */
2351*0Sstevel@tonic-gate 		pkt_hash = create_packet_hash_object();
2352*0Sstevel@tonic-gate 		if (pkt_hash == NULL) {
2353*0Sstevel@tonic-gate 			return (-1);
2354*0Sstevel@tonic-gate 		}
2355*0Sstevel@tonic-gate 
2356*0Sstevel@tonic-gate 		/* tag initialization */
2357*0Sstevel@tonic-gate 		(void) memcpy(&pkt_hash->u.pkt_obj->tag, &packet->tag,
2358*0Sstevel@tonic-gate 								tag_size);
2359*0Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->tag_size	= tag_size;
2360*0Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->paylen	= nbytes;
2361*0Sstevel@tonic-gate 		pkt_hash->u.pkt_obj->payload_offset = trailer_offset + tag_size;
2362*0Sstevel@tonic-gate 
2363*0Sstevel@tonic-gate 		/* add to hash table */
2364*0Sstevel@tonic-gate 		add_hashobject_to_hashtable(pkt_hash);
2365*0Sstevel@tonic-gate 
2366*0Sstevel@tonic-gate 		add_to_pkt_object_list(seg_hash, pkt_hash);
2367*0Sstevel@tonic-gate 
2368*0Sstevel@tonic-gate 		cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
2369*0Sstevel@tonic-gate 								CONTAINER_TYPE);
2370*0Sstevel@tonic-gate 		if (cont_hash == NULL) {
2371*0Sstevel@tonic-gate 			return (-1);
2372*0Sstevel@tonic-gate 		}
2373*0Sstevel@tonic-gate 
2374*0Sstevel@tonic-gate 		fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
2375*0Sstevel@tonic-gate 		if (fd < 0) {
2376*0Sstevel@tonic-gate 			return (-1);
2377*0Sstevel@tonic-gate 		}
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate 		/* update the trailer offset  */
2380*0Sstevel@tonic-gate 		trailer_offset += tag_size + nbytes;
2381*0Sstevel@tonic-gate 
2382*0Sstevel@tonic-gate 		/* calculate new checksum */
2383*0Sstevel@tonic-gate 		crc = get_checksum_crc(fd, seg_hash, (trailer_offset -
2384*0Sstevel@tonic-gate 					seg_hash->u.seg_obj->segment.offset));
2385*0Sstevel@tonic-gate 
2386*0Sstevel@tonic-gate 		retval = pwrite(fd, &packet->tag, tag_size, trailer_offset
2387*0Sstevel@tonic-gate 							- (tag_size + nbytes));
2388*0Sstevel@tonic-gate 		if (retval != tag_size) {
2389*0Sstevel@tonic-gate 			(void) close(fd);
2390*0Sstevel@tonic-gate 			return (-1);
2391*0Sstevel@tonic-gate 		}
2392*0Sstevel@tonic-gate 
2393*0Sstevel@tonic-gate 		retval = pwrite(fd, payload, nbytes, trailer_offset - nbytes);
2394*0Sstevel@tonic-gate 		if (retval != nbytes) {
2395*0Sstevel@tonic-gate 			(void) close(fd);
2396*0Sstevel@tonic-gate 			return (-1);
2397*0Sstevel@tonic-gate 		}
2398*0Sstevel@tonic-gate 
2399*0Sstevel@tonic-gate 		retval = pwrite(fd, trailer, sizeof (trailer), trailer_offset);
2400*0Sstevel@tonic-gate 		if (retval != sizeof (trailer)) {
2401*0Sstevel@tonic-gate 			(void) close(fd);
2402*0Sstevel@tonic-gate 			return (-1);
2403*0Sstevel@tonic-gate 		}
2404*0Sstevel@tonic-gate 
2405*0Sstevel@tonic-gate 		retval = pwrite(fd, &crc, sizeof (crc), trailer_offset + 1);
2406*0Sstevel@tonic-gate 		(void) close(fd);
2407*0Sstevel@tonic-gate 		if (retval != sizeof (crc)) {
2408*0Sstevel@tonic-gate 			return (-1);
2409*0Sstevel@tonic-gate 		}
2410*0Sstevel@tonic-gate 
2411*0Sstevel@tonic-gate 		seg_hash->u.seg_obj->trailer_offset = trailer_offset;
2412*0Sstevel@tonic-gate 		seg_hash->u.seg_obj->num_of_packets += 1;
2413*0Sstevel@tonic-gate 
2414*0Sstevel@tonic-gate 		*newsegment	= segment; 	/* return new segment handle */
2415*0Sstevel@tonic-gate 		return (0);
2416*0Sstevel@tonic-gate 	} else {
2417*0Sstevel@tonic-gate 		errno = EAGAIN;
2418*0Sstevel@tonic-gate 	}
2419*0Sstevel@tonic-gate 
2420*0Sstevel@tonic-gate 	return (-1);
2421*0Sstevel@tonic-gate }
2422*0Sstevel@tonic-gate 
2423*0Sstevel@tonic-gate static void
adjust_packets(int fd,hash_obj_t * free_obj,hash_obj_t * object_list)2424*0Sstevel@tonic-gate adjust_packets(int	fd, hash_obj_t	*free_obj, hash_obj_t	*object_list)
2425*0Sstevel@tonic-gate {
2426*0Sstevel@tonic-gate 	int		retval;
2427*0Sstevel@tonic-gate 	uint8_t		*payload;
2428*0Sstevel@tonic-gate 	uint32_t	new_offset;
2429*0Sstevel@tonic-gate 	hash_obj_t	*hash_ptr;
2430*0Sstevel@tonic-gate 
2431*0Sstevel@tonic-gate 
2432*0Sstevel@tonic-gate 	new_offset = free_obj->u.pkt_obj->payload_offset
2433*0Sstevel@tonic-gate 					- free_obj->u.pkt_obj->tag_size;
2434*0Sstevel@tonic-gate 	for (hash_ptr = object_list; hash_ptr != NULL;
2435*0Sstevel@tonic-gate 				hash_ptr = hash_ptr->u.pkt_obj->next) {
2436*0Sstevel@tonic-gate 
2437*0Sstevel@tonic-gate 		payload = malloc(hash_ptr->u.pkt_obj->paylen);
2438*0Sstevel@tonic-gate 		if (payload == NULL) {
2439*0Sstevel@tonic-gate 		    return;
2440*0Sstevel@tonic-gate 		}
2441*0Sstevel@tonic-gate 		retval = get_payload(fd, hash_ptr, payload);
2442*0Sstevel@tonic-gate 		if (retval == -1) {
2443*0Sstevel@tonic-gate 		    free(payload);
2444*0Sstevel@tonic-gate 		    return;
2445*0Sstevel@tonic-gate 		}
2446*0Sstevel@tonic-gate 
2447*0Sstevel@tonic-gate 		retval = pwrite(fd, &hash_ptr->u.pkt_obj->tag,
2448*0Sstevel@tonic-gate 				hash_ptr->u.pkt_obj->tag_size, new_offset);
2449*0Sstevel@tonic-gate 		if (retval != hash_ptr->u.pkt_obj->tag_size) {
2450*0Sstevel@tonic-gate 		    free(payload);
2451*0Sstevel@tonic-gate 		    return;
2452*0Sstevel@tonic-gate 		}
2453*0Sstevel@tonic-gate 		new_offset += hash_ptr->u.pkt_obj->tag_size;
2454*0Sstevel@tonic-gate 		hash_ptr->u.pkt_obj->payload_offset = new_offset;
2455*0Sstevel@tonic-gate 		retval = pwrite(fd, payload,
2456*0Sstevel@tonic-gate 				hash_ptr->u.pkt_obj->paylen, new_offset);
2457*0Sstevel@tonic-gate 		if (retval != hash_ptr->u.pkt_obj->paylen) {
2458*0Sstevel@tonic-gate 		    free(payload);
2459*0Sstevel@tonic-gate 		    return;
2460*0Sstevel@tonic-gate 		}
2461*0Sstevel@tonic-gate 		new_offset += hash_ptr->u.pkt_obj->paylen;
2462*0Sstevel@tonic-gate 		free(payload);
2463*0Sstevel@tonic-gate 	}
2464*0Sstevel@tonic-gate }
2465*0Sstevel@tonic-gate 
2466*0Sstevel@tonic-gate static void
free_packet_object(handle_t handle,hash_obj_t * seg_hash)2467*0Sstevel@tonic-gate free_packet_object(handle_t	handle, hash_obj_t *seg_hash)
2468*0Sstevel@tonic-gate {
2469*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
2470*0Sstevel@tonic-gate 	hash_obj_t	*next_hash;
2471*0Sstevel@tonic-gate 
2472*0Sstevel@tonic-gate 	pkt_hash	= seg_hash->u.seg_obj->pkt_obj_list;
2473*0Sstevel@tonic-gate 	if (pkt_hash == NULL) {
2474*0Sstevel@tonic-gate 		return;
2475*0Sstevel@tonic-gate 	}
2476*0Sstevel@tonic-gate 
2477*0Sstevel@tonic-gate 	if (pkt_hash->obj_hdl == handle) {
2478*0Sstevel@tonic-gate 		seg_hash->u.seg_obj->pkt_obj_list = pkt_hash->u.pkt_obj->next;
2479*0Sstevel@tonic-gate 	} else {
2480*0Sstevel@tonic-gate 		while (pkt_hash->obj_hdl != handle) {
2481*0Sstevel@tonic-gate 			next_hash = pkt_hash;
2482*0Sstevel@tonic-gate 			pkt_hash = pkt_hash->u.pkt_obj->next;
2483*0Sstevel@tonic-gate 			if (pkt_hash == NULL) {
2484*0Sstevel@tonic-gate 				return;
2485*0Sstevel@tonic-gate 			}
2486*0Sstevel@tonic-gate 		}
2487*0Sstevel@tonic-gate 		next_hash->u.pkt_obj->next = pkt_hash->u.pkt_obj->next;
2488*0Sstevel@tonic-gate 	}
2489*0Sstevel@tonic-gate 
2490*0Sstevel@tonic-gate 	if (pkt_hash->prev == NULL) {
2491*0Sstevel@tonic-gate 		hash_table[(pkt_hash->obj_hdl % TABLE_SIZE)] = pkt_hash->next;
2492*0Sstevel@tonic-gate 		if (pkt_hash->next != NULL) {
2493*0Sstevel@tonic-gate 			pkt_hash->next->prev = NULL;
2494*0Sstevel@tonic-gate 		}
2495*0Sstevel@tonic-gate 	} else {
2496*0Sstevel@tonic-gate 		pkt_hash->prev->next = pkt_hash->next;
2497*0Sstevel@tonic-gate 		if (pkt_hash->next != NULL) {
2498*0Sstevel@tonic-gate 			pkt_hash->next->prev = pkt_hash->prev;
2499*0Sstevel@tonic-gate 		}
2500*0Sstevel@tonic-gate 	}
2501*0Sstevel@tonic-gate 
2502*0Sstevel@tonic-gate 	free(pkt_hash->u.pkt_obj);
2503*0Sstevel@tonic-gate 	free(pkt_hash);
2504*0Sstevel@tonic-gate }
2505*0Sstevel@tonic-gate 
2506*0Sstevel@tonic-gate /*
2507*0Sstevel@tonic-gate  * Description	:
2508*0Sstevel@tonic-gate  *   		fru_delete_packet() deletes a packet from a segment.
2509*0Sstevel@tonic-gate  *
2510*0Sstevel@tonic-gate  * Arguments	: packet_hdl_t : packet number to be deleted.
2511*0Sstevel@tonic-gate  *		segment_hdl_t : new segment handler.
2512*0Sstevel@tonic-gate  *
2513*0Sstevel@tonic-gate  * Return	:
2514*0Sstevel@tonic-gate  *   		int
2515*0Sstevel@tonic-gate  *     		On success, 0 is returned; on error, -1.
2516*0Sstevel@tonic-gate  *
2517*0Sstevel@tonic-gate  * NOTES
2518*0Sstevel@tonic-gate  * 		Packets are adjacent; thus, deleting a packet requires moving
2519*0Sstevel@tonic-gate  *   		succeeding packets to compact the resulting hole.
2520*0Sstevel@tonic-gate  */
2521*0Sstevel@tonic-gate 
2522*0Sstevel@tonic-gate int
fru_delete_packet(packet_hdl_t packet,segment_hdl_t * newsegment,door_cred_t * cred)2523*0Sstevel@tonic-gate fru_delete_packet(packet_hdl_t packet, segment_hdl_t *newsegment,
2524*0Sstevel@tonic-gate 						door_cred_t *cred)
2525*0Sstevel@tonic-gate {
2526*0Sstevel@tonic-gate 	int		retval;
2527*0Sstevel@tonic-gate 	int		fd;
2528*0Sstevel@tonic-gate 	char		trailer[] = { 0x0c, 0x00, 0x00, 0x00, 0x00};
2529*0Sstevel@tonic-gate 	uint32_t	crc;
2530*0Sstevel@tonic-gate 	hash_obj_t	*tmp_obj;
2531*0Sstevel@tonic-gate 	hash_obj_t	*pkt_hash;
2532*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash;
2533*0Sstevel@tonic-gate 	hash_obj_t	*cont_hash;
2534*0Sstevel@tonic-gate 	hash_obj_t	*prev_obj;
2535*0Sstevel@tonic-gate 	hash_obj_t	*seg_hash;
2536*0Sstevel@tonic-gate 	fru_segdesc_t	*desc;
2537*0Sstevel@tonic-gate 
2538*0Sstevel@tonic-gate 	/* check the effective uid of the client */
2539*0Sstevel@tonic-gate 	if (cred->dc_euid != 0) {
2540*0Sstevel@tonic-gate 		errno = EPERM;
2541*0Sstevel@tonic-gate 		return (-1);	/* not a root */
2542*0Sstevel@tonic-gate 	}
2543*0Sstevel@tonic-gate 
2544*0Sstevel@tonic-gate 	/* packet hash object */
2545*0Sstevel@tonic-gate 	pkt_hash = lookup_handle_object(packet, PACKET_TYPE);
2546*0Sstevel@tonic-gate 	if (pkt_hash == NULL) {
2547*0Sstevel@tonic-gate 		return (-1);
2548*0Sstevel@tonic-gate 	}
2549*0Sstevel@tonic-gate 
2550*0Sstevel@tonic-gate 	/* segment hash object */
2551*0Sstevel@tonic-gate 	seg_hash = lookup_handle_object(pkt_hash->u.pkt_obj->segment_hdl,
2552*0Sstevel@tonic-gate 							SEGMENT_TYPE);
2553*0Sstevel@tonic-gate 	if (seg_hash == NULL) {
2554*0Sstevel@tonic-gate 		return (-1);
2555*0Sstevel@tonic-gate 	}
2556*0Sstevel@tonic-gate 
2557*0Sstevel@tonic-gate 	/* check for write perm. */
2558*0Sstevel@tonic-gate 	desc    = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
2559*0Sstevel@tonic-gate 	if (!(desc->field.field_perm & SEGMENT_WRITE)) {
2560*0Sstevel@tonic-gate 		errno = EPERM;
2561*0Sstevel@tonic-gate 		return (-1); /* write not allowed */
2562*0Sstevel@tonic-gate 	}
2563*0Sstevel@tonic-gate 
2564*0Sstevel@tonic-gate 	/* section hash object */
2565*0Sstevel@tonic-gate 	sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2566*0Sstevel@tonic-gate 							SECTION_TYPE);
2567*0Sstevel@tonic-gate 	if (sec_hash == NULL) {
2568*0Sstevel@tonic-gate 		return (-1);
2569*0Sstevel@tonic-gate 	}
2570*0Sstevel@tonic-gate 
2571*0Sstevel@tonic-gate 	if (sec_hash->u.sec_obj->section.protection == READ_ONLY_SECTION) {
2572*0Sstevel@tonic-gate 		errno = EPERM;
2573*0Sstevel@tonic-gate 		return (-1); 		/* read-only section */
2574*0Sstevel@tonic-gate 	}
2575*0Sstevel@tonic-gate 
2576*0Sstevel@tonic-gate 	prev_obj	= seg_hash->u.seg_obj->pkt_obj_list;
2577*0Sstevel@tonic-gate 	if (prev_obj == NULL) {
2578*0Sstevel@tonic-gate 		return (-1);
2579*0Sstevel@tonic-gate 	}
2580*0Sstevel@tonic-gate 
2581*0Sstevel@tonic-gate 	/* container hash object */
2582*0Sstevel@tonic-gate 	cont_hash = lookup_handle_object(sec_hash->u.sec_obj->cont_hdl,
2583*0Sstevel@tonic-gate 							CONTAINER_TYPE);
2584*0Sstevel@tonic-gate 	if (cont_hash == NULL) {
2585*0Sstevel@tonic-gate 		return (-1);
2586*0Sstevel@tonic-gate 	}
2587*0Sstevel@tonic-gate 
2588*0Sstevel@tonic-gate 	fd = open(cont_hash->u.cont_obj->device_pathname, O_RDWR);
2589*0Sstevel@tonic-gate 	if (fd < 0) {
2590*0Sstevel@tonic-gate 		return (-1);
2591*0Sstevel@tonic-gate 	}
2592*0Sstevel@tonic-gate 
2593*0Sstevel@tonic-gate 	if (prev_obj->obj_hdl == packet) { /* first object to be deleted */
2594*0Sstevel@tonic-gate 		adjust_packets(fd, prev_obj, prev_obj->u.pkt_obj->next);
2595*0Sstevel@tonic-gate 		seg_hash->u.seg_obj->trailer_offset -=
2596*0Sstevel@tonic-gate 			(prev_obj->u.pkt_obj->tag_size
2597*0Sstevel@tonic-gate 					+ prev_obj->u.pkt_obj->paylen);
2598*0Sstevel@tonic-gate 		free_packet_object(packet, seg_hash);
2599*0Sstevel@tonic-gate 	} else {
2600*0Sstevel@tonic-gate 		for (tmp_obj = prev_obj;
2601*0Sstevel@tonic-gate 			tmp_obj != NULL; tmp_obj = tmp_obj->u.pkt_obj->next) {
2602*0Sstevel@tonic-gate 			/* found the object */
2603*0Sstevel@tonic-gate 			if (tmp_obj->obj_hdl == packet) {
2604*0Sstevel@tonic-gate 				adjust_packets(fd, tmp_obj,
2605*0Sstevel@tonic-gate 						tmp_obj->u.pkt_obj->next);
2606*0Sstevel@tonic-gate 				seg_hash->u.seg_obj->trailer_offset -=
2607*0Sstevel@tonic-gate 					(tmp_obj->u.pkt_obj->tag_size
2608*0Sstevel@tonic-gate 						+ tmp_obj->u.pkt_obj->paylen);
2609*0Sstevel@tonic-gate 				free_packet_object(packet, seg_hash);
2610*0Sstevel@tonic-gate 			}
2611*0Sstevel@tonic-gate 		}
2612*0Sstevel@tonic-gate 	}
2613*0Sstevel@tonic-gate 
2614*0Sstevel@tonic-gate 	seg_hash->u.seg_obj->num_of_packets -= 1;
2615*0Sstevel@tonic-gate 
2616*0Sstevel@tonic-gate 	/* calculate checksum */
2617*0Sstevel@tonic-gate 	crc = get_checksum_crc(fd, seg_hash,
2618*0Sstevel@tonic-gate 				(seg_hash->u.seg_obj->trailer_offset
2619*0Sstevel@tonic-gate 				- seg_hash->u.seg_obj->segment.offset));
2620*0Sstevel@tonic-gate 	/* write trailer at new offset */
2621*0Sstevel@tonic-gate 	retval = pwrite(fd, &trailer, sizeof (trailer),
2622*0Sstevel@tonic-gate 					seg_hash->u.seg_obj->trailer_offset);
2623*0Sstevel@tonic-gate 	if (retval != sizeof (trailer)) {
2624*0Sstevel@tonic-gate 		(void) close(fd);
2625*0Sstevel@tonic-gate 		return (-1);
2626*0Sstevel@tonic-gate 	}
2627*0Sstevel@tonic-gate 
2628*0Sstevel@tonic-gate 	/* write the checksum value */
2629*0Sstevel@tonic-gate 	retval = pwrite(fd, &crc, sizeof (crc),
2630*0Sstevel@tonic-gate 				seg_hash->u.seg_obj->trailer_offset + 1);
2631*0Sstevel@tonic-gate 	(void) close(fd);
2632*0Sstevel@tonic-gate 	if (retval != sizeof (crc)) {
2633*0Sstevel@tonic-gate 		return (-1);
2634*0Sstevel@tonic-gate 	}
2635*0Sstevel@tonic-gate 
2636*0Sstevel@tonic-gate 	*newsegment = seg_hash->obj_hdl; /* return new segment handle */
2637*0Sstevel@tonic-gate 	return (0);
2638*0Sstevel@tonic-gate }
2639*0Sstevel@tonic-gate 
2640*0Sstevel@tonic-gate /*
2641*0Sstevel@tonic-gate  * Description :
2642*0Sstevel@tonic-gate  *		fru_close_container() removes the association between a
2643*0Sstevel@tonic-gate  *		container and its handle. this routines free's up all the
2644*0Sstevel@tonic-gate  *		hash object contained under container.
2645*0Sstevel@tonic-gate  *
2646*0Sstevel@tonic-gate  * Arguments   :
2647*0Sstevel@tonic-gate  *		container_hdl_t holds the file descriptor of the fru.
2648*0Sstevel@tonic-gate  *
2649*0Sstevel@tonic-gate  * Return      :
2650*0Sstevel@tonic-gate  *		int
2651*0Sstevel@tonic-gate  *		return 0.
2652*0Sstevel@tonic-gate  *
2653*0Sstevel@tonic-gate  */
2654*0Sstevel@tonic-gate 
2655*0Sstevel@tonic-gate /* ARGSUSED */
2656*0Sstevel@tonic-gate int
fru_close_container(container_hdl_t container)2657*0Sstevel@tonic-gate fru_close_container(container_hdl_t container)
2658*0Sstevel@tonic-gate {
2659*0Sstevel@tonic-gate 	hash_obj_t	*hash_obj;
2660*0Sstevel@tonic-gate 	hash_obj_t	*prev_hash;
2661*0Sstevel@tonic-gate 	hash_obj_t	*sec_hash_obj;
2662*0Sstevel@tonic-gate 	handle_t	obj_hdl;
2663*0Sstevel@tonic-gate 
2664*0Sstevel@tonic-gate 	/* lookup for container hash object */
2665*0Sstevel@tonic-gate 	hash_obj = lookup_handle_object(container, CONTAINER_TYPE);
2666*0Sstevel@tonic-gate 	if (hash_obj == NULL) {
2667*0Sstevel@tonic-gate 		return (0);
2668*0Sstevel@tonic-gate 	}
2669*0Sstevel@tonic-gate 
2670*0Sstevel@tonic-gate 	/* points to section object list */
2671*0Sstevel@tonic-gate 	sec_hash_obj = hash_obj->u.cont_obj->sec_obj_list;
2672*0Sstevel@tonic-gate 
2673*0Sstevel@tonic-gate 	/* traverse section object list */
2674*0Sstevel@tonic-gate 	while (sec_hash_obj != NULL) {
2675*0Sstevel@tonic-gate 
2676*0Sstevel@tonic-gate 		/* traverse segment hash object in the section */
2677*0Sstevel@tonic-gate 		while (sec_hash_obj->u.sec_obj->seg_obj_list != NULL) {
2678*0Sstevel@tonic-gate 			/* object handle of the segment hash object */
2679*0Sstevel@tonic-gate 			obj_hdl	=
2680*0Sstevel@tonic-gate 			sec_hash_obj->u.sec_obj->seg_obj_list->obj_hdl;
2681*0Sstevel@tonic-gate 			free_segment_hash(obj_hdl, sec_hash_obj);
2682*0Sstevel@tonic-gate 		}
2683*0Sstevel@tonic-gate 
2684*0Sstevel@tonic-gate 		/* going to free section hash object, relink the hash object */
2685*0Sstevel@tonic-gate 		if (sec_hash_obj->prev == NULL) {
2686*0Sstevel@tonic-gate 			hash_table[(sec_hash_obj->obj_hdl % TABLE_SIZE)]
2687*0Sstevel@tonic-gate 							= sec_hash_obj->next;
2688*0Sstevel@tonic-gate 			if (sec_hash_obj->next != NULL) {
2689*0Sstevel@tonic-gate 				sec_hash_obj->next->prev = NULL;
2690*0Sstevel@tonic-gate 			}
2691*0Sstevel@tonic-gate 		} else {
2692*0Sstevel@tonic-gate 			sec_hash_obj->prev->next = sec_hash_obj->next;
2693*0Sstevel@tonic-gate 			if (sec_hash_obj->next != NULL) {
2694*0Sstevel@tonic-gate 				sec_hash_obj->next->prev = sec_hash_obj->prev;
2695*0Sstevel@tonic-gate 			}
2696*0Sstevel@tonic-gate 		}
2697*0Sstevel@tonic-gate 
2698*0Sstevel@tonic-gate 		free(sec_hash_obj->u.sec_obj); /* free section hash object */
2699*0Sstevel@tonic-gate 
2700*0Sstevel@tonic-gate 		prev_hash = sec_hash_obj;
2701*0Sstevel@tonic-gate 
2702*0Sstevel@tonic-gate 		sec_hash_obj = sec_hash_obj->u.sec_obj->next;
2703*0Sstevel@tonic-gate 
2704*0Sstevel@tonic-gate 		free(prev_hash); /* free section hash */
2705*0Sstevel@tonic-gate 	}
2706*0Sstevel@tonic-gate 
2707*0Sstevel@tonic-gate 	/* free container hash object */
2708*0Sstevel@tonic-gate 	if (hash_obj->prev == NULL) {
2709*0Sstevel@tonic-gate 		hash_table[(sec_hash_obj->obj_hdl % TABLE_SIZE)] =
2710*0Sstevel@tonic-gate 							hash_obj->next;
2711*0Sstevel@tonic-gate 		if (hash_obj->next != NULL) {
2712*0Sstevel@tonic-gate 			hash_obj->next->prev = NULL;
2713*0Sstevel@tonic-gate 		}
2714*0Sstevel@tonic-gate 	} else {
2715*0Sstevel@tonic-gate 		hash_obj->prev->next = hash_obj->next;
2716*0Sstevel@tonic-gate 		if (hash_obj->next != NULL) {
2717*0Sstevel@tonic-gate 			hash_obj->next->prev = hash_obj->prev;
2718*0Sstevel@tonic-gate 		}
2719*0Sstevel@tonic-gate 	}
2720*0Sstevel@tonic-gate 
2721*0Sstevel@tonic-gate 	free(hash_obj->u.cont_obj);
2722*0Sstevel@tonic-gate 	free(hash_obj);
2723*0Sstevel@tonic-gate 	return (0);
2724*0Sstevel@tonic-gate }
2725*0Sstevel@tonic-gate 
2726*0Sstevel@tonic-gate /*
2727*0Sstevel@tonic-gate  * Description :
2728*0Sstevel@tonic-gate  *		fru_is_data_available() checks to see if the frudata
2729*0Sstevel@tonic-gate  *		is available on a fru.
2730*0Sstevel@tonic-gate  *
2731*0Sstevel@tonic-gate  * Arguments   :
2732*0Sstevel@tonic-gate  *		picl_nodehdl_t holds the picl node handle of the fru.
2733*0Sstevel@tonic-gate  *
2734*0Sstevel@tonic-gate  * Return      :
2735*0Sstevel@tonic-gate  *		int
2736*0Sstevel@tonic-gate  *		return 1: if FRUID information is available
2737*0Sstevel@tonic-gate  *		return 0: if FRUID information is not present
2738*0Sstevel@tonic-gate  *
2739*0Sstevel@tonic-gate  */
2740*0Sstevel@tonic-gate 
2741*0Sstevel@tonic-gate /* ARGSUSED */
2742*0Sstevel@tonic-gate int
fru_is_data_available(picl_nodehdl_t fru)2743*0Sstevel@tonic-gate fru_is_data_available(picl_nodehdl_t fru)
2744*0Sstevel@tonic-gate {
2745*0Sstevel@tonic-gate 	return (0);
2746*0Sstevel@tonic-gate }
2747