xref: /netbsd-src/sbin/disklabel/bswap.c (revision 0636a2fe9762aa2b4b0b7a12f5725feaf489d182)
1*0636a2feSchristos /*	$NetBSD: bswap.c,v 1.5 2016/01/31 18:57:29 christos Exp $	*/
2d3724ba4Stsutsui 
3d3724ba4Stsutsui /*-
4d3724ba4Stsutsui  * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
5d3724ba4Stsutsui  *
6d3724ba4Stsutsui  * Redistribution and use in source and binary forms, with or without
7d3724ba4Stsutsui  * modification, are permitted provided that the following conditions
8d3724ba4Stsutsui  * are met:
9d3724ba4Stsutsui  * 1. Redistributions of source code must retain the above copyright
10d3724ba4Stsutsui  *    notice, this list of conditions and the following disclaimer.
11d3724ba4Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
12d3724ba4Stsutsui  *    notice, this list of conditions and the following disclaimer in the
13d3724ba4Stsutsui  *    documentation and/or other materials provided with the distribution.
14d3724ba4Stsutsui  *
15d3724ba4Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16d3724ba4Stsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d3724ba4Stsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d3724ba4Stsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19d3724ba4Stsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d3724ba4Stsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d3724ba4Stsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d3724ba4Stsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d3724ba4Stsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d3724ba4Stsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d3724ba4Stsutsui  */
26d3724ba4Stsutsui 
27d3724ba4Stsutsui /*
28d3724ba4Stsutsui  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
29d3724ba4Stsutsui  * All rights reserved.
30d3724ba4Stsutsui  *
31d3724ba4Stsutsui  * Redistribution and use in source and binary forms, with or without
32d3724ba4Stsutsui  * modification, are permitted provided that the following conditions
33d3724ba4Stsutsui  * are met:
34d3724ba4Stsutsui  * 1. Redistributions of source code must retain the above copyright
35d3724ba4Stsutsui  *    notice, this list of conditions and the following disclaimer.
36d3724ba4Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
37d3724ba4Stsutsui  *    notice, this list of conditions and the following disclaimer in the
38d3724ba4Stsutsui  *    documentation and/or other materials provided with the distribution.
39d3724ba4Stsutsui  * 3. Neither the name of the University nor the names of its contributors
40d3724ba4Stsutsui  *    may be used to endorse or promote products derived from this software
41d3724ba4Stsutsui  *    without specific prior written permission.
42d3724ba4Stsutsui  *
43d3724ba4Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44d3724ba4Stsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45d3724ba4Stsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46d3724ba4Stsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47d3724ba4Stsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48d3724ba4Stsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49d3724ba4Stsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50d3724ba4Stsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51d3724ba4Stsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52d3724ba4Stsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53d3724ba4Stsutsui  * SUCH DAMAGE.
54d3724ba4Stsutsui  *
55d3724ba4Stsutsui  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
56d3724ba4Stsutsui  */
57d3724ba4Stsutsui 
58*0636a2feSchristos #if !defined(NATIVELABEL_ONLY)
59*0636a2feSchristos 
60d3724ba4Stsutsui #if HAVE_NBTOOL_CONFIG_H
61d3724ba4Stsutsui #include "nbtool_config.h"
62d3724ba4Stsutsui #endif
63d3724ba4Stsutsui 
64af8f0546Smatt #include <string.h>
65af8f0546Smatt 
66d3724ba4Stsutsui #include <sys/types.h>
67d3724ba4Stsutsui #if HAVE_NBTOOL_CONFIG_H
68d3724ba4Stsutsui #include <nbinclude/sys/disklabel.h>
69d3724ba4Stsutsui #else
70d3724ba4Stsutsui #include <sys/disklabel.h>
71d3724ba4Stsutsui #endif /* HAVE_NBTOOL_CONFIG_H */
72d3724ba4Stsutsui 
73d3724ba4Stsutsui #include "bswap.h"
74d3724ba4Stsutsui #include "dkcksum.h"
75d3724ba4Stsutsui 
76af8f0546Smatt static void
bswaplabel(struct disklabel * nlp,const struct disklabel * olp)77af8f0546Smatt bswaplabel(struct disklabel *nlp, const struct disklabel *olp)
78d3724ba4Stsutsui {
797f6bcf1fShtodd 	u_int i;
80d3724ba4Stsutsui 
81d3724ba4Stsutsui 	nlp->d_magic          = bswap32(olp->d_magic);
82d3724ba4Stsutsui 	nlp->d_type           = bswap16(olp->d_type);
83d3724ba4Stsutsui 	nlp->d_subtype        = bswap16(olp->d_subtype);
84d3724ba4Stsutsui 
85d3724ba4Stsutsui 	/* no need to swap char strings */
86d3724ba4Stsutsui 	memcpy(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
87d3724ba4Stsutsui 
88d3724ba4Stsutsui 	/* XXX What should we do for d_un (an union of char and pointers) ? */
89d3724ba4Stsutsui 	memcpy(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
90d3724ba4Stsutsui 
91d3724ba4Stsutsui 	nlp->d_secsize        = bswap32(olp->d_secsize);
92d3724ba4Stsutsui 	nlp->d_nsectors       = bswap32(olp->d_nsectors);
93d3724ba4Stsutsui 	nlp->d_ntracks        = bswap32(olp->d_ntracks);
94d3724ba4Stsutsui 	nlp->d_ncylinders     = bswap32(olp->d_ncylinders);
95d3724ba4Stsutsui 	nlp->d_secpercyl      = bswap32(olp->d_secpercyl);
96d3724ba4Stsutsui 	nlp->d_secperunit     = bswap32(olp->d_secperunit);
97d3724ba4Stsutsui 
98d3724ba4Stsutsui 	nlp->d_sparespertrack = bswap16(olp->d_sparespertrack);
99d3724ba4Stsutsui 	nlp->d_sparespercyl   = bswap16(olp->d_sparespercyl);
100d3724ba4Stsutsui 
101d3724ba4Stsutsui 	nlp->d_acylinders     = bswap32(olp->d_acylinders);
102d3724ba4Stsutsui 
103d3724ba4Stsutsui 	nlp->d_rpm            = bswap16(olp->d_rpm);
104d3724ba4Stsutsui 	nlp->d_interleave     = bswap16(olp->d_interleave);
105d3724ba4Stsutsui 	nlp->d_trackskew      = bswap16(olp->d_trackskew);
106d3724ba4Stsutsui 	nlp->d_cylskew        = bswap16(olp->d_cylskew);
107d3724ba4Stsutsui 	nlp->d_headswitch     = bswap32(olp->d_headswitch);
108d3724ba4Stsutsui 	nlp->d_trkseek        = bswap32(olp->d_trkseek);
109d3724ba4Stsutsui 	nlp->d_flags          = bswap32(olp->d_flags);
110d3724ba4Stsutsui 
111d3724ba4Stsutsui 	for (i = 0; i < NDDATA; i++)
112d3724ba4Stsutsui 		nlp->d_drivedata[i] = bswap32(olp->d_drivedata[i]);
113d3724ba4Stsutsui 
114d3724ba4Stsutsui 	for (i = 0; i < NSPARE; i++)
115d3724ba4Stsutsui 		nlp->d_spare[i]     = bswap32(olp->d_spare[i]);
116d3724ba4Stsutsui 
117d3724ba4Stsutsui 	nlp->d_magic2         = bswap32(olp->d_magic2);
118d3724ba4Stsutsui 	nlp->d_checksum       = bswap16(olp->d_checksum);
119d3724ba4Stsutsui 
120d3724ba4Stsutsui 	/* filesystem and partition information: */
121d3724ba4Stsutsui 	nlp->d_npartitions    = bswap16(olp->d_npartitions);
122d3724ba4Stsutsui 	nlp->d_bbsize         = bswap32(olp->d_bbsize);
123d3724ba4Stsutsui 	nlp->d_sbsize         = bswap32(olp->d_sbsize);
124d3724ba4Stsutsui 
1251d238273Stsutsui 	for (i = 0; i < maxpartitions; i++) {
126d3724ba4Stsutsui 		nlp->d_partitions[i].p_size =
127d3724ba4Stsutsui 		    bswap32(olp->d_partitions[i].p_size);
128d3724ba4Stsutsui 		nlp->d_partitions[i].p_offset =
129d3724ba4Stsutsui 		    bswap32(olp->d_partitions[i].p_offset);
130d3724ba4Stsutsui 		nlp->d_partitions[i].p_fsize =
131d3724ba4Stsutsui 		    bswap32(olp->d_partitions[i].p_fsize);
132d3724ba4Stsutsui 		/* p_fstype and p_frag is uint8_t, so no need to swap */
133d3724ba4Stsutsui 		nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
134d3724ba4Stsutsui 		nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
135d3724ba4Stsutsui 		nlp->d_partitions[i].p_cpg =
136d3724ba4Stsutsui 		    bswap16(olp->d_partitions[i].p_cpg);
137d3724ba4Stsutsui 	}
138d3724ba4Stsutsui }
139d3724ba4Stsutsui 
140d3724ba4Stsutsui void
targettohlabel(struct disklabel * hlp,const struct disklabel * tlp)141af8f0546Smatt targettohlabel(struct disklabel *hlp, const struct disklabel *tlp)
142d3724ba4Stsutsui {
143d3724ba4Stsutsui 
144af8f0546Smatt 	if (bswap32(tlp->d_magic) == DISKMAGIC)
145d3724ba4Stsutsui 		bswaplabel(hlp, tlp);
146af8f0546Smatt 	else
147af8f0546Smatt 		*hlp = *tlp;
148d3724ba4Stsutsui 	/* update checksum in host endian */
149d3724ba4Stsutsui 	hlp->d_checksum = 0;
150d3724ba4Stsutsui 	hlp->d_checksum = dkcksum(hlp);
151d3724ba4Stsutsui }
152d3724ba4Stsutsui 
153d3724ba4Stsutsui void
htotargetlabel(struct disklabel * tlp,const struct disklabel * hlp)154af8f0546Smatt htotargetlabel(struct disklabel *tlp, const struct disklabel *hlp)
155d3724ba4Stsutsui {
156d3724ba4Stsutsui 
157af8f0546Smatt 	if (bswap_p)
158d3724ba4Stsutsui 		bswaplabel(tlp, hlp);
159af8f0546Smatt 	else
160af8f0546Smatt 		*tlp = *hlp;
161af8f0546Smatt 
162d3724ba4Stsutsui 	/* update checksum in target endian */
163d3724ba4Stsutsui 	tlp->d_checksum = 0;
164af8f0546Smatt 	tlp->d_checksum = dkcksum_target(tlp);
165d3724ba4Stsutsui }
166d3724ba4Stsutsui 
167d3724ba4Stsutsui uint16_t
dkcksum_target(struct disklabel * lp)168af8f0546Smatt dkcksum_target(struct disklabel *lp)
169d3724ba4Stsutsui {
170d3724ba4Stsutsui 	uint16_t npartitions;
171d3724ba4Stsutsui 
172d3724ba4Stsutsui 	if (lp->d_magic == DISKMAGIC)
173d3724ba4Stsutsui 		npartitions = lp->d_npartitions;
174d3724ba4Stsutsui 	else if (bswap32(lp->d_magic) == DISKMAGIC)
175d3724ba4Stsutsui 		npartitions = bswap16(lp->d_npartitions);
176d3724ba4Stsutsui 	else
177d3724ba4Stsutsui 		npartitions = 0;
178d3724ba4Stsutsui 
179af8f0546Smatt 	if (npartitions > maxpartitions)
180d3724ba4Stsutsui 		npartitions = 0;
181d3724ba4Stsutsui 
182d3724ba4Stsutsui 	return dkcksum_sized(lp, npartitions);
183d3724ba4Stsutsui }
184*0636a2feSchristos 
185*0636a2feSchristos #endif /* !NATIVELABEL_ONLY */
186