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