1185222Ssam/*	$NetBSD: ffs_bswap.c,v 1.28 2004/05/25 14:54:59 hannken Exp $	*/
2185222Ssam
3330449Seadler/*-
4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5330449Seadler *
6185222Ssam * Copyright (c) 1998 Manuel Bouyer.
7185222Ssam *
8185222Ssam * Redistribution and use in source and binary forms, with or without
9185222Ssam * modification, are permitted provided that the following conditions
10185222Ssam * are met:
11185222Ssam * 1. Redistributions of source code must retain the above copyright
12185222Ssam *    notice, this list of conditions and the following disclaimer.
13185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
14185222Ssam *    notice, this list of conditions and the following disclaimer in the
15185222Ssam *    documentation and/or other materials provided with the distribution.
16185222Ssam * 3. All advertising materials mentioning features or use of this software
17185222Ssam *    must display the following acknowledgement:
18185222Ssam *	This product includes software developed by Manuel Bouyer.
19185222Ssam * 4. The name of the author may not be used to endorse or promote products
20185222Ssam *    derived from this software without specific prior written permission.
21185222Ssam *
22185222Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23185222Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24185222Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25185222Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26185222Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27185222Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28185222Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29185222Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30185222Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31185222Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32185222Ssam *
33185222Ssam */
34185222Ssam
35185222Ssam#include <sys/cdefs.h>
36186261Ssam__FBSDID("$FreeBSD: stable/11/usr.sbin/makefs/ffs/ffs_bswap.c 332980 2018-04-25 01:35:06Z benno $");
37185222Ssam
38185222Ssam#include <sys/param.h>
39185222Ssam#if defined(_KERNEL)
40185222Ssam#include <sys/systm.h>
41185222Ssam#endif
42185222Ssam
43185222Ssam#if !defined(_KERNEL)
44185222Ssam#include <stddef.h>
45332980Sbenno#include <stdint.h>
46185222Ssam#include <stdio.h>
47185222Ssam#include <stdlib.h>
48185222Ssam#include <string.h>
49185222Ssam#define panic(x)	printf("%s\n", (x)), abort()
50185222Ssam#endif
51185222Ssam
52332980Sbenno#include <ufs/ufs/dinode.h>
53332980Sbenno#include "ffs/ufs_bswap.h"
54332980Sbenno#include <ufs/ffs/fs.h>
55332980Sbenno
56186261Ssam#define	fs_old_postbloff	fs_spare5[0]
57186261Ssam#define	fs_old_rotbloff		fs_spare5[1]
58186261Ssam#define	fs_old_postbl_start	fs_maxbsize
59186261Ssam#define	fs_old_headswitch	fs_id[0]
60186261Ssam#define	fs_old_trkseek	fs_id[1]
61186261Ssam#define	fs_old_csmask	fs_spare1[0]
62186261Ssam#define	fs_old_csshift	fs_spare1[1]
63186261Ssam
64186261Ssam#define	FS_42POSTBLFMT		-1	/* 4.2BSD rotational table format */
65186261Ssam#define	FS_DYNAMICPOSTBLFMT	1	/* dynamic rotational table format */
66186261Ssam
67186261Ssamvoid ffs_csum_swap(struct csum *o, struct csum *n, int size);
68186261Ssamvoid ffs_csumtotal_swap(struct csum_total *o, struct csum_total *n);
69186261Ssam
70185222Ssamvoid
71185222Ssamffs_sb_swap(struct fs *o, struct fs *n)
72185222Ssam{
73290268Sngie	size_t i;
74185222Ssam	u_int32_t *o32, *n32;
75185222Ssam
76185222Ssam	/*
77185222Ssam	 * In order to avoid a lot of lines, as the first N fields (52)
78185222Ssam	 * of the superblock up to fs_fmod are u_int32_t, we just loop
79185222Ssam	 * here to convert them.
80185222Ssam	 */
81185222Ssam	o32 = (u_int32_t *)o;
82185222Ssam	n32 = (u_int32_t *)n;
83185222Ssam	for (i = 0; i < offsetof(struct fs, fs_fmod) / sizeof(u_int32_t); i++)
84185222Ssam		n32[i] = bswap32(o32[i]);
85185222Ssam
86185222Ssam	n->fs_swuid = bswap64(o->fs_swuid);
87185222Ssam	n->fs_cgrotor = bswap32(o->fs_cgrotor); /* Unused */
88185222Ssam	n->fs_old_cpc = bswap32(o->fs_old_cpc);
89185222Ssam
90185222Ssam	/* These fields overlap with a possible location for the
91185222Ssam	 * historic FS_DYNAMICPOSTBLFMT postbl table, and with the
92185222Ssam	 * first half of the historic FS_42POSTBLFMT postbl table.
93185222Ssam	 */
94185222Ssam	n->fs_maxbsize = bswap32(o->fs_maxbsize);
95185222Ssam	n->fs_sblockloc = bswap64(o->fs_sblockloc);
96185222Ssam	ffs_csumtotal_swap(&o->fs_cstotal, &n->fs_cstotal);
97185222Ssam	n->fs_time = bswap64(o->fs_time);
98185222Ssam	n->fs_size = bswap64(o->fs_size);
99185222Ssam	n->fs_dsize = bswap64(o->fs_dsize);
100185222Ssam	n->fs_csaddr = bswap64(o->fs_csaddr);
101185222Ssam	n->fs_pendingblocks = bswap64(o->fs_pendingblocks);
102185222Ssam	n->fs_pendinginodes = bswap32(o->fs_pendinginodes);
103290268Sngie
104185222Ssam	/* These fields overlap with the second half of the
105185222Ssam	 * historic FS_42POSTBLFMT postbl table
106185222Ssam	 */
107185222Ssam	for (i = 0; i < FSMAXSNAP; i++)
108185222Ssam		n->fs_snapinum[i] = bswap32(o->fs_snapinum[i]);
109185222Ssam	n->fs_avgfilesize = bswap32(o->fs_avgfilesize);
110185222Ssam	n->fs_avgfpdir = bswap32(o->fs_avgfpdir);
111185222Ssam	/* fs_sparecon[28] - ignore for now */
112185222Ssam	n->fs_flags = bswap32(o->fs_flags);
113185222Ssam	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
114185222Ssam	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
115185222Ssam	n->fs_old_inodefmt = bswap32(o->fs_old_inodefmt);
116185222Ssam	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
117185222Ssam	n->fs_qbmask = bswap64(o->fs_qbmask);
118185222Ssam	n->fs_qfmask = bswap64(o->fs_qfmask);
119185222Ssam	n->fs_state = bswap32(o->fs_state);
120185222Ssam	n->fs_old_postblformat = bswap32(o->fs_old_postblformat);
121185222Ssam	n->fs_old_nrpos = bswap32(o->fs_old_nrpos);
122185222Ssam	n->fs_old_postbloff = bswap32(o->fs_old_postbloff);
123185222Ssam	n->fs_old_rotbloff = bswap32(o->fs_old_rotbloff);
124185222Ssam
125185222Ssam	n->fs_magic = bswap32(o->fs_magic);
126185222Ssam}
127185222Ssam
128185222Ssamvoid
129185222Ssamffs_dinode1_swap(struct ufs1_dinode *o, struct ufs1_dinode *n)
130185222Ssam{
131185222Ssam
132185222Ssam	n->di_mode = bswap16(o->di_mode);
133185222Ssam	n->di_nlink = bswap16(o->di_nlink);
134185222Ssam	n->di_size = bswap64(o->di_size);
135185222Ssam	n->di_atime = bswap32(o->di_atime);
136185222Ssam	n->di_atimensec = bswap32(o->di_atimensec);
137185222Ssam	n->di_mtime = bswap32(o->di_mtime);
138185222Ssam	n->di_mtimensec = bswap32(o->di_mtimensec);
139185222Ssam	n->di_ctime = bswap32(o->di_ctime);
140185222Ssam	n->di_ctimensec = bswap32(o->di_ctimensec);
141299461Scem	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
142299461Scem	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
143185222Ssam	n->di_flags = bswap32(o->di_flags);
144185222Ssam	n->di_blocks = bswap32(o->di_blocks);
145185222Ssam	n->di_gen = bswap32(o->di_gen);
146185222Ssam	n->di_uid = bswap32(o->di_uid);
147185222Ssam	n->di_gid = bswap32(o->di_gid);
148185222Ssam}
149185222Ssam
150185222Ssamvoid
151185222Ssamffs_dinode2_swap(struct ufs2_dinode *o, struct ufs2_dinode *n)
152185222Ssam{
153185222Ssam	n->di_mode = bswap16(o->di_mode);
154185222Ssam	n->di_nlink = bswap16(o->di_nlink);
155185222Ssam	n->di_uid = bswap32(o->di_uid);
156185222Ssam	n->di_gid = bswap32(o->di_gid);
157185222Ssam	n->di_blksize = bswap32(o->di_blksize);
158185222Ssam	n->di_size = bswap64(o->di_size);
159185222Ssam	n->di_blocks = bswap64(o->di_blocks);
160185222Ssam	n->di_atime = bswap64(o->di_atime);
161185222Ssam	n->di_atimensec = bswap32(o->di_atimensec);
162185222Ssam	n->di_mtime = bswap64(o->di_mtime);
163185222Ssam	n->di_mtimensec = bswap32(o->di_mtimensec);
164185222Ssam	n->di_ctime = bswap64(o->di_ctime);
165185222Ssam	n->di_ctimensec = bswap32(o->di_ctimensec);
166185222Ssam	n->di_birthtime = bswap64(o->di_ctime);
167185222Ssam	n->di_birthnsec = bswap32(o->di_ctimensec);
168185222Ssam	n->di_gen = bswap32(o->di_gen);
169185222Ssam	n->di_kernflags = bswap32(o->di_kernflags);
170185222Ssam	n->di_flags = bswap32(o->di_flags);
171185222Ssam	n->di_extsize = bswap32(o->di_extsize);
172299461Scem	memcpy(n->di_extb, o->di_extb, sizeof(n->di_extb));
173299461Scem	memcpy(n->di_db, o->di_db, sizeof(n->di_db));
174299461Scem	memcpy(n->di_ib, o->di_ib, sizeof(n->di_ib));
175185222Ssam}
176185222Ssam
177185222Ssamvoid
178185222Ssamffs_csum_swap(struct csum *o, struct csum *n, int size)
179185222Ssam{
180290268Sngie	size_t i;
181185222Ssam	u_int32_t *oint, *nint;
182290268Sngie
183185222Ssam	oint = (u_int32_t*)o;
184185222Ssam	nint = (u_int32_t*)n;
185185222Ssam
186185222Ssam	for (i = 0; i < size / sizeof(u_int32_t); i++)
187185222Ssam		nint[i] = bswap32(oint[i]);
188185222Ssam}
189185222Ssam
190185222Ssamvoid
191185222Ssamffs_csumtotal_swap(struct csum_total *o, struct csum_total *n)
192185222Ssam{
193185222Ssam	n->cs_ndir = bswap64(o->cs_ndir);
194185222Ssam	n->cs_nbfree = bswap64(o->cs_nbfree);
195185222Ssam	n->cs_nifree = bswap64(o->cs_nifree);
196185222Ssam	n->cs_nffree = bswap64(o->cs_nffree);
197185222Ssam}
198185222Ssam
199185222Ssam/*
200185222Ssam * Note that ffs_cg_swap may be called with o == n.
201185222Ssam */
202185222Ssamvoid
203185222Ssamffs_cg_swap(struct cg *o, struct cg *n, struct fs *fs)
204185222Ssam{
205185222Ssam	int i;
206185222Ssam	u_int32_t *n32, *o32;
207185222Ssam	u_int16_t *n16, *o16;
208185222Ssam	int32_t btotoff, boff, clustersumoff;
209185222Ssam
210185222Ssam	n->cg_firstfield = bswap32(o->cg_firstfield);
211185222Ssam	n->cg_magic = bswap32(o->cg_magic);
212185222Ssam	n->cg_old_time = bswap32(o->cg_old_time);
213185222Ssam	n->cg_cgx = bswap32(o->cg_cgx);
214185222Ssam	n->cg_old_ncyl = bswap16(o->cg_old_ncyl);
215185222Ssam	n->cg_old_niblk = bswap16(o->cg_old_niblk);
216185222Ssam	n->cg_ndblk = bswap32(o->cg_ndblk);
217185222Ssam	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
218185222Ssam	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
219185222Ssam	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
220185222Ssam	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
221185222Ssam	n->cg_rotor = bswap32(o->cg_rotor);
222185222Ssam	n->cg_frotor = bswap32(o->cg_frotor);
223185222Ssam	n->cg_irotor = bswap32(o->cg_irotor);
224185222Ssam	for (i = 0; i < MAXFRAG; i++)
225185222Ssam		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
226185222Ssam
227186261Ssam	n->cg_old_btotoff = bswap32(o->cg_old_btotoff);
228186261Ssam	n->cg_old_boff = bswap32(o->cg_old_boff);
229186261Ssam	n->cg_iusedoff = bswap32(o->cg_iusedoff);
230186261Ssam	n->cg_freeoff = bswap32(o->cg_freeoff);
231186261Ssam	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
232186261Ssam	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
233186261Ssam	n->cg_clusteroff = bswap32(o->cg_clusteroff);
234186261Ssam	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
235186261Ssam	n->cg_niblk = bswap32(o->cg_niblk);
236186261Ssam	n->cg_initediblk = bswap32(o->cg_initediblk);
237186261Ssam	n->cg_time = bswap64(o->cg_time);
238185222Ssam
239186261Ssam	if (fs->fs_magic == FS_UFS2_MAGIC)
240186261Ssam		return;
241185222Ssam
242186261Ssam	if (n->cg_magic == CG_MAGIC) {
243186261Ssam		btotoff = n->cg_old_btotoff;
244186261Ssam		boff = n->cg_old_boff;
245186261Ssam		clustersumoff = n->cg_clustersumoff;
246186261Ssam	} else {
247186261Ssam		btotoff = bswap32(n->cg_old_btotoff);
248186261Ssam		boff = bswap32(n->cg_old_boff);
249186261Ssam		clustersumoff = bswap32(n->cg_clustersumoff);
250186261Ssam	}
251186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + btotoff);
252186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + btotoff);
253186261Ssam	n16 = (u_int16_t *)((u_int8_t *)n + boff);
254186261Ssam	o16 = (u_int16_t *)((u_int8_t *)o + boff);
255185222Ssam
256186261Ssam	for (i = 0; i < fs->fs_old_cpg; i++)
257186261Ssam		n32[i] = bswap32(o32[i]);
258186261Ssam
259186261Ssam	for (i = 0; i < fs->fs_old_cpg * fs->fs_old_nrpos; i++)
260186261Ssam		n16[i] = bswap16(o16[i]);
261185222Ssam
262186261Ssam	n32 = (u_int32_t *)((u_int8_t *)n + clustersumoff);
263186261Ssam	o32 = (u_int32_t *)((u_int8_t *)o + clustersumoff);
264186261Ssam	for (i = 1; i < fs->fs_contigsumsize + 1; i++)
265186261Ssam		n32[i] = bswap32(o32[i]);
266185222Ssam}
267