1230479Snetchild/*	$NetBSD: bswap.c,v 1.5 2016/01/31 18:57:29 christos Exp $	*/
2230479Snetchild
3230479Snetchild/*-
4230479Snetchild * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
5230479Snetchild *
6230479Snetchild * Redistribution and use in source and binary forms, with or without
7230479Snetchild * modification, are permitted provided that the following conditions
8230479Snetchild * are met:
9230479Snetchild * 1. Redistributions of source code must retain the above copyright
10230479Snetchild *    notice, this list of conditions and the following disclaimer.
11230479Snetchild * 2. Redistributions in binary form must reproduce the above copyright
12230479Snetchild *    notice, this list of conditions and the following disclaimer in the
13230479Snetchild *    documentation and/or other materials provided with the distribution.
14230479Snetchild *
15230479Snetchild * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16230479Snetchild * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17230479Snetchild * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18230479Snetchild * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19230479Snetchild * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20230479Snetchild * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21230479Snetchild * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22230479Snetchild * 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 !defined(NATIVELABEL_ONLY)
59
60#if HAVE_NBTOOL_CONFIG_H
61#include "nbtool_config.h"
62#endif
63
64#include <string.h>
65
66#include <sys/types.h>
67#if HAVE_NBTOOL_CONFIG_H
68#include <nbinclude/sys/disklabel.h>
69#else
70#include <sys/disklabel.h>
71#endif /* HAVE_NBTOOL_CONFIG_H */
72
73#include "bswap.h"
74#include "dkcksum.h"
75
76static void
77bswaplabel(struct disklabel *nlp, const struct disklabel *olp)
78{
79	u_int i;
80
81	nlp->d_magic          = bswap32(olp->d_magic);
82	nlp->d_type           = bswap16(olp->d_type);
83	nlp->d_subtype        = bswap16(olp->d_subtype);
84
85	/* no need to swap char strings */
86	memcpy(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
87
88	/* XXX What should we do for d_un (an union of char and pointers) ? */
89	memcpy(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
90
91	nlp->d_secsize        = bswap32(olp->d_secsize);
92	nlp->d_nsectors       = bswap32(olp->d_nsectors);
93	nlp->d_ntracks        = bswap32(olp->d_ntracks);
94	nlp->d_ncylinders     = bswap32(olp->d_ncylinders);
95	nlp->d_secpercyl      = bswap32(olp->d_secpercyl);
96	nlp->d_secperunit     = bswap32(olp->d_secperunit);
97
98	nlp->d_sparespertrack = bswap16(olp->d_sparespertrack);
99	nlp->d_sparespercyl   = bswap16(olp->d_sparespercyl);
100
101	nlp->d_acylinders     = bswap32(olp->d_acylinders);
102
103	nlp->d_rpm            = bswap16(olp->d_rpm);
104	nlp->d_interleave     = bswap16(olp->d_interleave);
105	nlp->d_trackskew      = bswap16(olp->d_trackskew);
106	nlp->d_cylskew        = bswap16(olp->d_cylskew);
107	nlp->d_headswitch     = bswap32(olp->d_headswitch);
108	nlp->d_trkseek        = bswap32(olp->d_trkseek);
109	nlp->d_flags          = bswap32(olp->d_flags);
110
111	for (i = 0; i < NDDATA; i++)
112		nlp->d_drivedata[i] = bswap32(olp->d_drivedata[i]);
113
114	for (i = 0; i < NSPARE; i++)
115		nlp->d_spare[i]     = bswap32(olp->d_spare[i]);
116
117	nlp->d_magic2         = bswap32(olp->d_magic2);
118	nlp->d_checksum       = bswap16(olp->d_checksum);
119
120	/* filesystem and partition information: */
121	nlp->d_npartitions    = bswap16(olp->d_npartitions);
122	nlp->d_bbsize         = bswap32(olp->d_bbsize);
123	nlp->d_sbsize         = bswap32(olp->d_sbsize);
124
125	for (i = 0; i < maxpartitions; i++) {
126		nlp->d_partitions[i].p_size =
127		    bswap32(olp->d_partitions[i].p_size);
128		nlp->d_partitions[i].p_offset =
129		    bswap32(olp->d_partitions[i].p_offset);
130		nlp->d_partitions[i].p_fsize =
131		    bswap32(olp->d_partitions[i].p_fsize);
132		/* p_fstype and p_frag is uint8_t, so no need to swap */
133		nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
134		nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
135		nlp->d_partitions[i].p_cpg =
136		    bswap16(olp->d_partitions[i].p_cpg);
137	}
138}
139
140void
141targettohlabel(struct disklabel *hlp, const struct disklabel *tlp)
142{
143
144	if (bswap32(tlp->d_magic) == DISKMAGIC)
145		bswaplabel(hlp, tlp);
146	else
147		*hlp = *tlp;
148	/* update checksum in host endian */
149	hlp->d_checksum = 0;
150	hlp->d_checksum = dkcksum(hlp);
151}
152
153void
154htotargetlabel(struct disklabel *tlp, const struct disklabel *hlp)
155{
156
157	if (bswap_p)
158		bswaplabel(tlp, hlp);
159	else
160		*tlp = *hlp;
161
162	/* update checksum in target endian */
163	tlp->d_checksum = 0;
164	tlp->d_checksum = dkcksum_target(tlp);
165}
166
167uint16_t
168dkcksum_target(struct disklabel *lp)
169{
170	uint16_t npartitions;
171
172	if (lp->d_magic == DISKMAGIC)
173		npartitions = lp->d_npartitions;
174	else if (bswap32(lp->d_magic) == DISKMAGIC)
175		npartitions = bswap16(lp->d_npartitions);
176	else
177		npartitions = 0;
178
179	if (npartitions > maxpartitions)
180		npartitions = 0;
181
182	return dkcksum_sized(lp, npartitions);
183}
184
185#endif /* !NATIVELABEL_ONLY */
186