Module Name:    src
Committed By:   matt
Date:           Fri May  3 16:05:12 UTC 2013

Modified Files:
        src/sbin/disklabel: Makefile bswap.c bswap.h dkcksum.c dkcksum.h
            extern.h interact.c main.c
        src/share/mk: bsd.own.mk
        src/sys/sys: disklabel.h
        src/tools/disklabel: Makefile

Log Message:
Make disklabel a MI tool.  It will use MACHINE/MACHINE_ARCH to determine
the disklabel params as well as allowing command-line options of -M <machine>
and -B {le,be} to specify MACHINE and byteorder to be used.


To generate a diff of this commit:
cvs rdiff -u -r1.69 -r1.70 src/sbin/disklabel/Makefile
cvs rdiff -u -r1.1 -r1.2 src/sbin/disklabel/bswap.c \
    src/sbin/disklabel/bswap.h
cvs rdiff -u -r1.13 -r1.14 src/sbin/disklabel/dkcksum.c
cvs rdiff -u -r1.5 -r1.6 src/sbin/disklabel/dkcksum.h
cvs rdiff -u -r1.12 -r1.13 src/sbin/disklabel/extern.h
cvs rdiff -u -r1.37 -r1.38 src/sbin/disklabel/interact.c
cvs rdiff -u -r1.28 -r1.29 src/sbin/disklabel/main.c
cvs rdiff -u -r1.729 -r1.730 src/share/mk/bsd.own.mk
cvs rdiff -u -r1.113 -r1.114 src/sys/sys/disklabel.h
cvs rdiff -u -r1.5 -r1.6 src/tools/disklabel/Makefile

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/disklabel/Makefile
diff -u src/sbin/disklabel/Makefile:1.69 src/sbin/disklabel/Makefile:1.70
--- src/sbin/disklabel/Makefile:1.69	Tue Aug 30 12:39:52 2011
+++ src/sbin/disklabel/Makefile	Fri May  3 16:05:12 2013
@@ -1,8 +1,8 @@
-#	$NetBSD: Makefile,v 1.69 2011/08/30 12:39:52 bouyer Exp $
+#	$NetBSD: Makefile,v 1.70 2013/05/03 16:05:12 matt Exp $
 #	@(#)Makefile	8.2 (Berkeley) 3/17/94
 
 PROG=	disklabel
-SRCS=	main.c dkcksum.c interact.c printlabel.c
+SRCS=	main.c dkcksum.c interact.c printlabel.c bswap.c
 MAN=	disklabel.5 disklabel.8
 .if (${HOSTPROG:U} == "")
 DPADD+= ${LIBUTIL}

Index: src/sbin/disklabel/bswap.c
diff -u src/sbin/disklabel/bswap.c:1.1 src/sbin/disklabel/bswap.c:1.2
--- src/sbin/disklabel/bswap.c:1.1	Tue Jan  5 15:45:26 2010
+++ src/sbin/disklabel/bswap.c	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: bswap.c,v 1.1 2010/01/05 15:45:26 tsutsui Exp $	*/
+/*	$NetBSD: bswap.c,v 1.2 2013/05/03 16:05:12 matt Exp $	*/
 
 /*-
  * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
@@ -59,6 +59,8 @@
 #include "nbtool_config.h"
 #endif
 
+#include <string.h>
+
 #include <sys/types.h>
 #if HAVE_NBTOOL_CONFIG_H
 #include <nbinclude/sys/disklabel.h>
@@ -69,11 +71,8 @@
 #include "bswap.h"
 #include "dkcksum.h"
 
-#if TARGET_BYTE_ORDER != BYTE_ORDER
-static void bswaplabel(struct disklabel *nlp, struct disklabel *olp);
-
-void
-bswaplabel(struct disklabel *nlp, struct disklabel *olp)
+static void
+bswaplabel(struct disklabel *nlp, const struct disklabel *olp)
 {
 	int i;
 
@@ -137,31 +136,37 @@ bswaplabel(struct disklabel *nlp, struct
 }
 
 void
-targettohlabel(struct disklabel *hlp, struct disklabel *tlp)
+targettohlabel(struct disklabel *hlp, const struct disklabel *tlp)
 {
 
-	bswaplabel(hlp, tlp);
+	if (bswap32(tlp->d_magic) == DISKMAGIC)
+		bswaplabel(hlp, tlp);
+	else
+		*hlp = *tlp;
 	/* update checksum in host endian */
 	hlp->d_checksum = 0;
 	hlp->d_checksum = dkcksum(hlp);
 }
 
 void
