fdisk.c revision 103665
1/*
2 * Mach Operating System
3 * Copyright (c) 1992 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19 *  School of Computer Science
20 *  Carnegie Mellon University
21 *  Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26
27#ifndef lint
28static const char rcsid[] =
29  "$FreeBSD: head/sbin/fdisk/fdisk.c 103665 2002-09-20 08:12:51Z phk $";
30#endif /* not lint */
31
32#include <sys/disk.h>
33#include <sys/disklabel.h>
34#include <sys/param.h>
35#include <sys/stat.h>
36#include <sys/mount.h>
37#include <ctype.h>
38#include <fcntl.h>
39#include <err.h>
40#include <errno.h>
41#include <paths.h>
42#include <regex.h>
43#include <stdint.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48
49int iotest;
50
51#define LBUF 100
52static char lbuf[LBUF];
53
54#define MBRSIGOFF	510
55
56/*
57 *
58 * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
59 *
60 * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
61 *	Copyright (c) 1989	Robert. V. Baron
62 *	Created.
63 */
64
65#define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
66
67#define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
68
69#define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
70#define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
71static int secsize = 0;		/* the sensed sector size */
72
73static char *disk;
74
75static struct disklabel disklabel;		/* disk parameters */
76
77static int cyls, sectors, heads, cylsecs, disksecs;
78
79struct mboot {
80	unsigned char padding[2]; /* force the longs to be long aligned */
81  	unsigned char *bootinst;  /* boot code */
82  	off_t bootinst_size;
83	struct	dos_partition parts[4];
84};
85
86static struct mboot mboot;
87static int fd;
88
89#define ACTIVE 0x80
90#define BOOT_MAGIC 0xAA55
91
92static uint dos_cyls;
93static uint dos_heads;
94static uint dos_sectors;
95static uint dos_cylsecs;
96
97#define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
98#define DOSCYL(c)	(c & 0xff)
99
100#define MAX_ARGS	10
101
102static int	current_line_number;
103
104static int	geom_processed = 0;
105static int	part_processed = 0;
106static int	active_processed = 0;
107
108typedef struct cmd {
109    char		cmd;
110    int			n_args;
111    struct arg {
112	char	argtype;
113	int	arg_val;
114    }			args[MAX_ARGS];
115} CMD;
116
117static int B_flag  = 0;		/* replace boot code */
118static int I_flag  = 0;		/* use entire disk for FreeBSD */
119static int a_flag  = 0;		/* set active partition */
120static char *b_flag = NULL;	/* path to boot code */
121static int i_flag  = 0;		/* replace partition data */
122static int u_flag  = 0;		/* update partition data */
123static int s_flag  = 0;		/* Print a summary and exit */
124static int t_flag  = 0;		/* test only */
125static char *f_flag = NULL;	/* Read config info from file */
126static int v_flag  = 0;		/* Be verbose */
127
128static struct part_type
129{
130 unsigned char type;
131	const char *name;
132} part_types[] = {
133	 {0x00, "unused"}
134	,{0x01, "Primary DOS with 12 bit FAT"}
135	,{0x02, "XENIX / file system"}
136	,{0x03, "XENIX /usr file system"}
137	,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
138	,{0x05, "Extended DOS"}
139	,{0x06, "Primary 'big' DOS (>= 32MB)"}
140	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
141	,{0x08, "AIX file system or SplitDrive"}
142	,{0x09, "AIX boot partition or Coherent"}
143	,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
144	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
145	,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
146	,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
147	,{0x0F, "Extended DOS (LBA)"}
148	,{0x10, "OPUS"}
149	,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
150	,{0x12, "Compaq diagnostics"}
151	,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
152	,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
153	,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
154	,{0x18, "AST Windows swapfile"}
155	,{0x24, "NEC DOS"}
156	,{0x3C, "PartitionMagic recovery"}
157	,{0x39, "plan9"}
158	,{0x40, "VENIX 286"}
159	,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
160	,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
161	,{0x43, "Linux native (sharing disk with DRDOS)"}
162	,{0x4D, "QNX 4.2 Primary"}
163	,{0x4E, "QNX 4.2 Secondary"}
164	,{0x4F, "QNX 4.2 Tertiary"}
165	,{0x50, "DM (disk manager)"}
166	,{0x51, "DM6 Aux1 (or Novell)"}
167	,{0x52, "CP/M or Microport SysV/AT"}
168	,{0x53, "DM6 Aux3"}
169	,{0x54, "DM6"}
170	,{0x55, "EZ-Drive (disk manager)"}
171	,{0x56, "Golden Bow (disk manager)"}
172	,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
173	,{0x61, "SpeedStor"}
174	,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
175	,{0x64, "Novell Netware/286 2.xx"}
176	,{0x65, "Novell Netware/386 3.xx"}
177	,{0x70, "DiskSecure Multi-Boot"}
178	,{0x75, "PCIX"}
179	,{0x77, "QNX4.x"}
180	,{0x78, "QNX4.x 2nd part"}
181	,{0x79, "QNX4.x 3rd part"}
182	,{0x80, "Minix until 1.4a"}
183	,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
184	,{0x82, "Linux swap or Solaris x86"}
185	,{0x83, "Linux native"}
186	,{0x84, "OS/2 hidden C: drive"}
187	,{0x85, "Linux extended"}
188	,{0x86, "NTFS volume set??"}
189	,{0x87, "NTFS volume set??"}
190	,{0x93, "Amoeba file system"}
191	,{0x94, "Amoeba bad block table"}
192	,{0x9F, "BSD/OS"}
193	,{0xA0, "Suspend to Disk"}
194	,{0xA5, "FreeBSD/NetBSD/386BSD"}
195	,{0xA6, "OpenBSD"}
196	,{0xA7, "NeXTSTEP"}
197	,{0xA9, "NetBSD"}
198	,{0xAC, "IBM JFS"}
199	,{0xB7, "BSDI BSD/386 file system"}
200	,{0xB8, "BSDI BSD/386 swap"}
201	,{0xC1, "DRDOS/sec with 12-bit FAT"}
202	,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
203	,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
204	,{0xC7, "Syrinx"}
205	,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
206	,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
207	,{0xE3, "DOS R/O or SpeedStor"}
208	,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
209	,{0xEB, "BeOS file system"}
210	,{0xEE, "EFI GPT"}
211	,{0xEF, "EFI System Partition"}
212	,{0xF1, "SpeedStor"}
213	,{0xF2, "DOS 3.3+ Secondary"}
214	,{0xF4, "SpeedStor large partition"}
215	,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
216	,{0xFF, "Xenix bad blocks table"}
217};
218
219static void print_s0(int which);
220static void print_part(int i);
221static void init_sector0(unsigned long start);
222static void init_boot(void);
223static void change_part(int i);
224static void print_params(void);
225static void change_active(int which);
226static void change_code(void);
227static void get_params_to_use(void);
228static char *get_rootdisk(void);
229static void dos(struct dos_partition *partp);
230static int open_disk(int flag);
231static ssize_t read_disk(off_t sector, void *buf);
232static ssize_t write_disk(off_t sector, void *buf);
233static int get_params(void);
234static int read_s0(void);
235static int write_s0(void);
236static int ok(const char *str);
237static int decimal(const char *str, int *num, int deflt);
238static const char *get_type(int type);
239static int read_config(char *config_file);
240static void reset_boot(void);
241static int sanitize_partition(struct dos_partition *);
242static void usage(void);
243
244int
245main(int argc, char *argv[])
246{
247	struct	stat sb;
248	int	c, i;
249	int	partition = -1;
250	struct	dos_partition *partp;
251
252	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
253		switch (c) {
254		case 'B':
255			B_flag = 1;
256			break;
257		case 'I':
258			I_flag = 1;
259			break;
260		case 'a':
261			a_flag = 1;
262			break;
263		case 'b':
264			b_flag = optarg;
265			break;
266		case 'f':
267			f_flag = optarg;
268			break;
269		case 'i':
270			i_flag = 1;
271			break;
272		case 's':
273			s_flag = 1;
274			break;
275		case 't':
276			t_flag = 1;
277			break;
278		case 'u':
279			u_flag = 1;
280			break;
281		case 'v':
282			v_flag = 1;
283			break;
284		case '1':
285		case '2':
286		case '3':
287		case '4':
288			partition = c - '0';
289			break;
290		default:
291			usage();
292		}
293	if (f_flag || i_flag)
294		u_flag = 1;
295	if (t_flag)
296		v_flag = 1;
297	argc -= optind;
298	argv += optind;
299
300	if (argc == 0) {
301		disk = get_rootdisk();
302	} else {
303		if (stat(argv[0], &sb) == 0) {
304			/* OK, full pathname given */
305			disk = argv[0];
306		} else if (errno == ENOENT) {
307			/* Try prepending "/dev" */
308			asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
309			if (disk == NULL)
310				errx(1, "out of memory");
311		} else {
312			/* other stat error, let it fail below */
313			disk = argv[0];
314		}
315	}
316	if (open_disk(u_flag) < 0)
317		err(1, "cannot open disk %s", disk);
318
319	/* (abu)use mboot.bootinst to probe for the sector size */
320	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
321		err(1, "cannot allocate buffer to determine disk sector size");
322	read_disk(0, mboot.bootinst);
323	free(mboot.bootinst);
324	mboot.bootinst = NULL;
325
326	if (s_flag) {
327		if (read_s0())
328			err(1, "read_s0");
329		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
330		    dos_sectors);
331		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
332		for (i = 0; i < NDOSPART; i++) {
333			partp = ((struct dos_partition *) &mboot.parts) + i;
334			if (partp->dp_start == 0 && partp->dp_size == 0)
335				continue;
336			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
337			    (u_long) partp->dp_start,
338			    (u_long) partp->dp_size, partp->dp_typ,
339			    partp->dp_flag);
340		}
341		exit(0);
342	}
343
344	printf("******* Working on device %s *******\n",disk);
345
346	if (I_flag) {
347		read_s0();
348		reset_boot();
349		partp = (struct dos_partition *) (&mboot.parts[0]);
350		partp->dp_typ = DOSPTYP_386BSD;
351		partp->dp_flag = ACTIVE;
352		partp->dp_start = dos_sectors;
353		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
354		    dos_sectors;
355		dos(partp);
356		if (v_flag)
357			print_s0(-1);
358		if (!t_flag)
359			write_s0();
360		exit(0);
361	}
362	if (f_flag) {
363	    if (read_s0() || i_flag)
364		reset_boot();
365	    if (!read_config(f_flag))
366		exit(1);
367	    if (v_flag)
368		print_s0(-1);
369	    if (!t_flag)
370		write_s0();
371	} else {
372	    if(u_flag)
373		get_params_to_use();
374	    else
375		print_params();
376
377	    if (read_s0())
378		init_sector0(dos_sectors);
379
380	    printf("Media sector size is %d\n", secsize);
381	    printf("Warning: BIOS sector numbering starts with sector 1\n");
382	    printf("Information from DOS bootblock is:\n");
383	    if (partition == -1)
384		for (i = 1; i <= NDOSPART; i++)
385		    change_part(i);
386	    else
387		change_part(partition);
388
389	    if (u_flag || a_flag)
390		change_active(partition);
391
392	    if (B_flag)
393		change_code();
394
395	    if (u_flag || a_flag || B_flag) {
396		if (!t_flag) {
397		    printf("\nWe haven't changed the partition table yet.  ");
398		    printf("This is your last chance.\n");
399		}
400		print_s0(-1);
401		if (!t_flag) {
402		    if (ok("Should we write new partition table?"))
403			write_s0();
404			} else {
405		    printf("\n-t flag specified -- partition table not written.\n");
406		}
407	    }
408	}
409
410	exit(0);
411}
412
413static void
414usage()
415{
416	fprintf(stderr, "%s%s",
417		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
418 		"       fdisk -f configfile [-itv] [disk]\n");
419        exit(1);
420}
421
422static void
423print_s0(int which)
424{
425	int	i;
426
427	print_params();
428	printf("Information from DOS bootblock is:\n");
429	if (which == -1)
430		for (i = 1; i <= NDOSPART; i++)
431			printf("%d: ", i), print_part(i);
432	else
433		print_part(which);
434}
435
436static struct dos_partition mtpart;
437
438static void
439print_part(int i)
440{
441	struct	  dos_partition *partp;
442	u_int64_t part_mb;
443
444	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
445
446	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
447		printf("<UNUSED>\n");
448		return;
449	}
450	/*
451	 * Be careful not to overflow.
452	 */
453	part_mb = partp->dp_size;
454	part_mb *= secsize;
455	part_mb /= (1024 * 1024);
456	printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
457	    get_type(partp->dp_typ));
458	printf("    start %lu, size %lu (%ju Meg), flag %x%s\n",
459		(u_long)partp->dp_start,
460		(u_long)partp->dp_size,
461		(uintmax_t)part_mb,
462		partp->dp_flag,
463		partp->dp_flag == ACTIVE ? " (active)" : "");
464	printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
465		,DPCYL(partp->dp_scyl, partp->dp_ssect)
466		,partp->dp_shd
467		,DPSECT(partp->dp_ssect)
468		,DPCYL(partp->dp_ecyl, partp->dp_esect)
469		,partp->dp_ehd
470		,DPSECT(partp->dp_esect));
471}
472
473
474static void
475init_boot(void)
476{
477#ifndef __ia64__
478	const char *fname;
479	int fdesc, n;
480	struct stat sb;
481
482	fname = b_flag ? b_flag : "/boot/mbr";
483	if ((fdesc = open(fname, O_RDONLY)) == -1 ||
484	    fstat(fdesc, &sb) == -1)
485		err(1, "%s", fname);
486	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
487		errx(1, "%s: length must be a multiple of sector size", fname);
488	if (mboot.bootinst != NULL)
489		free(mboot.bootinst);
490	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
491		errx(1, "%s: unable to allocate read buffer", fname);
492	if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
493	    close(fdesc))
494		err(1, "%s", fname);
495	if (n != mboot.bootinst_size)
496		errx(1, "%s: short read", fname);
497#else
498	if (mboot.bootinst != NULL)
499		free(mboot.bootinst);
500	mboot.bootinst_size = secsize;
501	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
502		errx(1, "unable to allocate boot block buffer");
503	memset(mboot.bootinst, 0, mboot.bootinst_size);
504	*(uint16_t *)(void *)&mboot.bootinst[MBRSIGOFF] = BOOT_MAGIC;
505#endif
506}
507
508
509static void
510init_sector0(unsigned long start)
511{
512	struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
513
514	init_boot();
515
516	partp->dp_typ = DOSPTYP_386BSD;
517	partp->dp_flag = ACTIVE;
518	start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
519	if(start == 0)
520		start = dos_sectors;
521	partp->dp_start = start;
522	partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
523
524	dos(partp);
525}
526
527static void
528change_part(int i)
529{
530	struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
531
532    printf("The data for partition %d is:\n", i);
533    print_part(i);
534
535    if (u_flag && ok("Do you want to change it?")) {
536	int tmp;
537
538	if (i_flag) {
539		bzero((char *)partp, sizeof (struct dos_partition));
540		if (i == 4) {
541			init_sector0(1);
542			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
543			print_part(i);
544		}
545	}
546
547	do {
548		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
549		Decimal("start", partp->dp_start, tmp);
550		Decimal("size", partp->dp_size, tmp);
551		if (!sanitize_partition(partp)) {
552			warnx("ERROR: failed to adjust; setting sysid to 0");
553			partp->dp_typ = 0;
554		}
555
556		if (ok("Explicitly specify beg/end address ?"))
557		{
558			int	tsec,tcyl,thd;
559			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
560			thd = partp->dp_shd;
561			tsec = DPSECT(partp->dp_ssect);
562			Decimal("beginning cylinder", tcyl, tmp);
563			Decimal("beginning head", thd, tmp);
564			Decimal("beginning sector", tsec, tmp);
565			partp->dp_scyl = DOSCYL(tcyl);
566			partp->dp_ssect = DOSSECT(tsec,tcyl);
567			partp->dp_shd = thd;
568
569			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
570			thd = partp->dp_ehd;
571			tsec = DPSECT(partp->dp_esect);
572			Decimal("ending cylinder", tcyl, tmp);
573			Decimal("ending head", thd, tmp);
574			Decimal("ending sector", tsec, tmp);
575			partp->dp_ecyl = DOSCYL(tcyl);
576			partp->dp_esect = DOSSECT(tsec,tcyl);
577			partp->dp_ehd = thd;
578		} else
579			dos(partp);
580
581		print_part(i);
582	} while (!ok("Are we happy with this entry?"));
583    }
584    }
585
586static void
587print_params()
588{
589	printf("parameters extracted from in-core disklabel are:\n");
590	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
591			,cyls,heads,sectors,cylsecs);
592	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
593		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
594	printf("parameters to be used for BIOS calculations are:\n");
595	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
596		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
597}
598
599static void
600change_active(int which)
601{
602	struct dos_partition *partp = &mboot.parts[0];
603	int active, i, new, tmp;
604
605	active = -1;
606	for (i = 0; i < NDOSPART; i++) {
607		if ((partp[i].dp_flag & ACTIVE) == 0)
608			continue;
609		printf("Partition %d is marked active\n", i + 1);
610		if (active == -1)
611			active = i + 1;
612	}
613	if (a_flag && which != -1)
614		active = which;
615	else if (active == -1)
616		active = 1;
617
618	if (!ok("Do you want to change the active partition?"))
619		return;
620setactive:
621	do {
622		new = active;
623		Decimal("active partition", new, tmp);
624		if (new < 1 || new > 4) {
625			printf("Active partition number must be in range 1-4."
626					"  Try again.\n");
627			goto setactive;
628		}
629		active = new;
630	} while (!ok("Are you happy with this choice"));
631	for (i = 0; i < NDOSPART; i++)
632		partp[i].dp_flag = 0;
633	if (active > 0 && active <= NDOSPART)
634		partp[active-1].dp_flag = ACTIVE;
635}
636
637static void
638change_code()
639{
640	if (ok("Do you want to change the boot code?"))
641		init_boot();
642}
643
644void
645get_params_to_use()
646{
647	int	tmp;
648	print_params();
649	if (ok("Do you want to change our idea of what BIOS thinks ?"))
650	{
651		do
652		{
653			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
654			Decimal("BIOS's idea of #heads", dos_heads, tmp);
655			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
656			dos_cylsecs = dos_heads * dos_sectors;
657			print_params();
658		}
659		while(!ok("Are you happy with this choice"));
660	}
661}
662
663
664/***********************************************\
665* Change real numbers into strange dos numbers	*
666\***********************************************/
667static void
668dos(struct dos_partition *partp)
669{
670	int cy, sec;
671	u_int32_t end;
672
673	if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
674		memcpy(partp, &mtpart, sizeof(*partp));
675		return;
676	}
677
678	/* Start c/h/s. */
679	partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
680	cy = partp->dp_start / dos_cylsecs;
681	sec = partp->dp_start % dos_sectors + 1;
682	partp->dp_scyl = DOSCYL(cy);
683	partp->dp_ssect = DOSSECT(sec, cy);
684
685	/* End c/h/s. */
686	end = partp->dp_start + partp->dp_size - 1;
687	partp->dp_ehd = end % dos_cylsecs / dos_sectors;
688	cy = end / dos_cylsecs;
689	sec = end % dos_sectors + 1;
690	partp->dp_ecyl = DOSCYL(cy);
691	partp->dp_esect = DOSSECT(sec, cy);
692}
693
694static int
695open_disk(int flag)
696{
697	struct stat 	st;
698
699	if (stat(disk, &st) == -1) {
700		if (errno == ENOENT)
701			return -2;
702		warnx("can't get file status of %s", disk);
703		return -1;
704	}
705	if ( !(st.st_mode & S_IFCHR) )
706		warnx("device %s is not character special", disk);
707	if ((fd = open(disk,
708	    a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY)) == -1) {
709		if(errno == ENXIO)
710			return -2;
711		warnx("can't open device %s", disk);
712		return -1;
713	}
714	if (get_params() == -1) {
715		warnx("can't get disk parameters on %s", disk);
716		return -1;
717	}
718	return fd;
719}
720
721static ssize_t
722read_disk(off_t sector, void *buf)
723{
724	lseek(fd,(sector * 512), 0);
725	if( secsize == 0 )
726		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
727			{
728			/* try the read */
729			int size = read(fd, buf, secsize);
730			if( size == secsize )
731				/* it worked so return */
732				return secsize;
733			}
734	else
735		return read( fd, buf, secsize );
736
737	/* we failed to read at any of the sizes */
738	return -1;
739}
740
741static ssize_t
742write_disk(off_t sector, void *buf)
743{
744	lseek(fd,(sector * 512), 0);
745	/* write out in the size that the read_disk found worked */
746	return write(fd, buf, secsize);
747}
748
749static int
750get_params()
751{
752	int error;
753	u_int u;
754	off_t o;
755
756	if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
757		warnx("can't get disk parameters on %s; supplying dummy ones", disk);
758		dos_cyls = cyls = 1;
759		dos_heads = heads = 1;
760		dos_sectors = sectors = 1;
761		dos_cylsecs = cylsecs = heads * sectors;
762		disksecs = cyls * heads * sectors;
763	} else {
764		dos_cyls = cyls = disklabel.d_ncylinders;
765		dos_heads = heads = disklabel.d_ntracks;
766		dos_sectors = sectors = disklabel.d_nsectors;
767	}
768	error = ioctl(fd, DIOCGFWSECTORS, &u);
769	if (error == 0)
770		sectors = dos_sectors = u;
771	error = ioctl(fd, DIOCGFWHEADS, &u);
772	if (error == 0)
773		heads = dos_heads = u;
774
775	dos_cylsecs = cylsecs = heads * sectors;
776	disksecs = cyls * heads * sectors;
777
778	error = ioctl(fd, DIOCGSECTORSIZE, &u);
779	if (error != 0)
780		u = 512;
781
782	error = ioctl(fd, DIOCGMEDIASIZE, &o);
783	if (error == 0) {
784		disksecs = o / u;
785		cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
786	}
787
788
789	return (disksecs);
790}
791
792
793static int
794read_s0()
795{
796	mboot.bootinst_size = secsize;
797	if (mboot.bootinst != NULL)
798		free(mboot.bootinst);
799	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
800		warnx("unable to allocate buffer to read fdisk "
801		      "partition table");
802		return -1;
803	}
804	if (read_disk(0, mboot.bootinst) == -1) {
805		warnx("can't read fdisk partition table");
806		return -1;
807	}
808	if (*(uint16_t *)(void *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
809		warnx("invalid fdisk partition table found");
810		/* So should we initialize things */
811		return -1;
812	}
813	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
814	return 0;
815}
816
817static int
818write_s0()
819{
820	int	sector;
821
822	if (iotest) {
823		print_s0(-1);
824		return 0;
825	}
826	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
827	/*
828	 * write enable label sector before write (if necessary),
829	 * disable after writing.
830	 * needed if the disklabel protected area also protects
831	 * sector 0. (e.g. empty disk)
832	 */
833	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
834		if (write_disk(sector,
835			       &mboot.bootinst[sector * secsize]) == -1) {
836			warn("can't write fdisk partition table");
837			return -1;
838		}
839	return(0);
840}
841
842
843static int
844ok(const char *str)
845{
846	printf("%s [n] ", str);
847	fflush(stdout);
848	if (fgets(lbuf, LBUF, stdin) == NULL)
849		exit(1);
850	lbuf[strlen(lbuf)-1] = 0;
851
852	if (*lbuf &&
853		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
854		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
855		return 1;
856	else
857		return 0;
858}
859
860static int
861decimal(const char *str, int *num, int deflt)
862{
863	int acc = 0, c;
864	char *cp;
865
866	while (1) {
867		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
868		fflush(stdout);
869		if (fgets(lbuf, LBUF, stdin) == NULL)
870			exit(1);
871		lbuf[strlen(lbuf)-1] = 0;
872
873		if (!*lbuf)
874			return 0;
875
876		cp = lbuf;
877		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
878		if (!c)
879			return 0;
880		while ((c = *cp++)) {
881			if (c <= '9' && c >= '0')
882				acc = acc * 10 + c - '0';
883			else
884				break;
885		}
886		if (c == ' ' || c == '\t')
887			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
888		if (!c) {
889			*num = acc;
890			return 1;
891		} else
892			printf("%s is an invalid decimal number.  Try again.\n",
893				lbuf);
894	}
895
896}
897
898static const char *
899get_type(int type)
900{
901	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
902	int	counter = 0;
903	struct	part_type *ptr = part_types;
904
905
906	while(counter < numentries) {
907		if(ptr->type == type)
908			return(ptr->name);
909		ptr++;
910		counter++;
911	}
912	return("unknown");
913}
914
915
916static void
917parse_config_line(char *line, CMD *command)
918{
919    char	*cp, *end;
920
921    cp = line;
922    while (1) {
923	memset(command, 0, sizeof(*command));
924
925	while (isspace(*cp)) ++cp;
926	if (*cp == '\0' || *cp == '#')
927	    break;
928	command->cmd = *cp++;
929
930	/*
931	 * Parse args
932	 */
933	    while (1) {
934	    while (isspace(*cp)) ++cp;
935	    if (*cp == '#')
936		break;		/* found comment */
937	    if (isalpha(*cp))
938		command->args[command->n_args].argtype = *cp++;
939	    if (!isdigit(*cp))
940		break;		/* assume end of line */
941	    end = NULL;
942	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
943	    if (cp == end)
944		break;		/* couldn't parse number */
945	    cp = end;
946	    command->n_args++;
947	}
948	break;
949    }
950}
951
952
953static int
954process_geometry(CMD *command)
955{
956    int		status = 1, i;
957
958    while (1) {
959	geom_processed = 1;
960	    if (part_processed) {
961	    warnx(
962	"ERROR line %d: the geometry specification line must occur before\n\
963    all partition specifications",
964		    current_line_number);
965	    status = 0;
966	    break;
967	}
968	    if (command->n_args != 3) {
969	    warnx("ERROR line %d: incorrect number of geometry args",
970		    current_line_number);
971	    status = 0;
972	    break;
973	}
974	    dos_cyls = 0;
975	    dos_heads = 0;
976	    dos_sectors = 0;
977	    for (i = 0; i < 3; ++i) {
978		    switch (command->args[i].argtype) {
979	    case 'c':
980		dos_cyls = command->args[i].arg_val;
981		break;
982	    case 'h':
983		dos_heads = command->args[i].arg_val;
984		break;
985	    case 's':
986		dos_sectors = command->args[i].arg_val;
987		break;
988	    default:
989		warnx(
990		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
991			current_line_number, command->args[i].argtype,
992			command->args[i].argtype);
993		status = 0;
994		break;
995	    }
996	}
997	if (status == 0)
998	    break;
999
1000	dos_cylsecs = dos_heads * dos_sectors;
1001
1002	/*
1003	 * Do sanity checks on parameter values
1004	 */
1005	    if (dos_cyls == 0) {
1006	    warnx("ERROR line %d: number of cylinders not specified",
1007		    current_line_number);
1008	    status = 0;
1009	}
1010	    if (dos_cyls > 1024) {
1011	    warnx(
1012	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1013    (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1014    is dedicated to FreeBSD)",
1015		    current_line_number, dos_cyls);
1016	}
1017
1018	    if (dos_heads == 0) {
1019	    warnx("ERROR line %d: number of heads not specified",
1020		    current_line_number);
1021	    status = 0;
1022	    } else if (dos_heads > 256) {
1023	    warnx("ERROR line %d: number of heads must be within (1-256)",
1024		    current_line_number);
1025	    status = 0;
1026	}
1027
1028	    if (dos_sectors == 0) {
1029	    warnx("ERROR line %d: number of sectors not specified",
1030		    current_line_number);
1031	    status = 0;
1032	    } else if (dos_sectors > 63) {
1033	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1034		    current_line_number);
1035	    status = 0;
1036	}
1037
1038	break;
1039    }
1040    return (status);
1041}
1042
1043
1044static int
1045process_partition(CMD *command)
1046{
1047    int				status = 0, partition;
1048    u_int32_t			prev_head_boundary, prev_cyl_boundary;
1049    u_int32_t			adj_size, max_end;
1050    struct dos_partition	*partp;
1051
1052	while (1) {
1053	part_processed = 1;
1054		if (command->n_args != 4) {
1055	    warnx("ERROR line %d: incorrect number of partition args",
1056		    current_line_number);
1057	    break;
1058	}
1059	partition = command->args[0].arg_val;
1060		if (partition < 1 || partition > 4) {
1061	    warnx("ERROR line %d: invalid partition number %d",
1062		    current_line_number, partition);
1063	    break;
1064	}
1065	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1066	bzero((char *)partp, sizeof (struct dos_partition));
1067	partp->dp_typ = command->args[1].arg_val;
1068	partp->dp_start = command->args[2].arg_val;
1069	partp->dp_size = command->args[3].arg_val;
1070	max_end = partp->dp_start + partp->dp_size;
1071
1072		if (partp->dp_typ == 0) {
1073	    /*
1074	     * Get out, the partition is marked as unused.
1075	     */
1076	    /*
1077	     * Insure that it's unused.
1078	     */
1079	    bzero((char *)partp, sizeof (struct dos_partition));
1080	    status = 1;
1081	    break;
1082	}
1083
1084	/*
1085	 * Adjust start upwards, if necessary, to fall on an head boundary.
1086	 */
1087		if (partp->dp_start % dos_sectors != 0) {
1088	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1089	    if (max_end < dos_sectors ||
1090			    prev_head_boundary > max_end - dos_sectors) {
1091		/*
1092		 * Can't go past end of partition
1093		 */
1094		warnx(
1095	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1096    a head boundary",
1097			current_line_number, partition);
1098		break;
1099	    }
1100	    warnx(
1101	"WARNING: adjusting start offset of partition %d\n\
1102    from %u to %u, to fall on a head boundary",
1103		    partition, (u_int)partp->dp_start,
1104		    (u_int)(prev_head_boundary + dos_sectors));
1105	    partp->dp_start = prev_head_boundary + dos_sectors;
1106	}
1107
1108	/*
1109	 * Adjust size downwards, if necessary, to fall on a cylinder
1110	 * boundary.
1111	 */
1112	prev_cyl_boundary =
1113	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1114	if (prev_cyl_boundary > partp->dp_start)
1115	    adj_size = prev_cyl_boundary - partp->dp_start;
1116		else {
1117	    warnx(
1118	"ERROR: could not adjust partition to start on a head boundary\n\
1119    and end on a cylinder boundary.");
1120	    return (0);
1121	}
1122		if (adj_size != partp->dp_size) {
1123	    warnx(
1124	"WARNING: adjusting size of partition %d from %u to %u\n\
1125    to end on a cylinder boundary",
1126		    partition, (u_int)partp->dp_size, (u_int)adj_size);
1127	    partp->dp_size = adj_size;
1128	}
1129		if (partp->dp_size == 0) {
1130	    warnx("ERROR line %d: size of partition %d is zero",
1131		    current_line_number, partition);
1132	    break;
1133	}
1134
1135	dos(partp);
1136	status = 1;
1137	break;
1138    }
1139    return (status);
1140}
1141
1142
1143static int
1144process_active(CMD *command)
1145{
1146    int				status = 0, partition, i;
1147    struct dos_partition	*partp;
1148
1149	while (1) {
1150	active_processed = 1;
1151		if (command->n_args != 1) {
1152	    warnx("ERROR line %d: incorrect number of active args",
1153		    current_line_number);
1154	    status = 0;
1155	    break;
1156	}
1157	partition = command->args[0].arg_val;
1158		if (partition < 1 || partition > 4) {
1159	    warnx("ERROR line %d: invalid partition number %d",
1160		    current_line_number, partition);
1161	    break;
1162	}
1163	/*
1164	 * Reset active partition
1165	 */
1166	partp = ((struct dos_partition *) &mboot.parts);
1167	for (i = 0; i < NDOSPART; i++)
1168	    partp[i].dp_flag = 0;
1169	partp[partition-1].dp_flag = ACTIVE;
1170
1171	status = 1;
1172	break;
1173    }
1174    return (status);
1175}
1176
1177
1178static int
1179process_line(char *line)
1180{
1181    CMD		command;
1182    int		status = 1;
1183
1184	while (1) {
1185	parse_config_line(line, &command);
1186		switch (command.cmd) {
1187	case 0:
1188	    /*
1189	     * Comment or blank line
1190	     */
1191	    break;
1192	case 'g':
1193	    /*
1194	     * Set geometry
1195	     */
1196	    status = process_geometry(&command);
1197	    break;
1198	case 'p':
1199	    status = process_partition(&command);
1200	    break;
1201	case 'a':
1202	    status = process_active(&command);
1203	    break;
1204	default:
1205	    status = 0;
1206	    break;
1207	}
1208	break;
1209    }
1210    return (status);
1211}
1212
1213
1214static int
1215read_config(char *config_file)
1216{
1217    FILE	*fp = NULL;
1218    int		status = 1;
1219    char	buf[1010];
1220
1221	while (1) {
1222		if (strcmp(config_file, "-") != 0) {
1223	    /*
1224	     * We're not reading from stdin
1225	     */
1226			if ((fp = fopen(config_file, "r")) == NULL) {
1227		status = 0;
1228		break;
1229	    }
1230		} else {
1231	    fp = stdin;
1232	}
1233	current_line_number = 0;
1234		while (!feof(fp)) {
1235	    if (fgets(buf, sizeof(buf), fp) == NULL)
1236		break;
1237	    ++current_line_number;
1238	    status = process_line(buf);
1239	    if (status == 0)
1240		break;
1241	    }
1242	break;
1243    }
1244	if (fp) {
1245	/*
1246	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1247	 */
1248	fclose(fp);
1249    }
1250    return (status);
1251}
1252
1253
1254static void
1255reset_boot(void)
1256{
1257    int				i;
1258    struct dos_partition	*partp;
1259
1260    init_boot();
1261	for (i = 0; i < 4; ++i) {
1262	partp = ((struct dos_partition *) &mboot.parts) + i;
1263	bzero((char *)partp, sizeof (struct dos_partition));
1264    }
1265}
1266
1267static int
1268sanitize_partition(struct dos_partition *partp)
1269{
1270    u_int32_t			prev_head_boundary, prev_cyl_boundary;
1271    u_int32_t			max_end, size, start;
1272
1273    start = partp->dp_start;
1274    size = partp->dp_size;
1275    max_end = start + size;
1276    /* Only allow a zero size if the partition is being marked unused. */
1277    if (size == 0) {
1278	if (start == 0 && partp->dp_typ == 0)
1279	    return (1);
1280	warnx("ERROR: size of partition is zero");
1281	return (0);
1282    }
1283    /* Return if no adjustment is necessary. */
1284    if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1285	return (1);
1286
1287    if (start % dos_sectors != 0)
1288	warnx("WARNING: partition does not start on a head boundary");
1289    if ((start  +size) % dos_sectors != 0)
1290	warnx("WARNING: partition does not end on a cylinder boundary");
1291    warnx("WARNING: this may confuse the BIOS or some operating systems");
1292    if (!ok("Correct this automatically?"))
1293	return (1);
1294
1295    /*
1296     * Adjust start upwards, if necessary, to fall on an head boundary.
1297     */
1298    if (start % dos_sectors != 0) {
1299	prev_head_boundary = start / dos_sectors * dos_sectors;
1300	if (max_end < dos_sectors ||
1301	    prev_head_boundary >= max_end - dos_sectors) {
1302	    /*
1303	     * Can't go past end of partition
1304	     */
1305	    warnx(
1306    "ERROR: unable to adjust start of partition to fall on a head boundary");
1307	    return (0);
1308        }
1309	start = prev_head_boundary + dos_sectors;
1310    }
1311
1312    /*
1313     * Adjust size downwards, if necessary, to fall on a cylinder
1314     * boundary.
1315     */
1316    prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1317    if (prev_cyl_boundary > start)
1318	size = prev_cyl_boundary - start;
1319    else {
1320	warnx("ERROR: could not adjust partition to start on a head boundary\n\
1321    and end on a cylinder boundary.");
1322	return (0);
1323    }
1324
1325    /* Finally, commit any changes to partp and return. */
1326    if (start != partp->dp_start) {
1327	warnx("WARNING: adjusting start offset of partition to %u",
1328	    (u_int)start);
1329	partp->dp_start = start;
1330    }
1331    if (size != partp->dp_size) {
1332	warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1333	partp->dp_size = size;
1334    }
1335
1336    return (1);
1337}
1338
1339/*
1340 * Try figuring out the root device's canonical disk name.
1341 * The following choices are considered:
1342 *   /dev/ad0s1a     => /dev/ad0
1343 *   /dev/da0a       => /dev/da0
1344 *   /dev/vinum/root => /dev/vinum/root
1345 */
1346static char *
1347get_rootdisk(void)
1348{
1349	struct statfs rootfs;
1350	regex_t re;
1351#define NMATCHES 2
1352	regmatch_t rm[NMATCHES];
1353	char *s;
1354	int rv;
1355
1356	if (statfs("/", &rootfs) == -1)
1357		err(1, "statfs(\"/\")");
1358
1359	if ((rv = regcomp(&re, "^(/dev/.*)(\\d+(s\\d+)?[a-h])?$",
1360		    REG_EXTENDED)) != 0)
1361		errx(1, "regcomp() failed (%d)", rv);
1362	if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1363		errx(1,
1364"mounted root fs resource doesn't match expectations (regexec returned %d)",
1365		    rv);
1366	if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1367		errx(1, "out of memory");
1368	memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1369	    rm[1].rm_eo - rm[1].rm_so);
1370	s[rm[1].rm_eo - rm[1].rm_so] = 0;
1371
1372	return s;
1373}
1374