tech-toolchain archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: disklabel endian issues
Lloyd Parkes wrote back on June:
> 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.
>
> Would anyone object if I added a couple of functions to sbin/disklabel/
> main.c to byte swap the struct fields? The hard thing will be working
> out if byte swapping will be needed.
I wonder if you have any progress on this one,
but how about this patch?
---
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 24 Oct 2009 18:35:41 -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/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 24 Oct 2009 18:35:41 -0000
@@ -117,6 +117,7 @@
#include "pathnames.h"
#include "extern.h"
#include "dkcksum.h"
+#include "bswap.h"
/*
* Disklabel: read and write disklabels.
@@ -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 *lp, hlp;
int i, offset;
const char *is_deleted;
@@ -932,16 +933,17 @@
lp->d_magic2 ^= ~0u;
is_deleted = "deleted ";
}
- if (lp->d_magic != DISKMAGIC) {
+ if (target32toh(lp->d_magic) != DISKMAGIC) {
/* XXX: Do something about byte-swapped labels ? */
- if (lp->d_magic == DISKMAGIC_REV &&
- lp->d_magic2 == DISKMAGIC_REV)
+ if (target32toh(lp->d_magic) == DISKMAGIC_REV &&
+ target32toh(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(lp->d_npartitions) > MAXPARTITIONS ||
+ dkcksum_target(lp) != 0) {
if (verbose > 0)
warnx("corrupt label found at offset %u in "
"sector %u", offset, sector);
@@ -955,15 +957,16 @@
/* To print all the labels we have to do it here */
/* XXX: maybe we should compare them? */
+ targettohlabel(&hlp, 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);
/* Remember we've found a label */
read_all = 2;
}
@@ -1033,7 +1036,7 @@
"to create label", label_sector);
}
- *disk_lp = lab;
+ htotargetlabel(disk_lp, &lab);
write_bootarea(f, label_sector);
return 1;
}
@@ -1069,7 +1072,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 +1082,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 03:15:01.000000000 +0900
+++ sbin/disklabel/bswap.c 2009-10-25 03:43:16.000000000 +0900
@@ -0,0 +1,181 @@
+/* $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"
+
+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 *nlp, struct disklabel *olp)
+{
+
+ bswaplabel(nlp, olp);
+ /* update checksum in host endian */
+ nlp->d_checksum = 0;
+ nlp->d_checksum = dkcksum(nlp);
+}
+
+void
+htotargetlabel(struct disklabel *nlp, struct disklabel *olp)
+{
+
+ bswaplabel(nlp, olp);
+ /* update checksum in target endian */
+ nlp->d_checksum = 0;
+ nlp->d_checksum = dkcksum_re(nlp);
+}
+
+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;
+}
--- /dev/null 2009-10-25 03:15:01.000000000 +0900
+++ sbin/disklabel/bswap.h 2009-10-25 03:25:13.000000000 +0900
@@ -0,0 +1,53 @@
+/* $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>
+
+#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(nlp, olp) \
+ do {*(nlp) = *(olp);} while (/* CONSTCOND */0)
+#define targettohlabel(nlp, olp) \
+ do {*(nlp) = *(olp);} 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