tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: disklabel endian issues



I wrote:

> > Is it my imagination, or is disklabel not endian aware? $TOOLDIR/bin/ 
> > nbdisklabel-i386 on my Power Mac G5 seems to be creating big endian  
> > disklabels. This is somewhat less than useful.
 :
> but how about this patch?

Ah, this one doesn't work if target endian is the same as host.

Here is updated one including some more minor improvements:

---
Index: tools/disklabel/Makefile
===================================================================
RCS file: /cvsroot/src/tools/disklabel/Makefile,v
retrieving revision 1.2
diff -u -r1.2 Makefile
--- tools/disklabel/Makefile    9 Dec 2006 20:13:13 -0000       1.2
+++ tools/disklabel/Makefile    25 Oct 2009 07:25:09 -0000
@@ -2,7 +2,36 @@
 
 HOSTPROGNAME=  nbdisklabel-${MAKEWRAPPERMACHINE}
 HOST_SRCDIR=   sbin/disklabel
-HOST_SRCS=     getcap.c disklabel.c
+HOST_SRCS=     getcap.c disklabel.c bswap.c
+
+# XXX should these be defined in <bsd.own.mk>?
+.if ( 0 \
+       || ${MACHINE_ARCH} == "alpha" \
+       || ${MACHINE_ARCH} == "arm" \
+       || ${MACHINE_ARCH} == "i386" \
+       || ${MACHINE_ARCH} == "ia64" \
+       || ${MACHINE_ARCH} == "mips64el" \
+       || ${MACHINE_ARCH} == "mipsel" \
+       || ${MACHINE_ARCH} == "sh3el" \
+       || ${MACHINE_ARCH} == "vax" \
+       || ${MACHINE_ARCH} == "x86_64" \
+    )
+CPPFLAGS+= -DTARGET_BYTE_ORDER=LITTLE_ENDIAN
+.endif
+.if ( 0 \
+       || ${MACHINE_ARCH} == "armeb" \
+       || ${MACHINE_ARCH} == "hppa" \
+       || ${MACHINE_ARCH} == "m68000" \
+       || ${MACHINE_ARCH} == "m68k" \
+       || ${MACHINE_ARCH} == "mips64eb" \
+       || ${MACHINE_ARCH} == "mipseb" \
+       || ${MACHINE_ARCH} == "powerpc" \
+       || ${MACHINE_ARCH} == "sh3eb" \
+       || ${MACHINE_ARCH} == "sparc" \
+       || ${MACHINE_ARCH} == "sparc64" \
+    )
+CPPFLAGS+= -DTARGET_BYTE_ORDER=BIG_ENDIAN
+.endif
 
 .include "${.CURDIR}/../Makefile.disklabel"
 .include "${.CURDIR}/../Makefile.host"
Index: sbin/disklabel/Makefile
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/Makefile,v
retrieving revision 1.62
diff -u -r1.62 Makefile
--- sbin/disklabel/Makefile     14 Feb 2009 13:52:51 -0000      1.62
+++ sbin/disklabel/Makefile     25 Oct 2009 07:25:09 -0000
@@ -47,4 +47,14 @@
 CPPFLAGS+= -DUSE_ACORN
 .endif
 
+.if (${MACHINE_ARCH} == "alpha")
+# alpha requires boot block checksum
+CPPFLAGS+= -DALPHA_BOOTBLOCK_CKSUM
+.endif
+
+.if (${MACHINE_ARCH} == "vax")
+# vax requires labels in alternative sectors on SMD disk
+CPPFLAGS+= -DVAX_ALTLABELS
+.endif
+
 .include <bsd.prog.mk>
Index: sbin/disklabel/main.c
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/main.c,v
retrieving revision 1.20
diff -u -r1.20 main.c
--- sbin/disklabel/main.c       4 May 2009 18:09:04 -0000       1.20
+++ sbin/disklabel/main.c       25 Oct 2009 07:25:09 -0000
@@ -117,6 +117,7 @@
 #include "pathnames.h"
 #include "extern.h"
 #include "dkcksum.h"
+#include "bswap.h"
 
 /*
  * Disklabel: read and write disklabels.
@@ -535,7 +536,7 @@
                }
        }
 
-#ifdef __vax__
+#ifdef VAX_ALTLABELS
        if (lab.d_type == DTYPE_SMD && lab.d_flags & D_BADSECT &&
            lab.d_secsize == 512) {
                /* Write the label to the odd sectors of the last track! */
