xref: /onnv-gate/usr/src/cmd/fs.d/udfs/mkfs/udfslib.c (revision 0:68f95e015346)
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 (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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 /*
30*0Sstevel@tonic-gate  * Library of support routines for UDFS data structures and conversions.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include	<stdio.h>
33*0Sstevel@tonic-gate #include	<string.h>
34*0Sstevel@tonic-gate #include	<strings.h>
35*0Sstevel@tonic-gate #include	<sys/time.h>
36*0Sstevel@tonic-gate #include	<sys/mutex.h>
37*0Sstevel@tonic-gate #include	<sys/vnode.h>
38*0Sstevel@tonic-gate #include	<sys/fs/udf_volume.h>
39*0Sstevel@tonic-gate #include	"udfs.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate char *tagerrs[] = {
42*0Sstevel@tonic-gate 	"no error",
43*0Sstevel@tonic-gate 	"invalid checksum",	/* TAGERR_CKSUM */
44*0Sstevel@tonic-gate 	"unknown tag id",	/* TAGERR_ID */
45*0Sstevel@tonic-gate 	"invalid version",	/* TAGERR_VERSION */
46*0Sstevel@tonic-gate 	"CRC length too large",	/* TAGERR_TOOBIG */
47*0Sstevel@tonic-gate 	"invalid CRC",		/* TAGERR_CRC */
48*0Sstevel@tonic-gate 	"location mismatch"	/* TAGERR_LOC */
49*0Sstevel@tonic-gate };
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #ifdef sparc
52*0Sstevel@tonic-gate #define	SWAP16(x) (((x) & 0xff) << 8 | ((x) >> 8) & 0xff)
53*0Sstevel@tonic-gate #define	SWAP32(x) (((x) & 0xff) << 24 | ((x) & 0xff00) << 8 | \
54*0Sstevel@tonic-gate 	((x) & 0xff0000) >> 8 | ((x) >> 24) & 0xff)
55*0Sstevel@tonic-gate #define	SWAP64(x) (SWAP32((x) >> 32) & 0xffffffff | SWAP32(x) << 32)
56*0Sstevel@tonic-gate #else
57*0Sstevel@tonic-gate #define	SWAP16(x) (x)
58*0Sstevel@tonic-gate #define	SWAP32(x) (x)
59*0Sstevel@tonic-gate #define	SWAP64(x) (x)
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate static void ud_swap_ext_ad(struct extent_ad *);
63*0Sstevel@tonic-gate static void ud_swap_tstamp(struct tstamp *);
64*0Sstevel@tonic-gate static void ud_swap_icb_tag(struct icb_tag *);
65*0Sstevel@tonic-gate void ud_swap_short_ad(struct short_ad *);
66*0Sstevel@tonic-gate void ud_swap_long_ad(struct long_ad *);
67*0Sstevel@tonic-gate static void ud_swap_pri_vol_desc(struct pri_vol_desc *);
68*0Sstevel@tonic-gate static void ud_swap_vdp(struct vol_desc_ptr *);
69*0Sstevel@tonic-gate static void ud_swap_iuvd(struct iuvd_desc *);
70*0Sstevel@tonic-gate static void ud_swap_avdp(struct anch_vol_desc_ptr *);
71*0Sstevel@tonic-gate static void ud_swap_part_desc(struct part_desc *);
72*0Sstevel@tonic-gate static void ud_swap_log_desc(struct log_vol_desc *);
73*0Sstevel@tonic-gate static void ud_swap_unall_desc(struct unall_spc_desc *);
74*0Sstevel@tonic-gate static void ud_swap_lvint(struct log_vol_int_desc *);
75*0Sstevel@tonic-gate static void ud_swap_fileset_desc(struct file_set_desc *);
76*0Sstevel@tonic-gate static void ud_swap_term_desc(struct term_desc *);
77*0Sstevel@tonic-gate static void ud_swap_file_id(struct file_id *);
78*0Sstevel@tonic-gate static void ud_swap_file_entry(struct file_entry *, int);
79*0Sstevel@tonic-gate static void ud_swap_alloc_ext(struct alloc_ext_desc *);
80*0Sstevel@tonic-gate static void ud_swap_tstamp(tstamp_t *);
81*0Sstevel@tonic-gate static void ud_swap_space_bitmap(struct space_bmap_desc *);
82*0Sstevel@tonic-gate static uint16_t crc16(uint8_t *, int32_t, int32_t);
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate extern uint32_t ecma_version;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate void
maketag(struct tag * itp,struct tag * otp)87*0Sstevel@tonic-gate maketag(struct tag *itp, struct tag *otp)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	int32_t sum, i;
90*0Sstevel@tonic-gate 	uint8_t *cp;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (itp != otp) {
93*0Sstevel@tonic-gate 		bcopy((unsigned char *)(itp + 1), (unsigned char *)(otp + 1),
94*0Sstevel@tonic-gate 			itp->tag_crc_len);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/* Swap fields */
98*0Sstevel@tonic-gate 	switch (itp->tag_id) {
99*0Sstevel@tonic-gate 		case UD_PRI_VOL_DESC:
100*0Sstevel@tonic-gate 			ud_swap_pri_vol_desc((struct pri_vol_desc *)otp);
101*0Sstevel@tonic-gate 			break;
102*0Sstevel@tonic-gate 		case UD_ANCH_VOL_DESC:
103*0Sstevel@tonic-gate 			ud_swap_avdp((struct anch_vol_desc_ptr *)otp);
104*0Sstevel@tonic-gate 			break;
105*0Sstevel@tonic-gate 		case UD_VOL_DESC_PTR:
106*0Sstevel@tonic-gate 			ud_swap_vdp((struct vol_desc_ptr *)otp);
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		case UD_IMPL_USE_DESC:
109*0Sstevel@tonic-gate 			ud_swap_iuvd((struct iuvd_desc *)otp);
110*0Sstevel@tonic-gate 			break;
111*0Sstevel@tonic-gate 		case UD_PART_DESC:
112*0Sstevel@tonic-gate 			ud_swap_part_desc((struct part_desc *)otp);
113*0Sstevel@tonic-gate 			break;
114*0Sstevel@tonic-gate 		case UD_LOG_VOL_DESC:
115*0Sstevel@tonic-gate 			ud_swap_log_desc((struct log_vol_desc *)otp);
116*0Sstevel@tonic-gate 			break;
117*0Sstevel@tonic-gate 		case UD_UNALL_SPA_DESC:
118*0Sstevel@tonic-gate 			ud_swap_unall_desc((struct unall_spc_desc *)otp);
119*0Sstevel@tonic-gate 			break;
120*0Sstevel@tonic-gate 		case UD_TERM_DESC:
121*0Sstevel@tonic-gate 			ud_swap_term_desc((struct term_desc *)otp);
122*0Sstevel@tonic-gate 			break;
123*0Sstevel@tonic-gate 		case UD_LOG_VOL_INT:
124*0Sstevel@tonic-gate 			/* LINTED */
125*0Sstevel@tonic-gate 			ud_swap_lvint((struct log_vol_int_desc *)otp);
126*0Sstevel@tonic-gate 			break;
127*0Sstevel@tonic-gate 		case UD_FILE_SET_DESC:
128*0Sstevel@tonic-gate 			ud_swap_fileset_desc((struct file_set_desc *)otp);
129*0Sstevel@tonic-gate 			break;
130*0Sstevel@tonic-gate 		case UD_FILE_ID_DESC:
131*0Sstevel@tonic-gate 			ud_swap_file_id((struct file_id *)otp);
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 		case UD_ALLOC_EXT_DESC:
134*0Sstevel@tonic-gate 			break;
135*0Sstevel@tonic-gate 		case UD_INDIRECT_ENT:
136*0Sstevel@tonic-gate 			break;
137*0Sstevel@tonic-gate 		case UD_TERMINAL_ENT:
138*0Sstevel@tonic-gate 			break;
139*0Sstevel@tonic-gate 		case UD_FILE_ENTRY:
140*0Sstevel@tonic-gate 			/* LINTED */
141*0Sstevel@tonic-gate 			ud_swap_file_entry((struct file_entry *)otp, 0);
142*0Sstevel@tonic-gate 			break;
143*0Sstevel@tonic-gate 		case UD_EXT_ATTR_HDR:
144*0Sstevel@tonic-gate 			break;
145*0Sstevel@tonic-gate 		case UD_UNALL_SPA_ENT:
146*0Sstevel@tonic-gate 			break;
147*0Sstevel@tonic-gate 		case UD_SPA_BMAP_DESC:
148*0Sstevel@tonic-gate 			ud_swap_space_bitmap((struct space_bmap_desc *)otp);
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 		case UD_PART_INT_DESC:
151*0Sstevel@tonic-gate 			break;
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 	otp->tag_id = SWAP16(itp->tag_id);
154*0Sstevel@tonic-gate 	otp->tag_desc_ver = SWAP16(itp->tag_desc_ver);
155*0Sstevel@tonic-gate 	otp->tag_cksum = otp->tag_res = 0;
156*0Sstevel@tonic-gate 	otp->tag_sno = SWAP16(itp->tag_sno);
157*0Sstevel@tonic-gate 	otp->tag_crc = SWAP16(crc16((unsigned char *)(otp+1),
158*0Sstevel@tonic-gate 		itp->tag_crc_len, 0));
159*0Sstevel@tonic-gate 	otp->tag_crc_len = SWAP16(itp->tag_crc_len);
160*0Sstevel@tonic-gate 	otp->tag_loc = SWAP32(itp->tag_loc);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	/*
163*0Sstevel@tonic-gate 	 * Now do checksum on tag itself
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	cp = (unsigned char *)otp;
166*0Sstevel@tonic-gate 	sum = 0;
167*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (*otp); i++)
168*0Sstevel@tonic-gate 		sum += *cp++;
169*0Sstevel@tonic-gate 	otp->tag_cksum = sum;
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate int32_t
verifytag(struct tag * tp,uint32_t loc,struct tag * otp,int expect)174*0Sstevel@tonic-gate verifytag(struct tag *tp, uint32_t loc, struct tag *otp, int expect)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	uint8_t *cp;
177*0Sstevel@tonic-gate 	uint32_t id, vers, length, tloc;
178*0Sstevel@tonic-gate 	int sum, i;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	sum = -tp->tag_cksum;
181*0Sstevel@tonic-gate 	cp = (unsigned char *)tp;
182*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (*tp); i++)
183*0Sstevel@tonic-gate 		sum += *cp++;
184*0Sstevel@tonic-gate 	if ((sum & 0xff) != tp->tag_cksum)
185*0Sstevel@tonic-gate 		return (TAGERR_CKSUM);
186*0Sstevel@tonic-gate 	id = SWAP16(tp->tag_id);
187*0Sstevel@tonic-gate 	if (id > 9 && id < 256 || id > 266 || (expect > 0 && id != expect))
188*0Sstevel@tonic-gate 		return (TAGERR_ID);
189*0Sstevel@tonic-gate 	vers = SWAP16(tp->tag_desc_ver);
190*0Sstevel@tonic-gate 	if (vers > ecma_version)
191*0Sstevel@tonic-gate 		return (TAGERR_VERSION);
192*0Sstevel@tonic-gate 	length = SWAP16(tp->tag_crc_len);
193*0Sstevel@tonic-gate 	if (length > MAXBSIZE)
194*0Sstevel@tonic-gate 		return (TAGERR_TOOBIG);
195*0Sstevel@tonic-gate 	if (crc16((unsigned char *)(tp+1), length, SWAP16(tp->tag_crc)) != 0)
196*0Sstevel@tonic-gate 		return (TAGERR_CRC);
197*0Sstevel@tonic-gate 	tloc = SWAP32(tp->tag_loc);
198*0Sstevel@tonic-gate 	if ((int)loc != -1 && tloc != loc)
199*0Sstevel@tonic-gate 		return (TAGERR_LOC);
200*0Sstevel@tonic-gate 	if (!otp)
201*0Sstevel@tonic-gate 		return (0);
202*0Sstevel@tonic-gate 	otp->tag_id = id;
203*0Sstevel@tonic-gate 	otp->tag_desc_ver = vers;
204*0Sstevel@tonic-gate 	otp->tag_cksum = tp->tag_cksum;
205*0Sstevel@tonic-gate 	otp->tag_res = 0;
206*0Sstevel@tonic-gate 	otp->tag_sno = SWAP16(tp->tag_sno);
207*0Sstevel@tonic-gate 	otp->tag_crc = SWAP16(tp->tag_crc);
208*0Sstevel@tonic-gate 	otp->tag_crc_len = length;
209*0Sstevel@tonic-gate 	otp->tag_loc = tloc;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	if (tp != otp)
212*0Sstevel@tonic-gate 		bcopy((unsigned char *)(tp + 1), (unsigned char *)(otp + 1),
213*0Sstevel@tonic-gate 			otp->tag_crc_len);
214*0Sstevel@tonic-gate 	/* Swap fields */
215*0Sstevel@tonic-gate 	switch (otp->tag_id) {
216*0Sstevel@tonic-gate 	case UD_PRI_VOL_DESC:
217*0Sstevel@tonic-gate 		ud_swap_pri_vol_desc((struct pri_vol_desc *)otp);
218*0Sstevel@tonic-gate 		break;
219*0Sstevel@tonic-gate 	case UD_ANCH_VOL_DESC:
220*0Sstevel@tonic-gate 		ud_swap_avdp((struct anch_vol_desc_ptr *)otp);
221*0Sstevel@tonic-gate 		break;
222*0Sstevel@tonic-gate 	case UD_VOL_DESC_PTR:
223*0Sstevel@tonic-gate 		ud_swap_vdp((struct vol_desc_ptr *)otp);
224*0Sstevel@tonic-gate 		break;
225*0Sstevel@tonic-gate 	case UD_IMPL_USE_DESC:
226*0Sstevel@tonic-gate 		ud_swap_iuvd((struct iuvd_desc *)otp);
227*0Sstevel@tonic-gate 		break;
228*0Sstevel@tonic-gate 	case UD_PART_DESC:
229*0Sstevel@tonic-gate 		ud_swap_part_desc((struct part_desc *)otp);
230*0Sstevel@tonic-gate 		break;
231*0Sstevel@tonic-gate 	case UD_LOG_VOL_DESC:
232*0Sstevel@tonic-gate 		ud_swap_log_desc((struct log_vol_desc *)otp);
233*0Sstevel@tonic-gate 		break;
234*0Sstevel@tonic-gate 	case UD_UNALL_SPA_DESC:
235*0Sstevel@tonic-gate 		ud_swap_unall_desc((struct unall_spc_desc *)otp);
236*0Sstevel@tonic-gate 		break;
237*0Sstevel@tonic-gate 	case UD_TERM_DESC:
238*0Sstevel@tonic-gate 		ud_swap_term_desc((struct term_desc *)otp);
239*0Sstevel@tonic-gate 		break;
240*0Sstevel@tonic-gate 	case UD_LOG_VOL_INT:
241*0Sstevel@tonic-gate 		/* LINTED */
242*0Sstevel@tonic-gate 		ud_swap_lvint((struct log_vol_int_desc *)otp);
243*0Sstevel@tonic-gate 		break;
244*0Sstevel@tonic-gate 	case UD_FILE_SET_DESC:
245*0Sstevel@tonic-gate 		ud_swap_fileset_desc((struct file_set_desc *)otp);
246*0Sstevel@tonic-gate 		break;
247*0Sstevel@tonic-gate 	case UD_FILE_ID_DESC:
248*0Sstevel@tonic-gate 		ud_swap_file_id((struct file_id *)otp);
249*0Sstevel@tonic-gate 		break;
250*0Sstevel@tonic-gate 	case UD_ALLOC_EXT_DESC:
251*0Sstevel@tonic-gate 		ud_swap_alloc_ext((struct alloc_ext_desc *)otp);
252*0Sstevel@tonic-gate 		break;
253*0Sstevel@tonic-gate 	case UD_INDIRECT_ENT:
254*0Sstevel@tonic-gate 		break;
255*0Sstevel@tonic-gate 	case UD_TERMINAL_ENT:
256*0Sstevel@tonic-gate 		break;
257*0Sstevel@tonic-gate 	case UD_FILE_ENTRY:
258*0Sstevel@tonic-gate 		/* LINTED */
259*0Sstevel@tonic-gate 		ud_swap_file_entry((struct file_entry *)otp, 1);
260*0Sstevel@tonic-gate 		break;
261*0Sstevel@tonic-gate 	case UD_EXT_ATTR_HDR:
262*0Sstevel@tonic-gate 		break;
263*0Sstevel@tonic-gate 	case UD_UNALL_SPA_ENT:
264*0Sstevel@tonic-gate 		break;
265*0Sstevel@tonic-gate 	case UD_SPA_BMAP_DESC:
266*0Sstevel@tonic-gate 		ud_swap_space_bitmap((struct space_bmap_desc *)otp);
267*0Sstevel@tonic-gate 		break;
268*0Sstevel@tonic-gate 	case UD_PART_INT_DESC:
269*0Sstevel@tonic-gate 		break;
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	return (0);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate static void
ud_swap_ext_ad(struct extent_ad * p)275*0Sstevel@tonic-gate ud_swap_ext_ad(struct extent_ad *p)
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	p->ext_len = SWAP32(p->ext_len);
278*0Sstevel@tonic-gate 	p->ext_loc = SWAP32(p->ext_loc);
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate /* ARGSUSED */
283*0Sstevel@tonic-gate static void
ud_swap_regid(struct regid * p)284*0Sstevel@tonic-gate ud_swap_regid(struct regid *p)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate static void
ud_swap_icb_tag(struct icb_tag * p)289*0Sstevel@tonic-gate ud_swap_icb_tag(struct icb_tag *p)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate 	p->itag_prnde = SWAP32(p->itag_prnde);
292*0Sstevel@tonic-gate 	p->itag_strategy = SWAP16(p->itag_strategy);
293*0Sstevel@tonic-gate 	p->itag_param = SWAP16(p->itag_param);
294*0Sstevel@tonic-gate 	p->itag_max_ent = SWAP16(p->itag_max_ent);
295*0Sstevel@tonic-gate 	p->itag_lb_loc = SWAP32(p->itag_lb_loc);
296*0Sstevel@tonic-gate 	p->itag_lb_prn = SWAP16(p->itag_lb_prn);
297*0Sstevel@tonic-gate 	p->itag_flags = SWAP16(p->itag_flags);
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate void
ud_swap_short_ad(struct short_ad * p)301*0Sstevel@tonic-gate ud_swap_short_ad(struct short_ad *p)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate 	p->sad_ext_len = SWAP32(p->sad_ext_len);
304*0Sstevel@tonic-gate 	p->sad_ext_loc = SWAP32(p->sad_ext_loc);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate void
ud_swap_long_ad(struct long_ad * p)308*0Sstevel@tonic-gate ud_swap_long_ad(struct long_ad *p)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate 	p->lad_ext_len = SWAP32(p->lad_ext_len);
311*0Sstevel@tonic-gate 	p->lad_ext_loc = SWAP32(p->lad_ext_loc);
312*0Sstevel@tonic-gate 	p->lad_ext_prn = SWAP16(p->lad_ext_prn);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate static void
ud_swap_pri_vol_desc(struct pri_vol_desc * p)316*0Sstevel@tonic-gate ud_swap_pri_vol_desc(struct pri_vol_desc *p)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate 	p->pvd_vdsn = SWAP32(p->pvd_vdsn);
319*0Sstevel@tonic-gate 	p->pvd_pvdn = SWAP32(p->pvd_pvdn);
320*0Sstevel@tonic-gate 	p->pvd_vsn = SWAP16(p->pvd_vsn);
321*0Sstevel@tonic-gate 	p->pvd_mvsn = SWAP16(p->pvd_mvsn);
322*0Sstevel@tonic-gate 	p->pvd_il = SWAP16(p->pvd_il);
323*0Sstevel@tonic-gate 	p->pvd_mil = SWAP16(p->pvd_mil);
324*0Sstevel@tonic-gate 	p->pvd_csl = SWAP32(p->pvd_csl);
325*0Sstevel@tonic-gate 	p->pvd_mcsl = SWAP32(p->pvd_mcsl);
326*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->pvd_vol_abs);
327*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->pvd_vcn);
328*0Sstevel@tonic-gate 	ud_swap_regid(&p->pvd_appl_id);
329*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->pvd_time);
330*0Sstevel@tonic-gate 	ud_swap_regid(&p->pvd_ii);
331*0Sstevel@tonic-gate 	p->pvd_pvdsl = SWAP32(p->pvd_pvdsl);
332*0Sstevel@tonic-gate 	p->pvd_flags = SWAP16(p->pvd_flags);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate static void
ud_swap_iuvd(struct iuvd_desc * p)336*0Sstevel@tonic-gate ud_swap_iuvd(struct iuvd_desc *p)
337*0Sstevel@tonic-gate {
338*0Sstevel@tonic-gate 	p->iuvd_vdsn = SWAP32(p->iuvd_vdsn);
339*0Sstevel@tonic-gate 	ud_swap_regid(&p->iuvd_ii);
340*0Sstevel@tonic-gate 	ud_swap_regid(&p->iuvd_iid);
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate static void
ud_swap_vdp(struct vol_desc_ptr * p)344*0Sstevel@tonic-gate ud_swap_vdp(struct vol_desc_ptr *p)
345*0Sstevel@tonic-gate {
346*0Sstevel@tonic-gate 	p->vdp_vdsn = SWAP32(p->vdp_vdsn);
347*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->vdp_nvdse);
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate static void
ud_swap_avdp(struct anch_vol_desc_ptr * p)351*0Sstevel@tonic-gate ud_swap_avdp(struct anch_vol_desc_ptr *p)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->avd_main_vdse);
354*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->avd_res_vdse);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate static void
ud_swap_part_desc(struct part_desc * p)358*0Sstevel@tonic-gate ud_swap_part_desc(struct part_desc *p)
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	struct phdr_desc *php;
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	p->pd_vdsn = SWAP32(p->pd_vdsn);
363*0Sstevel@tonic-gate 	p->pd_pflags = SWAP16(p->pd_pflags);
364*0Sstevel@tonic-gate 	p->pd_pnum = SWAP16(p->pd_pnum);
365*0Sstevel@tonic-gate 	ud_swap_regid(&p->pd_pcontents);
366*0Sstevel@tonic-gate 	p->pd_acc_type = SWAP32(p->pd_acc_type);
367*0Sstevel@tonic-gate 	p->pd_part_start = SWAP32(p->pd_part_start);
368*0Sstevel@tonic-gate 	p->pd_part_length = SWAP32(p->pd_part_length);
369*0Sstevel@tonic-gate 	ud_swap_regid(&p->pd_ii);
370*0Sstevel@tonic-gate 	if (strncmp(p->pd_pcontents.reg_id, "+NSR", 4) == 0) {
371*0Sstevel@tonic-gate 		/* LINTED */
372*0Sstevel@tonic-gate 		php = (struct phdr_desc *)p->pd_pc_use;
373*0Sstevel@tonic-gate 		ud_swap_short_ad(&php->phdr_ust);
374*0Sstevel@tonic-gate 		ud_swap_short_ad(&php->phdr_usb);
375*0Sstevel@tonic-gate 		ud_swap_short_ad(&php->phdr_it);
376*0Sstevel@tonic-gate 		ud_swap_short_ad(&php->phdr_fst);
377*0Sstevel@tonic-gate 		ud_swap_short_ad(&php->phdr_fsb);
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate static void
ud_swap_log_desc(struct log_vol_desc * p)382*0Sstevel@tonic-gate ud_swap_log_desc(struct log_vol_desc *p)
383*0Sstevel@tonic-gate {
384*0Sstevel@tonic-gate 	p->lvd_vdsn = SWAP32(p->lvd_vdsn);
385*0Sstevel@tonic-gate 	p->lvd_log_bsize = SWAP32(p->lvd_log_bsize);
386*0Sstevel@tonic-gate 	ud_swap_regid(&p->lvd_dom_id);
387*0Sstevel@tonic-gate 	ud_swap_long_ad(&p->lvd_lvcu);
388*0Sstevel@tonic-gate 	p->lvd_mtbl_len = SWAP32(p->lvd_mtbl_len);
389*0Sstevel@tonic-gate 	p->lvd_num_pmaps = SWAP32(p->lvd_num_pmaps);
390*0Sstevel@tonic-gate 	ud_swap_regid(&p->lvd_ii);
391*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->lvd_int_seq_ext);
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate static void
ud_swap_unall_desc(struct unall_spc_desc * p)395*0Sstevel@tonic-gate ud_swap_unall_desc(struct unall_spc_desc *p)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	p->ua_vdsn = SWAP32(p->ua_vdsn);
398*0Sstevel@tonic-gate 	p->ua_nad = SWAP32(p->ua_nad);
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate static void
ud_swap_lvint(struct log_vol_int_desc * p)402*0Sstevel@tonic-gate ud_swap_lvint(struct log_vol_int_desc *p)
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate 	struct lvid_iu *lvup;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->lvid_tstamp);
407*0Sstevel@tonic-gate 	p->lvid_int_type = SWAP32(p->lvid_int_type);
408*0Sstevel@tonic-gate 	ud_swap_ext_ad(&p->lvid_nie);
409*0Sstevel@tonic-gate 	p->lvid_npart = SWAP32(p->lvid_npart);
410*0Sstevel@tonic-gate 	p->lvid_liu = SWAP32(p->lvid_liu);
411*0Sstevel@tonic-gate 	p->lvid_uniqid = SWAP64(p->lvid_uniqid);
412*0Sstevel@tonic-gate 	p->lvid_fst[0] = SWAP32(p->lvid_fst[0]);
413*0Sstevel@tonic-gate 	p->lvid_fst[1] = SWAP32(p->lvid_fst[1]);
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	lvup = (struct lvid_iu *)&p->lvid_fst[2];
416*0Sstevel@tonic-gate 	ud_swap_regid(&lvup->lvidiu_regid);
417*0Sstevel@tonic-gate 	lvup->lvidiu_nfiles = SWAP32(lvup->lvidiu_nfiles);
418*0Sstevel@tonic-gate 	lvup->lvidiu_ndirs = SWAP32(lvup->lvidiu_ndirs);
419*0Sstevel@tonic-gate 	lvup->lvidiu_mread = SWAP16(lvup->lvidiu_mread);
420*0Sstevel@tonic-gate 	lvup->lvidiu_mwrite = SWAP16(lvup->lvidiu_mwrite);
421*0Sstevel@tonic-gate 	lvup->lvidiu_maxwr = SWAP16(lvup->lvidiu_maxwr);
422*0Sstevel@tonic-gate }
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate static void
ud_swap_fileset_desc(struct file_set_desc * p)425*0Sstevel@tonic-gate ud_swap_fileset_desc(struct file_set_desc *p)
426*0Sstevel@tonic-gate {
427*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->fsd_time);
428*0Sstevel@tonic-gate 	p->fsd_ilevel = SWAP16(p->fsd_ilevel);
429*0Sstevel@tonic-gate 	p->fsd_mi_level = SWAP16(p->fsd_mi_level);
430*0Sstevel@tonic-gate 	p->fsd_cs_list = SWAP32(p->fsd_cs_list);
431*0Sstevel@tonic-gate 	p->fsd_mcs_list = SWAP32(p->fsd_mcs_list);
432*0Sstevel@tonic-gate 	p->fsd_fs_no = SWAP32(p->fsd_fs_no);
433*0Sstevel@tonic-gate 	p->fsd_fsd_no = SWAP32(p->fsd_fsd_no);
434*0Sstevel@tonic-gate 	ud_swap_long_ad(&p->fsd_root_icb);
435*0Sstevel@tonic-gate 	ud_swap_regid(&p->fsd_did);
436*0Sstevel@tonic-gate 	ud_swap_long_ad(&p->fsd_next);
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate /* ARGSUSED */
440*0Sstevel@tonic-gate static void
ud_swap_term_desc(struct term_desc * p)441*0Sstevel@tonic-gate ud_swap_term_desc(struct term_desc *p)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate }
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate static void
ud_swap_file_id(struct file_id * p)446*0Sstevel@tonic-gate ud_swap_file_id(struct file_id *p)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate 	p->fid_ver = SWAP16(p->fid_ver);
449*0Sstevel@tonic-gate 	ud_swap_long_ad(&p->fid_icb);
450*0Sstevel@tonic-gate 	p->fid_iulen = SWAP16(p->fid_iulen);
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate static void
ud_swap_alloc_ext(struct alloc_ext_desc * p)454*0Sstevel@tonic-gate ud_swap_alloc_ext(struct alloc_ext_desc *p)
455*0Sstevel@tonic-gate {
456*0Sstevel@tonic-gate 	p->aed_rev_ael = SWAP32(p->aed_rev_ael);
457*0Sstevel@tonic-gate 	p->aed_len_aed = SWAP32(p->aed_len_aed);
458*0Sstevel@tonic-gate }
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate static void
ud_swap_space_bitmap(struct space_bmap_desc * p)461*0Sstevel@tonic-gate ud_swap_space_bitmap(struct space_bmap_desc *p)
462*0Sstevel@tonic-gate {
463*0Sstevel@tonic-gate 	p->sbd_nbits = SWAP32(p->sbd_nbits);
464*0Sstevel@tonic-gate 	p->sbd_nbytes = SWAP32(p->sbd_nbytes);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate static void
ud_swap_file_entry(struct file_entry * p,int32_t rdflag)468*0Sstevel@tonic-gate ud_swap_file_entry(struct file_entry *p, int32_t rdflag)
469*0Sstevel@tonic-gate {
470*0Sstevel@tonic-gate 	int32_t i;
471*0Sstevel@tonic-gate 	short_ad_t *sap;
472*0Sstevel@tonic-gate 	long_ad_t *lap;
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	/* Do Extended Attributes and Allocation Descriptors */
475*0Sstevel@tonic-gate 	if (rdflag) {
476*0Sstevel@tonic-gate 		p->fe_len_adesc = SWAP32(p->fe_len_adesc);
477*0Sstevel@tonic-gate 		p->fe_len_ear = SWAP32(p->fe_len_ear);
478*0Sstevel@tonic-gate 		ud_swap_icb_tag(&p->fe_icb_tag);
479*0Sstevel@tonic-gate 	}
480*0Sstevel@tonic-gate 	switch (p->fe_icb_tag.itag_flags & 0x3) {
481*0Sstevel@tonic-gate 	case ICB_FLAG_SHORT_AD:
482*0Sstevel@tonic-gate 		/* LINTED */
483*0Sstevel@tonic-gate 		sap = (short_ad_t *)(p->fe_spec + p->fe_len_ear);
484*0Sstevel@tonic-gate 		for (i = 0; i < p->fe_len_adesc / sizeof (short_ad_t);
485*0Sstevel@tonic-gate 			i++, sap++)
486*0Sstevel@tonic-gate 			ud_swap_short_ad(sap);
487*0Sstevel@tonic-gate 		break;
488*0Sstevel@tonic-gate 	case ICB_FLAG_LONG_AD:
489*0Sstevel@tonic-gate 		/* LINTED */
490*0Sstevel@tonic-gate 		lap = (long_ad_t *)(p->fe_spec + p->fe_len_ear);
491*0Sstevel@tonic-gate 		for (i = 0; i < p->fe_len_adesc / sizeof (long_ad_t);
492*0Sstevel@tonic-gate 			i++, lap++)
493*0Sstevel@tonic-gate 			ud_swap_long_ad(lap);
494*0Sstevel@tonic-gate 		break;
495*0Sstevel@tonic-gate 	case ICB_FLAG_EXT_AD:
496*0Sstevel@tonic-gate 		break;
497*0Sstevel@tonic-gate 	case ICB_FLAG_ONE_AD:
498*0Sstevel@tonic-gate 		break;
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 	p->fe_uid = SWAP32(p->fe_uid);
501*0Sstevel@tonic-gate 	p->fe_gid = SWAP32(p->fe_gid);
502*0Sstevel@tonic-gate 	p->fe_perms = SWAP32(p->fe_perms);
503*0Sstevel@tonic-gate 	p->fe_lcount = SWAP16(p->fe_lcount);
504*0Sstevel@tonic-gate 	p->fe_rec_len = SWAP32(p->fe_rec_len);
505*0Sstevel@tonic-gate 	p->fe_info_len = SWAP64(p->fe_info_len);
506*0Sstevel@tonic-gate 	p->fe_lbr = SWAP64(p->fe_lbr);
507*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->fe_acc_time);
508*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->fe_mod_time);
509*0Sstevel@tonic-gate 	ud_swap_tstamp(&p->fe_attr_time);
510*0Sstevel@tonic-gate 	p->fe_ckpoint = SWAP32(p->fe_ckpoint);
511*0Sstevel@tonic-gate 	ud_swap_long_ad(&p->fe_ea_icb);
512*0Sstevel@tonic-gate 	ud_swap_regid(&p->fe_impl_id);
513*0Sstevel@tonic-gate 	p->fe_uniq_id = SWAP64(p->fe_uniq_id);
514*0Sstevel@tonic-gate 	if (!rdflag) {
515*0Sstevel@tonic-gate 		p->fe_len_adesc = SWAP32(p->fe_len_adesc);
516*0Sstevel@tonic-gate 		p->fe_len_ear = SWAP32(p->fe_len_ear);
517*0Sstevel@tonic-gate 		ud_swap_icb_tag(&p->fe_icb_tag);
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate }
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate static void
ud_swap_tstamp(tstamp_t * tp)522*0Sstevel@tonic-gate ud_swap_tstamp(tstamp_t *tp)
523*0Sstevel@tonic-gate {
524*0Sstevel@tonic-gate 	tp->ts_tzone = SWAP16(tp->ts_tzone);
525*0Sstevel@tonic-gate 	tp->ts_year = SWAP16(tp->ts_year);
526*0Sstevel@tonic-gate }
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate void
setcharspec(struct charspec * cp,int32_t type,uint8_t * info)529*0Sstevel@tonic-gate setcharspec(struct charspec *cp, int32_t type, uint8_t *info)
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate 	cp->cs_type = type;
532*0Sstevel@tonic-gate 	bzero(cp->cs_info, sizeof (cp->cs_info));
533*0Sstevel@tonic-gate 	(void) strncpy(cp->cs_info, (int8_t *)info, sizeof (cp->cs_info));
534*0Sstevel@tonic-gate }
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate static unsigned short crctab[] = {
537*0Sstevel@tonic-gate 	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
538*0Sstevel@tonic-gate 	0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
539*0Sstevel@tonic-gate 	0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
540*0Sstevel@tonic-gate 	0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
541*0Sstevel@tonic-gate 	0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
542*0Sstevel@tonic-gate 	0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
543*0Sstevel@tonic-gate 	0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
544*0Sstevel@tonic-gate 	0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
545*0Sstevel@tonic-gate 	0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
546*0Sstevel@tonic-gate 	0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
547*0Sstevel@tonic-gate 	0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
548*0Sstevel@tonic-gate 	0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
549*0Sstevel@tonic-gate 	0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
550*0Sstevel@tonic-gate 	0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
551*0Sstevel@tonic-gate 	0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
552*0Sstevel@tonic-gate 	0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
553*0Sstevel@tonic-gate 	0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
554*0Sstevel@tonic-gate 	0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
555*0Sstevel@tonic-gate 	0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
556*0Sstevel@tonic-gate 	0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
557*0Sstevel@tonic-gate 	0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
558*0Sstevel@tonic-gate 	0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
559*0Sstevel@tonic-gate 	0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
560*0Sstevel@tonic-gate 	0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
561*0Sstevel@tonic-gate 	0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
562*0Sstevel@tonic-gate 	0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
563*0Sstevel@tonic-gate 	0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
564*0Sstevel@tonic-gate 	0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
565*0Sstevel@tonic-gate 	0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
566*0Sstevel@tonic-gate 	0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
567*0Sstevel@tonic-gate 	0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
568*0Sstevel@tonic-gate 	0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
569*0Sstevel@tonic-gate };
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate static uint16_t
crc16(uint8_t * buf,int32_t size,int32_t rem)572*0Sstevel@tonic-gate crc16(uint8_t *buf, int32_t size, int32_t rem)
573*0Sstevel@tonic-gate {
574*0Sstevel@tonic-gate 	uint16_t crc = 0;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	while (size-- > 0)
577*0Sstevel@tonic-gate 		crc = (crc << 8) ^ crctab[((crc >> 8) ^ *buf++) & 0xff];
578*0Sstevel@tonic-gate 	return ((crc ^ rem) & 0xffff);
579*0Sstevel@tonic-gate }
580