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