xref: /freebsd/usr.sbin/makefs/ffs/ffs_bswap.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*	$NetBSD: ffs_bswap.c,v 1.28 2004/05/25 14:54:59 hannken Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Manuel Bouyer.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #if defined(_KERNEL)
38 #include <sys/systm.h>
39 #endif
40 
41 #include <ufs/ufs/dinode.h>
42 #include "ffs/ufs_bswap.h"
43 #include <ufs/ffs/fs.h>
44 
45 #if !defined(_KERNEL)
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #define panic(x)	printf("%s\n", (x)), abort()
51 #endif
52 
53 #define	fs_old_postbloff	fs_spare5[0]
54 #define	fs_old_rotbloff		fs_spare5[1]
55 #define	fs_old_postbl_start	fs_maxbsize
56 #define	fs_old_headswitch	fs_id[0]
57 #define	fs_old_trkseek	fs_id[1]
58 #define	fs_old_csmask	fs_spare1[0]
59 #define	fs_old_csshift	fs_spare1[1]
60 
61 #define	FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
62 #define	FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
63 
64 void ffs_csum_swap(struct csum *o, struct csum *n, int size);
65 void ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n);
66 
67 void
68 ffs_sb_swap(struct fs *o, struct fs *n)
69 {
70 	size_t i;
71 	u_int32_t *o32, *n32;
72 
73 	/*
74 	 * In order to avoid a lot of lines, as the first N fields (52)
75 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
76 	 * here to convert them.
77 	 */
78 	o32 = (u_int32_t *)o;
79 	n32 = (u_int32_t *)n;
80 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
81 		n32[i] = bswap32(o32[i]);
82 
83 	n->fs_swuid = bswap64(o->fs_swuid);
84 	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
85 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
86 
87 	/* These fields overlap with a possible location for the
88 	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
89 	 * first half of the historic FS_42POSTBLFMT postbl table.
90 	 */
91 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
92 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
93 	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
94 	n->fs_time = bswap64(o->fs_time);
95 	n->fs_size = bswap64(o->fs_size);
96 	n->fs_dsize = bswap64(o->fs_dsize);
97 	n->fs_csaddr = bswap64(o->fs_csaddr);
98 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
99 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
100 
101 	/* These fields overlap with the second half of the
102 	 * historic FS_42POSTBLFMT postbl table
103 	 */
104 	for (i = 0; i < FSMAXSNAP; i++)
105 		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
106 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
107 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
108 	/* fs_sparecon[28] - ignore for now */
109 	n->fs_flags = bswap32(o->fs_flags);
110 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
111 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
112 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
113 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
114 	n->fs_qbmask = bswap64(o->fs_qbmask);
115 	n->fs_qfmask = bswap64(o->fs_qfmask);
116 	n->fs_state = bswap32(o->fs_state);
117 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
118 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
119 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
120 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
121 
122 	n->fs_magic = bswap32(o->fs_magic);
123 }
124 
125 void
126 ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
127 {
128 
129 	n->di_mode = bswap16(o->di_mode);
130 	n->di_nlink = bswap16(o->di_nlink);
131 	n->di_size = bswap64(o->di_size);
132 	n->di_atime = bswap32(o->di_atime);
133 	n->di_atimensec = bswap32(o->di_atimensec);
134 	n->di_mtime = bswap32(o->di_mtime);
135 	n->di_mtimensec = bswap32(o->di_mtimensec);
136 	n->di_ctime = bswap32(o->di_ctime);
137 	n->di_ctimensec = bswap32(o->di_ctimensec);
138 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
139 	n->di_flags = bswap32(o->di_flags);
140 	n->di_blocks = bswap32(o->di_blocks);
141 	n->di_gen = bswap32(o->di_gen);
142 	n->di_uid = bswap32(o->di_uid);
143 	n->di_gid = bswap32(o->di_gid);
144 }
145 
146 void
147 ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
148 {
149 	n->di_mode = bswap16(o->di_mode);
150 	n->di_nlink = bswap16(o->di_nlink);
151 	n->di_uid = bswap32(o->di_uid);
152 	n->di_gid = bswap32(o->di_gid);
153 	n->di_blksize = bswap32(o->di_blksize);
154 	n->di_size = bswap64(o->di_size);
155 	n->di_blocks = bswap64(o->di_blocks);
156 	n->di_atime = bswap64(o->di_atime);
157 	n->di_atimensec = bswap32(o->di_atimensec);
158 	n->di_mtime = bswap64(o->di_mtime);
159 	n->di_mtimensec = bswap32(o->di_mtimensec);
160 	n->di_ctime = bswap64(o->di_ctime);
161 	n->di_ctimensec = bswap32(o->di_ctimensec);
162 	n->di_birthtime = bswap64(o->di_ctime);
163 	n->di_birthnsec = bswap32(o->di_ctimensec);
164 	n->di_gen = bswap32(o->di_gen);
165 	n->di_kernflags = bswap32(o->di_kernflags);
166 	n->di_flags = bswap32(o->di_flags);
167 	n->di_extsize = bswap32(o->di_extsize);
168 	memcpy(n->di_extb, o->di_extb, (NXADDR + NDADDR + NIADDR) * 8);
169 }
170 
171 void
172 ffs_csum_swap(struct csum *o, struct csum *n, int size)
173 {
174 	size_t i;
175 	u_int32_t *oint, *nint;
176 
177 	oint = (u_int32_t*)o;
178 	nint = (u_int32_t*)n;
179 
180 	for (i = 0; i < size / sizeof(u_int32_t); i++)
181 		nint[i] = bswap32(oint[i]);
182 }
183 
184 void
185 ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
186 {
187 	n->cs_ndir = bswap64(o->cs_ndir);
188 	n->cs_nbfree = bswap64(o->cs_nbfree);
189 	n->cs_nifree = bswap64(o->cs_nifree);
190 	n->cs_nffree = bswap64(o->cs_nffree);
191 }
192 
193 /*
194  * Note that ffs_cg_swap may be called with o == n.
195  */
196 void
197 ffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
198 {
199 	int i;
200 	u_int32_t *n32, *o32;
201 	u_int16_t *n16, *o16;
202 	int32_t btotoff, boff, clustersumoff;
203 
204 	n->cg_firstfield = bswap32(o->cg_firstfield);
205 	n->cg_magic = bswap32(o->cg_magic);
206 	n->cg_old_time = bswap32(o->cg_old_time);
207 	n->cg_cgx = bswap32(o->cg_cgx);
208 	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
209 	n->cg_old_niblk = bswap16(o->cg_old_niblk);
210 	n->cg_ndblk = bswap32(o->cg_ndblk);
211 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
212 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
213 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
214 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
215 	n->cg_rotor = bswap32(o->cg_rotor);
216 	n->cg_frotor = bswap32(o->cg_frotor);
217 	n->cg_irotor = bswap32(o->cg_irotor);
218 	for (i = 0; i < MAXFRAG; i++)
219 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
220 
221 	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
222 	n->cg_old_boff = bswap32(o->cg_old_boff);
223 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
224 	n->cg_freeoff = bswap32(o->cg_freeoff);
225 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
226 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
227 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
228 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
229 	n->cg_niblk = bswap32(o->cg_niblk);
230 	n->cg_initediblk = bswap32(o->cg_initediblk);
231 	n->cg_time = bswap64(o->cg_time);
232 
233 	if (fs->fs_magic == FS_UFS2_MAGIC)
234 		return;
235 
236 	if (n->cg_magic == CG_MAGIC) {
237 		btotoff = n->cg_old_btotoff;
238 		boff = n->cg_old_boff;
239 		clustersumoff = n->cg_clustersumoff;
240 	} else {
241 		btotoff = bswap32(n->cg_old_btotoff);
242 		boff = bswap32(n->cg_old_boff);
243 		clustersumoff = bswap32(n->cg_clustersumoff);
244 	}
245 	n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
246 	o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
247 	n16 = (u_int16_t *)((u_int8_t *)n + boff);
248 	o16 = (u_int16_t *)((u_int8_t *)o + boff);
249 
250 	for (i = 0; i < fs->fs_old_cpg; i++)
251 		n32[i] = bswap32(o32[i]);
252 
253 	for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
254 		n16[i] = bswap16(o16[i]);
255 
256 	n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
257 	o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
258 	for (i = 1; i < fs->fs_contigsumsize + 1; i++)
259 		n32[i] = bswap32(o32[i]);
260 }
261