Subject: nbdisklabel, nbfdisk patch
To: None <tech-toolchain@netbsd.org, dyoung@ojctech.com>
From: David Young <dyoung@pobox.com>
List: tech-toolchain
Date: 04/22/2005 17:18:15
--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Soon I would like to commit the attached patches for disklabel and
fdisk host-tools. I have tested on RedHat 8.0 and on NetBSD-current,
and I can get some help to test on NetBSD 1.6.2. It would be great if
somebody would test on FreeBSD and/or OpenBSD.
fdisk surprises me on Linux by printing this:
command line: primary partition table invalid, no magic in sector 577801482179444736
Making partition 0 active.
I'm not sure what to make of 'command line:' and that huge sector number,
but the resulting CompactFlash image does boot my Soekris computers.
Dave
--
David Young OJC Technologies
dyoung@ojctech.com Urbana, IL * (217) 278-3933
--IJpNTDwzlM2Ie8A6
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=host-tools-patch-22-Apr-2005
Index: include/util.h
===================================================================
RCS file: /cvsroot/src/include/util.h,v
retrieving revision 1.34
diff -u -r1.34 util.h
--- include/util.h 12 Jan 2005 03:33:11 -0000 1.34
+++ include/util.h 22 Apr 2005 05:00:03 -0000
@@ -33,7 +33,9 @@
#define _UTIL_H_
#include <sys/cdefs.h>
+#if !HAVE_NBTOOL_CONFIG_H
#include <sys/ttycom.h>
+#endif /* !HAVE_NBTOOL_CONFIG_H */
#include <sys/types.h>
#include <stdio.h>
#include <pwd.h>
Index: lib/libc/gen/disklabel.c
===================================================================
RCS file: /cvsroot/src/lib/libc/gen/disklabel.c,v
retrieving revision 1.31
diff -u -r1.31 disklabel.c
--- lib/libc/gen/disklabel.c 23 Apr 2004 14:47:23 -0000 1.31
+++ lib/libc/gen/disklabel.c 22 Apr 2005 05:00:06 -0000
@@ -29,6 +29,10 @@
* SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
@@ -42,10 +46,17 @@
#include <sys/param.h>
#define DKTYPENAMES
#define FSTYPENAMES
-#include <sys/disklabel.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../sys/sys/disklabel.h"
+#include "../../include/disktab.h"
+#else
+#include <sys/disklabel.h>
+#include <disktab.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
#include <assert.h>
#include <ctype.h>
#include <errno.h>
@@ -55,8 +66,6 @@
#include <string.h>
#include <unistd.h>
-#include <disktab.h>
-
#ifdef __weak_alias
__weak_alias(getdiskbyname,_getdiskbyname)
#endif
Index: sbin/disklabel/Makefile
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/Makefile,v
retrieving revision 1.54
diff -u -r1.54 Makefile
--- sbin/disklabel/Makefile 20 Jan 2005 16:39:23 -0000 1.54
+++ sbin/disklabel/Makefile 22 Apr 2005 05:00:15 -0000
@@ -2,7 +2,7 @@
# @(#)Makefile 8.2 (Berkeley) 3/17/94
PROG= disklabel
-SRCS= disklabel.c dkcksum.c interact.c printlabel.c
+SRCS= main.c dkcksum.c interact.c printlabel.c
MAN= disklabel.5 disklabel.8
LDADD+= -lutil
DPADD+= ${LIBUTIL}
Index: sbin/disklabel/disklabel.c
===================================================================
RCS file: sbin/disklabel/disklabel.c
diff -N sbin/disklabel/disklabel.c
--- sbin/disklabel/disklabel.c 12 Nov 2004 01:00:40 -0000 1.135
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,1945 +0,0 @@
-/* $NetBSD: disklabel.c,v 1.135 2004/11/12 01:00:40 hubertf Exp $ */
-
-/*
- * Copyright (c) 1987, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Symmetric Computer Systems.
- *
- * 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
- The Regents of the University of California. All rights reserved.\n");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-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: disklabel.c,v 1.135 2004/11/12 01:00:40 hubertf Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/file.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#define DKTYPENAMES
-#define FSTYPENAMES
-#include <sys/disklabel.h>
-#include <sys/bootblock.h>
-
-#include <ufs/ufs/dinode.h>
-#include <ufs/ffs/fs.h>
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <unistd.h>
-#include <signal.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <util.h>
-
-#include <disktab.h>
-
-#include "pathnames.h"
-#include "extern.h"
-#include "dkcksum.h"
-
-/*
- * Disklabel: read and write disklabels.
- * The label is usually placed on one of the first sectors of the disk.
- * Many machines also place a bootstrap in the same area,
- * in which case the label is embedded in the bootstrap.
- * The bootstrap source must leave space at the proper offset
- * for the label on such machines.
- */
-
-#ifndef BBSIZE
-#define BBSIZE 8192 /* size of boot area, with label */
-#endif
-
-#ifndef NUMBOOT
-#define NUMBOOT 0
-#endif
-
-#define DEFEDITOR _PATH_VI
-
-static char *dkname;
-static char tmpfil[MAXPATHLEN];
-
-static char namebuf[BBSIZE], *np = namebuf;
-static struct disklabel lab;
-
-char bootarea[BBSIZE];
-char *specname;
-
-
-#if NUMBOOT > 0
-static int installboot; /* non-zero if we should install a boot program */
-static char *bootbuf; /* pointer to buffer with remainder of boot prog */
-static int bootsize; /* size of remaining boot program */
-static char *xxboot; /* primary boot */
-static char *bootxx; /* secondary boot */
-static char boot0[MAXPATHLEN];
-#if NUMBOOT > 1
-static char boot1[MAXPATHLEN];
-#endif /* NUMBOOT > 1 */
-#endif /* NUMBOOT > 0 */
-
-static enum {
- UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, WRITE, WRITEBOOT, INTERACT
-} op = UNSPEC;
-
-static int Fflag;
-static int rflag;
-static int tflag;
- int Cflag;
-static int Iflag;
-
-#define COMMON_OPTIONS "BCFINRWb:ef:irs:tw"
-
-#ifdef DEBUG
-static int debug;
-#define OPTIONS COMMON_OPTIONS "d"
-#else /* ! DEBUG */
-#define OPTIONS COMMON_OPTIONS
-#endif /* ! DEBUG */
-
-#ifdef USE_MBR
-static struct mbr_partition *dosdp; /* i386 DOS partition, if found */
-static struct mbr_partition *readmbr(int);
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
-static u_int filecore_partition_offset;
-static u_int get_filecore_partition(int);
-static int filecore_checksum(u_char *);
-#endif /* USE_ACORN */
-
-#if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
-static void confirm(const char *);
-#endif
-
-int main(int, char *[]);
-
-static void makedisktab(FILE *, struct disklabel *);
-static void makelabel(const char *, const char *,
- struct disklabel *);
-static void l_perror(const char *);
-static struct disklabel *readlabel(int);
-static struct disklabel *makebootarea(char *, struct disklabel *, int);
-static int edit(struct disklabel *, int);
-static int editit(void);
-static char *skip(char *);
-static char *word(char *);
-static int getasciilabel(FILE *, struct disklabel *);
-#if NUMBOOT > 0
-static void setbootflag(struct disklabel *);
-#endif
-static void usage(void);
-static int getulong(const char *, char, char **,
- unsigned long *, unsigned long);
-#define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX)
-#define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX)
-#define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX)
-
-int
-main(int argc, char *argv[])
-{
- struct disklabel *lp;
- FILE *t;
- int ch, f, writable, error;
-
- error = 0;
- while ((ch = getopt(argc, argv, OPTIONS)) != -1)
- switch (ch) {
-#if NUMBOOT > 0
- case 'B':
- ++installboot;
- break;
- case 'b':
- xxboot = optarg;
- break;
-#if NUMBOOT > 1
- case 's':
- bootxx = optarg;
- break;
-#endif /* NUMBOOT > 1 */
-#endif /* NUMBOOT > 0 */
- case 'C':
- ++Cflag;
- break;
- case 'F':
- ++Fflag;
- break;
- case 'I':
- ++Iflag;
- break;
- case 'N':
- if (op != UNSPEC)
- usage();
- writable = 0;
- op = SETWRITABLE;
- break;
- case 'R':
- if (op != UNSPEC)
- usage();
- op = RESTORE;
- break;
- case 'W':
- if (op != UNSPEC)
- usage();
- writable = 1;
- op = SETWRITABLE;
- break;
- case 'e':
- if (op != UNSPEC)
- usage();
- op = EDIT;
- break;
- case 'f':
- if (setdisktab(optarg) == -1)
- usage();
- break;
- case 'i':
- if (op != UNSPEC)
- usage();
- op = INTERACT;
- break;
- case 't':
- ++tflag;
- break;
- case 'r':
- ++rflag;
- break;
- case 'w':
- if (op != UNSPEC)
- usage();
- op = WRITE;
- break;
-#ifdef DEBUG
- case 'd':
- debug++;
- break;
-#endif
- case '?':
- default:
- usage();
- }
- argc -= optind;
- argv += optind;
-
-#if NUMBOOT > 0
- if (installboot) {
- rflag++;
- if (op == UNSPEC)
- op = WRITEBOOT;
- } else {
- if (op == UNSPEC)
- op = READ;
- }
-#else /* NUMBOOT <= 0 */
- if (op == UNSPEC)
- op = READ;
-#endif /* NUMBOOT <= 0 */
-
- if (argc < 1)
- usage();
-
- if (Iflag && op != EDIT && op != INTERACT)
- usage();
-
- dkname = argv[0];
- f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, np, MAXPATHLEN, 0);
- specname = np;
- np += strlen(specname) + 1;
- if (f < 0)
- err(4, "%s", specname);
-
-#ifdef USE_MBR
- /*
- * Check for presence of DOS partition table in
- * master boot record. Return pointer to NetBSD/i386
- * partition, if present.
- */
- dosdp = readmbr(f);
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
- /*
- * Check for the presence of a RiscOS filecore boot block
- * indicating an ADFS file system on the disc.
- * Return the offset to the NetBSD part of the disc if
- * this can be determined.
- * This routine will terminate disklabel if the disc
- * is found to be ADFS only.
- */
- filecore_partition_offset = get_filecore_partition(f);
-#endif /* USE_ACORN */
-
- switch (op) {
-
- case EDIT:
- if (argc != 1)
- usage();
- lp = readlabel(f);
- error = edit(lp, f);
- break;
-
- case INTERACT:
- if (argc != 1)
- usage();
- lp = readlabel(f);
- /*
- * XXX: Fill some default values so checklabel does not fail
- */
- if (lp->d_bbsize == 0)
- lp->d_bbsize = BBSIZE;
- if (lp->d_sbsize == 0)
- lp->d_sbsize = SBLOCKSIZE;
- interact(lp, f);
- break;
-
- case READ:
- if (argc != 1)
- usage();
- lp = readlabel(f);
- if (tflag)
- makedisktab(stdout, lp);
- else {
- showinfo(stdout, lp, specname);
- showpartitions(stdout, lp, Cflag);
- }
- error = checklabel(lp);
- if (error)
- error += 100;
- break;
-
- case RESTORE:
- if (argc < 2 || argc > 3)
- usage();
-#if NUMBOOT > 0
- if (installboot && argc == 3)
- makelabel(argv[2], (char *)0, &lab);
-#endif
- lp = makebootarea(bootarea, &lab, f);
- if (!(t = fopen(argv[1], "r")))
- err(4, "%s", argv[1]);
- if (getasciilabel(t, lp))
- error = writelabel(f, bootarea, lp);
- else
- error = 1;
- break;
-
- case SETWRITABLE:
- if (ioctl(f, DIOCWLABEL, (char *)&writable) < 0)
- err(4, "ioctl DIOCWLABEL");
- break;
-
- case WRITE:
- if (argc < 2 || argc > 3)
- usage();
- makelabel(argv[1], argc == 3 ? argv[2] : (char *)0, &lab);
- lp = makebootarea(bootarea, &lab, f);
- *lp = lab;
- if (checklabel(lp) == 0)
- error = writelabel(f, bootarea, lp);
- else
- error = 1;
- break;
-
- case WRITEBOOT:
-#if NUMBOOT > 0
- {
- struct disklabel tlab;
-
- lp = readlabel(f);
- tlab = *lp;
- if (argc == 2)
- makelabel(argv[1], (char *)0, &lab);
- lp = makebootarea(bootarea, &lab, f);
- *lp = tlab;
- if (checklabel(lp) == 0)
- error = writelabel(f, bootarea, lp);
- else
- error = 1;
- break;
- }
-#endif /* NUMBOOT > 0 */
-
- case UNSPEC:
- usage();
-
- }
- exit(error);
-}
-
-/*
- * Construct a prototype disklabel from /etc/disktab. As a side
- * effect, set the names of the primary and secondary boot files
- * if specified.
- */
-static void
-makelabel(const char *type, const char *name, struct disklabel *lp)
-{
- struct disklabel *dp;
-
- dp = getdiskbyname(type);
- if (dp == NULL)
- errx(1, "unknown disk type: %s", type);
- *lp = *dp;
-
-#if NUMBOOT > 0
- /*
- * Set bootstrap name(s).
- * 1. If set from command line, use those,
- * 2. otherwise, check if disktab specifies them (b0 or b1),
- * 3. otherwise, makebootarea() will choose ones based on the name
- * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
- */
- if (!xxboot && lp->d_boot0) {
- if (*lp->d_boot0 != '/')
- (void)snprintf(boot0, sizeof(boot0), "%s/%s",
- _PATH_BOOTDIR, lp->d_boot0);
- else
- (void)strlcpy(boot0, lp->d_boot0, sizeof(boot0));
- xxboot = boot0;
- }
-
-#if NUMBOOT > 1
- if (!bootxx && lp->d_boot1) {
- if (*lp->d_boot1 != '/')
- (void)snprintf(boot1, sizeof(boot1), "%s/%s",
- _PATH_BOOTDIR, lp->d_boot1);
- else
- (void)strlcpy(boot1, lp->d_boot1, sizeof(boot1));
- bootxx = boot1;
- }
-#endif /* NUMBOOT > 1 */
-#endif /* NUMBOOT > 0 */
-
- /* d_packname is union d_boot[01], so zero */
- (void) memset(lp->d_packname, 0, sizeof(lp->d_packname));
- if (name)
- (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
-}
-
-#if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
-static void
-confirm(const char *txt)
-{
- int first, ch;
-
- (void) printf("%s? [n]: ", txt);
- (void) fflush(stdout);
- first = ch = getchar();
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (first != 'y' && first != 'Y')
- exit(0);
-}
-#endif /* USE_MBR || USE_ACORN && notyet */
-
-int
-writelabel(int f, char *boot, struct disklabel *lp)
-{
- int writable;
- off_t sectoffset;
-
- sectoffset = 0;
-#if NUMBOOT > 0
- setbootflag(lp);
-#endif
- lp->d_magic = DISKMAGIC;
- lp->d_magic2 = DISKMAGIC;
- lp->d_checksum = 0;
- lp->d_checksum = dkcksum(lp);
-
- if (Fflag || rflag || Iflag)
- {
-#ifdef USE_MBR
- struct partition *pp = &lp->d_partitions[2];
-
- /*
- * If NetBSD/i386 DOS partition is missing, or if
- * the label to be written is not within partition,
- * prompt first. Need to allow this in case operator
- * wants to convert the drive for dedicated use.
- */
- if (dosdp) {
- if (dosdp->mbrp_start != pp->p_offset) {
- printf("NetBSD slice at %u, "
- "partition C at %u\n", dosdp->mbrp_start,
- pp->p_offset);
- confirm("Write outside MBR partition");
- }
- sectoffset = (off_t)pp->p_offset * lp->d_secsize;
- } else {
- sectoffset = 0;
- }
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
- /* XXX */
- sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
-#endif /* USE_ACORN */
-
- /*
- * First set the kernel disk label,
- * then write a label to the raw disk.
- * If the SDINFO ioctl fails because it is unimplemented,
- * keep going; otherwise, the kernel consistency checks
- * may prevent us from changing the current (in-core)
- * label.
- */
- if (ioctl(f, DIOCSDINFO, lp) < 0 &&
- errno != ENODEV && errno != ENOTTY) {
- l_perror("ioctl DIOCSDINFO");
- return (1);
- }
- if (lseek(f, sectoffset, SEEK_SET) < 0) {
- perror("lseek");
- return (1);
- }
- /*
- * write enable label sector before write (if necessary),
- * disable after writing.
- */
- writable = 1;
- if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
- perror("ioctl DIOCWLABEL");
-
-#ifdef __alpha__
- /*
- * The Alpha requires that the boot block be checksummed.
- * The NetBSD/alpha disklabel.h provides a macro to do it.
- */
- {
- struct alpha_boot_block *bb;
-
- bb = (struct alpha_boot_block *)boot;
- ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
- }
-#endif /* __alpha__ */
-
- if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
- perror("write");
- return (1);
- }
-
-#if NUMBOOT > 0
- /*
- * Output the remainder of the disklabel
- */
- if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
- perror("write");
- return (1);
- }
-#endif /* NUMBOOT > 0 */
-
- writable = 0;
- if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
- perror("ioctl DIOCWLABEL");
- /*
- * Now issue a DIOCWDINFO. This will let the kernel convert the
- * disklabel to some machdep format if needed.
- */
- if (!Fflag && ioctl(f, DIOCWDINFO, lp) < 0) {
- l_perror("ioctl DIOCWDINFO");
- return (1);
- }
- } else {
- if (ioctl(f, DIOCWDINFO, lp) < 0) {
- l_perror("ioctl DIOCWDINFO");
- return (1);
- }
- }
-
-#ifdef __vax__
- if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
- daddr_t alt;
- int i;
-
- alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
- for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
- (void)lseek(f, (off_t)(alt + i) * lp->d_secsize,
- SEEK_SET);
- if (write(f, boot, lp->d_secsize) < lp->d_secsize)
- warn("alternate label %d write", i/2);
- }
- }
-#endif /* __vax__ */
-
- return (0);
-}
-
-static void
-l_perror(const char *s)
-{
-
- switch (errno) {
-
- case ESRCH:
- warnx("%s: No disk label on disk;\n"
- "use \"disklabel -I\" to install initial label", s);
- break;
-
- case EINVAL:
- warnx("%s: Label magic number or checksum is wrong!\n"
- "(disklabel or kernel is out of date?)", s);
- break;
-
- case EBUSY:
- warnx("%s: Open partition would move or shrink", s);
- break;
-
- case EXDEV:
- warnx("%s: Labeled partition or 'a' partition must start"
- " at beginning of disk", s);
- break;
-
- default:
- warn("%s", s);
- break;
- }
-}
-
-#ifdef USE_MBR
-/*
- * Fetch DOS partition table from disk.
- */
-static struct mbr_partition *
-readmbr(int f)
-{
- struct mbr_partition *dp;
- struct mbr_sector mbr;
- int part;
- uint ext_base, next_ext, this_ext;
- static struct mbr_partition netbsd_part;
-
- /*
- * Don't (yet) know disk geometry (BIOS), use
- * partition table to find NetBSD/i386 partition, and obtain
- * disklabel from there.
- */
-
- ext_base = 0;
- next_ext = 0;
- for (;;) {
- this_ext = next_ext;
- next_ext = 0;
- if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE)
- != sizeof(mbr)) {
- warn("Can't read master boot record %d", this_ext);
- return 0;
- }
-
- /* Check if table is valid. */
- if (mbr.mbr_magic != htole16(MBR_MAGIC)) {
- warnx("Invalid signature in mbr record %d", this_ext);
- return 0;
- }
-
- dp = &mbr.mbr_parts[0];
-#if defined(_no_longer_needed) && !defined(__i386__) && !defined(__x86_64__)
- /* avoid alignment error */
- memcpy(mbr, dp, MBR_PART_COUNT * sizeof(*dp));
- dp = (struct mbr_partition *)mbr;
-#endif /* ! __i386__ */
-
- /* Find NetBSD partition. */
- for (part = 0; part < MBR_PART_COUNT; dp++, part++) {
- dp->mbrp_start = le32toh(dp->mbrp_start);
- dp->mbrp_size = le32toh(dp->mbrp_size);
- switch (dp->mbrp_type) {
- case MBR_PTYPE_NETBSD:
- netbsd_part = *dp;
- break;
- case MBR_PTYPE_EXT:
- case MBR_PTYPE_EXT_LBA:
- case MBR_PTYPE_EXT_LNX:
- next_ext = dp->mbrp_start;
- continue;
-#ifdef COMPAT_386BSD_MBRPART
- case MBR_PTYPE_386BSD:
- if (ext_base == 0)
- netbsd_part = *dp;
- continue;
-#endif /* COMPAT_386BSD_MBRPART */
- default:
- continue;
- }
- break;
- }
- if (part < MBR_PART_COUNT)
- /* We found a netbsd partition */
- break;
- if (next_ext == 0)
- /* No more extended partitions */
- break;
- next_ext += ext_base;
- if (ext_base == 0)
- ext_base = next_ext;
-
- if (next_ext <= this_ext) {
- warnx("Invalid extended chain %x <= %x",
- next_ext, this_ext);
- break;
- }
- }
-
- if (netbsd_part.mbrp_type == 0)
- return 0;
-
- netbsd_part.mbrp_start += this_ext;
- return &netbsd_part;
-}
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
-/*
- * static int filecore_checksum(u_char *bootblock)
- *
- * Calculates the filecore boot block checksum. This is used to validate
- * a filecore boot block on the disk. If a boot block is validated then
- * it is used to locate the partition table. If the boot block is not
- * validated, it is assumed that the whole disk is NetBSD.
- *
- * The basic algorithm is:
- *
- * for (each byte in block, excluding checksum) {
- * sum += byte;
- * if (sum > 255)
- * sum -= 255;
- * }
- *
- * That's equivalent to summing all of the bytes in the block
- * (excluding the checksum byte, of course), then calculating the
- * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
- * expression may or may not yield a faster checksum function,
- * but it's easier to reason about.
- *
- * Note that if you have a block filled with bytes of a single
- * value "X" (regardless of that value!) and calculate the cksum
- * of the block (excluding the checksum byte), you will _always_
- * end up with a checksum of X. (Do the math; that can be derived
- * from the checksum calculation function!) That means that
- * blocks which contain bytes which all have the same value will
- * always checksum properly. That's a _very_ unlikely occurence
- * (probably impossible, actually) for a valid filecore boot block,
- * so we treat such blocks as invalid.
- */
-static int
-filecore_checksum(u_char *bootblock)
-{
- u_char byte0, accum_diff;
- u_int sum;
- int i;
-
- sum = 0;
- accum_diff = 0;
- byte0 = bootblock[0];
-
- /*
- * Sum the contents of the block, keeping track of whether
- * or not all bytes are the same. If 'accum_diff' ends up
- * being zero, all of the bytes are, in fact, the same.
- */
- for (i = 0; i < 511; ++i) {
- sum += bootblock[i];
- accum_diff |= bootblock[i] ^ byte0;
- }
-
- /*
- * Check to see if the checksum byte is the same as the
- * rest of the bytes, too. (Note that if all of the bytes
- * are the same except the checksum, a checksum compare
- * won't succeed, but that's not our problem.)
- */
- accum_diff |= bootblock[i] ^ byte0;
-
- /* All bytes in block are the same; call it invalid. */
- if (accum_diff == 0)
- return (-1);
-
- return (sum - ((sum - 1) / 255) * 255);
-}
-
-/*
- * Fetch filecore bootblock from disk and analyse it
- */
-static u_int
-get_filecore_partition(int f)
-{
- struct filecore_bootblock *fcbb;
- static char bb[DEV_BSIZE];
- u_int offset;
-
- if (lseek(f, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0 ||
- read(f, bb, sizeof(bb)) != sizeof(bb))
- err(4, "can't read filecore boot block");
- fcbb = (struct filecore_bootblock *)bb;
-
- /* Check if table is valid. */
- if (filecore_checksum(bb) != fcbb->checksum)
- return (0);
-
- /*
- * Check for NetBSD/arm32 (RiscBSD) partition marker.
- * If found the NetBSD disklabel location is easy.
- */
- offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8))
- * fcbb->heads * fcbb->secspertrack;
- if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
- return (offset);
- else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
- struct riscix_partition_table *riscix_part;
- int loop;
-
- /*
- * Read the RISCiX partition table and search for the
- * first partition named "RiscBSD", "NetBSD", or "Empty:"
- *
- * XXX is use of 'Empty:' really desirable?! -- cgd
- */
-
- if (lseek(f, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0 ||
- read(f, bb, sizeof(bb)) != sizeof(bb))
- err(4, "can't read riscix partition table");
- riscix_part = (struct riscix_partition_table *)bb;
-
- for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
- if (strcmp(riscix_part->partitions[loop].rp_name,
- "RiscBSD") == 0 ||
- strcmp(riscix_part->partitions[loop].rp_name,
- "NetBSD") == 0 ||
- strcmp(riscix_part->partitions[loop].rp_name,
- "Empty:") == 0) {
- offset = riscix_part->partitions[loop].rp_start;
- break;
- }
- }
- if (loop == NRISCIX_PARTITIONS) {
- /*
- * Valid filecore boot block, RISCiX partition table
- * but no NetBSD partition. We should leave this
- * disc alone.
- */
- errx(4, "cannot label: no NetBSD partition found"
- " in RISCiX partition table");
- }
- return (offset);
- } else {
- /*
- * Valid filecore boot block and no non-ADFS partition.
- * This means that the whole disc is allocated for ADFS
- * so do not trash ! If the user really wants to put a
- * NetBSD disklabel on the disc then they should remove
- * the filecore boot block first with dd.
- */
- errx(4, "cannot label: filecore-only disk"
- " (no non-ADFS partition)");
- }
- return (0);
-}
-#endif /* USE_ACORN */
-
-/*
- * Fetch disklabel for disk.
- * Use ioctl to get label unless -r flag is given.
- */
-static struct disklabel *
-readlabel(int f)
-{
- struct disklabel *lp;
-
- if (Fflag || rflag || Iflag) {
- const char *msg;
- off_t sectoffset;
-
- msg = NULL;
- sectoffset = 0;
-
-#ifdef USE_MBR
- if (dosdp)
- sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
- /* XXX */
- sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
-#endif /* USE_ACORN */
-
- if (lseek(f, sectoffset, SEEK_SET) < 0 ||
- read(f, bootarea, BBSIZE) != BBSIZE)
- err(4, "%s", specname);
-
- msg = "no disklabel";
- for (lp = (struct disklabel *)bootarea;
- lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
- lp = (struct disklabel *)((char *)lp + sizeof(long))) {
- if (lp->d_magic == DISKMAGIC &&
- lp->d_magic2 == DISKMAGIC) {
- if (lp->d_npartitions <= MAXPARTITIONS &&
- dkcksum(lp) == 0)
- return (lp);
- msg = "disk label corrupted";
- }
- }
- if (msg != NULL && !Iflag)
- errx(1, "%s", msg);
- /*
- * There was no label on the disk. Get the fictious one
- * as a basis for initialisation.
- */
- lp = makebootarea(bootarea, &lab, f);
- if (ioctl(f, DIOCGDINFO, lp) < 0 &&
- ioctl(f, DIOCGDEFLABEL, lp) < 0)
- errx(1, "could not get initial label");
- } else {
- lp = &lab;
- if (ioctl(f, DIOCGDINFO, lp) < 0)
- err(4, "ioctl DIOCGDINFO");
- }
- return (lp);
-}
-
-/*
- * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
- * Returns a pointer to the disklabel portion of the bootarea.
- */
-static struct disklabel *
-makebootarea(char *boot, struct disklabel *dp, int f)
-{
- struct disklabel *lp;
- char *p;
- daddr_t lsec;
- off_t loff;
-#if NUMBOOT > 0
- int b;
- char *dkbasename;
-# if NUMBOOT <= 1
- struct stat sb;
-# endif
-#endif /* NUMBOOT > 0 */
-
- if ((lsec = getlabelsector()) < 0)
- err(4, "getlabelsector()");
- if ((loff = getlabeloffset()) < 0)
- err(4, "getlabeloffset()");
-
- /* XXX */
- if (dp->d_secsize == 0) {
- dp->d_secsize = DEV_BSIZE;
- dp->d_bbsize = BBSIZE;
- }
- lp = (struct disklabel *) (boot + (lsec * dp->d_secsize) + loff);
- (void) memset(lp, 0, sizeof(*lp));
-
-#ifdef SAVEBOOTAREA
- /*
- * We must read the current bootarea so we don't clobber the
- * existing boot block, if any.
- */
- if (rflag || Iflag) {
- off_t sectoffset;
-
- sectoffset = 0;
- if (lseek(f, sectoffset, SEEK_SET) < 0 ||
- read(f, boot, BBSIZE) != BBSIZE)
- err(4, "%s", specname);
- (void) memset(lp, 0, sizeof(*lp));
- }
-#endif /* SAVEBOOTAREA */
-
-#if NUMBOOT > 0
- /*
- * If we are not installing a boot program but we are installing a
- * label on disk then we must read the current bootarea so we don't
- * clobber the existing boot.
- */
- if (!installboot) {
- if (rflag || Iflag) {
- off_t sectoffset;
-
- sectoffset = 0;
-#ifdef USE_MBR
- if (dosdp)
- sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
-#endif /* USE_MBR */
-
-#ifdef USE_ACORN
- /* XXX */
- sectoffset = (off_t)filecore_partition_offset
- * DEV_BSIZE;
-#endif /* USE_ACORN */
-
- if (lseek(f, sectoffset, SEEK_SET) < 0 ||
- read(f, boot, BBSIZE) != BBSIZE)
- err(4, "%s", specname);
- (void) memset(lp, 0, sizeof(*lp));
- }
- return (lp);
- }
- /*
- * We are installing a boot program. Determine the name(s) and
- * read them into the appropriate places in the boot area.
- */
- if (!xxboot || !bootxx) {
- dkbasename = np;
- if ((p = strrchr(dkname, '/')) == NULL)
- p = dkname;
- else
- p++;
- while (*p && !isdigit(*p & 0xff))
- *np++ = *p++;
- *np++ = '\0';
-
- if (!xxboot) {
- (void)sprintf(np, "%s/%sboot",
- _PATH_BOOTDIR, dkbasename);
- if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
- dkbasename++;
- xxboot = np;
- (void)sprintf(xxboot, "%s/%sboot",
- _PATH_BOOTDIR, dkbasename);
- np += strlen(xxboot) + 1;
- }
-#if NUMBOOT > 1
- if (!bootxx) {
- (void)sprintf(np, "%s/boot%s",
- _PATH_BOOTDIR, dkbasename);
- if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
- dkbasename++;
- bootxx = np;
- (void)sprintf(bootxx, "%s/boot%s",
- _PATH_BOOTDIR, dkbasename);
- np += strlen(bootxx) + 1;
- }
-#endif /* NUMBOOT > 1 */
- }
-
-#ifdef DEBUG
- if (debug)
- warnx("bootstraps: xxboot = %s, bootxx = %s", xxboot,
- bootxx ? bootxx : "NONE");
-#endif
-
- /*
- * Strange rules:
- * 1. One-piece bootstrap (hp300/hp800)
- * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
- * is remembered and written later following the bootarea.
- * 2. Two-piece bootstraps (vax/i386?/mips?)
- * up to d_secsize bytes of ``xxboot'' go in first d_secsize
- * bytes of bootarea, remaining d_bbsize-d_secsize filled
- * from ``bootxx''.
- */
- b = open(xxboot, O_RDONLY);
- if (b < 0)
- err(4, "%s", xxboot);
-#if NUMBOOT > 1
- if (read(b, boot, (int)dp->d_secsize) < 0)
- err(4, "%s", xxboot);
- (void)close(b);
- b = open(bootxx, O_RDONLY);
- if (b < 0)
- err(4, "%s", bootxx);
- if (read(b, &boot[dp->d_secsize],
- (int)(dp->d_bbsize-dp->d_secsize)) < 0)
- err(4, "%s", bootxx);
-#else /* NUMBOOT <= 1 */
- if (read(b, boot, (int)dp->d_bbsize) < 0)
- err(4, "%s", xxboot);
- (void)fstat(b, &sb);
- bootsize = (int)sb.st_size - dp->d_bbsize;
- if (bootsize > 0) {
- /* XXX assume d_secsize is a power of two */
- bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
- bootbuf = (char *)malloc((size_t)bootsize);
- if (bootbuf == 0)
- err(4, "%s", xxboot);
- if (read(b, bootbuf, bootsize) < 0) {
- free(bootbuf);
- err(4, "%s", xxboot);
- }
- }
-#endif /* NUMBOOT <= 1 */
- (void)close(b);
-#endif /* NUMBOOT > 0 */
-
- /*
- * Make sure no part of the bootstrap is written in the area
- * reserved for the label.
- */
- for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
- if (*p)
- errx(2, "Bootstrap doesn't leave room for disk label");
- return (lp);
-}
-
-static void
-makedisktab(FILE *f, struct disklabel *lp)
-{
- int i;
- const char *did;
- struct partition *pp;
-
- did = "\\\n\t:";
- (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=",
- (int) sizeof(lp->d_typename), lp->d_typename);
- if ((unsigned) lp->d_type < DKMAXTYPES)
- (void) fprintf(f, "%s:", dktypenames[lp->d_type]);
- else
- (void) fprintf(f, "unknown%d:", lp->d_type);
-
- (void) fprintf(f, "se#%d:", lp->d_secsize);
- (void) fprintf(f, "ns#%d:", lp->d_nsectors);
- (void) fprintf(f, "nt#%d:", lp->d_ntracks);
- (void) fprintf(f, "sc#%d:", lp->d_secpercyl);
- (void) fprintf(f, "nc#%d:", lp->d_ncylinders);
-
- if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) {
- (void) fprintf(f, "%ssu#%d:", did, lp->d_secperunit);
- did = "";
- }
- if (lp->d_rpm != 3600) {
- (void) fprintf(f, "%srm#%d:", did, lp->d_rpm);
- did = "";
- }
- if (lp->d_interleave != 1) {
- (void) fprintf(f, "%sil#%d:", did, lp->d_interleave);
- did = "";
- }
- if (lp->d_trackskew != 0) {
- (void) fprintf(f, "%ssk#%d:", did, lp->d_trackskew);
- did = "";
- }
- if (lp->d_cylskew != 0) {
- (void) fprintf(f, "%scs#%d:", did, lp->d_cylskew);
- did = "";
- }
- if (lp->d_headswitch != 0) {
- (void) fprintf(f, "%shs#%d:", did, lp->d_headswitch);
- did = "";
- }
- if (lp->d_trkseek != 0) {
- (void) fprintf(f, "%sts#%d:", did, lp->d_trkseek);
- did = "";
- }
-#ifdef notyet
- (void) fprintf(f, "drivedata: ");
- for (i = NDDATA - 1; i >= 0; i--)
- if (lp->d_drivedata[i])
- break;
- if (i < 0)
- i = 0;
- for (j = 0; j <= i; j++)
- (void) fprintf(f, "%d ", lp->d_drivedata[j]);
-#endif /* notyet */
- pp = lp->d_partitions;
- for (i = 0; i < lp->d_npartitions; i++, pp++) {
- if (pp->p_size) {
- char c = 'a' + i;
- (void) fprintf(f, "\\\n\t:");
- (void) fprintf(f, "p%c#%d:", c, pp->p_size);
- (void) fprintf(f, "o%c#%d:", c, pp->p_offset);
- if (pp->p_fstype != FS_UNUSED) {
- if ((unsigned) pp->p_fstype < FSMAXTYPES)
- (void) fprintf(f, "t%c=%s:", c,
- fstypenames[pp->p_fstype]);
- else
- (void) fprintf(f, "t%c=unknown%d:",
- c, pp->p_fstype);
- }
- switch (pp->p_fstype) {
-
- case FS_UNUSED:
- break;
-
- case FS_BSDFFS:
- case FS_BSDLFS:
- case FS_EX2FS:
- case FS_ADOS:
- case FS_APPLEUFS:
- (void) fprintf(f, "b%c#%d:", c,
- pp->p_fsize * pp->p_frag);
- (void) fprintf(f, "f%c#%d:", c, pp->p_fsize);
- break;
- default:
- break;
- }
- }
- }
- (void) fprintf(f, "\n");
- (void) fflush(f);
-}
-
-static int
-edit(struct disklabel *lp, int f)
-{
- struct disklabel label;
- const char *tmpdir;
- int first, ch, fd;
- FILE *fp;
-
- if ((tmpdir = getenv("TMPDIR")) == NULL)
- tmpdir = _PATH_TMP;
- (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE);
- if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) {
- warn("%s", tmpfil);
- return (1);
- }
- (void)fchmod(fd, 0600);
- showinfo(fp, lp, specname);
- showpartitions(fp, lp, Cflag);
- (void) fclose(fp);
- for (;;) {
- if (!editit())
- break;
- fp = fopen(tmpfil, "r");
- if (fp == NULL) {
- warn("%s", tmpfil);
- break;
- }
- (void) memset(&label, 0, sizeof(label));
- if (getasciilabel(fp, &label)) {
- *lp = label;
- if (writelabel(f, bootarea, lp) == 0) {
- (void) unlink(tmpfil);
- return (0);
- }
- }
- (void) printf("re-edit the label? [y]: ");
- (void) fflush(stdout);
- first = ch = getchar();
- while (ch != '\n' && ch != EOF)
- ch = getchar();
- if (first == 'n' || first == 'N')
- break;
- }
- (void)unlink(tmpfil);
- return (1);
-}
-
-static int
-editit(void)
-{
- int pid, xpid;
- int status;
- sigset_t nsigset, osigset;
-
- sigemptyset(&nsigset);
- sigaddset(&nsigset, SIGINT);
- sigaddset(&nsigset, SIGQUIT);
- sigaddset(&nsigset, SIGHUP);
- sigprocmask(SIG_BLOCK, &nsigset, &osigset);
- while ((pid = fork()) < 0) {
- if (errno != EAGAIN) {
- sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
- warn("fork");
- return (0);
- }
- sleep(1);
- }
- if (pid == 0) {
- const char *ed;
- char *buf;
- int retval;
-
- sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
- setgid(getgid());
- setuid(getuid());
- if ((ed = getenv("EDITOR")) == (char *)0)
- ed = DEFEDITOR;
- /*
- * Jump through a few extra hoops in case someone's editor
- * is "editor arg1 arg2".
- */
- asprintf(&buf, "%s %s", ed, tmpfil);
- if (!buf)
- err(1, "malloc");
- retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL);
- if (retval == -1)
- perror(ed);
- exit(retval);
- }
- while ((xpid = wait(&status)) >= 0)
- if (xpid == pid)
- break;
- sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
- return (!status);
-}
-
-static char *
-skip(char *cp)
-{
-
- cp += strspn(cp, " \t");
- if (*cp == '\0')
- return (NULL);
- return (cp);
-}
-
-static char *
-word(char *cp)
-{
-
- if (cp == NULL || *cp == '\0')
- return (NULL);
-
- cp += strcspn(cp, " \t");
- if (*cp == '\0')
- return (NULL);
- *cp++ = '\0';
- cp += strspn(cp, " \t");
- if (*cp == '\0')
- return (NULL);
- return (cp);
-}
-
-#define _CHECKLINE \
- if (tp == NULL || *tp == '\0') { \
- warnx("line %d: too few fields", lineno); \
- errors++; \
- break; \
- }
-
-#define __CHECKLINE \
- if (*tp == NULL || **tp == '\0') { \
- warnx("line %d: too few fields", lineno); \
- *tp = _error_; \
- return 0; \
- }
-
-static char _error_[] = "";
-#define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \
- ; else goto error
-#define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \
- ; else goto error
-
-static unsigned long
-nxtnum(char **tp, int lineno)
-{
- char *cp;
- unsigned long v;
-
- __CHECKLINE
- if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) {
- warnx("line %d: syntax error", lineno);
- *tp = _error_;
- return 0;
- }
- *tp = cp;
- return v;
-}
-
-static unsigned long
-nxtxnum(char **tp, struct disklabel *lp, int lineno)
-{
- char *cp, *ncp;
- unsigned long n, v;
-
- __CHECKLINE
- cp = *tp;
- if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0)
- goto bad;
-
- if (*ncp == '/') {
- n *= lp->d_secpercyl;
- cp = ncp + 1;
- if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0)
- goto bad;
- n += v * lp->d_nsectors;
- cp = ncp + 1;
- if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0)
- goto bad;
- n += v;
- }
- *tp = ncp;
- return n;
-bad:
- warnx("line %d: invalid format", lineno);
- *tp = _error_;
- return 0;
-}
-
-/*
- * Read an ascii label in from fd f,
- * in the same format as that put out by showinfo() and showpartitions(),
- * and fill in lp.
- */
-static int
-getasciilabel(FILE *f, struct disklabel *lp)
-{
- const char *const *cpp, *s;
- struct partition *pp;
- char *cp, *tp, line[BUFSIZ], tbuf[15];
- int lineno, errors;
- unsigned long v;
- unsigned int part;
-
- lineno = 0;
- errors = 0;
- lp->d_bbsize = BBSIZE; /* XXX */
- lp->d_sbsize = SBLOCKSIZE; /* XXX */
- while (fgets(line, sizeof(line) - 1, f)) {
- lineno++;
- if ((cp = strpbrk(line, "#\r\n")) != NULL)
- *cp = '\0';
- cp = skip(line);
- if (cp == NULL) /* blank line or comment line */
- continue;
- tp = strchr(cp, ':'); /* everything has a colon in it */
- if (tp == NULL) {
- warnx("line %d: syntax error", lineno);
- errors++;
- continue;
- }
- *tp++ = '\0', tp = skip(tp);
- if (!strcmp(cp, "type")) {
- if (tp == NULL) {
- strlcpy(tbuf, "unknown", sizeof(tbuf));
- tp = tbuf;
- }
- cpp = dktypenames;
- for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
- if ((s = *cpp) && !strcasecmp(s, tp)) {
- lp->d_type = cpp - dktypenames;
- goto next;
- }
- if (GETNUM16(tp, &v) != 0) {
- warnx("line %d: syntax error", lineno);
- errors++;
- continue;
- }
- if (v >= DKMAXTYPES)
- warnx("line %d: warning, unknown disk type: %s",
- lineno, tp);
- lp->d_type = v;
- continue;
- }
- if (!strcmp(cp, "flags")) {
- for (v = 0; (cp = tp) && *cp != '\0';) {
- tp = word(cp);
- if (!strcasecmp(cp, "removable"))
- v |= D_REMOVABLE;
- else if (!strcasecmp(cp, "ecc"))
- v |= D_ECC;
- else if (!strcasecmp(cp, "badsect"))
- v |= D_BADSECT;
- else {
- warnx("line %d: bad flag: %s",
- lineno, cp);
- errors++;
- }
- }
- lp->d_flags = v;
- continue;
- }
- if (!strcmp(cp, "drivedata")) {
- int i;
-
- for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
- if (GETNUM32(cp, &v) != 0) {
- warnx("line %d: bad drive data",
- lineno);
- errors++;
- } else
- lp->d_drivedata[i] = v;
- i++;
- tp = word(cp);
- }
- continue;
- }
- if (sscanf(cp, "%lu partitions", &v) == 1) {
- if (v == 0 || v > MAXPARTITIONS) {
- warnx("line %d: bad # of partitions", lineno);
- lp->d_npartitions = MAXPARTITIONS;
- errors++;
- } else
- lp->d_npartitions = v;
- continue;
- }
- if (tp == NULL) {
- tbuf[0] = '\0';
- tp = tbuf;
- }
- if (!strcmp(cp, "disk")) {
- strncpy(lp->d_typename, tp, sizeof(lp->d_typename));
- continue;
- }
- if (!strcmp(cp, "label")) {
- strncpy(lp->d_packname, tp, sizeof(lp->d_packname));
- continue;
- }
- if (!strcmp(cp, "bytes/sector")) {
- if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_secsize = v;
- continue;
- }
- if (!strcmp(cp, "sectors/track")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_nsectors = v;
- continue;
- }
- if (!strcmp(cp, "sectors/cylinder")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_secpercyl = v;
- continue;
- }
- if (!strcmp(cp, "tracks/cylinder")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_ntracks = v;
- continue;
- }
- if (!strcmp(cp, "cylinders")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_ncylinders = v;
- continue;
- }
- if (!strcmp(cp, "total sectors")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_secperunit = v;
- continue;
- }
- if (!strcmp(cp, "rpm")) {
- if (GETNUM16(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_rpm = v;
- continue;
- }
- if (!strcmp(cp, "interleave")) {
- if (GETNUM16(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_interleave = v;
- continue;
- }
- if (!strcmp(cp, "trackskew")) {
- if (GETNUM16(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_trackskew = v;
- continue;
- }
- if (!strcmp(cp, "cylinderskew")) {
- if (GETNUM16(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_cylskew = v;
- continue;
- }
- if (!strcmp(cp, "headswitch")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_headswitch = v;
- continue;
- }
- if (!strcmp(cp, "track-to-track seek")) {
- if (GETNUM32(tp, &v) != 0) {
- warnx("line %d: bad %s: %s", lineno, cp, tp);
- errors++;
- } else
- lp->d_trkseek = v;
- continue;
- }
- if ('a' > *cp || *cp > 'z' || cp[1] != '\0') {
- warnx("line %d: unknown field: %s", lineno, cp);
- errors++;
- continue;
- }
-
- /* We have a partition entry */
- part = *cp - 'a';
-
- if (part >= MAXPARTITIONS) {
- warnx("line %d: bad partition name: %s", lineno, cp);
- errors++;
- continue;
- }
- pp = &lp->d_partitions[part];
-
- NXTXNUM(pp->p_size);
- NXTXNUM(pp->p_offset);
- /* can't use word() here because of blanks in fstypenames[] */
- tp += strspn(tp, " \t");
- _CHECKLINE
- cp = tp;
- cpp = fstypenames;
- for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
- s = *cpp;
- if (s == NULL ||
- (cp[strlen(s)] != ' ' &&
- cp[strlen(s)] != '\t' &&
- cp[strlen(s)] != '\0'))
- continue;
- if (!memcmp(s, cp, strlen(s))) {
- pp->p_fstype = cpp - fstypenames;
- tp += strlen(s);
- if (*tp == '\0')
- tp = NULL;
- else {
- tp += strspn(tp, " \t");
- if (*tp == '\0')
- tp = NULL;
- }
- goto gottype;
- }
- }
- tp = word(cp);
- if (isdigit(*cp & 0xff)) {
- if (GETNUM8(cp, &v) != 0) {
- warnx("line %d: syntax error", lineno);
- errors++;
- }
- } else
- v = FSMAXTYPES;
- if ((unsigned)v >= FSMAXTYPES) {
- warnx("line %d: warning, unknown filesystem type: %s",
- lineno, cp);
- v = FS_UNUSED;
- }
- pp->p_fstype = v;
-gottype:
- switch (pp->p_fstype) {
-
- case FS_UNUSED: /* XXX */
- NXTNUM(pp->p_fsize);
- if (pp->p_fsize == 0)
- break;
- NXTNUM(v);
- pp->p_frag = v / pp->p_fsize;
- break;
-
- case FS_BSDFFS:
- case FS_ADOS:
- case FS_APPLEUFS:
- NXTNUM(pp->p_fsize);
- if (pp->p_fsize == 0)
- break;
- NXTNUM(v);
- pp->p_frag = v / pp->p_fsize;
- NXTNUM(pp->p_cpg);
- break;
- case FS_BSDLFS:
- NXTNUM(pp->p_fsize);
- if (pp->p_fsize == 0)
- break;
- NXTNUM(v);
- pp->p_frag = v / pp->p_fsize;
- NXTNUM(pp->p_sgs);
- break;
- case FS_EX2FS:
- NXTNUM(pp->p_fsize);
- if (pp->p_fsize == 0)
- break;
- NXTNUM(v);
- pp->p_frag = v / pp->p_fsize;
- break;
- case FS_ISO9660:
- NXTNUM(pp->p_cdsession);
- break;
- default:
- break;
- }
- continue;
- error:
- errors++;
- next:
- ;
- }
- errors += checklabel(lp);
- return (errors == 0);
-}
-
-/*
- * Check disklabel for errors and fill in
- * derived fields according to supplied values.
- */
-int
-checklabel(struct disklabel *lp)
-{
- struct partition *pp, *qp;
- int i, j, errors;
- char part;
-
- errors = 0;
- if (lp->d_secsize == 0) {
- warnx("sector size %d", lp->d_secsize);
- return (1);
- }
- if (lp->d_nsectors == 0) {
- warnx("sectors/track %d", lp->d_nsectors);
- return (1);
- }
- if (lp->d_ntracks == 0) {
- warnx("tracks/cylinder %d", lp->d_ntracks);
- return (1);
- }
- if (lp->d_ncylinders == 0) {
- warnx("cylinders/unit %d", lp->d_ncylinders);
- errors++;
- }
- if (lp->d_rpm == 0)
- warnx("warning, revolutions/minute %d", lp->d_rpm);
- if (lp->d_secpercyl == 0)
- lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
- if (lp->d_secperunit == 0)
- lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
-#ifdef __i386__notyet__
- if (dosdp && lp->d_secperunit > dosdp->mbrp_start + dosdp->mbrp_size) {
- warnx("exceeds DOS partition size");
- errors++;
- lp->d_secperunit = dosdp->mbrp_start + dosdp->mbrp_size;
- }
- /* XXX should also check geometry against BIOS's idea */
-#endif /* __i386__notyet__ */
-#ifdef __arm32__notyet__
- /* XXX similar code as for i386 */
-#endif /* __arm32__notyet__ */
- if (lp->d_bbsize == 0) {
- warnx("boot block size %d", lp->d_bbsize);
- errors++;
- } else if (lp->d_bbsize % lp->d_secsize)
- warnx("warning, boot block size %% sector-size != 0");
- if (lp->d_sbsize == 0) {
- warnx("super block size %d", lp->d_sbsize);
- errors++;
- } else if (lp->d_sbsize % lp->d_secsize)
- warnx("warning, super block size %% sector-size != 0");
- if (lp->d_npartitions > MAXPARTITIONS)
- warnx("warning, number of partitions (%d) > MAXPARTITIONS (%d)",
- lp->d_npartitions, MAXPARTITIONS);
- else
- for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) {
- part = 'a' + i;
- pp = &lp->d_partitions[i];
- if (pp->p_size || pp->p_offset) {
- warnx("warning, partition %c increased "
- "number of partitions from %d to %d",
- part, lp->d_npartitions, i + 1);
- lp->d_npartitions = i + 1;
- break;
- }
- }
- for (i = 0; i < lp->d_npartitions; i++) {
- part = 'a' + i;
- pp = &lp->d_partitions[i];
- if (pp->p_size == 0 && pp->p_offset != 0)
- warnx("warning, partition %c: size 0, but offset %d",
- part, pp->p_offset);
-#ifdef STRICT_CYLINDER_ALIGNMENT
- if (pp->p_offset % lp->d_secpercyl) {
- warnx("warning, partition %c:"
- " not starting on cylinder boundary",
- part);
- errors++;
- }
-#endif /* STRICT_CYLINDER_ALIGNMENT */
- if (pp->p_offset > lp->d_secperunit) {
- warnx("partition %c: offset past end of unit", part);
- errors++;
- }
- if (pp->p_offset + pp->p_size > lp->d_secperunit) {
- warnx("partition %c: partition extends"
- " past end of unit",
- part);
- errors++;
- }
- if (pp->p_fstype != FS_UNUSED)
- for (j = i + 1; j < lp->d_npartitions; j++) {
- qp = &lp->d_partitions[j];
- if (qp->p_fstype == FS_UNUSED)
- continue;
- if (pp->p_offset < qp->p_offset + qp->p_size &&
- qp->p_offset < pp->p_offset + pp->p_size)
- warnx("partitions %c and %c overlap",
- part, 'a' + j);
- }
- }
- return (errors);
-}
-
-#if NUMBOOT > 0
-/*
- * If we are installing a boot program that doesn't fit in d_bbsize
- * we need to mark those partitions that the boot overflows into.
- * This allows newfs to prevent creation of a filesystem where it might
- * clobber bootstrap code.
- */
-static void
-setbootflag(struct disklabel *lp)
-{
- struct partition *pp;
- int i, errors;
- char part;
- u_long boffset;
-
- errors = 0;
- if (bootbuf == 0)
- return;
- boffset = bootsize / lp->d_secsize;
- for (i = 0; i < lp->d_npartitions; i++) {
- part = 'a' + i;
- pp = &lp->d_partitions[i];
- if (pp->p_size == 0)
- continue;
- if (boffset <= pp->p_offset) {
- if (pp->p_fstype == FS_BOOT)
- pp->p_fstype = FS_UNUSED;
- } else if (pp->p_fstype != FS_BOOT) {
- if (pp->p_fstype != FS_UNUSED) {
- warnx("boot overlaps used partition %c",
- part);
- errors++;
- } else {
- pp->p_fstype = FS_BOOT;
- warnx("warning, boot overlaps partition %c, %s",
- part, "marked as FS_BOOT");
- }
- }
- }
- if (errors)
- errx(4, "cannot install boot program");
-}
-#endif /* NUMBOOT > 0 */
-
-static void
-usage(void)
-{
- static const struct {
- const char *name;
- const char *expn;
- } usages[] = {
- { "[-rt] [-C] [-F] disk",
- "(to read label)" },
- { "-w [-r] [-F] [-f disktab] disk type [ packid ]",
-#if NUMBOOT > 0
- "(to write label with existing boot program)"
-#else
- "(to write label)"
-#endif
- },
- { "-e [-r] [-I] [-C] [-F] disk",
- "(to edit label)" },
- { "-i [-I] [-r] [-F] disk",
- "(to create a label interactively)" },
- { "-R [-r] [-F] disk protofile",
-#if NUMBOOT > 0
- "(to restore label with existing boot program)"
-#else
- "(to restore label)"
-#endif
- },
-#if NUMBOOT > 0
-# if NUMBOOT > 1
- { "-B [-f disktab] [ -b xxboot [ -s bootxx ] ] disk [ type ]",
- "(to install boot program with existing label)" },
- { "-w -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk type [ packid ]",
- "(to write label and boot program)" },
- { "-R -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk protofile [ type ]",
- "(to restore label and boot program)" },
-# else
- { "-B [-F] [-f disktab] [ -b bootprog ] disk [ type ]",
- "(to install boot program with existing on-disk label)" },
- { "-w -B [-F] [-f disktab] [ -b bootprog ] disk type [ packid ]",
- "(to write label and install boot program)" },
- { "-R -B [-F] [-f disktab] [ -b bootprog ] disk protofile [ type ]",
- "(to restore label and install boot program)" },
-# endif
-#endif
- { "[-NW] disk",
- "(to write disable/enable label)" },
- { NULL,
- NULL }
- };
- int i;
-
- for (i = 0; usages[i].name; i++) {
- (void) fputs(i ? "or " : "usage: ", stderr);
- (void) fprintf(stderr, "%s %s", getprogname(), usages[i].name);
- (void) fputs("\n\t", stderr);
- (void) fprintf(stderr, "%s %s", getprogname(), usages[i].expn);
- (void) fputs("\n", stderr);
- }
- exit(1);
-}
-
-static int
-getulong(const char *str, char sep, char **epp, unsigned long *ul,
- unsigned long max)
-{
- char *ep;
-
- if (epp == NULL)
- epp = &ep;
-
- *ul = strtoul(str, epp, 10);
-
- if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max)
- return ERANGE;
-
- if (*str == '\0' || (**epp != '\0' && **epp != sep &&
- !isspace((unsigned char)**epp)))
- return EFTYPE;
-
- return 0;
-}
Index: sbin/disklabel/dkcksum.c
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/dkcksum.c,v
retrieving revision 1.10
diff -u -r1.10 dkcksum.c
--- sbin/disklabel/dkcksum.c 7 Aug 2003 10:04:13 -0000 1.10
+++ sbin/disklabel/dkcksum.c 22 Apr 2005 05:00:15 -0000
@@ -29,6 +29,10 @@
* SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
#ifndef lint
#if 0
@@ -39,7 +43,11 @@
#endif /* not lint */
#include <sys/types.h>
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../sys/sys/disklabel.h"
+#else
#include <sys/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#include "dkcksum.h"
u_short
Index: sbin/disklabel/interact.c
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/interact.c,v
retrieving revision 1.23
diff -u -r1.23 interact.c
--- sbin/disklabel/interact.c 29 Dec 2003 21:21:25 -0000 1.23
+++ sbin/disklabel/interact.c 22 Apr 2005 05:00:15 -0000
@@ -29,6 +29,10 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: interact.c,v 1.23 2003/12/29 21:21:25 jdc Exp $");
@@ -37,13 +41,19 @@
#include <sys/param.h>
#define FSTYPENAMES
#define DKTYPENAMES
-#include <sys/disklabel.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+
+#if HAVE_NBTOOL_CONFIG_H
+#define getmaxpartitions() MAXPARTITIONS
+#include "../../sys/sys/disklabel.h"
+#else
#include <util.h>
+#include <sys/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#include "extern.h"
Index: sbin/disklabel/main.c
===================================================================
RCS file: sbin/disklabel/main.c
diff -N sbin/disklabel/main.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sbin/disklabel/main.c 22 Apr 2005 05:00:16 -0000
@@ -0,0 +1,1993 @@
+/* $NetBSD: disklabel.c,v 1.135 2004/11/12 01:00:40 hubertf Exp $ */
+
+/*
+ * Copyright (c) 1987, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Symmetric Computer Systems.
+ *
+ * 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.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/cdefs.h>
+#ifndef lint
+__COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+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: disklabel.c,v 1.135 2004/11/12 01:00:40 hubertf Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#define DKTYPENAMES
+#define FSTYPENAMES
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+#include <signal.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <ufs/ufs/dinode.h>
+#include <ufs/ffs/fs.h>
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../sys/sys/disklabel.h"
+#include "../../sys/sys/bootblock.h"
+#include "../../include/util.h"
+#include "../../include/disktab.h"
+#else
+#include <sys/ioctl.h>
+#include <sys/disklabel.h>
+#include <sys/bootblock.h>
+#include <util.h>
+#include <disktab.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
+#include "pathnames.h"
+#include "extern.h"
+#include "dkcksum.h"
+
+/*
+ * Disklabel: read and write disklabels.
+ * The label is usually placed on one of the first sectors of the disk.
+ * Many machines also place a bootstrap in the same area,
+ * in which case the label is embedded in the bootstrap.
+ * The bootstrap source must leave space at the proper offset
+ * for the label on such machines.
+ */
+
+#ifndef BBSIZE
+#define BBSIZE 8192 /* size of boot area, with label */
+#endif
+
+#ifndef NUMBOOT
+#define NUMBOOT 0
+#endif
+
+#define DEFEDITOR _PATH_VI
+
+static char *dkname;
+static char tmpfil[MAXPATHLEN];
+
+static char namebuf[BBSIZE], *np = namebuf;
+static struct disklabel lab;
+
+char bootarea[BBSIZE];
+char *specname;
+
+
+#if NUMBOOT > 0
+static int installboot; /* non-zero if we should install a boot program */
+static char *bootbuf; /* pointer to buffer with remainder of boot prog */
+static int bootsize; /* size of remaining boot program */
+static char *xxboot; /* primary boot */
+static char *bootxx; /* secondary boot */
+static char boot0[MAXPATHLEN];
+#if NUMBOOT > 1
+static char boot1[MAXPATHLEN];
+#endif /* NUMBOOT > 1 */
+#endif /* NUMBOOT > 0 */
+
+static enum {
+ UNSPEC, EDIT, READ, RESTORE, SETWRITABLE, WRITE, WRITEBOOT, INTERACT
+} op = UNSPEC;
+
+static int Fflag;
+static int rflag;
+static int tflag;
+ int Cflag;
+static int Iflag;
+
+#define COMMON_OPTIONS "BCFINRWb:ef:irs:tw"
+
+#ifdef DEBUG
+static int debug;
+#define OPTIONS COMMON_OPTIONS "d"
+#else /* ! DEBUG */
+#define OPTIONS COMMON_OPTIONS
+#endif /* ! DEBUG */
+
+#ifdef USE_MBR
+static struct mbr_partition *dosdp; /* i386 DOS partition, if found */
+static struct mbr_partition *readmbr(int);
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+static u_int filecore_partition_offset;
+static u_int get_filecore_partition(int);
+static int filecore_checksum(u_char *);
+#endif /* USE_ACORN */
+
+#if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
+static void confirm(const char *);
+#endif
+
+int main(int, char *[]);
+
+static void makedisktab(FILE *, struct disklabel *);
+static void makelabel(const char *, const char *,
+ struct disklabel *);
+static void l_perror(const char *);
+static struct disklabel *readlabel(int);
+static struct disklabel *makebootarea(char *, struct disklabel *, int);
+static int edit(struct disklabel *, int);
+static int editit(void);
+static char *skip(char *);
+static char *word(char *);
+static int getasciilabel(FILE *, struct disklabel *);
+#if NUMBOOT > 0
+static void setbootflag(struct disklabel *);
+#endif
+static void usage(void);
+static int getulong(const char *, char, char **,
+ unsigned long *, unsigned long);
+#define GETNUM32(a, v) getulong(a, '\0', NULL, v, UINT32_MAX)
+#define GETNUM16(a, v) getulong(a, '\0', NULL, v, UINT16_MAX)
+#define GETNUM8(a, v) getulong(a, '\0', NULL, v, UINT8_MAX)
+
+#if HAVE_NBTOOL_CONFIG_H
+#define GETLABELOFFSET() LABELOFFSET
+#define GETLABELSECTOR() LABELSECTOR
+#else /* HAVE_NBTOOL_CONFIG_H */
+#define GETLABELOFFSET() getlabeloffset()
+#define GETLABELSECTOR() getlabelsector()
+#endif
+
+int
+main(int argc, char *argv[])
+{
+ struct disklabel *lp;
+ FILE *t;
+ int ch, f, writable, error;
+
+ error = 0;
+ while ((ch = getopt(argc, argv, OPTIONS)) != -1)
+ switch (ch) {
+#if NUMBOOT > 0
+ case 'B':
+ ++installboot;
+ break;
+ case 'b':
+ xxboot = optarg;
+ break;
+#if NUMBOOT > 1
+ case 's':
+ bootxx = optarg;
+ break;
+#endif /* NUMBOOT > 1 */
+#endif /* NUMBOOT > 0 */
+ case 'C':
+ ++Cflag;
+ break;
+ case 'F':
+ ++Fflag;
+ break;
+ case 'I':
+ ++Iflag;
+ break;
+ case 'R':
+ if (op != UNSPEC)
+ usage();
+ op = RESTORE;
+ break;
+#if !HAVE_NBTOOL_CONFIG_H
+ case 'N':
+ if (op != UNSPEC)
+ usage();
+ writable = 0;
+ op = SETWRITABLE;
+ break;
+ case 'W':
+ if (op != UNSPEC)
+ usage();
+ writable = 1;
+ op = SETWRITABLE;
+ break;
+#endif /* !HAVE_NBTOOL_CONFIG_H */
+ case 'e':
+ if (op != UNSPEC)
+ usage();
+ op = EDIT;
+ break;
+ case 'f':
+ if (setdisktab(optarg) == -1)
+ usage();
+ break;
+ case 'i':
+ if (op != UNSPEC)
+ usage();
+ op = INTERACT;
+ break;
+ case 't':
+ ++tflag;
+ break;
+ case 'r':
+ ++rflag;
+ break;
+ case 'w':
+ if (op != UNSPEC)
+ usage();
+ op = WRITE;
+ break;
+#ifdef DEBUG
+ case 'd':
+ debug++;
+ break;
+#endif
+ case '?':
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+
+#if NUMBOOT > 0
+ if (installboot) {
+ rflag++;
+ if (op == UNSPEC)
+ op = WRITEBOOT;
+ } else {
+ if (op == UNSPEC)
+ op = READ;
+ }
+#else /* NUMBOOT <= 0 */
+ if (op == UNSPEC)
+ op = READ;
+#endif /* NUMBOOT <= 0 */
+
+ if (argc < 1)
+ usage();
+
+ if (Iflag && op != EDIT && op != INTERACT)
+ usage();
+
+ dkname = argv[0];
+#if HAVE_NBTOOL_CONFIG_H
+ f = open(dkname, op == READ ? O_RDONLY : O_RDWR, 0);
+ strlcpy(np, dkname, MAXPATHLEN);
+#else
+ f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, np, MAXPATHLEN, 0);
+#endif /* HAVE_NBTOOL_CONFIG_H */
+ specname = np;
+ np += strlen(specname) + 1;
+ if (f < 0)
+ err(4, "%s", specname);
+
+#ifdef USE_MBR
+ /*
+ * Check for presence of DOS partition table in
+ * master boot record. Return pointer to NetBSD/i386
+ * partition, if present.
+ */
+ dosdp = readmbr(f);
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+ /*
+ * Check for the presence of a RiscOS filecore boot block
+ * indicating an ADFS file system on the disc.
+ * Return the offset to the NetBSD part of the disc if
+ * this can be determined.
+ * This routine will terminate disklabel if the disc
+ * is found to be ADFS only.
+ */
+ filecore_partition_offset = get_filecore_partition(f);
+#endif /* USE_ACORN */
+
+ switch (op) {
+
+ case EDIT:
+ if (argc != 1)
+ usage();
+ lp = readlabel(f);
+ error = edit(lp, f);
+ break;
+
+ case INTERACT:
+ if (argc != 1)
+ usage();
+ lp = readlabel(f);
+ /*
+ * XXX: Fill some default values so checklabel does not fail
+ */
+ if (lp->d_bbsize == 0)
+ lp->d_bbsize = BBSIZE;
+ if (lp->d_sbsize == 0)
+ lp->d_sbsize = SBLOCKSIZE;
+ interact(lp, f);
+ break;
+
+ case READ:
+ if (argc != 1)
+ usage();
+ lp = readlabel(f);
+ if (tflag)
+ makedisktab(stdout, lp);
+ else {
+ showinfo(stdout, lp, specname);
+ showpartitions(stdout, lp, Cflag);
+ }
+ error = checklabel(lp);
+ if (error)
+ error += 100;
+ break;
+
+ case RESTORE:
+ if (argc < 2 || argc > 3)
+ usage();
+#if NUMBOOT > 0
+ if (installboot && argc == 3)
+ makelabel(argv[2], (char *)0, &lab);
+#endif
+ lp = makebootarea(bootarea, &lab, f);
+ if (!(t = fopen(argv[1], "r")))
+ err(4, "%s", argv[1]);
+ if (getasciilabel(t, lp))
+ error = writelabel(f, bootarea, lp);
+ else
+ error = 1;
+ break;
+
+#if !HAVE_NBTOOL_CONFIG_H
+ case SETWRITABLE:
+ if (ioctl(f, DIOCWLABEL, (char *)&writable) < 0)
+ err(4, "ioctl DIOCWLABEL");
+ break;
+#endif /* !HAVE_NBTOOL_CONFIG_H */
+
+ case WRITE:
+ if (argc < 2 || argc > 3)
+ usage();
+ makelabel(argv[1], argc == 3 ? argv[2] : (char *)0, &lab);
+ lp = makebootarea(bootarea, &lab, f);
+ *lp = lab;
+ if (checklabel(lp) == 0)
+ error = writelabel(f, bootarea, lp);
+ else
+ error = 1;
+ break;
+
+ case WRITEBOOT:
+#if NUMBOOT > 0
+ {
+ struct disklabel tlab;
+
+ lp = readlabel(f);
+ tlab = *lp;
+ if (argc == 2)
+ makelabel(argv[1], (char *)0, &lab);
+ lp = makebootarea(bootarea, &lab, f);
+ *lp = tlab;
+ if (checklabel(lp) == 0)
+ error = writelabel(f, bootarea, lp);
+ else
+ error = 1;
+ break;
+ }
+#endif /* NUMBOOT > 0 */
+
+ case UNSPEC:
+ usage();
+
+ }
+ exit(error);
+}
+
+/*
+ * Construct a prototype disklabel from /etc/disktab. As a side
+ * effect, set the names of the primary and secondary boot files
+ * if specified.
+ */
+static void
+makelabel(const char *type, const char *name, struct disklabel *lp)
+{
+ struct disklabel *dp;
+
+ dp = getdiskbyname(type);
+ if (dp == NULL)
+ errx(1, "unknown disk type: %s", type);
+ *lp = *dp;
+
+#if NUMBOOT > 0
+ /*
+ * Set bootstrap name(s).
+ * 1. If set from command line, use those,
+ * 2. otherwise, check if disktab specifies them (b0 or b1),
+ * 3. otherwise, makebootarea() will choose ones based on the name
+ * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
+ */
+ if (!xxboot && lp->d_boot0) {
+ if (*lp->d_boot0 != '/')
+ (void)snprintf(boot0, sizeof(boot0), "%s/%s",
+ _PATH_BOOTDIR, lp->d_boot0);
+ else
+ (void)strlcpy(boot0, lp->d_boot0, sizeof(boot0));
+ xxboot = boot0;
+ }
+
+#if NUMBOOT > 1
+ if (!bootxx && lp->d_boot1) {
+ if (*lp->d_boot1 != '/')
+ (void)snprintf(boot1, sizeof(boot1), "%s/%s",
+ _PATH_BOOTDIR, lp->d_boot1);
+ else
+ (void)strlcpy(boot1, lp->d_boot1, sizeof(boot1));
+ bootxx = boot1;
+ }
+#endif /* NUMBOOT > 1 */
+#endif /* NUMBOOT > 0 */
+
+ /* d_packname is union d_boot[01], so zero */
+ (void) memset(lp->d_packname, 0, sizeof(lp->d_packname));
+ if (name)
+ (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
+}
+
+#if defined(USE_MBR) || (defined(USE_ACORN) && defined(notyet))
+static void
+confirm(const char *txt)
+{
+ int first, ch;
+
+ (void) printf("%s? [n]: ", txt);
+ (void) fflush(stdout);
+ first = ch = getchar();
+ while (ch != '\n' && ch != EOF)
+ ch = getchar();
+ if (first != 'y' && first != 'Y')
+ exit(0);
+}
+#endif /* USE_MBR || USE_ACORN && notyet */
+
+int
+writelabel(int f, char *boot, struct disklabel *lp)
+{
+ int writable;
+ off_t sectoffset;
+
+ sectoffset = 0;
+#if NUMBOOT > 0
+ setbootflag(lp);
+#endif
+ lp->d_magic = DISKMAGIC;
+ lp->d_magic2 = DISKMAGIC;
+ lp->d_checksum = 0;
+ lp->d_checksum = dkcksum(lp);
+
+ if (Fflag || rflag || Iflag)
+ {
+#ifdef USE_MBR
+ struct partition *pp = &lp->d_partitions[2];
+
+ /*
+ * If NetBSD/i386 DOS partition is missing, or if
+ * the label to be written is not within partition,
+ * prompt first. Need to allow this in case operator
+ * wants to convert the drive for dedicated use.
+ */
+ if (dosdp) {
+ if (dosdp->mbrp_start != pp->p_offset) {
+ printf("NetBSD slice at %u, "
+ "partition C at %u\n", dosdp->mbrp_start,
+ pp->p_offset);
+ confirm("Write outside MBR partition");
+ }
+ sectoffset = (off_t)pp->p_offset * lp->d_secsize;
+ } else {
+ sectoffset = 0;
+ }
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+ /* XXX */
+ sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
+#endif /* USE_ACORN */
+
+ /*
+ * First set the kernel disk label,
+ * then write a label to the raw disk.
+ * If the SDINFO ioctl fails because it is unimplemented,
+ * keep going; otherwise, the kernel consistency checks
+ * may prevent us from changing the current (in-core)
+ * label.
+ */
+#if !HAVE_NBTOOL_CONFIG_H
+ if (!Fflag && ioctl(f, DIOCSDINFO, lp) < 0 &&
+ errno != ENODEV && errno != ENOTTY) {
+ l_perror("ioctl DIOCSDINFO");
+ return (1);
+ }
+#endif /* HAVE_NBTOOL_CONFIG_H */
+ if (lseek(f, sectoffset, SEEK_SET) < 0) {
+ perror("lseek");
+ return (1);
+ }
+ /*
+ * write enable label sector before write (if necessary),
+ * disable after writing.
+ */
+ writable = 1;
+#if !HAVE_NBTOOL_CONFIG_H
+ if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
+ perror("ioctl DIOCWLABEL");
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
+#ifdef __alpha__
+ /*
+ * The Alpha requires that the boot block be checksummed.
+ * The NetBSD/alpha disklabel.h provides a macro to do it.
+ */
+ {
+ struct alpha_boot_block *bb;
+
+ bb = (struct alpha_boot_block *)boot;
+ ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
+ }
+#endif /* __alpha__ */
+
+ if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
+ perror("write");
+ return (1);
+ }
+
+#if NUMBOOT > 0
+ /*
+ * Output the remainder of the disklabel
+ */
+ if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
+ perror("write");
+ return (1);
+ }
+#endif /* NUMBOOT > 0 */
+
+ writable = 0;
+#if !HAVE_NBTOOL_CONFIG_H
+ if (!Fflag && ioctl(f, DIOCWLABEL, &writable) < 0)
+ perror("ioctl DIOCWLABEL");
+ /*
+ * Now issue a DIOCWDINFO. This will let the kernel convert the
+ * disklabel to some machdep format if needed.
+ */
+ if (!Fflag && ioctl(f, DIOCWDINFO, lp) < 0) {
+ l_perror("ioctl DIOCWDINFO");
+ return (1);
+ }
+#endif /* !HAVE_NBTOOL_CONFIG_H */
+ } else {
+#if !HAVE_NBTOOL_CONFIG_H
+ if (ioctl(f, DIOCWDINFO, lp) < 0) {
+ l_perror("ioctl DIOCWDINFO");
+ return (1);
+ }
+#else
+ errx(1, "use -F, -r, or -I");
+#endif /* HAVE_NBTOOL_CONFIG_H */
+ }
+
+#ifdef __vax__
+ if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
+ daddr_t alt;
+ int i;
+
+ alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
+ for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
+ (void)lseek(f, (off_t)(alt + i) * lp->d_secsize,
+ SEEK_SET);
+ if (write(f, boot, lp->d_secsize) < lp->d_secsize)
+ warn("alternate label %d write", i/2);
+ }
+ }
+#endif /* __vax__ */
+
+ return (0);
+}
+
+static void
+l_perror(const char *s)
+{
+
+ switch (errno) {
+
+ case ESRCH:
+ warnx("%s: No disk label on disk;\n"
+ "use \"disklabel -I\" to install initial label", s);
+ break;
+
+ case EINVAL:
+ warnx("%s: Label magic number or checksum is wrong!\n"
+ "(disklabel or kernel is out of date?)", s);
+ break;
+
+ case EBUSY:
+ warnx("%s: Open partition would move or shrink", s);
+ break;
+
+ case EXDEV:
+ warnx("%s: Labeled partition or 'a' partition must start"
+ " at beginning of disk", s);
+ break;
+
+ default:
+ warn("%s", s);
+ break;
+ }
+}
+
+#ifdef USE_MBR
+/*
+ * Fetch DOS partition table from disk.
+ */
+static struct mbr_partition *
+readmbr(int f)
+{
+ struct mbr_partition *dp;
+ struct mbr_sector mbr;
+ int part;
+ u_int ext_base, next_ext, this_ext;
+ static struct mbr_partition netbsd_part;
+
+ /*
+ * Don't (yet) know disk geometry (BIOS), use
+ * partition table to find NetBSD/i386 partition, and obtain
+ * disklabel from there.
+ */
+
+ ext_base = 0;
+ next_ext = 0;
+ for (;;) {
+ this_ext = next_ext;
+ next_ext = 0;
+ if (pread(f, &mbr, sizeof mbr, this_ext * (off_t)DEV_BSIZE)
+ != sizeof(mbr)) {
+ warn("Can't read master boot record %d", this_ext);
+ return 0;
+ }
+
+ /* Check if table is valid. */
+ if (mbr.mbr_magic != htole16(MBR_MAGIC)) {
+ warnx("Invalid signature in mbr record %d", this_ext);
+ return 0;
+ }
+
+ dp = &mbr.mbr_parts[0];
+#if defined(_no_longer_needed) && !defined(__i386__) && !defined(__x86_64__)
+ /* avoid alignment error */
+ memcpy(mbr, dp, MBR_PART_COUNT * sizeof(*dp));
+ dp = (struct mbr_partition *)mbr;
+#endif /* ! __i386__ */
+
+ /* Find NetBSD partition. */
+ for (part = 0; part < MBR_PART_COUNT; dp++, part++) {
+ dp->mbrp_start = le32toh(dp->mbrp_start);
+ dp->mbrp_size = le32toh(dp->mbrp_size);
+ switch (dp->mbrp_type) {
+ case MBR_PTYPE_NETBSD:
+ netbsd_part = *dp;
+ break;
+ case MBR_PTYPE_EXT:
+ case MBR_PTYPE_EXT_LBA:
+ case MBR_PTYPE_EXT_LNX:
+ next_ext = dp->mbrp_start;
+ continue;
+#ifdef COMPAT_386BSD_MBRPART
+ case MBR_PTYPE_386BSD:
+ if (ext_base == 0)
+ netbsd_part = *dp;
+ continue;
+#endif /* COMPAT_386BSD_MBRPART */
+ default:
+ continue;
+ }
+ break;
+ }
+ if (part < MBR_PART_COUNT)
+ /* We found a netbsd partition */
+ break;
+ if (next_ext == 0)
+ /* No more extended partitions */
+ break;
+ next_ext += ext_base;
+ if (ext_base == 0)
+ ext_base = next_ext;
+
+ if (next_ext <= this_ext) {
+ warnx("Invalid extended chain %x <= %x",
+ next_ext, this_ext);
+ break;
+ }
+ }
+
+ if (netbsd_part.mbrp_type == 0)
+ return 0;
+
+ netbsd_part.mbrp_start += this_ext;
+ return &netbsd_part;
+}
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+/*
+ * static int filecore_checksum(u_char *bootblock)
+ *
+ * Calculates the filecore boot block checksum. This is used to validate
+ * a filecore boot block on the disk. If a boot block is validated then
+ * it is used to locate the partition table. If the boot block is not
+ * validated, it is assumed that the whole disk is NetBSD.
+ *
+ * The basic algorithm is:
+ *
+ * for (each byte in block, excluding checksum) {
+ * sum += byte;
+ * if (sum > 255)
+ * sum -= 255;
+ * }
+ *
+ * That's equivalent to summing all of the bytes in the block
+ * (excluding the checksum byte, of course), then calculating the
+ * checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
+ * expression may or may not yield a faster checksum function,
+ * but it's easier to reason about.
+ *
+ * Note that if you have a block filled with bytes of a single
+ * value "X" (regardless of that value!) and calculate the cksum
+ * of the block (excluding the checksum byte), you will _always_
+ * end up with a checksum of X. (Do the math; that can be derived
+ * from the checksum calculation function!) That means that
+ * blocks which contain bytes which all have the same value will
+ * always checksum properly. That's a _very_ unlikely occurence
+ * (probably impossible, actually) for a valid filecore boot block,
+ * so we treat such blocks as invalid.
+ */
+static int
+filecore_checksum(u_char *bootblock)
+{
+ u_char byte0, accum_diff;
+ u_int sum;
+ int i;
+
+ sum = 0;
+ accum_diff = 0;
+ byte0 = bootblock[0];
+
+ /*
+ * Sum the contents of the block, keeping track of whether
+ * or not all bytes are the same. If 'accum_diff' ends up
+ * being zero, all of the bytes are, in fact, the same.
+ */
+ for (i = 0; i < 511; ++i) {
+ sum += bootblock[i];
+ accum_diff |= bootblock[i] ^ byte0;
+ }
+
+ /*
+ * Check to see if the checksum byte is the same as the
+ * rest of the bytes, too. (Note that if all of the bytes
+ * are the same except the checksum, a checksum compare
+ * won't succeed, but that's not our problem.)
+ */
+ accum_diff |= bootblock[i] ^ byte0;
+
+ /* All bytes in block are the same; call it invalid. */
+ if (accum_diff == 0)
+ return (-1);
+
+ return (sum - ((sum - 1) / 255) * 255);
+}
+
+/*
+ * Fetch filecore bootblock from disk and analyse it
+ */
+static u_int
+get_filecore_partition(int f)
+{
+ struct filecore_bootblock *fcbb;
+ static char bb[DEV_BSIZE];
+ u_int offset;
+
+ if (lseek(f, (off_t)FILECORE_BOOT_SECTOR * DEV_BSIZE, SEEK_SET) < 0 ||
+ read(f, bb, sizeof(bb)) != sizeof(bb))
+ err(4, "can't read filecore boot block");
+ fcbb = (struct filecore_bootblock *)bb;
+
+ /* Check if table is valid. */
+ if (filecore_checksum(bb) != fcbb->checksum)
+ return (0);
+
+ /*
+ * Check for NetBSD/arm32 (RiscBSD) partition marker.
+ * If found the NetBSD disklabel location is easy.
+ */
+ offset = (fcbb->partition_cyl_low + (fcbb->partition_cyl_high << 8))
+ * fcbb->heads * fcbb->secspertrack;
+ if (fcbb->partition_type == PARTITION_FORMAT_RISCBSD)
+ return (offset);
+ else if (fcbb->partition_type == PARTITION_FORMAT_RISCIX) {
+ struct riscix_partition_table *riscix_part;
+ int loop;
+
+ /*
+ * Read the RISCiX partition table and search for the
+ * first partition named "RiscBSD", "NetBSD", or "Empty:"
+ *
+ * XXX is use of 'Empty:' really desirable?! -- cgd
+ */
+
+ if (lseek(f, (off_t)offset * DEV_BSIZE, SEEK_SET) < 0 ||
+ read(f, bb, sizeof(bb)) != sizeof(bb))
+ err(4, "can't read riscix partition table");
+ riscix_part = (struct riscix_partition_table *)bb;
+
+ for (loop = 0; loop < NRISCIX_PARTITIONS; ++loop) {
+ if (strcmp(riscix_part->partitions[loop].rp_name,
+ "RiscBSD") == 0 ||
+ strcmp(riscix_part->partitions[loop].rp_name,
+ "NetBSD") == 0 ||
+ strcmp(riscix_part->partitions[loop].rp_name,
+ "Empty:") == 0) {
+ offset = riscix_part->partitions[loop].rp_start;
+ break;
+ }
+ }
+ if (loop == NRISCIX_PARTITIONS) {
+ /*
+ * Valid filecore boot block, RISCiX partition table
+ * but no NetBSD partition. We should leave this
+ * disc alone.
+ */
+ errx(4, "cannot label: no NetBSD partition found"
+ " in RISCiX partition table");
+ }
+ return (offset);
+ } else {
+ /*
+ * Valid filecore boot block and no non-ADFS partition.
+ * This means that the whole disc is allocated for ADFS
+ * so do not trash ! If the user really wants to put a
+ * NetBSD disklabel on the disc then they should remove
+ * the filecore boot block first with dd.
+ */
+ errx(4, "cannot label: filecore-only disk"
+ " (no non-ADFS partition)");
+ }
+ return (0);
+}
+#endif /* USE_ACORN */
+
+/*
+ * Fetch disklabel for disk.
+ * Use ioctl to get label unless -r flag is given.
+ */
+static struct disklabel *
+readlabel(int f)
+{
+ struct disklabel *lp;
+
+ if (Fflag || rflag || Iflag) {
+ const char *msg;
+ off_t sectoffset;
+
+ msg = NULL;
+ sectoffset = 0;
+
+#ifdef USE_MBR
+ if (dosdp)
+ sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+ /* XXX */
+ sectoffset = (off_t)filecore_partition_offset * DEV_BSIZE;
+#endif /* USE_ACORN */
+
+ if (lseek(f, sectoffset, SEEK_SET) < 0 ||
+ read(f, bootarea, BBSIZE) != BBSIZE)
+ err(4, "%s", specname);
+
+ msg = "no disklabel";
+ for (lp = (struct disklabel *)bootarea;
+ lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
+ lp = (struct disklabel *)((char *)lp + sizeof(long))) {
+ if (lp->d_magic == DISKMAGIC &&
+ lp->d_magic2 == DISKMAGIC) {
+ if (lp->d_npartitions <= MAXPARTITIONS &&
+ dkcksum(lp) == 0)
+ return (lp);
+ msg = "disk label corrupted";
+ }
+ }
+ if (msg != NULL && !Iflag)
+ errx(1, "%s", msg);
+#if HAVE_NBTOOL_CONFIG_H
+ goto err;
+#else
+ /*
+ * There was no label on the disk. Get the fictious one
+ * as a basis for initialisation.
+ */
+ lp = makebootarea(bootarea, &lab, f);
+ if (ioctl(f, DIOCGDINFO, lp) < 0 &&
+ ioctl(f, DIOCGDEFLABEL, lp) < 0)
+ goto err;
+#endif /* HAVE_NBTOOL_CONFIG_H */
+ } else {
+#ifdef HAVE_NBTOOL_CONFIG_H
+ goto err;
+#else
+ lp = &lab;
+ if (ioctl(f, DIOCGDINFO, lp) < 0)
+ err(4, "ioctl DIOCGDINFO");
+#endif /* HAVE_NBTOOL_CONFIG_H */
+ }
+ return (lp);
+err:
+ errx(1, "could not get initial label");
+}
+
+/*
+ * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
+ * Returns a pointer to the disklabel portion of the bootarea.
+ */
+static struct disklabel *
+makebootarea(char *boot, struct disklabel *dp, int f)
+{
+ struct disklabel *lp;
+ char *p;
+ daddr_t lsec;
+ off_t loff;
+#if NUMBOOT > 0
+ int b;
+ char *dkbasename;
+# if NUMBOOT <= 1
+ struct stat sb;
+# endif
+#endif /* NUMBOOT > 0 */
+
+ if ((lsec = GETLABELSECTOR()) < 0)
+ err(4, "getlabelsector()");
+ if ((loff = GETLABELOFFSET()) < 0)
+ err(4, "getlabeloffset()");
+
+ /* XXX */
+ if (dp->d_secsize == 0) {
+ dp->d_secsize = DEV_BSIZE;
+ dp->d_bbsize = BBSIZE;
+ }
+ lp = (struct disklabel *) (boot + (lsec * dp->d_secsize) + loff);
+ (void) memset(lp, 0, sizeof(*lp));
+
+#ifdef SAVEBOOTAREA
+ /*
+ * We must read the current bootarea so we don't clobber the
+ * existing boot block, if any.
+ */
+ if (rflag || Iflag) {
+ off_t sectoffset;
+
+ sectoffset = 0;
+ if (lseek(f, sectoffset, SEEK_SET) < 0 ||
+ read(f, boot, BBSIZE) != BBSIZE)
+ err(4, "%s", specname);
+ (void) memset(lp, 0, sizeof(*lp));
+ }
+#endif /* SAVEBOOTAREA */
+
+#if NUMBOOT > 0
+ /*
+ * If we are not installing a boot program but we are installing a
+ * label on disk then we must read the current bootarea so we don't
+ * clobber the existing boot.
+ */
+ if (!installboot) {
+ if (rflag || Iflag) {
+ off_t sectoffset;
+
+ sectoffset = 0;
+#ifdef USE_MBR
+ if (dosdp)
+ sectoffset = (off_t)dosdp->mbrp_start * DEV_BSIZE;
+#endif /* USE_MBR */
+
+#ifdef USE_ACORN
+ /* XXX */
+ sectoffset = (off_t)filecore_partition_offset
+ * DEV_BSIZE;
+#endif /* USE_ACORN */
+
+ if (lseek(f, sectoffset, SEEK_SET) < 0 ||
+ read(f, boot, BBSIZE) != BBSIZE)
+ err(4, "%s", specname);
+ (void) memset(lp, 0, sizeof(*lp));
+ }
+ return (lp);
+ }
+ /*
+ * We are installing a boot program. Determine the name(s) and
+ * read them into the appropriate places in the boot area.
+ */
+ if (!xxboot || !bootxx) {
+ dkbasename = np;
+ if ((p = strrchr(dkname, '/')) == NULL)
+ p = dkname;
+ else
+ p++;
+ while (*p && !isdigit(*p & 0xff))
+ *np++ = *p++;
+ *np++ = '\0';
+
+ if (!xxboot) {
+ (void)sprintf(np, "%s/%sboot",
+ _PATH_BOOTDIR, dkbasename);
+ if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
+ dkbasename++;
+ xxboot = np;
+ (void)sprintf(xxboot, "%s/%sboot",
+ _PATH_BOOTDIR, dkbasename);
+ np += strlen(xxboot) + 1;
+ }
+#if NUMBOOT > 1
+ if (!bootxx) {
+ (void)sprintf(np, "%s/boot%s",
+ _PATH_BOOTDIR, dkbasename);
+ if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
+ dkbasename++;
+ bootxx = np;
+ (void)sprintf(bootxx, "%s/boot%s",
+ _PATH_BOOTDIR, dkbasename);
+ np += strlen(bootxx) + 1;
+ }
+#endif /* NUMBOOT > 1 */
+ }
+
+#ifdef DEBUG
+ if (debug)
+ warnx("bootstraps: xxboot = %s, bootxx = %s", xxboot,
+ bootxx ? bootxx : "NONE");
+#endif
+
+ /*
+ * Strange rules:
+ * 1. One-piece bootstrap (hp300/hp800)
+ * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
+ * is remembered and written later following the bootarea.
+ * 2. Two-piece bootstraps (vax/i386?/mips?)
+ * up to d_secsize bytes of ``xxboot'' go in first d_secsize
+ * bytes of bootarea, remaining d_bbsize-d_secsize filled
+ * from ``bootxx''.
+ */
+ b = open(xxboot, O_RDONLY);
+ if (b < 0)
+ err(4, "%s", xxboot);
+#if NUMBOOT > 1
+ if (read(b, boot, (int)dp->d_secsize) < 0)
+ err(4, "%s", xxboot);
+ (void)close(b);
+ b = open(bootxx, O_RDONLY);
+ if (b < 0)
+ err(4, "%s", bootxx);
+ if (read(b, &boot[dp->d_secsize],
+ (int)(dp->d_bbsize-dp->d_secsize)) < 0)
+ err(4, "%s", bootxx);
+#else /* NUMBOOT <= 1 */
+ if (read(b, boot, (int)dp->d_bbsize) < 0)
+ err(4, "%s", xxboot);
+ (void)fstat(b, &sb);
+ bootsize = (int)sb.st_size - dp->d_bbsize;
+ if (bootsize > 0) {
+ /* XXX assume d_secsize is a power of two */
+ bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
+ bootbuf = (char *)malloc((size_t)bootsize);
+ if (bootbuf == 0)
+ err(4, "%s", xxboot);
+ if (read(b, bootbuf, bootsize) < 0) {
+ free(bootbuf);
+ err(4, "%s", xxboot);
+ }
+ }
+#endif /* NUMBOOT <= 1 */
+ (void)close(b);
+#endif /* NUMBOOT > 0 */
+
+ /*
+ * Make sure no part of the bootstrap is written in the area
+ * reserved for the label.
+ */
+ for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
+ if (*p)
+ errx(2, "Bootstrap doesn't leave room for disk label");
+ return (lp);
+}
+
+static void
+makedisktab(FILE *f, struct disklabel *lp)
+{
+ int i;
+ const char *did;
+ struct partition *pp;
+
+ did = "\\\n\t:";
+ (void) fprintf(f, "%.*s|Automatically generated label:\\\n\t:dt=",
+ (int) sizeof(lp->d_typename), lp->d_typename);
+ if ((unsigned) lp->d_type < DKMAXTYPES)
+ (void) fprintf(f, "%s:", dktypenames[lp->d_type]);
+ else
+ (void) fprintf(f, "unknown%d:", lp->d_type);
+
+ (void) fprintf(f, "se#%d:", lp->d_secsize);
+ (void) fprintf(f, "ns#%d:", lp->d_nsectors);
+ (void) fprintf(f, "nt#%d:", lp->d_ntracks);
+ (void) fprintf(f, "sc#%d:", lp->d_secpercyl);
+ (void) fprintf(f, "nc#%d:", lp->d_ncylinders);
+
+ if ((lp->d_secpercyl * lp->d_ncylinders) != lp->d_secperunit) {
+ (void) fprintf(f, "%ssu#%d:", did, lp->d_secperunit);
+ did = "";
+ }
+ if (lp->d_rpm != 3600) {
+ (void) fprintf(f, "%srm#%d:", did, lp->d_rpm);
+ did = "";
+ }
+ if (lp->d_interleave != 1) {
+ (void) fprintf(f, "%sil#%d:", did, lp->d_interleave);
+ did = "";
+ }
+ if (lp->d_trackskew != 0) {
+ (void) fprintf(f, "%ssk#%d:", did, lp->d_trackskew);
+ did = "";
+ }
+ if (lp->d_cylskew != 0) {
+ (void) fprintf(f, "%scs#%d:", did, lp->d_cylskew);
+ did = "";
+ }
+ if (lp->d_headswitch != 0) {
+ (void) fprintf(f, "%shs#%d:", did, lp->d_headswitch);
+ did = "";
+ }
+ if (lp->d_trkseek != 0) {
+ (void) fprintf(f, "%sts#%d:", did, lp->d_trkseek);
+ did = "";
+ }
+#ifdef notyet
+ (void) fprintf(f, "drivedata: ");
+ for (i = NDDATA - 1; i >= 0; i--)
+ if (lp->d_drivedata[i])
+ break;
+ if (i < 0)
+ i = 0;
+ for (j = 0; j <= i; j++)
+ (void) fprintf(f, "%d ", lp->d_drivedata[j]);
+#endif /* notyet */
+ pp = lp->d_partitions;
+ for (i = 0; i < lp->d_npartitions; i++, pp++) {
+ if (pp->p_size) {
+ char c = 'a' + i;
+ (void) fprintf(f, "\\\n\t:");
+ (void) fprintf(f, "p%c#%d:", c, pp->p_size);
+ (void) fprintf(f, "o%c#%d:", c, pp->p_offset);
+ if (pp->p_fstype != FS_UNUSED) {
+ if ((unsigned) pp->p_fstype < FSMAXTYPES)
+ (void) fprintf(f, "t%c=%s:", c,
+ fstypenames[pp->p_fstype]);
+ else
+ (void) fprintf(f, "t%c=unknown%d:",
+ c, pp->p_fstype);
+ }
+ switch (pp->p_fstype) {
+
+ case FS_UNUSED:
+ break;
+
+ case FS_BSDFFS:
+ case FS_BSDLFS:
+ case FS_EX2FS:
+ case FS_ADOS:
+ case FS_APPLEUFS:
+ (void) fprintf(f, "b%c#%d:", c,
+ pp->p_fsize * pp->p_frag);
+ (void) fprintf(f, "f%c#%d:", c, pp->p_fsize);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ (void) fprintf(f, "\n");
+ (void) fflush(f);
+}
+
+static int
+edit(struct disklabel *lp, int f)
+{
+ struct disklabel label;
+ const char *tmpdir;
+ int first, ch, fd;
+ FILE *fp;
+
+ if ((tmpdir = getenv("TMPDIR")) == NULL)
+ tmpdir = _PATH_TMP;
+ (void)snprintf(tmpfil, sizeof(tmpfil), "%s/%s", tmpdir, TMPFILE);
+ if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) {
+ warn("%s", tmpfil);
+ return (1);
+ }
+ (void)fchmod(fd, 0600);
+ showinfo(fp, lp, specname);
+ showpartitions(fp, lp, Cflag);
+ (void) fclose(fp);
+ for (;;) {
+ if (!editit())
+ break;
+ fp = fopen(tmpfil, "r");
+ if (fp == NULL) {
+ warn("%s", tmpfil);
+ break;
+ }
+ (void) memset(&label, 0, sizeof(label));
+ if (getasciilabel(fp, &label)) {
+ *lp = label;
+ if (writelabel(f, bootarea, lp) == 0) {
+ (void) unlink(tmpfil);
+ return (0);
+ }
+ }
+ (void) printf("re-edit the label? [y]: ");
+ (void) fflush(stdout);
+ first = ch = getchar();
+ while (ch != '\n' && ch != EOF)
+ ch = getchar();
+ if (first == 'n' || first == 'N')
+ break;
+ }
+ (void)unlink(tmpfil);
+ return (1);
+}
+
+static int
+editit(void)
+{
+ int pid, xpid;
+ int status;
+ sigset_t nsigset, osigset;
+
+ sigemptyset(&nsigset);
+ sigaddset(&nsigset, SIGINT);
+ sigaddset(&nsigset, SIGQUIT);
+ sigaddset(&nsigset, SIGHUP);
+ sigprocmask(SIG_BLOCK, &nsigset, &osigset);
+ while ((pid = fork()) < 0) {
+ if (errno != EAGAIN) {
+ sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
+ warn("fork");
+ return (0);
+ }
+ sleep(1);
+ }
+ if (pid == 0) {
+ const char *ed;
+ char *buf;
+ int retval;
+
+ sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
+ setgid(getgid());
+ setuid(getuid());
+ if ((ed = getenv("EDITOR")) == (char *)0)
+ ed = DEFEDITOR;
+ /*
+ * Jump through a few extra hoops in case someone's editor
+ * is "editor arg1 arg2".
+ */
+ asprintf(&buf, "%s %s", ed, tmpfil);
+ if (!buf)
+ err(1, "malloc");
+ retval = execlp(_PATH_BSHELL, _PATH_BSHELL, "-c", buf, NULL);
+ if (retval == -1)
+ perror(ed);
+ exit(retval);
+ }
+ while ((xpid = wait(&status)) >= 0)
+ if (xpid == pid)
+ break;
+ sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
+ return (!status);
+}
+
+static char *
+skip(char *cp)
+{
+
+ cp += strspn(cp, " \t");
+ if (*cp == '\0')
+ return (NULL);
+ return (cp);
+}
+
+static char *
+word(char *cp)
+{
+
+ if (cp == NULL || *cp == '\0')
+ return (NULL);
+
+ cp += strcspn(cp, " \t");
+ if (*cp == '\0')
+ return (NULL);
+ *cp++ = '\0';
+ cp += strspn(cp, " \t");
+ if (*cp == '\0')
+ return (NULL);
+ return (cp);
+}
+
+#define _CHECKLINE \
+ if (tp == NULL || *tp == '\0') { \
+ warnx("line %d: too few fields", lineno); \
+ errors++; \
+ break; \
+ }
+
+#define __CHECKLINE \
+ if (*tp == NULL || **tp == '\0') { \
+ warnx("line %d: too few fields", lineno); \
+ *tp = _error_; \
+ return 0; \
+ }
+
+static char _error_[] = "";
+#define NXTNUM(n) if ((n = nxtnum(&tp, lineno),0) + tp != _error_) \
+ ; else goto error
+#define NXTXNUM(n) if ((n = nxtxnum(&tp, lp, lineno),0) + tp != _error_) \
+ ; else goto error
+
+static unsigned long
+nxtnum(char **tp, int lineno)
+{
+ char *cp;
+ unsigned long v;
+
+ __CHECKLINE
+ if (getulong(*tp, '\0', &cp, &v, UINT32_MAX) != 0) {
+ warnx("line %d: syntax error", lineno);
+ *tp = _error_;
+ return 0;
+ }
+ *tp = cp;
+ return v;
+}
+
+static unsigned long
+nxtxnum(char **tp, struct disklabel *lp, int lineno)
+{
+ char *cp, *ncp;
+ unsigned long n, v;
+
+ __CHECKLINE
+ cp = *tp;
+ if (getulong(cp, '/', &ncp, &n, UINT32_MAX) != 0)
+ goto bad;
+
+ if (*ncp == '/') {
+ n *= lp->d_secpercyl;
+ cp = ncp + 1;
+ if (getulong(cp, '/', &ncp, &v, UINT32_MAX) != 0)
+ goto bad;
+ n += v * lp->d_nsectors;
+ cp = ncp + 1;
+ if (getulong(cp, '\0', &ncp, &v, UINT32_MAX) != 0)
+ goto bad;
+ n += v;
+ }
+ *tp = ncp;
+ return n;
+bad:
+ warnx("line %d: invalid format", lineno);
+ *tp = _error_;
+ return 0;
+}
+
+/*
+ * Read an ascii label in from fd f,
+ * in the same format as that put out by showinfo() and showpartitions(),
+ * and fill in lp.
+ */
+static int
+getasciilabel(FILE *f, struct disklabel *lp)
+{
+ const char *const *cpp, *s;
+ struct partition *pp;
+ char *cp, *tp, line[BUFSIZ], tbuf[15];
+ int lineno, errors;
+ unsigned long v;
+ unsigned int part;
+
+ lineno = 0;
+ errors = 0;
+ lp->d_bbsize = BBSIZE; /* XXX */
+ lp->d_sbsize = SBLOCKSIZE; /* XXX */
+ while (fgets(line, sizeof(line) - 1, f)) {
+ lineno++;
+ if ((cp = strpbrk(line, "#\r\n")) != NULL)
+ *cp = '\0';
+ cp = skip(line);
+ if (cp == NULL) /* blank line or comment line */
+ continue;
+ tp = strchr(cp, ':'); /* everything has a colon in it */
+ if (tp == NULL) {
+ warnx("line %d: syntax error", lineno);
+ errors++;
+ continue;
+ }
+ *tp++ = '\0', tp = skip(tp);
+ if (!strcmp(cp, "type")) {
+ if (tp == NULL) {
+ strlcpy(tbuf, "unknown", sizeof(tbuf));
+ tp = tbuf;
+ }
+ cpp = dktypenames;
+ for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
+ if ((s = *cpp) && !strcasecmp(s, tp)) {
+ lp->d_type = cpp - dktypenames;
+ goto next;
+ }
+ if (GETNUM16(tp, &v) != 0) {
+ warnx("line %d: syntax error", lineno);
+ errors++;
+ continue;
+ }
+ if (v >= DKMAXTYPES)
+ warnx("line %d: warning, unknown disk type: %s",
+ lineno, tp);
+ lp->d_type = v;
+ continue;
+ }
+ if (!strcmp(cp, "flags")) {
+ for (v = 0; (cp = tp) && *cp != '\0';) {
+ tp = word(cp);
+ if (!strcasecmp(cp, "removable"))
+ v |= D_REMOVABLE;
+ else if (!strcasecmp(cp, "ecc"))
+ v |= D_ECC;
+ else if (!strcasecmp(cp, "badsect"))
+ v |= D_BADSECT;
+ else {
+ warnx("line %d: bad flag: %s",
+ lineno, cp);
+ errors++;
+ }
+ }
+ lp->d_flags = v;
+ continue;
+ }
+ if (!strcmp(cp, "drivedata")) {
+ int i;
+
+ for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
+ if (GETNUM32(cp, &v) != 0) {
+ warnx("line %d: bad drive data",
+ lineno);
+ errors++;
+ } else
+ lp->d_drivedata[i] = v;
+ i++;
+ tp = word(cp);
+ }
+ continue;
+ }
+ if (sscanf(cp, "%lu partitions", &v) == 1) {
+ if (v == 0 || v > MAXPARTITIONS) {
+ warnx("line %d: bad # of partitions", lineno);
+ lp->d_npartitions = MAXPARTITIONS;
+ errors++;
+ } else
+ lp->d_npartitions = v;
+ continue;
+ }
+ if (tp == NULL) {
+ tbuf[0] = '\0';
+ tp = tbuf;
+ }
+ if (!strcmp(cp, "disk")) {
+ strncpy(lp->d_typename, tp, sizeof(lp->d_typename));
+ continue;
+ }
+ if (!strcmp(cp, "label")) {
+ strncpy(lp->d_packname, tp, sizeof(lp->d_packname));
+ continue;
+ }
+ if (!strcmp(cp, "bytes/sector")) {
+ if (GETNUM32(tp, &v) != 0 || v <= 0 || (v % 512) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_secsize = v;
+ continue;
+ }
+ if (!strcmp(cp, "sectors/track")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_nsectors = v;
+ continue;
+ }
+ if (!strcmp(cp, "sectors/cylinder")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_secpercyl = v;
+ continue;
+ }
+ if (!strcmp(cp, "tracks/cylinder")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_ntracks = v;
+ continue;
+ }
+ if (!strcmp(cp, "cylinders")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_ncylinders = v;
+ continue;
+ }
+ if (!strcmp(cp, "total sectors")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_secperunit = v;
+ continue;
+ }
+ if (!strcmp(cp, "rpm")) {
+ if (GETNUM16(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_rpm = v;
+ continue;
+ }
+ if (!strcmp(cp, "interleave")) {
+ if (GETNUM16(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_interleave = v;
+ continue;
+ }
+ if (!strcmp(cp, "trackskew")) {
+ if (GETNUM16(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_trackskew = v;
+ continue;
+ }
+ if (!strcmp(cp, "cylinderskew")) {
+ if (GETNUM16(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_cylskew = v;
+ continue;
+ }
+ if (!strcmp(cp, "headswitch")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_headswitch = v;
+ continue;
+ }
+ if (!strcmp(cp, "track-to-track seek")) {
+ if (GETNUM32(tp, &v) != 0) {
+ warnx("line %d: bad %s: %s", lineno, cp, tp);
+ errors++;
+ } else
+ lp->d_trkseek = v;
+ continue;
+ }
+ if ('a' > *cp || *cp > 'z' || cp[1] != '\0') {
+ warnx("line %d: unknown field: %s", lineno, cp);
+ errors++;
+ continue;
+ }
+
+ /* We have a partition entry */
+ part = *cp - 'a';
+
+ if (part >= MAXPARTITIONS) {
+ warnx("line %d: bad partition name: %s", lineno, cp);
+ errors++;
+ continue;
+ }
+ pp = &lp->d_partitions[part];
+
+ NXTXNUM(pp->p_size);
+ NXTXNUM(pp->p_offset);
+ /* can't use word() here because of blanks in fstypenames[] */
+ tp += strspn(tp, " \t");
+ _CHECKLINE
+ cp = tp;
+ cpp = fstypenames;
+ for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
+ s = *cpp;
+ if (s == NULL ||
+ (cp[strlen(s)] != ' ' &&
+ cp[strlen(s)] != '\t' &&
+ cp[strlen(s)] != '\0'))
+ continue;
+ if (!memcmp(s, cp, strlen(s))) {
+ pp->p_fstype = cpp - fstypenames;
+ tp += strlen(s);
+ if (*tp == '\0')
+ tp = NULL;
+ else {
+ tp += strspn(tp, " \t");
+ if (*tp == '\0')
+ tp = NULL;
+ }
+ goto gottype;
+ }
+ }
+ tp = word(cp);
+ if (isdigit(*cp & 0xff)) {
+ if (GETNUM8(cp, &v) != 0) {
+ warnx("line %d: syntax error", lineno);
+ errors++;
+ }
+ } else
+ v = FSMAXTYPES;
+ if ((unsigned)v >= FSMAXTYPES) {
+ warnx("line %d: warning, unknown filesystem type: %s",
+ lineno, cp);
+ v = FS_UNUSED;
+ }
+ pp->p_fstype = v;
+gottype:
+ switch (pp->p_fstype) {
+
+ case FS_UNUSED: /* XXX */
+ NXTNUM(pp->p_fsize);
+ if (pp->p_fsize == 0)
+ break;
+ NXTNUM(v);
+ pp->p_frag = v / pp->p_fsize;
+ break;
+
+ case FS_BSDFFS:
+ case FS_ADOS:
+ case FS_APPLEUFS:
+ NXTNUM(pp->p_fsize);
+ if (pp->p_fsize == 0)
+ break;
+ NXTNUM(v);
+ pp->p_frag = v / pp->p_fsize;
+ NXTNUM(pp->p_cpg);
+ break;
+ case FS_BSDLFS:
+ NXTNUM(pp->p_fsize);
+ if (pp->p_fsize == 0)
+ break;
+ NXTNUM(v);
+ pp->p_frag = v / pp->p_fsize;
+ NXTNUM(pp->p_sgs);
+ break;
+ case FS_EX2FS:
+ NXTNUM(pp->p_fsize);
+ if (pp->p_fsize == 0)
+ break;
+ NXTNUM(v);
+ pp->p_frag = v / pp->p_fsize;
+ break;
+ case FS_ISO9660:
+ NXTNUM(pp->p_cdsession);
+ break;
+ default:
+ break;
+ }
+ continue;
+ error:
+ errors++;
+ next:
+ ;
+ }
+ errors += checklabel(lp);
+ return (errors == 0);
+}
+
+/*
+ * Check disklabel for errors and fill in
+ * derived fields according to supplied values.
+ */
+int
+checklabel(struct disklabel *lp)
+{
+ struct partition *pp, *qp;
+ int i, j, errors;
+ char part;
+
+ errors = 0;
+ if (lp->d_secsize == 0) {
+ warnx("sector size %d", lp->d_secsize);
+ return (1);
+ }
+ if (lp->d_nsectors == 0) {
+ warnx("sectors/track %d", lp->d_nsectors);
+ return (1);
+ }
+ if (lp->d_ntracks == 0) {
+ warnx("tracks/cylinder %d", lp->d_ntracks);
+ return (1);
+ }
+ if (lp->d_ncylinders == 0) {
+ warnx("cylinders/unit %d", lp->d_ncylinders);
+ errors++;
+ }
+ if (lp->d_rpm == 0)
+ warnx("warning, revolutions/minute %d", lp->d_rpm);
+ if (lp->d_secpercyl == 0)
+ lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
+ if (lp->d_secperunit == 0)
+ lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
+#ifdef __i386__notyet__
+ if (dosdp && lp->d_secperunit > dosdp->mbrp_start + dosdp->mbrp_size) {
+ warnx("exceeds DOS partition size");
+ errors++;
+ lp->d_secperunit = dosdp->mbrp_start + dosdp->mbrp_size;
+ }
+ /* XXX should also check geometry against BIOS's idea */
+#endif /* __i386__notyet__ */
+#ifdef __arm32__notyet__
+ /* XXX similar code as for i386 */
+#endif /* __arm32__notyet__ */
+ if (lp->d_bbsize == 0) {
+ warnx("boot block size %d", lp->d_bbsize);
+ errors++;
+ } else if (lp->d_bbsize % lp->d_secsize)
+ warnx("warning, boot block size %% sector-size != 0");
+ if (lp->d_sbsize == 0) {
+ warnx("super block size %d", lp->d_sbsize);
+ errors++;
+ } else if (lp->d_sbsize % lp->d_secsize)
+ warnx("warning, super block size %% sector-size != 0");
+ if (lp->d_npartitions > MAXPARTITIONS)
+ warnx("warning, number of partitions (%d) > MAXPARTITIONS (%d)",
+ lp->d_npartitions, MAXPARTITIONS);
+ else
+ for (i = MAXPARTITIONS - 1; i >= lp->d_npartitions; i--) {
+ part = 'a' + i;
+ pp = &lp->d_partitions[i];
+ if (pp->p_size || pp->p_offset) {
+ warnx("warning, partition %c increased "
+ "number of partitions from %d to %d",
+ part, lp->d_npartitions, i + 1);
+ lp->d_npartitions = i + 1;
+ break;
+ }
+ }
+ for (i = 0; i < lp->d_npartitions; i++) {
+ part = 'a' + i;
+ pp = &lp->d_partitions[i];
+ if (pp->p_size == 0 && pp->p_offset != 0)
+ warnx("warning, partition %c: size 0, but offset %d",
+ part, pp->p_offset);
+#ifdef STRICT_CYLINDER_ALIGNMENT
+ if (pp->p_offset % lp->d_secpercyl) {
+ warnx("warning, partition %c:"
+ " not starting on cylinder boundary",
+ part);
+ errors++;
+ }
+#endif /* STRICT_CYLINDER_ALIGNMENT */
+ if (pp->p_offset > lp->d_secperunit) {
+ warnx("partition %c: offset past end of unit", part);
+ errors++;
+ }
+ if (pp->p_offset + pp->p_size > lp->d_secperunit) {
+ warnx("partition %c: partition extends"
+ " past end of unit",
+ part);
+ errors++;
+ }
+ if (pp->p_fstype != FS_UNUSED)
+ for (j = i + 1; j < lp->d_npartitions; j++) {
+ qp = &lp->d_partitions[j];
+ if (qp->p_fstype == FS_UNUSED)
+ continue;
+ if (pp->p_offset < qp->p_offset + qp->p_size &&
+ qp->p_offset < pp->p_offset + pp->p_size)
+ warnx("partitions %c and %c overlap",
+ part, 'a' + j);
+ }
+ }
+ return (errors);
+}
+
+#if NUMBOOT > 0
+/*
+ * If we are installing a boot program that doesn't fit in d_bbsize
+ * we need to mark those partitions that the boot overflows into.
+ * This allows newfs to prevent creation of a filesystem where it might
+ * clobber bootstrap code.
+ */
+static void
+setbootflag(struct disklabel *lp)
+{
+ struct partition *pp;
+ int i, errors;
+ char part;
+ u_long boffset;
+
+ errors = 0;
+ if (bootbuf == 0)
+ return;
+ boffset = bootsize / lp->d_secsize;
+ for (i = 0; i < lp->d_npartitions; i++) {
+ part = 'a' + i;
+ pp = &lp->d_partitions[i];
+ if (pp->p_size == 0)
+ continue;
+ if (boffset <= pp->p_offset) {
+ if (pp->p_fstype == FS_BOOT)
+ pp->p_fstype = FS_UNUSED;
+ } else if (pp->p_fstype != FS_BOOT) {
+ if (pp->p_fstype != FS_UNUSED) {
+ warnx("boot overlaps used partition %c",
+ part);
+ errors++;
+ } else {
+ pp->p_fstype = FS_BOOT;
+ warnx("warning, boot overlaps partition %c, %s",
+ part, "marked as FS_BOOT");
+ }
+ }
+ }
+ if (errors)
+ errx(4, "cannot install boot program");
+}
+#endif /* NUMBOOT > 0 */
+
+static void
+usage(void)
+{
+ static const struct {
+ const char *name;
+ const char *expn;
+ } usages[] = {
+ { "[-rt] [-C] [-F] disk",
+ "(to read label)" },
+ { "-w [-r] [-F] [-f disktab] disk type [ packid ]",
+#if NUMBOOT > 0
+ "(to write label with existing boot program)"
+#else
+ "(to write label)"
+#endif
+ },
+ { "-e [-r] [-I] [-C] [-F] disk",
+ "(to edit label)" },
+ { "-i [-I] [-r] [-F] disk",
+ "(to create a label interactively)" },
+ { "-R [-r] [-F] disk protofile",
+#if NUMBOOT > 0
+ "(to restore label with existing boot program)"
+#else
+ "(to restore label)"
+#endif
+ },
+#if NUMBOOT > 0
+# if NUMBOOT > 1
+ { "-B [-f disktab] [ -b xxboot [ -s bootxx ] ] disk [ type ]",
+ "(to install boot program with existing label)" },
+ { "-w -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk type [ packid ]",
+ "(to write label and boot program)" },
+ { "-R -B [-F] [-f disktab] [ -b xxboot [ -s bootxx ] ] disk protofile [ type ]",
+ "(to restore label and boot program)" },
+# else
+ { "-B [-F] [-f disktab] [ -b bootprog ] disk [ type ]",
+ "(to install boot program with existing on-disk label)" },
+ { "-w -B [-F] [-f disktab] [ -b bootprog ] disk type [ packid ]",
+ "(to write label and install boot program)" },
+ { "-R -B [-F] [-f disktab] [ -b bootprog ] disk protofile [ type ]",
+ "(to restore label and install boot program)" },
+# endif
+#endif
+ { "[-NW] disk",
+ "(to write disable/enable label)" },
+ { NULL,
+ NULL }
+ };
+ int i;
+
+ for (i = 0; usages[i].name; i++) {
+ (void) fputs(i ? "or " : "usage: ", stderr);
+ (void) fprintf(stderr, "%s %s", getprogname(), usages[i].name);
+ (void) fputs("\n\t", stderr);
+ (void) fprintf(stderr, "%s %s", getprogname(), usages[i].expn);
+ (void) fputs("\n", stderr);
+ }
+ exit(1);
+}
+
+static int
+getulong(const char *str, char sep, char **epp, unsigned long *ul,
+ unsigned long max)
+{
+ char *ep;
+
+ if (epp == NULL)
+ epp = &ep;
+
+ *ul = strtoul(str, epp, 10);
+
+ if ((*ul == ULONG_MAX && errno == ERANGE) || *ul > max)
+ return ERANGE;
+
+ if (*str == '\0' || (**epp != '\0' && **epp != sep &&
+ !isspace((unsigned char)**epp)))
+ return EFTYPE;
+
+ return 0;
+}
Index: sbin/disklabel/printlabel.c
===================================================================
RCS file: /cvsroot/src/sbin/disklabel/printlabel.c,v
retrieving revision 1.11
diff -u -r1.11 printlabel.c
--- sbin/disklabel/printlabel.c 28 Feb 2004 18:19:00 -0000 1.11
+++ sbin/disklabel/printlabel.c 22 Apr 2005 05:00:16 -0000
@@ -32,6 +32,10 @@
* SUCH DAMAGE.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: printlabel.c,v 1.11 2004/02/28 18:19:00 dsl Exp $");
@@ -41,7 +45,11 @@
#define DKTYPENAMES
#define FSTYPENAMES
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../sys/sys/disklabel.h"
+#else
#include <sys/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#include <stdio.h>
Index: sbin/fdisk/Makefile
===================================================================
RCS file: /cvsroot/src/sbin/fdisk/Makefile,v
retrieving revision 1.31
diff -u -r1.31 Makefile
--- sbin/fdisk/Makefile 20 Jan 2005 16:39:23 -0000 1.31
+++ sbin/fdisk/Makefile 22 Apr 2005 05:00:16 -0000
@@ -1,6 +1,8 @@
# $NetBSD: Makefile,v 1.31 2005/01/20 16:39:23 xtraeme Exp $
+.if (${HOSTPROG:U} == "")
SUBDIR= mbr
+.endif
.if (${MACHINE} == "i386" || \
${MACHINE} == "bebox" || \
@@ -34,4 +36,6 @@
.endif
.include <bsd.prog.mk>
+.if (${HOSTPROG:U} == "")
.include <bsd.subdir.mk>
+.endif
Index: sbin/fdisk/fdisk.c
===================================================================
RCS file: /cvsroot/src/sbin/fdisk/fdisk.c,v
retrieving revision 1.88
diff -u -r1.88 fdisk.c
--- sbin/fdisk/fdisk.c 20 Jan 2005 16:01:02 -0000 1.88
+++ sbin/fdisk/fdisk.c 22 Apr 2005 05:00:16 -0000
@@ -32,6 +32,10 @@
* Created.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
#include <sys/cdefs.h>
#ifndef lint
@@ -40,15 +44,20 @@
#define MBRPTYPENAMES
#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../sys/sys/disklabel.h"
+#include "../../sys/sys/bootblock.h"
+#else
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <sys/ioctl.h>
-#include <sys/param.h>
-#include <sys/stat.h>
#include <sys/sysctl.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#include <ctype.h>
-#include <disktab.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
@@ -59,12 +68,21 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "../../include/disktab.h"
+#include "../../include/util.h"
+#else
+#include <disktab.h>
#include <util.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#define DEFAULT_BOOTDIR "/usr/mdec"
#if defined(__i386__) || defined(__x86_64__)
+#if !HAVE_NBTOOL_CONFIG_H
#include <machine/cpu.h>
+#endif /* !HAVE_NBTOOL_CONFIG_H */
#define BOOTSEL
#define DEFAULT_BOOTCODE "mbr"
@@ -93,7 +111,7 @@
struct disklabel disklabel; /* disk parameters */
-uint cylinders, sectors, heads;
+u_int cylinders, sectors, heads;
daddr_t disksectors;
#define cylindersectors (heads * sectors)
@@ -122,9 +140,9 @@
#define OPTIONS "0123FSafiluvs:b:c:E:r:w:"
#endif
-uint dos_cylinders;
-uint dos_heads;
-uint dos_sectors;
+u_int dos_cylinders;
+u_int dos_heads;
+u_int dos_sectors;
daddr_t dos_disksectors;
#define dos_cylindersectors (dos_heads * dos_sectors)
#define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
@@ -132,7 +150,7 @@
#define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
#define DOSCYL(c) ((c) & 0xff)
#define SEC_IN_1M (1024 * 1024 / 512)
-#define SEC_TO_MB(sec) ((uint)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
+#define SEC_TO_MB(sec) ((u_int)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
#define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
#define MAXCYL 1024 /* Usual limit is 1023 */
@@ -141,7 +159,7 @@
int partition = -1;
int fd = -1, wfd = -1, *rfd = &fd;
-char *disk_file;
+char *disk_file = NULL;
char *disk_type = NULL;
int a_flag; /* set active partition */
@@ -161,7 +179,7 @@
int bootsize; /* actual size of bootcode */
int boot_installed; /* 1 if we've copied code into the mbr */
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
struct disklist *dl;
#endif
@@ -200,7 +218,7 @@
int ptn_id(const char *, int *);
int type_match(const void *, const void *);
const char *get_type(int);
-int get_mapping(int, uint *, uint *, uint *, unsigned long *);
+int get_mapping(int, u_int *, u_int *, u_int *, unsigned long *);
#ifdef BOOTSEL
daddr_t configure_bootsel(daddr_t);
void install_bootsel(int);
@@ -208,14 +226,31 @@
void set_default_boot(daddr_t);
#endif
+#if !HAVE_NBTOOL_CONFIG_H
+static void
+initvar_disk(const char **diskp)
+{
+ int mib[2];
+ size_t len;
+ char *root_device;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_ROOT_DEVICE;
+ if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1 ||
+ (root_device = malloc(len)) == NULL ||
+ sysctl(mib, 2, root_device, &len, NULL, 0) == -1)
+ return;
+
+ *diskp = root_device;
+}
+#endif /* HAVE_NBTOOL_CONFIG_H */
int
main(int argc, char *argv[])
{
struct stat sb;
- int ch, mib[2];
+ int ch;
size_t len;
- char *root_device;
char *cp;
int n;
#ifdef BOOTSEL
@@ -225,12 +260,9 @@
int csysid, cstart, csize; /* For the b_flag. */
- mib[0] = CTL_KERN;
- mib[1] = KERN_ROOT_DEVICE;
- if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 &&
- (root_device = malloc(len)) != NULL &&
- sysctl(mib, 2, root_device, &len, NULL, 0) != -1)
- disk = root_device;
+#if !HAVE_NBTOOL_CONFIG_H
+ initvar_disk(&disk);
+#endif /* HAVE_NBTOOL_CONFIG_H */
a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
v_flag = 0;
@@ -340,6 +372,13 @@
argc -= optind;
argv += optind;
+#if HAVE_NBTOOL_CONFIG_H
+ if (disk_file == NULL && argc > 0)
+ disk_file = argv[0];
+ else if (disk_file == NULL)
+ usage();
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
if (disk_type != NULL && getdiskbyname(disk_type) == NULL)
errx(EXIT_FAILURE, "bad disktype");
@@ -371,7 +410,7 @@
/* must have been a blank disk */
init_sector0(1);
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
get_geometry();
#else
intuit_translated_geometry();
@@ -455,7 +494,12 @@
"%*s[-0123 | -E num "
"[-s id/start/size[/bootmenu]]] \\\n"
"%*s[-t disktab] [-T disktype] \\\n"
- "%*s[-c bootcode] [-r|-w file] [device]\n"
+ "%*s[-c bootcode] "
+#if !HAVE_NBTOOL_CONFIG_H
+ "[-r|-w file] [device]\n"
+#else
+ "[-r|-w] file\n"
+#endif /* HAVE_NBTOOL_CONFIG_H */
"\t-a change active partition\n"
"\t-f force - not interactive\n"
"\t-i initialise MBR code\n"
@@ -586,7 +630,7 @@
static void
pr_cyls(daddr_t sector)
{
- ulong cyl, head, sect;
+ u_long cyl, head, sect;
cyl = sector / dos_cylindersectors;
sect = sector - cyl * dos_cylindersectors;
head = sect / dos_sectors;
@@ -876,6 +920,7 @@
diskname[len] = 0;
}
+#if !HAVE_NBTOOL_CONFIG_H
void
get_geometry(void)
{
@@ -923,13 +968,14 @@
/* Allright, allright, make a stupid guess.. */
intuit_translated_geometry();
}
-#endif
+#endif /* HAVE_NBTOOL_CONFIG_H */
+#endif /* defined(__i386__) || defined(__x86_64__) */
#ifdef BOOTSEL
daddr_t
get_default_boot(void)
{
- uint id;
+ u_int id;
int p;
if (le16toh(mboot.mbr_bootsel_magic) != MBR_BS_MAGIC)
@@ -1133,11 +1179,13 @@
daddr_t *off;
int num_bios_disks;
+#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
if (dl != NULL) {
num_bios_disks = dl->dl_nbiosdisks;
if (num_bios_disks > 8)
num_bios_disks = 8;
} else
+#endif
num_bios_disks = 8;
printf("\nBoot selector configuration:\n");
@@ -1219,8 +1267,8 @@
intuit_translated_geometry(void)
{
int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
- uint c1, h1, s1, c2, h2, s2;
- ulong a1, a2;
+ u_int c1, h1, s1, c2, h2, s2;
+ u_long a1, a2;
uint64_t num, denom;
/*
@@ -1234,7 +1282,7 @@
dos_sectors > MAXSECTOR) {
h1 = MAXHEAD - 1;
c1 = MAXCYL - 1;
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
if (dl != NULL) {
/* BIOS may use 256 heads or 1024 cylinders */
for (i = 0; i < dl->dl_nbiosdisks; i++) {
@@ -1316,7 +1364,7 @@
* Note: for simplicity, the returned sector is 0-based.
*/
int
-get_mapping(int i, uint *cylinder, uint *head, uint *sector,
+get_mapping(int i, u_int *cylinder, u_int *head, u_int *sector,
unsigned long *absolute)
{
struct mbr_partition *part = &mboot.mbr_parts[i / 2];
@@ -1419,7 +1467,7 @@
check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
{
int p;
- uint p_s, p_e;
+ u_int p_s, p_e;
if (sysid != 0) {
if (start < dos_sectors)
@@ -1538,7 +1586,7 @@
check_ext_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
{
int p;
- uint p_s, p_e;
+ u_int p_s, p_e;
if (sysid == 0)
return 0;
@@ -1994,7 +2042,7 @@
if (!yesno("Do you want to change our idea of what BIOS thinks?"))
return;
-#if defined(__i386__) || defined(__x86_64__)
+#if (defined(__i386__) || defined(__x86_64__)) && !HAVE_NBTOOL_CONFIG_H
if (dl != NULL) {
for (i = 0; i < dl->dl_nbiosdisks; i++) {
if (i == 0)
@@ -2048,6 +2096,13 @@
{
static char namebuf[MAXPATHLEN + 1];
+#if HAVE_NBTOOL_CONFIG_H
+ strlcpy(namebuf, disk_file, sizeof(namebuf));
+ if ((fd = open(disk_file, update ? O_RDWR : O_RDONLY, 0)) == -1) {
+ warn("%s", disk_file);
+ return -1;
+ }
+#else
fd = opendisk(disk, update && disk_file == NULL ? O_RDWR : O_RDONLY,
namebuf, sizeof(namebuf), 0);
if (fd < 0) {
@@ -2057,6 +2112,7 @@
warn("%s", namebuf);
return (-1);
}
+#endif /* HAVE_NBTOOL_CONFIG_H */
disk = namebuf;
if (get_params() == -1) {
close(fd);
@@ -2138,6 +2194,12 @@
disklabel.d_ncylinders = dos_cylinders;
disklabel.d_ntracks = dos_heads;
disklabel.d_nsectors = dos_sectors;
+#if HAVE_NBTOOL_CONFIG_H
+ } else {
+ warnx("no disklabel specified");
+ return -1;
+ }
+#else
} else if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
warn("DIOCGDEFLABEL");
if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
@@ -2145,6 +2207,7 @@
return (-1);
}
}
+#endif /* HAVE_NBTOOL_CONFIG_H */
disksectors = disklabel.d_secperunit;
cylinders = disklabel.d_ncylinders;
heads = disklabel.d_ntracks;
@@ -2175,8 +2238,8 @@
static int
validate_bootsel(struct mbr_bootsel *mbs)
{
- uint key = mbs->mbrbs_defkey;
- uint tmo;
+ u_int key = mbs->mbrbs_defkey;
+ u_int tmo;
int i;
if (v_flag)
@@ -2290,8 +2353,10 @@
* sector 0. (e.g. empty disk)
*/
flag = 1;
+#if !HAVE_NBTOOL_CONFIG_H
if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
warn("DIOCWLABEL");
+#endif /* HAVE_NBTOOL_CONFIG_H */
if (write_disk(0, &mboot) == -1) {
warn("Can't write fdisk partition table");
goto protect_label;
@@ -2311,9 +2376,11 @@
}
rval = 0;
protect_label:
+#if !HAVE_NBTOOL_CONFIG_H
flag = 0;
if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
warn("DIOCWLABEL");
+#endif /* HAVE_NBTOOL_CONFIG_H */
return rval;
}
@@ -2405,7 +2472,7 @@
int
ptn_id(const char *prompt, int *extended)
{
- uint acc = 0;
+ u_int acc = 0;
char *cp;
for (;; printf("%s is not a valid partition number.\n", lbuf)) {
Index: share/mk/bsd.own.mk
===================================================================
RCS file: /cvsroot/src/share/mk/bsd.own.mk,v
retrieving revision 1.438
diff -u -r1.438 bsd.own.mk
--- share/mk/bsd.own.mk 28 Feb 2005 02:28:09 -0000 1.438
+++ share/mk/bsd.own.mk 22 Apr 2005 05:00:20 -0000
@@ -233,6 +233,8 @@
TOOL_MAKEINFO= ${TOOLDIR}/bin/${_TOOL_PREFIX}makeinfo
TOOL_MAKEWHATIS= ${TOOLDIR}/bin/${_TOOL_PREFIX}makewhatis
TOOL_MDSETIMAGE= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-mdsetimage
+TOOL_DISKLABEL= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-disklabel
+TOOL_FDISK= ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-fdisk
TOOL_MENUC= MENUDEF=${TOOLDIR}/share/misc ${TOOLDIR}/bin/${_TOOL_PREFIX}menuc
TOOL_MIPSELF2ECOFF= ${TOOLDIR}/bin/${_TOOL_PREFIX}mips-elf2ecoff
TOOL_MKCSMAPPER= ${TOOLDIR}/bin/${_TOOL_PREFIX}mkcsmapper
@@ -650,6 +652,8 @@
INSTALL_LINK?= ${INSTALL} ${INSTPRIV} ${HRDLINK} ${RENAME}
INSTALL_SYMLINK?= ${INSTALL} ${INSTPRIV} ${SYMLINK} ${RENAME}
HOST_INSTALL_FILE?= ${INSTALL} ${COPY} ${PRESERVE} ${RENAME}
+HOST_INSTALL_DIR?= ${INSTALL} -d
+HOST_INSTALL_SYMLINK?= ${INSTALL} ${SYMLINK} ${RENAME}
.endif
#
Index: sys/arch/acorn26/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/acorn26/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/acorn26/include/disklabel.h 24 Mar 2002 15:46:56 -0000 1.1
+++ sys/arch/acorn26/include/disklabel.h 22 Apr 2005 05:00:23 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2002/03/24 15:46:56 bjh21 Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/acorn32/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/acorn32/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/acorn32/include/disklabel.h 5 Oct 2001 22:27:49 -0000 1.1
+++ sys/arch/acorn32/include/disklabel.h 22 Apr 2005 05:00:23 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2001/10/05 22:27:49 reinoud Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/algor/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/algor/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/algor/include/disklabel.h 28 May 2001 16:22:18 -0000 1.1
+++ sys/arch/algor/include/disklabel.h 22 Apr 2005 05:00:23 -0000
@@ -38,7 +38,11 @@
#define MAXPARTITIONS 8 /* number of partitions */
#define RAW_PART 2 /* raw partition: xx?c */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
/* Just a dummy */
struct cpu_disklabel {
Index: sys/arch/alpha/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/alpha/include/disklabel.h,v
retrieving revision 1.7
diff -u -r1.7 disklabel.h
--- sys/arch/alpha/include/disklabel.h 3 Apr 2002 06:16:01 -0000 1.7
+++ sys/arch/alpha/include/disklabel.h 22 Apr 2005 05:00:24 -0000
@@ -39,7 +39,11 @@
#define MAXPARTITIONS 8 /* number of partitions */
#define RAW_PART 2 /* raw partition: xx?c */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
/* Just a dummy */
struct cpu_disklabel {
Index: sys/arch/amd64/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/amd64/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/amd64/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.4
+++ sys/arch/amd64/include/disklabel.h 22 Apr 2005 05:00:24 -0000
@@ -44,10 +44,18 @@
* MAXPARTITIONS was increased.
*/
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct dkbad bad;
};
Index: sys/arch/arc/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/arc/include/disklabel.h,v
retrieving revision 1.7
diff -u -r1.7 disklabel.h
--- sys/arch/arc/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.7
+++ sys/arch/arc/include/disklabel.h 22 Apr 2005 05:00:25 -0000
@@ -43,9 +43,17 @@
#define OPENBSD_RAW_PART 2 /* raw partition: XX?c */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/arm/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/arm/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/arm/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.4
+++ sys/arch/arm/include/disklabel.h 22 Apr 2005 05:00:26 -0000
@@ -51,9 +51,15 @@
#define MAXPARTITIONS 8 /* number of partitions */
#define RAW_PART 2 /* raw partition: XX?c */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#include <nbsys/disklabel_acorn.h>
+#include <nbsys/bootblock.h>
+#else
#include <sys/dkbad.h>
#include <sys/disklabel_acorn.h>
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition mbrparts[MBR_PART_COUNT];
Index: sys/arch/bebox/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/bebox/include/disklabel.h,v
retrieving revision 1.6
diff -u -r1.6 disklabel.h
--- sys/arch/bebox/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.6
+++ sys/arch/bebox/include/disklabel.h 22 Apr 2005 05:00:27 -0000
@@ -39,9 +39,17 @@
#define RAW_PART 3 /* raw partition: XX?d (XXX) */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/cats/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/cats/include/disklabel.h,v
retrieving revision 1.3
diff -u -r1.3 disklabel.h
--- sys/arch/cats/include/disklabel.h 25 Nov 2001 15:55:55 -0000 1.3
+++ sys/arch/cats/include/disklabel.h 22 Apr 2005 05:00:27 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.3 2001/11/25 15:55:55 thorpej Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/cobalt/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/cobalt/include/disklabel.h,v
retrieving revision 1.7
diff -u -r1.7 disklabel.h
--- sys/arch/cobalt/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.7
+++ sys/arch/cobalt/include/disklabel.h 22 Apr 2005 05:00:27 -0000
@@ -39,10 +39,18 @@
#define RAW_PART 3
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/dreamcast/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/dreamcast/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/dreamcast/include/disklabel.h 11 Dec 2000 18:19:14 -0000 1.1
+++ sys/arch/dreamcast/include/disklabel.h 22 Apr 2005 05:00:28 -0000
@@ -2,6 +2,10 @@
#ifndef _DREAMCAST_DISKLABEL_H_
#define _DREAMCAST_DISKLABEL_H_
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsh3/disklabel.h>
+#else
#include <sh3/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#endif /* _DREAMCAST_DISKLABEL_H_ */
Index: sys/arch/evbarm/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/evbarm/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/evbarm/include/disklabel.h 25 Nov 2001 15:56:03 -0000 1.1
+++ sys/arch/evbarm/include/disklabel.h 22 Apr 2005 05:00:28 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2001/11/25 15:56:03 thorpej Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/evbmips/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/evbmips/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/evbmips/include/disklabel.h 7 Mar 2002 14:44:00 -0000 1.1
+++ sys/arch/evbmips/include/disklabel.h 22 Apr 2005 05:00:28 -0000
@@ -38,7 +38,11 @@
#define MAXPARTITIONS 16 /* number of partitions */
#define RAW_PART 2 /* raw partition: xx?c */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
/* Just a dummy */
struct cpu_disklabel {
Index: sys/arch/evbppc/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/evbppc/include/disklabel.h,v
retrieving revision 1.3
diff -u -r1.3 disklabel.h
--- sys/arch/evbppc/include/disklabel.h 19 Nov 2003 13:58:11 -0000 1.3
+++ sys/arch/evbppc/include/disklabel.h 22 Apr 2005 05:00:29 -0000
@@ -40,13 +40,21 @@
#define LABELSECTOR 1 /* sector containing label */
#define LABELOFFSET 0 /* offset of label in sector */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#else
#define LABELSECTOR 0 /* sector containing label */
#define LABELOFFSET 64 /* offset of label in sector */
#endif /* EVBPPC_HAS_MBR */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
#ifdef EVBPPC_HAS_MBR
Index: sys/arch/evbsh3/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/evbsh3/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/evbsh3/include/disklabel.h 13 Sep 1999 10:30:32 -0000 1.1
+++ sys/arch/evbsh3/include/disklabel.h 22 Apr 2005 05:00:29 -0000
@@ -2,6 +2,10 @@
#ifndef _EVBSH3_DISKLABEL_H_
#define _EVBSH3_DISKLABEL_H_
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsh3/disklabel.h>
+#else
#include <sh3/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#endif /* _EVBSH3_DISKLABEL_H_ */
Index: sys/arch/evbsh5/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/evbsh5/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/evbsh5/include/disklabel.h 5 Jul 2002 13:31:43 -0000 1.1
+++ sys/arch/evbsh5/include/disklabel.h 22 Apr 2005 05:00:29 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2002/07/05 13:31:43 scw Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsh5/disklabel.h>
+#else
#include <sh5/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/hp700/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hp700/include/disklabel.h,v
retrieving revision 1.5
diff -u -r1.5 disklabel.h
--- sys/arch/hp700/include/disklabel.h 28 Jul 2004 09:17:31 -0000 1.5
+++ sys/arch/hp700/include/disklabel.h 22 Apr 2005 05:00:29 -0000
@@ -41,9 +41,17 @@
#define MAXPARTITIONS 16 /* number of partitions */
#define RAW_PART 2 /* raw partition: xx?c */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct hp700_lifvol lifvol;
struct hp700_lifdir lifdir[HP700_LIF_NUMDIR];
Index: sys/arch/hpcarm/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hpcarm/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/hpcarm/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.4
+++ sys/arch/hpcarm/include/disklabel.h 22 Apr 2005 05:00:30 -0000
@@ -39,10 +39,18 @@
#define RAW_PART 3 /* raw partition: XX?d (XXX) */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/hpcmips/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hpcmips/include/disklabel.h,v
retrieving revision 1.6
diff -u -r1.6 disklabel.h
--- sys/arch/hpcmips/include/disklabel.h 8 Oct 2003 04:25:44 -0000 1.6
+++ sys/arch/hpcmips/include/disklabel.h 22 Apr 2005 05:00:30 -0000
@@ -53,10 +53,18 @@
((part) / OLDMAXPARTITIONS) * (__HPCMIPS_MAXDISKS * OLDMAXPARTITIONS))
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/hpcsh/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hpcsh/include/disklabel.h,v
retrieving revision 1.2
diff -u -r1.2 disklabel.h
--- sys/arch/hpcsh/include/disklabel.h 28 Jan 2001 03:31:31 -0000 1.2
+++ sys/arch/hpcsh/include/disklabel.h 22 Apr 2005 05:00:31 -0000
@@ -2,6 +2,10 @@
#ifndef _HPCSH_DISKLABEL_H_
#define _HPCSH_DISKLABEL_H_
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsh3/disklabel.h>
+#else
#include <sh3/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#endif /* _HPCSH_DISKLABEL_H_ */
Index: sys/arch/i386/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/include/disklabel.h,v
retrieving revision 1.12
diff -u -r1.12 disklabel.h
--- sys/arch/i386/include/disklabel.h 8 Oct 2003 04:25:45 -0000 1.12
+++ sys/arch/i386/include/disklabel.h 22 Apr 2005 05:00:31 -0000
@@ -53,10 +53,18 @@
((part) / OLDMAXPARTITIONS) * (__I386_MAXDISKS * OLDMAXPARTITIONS))
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct dkbad bad;
};
Index: sys/arch/ibmnws/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/ibmnws/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/ibmnws/include/disklabel.h 19 Oct 2003 03:33:50 -0000 1.1
+++ sys/arch/ibmnws/include/disklabel.h 22 Apr 2005 05:00:32 -0000
@@ -39,8 +39,13 @@
#define RAW_PART 2 /* raw partition: xx?c */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#include <nbsys/dkbad.h>
+#else
#include <sys/bootblock.h>
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
Index: sys/arch/iyonix/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/iyonix/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/iyonix/include/disklabel.h 13 Oct 2004 23:28:35 -0000 1.1
+++ sys/arch/iyonix/include/disklabel.h 22 Apr 2005 05:00:32 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2004/10/13 23:28:35 gavan Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/mmeye/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/mmeye/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/mmeye/include/disklabel.h 13 Sep 1999 10:30:55 -0000 1.1
+++ sys/arch/mmeye/include/disklabel.h 22 Apr 2005 05:00:34 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 1999/09/13 10:30:55 itojun Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsh3/disklabel.h>
+#else
#include <sh3/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/mvmeppc/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/mvmeppc/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/mvmeppc/include/disklabel.h 8 Oct 2003 04:25:45 -0000 1.4
+++ sys/arch/mvmeppc/include/disklabel.h 22 Apr 2005 05:00:34 -0000
@@ -39,9 +39,14 @@
#define RAW_PART 2 /* raw partition: xx?c */
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#include <nbsys/dkbad.h>
+#else
#include <sys/bootblock.h>
-
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
+
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/netwinder/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/netwinder/include/disklabel.h,v
retrieving revision 1.3
diff -u -r1.3 disklabel.h
--- sys/arch/netwinder/include/disklabel.h 25 Nov 2001 15:56:07 -0000 1.3
+++ sys/arch/netwinder/include/disklabel.h 22 Apr 2005 05:00:34 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.3 2001/11/25 15:56:07 thorpej Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/next68k/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/next68k/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/next68k/include/disklabel.h 15 Nov 2003 17:52:30 -0000 1.4
+++ sys/arch/next68k/include/disklabel.h 22 Apr 2005 05:00:35 -0000
@@ -32,7 +32,11 @@
#ifndef _MACHINE_DISKLABEL_H_
#define _MACHINE_DISKLABEL_H_
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#if 0 /* XXX the following doesn't work - still need to find a proper place for the NetBSD disklabel */
/*
Index: sys/arch/playstation2/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/playstation2/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/playstation2/include/disklabel.h 8 Oct 2003 04:25:45 -0000 1.4
+++ sys/arch/playstation2/include/disklabel.h 22 Apr 2005 05:00:36 -0000
@@ -39,10 +39,18 @@
#define RAW_PART 2
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/pmppc/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/pmppc/include/disklabel.h,v
retrieving revision 1.3
diff -u -r1.3 disklabel.h
--- sys/arch/pmppc/include/disklabel.h 8 Oct 2003 04:25:46 -0000 1.3
+++ sys/arch/pmppc/include/disklabel.h 22 Apr 2005 05:00:36 -0000
@@ -38,10 +38,13 @@
#define MAXPARTITIONS 16 /* number of partitions */
#define RAW_PART 2 /* raw partition: wdNc */
-/* Pull in MBR partition definitions. */
-#include <sys/bootblock.h>
-
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h> /* Pull in MBR partition definitions. */
+#include <nbsys/dkbad.h>
+#else
+#include <sys/bootblock.h> /* Pull in MBR partition definitions. */
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/prep/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/prep/include/disklabel.h,v
retrieving revision 1.5
diff -u -r1.5 disklabel.h
--- sys/arch/prep/include/disklabel.h 8 Oct 2003 04:25:46 -0000 1.5
+++ sys/arch/prep/include/disklabel.h 22 Apr 2005 05:00:37 -0000
@@ -38,10 +38,13 @@
#define MAXPARTITIONS 8 /* number of partitions */
#define RAW_PART 2 /* raw partition: xx?c */
-/* Pull in MBR partition definitions. */
-#include <sys/bootblock.h>
-
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h> /* Pull in MBR partition definitions. */
+#include <nbsys/dkbad.h>
+#else
+#include <sys/bootblock.h> /* Pull in MBR partition definitions. */
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/sandpoint/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sandpoint/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/sandpoint/include/disklabel.h 8 Oct 2003 04:25:46 -0000 1.4
+++ sys/arch/sandpoint/include/disklabel.h 22 Apr 2005 05:00:37 -0000
@@ -38,10 +38,13 @@
#define MAXPARTITIONS 16 /* number of partitions */
#define RAW_PART 2 /* raw partition: wdNc */
-/* Pull in MBR partition definitions. */
-#include <sys/bootblock.h>
-
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h> /* Pull in MBR partition definitions. */
+#include <nbsys/dkbad.h>
+#else
+#include <sys/bootblock.h> /* Pull in MBR partition definitions. */
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/sbmips/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sbmips/include/disklabel.h,v
retrieving revision 1.4
diff -u -r1.4 disklabel.h
--- sys/arch/sbmips/include/disklabel.h 25 Oct 2003 13:06:03 -0000 1.4
+++ sys/arch/sbmips/include/disklabel.h 22 Apr 2005 05:00:37 -0000
@@ -74,10 +74,18 @@
#ifdef __NetBSD__
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/sgimips/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sgimips/include/disklabel.h,v
retrieving revision 1.7
diff -u -r1.7 disklabel.h
--- sys/arch/sgimips/include/disklabel.h 24 Nov 2004 21:59:32 -0000 1.7
+++ sys/arch/sgimips/include/disklabel.h 22 Apr 2005 05:00:37 -0000
@@ -46,6 +46,10 @@
};
/* Pull in partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#endif /* _MACHINE_DISKLABEL_H_ */
Index: sys/arch/sh3/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sh3/include/disklabel.h,v
retrieving revision 1.5
diff -u -r1.5 disklabel.h
--- sys/arch/sh3/include/disklabel.h 8 Oct 2003 04:25:46 -0000 1.5
+++ sys/arch/sh3/include/disklabel.h 22 Apr 2005 05:00:38 -0000
@@ -39,10 +39,18 @@
#define RAW_PART 2
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/sh5/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sh5/include/disklabel.h,v
retrieving revision 1.2
diff -u -r1.2 disklabel.h
--- sys/arch/sh5/include/disklabel.h 8 Oct 2003 04:25:46 -0000 1.2
+++ sys/arch/sh5/include/disklabel.h 22 Apr 2005 05:00:38 -0000
@@ -44,10 +44,18 @@
#define RAW_PART 2
/* Pull in MBR partition definitions. */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
#ifndef __ASSEMBLER__
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct mbr_partition dosparts[MBR_PART_COUNT];
struct dkbad bad;
Index: sys/arch/shark/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/shark/include/disklabel.h,v
retrieving revision 1.1
diff -u -r1.1 disklabel.h
--- sys/arch/shark/include/disklabel.h 10 Feb 2002 01:57:14 -0000 1.1
+++ sys/arch/shark/include/disklabel.h 22 Apr 2005 05:00:38 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.1 2002/02/10 01:57:14 thorpej Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbarm/disklabel.h>
+#else
#include <arm/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/sparc64/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sparc64/include/disklabel.h,v
retrieving revision 1.2
diff -u -r1.2 disklabel.h
--- sys/arch/sparc64/include/disklabel.h 20 Jul 2002 11:52:21 -0000 1.2
+++ sys/arch/sparc64/include/disklabel.h 22 Apr 2005 05:00:38 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.2 2002/07/20 11:52:21 mrg Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsparc/disklabel.h>
+#else
#include <sparc/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/sun2/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/sun2/include/disklabel.h,v
retrieving revision 1.2
diff -u -r1.2 disklabel.h
--- sys/arch/sun2/include/disklabel.h 14 Jun 2001 13:12:50 -0000 1.2
+++ sys/arch/sun2/include/disklabel.h 22 Apr 2005 05:00:39 -0000
@@ -1,3 +1,7 @@
/* $NetBSD: disklabel.h,v 1.2 2001/06/14 13:12:50 fredette Exp $ */
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsun68k/disklabel.h>
+#else
#include <sun68k/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
Index: sys/arch/x68k/include/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/arch/x68k/include/disklabel.h,v
retrieving revision 1.2
diff -u -r1.2 disklabel.h
--- sys/arch/x68k/include/disklabel.h 30 Jun 1998 11:59:11 -0000 1.2
+++ sys/arch/x68k/include/disklabel.h 22 Apr 2005 05:00:40 -0000
@@ -51,7 +51,11 @@
unsigned long dp_size; /* partition size (1024bytes/block) */
};
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/dkbad.h>
+#else
#include <sys/dkbad.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
struct cpu_disklabel {
struct dos_partition dosparts[NDOSPART];
struct dkbad bad;
Index: sys/arch/x86/include/bootinfo.h
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/include/bootinfo.h,v
retrieving revision 1.7
diff -u -r1.7 bootinfo.h
--- sys/arch/x86/include/bootinfo.h 4 Feb 2005 22:03:53 -0000 1.7
+++ sys/arch/x86/include/bootinfo.h 22 Apr 2005 05:00:40 -0000
@@ -111,7 +111,11 @@
struct bi_memmap_entry entry[1]; /* var len */
};
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbsys/bootblock.h>
+#else
#include <sys/bootblock.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
/*
* Structure describing disk info as seen by the BIOS.
Index: sys/sys/disklabel.h
===================================================================
RCS file: /cvsroot/src/sys/sys/disklabel.h,v
retrieving revision 1.90
diff -u -r1.90 disklabel.h
--- sys/sys/disklabel.h 7 Jan 2005 18:29:15 -0000 1.90
+++ sys/sys/disklabel.h 22 Apr 2005 05:01:14 -0000
@@ -48,7 +48,11 @@
* label can describe and the number of the "whole disk" (raw)
* paritition are machine dependent.
*/
+#if HAVE_NBTOOL_CONFIG_H
+#include <nbmachine/disklabel.h>
+#else
#include <machine/disklabel.h>
+#endif /* HAVE_NBTOOL_CONFIG_H */
/*
* The absolute maximum number of disk partitions allowed.
@@ -80,10 +84,12 @@
* Translate between device numbers and major/disk unit/disk partition.
*/
#ifndef __HAVE_OLD_DISKLABEL
+#if !HAVE_NBTOOL_CONFIG_H
#define DISKUNIT(dev) (minor(dev) / MAXPARTITIONS)
#define DISKPART(dev) (minor(dev) % MAXPARTITIONS)
#define DISKMINOR(unit, part) \
(((unit) * MAXPARTITIONS) + (part))
+#endif /* !HAVE_NBTOOL_CONFIG_H */
#endif
#define MAKEDISKDEV(maj, unit, part) \
(makedev((maj), DISKMINOR((unit), (part))))
Index: tools/Makefile
===================================================================
RCS file: /cvsroot/src/tools/Makefile,v
retrieving revision 1.88
diff -u -r1.88 Makefile
--- tools/Makefile 27 Jan 2005 12:54:05 -0000 1.88
+++ tools/Makefile 22 Apr 2005 05:01:15 -0000
@@ -28,9 +28,10 @@
yacc .WAIT \
lex .WAIT \
${TOOLCHAIN_BITS} \
- asn1_compile cat cksum compile_et config db file installboot \
- lint1 makefs menuc mkcsmapper mkesdb mklocale mknod msgc pax \
- pwd_mkdb stat sunlabel zic
+ asn1_compile cat cksum compile_et config db disklabel \
+ fdisk file installboot lint1 \
+ makefs menuc mkcsmapper mkesdb mklocale mknod msgc \
+ pax pwd_mkdb stat sunlabel zic
.if ${MKMAN} != "no"
SUBDIR+= groff
Index: tools/Makefile.disklabel
===================================================================
RCS file: tools/Makefile.disklabel
diff -N tools/Makefile.disklabel
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tools/Makefile.disklabel 22 Apr 2005 05:01:15 -0000
@@ -0,0 +1,24 @@
+# $NetBSD$
+
+_ARCHDIR= ${.CURDIR}/../../sys/arch
+_SYSDIR= ${.CURDIR}/../../sys/sys
+_SUBDIR!= cd ${_ARCHDIR} && ${MAKE} -V SUBDIR
+
+_ARCH_INCS!= cd ${_ARCHDIR} && \
+ find ${_SUBDIR:O:u} \( -name disklabel.h -o -name dkbad.h \)
+
+_INCS= ${_SYSDIR}/bootblock.h \
+ ${_SYSDIR}/disklabel.h ${_SYSDIR}/disklabel_acorn.h \
+ ${_SYSDIR}/disklabel_gpt.h ${_SYSDIR}/dkbad.h
+
+HOST_CPPFLAGS+= -I${TOOLDIR}/include
+
+beforedepend:
+ ${HOST_INSTALL_DIR} ${TOOLDIR}/include/nb${MACHINE}
+ ${HOST_INSTALL_DIR} ${TOOLDIR}/include/nbsys
+ cd ${_ARCHDIR} && \
+ ${HOST_INSTALL_FILE} -c $(_ARCH_INCS) \
+ ${TOOLDIR}/include/nb${MACHINE}
+ cd ${TOOLDIR}/include && rm -f nbmachine && \
+ ${HOST_INSTALL_SYMLINK} nb${MACHINE} nbmachine
+ ${HOST_INSTALL_FILE} -c ${_INCS} ${TOOLDIR}/include/nbsys
Index: tools/disklabel/Makefile
===================================================================
RCS file: tools/disklabel/Makefile
diff -N tools/disklabel/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tools/disklabel/Makefile 22 Apr 2005 05:01:15 -0000
@@ -0,0 +1,10 @@
+# $NetBSD: Makefile,v 1.4 2003/11/12 02:18:27 matt Exp $
+
+HOSTPROGNAME= ${MACHINE_GNU_PLATFORM}-disklabel
+HOST_SRCDIR= sbin/disklabel
+HOST_SRCS= getcap.c disklabel.c
+
+.include "${.CURDIR}/../Makefile.disklabel"
+.include "${.CURDIR}/../Makefile.host"
+
+.PATH: ${.CURDIR}/../../lib/libc/gen
Index: tools/fdisk/Makefile
===================================================================
RCS file: tools/fdisk/Makefile
diff -N tools/fdisk/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tools/fdisk/Makefile 22 Apr 2005 05:01:15 -0000
@@ -0,0 +1,10 @@
+# $NetBSD: Makefile,v 1.4 2003/11/12 02:18:27 matt Exp $
+
+HOSTPROGNAME= ${MACHINE_GNU_PLATFORM}-fdisk
+HOST_SRCDIR= sbin/fdisk
+HOST_SRCS= getcap.c disklabel.c
+
+.include "${.CURDIR}/../Makefile.disklabel"
+.include "${.CURDIR}/../Makefile.host"
+
+.PATH: ${.CURDIR}/../../lib/libc/gen
--IJpNTDwzlM2Ie8A6--