@@ -554,7 +555,7 @@
                                warn("alternate label %d write", i/2);
                }
        }
-#endif /* __vax__ */
+#endif /* VAX_ALTLABELS */
 
        return 0;
 }
@@ -688,12 +689,12 @@
 static int
 readlabel_mbr(int f, u_int sector)
 {
-       struct disklabel *lp;
+       struct disklabel *disk_lp;
 
-       lp = find_label(f, sector);
-       if (lp == NULL)
+       disk_lp = find_label(f, sector);
+       if (disk_lp == NULL)
                return 1;
-       lab = *lp;
+       targettohlabel(&lab, disk_lp);
        return 0;
 }
 
@@ -900,7 +901,7 @@
 static struct disklabel *
 find_label(int f, u_int sector)
 {
-       struct disklabel *lp;
+       struct disklabel *disk_lp, hlp;
        int i, offset;
        const char *is_deleted;
 
@@ -919,29 +920,30 @@
        /* Check expected offset first */
        for (offset = LABEL_OFFSET, i = -4;; offset = i += 4) {
                is_deleted = "";
-               lp = (void *)(bootarea + offset);
+               disk_lp = (void *)(bootarea + offset);
                if (i == LABEL_OFFSET)
                        continue;
-               if ((char *)(lp + 1) > bootarea + bootarea_len)
+               if ((char *)(disk_lp + 1) > bootarea + bootarea_len)
                        break;
-               if (lp->d_magic2 != lp->d_magic)
+               if (disk_lp->d_magic2 != disk_lp->d_magic)
                        continue;
-               if (read_all && (lp->d_magic == DISKMAGIC_DELETED ||
-                   lp->d_magic == DISKMAGIC_DELETED_REV)) {
-                       lp->d_magic ^= ~0u;
-                       lp->d_magic2 ^= ~0u;
+               if (read_all && (disk_lp->d_magic == DISKMAGIC_DELETED ||
+                   disk_lp->d_magic == DISKMAGIC_DELETED_REV)) {
+                       disk_lp->d_magic ^= ~0u;
+                       disk_lp->d_magic2 ^= ~0u;
                        is_deleted = "deleted ";
                }
-               if (lp->d_magic != DISKMAGIC) {
+               if (target32toh(disk_lp->d_magic) != DISKMAGIC) {
                        /* XXX: Do something about byte-swapped labels ? */
-                       if (lp->d_magic == DISKMAGIC_REV &&
-                           lp->d_magic2 == DISKMAGIC_REV)
+                       if (target32toh(disk_lp->d_magic) == DISKMAGIC_REV &&
+                           target32toh(disk_lp->d_magic2) == DISKMAGIC_REV)
                                warnx("ignoring %sbyteswapped label"
                                    " at offset %u from sector %u",
                                    is_deleted, offset, sector);
                        continue;
                }
-               if (lp->d_npartitions > MAXPARTITIONS || dkcksum(lp) != 0) {
+               if (target16toh(disk_lp->d_npartitions) > MAXPARTITIONS ||
+                   dkcksum_target(disk_lp) != 0) {
                        if (verbose > 0)
                                warnx("corrupt label found at offset %u in "
                                    "sector %u", offset, sector);
@@ -951,19 +953,21 @@
                        warnx("%slabel found at offset %u from sector %u",
                            is_deleted, offset, sector);
                if (!read_all)
-                       return lp;
+                       return disk_lp;
 
                /* To print all the labels we have to do it here */
                /* XXX: maybe we should compare them? */
+               targettohlabel(&hlp, disk_lp);
                printf("# %ssector %u offset %u bytes\n",
                    is_deleted, sector, offset);
                if (tflag)
-                       makedisktab(stdout, lp);
+                       makedisktab(stdout, &hlp);
                else {
-                       showinfo(stdout, lp, specname);
-                       showpartitions(stdout, lp, Cflag);
+                       showinfo(stdout, &hlp, specname);
+                       showpartitions(stdout, &hlp, Cflag);
                }
-               checklabel(lp);
+               checklabel(&hlp);
+               htotargetlabel(disk_lp, &hlp);
                /* Remember we've found a label */
                read_all = 2;
        }
@@ -978,7 +982,7 @@
        if (bootarea_len <= 0)
                errx(1, "attempting to write after failed read");
 
-#ifdef __alpha__
+#ifdef ALPHA_BOOTBLOCK_CKSUM
        /*
         * The Alpha requires that the boot block be checksummed.
         * The NetBSD/alpha disklabel.h provides a macro to do it.
@@ -990,7 +994,7 @@
                bb->bb_cksum = 0;
                ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
        }
-#endif /* __alpha__ */
+#endif /* ALPHA_BOOTBLOCK_CKSUM */
 
        wlen = pwrite(f, bootarea, bootarea_len, sector * (off_t)DEV_BSIZE);
        if (wlen == bootarea_len)
@@ -1033,7 +1037,7 @@
                            "to create label", label_sector);
        }
 
-       *disk_lp = lab;
+       htotargetlabel(disk_lp, &lab);
        write_bootarea(f, label_sector);
        return 1;
 }
@@ -1069,7 +1073,7 @@
        if (filecore_partition_offset != 0) {
                disk_lp = find_label(f, filecore_partition_offset);
                if (disk_lp != NULL) {
-                       lab = *disk_lp;
+                       targettohlabel(&lab, disk_lp);
                        return 0;
                }
        }
@@ -1079,7 +1083,7 @@
 
        disk_lp = find_label(f, 0);
        if (disk_lp != NULL) {
-               lab = *disk_lp;
+               targettohlabel(&lab, disk_lp);
                return 0;
        }
 
--- /dev/null   2009-10-25 13:02:17.000000000 +0900
+++ sbin/disklabel/bswap.c      2009-10-25 12:41:41.000000000 +0900
@@ -0,0 +1,183 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)ufs_disksubr.c      7.16 (Berkeley) 5/4/91
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/types.h>
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbinclude/sys/disklabel.h>
+#else
+#include <sys/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
+#include "bswap.h"
+
+#if TARGET_BYTE_ORDER != BYTE_ORDER
+static void bswaplabel(struct disklabel *nlp, struct disklabel *olp);
+
+void
+bswaplabel(struct disklabel *nlp, struct disklabel *olp)
+{
+       int i;
+
+       nlp->d_magic          = bswap32(olp->d_magic);
+       nlp->d_type           = bswap16(olp->d_type);
+       nlp->d_subtype        = bswap16(olp->d_subtype);
+
+       /* no need to swap char strings */
+       memcpy(nlp->d_typename, olp->d_typename, sizeof(nlp->d_typename));
+
+       /* XXX What should we do for d_un (an union of char and pointers) ? */
+       memcpy(nlp->d_packname, olp->d_packname, sizeof(nlp->d_packname));
+
+       nlp->d_secsize        = bswap32(olp->d_secsize);
+       nlp->d_nsectors       = bswap32(olp->d_nsectors);
+       nlp->d_ntracks        = bswap32(olp->d_ntracks);
+       nlp->d_ncylinders     = bswap32(olp->d_ncylinders);
+       nlp->d_secpercyl      = bswap32(olp->d_secpercyl);
+       nlp->d_secperunit     = bswap32(olp->d_secperunit);
+
+       nlp->d_sparespertrack = bswap16(olp->d_sparespertrack);
+       nlp->d_sparespercyl   = bswap16(olp->d_sparespercyl);
+
+       nlp->d_acylinders     = bswap32(olp->d_acylinders);
+
+       nlp->d_rpm            = bswap16(olp->d_rpm);
+       nlp->d_interleave     = bswap16(olp->d_interleave);
+       nlp->d_trackskew      = bswap16(olp->d_trackskew);
+       nlp->d_cylskew        = bswap16(olp->d_cylskew);
+       nlp->d_headswitch     = bswap32(olp->d_headswitch);
+       nlp->d_trkseek        = bswap32(olp->d_trkseek);
+       nlp->d_flags          = bswap32(olp->d_flags);
+
+       for (i = 0; i < NDDATA; i++)
+               nlp->d_drivedata[i] = bswap32(olp->d_drivedata[i]);
+
+       for (i = 0; i < NSPARE; i++)
+               nlp->d_spare[i]     = bswap32(olp->d_spare[i]);
+
+       nlp->d_magic2         = bswap32(olp->d_magic2);
+       nlp->d_checksum       = bswap16(olp->d_checksum);
+
+       /* filesystem and partition information: */
+       nlp->d_npartitions    = bswap16(olp->d_npartitions);
+       nlp->d_bbsize         = bswap32(olp->d_bbsize);
+       nlp->d_sbsize         = bswap32(olp->d_sbsize);
+
+       for (i = 0; i < MAXPARTITIONS; i++) {
+               nlp->d_partitions[i].p_size =
+                   bswap32(olp->d_partitions[i].p_size);
+               nlp->d_partitions[i].p_offset =
+                   bswap32(olp->d_partitions[i].p_offset);
+               nlp->d_partitions[i].p_fsize =
+                   bswap32(olp->d_partitions[i].p_fsize);
+               /* p_fstype and p_frag is uint8_t, so no need to swap */
+               nlp->d_partitions[i].p_fstype = olp->d_partitions[i].p_fstype;
+               nlp->d_partitions[i].p_frag = olp->d_partitions[i].p_frag;
+               nlp->d_partitions[i].p_cpg =
+                   bswap16(olp->d_partitions[i].p_cpg);
+       }
+}
+
+void
+targettohlabel(struct disklabel *hlp, struct disklabel *tlp)
+{
+
+       bswaplabel(hlp, tlp);
+       /* update checksum in host endian */
+       hlp->d_checksum = 0;
+       hlp->d_checksum = dkcksum(hlp);
+}
+
+void
+htotargetlabel(struct disklabel *tlp, struct disklabel *hlp)
+{
+
+       bswaplabel(tlp, hlp);
+       /* update checksum in target endian */
+       tlp->d_checksum = 0;
+       tlp->d_checksum = dkcksum_re(tlp);
+}
+
+uint16_t
+dkcksum_re(struct disklabel *lp)
+{
+       uint16_t *start, *end;
+       uint16_t npartitions, sum;
+
+       sum = 0;
+
+       /* we can assume lp is reversed, but check it again for sanity */
+       if (lp->d_magic == DISKMAGIC)
+               npartitions = lp->d_npartitions;
+       else if (bswap32(lp->d_magic) == DISKMAGIC)
+               npartitions = bswap16(lp->d_npartitions);
+       else
+               npartitions = 0;
+
+       if (npartitions > MAXPARTITIONS)
+               npartitions = 0;
+
+       start = (uint16_t *)lp;
+       end   = (uint16_t *)&lp->d_partitions[npartitions];
+       while (start < end)
+               sum ^= *start++;
+       return sum;
+}
+#endif
--- /dev/null   2009-10-25 13:02:17.000000000 +0900
+++ sbin/disklabel/bswap.h      2009-10-25 12:22:23.000000000 +0900
@@ -0,0 +1,63 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+
+#if HAVE_NBTOOL_CONFIG_H
+#ifndef BYTE_ORDER
+#ifdef WORDS_BIGENDIAN
+#define BYTE_ORDER             BIG_ENDIAN
+#else
+#define BYTE_ORDER             LITTLE_ENDIAN
+#endif
+#endif
+#endif
+
+#ifndef TARGET_BYTE_ORDER
+#define TARGET_BYTE_ORDER      BYTE_ORDER
+#endif
+
+#if TARGET_BYTE_ORDER == BYTE_ORDER
+#define htotarget16(x)         (x)
+#define target16toh(x)         (x)
+#define htotarget32(x)         (x)
+#define target32toh(x)         (x)
+#define dkcksum_target(lp)     dkcksum(lp)
+#define htotargetlabel(tlp, hlp)                                       \
+           do {*(tlp) = *(hlp);} while (/* CONSTCOND */0)
+#define targettohlabel(hlp, tlp)                                       \
+           do {*(hlp) = *(tlp);} while (/* CONSTCOND */0)
+#else
+#define htotarget16(x)         bswap16(x)
+#define target16toh(x)         bswap16(x)
+#define htotarget32(x)         bswap32(x)
+#define target32toh(x)         bswap32(x)
+#define dkcksum_target(lp)     dkcksum_re(lp)
+
+void htotargetlabel(struct disklabel *, struct disklabel *);
+void targettohlabel(struct disklabel *, struct disklabel *);
+uint16_t dkcksum_re(struct disklabel *);
+#endif

---
Izumi Tsutsui


Home | Main Index | Thread Index | Old Index