Home | History | Annotate | Line # | Download | only in disklabel
bswap.c revision 1.1
      1 /*	$NetBSD: bswap.c,v 1.1 2010/01/05 15:45:26 tsutsui 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 <sys/types.h>
     63 #if HAVE_NBTOOL_CONFIG_H
     64 #include <nbinclude/sys/disklabel.h>
     65 #else
     66 #include <sys/disklabel.h>
     67 #endif /* HAVE_NBTOOL_CONFIG_H */
     68 
     69 #include "bswap.h"
     70 #include "dkcksum.h"
     71 
     72 #if TARGET_BYTE_ORDER != BYTE_ORDER
     73 static void bswaplabel(struct disklabel *nlp, struct disklabel *olp);
     74 
     75 void
     76 bswaplabel(struct disklabel *nlp, struct disklabel *olp)
     77 {
     78 	int i;
     79 
     80 	nlp->d_magic          = bswap32(olp->d_magic);
     81 	nlp->d_type           = bswap16(olp->d_type);
     82 	nlp->d_subtype        = bswap16(olp->d_subtype);
     83 
     84 	/* no need to swap char strings */
     85 	memcpy(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
     86 
     87 	/* XXX What should we do for d_un (an union of char and pointers) ? */
     88 	memcpy(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
     89 
     90 	nlp->d_secsize        = bswap32(olp->d_secsize);
     91 	nlp->d_nsectors       = bswap32(olp->d_nsectors);
     92 	nlp->d_ntracks        = bswap32(olp->d_ntracks);
     93 	nlp->d_ncylinders     = bswap32(olp->d_ncylinders);
     94 	nlp->d_secpercyl      = bswap32(olp->d_secpercyl);
     95 	nlp->d_secperunit     = bswap32(olp->d_secperunit);
     96 
     97 	nlp->d_sparespertrack = bswap16(olp->d_sparespertrack);
     98 	nlp->d_sparespercyl   = bswap16(olp->d_sparespercyl);
     99 
    100 	nlp->d_acylinders     = bswap32(olp->d_acylinders);
    101 
    102 	nlp->d_rpm            = bswap16(olp->d_rpm);
    103 	nlp->d_interleave     = bswap16(olp->d_interleave);
    104 	nlp->d_trackskew      = bswap16(olp->d_trackskew);
    105 	nlp->d_cylskew        = bswap16(olp->d_cylskew);
    106 	nlp->d_headswitch     = bswap32(olp->d_headswitch);
    107 	nlp->d_trkseek        = bswap32(olp->d_trkseek);
    108 	nlp->d_flags          = bswap32(olp->d_flags);
    109 
    110 	for (i = 0; i < NDDATA; i++)
    111 		nlp->d_drivedata[i] = bswap32(olp->d_drivedata[i]);
    112 
    113 	for (i = 0; i < NSPARE; i++)
    114 		nlp->d_spare[i]     = bswap32(olp->d_spare[i]);
    115 
    116 	nlp->d_magic2         = bswap32(olp->d_magic2);
    117 	nlp->d_checksum       = bswap16(olp->d_checksum);
    118 
    119 	/* filesystem and partition information: */
    120 	nlp->d_npartitions    = bswap16(olp->d_npartitions);
    121 	nlp->d_bbsize         = bswap32(olp->d_bbsize);
    122 	nlp->d_sbsize         = bswap32(olp->d_sbsize);
    123 
    124 	for (i = 0; i < MAXPARTITIONS; i++) {
    125 		nlp->d_partitions[i].p_size =
    126 		    bswap32(olp->d_partitions[i].p_size);
    127 		nlp->d_partitions[i].p_offset =
    128 		    bswap32(olp->d_partitions[i].p_offset);
    129 		nlp->d_partitions[i].p_fsize =
    130 		    bswap32(olp->d_partitions[i].p_fsize);
    131 		/* p_fstype and p_frag is uint8_t, so no need to swap */
    132 		nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
    133 		nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
    134 		nlp->d_partitions[i].p_cpg =
    135 		    bswap16(olp->d_partitions[i].p_cpg);
    136 	}
    137 }
    138 
    139 void
    140 targettohlabel(struct disklabel *hlp, struct disklabel *tlp)
    141 {
    142 
    143 	bswaplabel(hlp, tlp);
    144 	/* update checksum in host endian */
    145 	hlp->d_checksum = 0;
    146 	hlp->d_checksum = dkcksum(hlp);
    147 }
    148 
    149 void
    150 htotargetlabel(struct disklabel *tlp, struct disklabel *hlp)
    151 {
    152 
    153 	bswaplabel(tlp, hlp);
    154 	/* update checksum in target endian */
    155 	tlp->d_checksum = 0;
    156 	tlp->d_checksum = dkcksum_re(tlp);
    157 }
    158 
    159 uint16_t
    160 dkcksum_re(struct disklabel *lp)
    161 {
    162 	uint16_t npartitions;
    163 
    164 	/* we can assume lp is reversed, but check it again for sanity */
    165 	if (lp->d_magic == DISKMAGIC)
    166 		npartitions = lp->d_npartitions;
    167 	else if (bswap32(lp->d_magic) == DISKMAGIC)
    168 		npartitions = bswap16(lp->d_npartitions);
    169 	else
    170 		npartitions = 0;
    171 
    172 	if (npartitions > MAXPARTITIONS)
    173 		npartitions = 0;
    174 
    175 	return dkcksum_sized(lp, npartitions);
    176 }
    177 #endif
    178