summaryrefslogtreecommitdiffstats
path: root/sbin/fdisk
diff options
context:
space:
mode:
authorrnordier <rnordier@FreeBSD.org>1999-06-27 19:29:15 +0000
committerrnordier <rnordier@FreeBSD.org>1999-06-27 19:29:15 +0000
commitdb2d7d3fb63ac416da574c0197bb63210e06f314 (patch)
tree7a51bd632496d51e31d5632484d4219fe731045f /sbin/fdisk
parentbdf6f33fa047eab1f865f80a7647d95a0ad1cacc (diff)
downloadFreeBSD-src-db2d7d3fb63ac416da574c0197bb63210e06f314.zip
FreeBSD-src-db2d7d3fb63ac416da574c0197bb63210e06f314.tar.gz
Drop the embedded boot code in favour of a -B option which reads
the boot code from /boot/mbr, or elsewhere as defined by the revised "-b bootcode" option; use getopt(3); clarify usage(); partially revise man page; etc.
Diffstat (limited to 'sbin/fdisk')
-rw-r--r--sbin/fdisk/Makefile5
-rw-r--r--sbin/fdisk/fdisk.878
-rw-r--r--sbin/fdisk/fdisk.c159
3 files changed, 100 insertions, 142 deletions
diff --git a/sbin/fdisk/Makefile b/sbin/fdisk/Makefile
index 5efa9c5..3e408ca 100644
--- a/sbin/fdisk/Makefile
+++ b/sbin/fdisk/Makefile
@@ -1,9 +1,6 @@
-# @(#)Makefile 1.1 (Julian Elischer) 3/28/93
-#
-#
+# $Id: $
PROG= fdisk
-SRCS= fdisk.c
MAN8= fdisk.8
.include <bsd.prog.mk>
diff --git a/sbin/fdisk/fdisk.8 b/sbin/fdisk/fdisk.8
index cd82ed5..b6e0748 100644
--- a/sbin/fdisk/fdisk.8
+++ b/sbin/fdisk/fdisk.8
@@ -1,23 +1,21 @@
+.\" $Id: $
+.\"
.Dd October 4, 1996
.Dt FDISK 8
.\".Os BSD 4
.Sh NAME
.Nm fdisk
-.Nd DOS partition maintenance program
+.Nd PC partition table maintenance program
.Sh SYNOPSIS
.Nm fdisk
-.Op Fl i
-.Op Fl u
-.Op Fl a
-.Op Fl b
+.Op Fl Baitu
+.Op Fl b Ar bootcode
.Op Fl 1234
.Op Ar disk
.Bl -tag -width time
.Nm fdisk
-.Op Fl f Ar configfile
-.Op Fl i
-.Op Fl v
-.Op Fl t
+.Fl f Ar configfile
+.Op Fl itv
.Op Ar disk
.Sh PROLOGUE
In order for the BIOS to boot the kernel,
@@ -26,17 +24,15 @@ Sector 0 of the disk must contain boot code,
a partition table,
and a magic number.
BIOS partitions can be used to break the disk up into several pieces.
-The BIOS brings in sector 0
-(does it really use the code?)
-and verifies the magic number.
-It then searches the 4 BIOS partitions described by sector 0
-to determine which of them is
+The BIOS brings in sector 0 and verifies the magic number. The sector
+0 boot code then searches the partition table to determine which
+partition is marked
.Em active .
-This boot then brings in the secondary boot block from the
+This boot code then brings in the bootstrap from the
.Em active
-partition and runs it.
+partition and, if marked bootable, runs it.
Under DOS,
-you could have one or more partitions with one
+you can have one or more partitions with one
.Em active .
The DOS
.Nm
@@ -54,32 +50,18 @@ table. The second is used to write a partition table using a
and is designed to be used by other scripts/programs.
.Pp
Options are:
-.It Fl u
-Is used for updating (editing) sector 0 of the disk. Ignored if
-.Fl f
-is given.
-.It Fl i
-Initialize sector 0 of the disk. This implies
-.Fl u ,
-unless
+.It Fl B
+Reinitialize the boot code contained in sector 0 of the disk. Ignored
+if
.Fl f
is given.
.It Fl a
Change the active partition only. Ignored if
.Fl f
is given.
-.It Fl b
-Reinitialize the boot code contained in sector 0 of the disk. Ignored
-if
-.Fl f
-is given. It should be noted, however, that the standard boot code
-written by
-.Nm
-is suitable for booting only from hard drive 0 (BIOS drive 0x80).
-.It Fl 1234
-Operate on a single fdisk entry only. Ignored if
-.Fl f
-is given.
+.It Fl b Ar bootcode
+Get the boot code from the file
+.Ar bootcode .
.It Fl f Ar configfile
Set partition values using the file
.Ar configfile .
@@ -103,17 +85,31 @@ when
.Fl f
is used, you are not asked if you really want to write the partition
table (as you are in the interactive mode). Use with caution!
+.It Fl i
+Initialize sector 0 of the disk. This implies
+.Fl u ,
+unless
+.Fl f
+is given.
.It Fl t
Test mode; do not write partition values. Generally used with the
.Fl f
option to see what would be written to the partition table. Implies
.Fl v .
+.It Fl u
+Is used for updating (editing) sector 0 of the disk. Ignored if
+.Fl f
+is given.
.It Fl v
Be verbose. When
.Fl f
is used,
.Nm
prints out the partition table that is written to the disk.
+.It Fl 1234
+Operate on a single fdisk entry only. Ignored if
+.Fl f
+is given.
.El
.Pp
The final disk name can be provided as a
@@ -411,9 +407,17 @@ Example: to make partition 1 the active partition:
.El
.Pp
+.Sh FILES
+.Bl -tag -width /boot/mbr -compact
+.It Pa /boot/mbr
+The default boot code
+.El
.Sh SEE ALSO
.Xr disklabel 8
.Sh BUGS
+The default boot code will not necessarily handle all partition types
+correctly, in particular those introduced since MS-DOS 6.x.
+.Pp
The entire program should be made more user-friendly.
.Pp
Throughout this man page, the term
diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c
index eb9354c..f565095 100644
--- a/sbin/fdisk/fdisk.c
+++ b/sbin/fdisk/fdisk.c
@@ -26,7 +26,7 @@
#ifndef lint
static const char rcsid[] =
- "$Id: fdisk.c,v 1.27 1998/11/26 12:24:35 joerg Exp $";
+ "$Id: fdisk.c,v 1.28 1999/01/22 11:54:17 rnordier Exp $";
#endif /* not lint */
#include <sys/disklabel.h>
@@ -117,48 +117,15 @@ typedef struct cmd {
} CMD;
+static int B_flag = 0; /* replace boot code */
static int a_flag = 0; /* set active partition */
-static int b_flag = 0; /* replace boot code */
+static char *b_flag = NULL; /* path to boot code */
static int i_flag = 0; /* replace partition data */
static int u_flag = 0; /* update partition data */
static int t_flag = 0; /* test only, if f_flag is given */
static char *f_flag = NULL; /* Read config info from file */
static int v_flag = 0; /* Be verbose */
-static unsigned char bootcode[] = {
-0x33, 0xc0, 0xfa, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x8e, 0xc0, 0x8e, 0xd8, 0xfb, 0x8b, 0xf4, 0xbf,
-0x00, 0x06, 0xb9, 0x00, 0x02, 0xfc, 0xf3, 0xa4, 0xea, 0x1d, 0x06, 0x00, 0x00, 0xb0, 0x04, 0xbe,
-0xbe, 0x07, 0x80, 0x3c, 0x80, 0x74, 0x0c, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x75, 0xf4, 0xbe, 0xbd,
-0x06, 0xeb, 0x43, 0x8b, 0xfe, 0x8b, 0x14, 0x8b, 0x4c, 0x02, 0x83, 0xc6, 0x10, 0xfe, 0xc8, 0x74,
-0x0a, 0x80, 0x3c, 0x80, 0x75, 0xf4, 0xbe, 0xbd, 0x06, 0xeb, 0x2b, 0xbd, 0x05, 0x00, 0xbb, 0x00,
-0x7c, 0xb8, 0x01, 0x02, 0xcd, 0x13, 0x73, 0x0c, 0x33, 0xc0, 0xcd, 0x13, 0x4d, 0x75, 0xef, 0xbe,
-0x9e, 0x06, 0xeb, 0x12, 0x81, 0x3e, 0xfe, 0x7d, 0x55, 0xaa, 0x75, 0x07, 0x8b, 0xf7, 0xea, 0x00,
-0x7c, 0x00, 0x00, 0xbe, 0x85, 0x06, 0x2e, 0xac, 0x0a, 0xc0, 0x74, 0x06, 0xb4, 0x0e, 0xcd, 0x10,
-0xeb, 0xf4, 0xfb, 0xeb, 0xfe,
-'M', 'i', 's', 's', 'i', 'n', 'g', ' ',
- 'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
-'E', 'r', 'r', 'o', 'r', ' ', 'l', 'o', 'a', 'd', 'i', 'n', 'g', ' ',
- 'o', 'p', 'e', 'r', 'a', 't', 'i', 'n', 'g', ' ', 's', 'y', 's', 't', 'e', 'm', 0,
-'I', 'n', 'v', 'a', 'l', 'i', 'd', ' ',
- 'p', 'a', 'r', 't', 'i', 't', 'i', 'o', 'n', ' ', 't', 'a', 'b', 'l', 'e', 0,
-'A', 'u', 't', 'h', 'o', 'r', ' ', '-', ' ',
- 'S', 'i', 'e', 'g', 'm', 'a', 'r', ' ', 'S', 'c', 'h', 'm', 'i', 'd', 't', 0,0,0,
-
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
struct part_type
{
unsigned char type;
@@ -246,67 +213,49 @@ static int string(char *str, char **ans);
int
main(int argc, char *argv[])
{
- int i;
+ int c, i;
- for ( argv++ ; --argc ; argv++ ) { register char *token = *argv;
- if (*token++ != '-' || !*token)
+ while ((c = getopt(argc, argv, "Bab:f:ituv1234")) != -1)
+ switch (c) {
+ case 'B':
+ B_flag = 1;
break;
- else { register int flag;
- for ( ; (flag = *token++) ; ) {
- switch (flag) {
- case '1':
- partition = 1;
- break;
- case '2':
- partition = 2;
- break;
- case '3':
- partition = 3;
- break;
- case '4':
- partition = 4;
- break;
- case 'a':
- a_flag = 1;
- break;
- case 'b':
- b_flag = 1;
- break;
- case 'f':
- if (*token)
- {
- f_flag = token;
- token = "";
- }
- else
- {
- if (argc == 1)
- usage();
- --argc;
- f_flag = *++argv;
- }
- /*
- * u_flag is needed, because we're
- * writing to the disk.
- */
- u_flag = 1;
- break;
- case 'i':
- i_flag = 1;
- case 'u':
- u_flag = 1;
- break;
- case 't':
- t_flag = 1;
- case 'v':
- v_flag = 1;
- break;
- default:
- usage();
- }
- }
+ case 'a':
+ a_flag = 1;
+ break;
+ case 'b':
+ b_flag = optarg;
+ break;
+ case 'f':
+ f_flag = optarg;
+ break;
+ case 'i':
+ i_flag = 1;
+ break;
+ case 't':
+ t_flag = 1;
+ break;
+ case 'u':
+ u_flag = 1;
+ break;
+ case 'v':
+ v_flag = 1;
+ break;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ partition = c - '0';
+ break;
+ default:
+ usage();
}
- }
+ if (f_flag || i_flag)
+ u_flag = 1;
+ if (t_flag)
+ v_flag = 1;
+ argc -= optind;
+ argv += optind;
if (argc > 0)
{
@@ -325,7 +274,7 @@ main(int argc, char *argv[])
}
else
{
- int i, rv = 0;
+ int rv = 0;
for(i = 0; disks[i]; i++)
{
@@ -385,10 +334,10 @@ main(int argc, char *argv[])
if (u_flag || a_flag)
change_active(partition);
- if (b_flag)
+ if (B_flag)
change_code();
- if (u_flag || a_flag || b_flag) {
+ if (u_flag || a_flag || B_flag) {
if (!t_flag)
{
printf("\nWe haven't changed the partition table yet. ");
@@ -413,8 +362,9 @@ main(int argc, char *argv[])
static void
usage()
{
- fprintf(stderr,
- "usage: fdisk {-a|-b|-i|-u} [-f configfile [-t] [-v]] [-{1,2,3,4}] [disk]\n");
+ fprintf(stderr, "%s%s",
+ "usage: fdisk [-Baitu] [-b bootcode] [-1234] [disk]\n",
+ " fdisk -f configfile [-itv] [disk]\n");
exit(1);
}
@@ -472,7 +422,14 @@ print_part(int i)
static void
init_boot(void)
{
- memcpy(mboot.bootinst, bootcode, sizeof(bootcode));
+ const char *fname;
+ int fd;
+
+ fname = b_flag ? b_flag : "/boot/mbr";
+ if ((fd = open(fname, O_RDONLY)) == -1 ||
+ read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
+ close(fd))
+ err(1, "%s", fname);
mboot.signature = BOOT_MAGIC;
}
@@ -666,7 +623,7 @@ struct stat st;
if ( !(st.st_mode & S_IFCHR) )
warnx("device %s is not character special", disk);
if ((fd = open(disk,
- a_flag || b_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
+ a_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
if(errno == ENXIO)
return -2;
warnx("can't open device %s", disk);
OpenPOWER on IntegriCloud