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