fdisk.c revision 95002
1139825Simp/*
236270Swpaul * Mach Operating System
336270Swpaul * Copyright (c) 1992 Carnegie Mellon University
436270Swpaul * All Rights Reserved.
536270Swpaul *
636270Swpaul * Permission to use, copy, modify and distribute this software and its
736270Swpaul * documentation is hereby granted, provided that both the copyright
836270Swpaul * notice and this permission notice appear in all copies of the
936270Swpaul * software, derivative works or modified versions, and any portions
1036270Swpaul * thereof, and that both notices appear in supporting documentation.
1136270Swpaul *
1236270Swpaul * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1336270Swpaul * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1436270Swpaul * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1536270Swpaul *
1636270Swpaul * Carnegie Mellon requests users of this software to return to
1736270Swpaul *
1836270Swpaul *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
1936270Swpaul *  School of Computer Science
2036270Swpaul *  Carnegie Mellon University
2136270Swpaul *  Pittsburgh PA 15213-3890
2236270Swpaul *
2336270Swpaul * any improvements or extensions that they make and grant Carnegie Mellon
2436270Swpaul * the rights to redistribute these changes.
2536270Swpaul */
2636270Swpaul
2736270Swpaul#ifndef lint
2836270Swpaulstatic const char rcsid[] =
2936270Swpaul  "$FreeBSD: head/sbin/fdisk/fdisk.c 95002 2002-04-18 16:25:18Z trhodes $";
3036270Swpaul#endif /* not lint */
3136270Swpaul
3236270Swpaul#include <sys/disklabel.h>
33122678Sobrien#include <sys/param.h>
34122678Sobrien#include <sys/stat.h>
35122678Sobrien#include <sys/mount.h>
3636270Swpaul#include <ctype.h>
3736270Swpaul#include <fcntl.h>
3836270Swpaul#include <err.h>
3936270Swpaul#include <errno.h>
4036270Swpaul#include <paths.h>
4136270Swpaul#include <regex.h>
4239583Swpaul#include <stdio.h>
4336270Swpaul#include <stdlib.h>
4436270Swpaul#include <string.h>
4536270Swpaul#include <unistd.h>
4636270Swpaul
4739583Swpaulint iotest;
4836270Swpaul
4936270Swpaul#define LBUF 100
5036270Swpaulstatic char lbuf[LBUF];
5136270Swpaul
5236270Swpaul#define MBRSIGOFF	510
5336270Swpaul
5436270Swpaul/*
5536270Swpaul *
5636270Swpaul * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
5736270Swpaul *
5839583Swpaul * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
5936270Swpaul *	Copyright (c) 1989	Robert. V. Baron
6036270Swpaul *	Created.
6136270Swpaul */
6236270Swpaul
6336270Swpaul#define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
6436270Swpaul
6536270Swpaul#define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
6639583Swpaul
6739583Swpaul#define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
6839583Swpaul#define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
6939583Swpaulstatic int secsize = 0;		/* the sensed sector size */
7039583Swpaul
7139583Swpaulstatic char *disk;
7239583Swpaul
7339583Swpaulstatic struct disklabel disklabel;		/* disk parameters */
7439583Swpaul
7536270Swpaulstatic int cyls, sectors, heads, cylsecs, disksecs;
7636270Swpaul
7739583Swpaulstruct mboot {
7836270Swpaul	unsigned char padding[2]; /* force the longs to be long aligned */
7936270Swpaul  	unsigned char *bootinst;  /* boot code */
8036270Swpaul  	off_t bootinst_size;
8136270Swpaul	struct	dos_partition parts[4];
8236270Swpaul};
8336270Swpaul
8436270Swpaulstatic struct mboot mboot;
8536270Swpaulstatic int fd;
8636270Swpaul
8736270Swpaul#define ACTIVE 0x80
8836270Swpaul#define BOOT_MAGIC 0xAA55
8936270Swpaul
9036270Swpaulstatic uint dos_cyls;
9136270Swpaulstatic uint dos_heads;
9236270Swpaulstatic uint dos_sectors;
9336270Swpaulstatic uint dos_cylsecs;
9436270Swpaul
9536270Swpaul#define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
9636270Swpaul#define DOSCYL(c)	(c & 0xff)
9736270Swpaul
9836270Swpaul#define MAX_ARGS	10
9936270Swpaul
10036270Swpaulstatic int	current_line_number;
10136270Swpaul
10236270Swpaulstatic int	geom_processed = 0;
10336270Swpaulstatic int	part_processed = 0;
10436270Swpaulstatic int	active_processed = 0;
10536270Swpaul
10636270Swpaultypedef struct cmd {
10736270Swpaul    char		cmd;
10836270Swpaul    int			n_args;
10936270Swpaul    struct arg {
11036270Swpaul	char	argtype;
11136270Swpaul	int	arg_val;
11236270Swpaul    }			args[MAX_ARGS];
11336270Swpaul} CMD;
11436270Swpaul
11536270Swpaulstatic int B_flag  = 0;		/* replace boot code */
11636270Swpaulstatic int I_flag  = 0;		/* use entire disk for FreeBSD */
11736270Swpaulstatic int a_flag  = 0;		/* set active partition */
11836270Swpaulstatic char *b_flag = NULL;	/* path to boot code */
11936270Swpaulstatic int i_flag  = 0;		/* replace partition data */
12036270Swpaulstatic int u_flag  = 0;		/* update partition data */
12136270Swpaulstatic int s_flag  = 0;		/* Print a summary and exit */
12236270Swpaulstatic int t_flag  = 0;		/* test only */
12336270Swpaulstatic char *f_flag = NULL;	/* Read config info from file */
12436270Swpaulstatic int v_flag  = 0;		/* Be verbose */
12536270Swpaul
12636270Swpaulstatic struct part_type
12736270Swpaul{
12836270Swpaul unsigned char type;
12936270Swpaul	const char *name;
13036270Swpaul} part_types[] = {
13136270Swpaul	 {0x00, "unused"}
13236270Swpaul	,{0x01, "Primary DOS with 12 bit FAT"}
13336270Swpaul	,{0x02, "XENIX / filesystem"}
13436270Swpaul	,{0x03, "XENIX /usr filesystem"}
13536270Swpaul	,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
13636270Swpaul	,{0x05, "Extended DOS"}
13736270Swpaul	,{0x06, "Primary 'big' DOS (>= 32MB)"}
13836270Swpaul	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
13936270Swpaul	,{0x08, "AIX filesystem or SplitDrive"}
14036270Swpaul	,{0x09, "AIX boot partition or Coherent"}
14136270Swpaul	,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
14236270Swpaul	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
14336270Swpaul	,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
14436270Swpaul	,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
14536270Swpaul	,{0x0F, "Extended DOS (LBA)"}
14636270Swpaul	,{0x10, "OPUS"}
14736270Swpaul	,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
14836270Swpaul	,{0x12, "Compaq diagnostics"}
14936270Swpaul	,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
15036270Swpaul	,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
15136270Swpaul	,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
15236270Swpaul	,{0x18, "AST Windows swapfile"}
15336270Swpaul	,{0x24, "NEC DOS"}
15436270Swpaul	,{0x3C, "PartitionMagic recovery"}
15536270Swpaul	,{0x39, "plan9"}
15636270Swpaul	,{0x40, "VENIX 286"}
15736270Swpaul	,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
15836270Swpaul	,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
15936270Swpaul	,{0x43, "Linux native (sharing disk with DRDOS)"}
16036270Swpaul	,{0x4D, "QNX 4.2 Primary"}
16136270Swpaul	,{0x4E, "QNX 4.2 Secondary"}
16236270Swpaul	,{0x4F, "QNX 4.2 Tertiary"}
16336270Swpaul	,{0x50, "DM (disk manager)"}
16436270Swpaul	,{0x51, "DM6 Aux1 (or Novell)"}
16536270Swpaul	,{0x52, "CP/M or Microport SysV/AT"}
16636270Swpaul	,{0x53, "DM6 Aux3"}
16736270Swpaul	,{0x54, "DM6"}
16836270Swpaul	,{0x55, "EZ-Drive (disk manager)"}
16936270Swpaul	,{0x56, "Golden Bow (disk manager)"}
17036270Swpaul	,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
17136270Swpaul	,{0x61, "SpeedStor"}
17236270Swpaul	,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
17336270Swpaul	,{0x64, "Novell Netware/286 2.xx"}
17436270Swpaul	,{0x65, "Novell Netware/386 3.xx"}
17536270Swpaul	,{0x70, "DiskSecure Multi-Boot"}
17636270Swpaul	,{0x75, "PCIX"}
17736270Swpaul	,{0x77, "QNX4.x"}
17836270Swpaul	,{0x78, "QNX4.x 2nd part"}
17936270Swpaul	,{0x79, "QNX4.x 3rd part"}
18036270Swpaul	,{0x80, "Minix until 1.4a"}
18136270Swpaul	,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
18236270Swpaul	,{0x82, "Linux swap or Solaris x86"}
18336270Swpaul	,{0x83, "Linux native"}
18436270Swpaul	,{0x84, "OS/2 hidden C: drive"}
18536270Swpaul	,{0x85, "Linux extended"}
186129878Sphk	,{0x86, "NTFS volume set??"}
18736270Swpaul	,{0x87, "NTFS volume set??"}
18836270Swpaul	,{0x93, "Amoeba filesystem"}
18936270Swpaul	,{0x94, "Amoeba bad block table"}
19036270Swpaul	,{0x9F, "BSD/OS"}
19136270Swpaul	,{0xA0, "Suspend to Disk"}
19236270Swpaul	,{0xA5, "FreeBSD/NetBSD/386BSD"}
19336270Swpaul	,{0xA6, "OpenBSD"}
19436270Swpaul	,{0xA7, "NeXTSTEP"}
19536270Swpaul	,{0xA9, "NetBSD"}
19636270Swpaul	,{0xB7, "BSDI BSD/386 filesystem"}
19736270Swpaul	,{0xB8, "BSDI BSD/386 swap"}
19836270Swpaul	,{0xC1, "DRDOS/sec with 12-bit FAT"}
19945155Swpaul	,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
20045155Swpaul	,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
20145155Swpaul	,{0xC7, "Syrinx"}
20248992Swpaul	,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
20348992Swpaul	,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
20448992Swpaul	,{0xE3, "DOS R/O or SpeedStor"}
20536270Swpaul	,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
20650462Swpaul	,{0xEB, "BeOS filesystem"}
20750462Swpaul	,{0xEE, "EFI GPT"}
20850462Swpaul	,{0xEF, "EFI System Partition"}
209119288Simp	,{0xF1, "SpeedStor"}
210119288Simp	,{0xF2, "DOS 3.3+ Secondary"}
21136270Swpaul	,{0xF4, "SpeedStor large partition"}
21239957Swpaul	,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
21339957Swpaul	,{0xFF, "Xenix bad blocks table"}
21439957Swpaul};
21539957Swpaul
21639957Swpaulstatic void print_s0(int which);
21739957Swpaulstatic void print_part(int i);
21839957Swpaulstatic void init_sector0(unsigned long start);
21936270Swpaulstatic void init_boot(void);
22036270Swpaulstatic void change_part(int i);
221113506Smdoddstatic void print_params(void);
222113506Smdoddstatic void change_active(int which);
22359758Speterstatic void change_code(void);
22459758Speterstatic void get_params_to_use(void);
22551089Speterstatic char *get_rootdisk(void);
22650462Swpaulstatic void dos(struct dos_partition *partp);
22750462Swpaulstatic int open_disk(int flag);
22836270Swpaulstatic ssize_t read_disk(off_t sector, void *buf);
22936270Swpaulstatic ssize_t write_disk(off_t sector, void *buf);
23036270Swpaulstatic int get_params(void);
23136270Swpaulstatic int read_s0(void);
23236270Swpaulstatic int write_s0(void);
23336270Swpaulstatic int ok(const char *str);
23436270Swpaulstatic int decimal(const char *str, int *num, int deflt);
23536270Swpaulstatic const char *get_type(int type);
23636270Swpaulstatic int read_config(char *config_file);
23736270Swpaulstatic void reset_boot(void);
23836270Swpaulstatic int sanitize_partition(struct dos_partition *);
23936270Swpaulstatic void usage(void);
24036270Swpaul
24136270Swpaulint
24236270Swpaulmain(int argc, char *argv[])
24336270Swpaul{
24436270Swpaul	struct	stat sb;
24536270Swpaul	int	c, i;
24636270Swpaul	int	partition = -1;
24736270Swpaul	struct	dos_partition *partp;
24836270Swpaul
24937626Swpaul	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
25037626Swpaul		switch (c) {
25137626Swpaul		case 'B':
25237626Swpaul			B_flag = 1;
25337626Swpaul			break;
25437626Swpaul		case 'I':
25537626Swpaul			I_flag = 1;
25637626Swpaul			break;
25737626Swpaul		case 'a':
25837626Swpaul			a_flag = 1;
25937626Swpaul			break;
26037626Swpaul		case 'b':
26136270Swpaul			b_flag = optarg;
26236270Swpaul			break;
26336270Swpaul		case 'f':
26492739Salfred			f_flag = optarg;
26592739Salfred			break;
26692739Salfred		case 'i':
26792739Salfred			i_flag = 1;
26892739Salfred			break;
26992739Salfred		case 's':
27092739Salfred			s_flag = 1;
27192739Salfred			break;
27292739Salfred		case 't':
27336270Swpaul			t_flag = 1;
27492739Salfred			break;
27592739Salfred		case 'u':
27692739Salfred			u_flag = 1;
27792739Salfred			break;
27836270Swpaul		case 'v':
27992739Salfred			v_flag = 1;
28092739Salfred			break;
28192739Salfred		case '1':
28292739Salfred		case '2':
28392739Salfred		case '3':
28492739Salfred		case '4':
28592739Salfred			partition = c - '0';
28692739Salfred			break;
28792739Salfred		default:
28836270Swpaul			usage();
28992739Salfred		}
29092739Salfred	if (f_flag || i_flag)
29192739Salfred		u_flag = 1;
29236270Swpaul	if (t_flag)
29392739Salfred		v_flag = 1;
29492739Salfred	argc -= optind;
29592739Salfred	argv += optind;
29692739Salfred
29792739Salfred	if (argc == 0) {
29892739Salfred		disk = get_rootdisk();
29992739Salfred	} else {
30036270Swpaul		if (stat(argv[0], &sb) == 0) {
30192739Salfred			/* OK, full pathname given */
302123289Sobrien			disk = argv[0];
30392739Salfred		} else if (errno == ENOENT) {
30492739Salfred			/* Try prepending "/dev" */
30592739Salfred			asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
30692739Salfred			if (disk == NULL)
30792739Salfred				errx(1, "out of memory");
30892739Salfred		} else {
30936270Swpaul			/* other stat error, let it fail below */
31092739Salfred			disk = argv[0];
31192739Salfred		}
31292739Salfred	}
31392739Salfred	if (open_disk(u_flag) < 0)
31492739Salfred		err(1, "cannot open disk %s", disk);
31592739Salfred
31692739Salfred	/* (abu)use mboot.bootinst to probe for the sector size */
31792739Salfred	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
31892739Salfred		err(1, "cannot allocate buffer to determine disk sector size");
31992739Salfred	read_disk(0, mboot.bootinst);
32039583Swpaul	free(mboot.bootinst);
32149010Swpaul	mboot.bootinst = NULL;
32249010Swpaul
32349010Swpaul	if (s_flag) {
32449010Swpaul		if (read_s0())
32549010Swpaul			err(1, "read_s0");
32649010Swpaul		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
32749010Swpaul		    dos_sectors);
32849010Swpaul		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
32948992Swpaul		for (i = 0; i < NDOSPART; i++) {
33048992Swpaul			partp = ((struct dos_partition *) &mboot.parts) + i;
33148992Swpaul			if (partp->dp_start == 0 && partp->dp_size == 0)
33248992Swpaul				continue;
33348992Swpaul			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
33448992Swpaul			    (u_long) partp->dp_start,
33550462Swpaul			    (u_long) partp->dp_size, partp->dp_typ,
33650462Swpaul			    partp->dp_flag);
33750462Swpaul		}
33850462Swpaul		exit(0);
33950462Swpaul	}
34050462Swpaul
34150462Swpaul	printf("******* Working on device %s *******\n",disk);
34250462Swpaul
34350462Swpaul	if (I_flag) {
34450462Swpaul		read_s0();
34548992Swpaul		reset_boot();
34648992Swpaul		partp = (struct dos_partition *) (&mboot.parts[0]);
34748992Swpaul		partp->dp_typ = DOSPTYP_386BSD;
34848992Swpaul		partp->dp_flag = ACTIVE;
34951455Swpaul		partp->dp_start = dos_sectors;
35048992Swpaul		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
35148992Swpaul		    dos_sectors;
35248992Swpaul		dos(partp);
35348992Swpaul		if (v_flag)
35448992Swpaul			print_s0(-1);
35548992Swpaul		if (!t_flag)
356113506Smdodd			write_s0();
35751473Swpaul		exit(0);
35848992Swpaul	}
35939583Swpaul	if (f_flag) {
36041656Swpaul	    if (read_s0() || i_flag)
36141656Swpaul		reset_boot();
36239583Swpaul	    if (!read_config(f_flag))
36339583Swpaul		exit(1);
36439583Swpaul	    if (v_flag)
36539583Swpaul		print_s0(-1);
36639583Swpaul	    if (!t_flag)
36739583Swpaul		write_s0();
36841656Swpaul	} else {
36941656Swpaul	    if(u_flag)
37039583Swpaul		get_params_to_use();
37139583Swpaul	    else
37239583Swpaul		print_params();
37339583Swpaul
37439583Swpaul	    if (read_s0())
37539583Swpaul		init_sector0(dos_sectors);
37641656Swpaul
37741656Swpaul	    printf("Media sector size is %d\n", secsize);
37839583Swpaul	    printf("Warning: BIOS sector numbering starts with sector 1\n");
37939583Swpaul	    printf("Information from DOS bootblock is:\n");
38039583Swpaul	    if (partition == -1)
38139583Swpaul		for (i = 1; i <= NDOSPART; i++)
38239583Swpaul		    change_part(i);
38339583Swpaul	    else
38441656Swpaul		change_part(partition);
38541656Swpaul
38641656Swpaul	    if (u_flag || a_flag)
38739583Swpaul		change_active(partition);
38839583Swpaul
38939583Swpaul	    if (B_flag)
39039583Swpaul		change_code();
39139583Swpaul
39239583Swpaul	    if (u_flag || a_flag || B_flag) {
39339583Swpaul		if (!t_flag) {
39441656Swpaul		    printf("\nWe haven't changed the partition table yet.  ");
39541656Swpaul		    printf("This is your last chance.\n");
39641656Swpaul		}
39739583Swpaul		print_s0(-1);
39839583Swpaul		if (!t_flag) {
39939583Swpaul		    if (ok("Should we write new partition table?"))
40039583Swpaul			write_s0();
40139583Swpaul			} else {
40239583Swpaul		    printf("\n-t flag specified -- partition table not written.\n");
40339583Swpaul		}
40441656Swpaul	    }
40541656Swpaul	}
40641656Swpaul
40739583Swpaul	exit(0);
40839583Swpaul}
40939583Swpaul
41039583Swpaulstatic void
41139583Swpaulusage()
41239583Swpaul{
413102336Salfred	fprintf(stderr, "%s%s",
414102336Salfred		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
41541656Swpaul 		"       fdisk -f configfile [-itv] [disk]\n");
41641656Swpaul        exit(1);
41741656Swpaul}
41839583Swpaul
41939583Swpaulstatic void
42039583Swpaulprint_s0(int which)
42139583Swpaul{
42239583Swpaul	int	i;
42339583Swpaul
42439583Swpaul	print_params();
42539583Swpaul	printf("Information from DOS bootblock is:\n");
42639583Swpaul	if (which == -1)
42739583Swpaul		for (i = 1; i <= NDOSPART; i++)
42839583Swpaul			printf("%d: ", i), print_part(i);
429102336Salfred	else
430102336Salfred		print_part(which);
43141656Swpaul}
43241656Swpaul
43341656Swpaulstatic struct dos_partition mtpart;
43439583Swpaul
43539583Swpaulstatic void
43639583Swpaulprint_part(int i)
43739583Swpaul{
43839583Swpaul	struct	  dos_partition *partp;
43939583Swpaul	u_int64_t part_mb;
44039583Swpaul
44139583Swpaul	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
44239583Swpaul
44339583Swpaul	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
44439583Swpaul		printf("<UNUSED>\n");
44539583Swpaul		return;
44641656Swpaul	}
44741656Swpaul	/*
44841656Swpaul	 * Be careful not to overflow.
44939583Swpaul	 */
45039583Swpaul	part_mb = partp->dp_size;
45139583Swpaul	part_mb *= secsize;
45239583Swpaul	part_mb /= (1024 * 1024);
45339583Swpaul	printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
45439583Swpaul	    get_type(partp->dp_typ));
45539583Swpaul	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
45639583Swpaul		(u_long)partp->dp_start,
45739583Swpaul		(u_long)partp->dp_size,
45839583Swpaul		part_mb,
45939583Swpaul		partp->dp_flag,
46039583Swpaul		partp->dp_flag == ACTIVE ? " (active)" : "");
46141656Swpaul	printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
46241656Swpaul		,DPCYL(partp->dp_scyl, partp->dp_ssect)
46341656Swpaul		,partp->dp_shd
46439583Swpaul		,DPSECT(partp->dp_ssect)
46539583Swpaul		,DPCYL(partp->dp_ecyl, partp->dp_esect)
46639583Swpaul		,partp->dp_ehd
46739583Swpaul		,DPSECT(partp->dp_esect));
46839583Swpaul}
46939583Swpaul
47039583Swpaul
47139583Swpaulstatic void
47239583Swpaulinit_boot(void)
47339583Swpaul{
47439583Swpaul	const char *fname;
47536270Swpaul	int fdesc, n;
47636270Swpaul	struct stat sb;
47736270Swpaul
47839583Swpaul	fname = b_flag ? b_flag : "/boot/mbr";
47939583Swpaul	if ((fdesc = open(fname, O_RDONLY)) == -1 ||
48041656Swpaul	    fstat(fdesc, &sb) == -1)
48136270Swpaul		err(1, "%s", fname);
48236270Swpaul	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
48336270Swpaul		errx(1, "%s: length must be a multiple of sector size", fname);
48436270Swpaul	if (mboot.bootinst != NULL)
48536270Swpaul		free(mboot.bootinst);
48636270Swpaul	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
48739583Swpaul		errx(1, "%s: unable to allocate read buffer", fname);
48836270Swpaul	if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
48936270Swpaul	    close(fdesc))
49036270Swpaul		err(1, "%s", fname);
49136270Swpaul	if (n != mboot.bootinst_size)
49236270Swpaul		errx(1, "%s: short read", fname);
49336270Swpaul}
49439583Swpaul
49536270Swpaul
49639583Swpaulstatic void
49736270Swpaulinit_sector0(unsigned long start)
49839583Swpaul{
49939583Swpaul	struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
50039583Swpaul
50139583Swpaul	init_boot();
50236270Swpaul
50336270Swpaul	partp->dp_typ = DOSPTYP_386BSD;
50436270Swpaul	partp->dp_flag = ACTIVE;
50536270Swpaul	start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
50636270Swpaul	if(start == 0)
50739583Swpaul		start = dos_sectors;
50836270Swpaul	partp->dp_start = start;
50936270Swpaul	partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
51036270Swpaul
51136270Swpaul	dos(partp);
51239583Swpaul}
51339583Swpaul
51439583Swpaulstatic void
51536270Swpaulchange_part(int i)
51636270Swpaul{
51736270Swpaul	struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
51836270Swpaul
51936270Swpaul    printf("The data for partition %d is:\n", i);
52036270Swpaul    print_part(i);
52136270Swpaul
52239583Swpaul    if (u_flag && ok("Do you want to change it?")) {
52339583Swpaul	int tmp;
52441656Swpaul
52536270Swpaul	if (i_flag) {
52636270Swpaul		bzero((char *)partp, sizeof (struct dos_partition));
52736270Swpaul		if (i == 4) {
52836270Swpaul			init_sector0(1);
529105599Sbrooks			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
53036270Swpaul			print_part(i);
53139583Swpaul		}
53239583Swpaul	}
53336270Swpaul
53439583Swpaul	do {
53536270Swpaul		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
53636270Swpaul		Decimal("start", partp->dp_start, tmp);
53736270Swpaul		Decimal("size", partp->dp_size, tmp);
53839583Swpaul		if (!sanitize_partition(partp)) {
539105599Sbrooks			warnx("ERROR: failed to adjust; setting sysid to 0");
540105599Sbrooks			partp->dp_typ = 0;
54136270Swpaul		}
54239583Swpaul
54336270Swpaul		if (ok("Explicitly specify beg/end address ?"))
54436270Swpaul		{
54536270Swpaul			int	tsec,tcyl,thd;
54636270Swpaul			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
54739583Swpaul			thd = partp->dp_shd;
548105599Sbrooks			tsec = DPSECT(partp->dp_ssect);
549105599Sbrooks			Decimal("beginning cylinder", tcyl, tmp);
55036270Swpaul			Decimal("beginning head", thd, tmp);
55139583Swpaul			Decimal("beginning sector", tsec, tmp);
55236270Swpaul			partp->dp_scyl = DOSCYL(tcyl);
55336270Swpaul			partp->dp_ssect = DOSSECT(tsec,tcyl);
55436270Swpaul			partp->dp_shd = thd;
55536270Swpaul
55636270Swpaul			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
55736270Swpaul			thd = partp->dp_ehd;
55839583Swpaul			tsec = DPSECT(partp->dp_esect);
559105599Sbrooks			Decimal("ending cylinder", tcyl, tmp);
560105599Sbrooks			Decimal("ending head", thd, tmp);
56136270Swpaul			Decimal("ending sector", tsec, tmp);
56239583Swpaul			partp->dp_ecyl = DOSCYL(tcyl);
56336270Swpaul			partp->dp_esect = DOSSECT(tsec,tcyl);
56436270Swpaul			partp->dp_ehd = thd;
56536270Swpaul		} else
56636270Swpaul			dos(partp);
56739583Swpaul
56836270Swpaul		print_part(i);
56939583Swpaul	} while (!ok("Are we happy with this entry?"));
57039583Swpaul    }
57139583Swpaul    }
57236270Swpaul
57339583Swpaulstatic void
57436501Swpaulprint_params()
57536270Swpaul{
57636270Swpaul	printf("parameters extracted from in-core disklabel are:\n");
57736270Swpaul	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
57836270Swpaul			,cyls,heads,sectors,cylsecs);
57936270Swpaul	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
58036270Swpaul		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
58136270Swpaul	printf("parameters to be used for BIOS calculations are:\n");
58236270Swpaul	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
58336270Swpaul		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
58436270Swpaul}
58536270Swpaul
58636270Swpaulstatic void
58736270Swpaulchange_active(int which)
58839583Swpaul{
58939583Swpaul	struct dos_partition *partp = &mboot.parts[0];
59039583Swpaul	int active, i, new, tmp;
591102336Salfred
592102336Salfred	active = -1;
59339583Swpaul	for (i = 0; i < NDOSPART; i++) {
59439583Swpaul		if ((partp[i].dp_flag & ACTIVE) == 0)
59539583Swpaul			continue;
59639583Swpaul		printf("Partition %d is marked active\n", i + 1);
59736270Swpaul		if (active == -1)
59839583Swpaul			active = i + 1;
59939583Swpaul	}
60039583Swpaul	if (a_flag && which != -1)
60139583Swpaul		active = which;
60239583Swpaul	else if (active == -1)
60339583Swpaul		active = 1;
60439583Swpaul
60539583Swpaul	if (!ok("Do you want to change the active partition?"))
60639583Swpaul		return;
60739583Swpaulsetactive:
60839583Swpaul	do {
60939583Swpaul		new = active;
61039583Swpaul		Decimal("active partition", new, tmp);
611102336Salfred		if (new < 1 || new > 4) {
612102336Salfred			printf("Active partition number must be in range 1-4."
61339583Swpaul					"  Try again.\n");
61439583Swpaul			goto setactive;
61536270Swpaul		}
61636270Swpaul		active = new;
61739583Swpaul	} while (!ok("Are you happy with this choice"));
61836270Swpaul	for (i = 0; i < NDOSPART; i++)
61936270Swpaul		partp[i].dp_flag = 0;
62039583Swpaul	if (active > 0 && active <= NDOSPART)
62139583Swpaul		partp[active-1].dp_flag = ACTIVE;
62236270Swpaul}
62336270Swpaul
62436270Swpaulstatic void
62536270Swpaulchange_code()
62636270Swpaul{
627102336Salfred	if (ok("Do you want to change the boot code?"))
628102336Salfred		init_boot();
62939583Swpaul}
63036270Swpaul
63136270Swpaulvoid
63236270Swpaulget_params_to_use()
63336270Swpaul{
63436270Swpaul	int	tmp;
63536270Swpaul	print_params();
63639583Swpaul	if (ok("Do you want to change our idea of what BIOS thinks ?"))
63736270Swpaul	{
63839583Swpaul		do
63936270Swpaul		{
64039583Swpaul			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
64136270Swpaul			Decimal("BIOS's idea of #heads", dos_heads, tmp);
64239583Swpaul			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
64336270Swpaul			dos_cylsecs = dos_heads * dos_sectors;
64436270Swpaul			print_params();
64536270Swpaul		}
646102336Salfred		while(!ok("Are you happy with this choice"));
647102336Salfred	}
64839583Swpaul}
64936270Swpaul
65036270Swpaul
65136270Swpaul/***********************************************\
65267087Swpaul* Change real numbers into strange dos numbers	*
65336270Swpaul\***********************************************/
65436270Swpaulstatic void
65567087Swpauldos(struct dos_partition *partp)
65636270Swpaul{
65739583Swpaul	int cy, sec;
65836270Swpaul	u_int32_t end;
65936270Swpaul
66036270Swpaul	if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
66136270Swpaul		memcpy(partp, &mtpart, sizeof(*partp));
66236270Swpaul		return;
66336270Swpaul	}
66436270Swpaul
66536270Swpaul	/* Start c/h/s. */
66636270Swpaul	partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
66736270Swpaul	cy = partp->dp_start / dos_cylsecs;
66836270Swpaul	sec = partp->dp_start % dos_sectors + 1;
66936270Swpaul	partp->dp_scyl = DOSCYL(cy);
67039583Swpaul	partp->dp_ssect = DOSSECT(sec, cy);
67136270Swpaul
67239583Swpaul	/* End c/h/s. */
67336270Swpaul	end = partp->dp_start + partp->dp_size - 1;
67436270Swpaul	partp->dp_ehd = end % dos_cylsecs / dos_sectors;
67536270Swpaul	cy = end / dos_cylsecs;
67636270Swpaul	sec = end % dos_sectors + 1;
67736270Swpaul	partp->dp_ecyl = DOSCYL(cy);
67839583Swpaul	partp->dp_esect = DOSSECT(sec, cy);
67936270Swpaul}
68036270Swpaul
68136270Swpaulstatic int
68236270Swpaulopen_disk(int flag)
68339583Swpaul{
68439583Swpaul	struct stat 	st;
68539583Swpaul
68639583Swpaul	if (stat(disk, &st) == -1) {
68736270Swpaul		if (errno == ENOENT)
68836270Swpaul			return -2;
68936270Swpaul		warnx("can't get file status of %s", disk);
69036270Swpaul		return -1;
69139583Swpaul	}
69236270Swpaul	if ( !(st.st_mode & S_IFCHR) )
69336270Swpaul		warnx("device %s is not character special", disk);
69439583Swpaul	if ((fd = open(disk,
69539583Swpaul	    a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY)) == -1) {
69636270Swpaul		if(errno == ENXIO)
69736270Swpaul			return -2;
69839583Swpaul		warnx("can't open device %s", disk);
69939583Swpaul		return -1;
70036270Swpaul	}
70136270Swpaul	if (get_params() == -1) {
70239583Swpaul		warnx("can't get disk parameters on %s", disk);
70336270Swpaul		return -1;
70436270Swpaul	}
70536270Swpaul	return fd;
70636270Swpaul}
70736270Swpaul
70836270Swpaulstatic ssize_t
70936270Swpaulread_disk(off_t sector, void *buf)
71039583Swpaul{
71139583Swpaul	lseek(fd,(sector * 512), 0);
71236270Swpaul	if( secsize == 0 )
71336270Swpaul		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
71436270Swpaul			{
71536270Swpaul			/* try the read */
71636270Swpaul			int size = read(fd, buf, secsize);
71739583Swpaul			if( size == secsize )
71836270Swpaul				/* it worked so return */
71939583Swpaul				return secsize;
72036270Swpaul			}
72136270Swpaul	else
72239583Swpaul		return read( fd, buf, secsize );
72336270Swpaul
72436270Swpaul	/* we failed to read at any of the sizes */
72536270Swpaul	return -1;
72636270Swpaul}
72739583Swpaul
72839583Swpaulstatic ssize_t
72936270Swpaulwrite_disk(off_t sector, void *buf)
73036270Swpaul{
73136270Swpaul	lseek(fd,(sector * 512), 0);
73239583Swpaul	/* write out in the size that the read_disk found worked */
73336270Swpaul	return write(fd, buf, secsize);
73436270Swpaul}
73567087Swpaul
73636270Swpaulstatic int
73736270Swpaulget_params()
73836270Swpaul{
73936270Swpaul
74036270Swpaul    if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
74136270Swpaul	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
742102336Salfred	dos_cyls = cyls = 1;
743102336Salfred	dos_heads = heads = 1;
74439583Swpaul	dos_sectors = sectors = 1;
74536270Swpaul	dos_cylsecs = cylsecs = heads * sectors;
74636270Swpaul	disksecs = cyls * heads * sectors;
74736270Swpaul	return disksecs;
74836270Swpaul    }
74936270Swpaul
75067087Swpaul    dos_cyls = cyls = disklabel.d_ncylinders;
75167087Swpaul    dos_heads = heads = disklabel.d_ntracks;
75239583Swpaul    dos_sectors = sectors = disklabel.d_nsectors;
75336270Swpaul    dos_cylsecs = cylsecs = heads * sectors;
75436270Swpaul    disksecs = cyls * heads * sectors;
75536270Swpaul
75636270Swpaul    return (disksecs);
75736270Swpaul}
75836270Swpaul
75936270Swpaul
76036270Swpaulstatic int
76136270Swpaulread_s0()
76236270Swpaul{
76336270Swpaul	mboot.bootinst_size = secsize;
76436270Swpaul	if (mboot.bootinst != NULL)
76539583Swpaul		free(mboot.bootinst);
76636270Swpaul	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
76739583Swpaul		warnx("unable to allocate buffer to read fdisk "
76836270Swpaul		      "partition table");
76936270Swpaul		return -1;
77036270Swpaul	}
77136270Swpaul	if (read_disk(0, mboot.bootinst) == -1) {
77236270Swpaul		warnx("can't read fdisk partition table");
77339583Swpaul		return -1;
77436270Swpaul	}
77539583Swpaul	if (*(uint16_t *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
77639583Swpaul		warnx("invalid fdisk partition table found");
77739583Swpaul		/* So should we initialize things */
77839583Swpaul		return -1;
77939583Swpaul	}
78039583Swpaul	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
78136270Swpaul	return 0;
78239583Swpaul}
78339583Swpaul
78436270Swpaulstatic int
78536270Swpaulwrite_s0()
78636270Swpaul{
78736270Swpaul	int	sector;
78839583Swpaul
78936270Swpaul	if (iotest) {
79036270Swpaul		print_s0(-1);
79136270Swpaul		return 0;
79239583Swpaul	}
79336270Swpaul	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
79467087Swpaul	/*
79536270Swpaul	 * write enable label sector before write (if necessary),
79636270Swpaul	 * disable after writing.
79736270Swpaul	 * needed if the disklabel protected area also protects
79836270Swpaul	 * sector 0. (e.g. empty disk)
799102336Salfred	 */
800102336Salfred	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
80150462Swpaul		if (write_disk(sector,
80250462Swpaul			       &mboot.bootinst[sector * secsize]) == -1) {
80350462Swpaul			warn("can't write fdisk partition table");
80436270Swpaul			return -1;
80536270Swpaul		}
80636270Swpaul	return(0);
80750462Swpaul}
80836270Swpaul
80936270Swpaul
81050462Swpaulstatic int
81136270Swpaulok(const char *str)
81239583Swpaul{
81336270Swpaul	printf("%s [n] ", str);
81436270Swpaul	fflush(stdout);
81536270Swpaul	if (fgets(lbuf, LBUF, stdin) == NULL)
81636270Swpaul		exit(1);
817102336Salfred	lbuf[strlen(lbuf)-1] = 0;
818102336Salfred
81950462Swpaul	if (*lbuf &&
82050462Swpaul		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
82150462Swpaul		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
82236270Swpaul		return 1;
82336270Swpaul	else
82436270Swpaul		return 0;
82550462Swpaul}
82636270Swpaul
82736270Swpaulstatic int
82850462Swpauldecimal(const char *str, int *num, int deflt)
82936270Swpaul{
83036270Swpaul	int acc = 0, c;
83136270Swpaul	char *cp;
83239583Swpaul
83336270Swpaul	while (1) {
83450462Swpaul		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
83536270Swpaul		fflush(stdout);
83636270Swpaul		if (fgets(lbuf, LBUF, stdin) == NULL)
837102336Salfred			exit(1);
838102336Salfred		lbuf[strlen(lbuf)-1] = 0;
83950462Swpaul
84050462Swpaul		if (!*lbuf)
84136270Swpaul			return 0;
84250462Swpaul
84336270Swpaul		cp = lbuf;
84450462Swpaul		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
84567087Swpaul		if (!c)
84650462Swpaul			return 0;
84736270Swpaul		while ((c = *cp++)) {
84850462Swpaul			if (c <= '9' && c >= '0')
84950462Swpaul				acc = acc * 10 + c - '0';
85036270Swpaul			else
85150462Swpaul				break;
85236270Swpaul		}
85367087Swpaul		if (c == ' ' || c == '\t')
85436270Swpaul			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
85536270Swpaul		if (!c) {
85636270Swpaul			*num = acc;
85736270Swpaul			return 1;
85836270Swpaul		} else
85950462Swpaul			printf("%s is an invalid decimal number.  Try again.\n",
86036270Swpaul				lbuf);
861102336Salfred	}
862102336Salfred
86336270Swpaul}
86436270Swpaul
86536270Swpaulstatic const char *
86650462Swpaulget_type(int type)
86750462Swpaul{
86836270Swpaul	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
86950462Swpaul	int	counter = 0;
87036270Swpaul	struct	part_type *ptr = part_types;
87150462Swpaul
87239583Swpaul
87336270Swpaul	while(counter < numentries) {
87450462Swpaul		if(ptr->type == type)
87539583Swpaul			return(ptr->name);
87636270Swpaul		ptr++;
87736270Swpaul		counter++;
87836270Swpaul	}
87936270Swpaul	return("unknown");
88036270Swpaul}
88136270Swpaul
88236464Swpaul
88336464Swpaulstatic void
88436464Swpaulparse_config_line(char *line, CMD *command)
88536464Swpaul{
88636464Swpaul    char	*cp, *end;
88736464Swpaul
88836464Swpaul    cp = line;
88936464Swpaul    while (1) {
89036464Swpaul	memset(command, 0, sizeof(*command));
891123289Sobrien
892122625Sobrien	while (isspace(*cp)) ++cp;
893123289Sobrien	if (*cp == '\0' || *cp == '#')
89436270Swpaul	    break;
895123289Sobrien	command->cmd = *cp++;
89636270Swpaul
89736464Swpaul	/*
89836464Swpaul	 * Parse args
89936464Swpaul	 */
90036270Swpaul	    while (1) {
90136270Swpaul	    while (isspace(*cp)) ++cp;
90239583Swpaul	    if (*cp == '#')
90339583Swpaul		break;		/* found comment */
90439583Swpaul	    if (isalpha(*cp))
90539583Swpaul		command->args[command->n_args].argtype = *cp++;
90639583Swpaul	    if (!isdigit(*cp))
90739583Swpaul		break;		/* assume end of line */
90839583Swpaul	    end = NULL;
909102336Salfred	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
910102336Salfred	    if (cp == end)
91139583Swpaul		break;		/* couldn't parse number */
91241656Swpaul	    cp = end;
91339583Swpaul	    command->n_args++;
91439583Swpaul	}
91539583Swpaul	break;
91639583Swpaul    }
91739583Swpaul}
91839583Swpaul
91939583Swpaul
92039583Swpaulstatic int
92139583Swpaulprocess_geometry(CMD *command)
92239583Swpaul{
92339583Swpaul    int		status = 1, i;
92439583Swpaul
92539583Swpaul    while (1) {
92639583Swpaul	geom_processed = 1;
92739583Swpaul	    if (part_processed) {
92839583Swpaul	    warnx(
92939583Swpaul	"ERROR line %d: the geometry specification line must occur before\n\
93039583Swpaul    all partition specifications",
93139583Swpaul		    current_line_number);
93239583Swpaul	    status = 0;
93339583Swpaul	    break;
93439583Swpaul	}
93539583Swpaul	    if (command->n_args != 3) {
93639583Swpaul	    warnx("ERROR line %d: incorrect number of geometry args",
93739583Swpaul		    current_line_number);
93839583Swpaul	    status = 0;
93939583Swpaul	    break;
94039583Swpaul	}
94139583Swpaul	    dos_cyls = 0;
942102336Salfred	    dos_heads = 0;
943102336Salfred	    dos_sectors = 0;
94436270Swpaul	    for (i = 0; i < 3; ++i) {
94536270Swpaul		    switch (command->args[i].argtype) {
94636270Swpaul	    case 'c':
94736270Swpaul		dos_cyls = command->args[i].arg_val;
94839583Swpaul		break;
94936270Swpaul	    case 'h':
95039583Swpaul		dos_heads = command->args[i].arg_val;
95136270Swpaul		break;
95236270Swpaul	    case 's':
95339583Swpaul		dos_sectors = command->args[i].arg_val;
95439583Swpaul		break;
95541656Swpaul	    default:
95639583Swpaul		warnx(
95739583Swpaul		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
95839583Swpaul			current_line_number, command->args[i].argtype,
95939583Swpaul			command->args[i].argtype);
96039583Swpaul		status = 0;
96136270Swpaul		break;
96236270Swpaul	    }
96336270Swpaul	}
96439583Swpaul	if (status == 0)
96572084Sphk	    break;
96636270Swpaul
96736270Swpaul	dos_cylsecs = dos_heads * dos_sectors;
96839583Swpaul
96939583Swpaul	/*
97039583Swpaul	 * Do sanity checks on parameter values
97139583Swpaul	 */
97239583Swpaul	    if (dos_cyls == 0) {
97339583Swpaul	    warnx("ERROR line %d: number of cylinders not specified",
97439583Swpaul		    current_line_number);
97539583Swpaul	    status = 0;
97639583Swpaul	}
97739583Swpaul	    if (dos_cyls > 1024) {
97839583Swpaul	    warnx(
97939583Swpaul	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
980122625Sobrien    (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
98136270Swpaul    is dedicated to FreeBSD)",
98236270Swpaul		    current_line_number, dos_cyls);
98336270Swpaul	}
98436270Swpaul
98536317Swpaul	    if (dos_heads == 0) {
98636270Swpaul	    warnx("ERROR line %d: number of heads not specified",
98736270Swpaul		    current_line_number);
98836270Swpaul	    status = 0;
98939583Swpaul	    } else if (dos_heads > 256) {
99039583Swpaul	    warnx("ERROR line %d: number of heads must be within (1-256)",
99136270Swpaul		    current_line_number);
99236270Swpaul	    status = 0;
99336270Swpaul	}
99436270Swpaul
99539583Swpaul	    if (dos_sectors == 0) {
99639583Swpaul	    warnx("ERROR line %d: number of sectors not specified",
99739583Swpaul		    current_line_number);
99839583Swpaul	    status = 0;
99939583Swpaul	    } else if (dos_sectors > 63) {
100039583Swpaul	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1001102336Salfred		    current_line_number);
1002102336Salfred	    status = 0;
100350468Swpaul	}
100450468Swpaul
100539583Swpaul	break;
100639583Swpaul    }
100750468Swpaul    return (status);
100839583Swpaul}
100950468Swpaul
101039583Swpaul
101150468Swpaulstatic int
101239583Swpaulprocess_partition(CMD *command)
101350468Swpaul{
101439583Swpaul    int				status = 0, partition;
101550468Swpaul    u_int32_t			prev_head_boundary, prev_cyl_boundary;
101650468Swpaul    u_int32_t			adj_size, max_end;
101739583Swpaul    struct dos_partition	*partp;
101850468Swpaul
101939583Swpaul	while (1) {
102050468Swpaul	part_processed = 1;
102139583Swpaul		if (command->n_args != 4) {
102250468Swpaul	    warnx("ERROR line %d: incorrect number of partition args",
102339583Swpaul		    current_line_number);
102450468Swpaul	    break;
102539583Swpaul	}
102639583Swpaul	partition = command->args[0].arg_val;
102739583Swpaul		if (partition < 1 || partition > 4) {
1028102336Salfred	    warnx("ERROR line %d: invalid partition number %d",
1029102336Salfred		    current_line_number, partition);
103039583Swpaul	    break;
103136270Swpaul	}
103236270Swpaul	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
103339583Swpaul	bzero((char *)partp, sizeof (struct dos_partition));
103436270Swpaul	partp->dp_typ = command->args[1].arg_val;
103536270Swpaul	partp->dp_start = command->args[2].arg_val;
103639583Swpaul	partp->dp_size = command->args[3].arg_val;
103750468Swpaul	max_end = partp->dp_start + partp->dp_size;
103836270Swpaul
103939583Swpaul		if (partp->dp_typ == 0) {
104036270Swpaul	    /*
104136270Swpaul	     * Get out, the partition is marked as unused.
104239583Swpaul	     */
104339583Swpaul	    /*
104436270Swpaul	     * Insure that it's unused.
104536270Swpaul	     */
104639583Swpaul	    bzero((char *)partp, sizeof (struct dos_partition));
104739583Swpaul	    status = 1;
104836270Swpaul	    break;
104936270Swpaul	}
105036270Swpaul
105136270Swpaul	/*
105236270Swpaul	 * Adjust start upwards, if necessary, to fall on an head boundary.
105339583Swpaul	 */
105445155Swpaul		if (partp->dp_start % dos_sectors != 0) {
105539583Swpaul	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
105636270Swpaul	    if (max_end < dos_sectors ||
105739583Swpaul			    prev_head_boundary > max_end - dos_sectors) {
105836270Swpaul		/*
105936270Swpaul		 * Can't go past end of partition
106045155Swpaul		 */
106145155Swpaul		warnx(
106245155Swpaul	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
106345155Swpaul    a head boundary",
106436270Swpaul			current_line_number, partition);
106536270Swpaul		break;
106636270Swpaul	    }
106736270Swpaul	    warnx(
106836270Swpaul	"WARNING: adjusting start offset of partition %d\n\
106939583Swpaul    from %u to %u, to fall on a head boundary",
107036270Swpaul		    partition, (u_int)partp->dp_start,
107136270Swpaul		    (u_int)(prev_head_boundary + dos_sectors));
107239583Swpaul	    partp->dp_start = prev_head_boundary + dos_sectors;
107339583Swpaul	}
107436270Swpaul
107536270Swpaul	/*
107639583Swpaul	 * Adjust size downwards, if necessary, to fall on a cylinder
107736270Swpaul	 * boundary.
107836270Swpaul	 */
107939583Swpaul	prev_cyl_boundary =
108036270Swpaul	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
108136270Swpaul	if (prev_cyl_boundary > partp->dp_start)
108236270Swpaul	    adj_size = prev_cyl_boundary - partp->dp_start;
108336270Swpaul		else {
108436270Swpaul	    warnx(
108536270Swpaul	"ERROR: could not adjust partition to start on a head boundary\n\
108636270Swpaul    and end on a cylinder boundary.");
108736270Swpaul	    return (0);
108836270Swpaul	}
108939583Swpaul		if (adj_size != partp->dp_size) {
109036270Swpaul	    warnx(
1091102336Salfred	"WARNING: adjusting size of partition %d from %u to %u\n\
1092102336Salfred    to end on a cylinder boundary",
109348992Swpaul		    partition, (u_int)partp->dp_size, (u_int)adj_size);
109436270Swpaul	    partp->dp_size = adj_size;
109536270Swpaul	}
109636270Swpaul		if (partp->dp_size == 0) {
109736270Swpaul	    warnx("ERROR line %d: size of partition %d is zero",
109836270Swpaul		    current_line_number, partition);
109936270Swpaul	    break;
110048992Swpaul	}
110148992Swpaul
110248992Swpaul	dos(partp);
110348992Swpaul	status = 1;
110448992Swpaul	break;
110536270Swpaul    }
110636270Swpaul    return (status);
110736270Swpaul}
110848992Swpaul
110936270Swpaul
111036270Swpaulstatic int
1111102336Salfredprocess_active(CMD *command)
1112102336Salfred{
111348992Swpaul    int				status = 0, partition, i;
111436270Swpaul    struct dos_partition	*partp;
111567087Swpaul
111639583Swpaul	while (1) {
111739583Swpaul	active_processed = 1;
111839583Swpaul		if (command->n_args != 1) {
111939583Swpaul	    warnx("ERROR line %d: incorrect number of active args",
112048992Swpaul		    current_line_number);
112136270Swpaul	    status = 0;
112248992Swpaul	    break;
112348992Swpaul	}
112448992Swpaul	partition = command->args[0].arg_val;
112548992Swpaul		if (partition < 1 || partition > 4) {
112639583Swpaul	    warnx("ERROR line %d: invalid partition number %d",
112739583Swpaul		    current_line_number, partition);
112839583Swpaul	    break;
112939583Swpaul	}
113036270Swpaul	/*
113139583Swpaul	 * Reset active partition
113239583Swpaul	 */
113336270Swpaul	partp = ((struct dos_partition *) &mboot.parts);
113439583Swpaul	for (i = 0; i < NDOSPART; i++)
1135105599Sbrooks	    partp[i].dp_flag = 0;
1136112878Sjhb	partp[partition-1].dp_flag = ACTIVE;
113736270Swpaul
113836270Swpaul	status = 1;
113993818Sjhb	break;
114093818Sjhb    }
114169583Swpaul    return (status);
114236270Swpaul}
114336270Swpaul
114436270Swpaul
114572813Swpaulstatic int
114636270Swpaulprocess_line(char *line)
114739583Swpaul{
114839583Swpaul    CMD		command;
114948992Swpaul    int		status = 1;
1150127135Snjl
1151127135Snjl	while (1) {
115248992Swpaul	parse_config_line(line, &command);
115348992Swpaul		switch (command.cmd) {
115448992Swpaul	case 0:
115548992Swpaul	    /*
115648992Swpaul	     * Comment or blank line
115748992Swpaul	     */
115848992Swpaul	    break;
1159127135Snjl	case 'g':
1160127135Snjl	    /*
116145155Swpaul	     * Set geometry
116239583Swpaul	     */
116348992Swpaul	    status = process_geometry(&command);
1164127135Snjl	    break;
1165127135Snjl	case 'p':
116648992Swpaul	    status = process_partition(&command);
116748992Swpaul	    break;
1168127135Snjl	case 'a':
1169127135Snjl	    status = process_active(&command);
117036270Swpaul	    break;
117139583Swpaul	default:
117236270Swpaul	    status = 0;
117348992Swpaul	    break;
1174105599Sbrooks	}
117548992Swpaul	break;
117648992Swpaul    }
117748992Swpaul    return (status);
117848992Swpaul}
117948992Swpaul
118048992Swpaul
118148992Swpaulstatic int
118239583Swpaulread_config(char *config_file)
118339583Swpaul{
118439583Swpaul    FILE	*fp = NULL;
118539583Swpaul    int		status = 1;
118639583Swpaul    char	buf[1010];
118739583Swpaul
118839583Swpaul	while (1) {
118948992Swpaul		if (strcmp(config_file, "-") != 0) {
119039583Swpaul	    /*
119148992Swpaul	     * We're not reading from stdin
119239583Swpaul	     */
119336270Swpaul			if ((fp = fopen(config_file, "r")) == NULL) {
119436270Swpaul		status = 0;
119548992Swpaul		break;
1196127135Snjl	    }
119748992Swpaul		} else {
119848992Swpaul	    fp = stdin;
119948992Swpaul	}
1200105599Sbrooks	current_line_number = 0;
120148992Swpaul		while (!feof(fp)) {
120236270Swpaul	    if (fgets(buf, sizeof(buf), fp) == NULL)
120336270Swpaul		break;
120436270Swpaul	    ++current_line_number;
120536270Swpaul	    status = process_line(buf);
120651439Swpaul	    if (status == 0)
120736270Swpaul		break;
120851439Swpaul	    }
120951657Swpaul	break;
121039583Swpaul    }
121151439Swpaul	if (fp) {
1212105599Sbrooks	/*
121348992Swpaul	 * It doesn't matter if we're reading from stdin, as we've reached EOF
121436270Swpaul	 */
121536270Swpaul	fclose(fp);
121636270Swpaul    }
121739583Swpaul    return (status);
121839583Swpaul}
121939583Swpaul
122043235Swpaul
122139583Swpaulstatic void
122239583Swpaulreset_boot(void)
122339583Swpaul{
122439583Swpaul    int				i;
122539583Swpaul    struct dos_partition	*partp;
122639583Swpaul
122750468Swpaul    init_boot();
122839583Swpaul	for (i = 0; i < 4; ++i) {
122939583Swpaul	partp = ((struct dos_partition *) &mboot.parts) + i;
123038030Swpaul	bzero((char *)partp, sizeof (struct dos_partition));
123139583Swpaul    }
123239583Swpaul}
123339583Swpaul
123439583Swpaulstatic int
1235105599Sbrookssanitize_partition(struct dos_partition *partp)
123648992Swpaul{
123739583Swpaul    u_int32_t			prev_head_boundary, prev_cyl_boundary;
123839583Swpaul    u_int32_t			max_end, size, start;
123939583Swpaul
124039583Swpaul    start = partp->dp_start;
124139583Swpaul    size = partp->dp_size;
124239583Swpaul    max_end = start + size;
124339583Swpaul    /* Only allow a zero size if the partition is being marked unused. */
124439583Swpaul    if (size == 0) {
124539583Swpaul	if (start == 0 && partp->dp_typ == 0)
124639583Swpaul	    return (1);
124739583Swpaul	warnx("ERROR: size of partition is zero");
124839583Swpaul	return (0);
124939583Swpaul    }
125039583Swpaul    /* Return if no adjustment is necessary. */
125139583Swpaul    if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
125239583Swpaul	return (1);
125339583Swpaul
125439583Swpaul    if (start % dos_sectors != 0)
125539583Swpaul	warnx("WARNING: partition does not start on a head boundary");
125639583Swpaul    if ((start  +size) % dos_sectors != 0)
125739583Swpaul	warnx("WARNING: partition does not end on a cylinder boundary");
125839583Swpaul    warnx("WARNING: this may confuse the BIOS or some operating systems");
125939583Swpaul    if (!ok("Correct this automatically?"))
126039583Swpaul	return (1);
126139583Swpaul
126239583Swpaul    /*
126339583Swpaul     * Adjust start upwards, if necessary, to fall on an head boundary.
1264121816Sbrooks     */
1265134442Srwatson    if (start % dos_sectors != 0) {
1266134442Srwatson	prev_head_boundary = start / dos_sectors * dos_sectors;
126739583Swpaul	if (max_end < dos_sectors ||
126839583Swpaul	    prev_head_boundary >= max_end - dos_sectors) {
126939583Swpaul	    /*
127039583Swpaul	     * Can't go past end of partition
127139583Swpaul	     */
127251439Swpaul	    warnx(
127339583Swpaul    "ERROR: unable to adjust start of partition to fall on a head boundary");
127439583Swpaul	    return (0);
127539583Swpaul        }
127639583Swpaul	start = prev_head_boundary + dos_sectors;
127750468Swpaul    }
127839583Swpaul
127939583Swpaul    /*
128036270Swpaul     * Adjust size downwards, if necessary, to fall on a cylinder
128150462Swpaul     * boundary.
128250462Swpaul     */
128350462Swpaul    prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
128436270Swpaul    if (prev_cyl_boundary > start)
128550462Swpaul	size = prev_cyl_boundary - start;
128650462Swpaul    else {
128745155Swpaul	warnx("ERROR: could not adjust partition to start on a head boundary\n\
128845155Swpaul    and end on a cylinder boundary.");
128945155Swpaul	return (0);
129045155Swpaul    }
129145155Swpaul
129245155Swpaul    /* Finally, commit any changes to partp and return. */
129345155Swpaul    if (start != partp->dp_start) {
129445166Swpaul	warnx("WARNING: adjusting start offset of partition to %u",
129545155Swpaul	    (u_int)start);
129645155Swpaul	partp->dp_start = start;
129745155Swpaul    }
129845155Swpaul    if (size != partp->dp_size) {
129945155Swpaul	warnx("WARNING: adjusting size of partition to %u", (u_int)size);
130036270Swpaul	partp->dp_size = size;
130136270Swpaul    }
130239583Swpaul
130363090Sarchie    return (1);
130439583Swpaul}
1305106936Ssam
130638030Swpaul/*
1307113609Snjl * Try figuring out the root device's canonical disk name.
1308112872Snjl * The following choices are considered:
1309112872Snjl *   /dev/ad0s1a     => /dev/ad0
1310112872Snjl *   /dev/da0a       => /dev/da0
1311112872Snjl *   /dev/vinum/root => /dev/vinum/root
1312112872Snjl */
1313113609Snjlstatic char *
1314112872Snjlget_rootdisk(void)
1315112872Snjl{
1316112872Snjl	struct statfs rootfs;
131736270Swpaul	regex_t re;
1318112872Snjl#define NMATCHES 2
1319112872Snjl	regmatch_t rm[NMATCHES];
1320112872Snjl	char *s;
132148992Swpaul	int rv;
132236270Swpaul
132336270Swpaul	if (statfs("/", &rootfs) == -1)
1324113609Snjl		err(1, "statfs(\"/\")");
1325113609Snjl
1326113609Snjl	if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1327113609Snjl		    REG_EXTENDED)) != 0)
1328113609Snjl		errx(1, "regcomp() failed (%d)", rv);
1329113609Snjl	if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1330113609Snjl		errx(1,
1331102336Salfred"mounted root fs resource doesn't match expectations (regexec returned %d)",
1332102336Salfred		    rv);
133348992Swpaul	if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
133448992Swpaul		errx(1, "out of memory");
133548992Swpaul	memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
133648992Swpaul	    rm[1].rm_eo - rm[1].rm_so);
133748992Swpaul	s[rm[1].rm_eo - rm[1].rm_so] = 0;
133848992Swpaul
1339112880Sjhb	return s;
134067087Swpaul}
134148992Swpaul