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