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