xref: /netbsd/sys/lib/libkern/disklabel_swap.c (revision 4a1c505c)
1*4a1c505cSmrg /*	$NetBSD: disklabel_swap.c,v 1.1 2021/05/17 08:50:36 mrg Exp $	*/
2*4a1c505cSmrg 
3*4a1c505cSmrg /*
4*4a1c505cSmrg  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
5*4a1c505cSmrg  * All rights reserved.
6*4a1c505cSmrg  *
7*4a1c505cSmrg  * Redistribution and use in source and binary forms, with or without
8*4a1c505cSmrg  * modification, are permitted provided that the following conditions
9*4a1c505cSmrg  * are met:
10*4a1c505cSmrg  * 1. Redistributions of source code must retain the above copyright
11*4a1c505cSmrg  *    notice, this list of conditions and the following disclaimer.
12*4a1c505cSmrg  * 2. Redistributions in binary form must reproduce the above copyright
13*4a1c505cSmrg  *    notice, this list of conditions and the following disclaimer in the
14*4a1c505cSmrg  *    documentation and/or other materials provided with the distribution.
15*4a1c505cSmrg  * 3. Neither the name of the University nor the names of its contributors
16*4a1c505cSmrg  *    may be used to endorse or promote products derived from this software
17*4a1c505cSmrg  *    without specific prior written permission.
18*4a1c505cSmrg  *
19*4a1c505cSmrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*4a1c505cSmrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*4a1c505cSmrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*4a1c505cSmrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*4a1c505cSmrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*4a1c505cSmrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*4a1c505cSmrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*4a1c505cSmrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*4a1c505cSmrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*4a1c505cSmrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*4a1c505cSmrg  * SUCH DAMAGE.
30*4a1c505cSmrg  *
31*4a1c505cSmrg  *	@(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
32*4a1c505cSmrg  */
33*4a1c505cSmrg 
34*4a1c505cSmrg #include <sys/cdefs.h>
35*4a1c505cSmrg __KERNEL_RCSID(0, "$NetBSD: disklabel_swap.c,v 1.1 2021/05/17 08:50:36 mrg Exp $");
36*4a1c505cSmrg 
37*4a1c505cSmrg #ifdef _KERNEL_OPT
38*4a1c505cSmrg #include "opt_disklabel.h"
39*4a1c505cSmrg #endif /* _KERNEL_OPT */
40*4a1c505cSmrg 
41*4a1c505cSmrg #if defined(DISKLABEL_EI) || defined(LIBSA_DISKLABEL_EI)
42*4a1c505cSmrg 
43*4a1c505cSmrg #include <sys/param.h>
44*4a1c505cSmrg #include <sys/systm.h>
45*4a1c505cSmrg #include <sys/disklabel.h>
46*4a1c505cSmrg #include <sys/conf.h>
47*4a1c505cSmrg #include <lib/libkern/libkern.h>
48*4a1c505cSmrg 
49*4a1c505cSmrg /*
50*4a1c505cSmrg  * from sh3/disksubr.c and kern/subr_disk_mbr.c with modifications:
51*4a1c505cSmrg  *	- update d_checksum properly
52*4a1c505cSmrg  *	- replace memcpy(9) by memmove(9) as a precaution
53*4a1c505cSmrg  *	- avoid memmove(9) for libkern version, check if the labels
54*4a1c505cSmrg  *	  are the same and skip copying in-place.
55*4a1c505cSmrg  */
56*4a1c505cSmrg void
disklabel_swap(struct disklabel * nlp,struct disklabel * olp)57*4a1c505cSmrg disklabel_swap(struct disklabel *nlp, struct disklabel *olp)
58*4a1c505cSmrg {
59*4a1c505cSmrg 	int i;
60*4a1c505cSmrg 	uint16_t npartitions;
61*4a1c505cSmrg 
62*4a1c505cSmrg #define	SWAP16(x)	nlp->x = bswap16(olp->x)
63*4a1c505cSmrg #define	SWAP32(x)	nlp->x = bswap32(olp->x)
64*4a1c505cSmrg 
65*4a1c505cSmrg 	SWAP32(d_magic);
66*4a1c505cSmrg 	SWAP16(d_type);
67*4a1c505cSmrg 	SWAP16(d_subtype);
68*4a1c505cSmrg 	if (nlp != olp) {
69*4a1c505cSmrg 		/* Do not need to swap char strings. */
70*4a1c505cSmrg 		memcpy(nlp->d_typename, olp->d_typename,
71*4a1c505cSmrg 			sizeof(nlp->d_typename));
72*4a1c505cSmrg 
73*4a1c505cSmrg 		/*
74*4a1c505cSmrg 		 * XXX What should we do for d_un (an union of char and
75*4a1c505cSmrg 		 * pointers)?
76*4a1c505cSmrg 		 */
77*4a1c505cSmrg 		memcpy(nlp->d_packname, olp->d_packname,
78*4a1c505cSmrg 			sizeof(nlp->d_packname));
79*4a1c505cSmrg 	}
80*4a1c505cSmrg 
81*4a1c505cSmrg 	SWAP32(d_secsize);
82*4a1c505cSmrg 	SWAP32(d_nsectors);
83*4a1c505cSmrg 	SWAP32(d_ntracks);
84*4a1c505cSmrg 	SWAP32(d_ncylinders);
85*4a1c505cSmrg 	SWAP32(d_secpercyl);
86*4a1c505cSmrg 	SWAP32(d_secperunit);
87*4a1c505cSmrg 
88*4a1c505cSmrg 	SWAP16(d_sparespertrack);
89*4a1c505cSmrg 	SWAP16(d_sparespercyl);
90*4a1c505cSmrg 
91*4a1c505cSmrg 	SWAP32(d_acylinders);
92*4a1c505cSmrg 
93*4a1c505cSmrg 	SWAP16(d_rpm);
94*4a1c505cSmrg 	SWAP16(d_interleave);
95*4a1c505cSmrg 	SWAP16(d_trackskew);
96*4a1c505cSmrg 	SWAP16(d_cylskew);
97*4a1c505cSmrg 	SWAP32(d_headswitch);
98*4a1c505cSmrg 	SWAP32(d_trkseek);
99*4a1c505cSmrg 	SWAP32(d_flags);
100*4a1c505cSmrg 	for (i = 0; i < NDDATA; i++)
101*4a1c505cSmrg 		SWAP32(d_drivedata[i]);
102*4a1c505cSmrg 	for (i = 0; i < NSPARE; i++)
103*4a1c505cSmrg 		SWAP32(d_spare[i]);
104*4a1c505cSmrg 	SWAP32(d_magic2);
105*4a1c505cSmrg 	/* d_checksum is updated later. */
106*4a1c505cSmrg 
107*4a1c505cSmrg 	SWAP16(d_npartitions);
108*4a1c505cSmrg 	SWAP32(d_bbsize);
109*4a1c505cSmrg 	SWAP32(d_sbsize);
110*4a1c505cSmrg 	for (i = 0; i < MAXPARTITIONS; i++) {
111*4a1c505cSmrg 		SWAP32(d_partitions[i].p_size);
112*4a1c505cSmrg 		SWAP32(d_partitions[i].p_offset);
113*4a1c505cSmrg 		SWAP32(d_partitions[i].p_fsize);
114*4a1c505cSmrg 		/* p_fstype and p_frag is uint8_t, so no need to swap. */
115*4a1c505cSmrg 		nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
116*4a1c505cSmrg 		nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
117*4a1c505cSmrg 		SWAP16(d_partitions[i].p_cpg);
118*4a1c505cSmrg 	}
119*4a1c505cSmrg 
120*4a1c505cSmrg #undef SWAP16
121*4a1c505cSmrg #undef SWAP32
122*4a1c505cSmrg 
123*4a1c505cSmrg 	/* Update checksum in the target endian. */
124*4a1c505cSmrg 	nlp->d_checksum = 0;
125*4a1c505cSmrg 	npartitions = nlp->d_magic == DISKMAGIC ?
126*4a1c505cSmrg 	    nlp->d_npartitions : olp->d_npartitions;
127*4a1c505cSmrg 	/*
128*4a1c505cSmrg 	 * npartitions can be larger than MAXPARTITIONS when the label was not
129*4a1c505cSmrg 	 * validated by setdisklabel. If so, the label is intentionally(?)
130*4a1c505cSmrg 	 * corrupted and checksum should be meaningless.
131*4a1c505cSmrg 	 */
132*4a1c505cSmrg 	if (npartitions <= MAXPARTITIONS)
133*4a1c505cSmrg 		nlp->d_checksum = dkcksum_sized(nlp, npartitions);
134*4a1c505cSmrg }
135*4a1c505cSmrg #endif /* DISKLABEL_EI || LIBSA_DISKLABEL_EI */
136