-htotargetlabel(struct disklabel *tlp, struct disklabel *hlp)
+htotargetlabel(struct disklabel *tlp, const struct disklabel *hlp)
 {
 
-	bswaplabel(tlp, hlp);
+	if (bswap_p)
+		bswaplabel(tlp, hlp);
+	else
+		*tlp = *hlp;
+
 	/* update checksum in target endian */
 	tlp->d_checksum = 0;
-	tlp->d_checksum = dkcksum_re(tlp);
+	tlp->d_checksum = dkcksum_target(tlp);
 }
 
 uint16_t
-dkcksum_re(struct disklabel *lp)
+dkcksum_target(struct disklabel *lp)
 {
 	uint16_t npartitions;
 
-	/* 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)
@@ -169,9 +174,8 @@ dkcksum_re(struct disklabel *lp)
 	else
 		npartitions = 0;
 
-	if (npartitions > MAXPARTITIONS)
+	if (npartitions > maxpartitions)
 		npartitions = 0;
 
 	return dkcksum_sized(lp, npartitions);
 }
-#endif
Index: src/sbin/disklabel/bswap.h
diff -u src/sbin/disklabel/bswap.h:1.1 src/sbin/disklabel/bswap.h:1.2
--- src/sbin/disklabel/bswap.h:1.1	Tue Jan  5 15:45:26 2010
+++ src/sbin/disklabel/bswap.h	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: bswap.h,v 1.1 2010/01/05 15:45:26 tsutsui Exp $	*/
+/*	$NetBSD: bswap.h,v 1.2 2013/05/03 16:05:12 matt Exp $	*/
 
 /*-
  * Copyright (c) 2009 Izumi Tsutsui.  All rights reserved.
@@ -34,31 +34,18 @@
 #define BYTE_ORDER		LITTLE_ENDIAN
 #endif
 #endif
+#else
+#include <sys/endian.h>
 #endif
 
-#ifndef TARGET_BYTE_ORDER
-#define TARGET_BYTE_ORDER	BYTE_ORDER
-#endif
-
-#if TARGET_BYTE_ORDER != BYTE_ORDER
-#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 *);
+extern int bswap_p;
+extern u_int maxpartitions;
 
-#else
-#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)
-#endif
+#define htotarget16(x)		(bswap_p ? bswap16(x) : (x))
+#define target16toh(x)		(bswap_p ? bswap16(x) : (x))
+#define htotarget32(x)		(bswap_p ? bswap32(x) : (x))
+#define target32toh(x)		(bswap_p ? bswap32(x) : (x))
+
+void htotargetlabel(struct disklabel *, const struct disklabel *);
+void targettohlabel(struct disklabel *, const struct disklabel *);
+uint16_t dkcksum_target(struct disklabel *);

Index: src/sbin/disklabel/dkcksum.c
diff -u src/sbin/disklabel/dkcksum.c:1.13 src/sbin/disklabel/dkcksum.c:1.14
--- src/sbin/disklabel/dkcksum.c:1.13	Tue Jan  5 15:45:26 2010
+++ src/sbin/disklabel/dkcksum.c	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: dkcksum.c,v 1.13 2010/01/05 15:45:26 tsutsui Exp $	*/
+/*	$NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)dkcksum.c	8.1 (Berkeley) 6/5/93";
 #else
-__RCSID("$NetBSD: dkcksum.c,v 1.13 2010/01/05 15:45:26 tsutsui Exp $");
+__RCSID("$NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $");
 #endif
 #endif /* not lint */
 
@@ -51,22 +51,23 @@ __RCSID("$NetBSD: dkcksum.c,v 1.13 2010/
 #include "dkcksum.h"
 
 uint16_t
-dkcksum(struct disklabel *lp)
+dkcksum(const struct disklabel *lp)
 {
 
 	return dkcksum_sized(lp, lp->d_npartitions);
 }
 
 uint16_t
-dkcksum_sized(struct disklabel *lp, size_t npartitions)
+dkcksum_sized(const struct disklabel *lp, size_t npartitions)
 {
-	uint16_t *start, *end;
+	const uint16_t *start, *end;
 	uint16_t sum;
 
 	sum = 0;
-	start = (uint16_t *)lp;
-	end = (uint16_t *)&lp->d_partitions[npartitions];
-	while (start < end)
+	start = (const uint16_t *)lp;
+	end = (const uint16_t *)&lp->d_partitions[npartitions];
+	while (start < end) {
 		sum ^= *start++;
+	}
 	return sum;
 }

Index: src/sbin/disklabel/dkcksum.h
diff -u src/sbin/disklabel/dkcksum.h:1.5 src/sbin/disklabel/dkcksum.h:1.6
--- src/sbin/disklabel/dkcksum.h:1.5	Tue Jan  5 15:45:26 2010
+++ src/sbin/disklabel/dkcksum.h	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: dkcksum.h,v 1.5 2010/01/05 15:45:26 tsutsui Exp $	*/
+/*	$NetBSD: dkcksum.h,v 1.6 2013/05/03 16:05:12 matt Exp $	*/
 
-uint16_t	dkcksum(struct disklabel *);
-uint16_t	dkcksum_sized(struct disklabel *, size_t);
+uint16_t	dkcksum(const struct disklabel *);
+uint16_t	dkcksum_sized(const struct disklabel *, size_t);

Index: src/sbin/disklabel/extern.h
diff -u src/sbin/disklabel/extern.h:1.12 src/sbin/disklabel/extern.h:1.13
--- src/sbin/disklabel/extern.h:1.12	Thu Jan 17 18:33:58 2013
+++ src/sbin/disklabel/extern.h	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: extern.h,v 1.12 2013/01/17 18:33:58 christos Exp $	*/
+/*	$NetBSD: extern.h,v 1.13 2013/05/03 16:05:12 matt Exp $	*/
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -32,17 +32,12 @@ void	showpartition(FILE *, struct diskla
 void	interact(struct disklabel *, int);
 int	list_fs_types(void);
 
+extern	u_int	maxpartitions;
 extern	char	specname[];
 extern	int	 Cflag;
 
 #ifdef HAVE_NBTOOL_CONFIG_H
-static int
-dk_ioctl(int f, void *arg)
-{
-	errno = ENOTTY;
-	return -1;
-}
-# define dk_ioctl(f, cmd, arg) dk_ioctl(f, arg)
+#define	dk_ioctl(f, cmd, arg)	(errno = ENOTTY, -1)
 #else
-# define dk_ioctl(f, cmd, arg) ioctl(f, cmd, arg)
-#endif /* HAVE_NBTOOL_CONFIG_H */
+int	dk_ioctl(int, u_long cmd, void *);
+#endif

Index: src/sbin/disklabel/interact.c
diff -u src/sbin/disklabel/interact.c:1.37 src/sbin/disklabel/interact.c:1.38
--- src/sbin/disklabel/interact.c:1.37	Thu Jan 17 18:33:58 2013
+++ src/sbin/disklabel/interact.c	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: interact.c,v 1.37 2013/01/17 18:33:58 christos Exp $	*/
+/*	$NetBSD: interact.c,v 1.38 2013/05/03 16:05:12 matt Exp $	*/
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: interact.c,v 1.37 2013/01/17 18:33:58 christos Exp $");
+__RCSID("$NetBSD: interact.c,v 1.38 2013/05/03 16:05:12 matt Exp $");
 #endif /* lint */
 
 #include <sys/param.h>
@@ -45,7 +45,7 @@ __RCSID("$NetBSD: interact.c,v 1.37 2013
 #include <sys/ioctl.h>
 
 #if HAVE_NBTOOL_CONFIG_H
-#define	getmaxpartitions()	MAXPARTITIONS
+#define	getmaxpartitions()	maxpartitions
 #include <nbinclude/sys/disklabel.h>
 #else
 #include <util.h>

Index: src/sbin/disklabel/main.c
diff -u src/sbin/disklabel/main.c:1.28 src/sbin/disklabel/main.c:1.29
--- src/sbin/disklabel/main.c:1.28	Thu Jan 17 18:33:58 2013
+++ src/sbin/disklabel/main.c	Fri May  3 16:05:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.28 2013/01/17 18:33:58 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.29 2013/05/03 16:05:12 matt Exp $	*/
 
 /*
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -76,7 +76,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 19
 static char sccsid[] = "@(#)disklabel.c	8.4 (Berkeley) 5/4/95";
 /* from static char sccsid[] = "@(#)disklabel.c	1.2 (Symmetric) 11/28/85"; */
 #else
-__RCSID("$NetBSD: main.c,v 1.28 2013/01/17 18:33:58 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.29 2013/05/03 16:05:12 matt Exp $");
 #endif
 #endif	/* not lint */
 
@@ -163,6 +163,7 @@ static int readlabel_direct(int);
 static void writelabel_direct(int);
 static int update_label(int, u_int, u_int);
 static struct disklabel *find_label(int, u_int);
+static void getmachineparams(const char *);
 
 static void		 makedisktab(FILE *, struct disklabel *);
 static void		 makelabel(const char *, const char *);
@@ -183,15 +184,142 @@ static int		 getulong(const char *, char
 
 static int set_writable_fd = -1;
 
-#if HAVE_NBTOOL_CONFIG_H
-#define GETLABELOFFSET()	LABELOFFSET
-#define GETLABELSECTOR()	LABELSECTOR
-#define GETLABELUSESMBR()	LABELUSESMBR
-#else /* HAVE_NBTOOL_CONFIG_H */
-#define GETLABELOFFSET()	getlabeloffset()
-#define GETLABELSECTOR()	getlabelsector()
-#define GETLABELUSESMBR()	getlabelusesmbr()
+static u_int labeloffset;
+static u_int labelsector;
+static int labelusesmbr;
+u_int maxpartitions;
+static int byteorder;
+
+static int biendian_p;
+#ifndef HAVE_NBTOOL_CONFIG_H
+static int native_p = 1;
 #endif
+int bswap_p;
+
+static const struct disklabel_params {
+	const char *machine;
+	int labelusesmbr;
+	u_int labelsector;
+	u_int labeloffset;
+	u_int maxpartitions;
+	u_int raw_part;
+	u_int oldmaxpartitions;
+	int byteorder;
+} disklabel_params[] = {
+	{ "mvme68k",	0, 0,   0,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "next68k",	0, 0,   0,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+
+	{ "algor",	0, 0,  64,  8, 2, 0, LITTLE_ENDIAN },	/* mips */
+	{ "alpha",	0, 0,  64,  8, 2, 0, LITTLE_ENDIAN },	/* alpha */
+	{ "luna68k",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "mac68k",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "news68k",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "newsmips",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* mips */
+	{ "pmax",	0, 0,  64,  8, 2, 0, LITTLE_ENDIAN },	/* mips */
+	{ "sun2",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "sun68k",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68010 */
+	{ "x68k",	0, 0,  64,  8, 2, 0, BIG_ENDIAN },	/* m68010 */
+
+	{ "vax",	0, 0,  64, 12, 2, 8, LITTLE_ENDIAN },	/* vax */
+
+	{ "amiga",	0, 0,  64, 16, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "amigappc",	0, 0,  64, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "evbmips",	0, 0,  64, 16, 2, 0, 0 },		/* mips */
+	{ "evbppc",	0, 0,  64, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+
+	{ "sparc",	0, 0, 128,  8, 2, 0, BIG_ENDIAN },	/* sun */
+	{ "sparc64",	0, 0, 128,  8, 2, 0, BIG_ENDIAN },	/* sun */
+	{ "sun3",	0, 0, 128,  8, 2, 0, BIG_ENDIAN },	/* sun */
+
+	{ "atari",	0, 0, 516, 16, 2, 0, BIG_ENDIAN },	/* m68k */
+
+	{ "mipsco",	0, 1,   0,  8, 2, 0, BIG_ENDIAN },	/* mips */
+	{ "mvmeppc",	0, 1,   0,  8, 3, 0, BIG_ENDIAN },	/* powerpc */
+
+	{ "bebox",	0, 1,   0,  8, 3, 0, BIG_ENDIAN },	/* powerpc */
+
+	{ "emips",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* mips */
+	{ "hp700",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* hppa */
+	{ "ibmnws",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "ofppc",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "rs6000",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "sandpoint",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "sgimips",	0, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* mips */
+
+	{ "sbmips",	0, 1,   0, 16, 3, 0, 0 },	/* mips */
+
+	{ "cesfic",	0, 2,   0,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+	{ "hp300",	0, 2,   0,  8, 2, 0, BIG_ENDIAN },	/* m68k */
+
+	{ "ews4800mips",0, 9,   0, 16, 15, 0, BIG_ENDIAN },	/* mips */
+
+	{ "macppc",	1, 0,  64, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "pmon",	1, 0,  64, 16, 2, 0, 0 },		/* evbmips */
+
+	{ "prep",	1, 1,   0,  8, 2, 0, BIG_ENDIAN },	/* powerpc */
+
+	{ "dreadmcast",	1, 1,   0, 16, 2, 0, LITTLE_ENDIAN },	/* sh3 */
+	{ "evbsh3",	1, 1,   0, 16, 2, 0, 0 },		/* sh3 */
+	{ "hpcsh",	1, 1,   0, 16, 2, 0, LITTLE_ENDIAN },	/* sh3 */
+	{ "evbppc-mbr",	1, 1,   0, 16, 2, 0, BIG_ENDIAN },	/* powerpc */
+	{ "mmeye",	1, 1,   0, 16, 2, 0, 0 },		/* sh3 */
+
+	{ "acorn26",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+	{ "acorn32",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+	{ "cats",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+	{ "evbarm",	1, 1,   0, 16, 2, 8, 0 },		/* arm */
+	{ "iyonix",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+	{ "netwinder",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+	{ "shark",	1, 1,   0, 16, 2, 8, LITTLE_ENDIAN },	/* arm */
+
+	{ "amd64",	1, 1,   0, 16, 3, 0, LITTLE_ENDIAN },	/* x86 */
+	{ "arc",	1, 1,   0, 16, 3, 0, LITTLE_ENDIAN },	/* mips */
+	{ "cobalt",	1, 1,   0, 16, 3, 0, LITTLE_ENDIAN },	/* mips */
+	{ "landisk",	1, 1,   0, 16, 3, 0, LITTLE_ENDIAN },	/* sh3 */
+
+	{ "epoc32",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* arm */
+	{ "hpcarm",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* arm */
+	{ "hpcmips",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* mips */
+	{ "i386",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* x86 */
+	{ "ia64",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* x86 */
+	{ "zaurus",	1, 1,   0, 16, 3, 8, LITTLE_ENDIAN },	/* arm */
+
+	{ NULL,		0, 0,   0,  0, 0, 0, 0 },	/* must be last */
+};
+
+#ifndef HAVE_NBTOOL_CONFIG_H
+static struct disklabel_params native_params;
+#endif
+
+static const struct arch_endian {
+	int byteorder;
+	const char *arch;
+} arch_endians[] = {
+	{ LITTLE_ENDIAN, "alpha" },
+	{ LITTLE_ENDIAN, "arm" },
+	{ LITTLE_ENDIAN, "earm" },
+	{ LITTLE_ENDIAN, "earmhf" },
+	{ LITTLE_ENDIAN, "i386" },
+	{ LITTLE_ENDIAN, "ia64" },
+	{ LITTLE_ENDIAN, "mipsel" },
+	{ LITTLE_ENDIAN, "mips64el" },
+	{ LITTLE_ENDIAN, "sh3el" },
+	{ LITTLE_ENDIAN, "vax" },
+
+	{ BIG_ENDIAN, "armeb" },
+	{ BIG_ENDIAN, "earmeb" },
+	{ BIG_ENDIAN, "earmhfeb" },
+	{ BIG_ENDIAN, "hppa" },
+	{ BIG_ENDIAN, "m68k" },
+	{ BIG_ENDIAN, "mipseb" },
+	{ BIG_ENDIAN, "mips64eb" },
+	{ BIG_ENDIAN, "powerpc" },
+	{ BIG_ENDIAN, "sh3eb" },
+	{ BIG_ENDIAN, "sparc" },
+	{ BIG_ENDIAN, "sparc64" },
+
+	{ 0, NULL },
+};
 
 /* Default location for label - only used if we don't find one to update */
 #define LABEL_OFFSET (dklabel_getlabelsector() * DEV_BSIZE + dklabel_getlabeloffset())
@@ -216,6 +344,55 @@ opendisk(const char *path, int flags, ch
 }
 #endif /* HAVE_NBTOOL_CONFIG_H */
 
+static void
+setbyteorder(int new_byteorder)
+{
+	static int set_p;
+
+	if ((!biendian_p || set_p)
+	    && byteorder != 0
+	    && byteorder != new_byteorder) {
+		warn("changing %s byteorder to %s",
+		    byteorder == LITTLE_ENDIAN ? "le" : "be",
+		    new_byteorder == LITTLE_ENDIAN ? "le" : "be");
+	}
+	byteorder = new_byteorder;
+	biendian_p = 0;
+	set_p = 1;
+}
+
+static void
+getmachineparams(const char *mach)
+{
+	const struct disklabel_params *dp = disklabel_params;
+	for (; dp->machine != NULL; dp++) {
+		if (!strcmp(mach, dp->machine)) {
+			labelusesmbr = dp->labelusesmbr;
+			labelsector = dp->labelsector;
+			labeloffset = dp->labeloffset;
+			maxpartitions = dp->maxpartitions;
+			biendian_p = (dp->byteorder == 0);
+			if (!biendian_p)
+				setbyteorder(dp->byteorder);
+			return;
+		}
+	}
+	errx(1, "%s: unknown machine type", mach);
+}
+
+static void
+getarchbyteorder(const char *arch)
+{
+	const struct arch_endian *p = arch_endians;
+	for (; p->arch != NULL; p++) {
+		if (!strcmp(arch, p->arch)) {
+			setbyteorder(p->byteorder);
+			return;
+		}
+	}
+	errx(1, "%s: unknown arch", arch);
+}
+
 static daddr_t
 dklabel_getlabelsector(void)
 {
@@ -224,7 +401,7 @@ dklabel_getlabelsector(void)
 	const char *val;
 
 	if ((val = getenv("DISKLABELSECTOR")) == NULL)
-		return GETLABELSECTOR();
+		return labelsector;
 	if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE)
 		err(EXIT_FAILURE, "DISKLABELSECTOR in environment");
 	return nval;
@@ -238,7 +415,7 @@ dklabel_getlabeloffset(void)
 	const char *val;
 
 	if ((val = getenv("DISKLABELOFFSET")) == NULL)
-		return GETLABELOFFSET();
+		return labeloffset;
 	if ((nval = strtoul(val, &end, 10)) == ULONG_MAX && errno == ERANGE)
 		err(EXIT_FAILURE, "DISKLABELOFFSET in environment");
 	return nval;
@@ -257,6 +434,7 @@ main(int argc, char *argv[])
 	FILE	*t;
 	int	 ch, f, error;
 	char	*dkname;
+	char	*cp;
 	struct stat sb;
 	int	 writable;
 	enum {
@@ -264,10 +442,30 @@ main(int argc, char *argv[])
 		WRITE, INTERACT, DELETE
 	} op = UNSPEC, old_op;
 
-	mflag = GETLABELUSESMBR();
+#ifndef HAVE_NBTOOL_CONFIG_H
+	labeloffset = native_params.labeloffset = getlabeloffset();
+	labelsector = native_params.labelsector = getlabelsector();
+	labelusesmbr = native_params.labelusesmbr = getlabelusesmbr();
+	maxpartitions = native_params.maxpartitions = getmaxpartitions();
+	native_params.byteorder = BYTE_ORDER;
+#endif
+
+	if ((cp = getenv("MACHINE")) != NULL) {
+		getmachineparams(cp);
+	}
+
+	if ((cp = getenv("MACHINE_ARCH")) != NULL) {
+		getarchbyteorder(cp);
+	}
+
+	mflag = labelusesmbr;
 	if (mflag < 0) {
+#if HAVE_NBTOOL_CONFIG_H
+		warn("getlabelusesmbr() failed");
+#else
 		warn("getlabelusesmbr() failed");
 		mflag = LABELUSESMBR;
+#endif
 	}
 #if HAVE_NBTOOL_CONFIG_H
 	/* We must avoid doing any ioctl requests */
@@ -275,7 +473,7 @@ main(int argc, char *argv[])
 #endif
 
 	error = 0;
-	while ((ch = getopt(argc, argv, "ACDFINRWef:ilmrtvw")) != -1) {
+	while ((ch = getopt(argc, argv, "AB:CDFIM:NRWef:ilmrtvw")) != -1) {
 		old_op = op;
 		switch (ch) {
 		case 'A':	/* Action all labels */
@@ -300,6 +498,18 @@ main(int argc, char *argv[])
 		case 'R':	/* Restore label from text file */
 			op = RESTORE;
 			break;
+		case 'B':	/* byteorder */
+			if (!strcmp(optarg, "be")) {
+				setbyteorder(BIG_ENDIAN);
+			} else if (!strcmp(optarg, "le")) {
+				setbyteorder(LITTLE_ENDIAN);
+			} else {
+				errx(1, "%s: not be or le", optarg);
+			}
+			break;
+		case 'M':	/* machine type */
+			getmachineparams(optarg);
+			break;
 		case 'N':	/* Disallow writes to label sector */
 			op = SETREADONLY;
 			break;
@@ -341,6 +551,34 @@ main(int argc, char *argv[])
 		if (old_op != UNSPEC && old_op != op)
 			usage();
 	}
+
+	if (maxpartitions == 0) {
+		errx(1, "unknown label: use -M/-B and $MACHINE/$MACHINE_ARCH");
+	}
+	if (byteorder != BIG_ENDIAN && byteorder != LITTLE_ENDIAN) {
+		errx(1, "unknown byteorder");
+	}
+	bswap_p = (byteorder != BYTE_ORDER);
+#ifdef DEBUG
+	printf("labelusesmbr=%d labelsector=%u labeloffset=%u maxparitions=%u\n",
+	    labelusesmbr, labelsector, labeloffset, maxpartitions);
+	printf("byteorder=%d bswap_p=%d\n", byteorder, bswap_p);
+#endif
+#ifndef HAVE_NBTOOL_CONFIG_H
+	/*
+	 * If the disklabel has the same location as the native disklabel and
+	 * fewer or equal paritions, we can use the native ioctls.  Otherwise
+	 * force file/raw access.
+	 */
+	native_p = native_params.labelusesmbr == labelusesmbr
+	    && native_params.labelsector == labelsector
+	    && native_params.labeloffset == labeloffset
+	    && maxparitions <= native_params.maxpartitions
+	    && !bswap_p;
+	if (!native_p)
+		Fflag = rflag = 1;
+#endif
+
 	argc -= optind;
 	argv += optind;
 
@@ -937,7 +1175,7 @@ find_label(int f, u_int sector)
 				    is_deleted, offset, sector);
 			continue;
 		}
-		if (target16toh(disk_lp->d_npartitions) > MAXPARTITIONS ||
+		if (target16toh(disk_lp->d_npartitions) > maxpartitions ||
 		    dkcksum_target(disk_lp) != 0) {
 			if (verbose > 0)
 				warnx("corrupt label found at offset %u in "
@@ -1465,9 +1703,9 @@ getasciilabel(FILE *f, struct disklabel 
 			continue;
 		}
 		if (sscanf(cp, "%lu partitions", &v) == 1) {
-			if (v == 0 || v > MAXPARTITIONS) {
+			if (v == 0 || v > maxpartitions) {
 				warnx("line %d: bad # of partitions", lineno);
-				lp->d_npartitions = MAXPARTITIONS;
+				lp->d_npartitions = maxpartitions;
 				errors++;
 			} else
 				lp->d_npartitions = v;
@@ -1591,7 +1829,7 @@ getasciilabel(FILE *f, struct disklabel 
 		/* We have a partition entry */
 		part = *cp - 'a';
 
-		if (part >= MAXPARTITIONS) {
+		if (part >= maxpartitions) {
 			warnx("line %d: bad partition name: %s", lineno, cp);
 			errors++;
 			continue;
@@ -1737,12 +1975,12 @@ checklabel(struct disklabel *lp)
 		errors++;
 	} else if (lp->d_sbsize % lp->d_secsize)
 		warnx("warning, super block size %% sector-size != 0");
-	if (lp->d_npartitions > MAXPARTITIONS)
+	if (lp->d_npartitions > maxpartitions)
 		warnx("warning, number of partitions (%" PRIu16 ") > "
 		    "MAXPARTITIONS (%d)",
-		    lp->d_npartitions, MAXPARTITIONS);
+		    lp->d_npartitions, maxpartitions);
 	else
-		for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) {
+		for (i = maxpartitions - 1; i >= lp->d_npartitions; i--) {
 			part = 'a' + i;
 			pp = &lp->d_partitions[i];
 			if (pp->p_size || pp->p_offset) {
@@ -1800,12 +2038,12 @@ usage(void)
 		const char *name;
 		const char *expn;
 	} usages[] = {
-	{ "[-ACFrtv] disk", "(to read label)" },
-	{ "-w [-DFrv] [-f disktab] disk disktype [packid]", "(to write label)" },
-	{ "-e [-CDFIrv] disk", "(to edit label)" },
-	{ "-i [-DFIrv] disk", "(to create a label interactively)" },
+	{ "[-ABCFMrtv] disk", "(to read label)" },
+	{ "-w [-BDFMrv] [-f disktab] disk disktype [packid]", "(to write label)" },
+	{ "-e [-BCDFMIrv] disk", "(to edit label)" },
+	{ "-i [-BDFMIrv] disk", "(to create a label interactively)" },
 	{ "-D [-v] disk", "(to delete existing label(s))" },
-	{ "-R [-DFrv] disk protofile", "(to restore label)" },
+	{ "-R [-BDFMrv] disk protofile", "(to restore label)" },
 	{ "[-NW] disk", "(to write disable/enable label)" },
 	{ "-l", "(to show all known file system types)" },
 	{ NULL, NULL }
@@ -1899,3 +2137,15 @@ list_fs_types(void)
 
 	return ret;
 }
+
+#ifndef HAVE_NBTOOL_CONFIG_H
+int
+dk_ioctl(int f, u_long cmd, void *arg)
+{
+	if (!native_p) {
+		errno = ENOTTY;
+		return -1;
+	}
+	return ioctl(f, cmd, arg);
+}
+#endif

Index: src/share/mk/bsd.own.mk
diff -u src/share/mk/bsd.own.mk:1.729 src/share/mk/bsd.own.mk:1.730
--- src/share/mk/bsd.own.mk:1.729	Fri May  3 15:55:21 2013
+++ src/share/mk/bsd.own.mk	Fri May  3 16:05:11 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: bsd.own.mk,v 1.729 2013/05/03 15:55:21 matt Exp $
+#	$NetBSD: bsd.own.mk,v 1.730 2013/05/03 16:05:11 matt Exp $
 
 # This needs to be before bsd.init.mk
 .if defined(BSD_MK_COMPAT_FILE)
@@ -289,7 +289,7 @@ TOOL_CTAGS=		${TOOLDIR}/bin/${_TOOL_PREF
 TOOL_CTFCONVERT=	${TOOLDIR}/bin/${_TOOL_PREFIX}ctfconvert
 TOOL_CTFMERGE=		${TOOLDIR}/bin/${_TOOL_PREFIX}ctfmerge
 TOOL_DB=		${TOOLDIR}/bin/${_TOOL_PREFIX}db
-TOOL_DISKLABEL=		${TOOLDIR}/bin/nbdisklabel-${MAKEWRAPPERMACHINE}
+TOOL_DISKLABEL=		${TOOLDIR}/bin/nbdisklabel
 TOOL_EQN=		${TOOLDIR}/bin/${_TOOL_PREFIX}eqn
 TOOL_FDISK=		${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-fdisk
 TOOL_FGEN=		${TOOLDIR}/bin/${_TOOL_PREFIX}fgen

Index: src/sys/sys/disklabel.h
diff -u src/sys/sys/disklabel.h:1.113 src/sys/sys/disklabel.h:1.114
--- src/sys/sys/disklabel.h:1.113	Thu Apr  4 12:52:25 2013
+++ src/sys/sys/disklabel.h	Fri May  3 16:05:11 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: disklabel.h,v 1.113 2013/04/04 12:52:25 martin Exp $	*/
+/*	$NetBSD: disklabel.h,v 1.114 2013/05/03 16:05:11 matt Exp $	*/
 
 /*
  * Copyright (c) 1987, 1988, 1993
@@ -49,7 +49,7 @@
  * paritition are machine dependent.
  */
 #if HAVE_NBTOOL_CONFIG_H
-#include <nbinclude/machine/disklabel.h>
+#define MAXPARTITIONS		MAXMAXPARTITIONS
 #else
 #include <machine/disklabel.h>
 #endif /* HAVE_NBTOOL_CONFIG_H */

Index: src/tools/disklabel/Makefile
diff -u src/tools/disklabel/Makefile:1.5 src/tools/disklabel/Makefile:1.6
--- src/tools/disklabel/Makefile:1.5	Mon Jun  4 18:53:03 2012
+++ src/tools/disklabel/Makefile	Fri May  3 16:05:12 2013
@@ -1,19 +1,11 @@
-#	$NetBSD: Makefile,v 1.5 2012/06/04 18:53:03 joerg Exp $
+#	$NetBSD: Makefile,v 1.6 2013/05/03 16:05:12 matt Exp $
 
-HOSTPROGNAME=	nbdisklabel-${MAKEWRAPPERMACHINE}
+HOSTPROGNAME=	nbdisklabel
 HOST_SRCDIR=	sbin/disklabel
-HOST_SRCS=	disklabel.c bswap.c
+HOST_SRCS=	disklabel.c
 
 NOMAN=	# defined
 
-.include <bsd.endian.mk>
-
-.if   ${TARGET_ENDIANNESS} == "1234"
-CPPFLAGS+= -DTARGET_BYTE_ORDER=LITTLE_ENDIAN
-.elif ${TARGET_ENDIANNESS} == "4321"
-CPPFLAGS+= -DTARGET_BYTE_ORDER=BIG_ENDIAN
-.endif
-
 .include "${.CURDIR}/../Makefile.nbincludes"
 .include "${.CURDIR}/../Makefile.host"
 

Reply via email to