xref: /freebsd/usr.sbin/makefs/ffs/ffs_bswap.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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 #if !defined(_KERNEL)
42 #include <stddef.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #define panic(x)	printf("%s\n", (x)), abort()
48 #endif
49 
50 #include <ufs/ufs/dinode.h>
51 #include "ffs/ufs_bswap.h"
52 #include <ufs/ffs/fs.h>
53 
54 #define	fs_old_postbloff	fs_spare5[0]
55 #define	fs_old_rotbloff		fs_spare5[1]
56 #define	fs_old_postbl_start	fs_maxbsize
57 #define	fs_old_headswitch	fs_id[0]
58 #define	fs_old_trkseek	fs_id[1]
59 #define	fs_old_csmask	fs_spare1[0]
60 #define	fs_old_csshift	fs_spare1[1]
61 
62 #define	FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
63 #define	FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
64 
65 void ffs_csum_swap(struct csum *o, struct csum *n, int size);
66 void ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n);
67 
68 void
69 ffs_sb_swap(struct fs *o, struct fs *n)
70 {
71 	size_t i;
72 	u_int32_t *o32, *n32;
73 
74 	/*
75 	 * In order to avoid a lot of lines, as the first N fields (52)
76 	 * of the superblock up to fs_fmod are u_int32_t, we just loop
77 	 * here to convert them.
78 	 */
79 	o32 = (u_int32_t *)o;
80 	n32 = (u_int32_t *)n;
81 	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
82 		n32[i] = bswap32(o32[i]);
83 
84 	n->fs_swuid = bswap64(o->fs_swuid);
85 	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
86 	n->fs_old_cpc = bswap32(o->fs_old_cpc);
87 
88 	/* These fields overlap with a possible location for the
89 	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
90 	 * first half of the historic FS_42POSTBLFMT postbl table.
91 	 */
92 	n->fs_maxbsize = bswap32(o->fs_maxbsize);
93 	n->fs_sblockloc = bswap64(o->fs_sblockloc);
94 	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
95 	n->fs_time = bswap64(o->fs_time);
96 	n->fs_size = bswap64(o->fs_size);
97 	n->fs_dsize = bswap64(o->fs_dsize);
98 	n->fs_csaddr = bswap64(o->fs_csaddr);
99 	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
100 	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
101 
102 	/* These fields overlap with the second half of the
103 	 * historic FS_42POSTBLFMT postbl table
104 	 */
105 	for (i = 0; i < FSMAXSNAP; i++)
106 		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
107 	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
108 	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
109 	/* fs_sparecon[28] - ignore for now */
110 	n->fs_flags = bswap32(o->fs_flags);
111 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
112 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
113 	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
114 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
115 	n->fs_qbmask = bswap64(o->fs_qbmask);
116 	n->fs_qfmask = bswap64(o->fs_qfmask);
117 	n->fs_state = bswap32(o->fs_state);
118 	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
119 	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
120 	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
121 	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
122 
123 	n->fs_magic = bswap32(o->fs_magic);
124 }
125 
126 void
127 ffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
128 {
129 
130 	n->di_mode = bswap16(o->di_mode);
131 	n->di_nlink = bswap16(o->di_nlink);
132 	n->di_size = bswap64(o->di_size);
133 	n->di_atime = bswap32(o->di_atime);
134 	n->di_atimensec = bswap32(o->di_atimensec);
135 	n->di_mtime = bswap32(o->di_mtime);
136 	n->di_mtimensec = bswap32(o->di_mtimensec);
137 	n->di_ctime = bswap32(o->di_ctime);
138 	n->di_ctimensec = bswap32(o->di_ctimensec);
139 	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
140 	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
141 	n->di_flags = bswap32(o->di_flags);
142 	n->di_blocks = bswap32(o->di_blocks);
143 	n->di_gen = bswap32(o->di_gen);
144 	n->di_uid = bswap32(o->di_uid);
145 	n->di_gid = bswap32(o->di_gid);
146 }
147 
148 void
149 ffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
150 {
151 	n->di_mode = bswap16(o->di_mode);
152 	n->di_nlink = bswap16(o->di_nlink);
153 	n->di_uid = bswap32(o->di_uid);
154 	n->di_gid = bswap32(o->di_gid);
155 	n->di_blksize = bswap32(o->di_blksize);
156 	n->di_size = bswap64(o->di_size);
157 	n->di_blocks = bswap64(o->di_blocks);
158 	n->di_atime = bswap64(o->di_atime);
159 	n->di_atimensec = bswap32(o->di_atimensec);
160 	n->di_mtime = bswap64(o->di_mtime);
161 	n->di_mtimensec = bswap32(o->di_mtimensec);
162 	n->di_ctime = bswap64(o->di_ctime);
163 	n->di_ctimensec = bswap32(o->di_ctimensec);
164 	n->di_birthtime = bswap64(o->di_ctime);
165 	n->di_birthnsec = bswap32(o->di_ctimensec);
166 	n->di_gen = bswap32(o->di_gen);
167 	n->di_kernflags = bswap32(o->di_kernflags);
168 	n->di_flags = bswap32(o->di_flags);
169 	n->di_extsize = bswap32(o->di_extsize);
170 	memcpy(n->di_extb, o->di_extb, sizeof(n->di_extb));
171 	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
172 	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
173 }
174 
175 void
176 ffs_csum_swap(struct csum *o, struct csum *n, int size)
177 {
178 	size_t i;
179 	u_int32_t *oint, *nint;
180 
181 	oint = (u_int32_t*)o;
182 	nint = (u_int32_t*)n;
183 
184 	for (i = 0; i < size / sizeof(u_int32_t); i++)
185 		nint[i] = bswap32(oint[i]);
186 }
187 
188 void
189 ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
190 {
191 	n->cs_ndir = bswap64(o->cs_ndir);
192 	n->cs_nbfree = bswap64(o->cs_nbfree);
193 	n->cs_nifree = bswap64(o->cs_nifree);
194 	n->cs_nffree = bswap64(o->cs_nffree);
195 }
196 
197 /*
198  * Note that ffs_cg_swap may be called with o == n.
199  */
200 void
201 ffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
202 {
203 	int i;
204 	u_int32_t *n32, *o32;
205 	u_int16_t *n16, *o16;
206 	int32_t btotoff, boff, clustersumoff;
207 
208 	n->cg_firstfield = bswap32(o->cg_firstfield);
209 	n->cg_magic = bswap32(o->cg_magic);
210 	n->cg_old_time = bswap32(o->cg_old_time);
211 	n->cg_cgx = bswap32(o->cg_cgx);
212 	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
213 	n->cg_old_niblk = bswap16(o->cg_old_niblk);
214 	n->cg_ndblk = bswap32(o->cg_ndblk);
215 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
216 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
217 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
218 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
219 	n->cg_rotor = bswap32(o->cg_rotor);
220 	n->cg_frotor = bswap32(o->cg_frotor);
221 	n->cg_irotor = bswap32(o->cg_irotor);
222 	for (i = 0; i < MAXFRAG; i++)
223 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
224 
225 	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
226 	n->cg_old_boff = bswap32(o->cg_old_boff);
227 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
228 	n->cg_freeoff = bswap32(o->cg_freeoff);
229 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
230 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
231 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
232 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
233 	n->cg_niblk = bswap32(o->cg_niblk);
234 	n->cg_initediblk = bswap32(o->cg_initediblk);
235 	n->cg_time = bswap64(o->cg_time);
236 
237 	if (fs->fs_magic == FS_UFS2_MAGIC)
238 		return;
239 
240 	if (n->cg_magic == CG_MAGIC) {
241 		btotoff = n->cg_old_btotoff;
242 		boff = n->cg_old_boff;
243 		clustersumoff = n->cg_clustersumoff;
244 	} else {
245 		btotoff = bswap32(n->cg_old_btotoff);
246 		boff = bswap32(n->cg_old_boff);
247 		clustersumoff = bswap32(n->cg_clustersumoff);
248 	}
249 	n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
250 	o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
251 	n16 = (u_int16_t *)((u_int8_t *)n + boff);
252 	o16 = (u_int16_t *)((u_int8_t *)o + boff);
253 
254 	for (i = 0; i < fs->fs_old_cpg; i++)
255 		n32[i] = bswap32(o32[i]);
256 
257 	for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
258 		n16[i] = bswap16(o16[i]);
259 
260 	n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
261 	o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
262 	for (i = 1; i < fs->fs_contigsumsize + 1; i++)
263 		n32[i] = bswap32(o32[i]);
264 }
265