xref: /freebsd/sbin/fdisk/fdisk.c (revision de78c288db085f012572d4721fb66a45578e696f)
15b81b6b3SRodney W. Grimes /*
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1992 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie Mellon
245b81b6b3SRodney W. Grimes  * the rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes 
27c69284caSDavid E. O'Brien #include <sys/cdefs.h>
28c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
29d98b1668SPhilippe Charnier 
303f8ba8b5SPoul-Henning Kamp #include <sys/disk.h>
315b81b6b3SRodney W. Grimes #include <sys/disklabel.h>
323bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h>
331a03d6d5SPoul-Henning Kamp #include <sys/endian.h>
34df77f711SJoerg Wunsch #include <sys/param.h>
355b81b6b3SRodney W. Grimes #include <sys/stat.h>
36df77f711SJoerg Wunsch #include <sys/mount.h>
37d98b1668SPhilippe Charnier #include <ctype.h>
385b81b6b3SRodney W. Grimes #include <fcntl.h>
39d98b1668SPhilippe Charnier #include <err.h>
40d98b1668SPhilippe Charnier #include <errno.h>
41de78c288SPoul-Henning Kamp #include <libgeom.h>
42df77f711SJoerg Wunsch #include <paths.h>
43df77f711SJoerg Wunsch #include <regex.h>
44689fee87SBruce Evans #include <stdint.h>
45d98b1668SPhilippe Charnier #include <stdio.h>
46d98b1668SPhilippe Charnier #include <stdlib.h>
47d98b1668SPhilippe Charnier #include <string.h>
484be1e61bSAlexander Langer #include <unistd.h>
495b81b6b3SRodney W. Grimes 
505b81b6b3SRodney W. Grimes int iotest;
515b81b6b3SRodney W. Grimes 
525b81b6b3SRodney W. Grimes #define LBUF 100
535b81b6b3SRodney W. Grimes static char lbuf[LBUF];
545b81b6b3SRodney W. Grimes 
555b81b6b3SRodney W. Grimes /*
565b81b6b3SRodney W. Grimes  *
575b81b6b3SRodney W. Grimes  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
585b81b6b3SRodney W. Grimes  *
595b81b6b3SRodney W. Grimes  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
605b81b6b3SRodney W. Grimes  *	Copyright (c) 1989	Robert. V. Baron
615b81b6b3SRodney W. Grimes  *	Created.
625b81b6b3SRodney W. Grimes  */
635b81b6b3SRodney W. Grimes 
645b81b6b3SRodney W. Grimes #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
655b81b6b3SRodney W. Grimes 
665b81b6b3SRodney W. Grimes #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
675b81b6b3SRodney W. Grimes 
687cb29d33SSøren Schmidt #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
697cb29d33SSøren Schmidt #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
70041b8b00SPoul-Henning Kamp static int secsize = 0;		/* the sensed sector size */
715b81b6b3SRodney W. Grimes 
72041b8b00SPoul-Henning Kamp static char *disk;
73e3038c6eSJoerg Wunsch 
74041b8b00SPoul-Henning Kamp static int cyls, sectors, heads, cylsecs, disksecs;
755b81b6b3SRodney W. Grimes 
76041b8b00SPoul-Henning Kamp struct mboot {
77d98b1668SPhilippe Charnier 	unsigned char padding[2]; /* force the longs to be long aligned */
7885c2cf30SJohn Baldwin 	unsigned char *bootinst;  /* boot code */
7985c2cf30SJohn Baldwin 	off_t bootinst_size;
801a03d6d5SPoul-Henning Kamp 	struct	dos_partition parts[NDOSPART];
815b81b6b3SRodney W. Grimes };
82041b8b00SPoul-Henning Kamp 
83041b8b00SPoul-Henning Kamp static struct mboot mboot;
84de78c288SPoul-Henning Kamp static int fd;
85f353c761SPoul-Henning Kamp 
865b81b6b3SRodney W. Grimes #define ACTIVE 0x80
875b81b6b3SRodney W. Grimes 
88041b8b00SPoul-Henning Kamp static uint dos_cyls;
89041b8b00SPoul-Henning Kamp static uint dos_heads;
90041b8b00SPoul-Henning Kamp static uint dos_sectors;
91041b8b00SPoul-Henning Kamp static uint dos_cylsecs;
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
945b81b6b3SRodney W. Grimes #define DOSCYL(c)	(c & 0xff)
955b81b6b3SRodney W. Grimes 
96f46af505SJordan K. Hubbard #define MAX_ARGS	10
97f46af505SJordan K. Hubbard 
98f46af505SJordan K. Hubbard static int	current_line_number;
99f46af505SJordan K. Hubbard 
100f46af505SJordan K. Hubbard static int	geom_processed = 0;
101f46af505SJordan K. Hubbard static int	part_processed = 0;
102f46af505SJordan K. Hubbard static int	active_processed = 0;
103f46af505SJordan K. Hubbard 
104f46af505SJordan K. Hubbard typedef struct cmd {
105f46af505SJordan K. Hubbard     char		cmd;
106f46af505SJordan K. Hubbard     int			n_args;
107f46af505SJordan K. Hubbard     struct arg {
108f46af505SJordan K. Hubbard 	char	argtype;
109f46af505SJordan K. Hubbard 	int	arg_val;
110f46af505SJordan K. Hubbard     }			args[MAX_ARGS];
111f46af505SJordan K. Hubbard } CMD;
112f46af505SJordan K. Hubbard 
11326721a89SRobert Nordier static int B_flag  = 0;		/* replace boot code */
11410b0ee93SWarner Losh static int I_flag  = 0;		/* use entire disk for FreeBSD */
1155b81b6b3SRodney W. Grimes static int a_flag  = 0;		/* set active partition */
11626721a89SRobert Nordier static char *b_flag = NULL;	/* path to boot code */
1175b81b6b3SRodney W. Grimes static int i_flag  = 0;		/* replace partition data */
1185b81b6b3SRodney W. Grimes static int u_flag  = 0;		/* update partition data */
11910b0ee93SWarner Losh static int s_flag  = 0;		/* Print a summary and exit */
12005a213f1STom Rhodes static int t_flag  = 0;		/* test only */
121f46af505SJordan K. Hubbard static char *f_flag = NULL;	/* Read config info from file */
122f46af505SJordan K. Hubbard static int v_flag  = 0;		/* Be verbose */
1235b81b6b3SRodney W. Grimes 
124041b8b00SPoul-Henning Kamp static struct part_type
1255b81b6b3SRodney W. Grimes {
1265b81b6b3SRodney W. Grimes 	unsigned char type;
127041b8b00SPoul-Henning Kamp 	const char *name;
128041b8b00SPoul-Henning Kamp } part_types[] = {
1295b81b6b3SRodney W. Grimes 	 {0x00, "unused"}
1305b81b6b3SRodney W. Grimes 	,{0x01, "Primary DOS with 12 bit FAT"}
1315b81b6b3SRodney W. Grimes 	,{0x02, "XENIX / file system"}
1325b81b6b3SRodney W. Grimes 	,{0x03, "XENIX /usr file system"}
133978d3bfeSJosef Karthauser 	,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
1345b81b6b3SRodney W. Grimes 	,{0x05, "Extended DOS"}
135978d3bfeSJosef Karthauser 	,{0x06, "Primary 'big' DOS (>= 32MB)"}
136691e5f80SGuy Helmer 	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
137978d3bfeSJosef Karthauser 	,{0x08, "AIX file system or SplitDrive"}
1385b81b6b3SRodney W. Grimes 	,{0x09, "AIX boot partition or Coherent"}
139978d3bfeSJosef Karthauser 	,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
14026555b64SAndrey A. Chernov 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
141978d3bfeSJosef Karthauser 	,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
142978d3bfeSJosef Karthauser 	,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
143978d3bfeSJosef Karthauser 	,{0x0F, "Extended DOS (LBA)"}
1445b81b6b3SRodney W. Grimes 	,{0x10, "OPUS"}
145978d3bfeSJosef Karthauser 	,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
146978d3bfeSJosef Karthauser 	,{0x12, "Compaq diagnostics"}
147978d3bfeSJosef Karthauser 	,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
148978d3bfeSJosef Karthauser 	,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
149978d3bfeSJosef Karthauser 	,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
150978d3bfeSJosef Karthauser 	,{0x18, "AST Windows swapfile"}
151978d3bfeSJosef Karthauser 	,{0x24, "NEC DOS"}
152978d3bfeSJosef Karthauser 	,{0x3C, "PartitionMagic recovery"}
15335bfe0c3SBrian Somers 	,{0x39, "plan9"}
1545b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
155978d3bfeSJosef Karthauser 	,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
156978d3bfeSJosef Karthauser 	,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
157978d3bfeSJosef Karthauser 	,{0x43, "Linux native (sharing disk with DRDOS)"}
158691e5f80SGuy Helmer 	,{0x4D, "QNX 4.2 Primary"}
159691e5f80SGuy Helmer 	,{0x4E, "QNX 4.2 Secondary"}
160691e5f80SGuy Helmer 	,{0x4F, "QNX 4.2 Tertiary"}
161978d3bfeSJosef Karthauser 	,{0x50, "DM (disk manager)"}
162978d3bfeSJosef Karthauser 	,{0x51, "DM6 Aux1 (or Novell)"}
1635b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
164978d3bfeSJosef Karthauser 	,{0x53, "DM6 Aux3"}
165978d3bfeSJosef Karthauser 	,{0x54, "DM6"}
166978d3bfeSJosef Karthauser 	,{0x55, "EZ-Drive (disk manager)"}
167978d3bfeSJosef Karthauser 	,{0x56, "Golden Bow (disk manager)"}
168978d3bfeSJosef Karthauser 	,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
169978d3bfeSJosef Karthauser 	,{0x61, "SpeedStor"}
170978d3bfeSJosef Karthauser 	,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
171978d3bfeSJosef Karthauser 	,{0x64, "Novell Netware/286 2.xx"}
172978d3bfeSJosef Karthauser 	,{0x65, "Novell Netware/386 3.xx"}
173978d3bfeSJosef Karthauser 	,{0x70, "DiskSecure Multi-Boot"}
1745b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
175978d3bfeSJosef Karthauser 	,{0x77, "QNX4.x"}
176978d3bfeSJosef Karthauser 	,{0x78, "QNX4.x 2nd part"}
177978d3bfeSJosef Karthauser 	,{0x79, "QNX4.x 3rd part"}
178978d3bfeSJosef Karthauser 	,{0x80, "Minix until 1.4a"}
179041b8b00SPoul-Henning Kamp 	,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
18063ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
181978d3bfeSJosef Karthauser 	,{0x83, "Linux native"}
182978d3bfeSJosef Karthauser 	,{0x84, "OS/2 hidden C: drive"}
183978d3bfeSJosef Karthauser 	,{0x85, "Linux extended"}
184978d3bfeSJosef Karthauser 	,{0x86, "NTFS volume set??"}
185978d3bfeSJosef Karthauser 	,{0x87, "NTFS volume set??"}
1865b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba file system"}
1875b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
188d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
189197ef307SJosef Karthauser 	,{0xA0, "Suspend to Disk"}
19049f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
191e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
192978d3bfeSJosef Karthauser 	,{0xA7, "NeXTSTEP"}
193ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1941254a3baSGreg Lehey 	,{0xAC, "IBM JFS"}
1955b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 file system"}
1965b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
197bb007328SHartmut Brandt 	,{0xBE, "Solaris x86 boot"}
19859a51468SPoul-Henning Kamp 	,{0xBF, "Solaris x86 (new)"}
199978d3bfeSJosef Karthauser 	,{0xC1, "DRDOS/sec with 12-bit FAT"}
200978d3bfeSJosef Karthauser 	,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
201978d3bfeSJosef Karthauser 	,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
202978d3bfeSJosef Karthauser 	,{0xC7, "Syrinx"}
203978d3bfeSJosef Karthauser 	,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
204978d3bfeSJosef Karthauser 	,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
205978d3bfeSJosef Karthauser 	,{0xE3, "DOS R/O or SpeedStor"}
206978d3bfeSJosef Karthauser 	,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
207978d3bfeSJosef Karthauser 	,{0xEB, "BeOS file system"}
208a528d318SPeter Wemm 	,{0xEE, "EFI GPT"}
2092175f5f1SBruce Evans 	,{0xEF, "EFI System Partition"}
210978d3bfeSJosef Karthauser 	,{0xF1, "SpeedStor"}
2115b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
212978d3bfeSJosef Karthauser 	,{0xF4, "SpeedStor large partition"}
213978d3bfeSJosef Karthauser 	,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
214978d3bfeSJosef Karthauser 	,{0xFF, "Xenix bad blocks table"}
2155b81b6b3SRodney W. Grimes };
2165b81b6b3SRodney W. Grimes 
2174be1e61bSAlexander Langer static void print_s0(int which);
2184be1e61bSAlexander Langer static void print_part(int i);
2194be1e61bSAlexander Langer static void init_sector0(unsigned long start);
220f46af505SJordan K. Hubbard static void init_boot(void);
2214be1e61bSAlexander Langer static void change_part(int i);
222041b8b00SPoul-Henning Kamp static void print_params(void);
2234be1e61bSAlexander Langer static void change_active(int which);
224041b8b00SPoul-Henning Kamp static void change_code(void);
225041b8b00SPoul-Henning Kamp static void get_params_to_use(void);
226df77f711SJoerg Wunsch static char *get_rootdisk(void);
22763692187SIan Dowse static void dos(struct dos_partition *partp);
228041b8b00SPoul-Henning Kamp static int open_disk(int flag);
2294be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
230de78c288SPoul-Henning Kamp static int write_disk(off_t sector, void *buf);
231041b8b00SPoul-Henning Kamp static int get_params(void);
232041b8b00SPoul-Henning Kamp static int read_s0(void);
233041b8b00SPoul-Henning Kamp static int write_s0(void);
234041b8b00SPoul-Henning Kamp static int ok(const char *str);
235041b8b00SPoul-Henning Kamp static int decimal(const char *str, int *num, int deflt);
236041b8b00SPoul-Henning Kamp static const char *get_type(int type);
237f46af505SJordan K. Hubbard static int read_config(char *config_file);
238f46af505SJordan K. Hubbard static void reset_boot(void);
239b594e0feSJohn Baldwin static int sanitize_partition(struct dos_partition *);
240d98b1668SPhilippe Charnier static void usage(void);
2414be1e61bSAlexander Langer 
2424be1e61bSAlexander Langer int
2434be1e61bSAlexander Langer main(int argc, char *argv[])
2445b81b6b3SRodney W. Grimes {
245df77f711SJoerg Wunsch 	struct	stat sb;
24626721a89SRobert Nordier 	int	c, i;
247041b8b00SPoul-Henning Kamp 	int	partition = -1;
248041b8b00SPoul-Henning Kamp 	struct	dos_partition *partp;
2495b81b6b3SRodney W. Grimes 
25010b0ee93SWarner Losh 	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
25126721a89SRobert Nordier 		switch (c) {
25226721a89SRobert Nordier 		case 'B':
25326721a89SRobert Nordier 			B_flag = 1;
2544ddd60b9SBrian Somers 			break;
25510b0ee93SWarner Losh 		case 'I':
25610b0ee93SWarner Losh 			I_flag = 1;
25710b0ee93SWarner Losh 			break;
2585b81b6b3SRodney W. Grimes 		case 'a':
2595b81b6b3SRodney W. Grimes 			a_flag = 1;
2605b81b6b3SRodney W. Grimes 			break;
26112d910e9SRobert Nordier 		case 'b':
26226721a89SRobert Nordier 			b_flag = optarg;
26312d910e9SRobert Nordier 			break;
264f46af505SJordan K. Hubbard 		case 'f':
26526721a89SRobert Nordier 			f_flag = optarg;
266f46af505SJordan K. Hubbard 			break;
2675b81b6b3SRodney W. Grimes 		case 'i':
2685b81b6b3SRodney W. Grimes 			i_flag = 1;
2695b81b6b3SRodney W. Grimes 			break;
27010b0ee93SWarner Losh 		case 's':
27110b0ee93SWarner Losh 			s_flag = 1;
27210b0ee93SWarner Losh 			break;
273f46af505SJordan K. Hubbard 		case 't':
274f46af505SJordan K. Hubbard 			t_flag = 1;
27526721a89SRobert Nordier 			break;
27626721a89SRobert Nordier 		case 'u':
27726721a89SRobert Nordier 			u_flag = 1;
27826721a89SRobert Nordier 			break;
279f46af505SJordan K. Hubbard 		case 'v':
280f46af505SJordan K. Hubbard 			v_flag = 1;
281f46af505SJordan K. Hubbard 			break;
28226721a89SRobert Nordier 		case '1':
28326721a89SRobert Nordier 		case '2':
28426721a89SRobert Nordier 		case '3':
28526721a89SRobert Nordier 		case '4':
28626721a89SRobert Nordier 			partition = c - '0';
28726721a89SRobert Nordier 			break;
2885b81b6b3SRodney W. Grimes 		default:
289d98b1668SPhilippe Charnier 			usage();
2905b81b6b3SRodney W. Grimes 		}
29126721a89SRobert Nordier 	if (f_flag || i_flag)
29226721a89SRobert Nordier 		u_flag = 1;
29326721a89SRobert Nordier 	if (t_flag)
29426721a89SRobert Nordier 		v_flag = 1;
29526721a89SRobert Nordier 	argc -= optind;
29626721a89SRobert Nordier 	argv += optind;
2975b81b6b3SRodney W. Grimes 
298df77f711SJoerg Wunsch 	if (argc == 0) {
299df77f711SJoerg Wunsch 		disk = get_rootdisk();
300df77f711SJoerg Wunsch 	} else {
301df77f711SJoerg Wunsch 		if (stat(argv[0], &sb) == 0) {
302df77f711SJoerg Wunsch 			/* OK, full pathname given */
3035b81b6b3SRodney W. Grimes 			disk = argv[0];
30426cd3449SRuslan Ermilov 		} else if (errno == ENOENT && argv[0][0] != '/') {
305df77f711SJoerg Wunsch 			/* Try prepending "/dev" */
306041b8b00SPoul-Henning Kamp 			asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
307041b8b00SPoul-Henning Kamp 			if (disk == NULL)
308df77f711SJoerg Wunsch 				errx(1, "out of memory");
309df77f711SJoerg Wunsch 		} else {
310df77f711SJoerg Wunsch 			/* other stat error, let it fail below */
311df77f711SJoerg Wunsch 			disk = argv[0];
312e3038c6eSJoerg Wunsch 		}
313df77f711SJoerg Wunsch 	}
3145b81b6b3SRodney W. Grimes 	if (open_disk(u_flag) < 0)
315d98b1668SPhilippe Charnier 		err(1, "cannot open disk %s", disk);
31685c2cf30SJohn Baldwin 
31785c2cf30SJohn Baldwin 	/* (abu)use mboot.bootinst to probe for the sector size */
31885c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
31985c2cf30SJohn Baldwin 		err(1, "cannot allocate buffer to determine disk sector size");
32085c2cf30SJohn Baldwin 	read_disk(0, mboot.bootinst);
321a12de062SJohn Baldwin 	free(mboot.bootinst);
322a12de062SJohn Baldwin 	mboot.bootinst = NULL;
32385c2cf30SJohn Baldwin 
324041b8b00SPoul-Henning Kamp 	if (s_flag) {
32510b0ee93SWarner Losh 		if (read_s0())
32610b0ee93SWarner Losh 			err(1, "read_s0");
32710b0ee93SWarner Losh 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
32810b0ee93SWarner Losh 		    dos_sectors);
32910b0ee93SWarner Losh 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
33010b0ee93SWarner Losh 		for (i = 0; i < NDOSPART; i++) {
33110b0ee93SWarner Losh 			partp = ((struct dos_partition *) &mboot.parts) + i;
33210b0ee93SWarner Losh 			if (partp->dp_start == 0 && partp->dp_size == 0)
33310b0ee93SWarner Losh 				continue;
33410b0ee93SWarner Losh 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
33510b0ee93SWarner Losh 			    (u_long) partp->dp_start,
33610b0ee93SWarner Losh 			    (u_long) partp->dp_size, partp->dp_typ,
33710b0ee93SWarner Losh 			    partp->dp_flag);
33810b0ee93SWarner Losh 		}
33910b0ee93SWarner Losh 		exit(0);
34010b0ee93SWarner Losh 	}
3415b81b6b3SRodney W. Grimes 
3425b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
343f46af505SJordan K. Hubbard 
344041b8b00SPoul-Henning Kamp 	if (I_flag) {
345e6fb3ddeSPoul-Henning Kamp 		read_s0();
346e6fb3ddeSPoul-Henning Kamp 		reset_boot();
347e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
348e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
349e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
350e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
351b594e0feSJohn Baldwin 		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
352b594e0feSJohn Baldwin 		    dos_sectors;
35363692187SIan Dowse 		dos(partp);
354e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
355e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
35605a213f1STom Rhodes 		if (!t_flag)
357e6fb3ddeSPoul-Henning Kamp 			write_s0();
358e6fb3ddeSPoul-Henning Kamp 		exit(0);
359e6fb3ddeSPoul-Henning Kamp 	}
360041b8b00SPoul-Henning Kamp 	if (f_flag) {
361f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
362f46af505SJordan K. Hubbard 		reset_boot();
363f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
364f46af505SJordan K. Hubbard 		exit(1);
365f46af505SJordan K. Hubbard 	    if (v_flag)
366f46af505SJordan K. Hubbard 		print_s0(-1);
367f46af505SJordan K. Hubbard 	    if (!t_flag)
368f46af505SJordan K. Hubbard 		write_s0();
369041b8b00SPoul-Henning Kamp 	} else {
3705b81b6b3SRodney W. Grimes 	    if(u_flag)
3715b81b6b3SRodney W. Grimes 		get_params_to_use();
3725b81b6b3SRodney W. Grimes 	    else
3735b81b6b3SRodney W. Grimes 		print_params();
3745b81b6b3SRodney W. Grimes 
3755b81b6b3SRodney W. Grimes 	    if (read_s0())
376b594e0feSJohn Baldwin 		init_sector0(dos_sectors);
3775b81b6b3SRodney W. Grimes 
3787cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3795b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3805b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3815b81b6b3SRodney W. Grimes 	    if (partition == -1)
3824ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3835b81b6b3SRodney W. Grimes 		    change_part(i);
3845b81b6b3SRodney W. Grimes 	    else
3855b81b6b3SRodney W. Grimes 		change_part(partition);
3865b81b6b3SRodney W. Grimes 
3875b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3885b81b6b3SRodney W. Grimes 		change_active(partition);
3895b81b6b3SRodney W. Grimes 
39026721a89SRobert Nordier 	    if (B_flag)
39112d910e9SRobert Nordier 		change_code();
39212d910e9SRobert Nordier 
39326721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
394041b8b00SPoul-Henning Kamp 		if (!t_flag) {
3955b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
3965b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
397f46af505SJordan K. Hubbard 		}
3985b81b6b3SRodney W. Grimes 		print_s0(-1);
399041b8b00SPoul-Henning Kamp 		if (!t_flag) {
4005b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
4015b81b6b3SRodney W. Grimes 			write_s0();
402041b8b00SPoul-Henning Kamp 		} else {
403f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
404f46af505SJordan K. Hubbard 		}
405f46af505SJordan K. Hubbard 	    }
406f46af505SJordan K. Hubbard 	}
4075b81b6b3SRodney W. Grimes 
4085b81b6b3SRodney W. Grimes 	exit(0);
409d98b1668SPhilippe Charnier }
4105b81b6b3SRodney W. Grimes 
411d98b1668SPhilippe Charnier static void
412d98b1668SPhilippe Charnier usage()
413d98b1668SPhilippe Charnier {
41426721a89SRobert Nordier 	fprintf(stderr, "%s%s",
415ed1235adSJohn Baldwin 		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
41626721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
417d98b1668SPhilippe Charnier         exit(1);
4185b81b6b3SRodney W. Grimes }
4195b81b6b3SRodney W. Grimes 
4204be1e61bSAlexander Langer static void
4214be1e61bSAlexander Langer print_s0(int which)
4225b81b6b3SRodney W. Grimes {
4235b81b6b3SRodney W. Grimes 	int	i;
4245b81b6b3SRodney W. Grimes 
4255b81b6b3SRodney W. Grimes 	print_params();
4265b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4275b81b6b3SRodney W. Grimes 	if (which == -1)
4284ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4295b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4305b81b6b3SRodney W. Grimes 	else
4315b81b6b3SRodney W. Grimes 		print_part(which);
4325b81b6b3SRodney W. Grimes }
4335b81b6b3SRodney W. Grimes 
434041b8b00SPoul-Henning Kamp static struct dos_partition mtpart;
4355b81b6b3SRodney W. Grimes 
4364be1e61bSAlexander Langer static void
4374be1e61bSAlexander Langer print_part(int i)
4385b81b6b3SRodney W. Grimes {
439637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
440637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4415b81b6b3SRodney W. Grimes 
4424ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4435b81b6b3SRodney W. Grimes 
4445b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4455b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4465b81b6b3SRodney W. Grimes 		return;
4475b81b6b3SRodney W. Grimes 	}
448637fe2f7SJustin T. Gibbs 	/*
449637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
450637fe2f7SJustin T. Gibbs 	 */
451637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
452637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
453637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
454978d3bfeSJosef Karthauser 	printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
455978d3bfeSJosef Karthauser 	    get_type(partp->dp_typ));
456689fee87SBruce Evans 	printf("    start %lu, size %lu (%ju Meg), flag %x%s\n",
457ba198492SBruce Evans 		(u_long)partp->dp_start,
458ba198492SBruce Evans 		(u_long)partp->dp_size,
459689fee87SBruce Evans 		(uintmax_t)part_mb,
460680426beSDavid E. O'Brien 		partp->dp_flag,
461680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4626580291bSDavid E. O'Brien 	printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
4635b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4645b81b6b3SRodney W. Grimes 		,partp->dp_shd
4656580291bSDavid E. O'Brien 		,DPSECT(partp->dp_ssect)
4665b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4676580291bSDavid E. O'Brien 		,partp->dp_ehd
4686580291bSDavid E. O'Brien 		,DPSECT(partp->dp_esect));
4695b81b6b3SRodney W. Grimes }
4705b81b6b3SRodney W. Grimes 
471f46af505SJordan K. Hubbard 
472f46af505SJordan K. Hubbard static void
473f46af505SJordan K. Hubbard init_boot(void)
474f46af505SJordan K. Hubbard {
47529ea697dSPeter Wemm #ifndef __ia64__
47626721a89SRobert Nordier 	const char *fname;
477041b8b00SPoul-Henning Kamp 	int fdesc, n;
47885c2cf30SJohn Baldwin 	struct stat sb;
47926721a89SRobert Nordier 
48026721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
481041b8b00SPoul-Henning Kamp 	if ((fdesc = open(fname, O_RDONLY)) == -1 ||
482041b8b00SPoul-Henning Kamp 	    fstat(fdesc, &sb) == -1)
48385c2cf30SJohn Baldwin 		err(1, "%s", fname);
48485c2cf30SJohn Baldwin 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
48585c2cf30SJohn Baldwin 		errx(1, "%s: length must be a multiple of sector size", fname);
4862b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
4872b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
48885c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
48985c2cf30SJohn Baldwin 		errx(1, "%s: unable to allocate read buffer", fname);
490041b8b00SPoul-Henning Kamp 	if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
491041b8b00SPoul-Henning Kamp 	    close(fdesc))
49226721a89SRobert Nordier 		err(1, "%s", fname);
49385c2cf30SJohn Baldwin 	if (n != mboot.bootinst_size)
49485c2cf30SJohn Baldwin 		errx(1, "%s: short read", fname);
49529ea697dSPeter Wemm #else
49629ea697dSPeter Wemm 	if (mboot.bootinst != NULL)
49729ea697dSPeter Wemm 		free(mboot.bootinst);
49829ea697dSPeter Wemm 	mboot.bootinst_size = secsize;
49929ea697dSPeter Wemm 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
50029ea697dSPeter Wemm 		errx(1, "unable to allocate boot block buffer");
50129ea697dSPeter Wemm 	memset(mboot.bootinst, 0, mboot.bootinst_size);
5021a03d6d5SPoul-Henning Kamp 	le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
50329ea697dSPeter Wemm #endif
504f46af505SJordan K. Hubbard }
505f46af505SJordan K. Hubbard 
506f46af505SJordan K. Hubbard 
5074be1e61bSAlexander Langer static void
5084be1e61bSAlexander Langer init_sector0(unsigned long start)
5095b81b6b3SRodney W. Grimes {
510a67ed623SPoul-Henning Kamp 	struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[0]);
5115b81b6b3SRodney W. Grimes 
512f46af505SJordan K. Hubbard 	init_boot();
5135b81b6b3SRodney W. Grimes 
5145b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
5155b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
516b594e0feSJohn Baldwin 	start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
51785c2cf30SJohn Baldwin 	if(start == 0)
51885c2cf30SJohn Baldwin 		start = dos_sectors;
5195b81b6b3SRodney W. Grimes 	partp->dp_start = start;
520b594e0feSJohn Baldwin 	partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
5215b81b6b3SRodney W. Grimes 
52263692187SIan Dowse 	dos(partp);
5235b81b6b3SRodney W. Grimes }
5245b81b6b3SRodney W. Grimes 
5254be1e61bSAlexander Langer static void
5264be1e61bSAlexander Langer change_part(int i)
5275b81b6b3SRodney W. Grimes {
5284ddd60b9SBrian Somers 	struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
5295b81b6b3SRodney W. Grimes 
5305b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
5315b81b6b3SRodney W. Grimes     print_part(i);
5325b81b6b3SRodney W. Grimes 
5335b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
5345b81b6b3SRodney W. Grimes 	int tmp;
5355b81b6b3SRodney W. Grimes 
5365b81b6b3SRodney W. Grimes 	if (i_flag) {
5375b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
538a67ed623SPoul-Henning Kamp 		if (i == 1) {
5395b81b6b3SRodney W. Grimes 			init_sector0(1);
540a67ed623SPoul-Henning Kamp 			printf("\nThe static data for the slice 1 has been reinitialized to:\n");
5415b81b6b3SRodney W. Grimes 			print_part(i);
5425b81b6b3SRodney W. Grimes 		}
5435b81b6b3SRodney W. Grimes 	}
5445b81b6b3SRodney W. Grimes 
5455b81b6b3SRodney W. Grimes 	do {
546680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5475b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5485b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
54963692187SIan Dowse 		if (!sanitize_partition(partp)) {
55063692187SIan Dowse 			warnx("ERROR: failed to adjust; setting sysid to 0");
55163692187SIan Dowse 			partp->dp_typ = 0;
55263692187SIan Dowse 		}
5535b81b6b3SRodney W. Grimes 
5544b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5555b81b6b3SRodney W. Grimes 		{
5565b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5575b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5585b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5595b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5605b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5610469c254SDavid E. O'Brien 			Decimal("beginning head", thd, tmp);
5626580291bSDavid E. O'Brien 			Decimal("beginning sector", tsec, tmp);
5635b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5645b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5655b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5665b81b6b3SRodney W. Grimes 
5675b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5685b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5695b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5705b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5710469c254SDavid E. O'Brien 			Decimal("ending head", thd, tmp);
5726580291bSDavid E. O'Brien 			Decimal("ending sector", tsec, tmp);
5735b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5745b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5755b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
57663692187SIan Dowse 		} else
57763692187SIan Dowse 			dos(partp);
5785b81b6b3SRodney W. Grimes 
5795b81b6b3SRodney W. Grimes 		print_part(i);
5805b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
5815b81b6b3SRodney W. Grimes     }
5825b81b6b3SRodney W. Grimes }
5835b81b6b3SRodney W. Grimes 
5844be1e61bSAlexander Langer static void
5855b81b6b3SRodney W. Grimes print_params()
5865b81b6b3SRodney W. Grimes {
5875b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
5885b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5895b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
5907fa181d4SYoshihiro Takahashi 	if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
5915b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
5925b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
5935b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5945b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
5955b81b6b3SRodney W. Grimes }
5965b81b6b3SRodney W. Grimes 
5974be1e61bSAlexander Langer static void
5984be1e61bSAlexander Langer change_active(int which)
5995b81b6b3SRodney W. Grimes {
60063692187SIan Dowse 	struct dos_partition *partp = &mboot.parts[0];
60163692187SIan Dowse 	int active, i, new, tmp;
6025b81b6b3SRodney W. Grimes 
60363692187SIan Dowse 	active = -1;
60463692187SIan Dowse 	for (i = 0; i < NDOSPART; i++) {
60563692187SIan Dowse 		if ((partp[i].dp_flag & ACTIVE) == 0)
60663692187SIan Dowse 			continue;
60763692187SIan Dowse 		printf("Partition %d is marked active\n", i + 1);
60863692187SIan Dowse 		if (active == -1)
60963692187SIan Dowse 			active = i + 1;
61063692187SIan Dowse 	}
6115b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
6125b81b6b3SRodney W. Grimes 		active = which;
61363692187SIan Dowse 	else if (active == -1)
61463692187SIan Dowse 		active = 1;
61563692187SIan Dowse 
6160b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
6170b461cd7SBruce Evans 		return;
618680426beSDavid E. O'Brien setactive:
619680426beSDavid E. O'Brien 	do {
62063692187SIan Dowse 		new = active;
62163692187SIan Dowse 		Decimal("active partition", new, tmp);
62263692187SIan Dowse 		if (new < 1 || new > 4) {
623680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
624680426beSDavid E. O'Brien 					"  Try again.\n");
625680426beSDavid E. O'Brien 			goto setactive;
626680426beSDavid E. O'Brien 		}
62763692187SIan Dowse 		active = new;
628680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
6295b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
6305b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
6314ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
6324ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
6335b81b6b3SRodney W. Grimes }
6345b81b6b3SRodney W. Grimes 
63512d910e9SRobert Nordier static void
63612d910e9SRobert Nordier change_code()
63712d910e9SRobert Nordier {
63812d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
63912d910e9SRobert Nordier 		init_boot();
64012d910e9SRobert Nordier }
64112d910e9SRobert Nordier 
6424be1e61bSAlexander Langer void
6435b81b6b3SRodney W. Grimes get_params_to_use()
6445b81b6b3SRodney W. Grimes {
6455b81b6b3SRodney W. Grimes 	int	tmp;
6465b81b6b3SRodney W. Grimes 	print_params();
6475b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
6485b81b6b3SRodney W. Grimes 	{
6495b81b6b3SRodney W. Grimes 		do
6505b81b6b3SRodney W. Grimes 		{
6515b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
6525b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
6535b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6545b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6555b81b6b3SRodney W. Grimes 			print_params();
6565b81b6b3SRodney W. Grimes 		}
6575b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6585b81b6b3SRodney W. Grimes 	}
6595b81b6b3SRodney W. Grimes }
6605b81b6b3SRodney W. Grimes 
661f46af505SJordan K. Hubbard 
6625b81b6b3SRodney W. Grimes /***********************************************\
6635b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6645b81b6b3SRodney W. Grimes \***********************************************/
6654be1e61bSAlexander Langer static void
666041b8b00SPoul-Henning Kamp dos(struct dos_partition *partp)
6675b81b6b3SRodney W. Grimes {
66863692187SIan Dowse 	int cy, sec;
66963692187SIan Dowse 	u_int32_t end;
6705b81b6b3SRodney W. Grimes 
671881c9063SIan Dowse 	if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
672881c9063SIan Dowse 		memcpy(partp, &mtpart, sizeof(*partp));
6730b461cd7SBruce Evans 		return;
6740b461cd7SBruce Evans 	}
6750b461cd7SBruce Evans 
67663692187SIan Dowse 	/* Start c/h/s. */
67763692187SIan Dowse 	partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
67863692187SIan Dowse 	cy = partp->dp_start / dos_cylsecs;
67963692187SIan Dowse 	sec = partp->dp_start % dos_sectors + 1;
68063692187SIan Dowse 	partp->dp_scyl = DOSCYL(cy);
68163692187SIan Dowse 	partp->dp_ssect = DOSSECT(sec, cy);
6825b81b6b3SRodney W. Grimes 
68363692187SIan Dowse 	/* End c/h/s. */
68463692187SIan Dowse 	end = partp->dp_start + partp->dp_size - 1;
68563692187SIan Dowse 	partp->dp_ehd = end % dos_cylsecs / dos_sectors;
68663692187SIan Dowse 	cy = end / dos_cylsecs;
68763692187SIan Dowse 	sec = end % dos_sectors + 1;
68863692187SIan Dowse 	partp->dp_ecyl = DOSCYL(cy);
68963692187SIan Dowse 	partp->dp_esect = DOSSECT(sec, cy);
6905b81b6b3SRodney W. Grimes }
6915b81b6b3SRodney W. Grimes 
6924be1e61bSAlexander Langer static int
693041b8b00SPoul-Henning Kamp open_disk(int flag)
6945b81b6b3SRodney W. Grimes {
6955b81b6b3SRodney W. Grimes 	struct stat 	st;
696de78c288SPoul-Henning Kamp 	int rwmode;
6975b81b6b3SRodney W. Grimes 
6985b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
6998de9ed22SJoerg Wunsch 		if (errno == ENOENT)
7008de9ed22SJoerg Wunsch 			return -2;
701d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
7025b81b6b3SRodney W. Grimes 		return -1;
703b60eb395SBruce Evans 	}
704b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
705d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
706f353c761SPoul-Henning Kamp 	rwmode = a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY;
707f353c761SPoul-Henning Kamp 	fd = open(disk, rwmode);
708de78c288SPoul-Henning Kamp 	if (fd == -1 && errno == EPERM && rwmode == O_RDWR)
709de78c288SPoul-Henning Kamp 		fd = open(disk, O_RDONLY);
710f353c761SPoul-Henning Kamp 	if (fd == -1 && errno == ENXIO)
711e3038c6eSJoerg Wunsch 		return -2;
712f353c761SPoul-Henning Kamp 	if (fd == -1) {
713d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
7145b81b6b3SRodney W. Grimes 		return -1;
7155b81b6b3SRodney W. Grimes 	}
716041b8b00SPoul-Henning Kamp 	if (get_params() == -1) {
717d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
7185b81b6b3SRodney W. Grimes 		return -1;
7195b81b6b3SRodney W. Grimes 	}
7205b81b6b3SRodney W. Grimes 	return fd;
7215b81b6b3SRodney W. Grimes }
7225b81b6b3SRodney W. Grimes 
7234be1e61bSAlexander Langer static ssize_t
7244be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
7255b81b6b3SRodney W. Grimes {
7267fa181d4SYoshihiro Takahashi 
7275b81b6b3SRodney W. Grimes 	lseek(fd, (sector * 512), 0);
7287cb29d33SSøren Schmidt 	if (secsize == 0)
7297fa181d4SYoshihiro Takahashi 		for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
7307fa181d4SYoshihiro Takahashi 		     secsize *= 2) {
7317cb29d33SSøren Schmidt 			/* try the read */
7327cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
7337cb29d33SSøren Schmidt 			if (size == secsize)
7347cb29d33SSøren Schmidt 				/* it worked so return */
7357cb29d33SSøren Schmidt 				return secsize;
7367cb29d33SSøren Schmidt 		}
7377cb29d33SSøren Schmidt 	else
7387cb29d33SSøren Schmidt 		return read(fd, buf, secsize);
7397cb29d33SSøren Schmidt 
7407cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
7417cb29d33SSøren Schmidt 	return -1;
7425b81b6b3SRodney W. Grimes }
7435b81b6b3SRodney W. Grimes 
744de78c288SPoul-Henning Kamp static int
7454be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
7465b81b6b3SRodney W. Grimes {
747de78c288SPoul-Henning Kamp 	int error;
748de78c288SPoul-Henning Kamp 	struct gctl_req *grq;
749de78c288SPoul-Henning Kamp 	const char *q;
750de78c288SPoul-Henning Kamp 	char fbuf[BUFSIZ];
751de78c288SPoul-Henning Kamp 	int i, fdw;
752f353c761SPoul-Henning Kamp 
753de78c288SPoul-Henning Kamp 	grq = gctl_get_handle();
754de78c288SPoul-Henning Kamp 	gctl_ro_param(grq, "verb", -1, "write MBR");
755de78c288SPoul-Henning Kamp 	gctl_ro_param(grq, "class", -1, "MBR");
756de78c288SPoul-Henning Kamp 	q = strrchr(disk, '/');
757de78c288SPoul-Henning Kamp 	if (q == NULL)
758de78c288SPoul-Henning Kamp 		q = disk;
759de78c288SPoul-Henning Kamp 	else
760de78c288SPoul-Henning Kamp 		q++;
761de78c288SPoul-Henning Kamp 	gctl_ro_param(grq, "geom", -1, q);
762de78c288SPoul-Henning Kamp 	gctl_ro_param(grq, "data", secsize, buf);
763de78c288SPoul-Henning Kamp 	q = gctl_issue(grq);
764de78c288SPoul-Henning Kamp 	if (q == NULL)
765de78c288SPoul-Henning Kamp 		return(0);
766de78c288SPoul-Henning Kamp 	warnx("%s", q);
767de78c288SPoul-Henning Kamp 
768de78c288SPoul-Henning Kamp 	error = pwrite(fd, buf, secsize, (sector * 512));
769de78c288SPoul-Henning Kamp 	if (error == secsize)
770de78c288SPoul-Henning Kamp 		return (0);
771de78c288SPoul-Henning Kamp 
772de78c288SPoul-Henning Kamp 	for (i = 1; i < 5; i++) {
773de78c288SPoul-Henning Kamp 		sprintf(fbuf, "%ss%d", disk, i);
774de78c288SPoul-Henning Kamp 		fdw = open(fbuf, O_RDWR, 0);
775de78c288SPoul-Henning Kamp 		if (fdw < 0)
776de78c288SPoul-Henning Kamp 			continue;
777de78c288SPoul-Henning Kamp 		error = ioctl(fdw, DIOCSMBR, buf);
778de78c288SPoul-Henning Kamp 		close(fdw);
779de78c288SPoul-Henning Kamp 		if (error == 0)
780de78c288SPoul-Henning Kamp 			return (0);
7815b81b6b3SRodney W. Grimes 	}
782de78c288SPoul-Henning Kamp 	warnx("Failed to write sector zero");
783de78c288SPoul-Henning Kamp 	return(EINVAL);
784f353c761SPoul-Henning Kamp }
7855b81b6b3SRodney W. Grimes 
7864be1e61bSAlexander Langer static int
7874be1e61bSAlexander Langer get_params()
7885b81b6b3SRodney W. Grimes {
7893f8ba8b5SPoul-Henning Kamp 	int error;
7903f8ba8b5SPoul-Henning Kamp 	u_int u;
7913f8ba8b5SPoul-Henning Kamp 	off_t o;
7925b81b6b3SRodney W. Grimes 
7937963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGFWSECTORS, &u);
7947963fae6SPoul-Henning Kamp 	if (error == 0)
7957963fae6SPoul-Henning Kamp 		sectors = dos_sectors = u;
796c0fdfdbaSPoul-Henning Kamp 	else
797c0fdfdbaSPoul-Henning Kamp 		sectors = dos_sectors = 63;
798c0fdfdbaSPoul-Henning Kamp 
7997963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGFWHEADS, &u);
8007963fae6SPoul-Henning Kamp 	if (error == 0)
8017963fae6SPoul-Henning Kamp 		heads = dos_heads = u;
802c0fdfdbaSPoul-Henning Kamp 	else
803c0fdfdbaSPoul-Henning Kamp 		heads = dos_heads = 255;
8047963fae6SPoul-Henning Kamp 
8055b81b6b3SRodney W. Grimes 	dos_cylsecs = cylsecs = heads * sectors;
8065b81b6b3SRodney W. Grimes 	disksecs = cyls * heads * sectors;
8075b81b6b3SRodney W. Grimes 
8087963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGSECTORSIZE, &u);
809c0fdfdbaSPoul-Henning Kamp 	if (error != 0 || u == 0)
8107963fae6SPoul-Henning Kamp 		u = 512;
8117963fae6SPoul-Henning Kamp 
8127963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGMEDIASIZE, &o);
8137963fae6SPoul-Henning Kamp 	if (error == 0) {
8147963fae6SPoul-Henning Kamp 		disksecs = o / u;
8157963fae6SPoul-Henning Kamp 		cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
8167963fae6SPoul-Henning Kamp 	}
8177963fae6SPoul-Henning Kamp 
8185b81b6b3SRodney W. Grimes 	return (disksecs);
8195b81b6b3SRodney W. Grimes }
8205b81b6b3SRodney W. Grimes 
8215b81b6b3SRodney W. Grimes 
8224be1e61bSAlexander Langer static int
8235b81b6b3SRodney W. Grimes read_s0()
8245b81b6b3SRodney W. Grimes {
8251a03d6d5SPoul-Henning Kamp 	int i;
8261a03d6d5SPoul-Henning Kamp 
82785c2cf30SJohn Baldwin 	mboot.bootinst_size = secsize;
8282b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
8292b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
83085c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
83185c2cf30SJohn Baldwin 		warnx("unable to allocate buffer to read fdisk "
83285c2cf30SJohn Baldwin 		      "partition table");
83385c2cf30SJohn Baldwin 		return -1;
83485c2cf30SJohn Baldwin 	}
83585c2cf30SJohn Baldwin 	if (read_disk(0, mboot.bootinst) == -1) {
836d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
8375b81b6b3SRodney W. Grimes 		return -1;
8385b81b6b3SRodney W. Grimes 	}
8391a03d6d5SPoul-Henning Kamp 	if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
840d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
8415b81b6b3SRodney W. Grimes 		/* So should we initialize things */
8425b81b6b3SRodney W. Grimes 		return -1;
8435b81b6b3SRodney W. Grimes 	}
8441a03d6d5SPoul-Henning Kamp 	for (i = 0; i < NDOSPART; i++)
8451a03d6d5SPoul-Henning Kamp 		dos_partition_dec(
8461a03d6d5SPoul-Henning Kamp 		    &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
8471a03d6d5SPoul-Henning Kamp 		    &mboot.parts[i]);
8485b81b6b3SRodney W. Grimes 	return 0;
8495b81b6b3SRodney W. Grimes }
8505b81b6b3SRodney W. Grimes 
8514be1e61bSAlexander Langer static int
8525b81b6b3SRodney W. Grimes write_s0()
8535b81b6b3SRodney W. Grimes {
8541a03d6d5SPoul-Henning Kamp 	int	sector, i;
85585c2cf30SJohn Baldwin 
8565b81b6b3SRodney W. Grimes 	if (iotest) {
8575b81b6b3SRodney W. Grimes 		print_s0(-1);
8585b81b6b3SRodney W. Grimes 		return 0;
8595b81b6b3SRodney W. Grimes 	}
8601a03d6d5SPoul-Henning Kamp 	for(i = 0; i < NDOSPART; i++)
8611a03d6d5SPoul-Henning Kamp 		dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
8621a03d6d5SPoul-Henning Kamp 		    &mboot.parts[i]);
8631a03d6d5SPoul-Henning Kamp 	le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
86485c2cf30SJohn Baldwin 	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
86585c2cf30SJohn Baldwin 		if (write_disk(sector,
86685c2cf30SJohn Baldwin 			       &mboot.bootinst[sector * secsize]) == -1) {
867e6fb3ddeSPoul-Henning Kamp 			warn("can't write fdisk partition table");
8685b81b6b3SRodney W. Grimes 			return -1;
8695b81b6b3SRodney W. Grimes 		}
8704be1e61bSAlexander Langer 	return(0);
8715b81b6b3SRodney W. Grimes }
8725b81b6b3SRodney W. Grimes 
8735b81b6b3SRodney W. Grimes 
8744be1e61bSAlexander Langer static int
875041b8b00SPoul-Henning Kamp ok(const char *str)
8765b81b6b3SRodney W. Grimes {
8775b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
87863692187SIan Dowse 	fflush(stdout);
87963692187SIan Dowse 	if (fgets(lbuf, LBUF, stdin) == NULL)
88063692187SIan Dowse 		exit(1);
8815b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
8825b81b6b3SRodney W. Grimes 
8835b81b6b3SRodney W. Grimes 	if (*lbuf &&
8845b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
8855b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
8865b81b6b3SRodney W. Grimes 		return 1;
8875b81b6b3SRodney W. Grimes 	else
8885b81b6b3SRodney W. Grimes 		return 0;
8895b81b6b3SRodney W. Grimes }
8905b81b6b3SRodney W. Grimes 
8914be1e61bSAlexander Langer static int
892041b8b00SPoul-Henning Kamp decimal(const char *str, int *num, int deflt)
8935b81b6b3SRodney W. Grimes {
8945b81b6b3SRodney W. Grimes 	int acc = 0, c;
8955b81b6b3SRodney W. Grimes 	char *cp;
8965b81b6b3SRodney W. Grimes 
8975b81b6b3SRodney W. Grimes 	while (1) {
8985b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
89963692187SIan Dowse 		fflush(stdout);
90063692187SIan Dowse 		if (fgets(lbuf, LBUF, stdin) == NULL)
90163692187SIan Dowse 			exit(1);
9025b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
9035b81b6b3SRodney W. Grimes 
9045b81b6b3SRodney W. Grimes 		if (!*lbuf)
9055b81b6b3SRodney W. Grimes 			return 0;
9065b81b6b3SRodney W. Grimes 
9075b81b6b3SRodney W. Grimes 		cp = lbuf;
9085b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9095b81b6b3SRodney W. Grimes 		if (!c)
9105b81b6b3SRodney W. Grimes 			return 0;
9114be1e61bSAlexander Langer 		while ((c = *cp++)) {
9125b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
9135b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
9145b81b6b3SRodney W. Grimes 			else
9155b81b6b3SRodney W. Grimes 				break;
9165b81b6b3SRodney W. Grimes 		}
9175b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
9185b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9195b81b6b3SRodney W. Grimes 		if (!c) {
9205b81b6b3SRodney W. Grimes 			*num = acc;
9215b81b6b3SRodney W. Grimes 			return 1;
9225b81b6b3SRodney W. Grimes 		} else
923680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
9245b81b6b3SRodney W. Grimes 				lbuf);
9255b81b6b3SRodney W. Grimes 	}
9265b81b6b3SRodney W. Grimes 
9275b81b6b3SRodney W. Grimes }
9285b81b6b3SRodney W. Grimes 
929041b8b00SPoul-Henning Kamp static const char *
9304be1e61bSAlexander Langer get_type(int type)
9315b81b6b3SRodney W. Grimes {
9325b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
9335b81b6b3SRodney W. Grimes 	int	counter = 0;
9345b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
9355b81b6b3SRodney W. Grimes 
9365b81b6b3SRodney W. Grimes 
937041b8b00SPoul-Henning Kamp 	while(counter < numentries) {
9385b81b6b3SRodney W. Grimes 		if(ptr->type == type)
9395b81b6b3SRodney W. Grimes 			return(ptr->name);
9405b81b6b3SRodney W. Grimes 		ptr++;
9415b81b6b3SRodney W. Grimes 		counter++;
9425b81b6b3SRodney W. Grimes 	}
9435b81b6b3SRodney W. Grimes 	return("unknown");
9445b81b6b3SRodney W. Grimes }
945f46af505SJordan K. Hubbard 
946f46af505SJordan K. Hubbard 
947f46af505SJordan K. Hubbard static void
948041b8b00SPoul-Henning Kamp parse_config_line(char *line, CMD *command)
949f46af505SJordan K. Hubbard {
950f46af505SJordan K. Hubbard     char	*cp, *end;
951f46af505SJordan K. Hubbard 
952f46af505SJordan K. Hubbard     cp = line;
953041b8b00SPoul-Henning Kamp     while (1) {
954f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
955f46af505SJordan K. Hubbard 
956f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
957f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
958f46af505SJordan K. Hubbard 	    break;
959f46af505SJordan K. Hubbard 	command->cmd = *cp++;
960f46af505SJordan K. Hubbard 
961f46af505SJordan K. Hubbard 	/*
962f46af505SJordan K. Hubbard 	 * Parse args
963f46af505SJordan K. Hubbard 	 */
964041b8b00SPoul-Henning Kamp 	    while (1) {
965f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
966f46af505SJordan K. Hubbard 	    if (*cp == '#')
967f46af505SJordan K. Hubbard 		break;		/* found comment */
968f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
969f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
970f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
971f46af505SJordan K. Hubbard 		break;		/* assume end of line */
972f46af505SJordan K. Hubbard 	    end = NULL;
973f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
974f46af505SJordan K. Hubbard 	    if (cp == end)
975f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
976f46af505SJordan K. Hubbard 	    cp = end;
977f46af505SJordan K. Hubbard 	    command->n_args++;
978f46af505SJordan K. Hubbard 	}
979f46af505SJordan K. Hubbard 	break;
980f46af505SJordan K. Hubbard     }
981f46af505SJordan K. Hubbard }
982f46af505SJordan K. Hubbard 
983f46af505SJordan K. Hubbard 
984f46af505SJordan K. Hubbard static int
985041b8b00SPoul-Henning Kamp process_geometry(CMD *command)
986f46af505SJordan K. Hubbard {
987f46af505SJordan K. Hubbard     int		status = 1, i;
988f46af505SJordan K. Hubbard 
989041b8b00SPoul-Henning Kamp     while (1) {
990f46af505SJordan K. Hubbard 	geom_processed = 1;
991041b8b00SPoul-Henning Kamp 	    if (part_processed) {
992d98b1668SPhilippe Charnier 	    warnx(
993d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
994d98b1668SPhilippe Charnier     all partition specifications",
995d98b1668SPhilippe Charnier 		    current_line_number);
996f46af505SJordan K. Hubbard 	    status = 0;
997f46af505SJordan K. Hubbard 	    break;
998f46af505SJordan K. Hubbard 	}
999041b8b00SPoul-Henning Kamp 	    if (command->n_args != 3) {
1000d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
1001d98b1668SPhilippe Charnier 		    current_line_number);
1002f46af505SJordan K. Hubbard 	    status = 0;
1003f46af505SJordan K. Hubbard 	    break;
1004f46af505SJordan K. Hubbard 	}
1005041b8b00SPoul-Henning Kamp 	    dos_cyls = 0;
1006041b8b00SPoul-Henning Kamp 	    dos_heads = 0;
1007041b8b00SPoul-Henning Kamp 	    dos_sectors = 0;
1008041b8b00SPoul-Henning Kamp 	    for (i = 0; i < 3; ++i) {
1009041b8b00SPoul-Henning Kamp 		    switch (command->args[i].argtype) {
1010f46af505SJordan K. Hubbard 	    case 'c':
1011f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
1012f46af505SJordan K. Hubbard 		break;
1013f46af505SJordan K. Hubbard 	    case 'h':
1014f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
1015f46af505SJordan K. Hubbard 		break;
1016f46af505SJordan K. Hubbard 	    case 's':
1017f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
1018f46af505SJordan K. Hubbard 		break;
1019f46af505SJordan K. Hubbard 	    default:
1020d98b1668SPhilippe Charnier 		warnx(
1021d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1022d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1023f46af505SJordan K. Hubbard 			command->args[i].argtype);
1024f46af505SJordan K. Hubbard 		status = 0;
1025f46af505SJordan K. Hubbard 		break;
1026f46af505SJordan K. Hubbard 	    }
1027f46af505SJordan K. Hubbard 	}
1028f46af505SJordan K. Hubbard 	if (status == 0)
1029f46af505SJordan K. Hubbard 	    break;
1030f46af505SJordan K. Hubbard 
1031f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1032f46af505SJordan K. Hubbard 
1033f46af505SJordan K. Hubbard 	/*
1034f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1035f46af505SJordan K. Hubbard 	 */
1036041b8b00SPoul-Henning Kamp 	    if (dos_cyls == 0) {
1037d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1038d98b1668SPhilippe Charnier 		    current_line_number);
1039f46af505SJordan K. Hubbard 	    status = 0;
1040f46af505SJordan K. Hubbard 	}
1041041b8b00SPoul-Henning Kamp 	    if (dos_cyls > 1024) {
1042d98b1668SPhilippe Charnier 	    warnx(
1043d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1044f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1045d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1046d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1047f46af505SJordan K. Hubbard 	}
1048f46af505SJordan K. Hubbard 
1049041b8b00SPoul-Henning Kamp 	    if (dos_heads == 0) {
1050d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1051d98b1668SPhilippe Charnier 		    current_line_number);
1052f46af505SJordan K. Hubbard 	    status = 0;
1053041b8b00SPoul-Henning Kamp 	    } else if (dos_heads > 256) {
1054d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1055d98b1668SPhilippe Charnier 		    current_line_number);
1056f46af505SJordan K. Hubbard 	    status = 0;
1057f46af505SJordan K. Hubbard 	}
1058f46af505SJordan K. Hubbard 
1059041b8b00SPoul-Henning Kamp 	    if (dos_sectors == 0) {
1060d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1061d98b1668SPhilippe Charnier 		    current_line_number);
1062f46af505SJordan K. Hubbard 	    status = 0;
1063041b8b00SPoul-Henning Kamp 	    } else if (dos_sectors > 63) {
1064d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1065d98b1668SPhilippe Charnier 		    current_line_number);
1066f46af505SJordan K. Hubbard 	    status = 0;
1067f46af505SJordan K. Hubbard 	}
1068f46af505SJordan K. Hubbard 
1069f46af505SJordan K. Hubbard 	break;
1070f46af505SJordan K. Hubbard     }
1071f46af505SJordan K. Hubbard     return (status);
1072f46af505SJordan K. Hubbard }
1073f46af505SJordan K. Hubbard 
1074f46af505SJordan K. Hubbard 
1075f46af505SJordan K. Hubbard static int
1076041b8b00SPoul-Henning Kamp process_partition(CMD *command)
1077f46af505SJordan K. Hubbard {
1078f46af505SJordan K. Hubbard     int				status = 0, partition;
1079b594e0feSJohn Baldwin     u_int32_t			prev_head_boundary, prev_cyl_boundary;
1080b594e0feSJohn Baldwin     u_int32_t			adj_size, max_end;
1081f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1082f46af505SJordan K. Hubbard 
1083041b8b00SPoul-Henning Kamp 	while (1) {
1084f46af505SJordan K. Hubbard 	part_processed = 1;
1085041b8b00SPoul-Henning Kamp 		if (command->n_args != 4) {
1086d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1087d98b1668SPhilippe Charnier 		    current_line_number);
1088f46af505SJordan K. Hubbard 	    break;
1089f46af505SJordan K. Hubbard 	}
1090f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
1091041b8b00SPoul-Henning Kamp 		if (partition < 1 || partition > 4) {
1092d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1093d98b1668SPhilippe Charnier 		    current_line_number, partition);
1094f46af505SJordan K. Hubbard 	    break;
1095f46af505SJordan K. Hubbard 	}
10964ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1097f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1098f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1099f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1100f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1101f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1102f46af505SJordan K. Hubbard 
1103041b8b00SPoul-Henning Kamp 		if (partp->dp_typ == 0) {
1104f46af505SJordan K. Hubbard 	    /*
1105f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1106f46af505SJordan K. Hubbard 	     */
1107f46af505SJordan K. Hubbard 	    /*
1108f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1109f46af505SJordan K. Hubbard 	     */
1110f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1111f46af505SJordan K. Hubbard 	    status = 1;
1112f46af505SJordan K. Hubbard 	    break;
1113f46af505SJordan K. Hubbard 	}
1114f46af505SJordan K. Hubbard 
1115f46af505SJordan K. Hubbard 	/*
1116d64ada50SJens Schweikhardt 	 * Adjust start upwards, if necessary, to fall on a head boundary.
1117f46af505SJordan K. Hubbard 	 */
1118041b8b00SPoul-Henning Kamp 		if (partp->dp_start % dos_sectors != 0) {
1119b594e0feSJohn Baldwin 	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1120b594e0feSJohn Baldwin 	    if (max_end < dos_sectors ||
1121041b8b00SPoul-Henning Kamp 			    prev_head_boundary > max_end - dos_sectors) {
1122f46af505SJordan K. Hubbard 		/*
1123f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1124f46af505SJordan K. Hubbard 		 */
1125d98b1668SPhilippe Charnier 		warnx(
1126d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1127b594e0feSJohn Baldwin     a head boundary",
1128d98b1668SPhilippe Charnier 			current_line_number, partition);
1129f46af505SJordan K. Hubbard 		break;
1130f46af505SJordan K. Hubbard 	    }
1131d98b1668SPhilippe Charnier 	    warnx(
1132b594e0feSJohn Baldwin 	"WARNING: adjusting start offset of partition %d\n\
1133b594e0feSJohn Baldwin     from %u to %u, to fall on a head boundary",
1134b594e0feSJohn Baldwin 		    partition, (u_int)partp->dp_start,
1135b594e0feSJohn Baldwin 		    (u_int)(prev_head_boundary + dos_sectors));
1136b594e0feSJohn Baldwin 	    partp->dp_start = prev_head_boundary + dos_sectors;
1137f46af505SJordan K. Hubbard 	}
1138f46af505SJordan K. Hubbard 
1139f46af505SJordan K. Hubbard 	/*
1140f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1141f46af505SJordan K. Hubbard 	 * boundary.
1142f46af505SJordan K. Hubbard 	 */
1143b594e0feSJohn Baldwin 	prev_cyl_boundary =
1144f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1145b594e0feSJohn Baldwin 	if (prev_cyl_boundary > partp->dp_start)
1146b594e0feSJohn Baldwin 	    adj_size = prev_cyl_boundary - partp->dp_start;
1147041b8b00SPoul-Henning Kamp 		else {
1148b594e0feSJohn Baldwin 	    warnx(
1149b594e0feSJohn Baldwin 	"ERROR: could not adjust partition to start on a head boundary\n\
1150b594e0feSJohn Baldwin     and end on a cylinder boundary.");
1151b594e0feSJohn Baldwin 	    return (0);
1152b594e0feSJohn Baldwin 	}
1153041b8b00SPoul-Henning Kamp 		if (adj_size != partp->dp_size) {
1154d98b1668SPhilippe Charnier 	    warnx(
1155b594e0feSJohn Baldwin 	"WARNING: adjusting size of partition %d from %u to %u\n\
1156b594e0feSJohn Baldwin     to end on a cylinder boundary",
1157b594e0feSJohn Baldwin 		    partition, (u_int)partp->dp_size, (u_int)adj_size);
1158f46af505SJordan K. Hubbard 	    partp->dp_size = adj_size;
1159f46af505SJordan K. Hubbard 	}
1160041b8b00SPoul-Henning Kamp 		if (partp->dp_size == 0) {
116163692187SIan Dowse 	    warnx("ERROR line %d: size of partition %d is zero",
1162d98b1668SPhilippe Charnier 		    current_line_number, partition);
1163f46af505SJordan K. Hubbard 	    break;
1164f46af505SJordan K. Hubbard 	}
1165f46af505SJordan K. Hubbard 
116663692187SIan Dowse 	dos(partp);
1167f46af505SJordan K. Hubbard 	status = 1;
1168f46af505SJordan K. Hubbard 	break;
1169f46af505SJordan K. Hubbard     }
1170f46af505SJordan K. Hubbard     return (status);
1171f46af505SJordan K. Hubbard }
1172f46af505SJordan K. Hubbard 
1173f46af505SJordan K. Hubbard 
1174f46af505SJordan K. Hubbard static int
1175041b8b00SPoul-Henning Kamp process_active(CMD *command)
1176f46af505SJordan K. Hubbard {
1177f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1178f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1179f46af505SJordan K. Hubbard 
1180041b8b00SPoul-Henning Kamp 	while (1) {
1181f46af505SJordan K. Hubbard 	active_processed = 1;
1182041b8b00SPoul-Henning Kamp 		if (command->n_args != 1) {
1183d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1184d98b1668SPhilippe Charnier 		    current_line_number);
1185f46af505SJordan K. Hubbard 	    status = 0;
1186f46af505SJordan K. Hubbard 	    break;
1187f46af505SJordan K. Hubbard 	}
1188f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
1189041b8b00SPoul-Henning Kamp 		if (partition < 1 || partition > 4) {
1190d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1191d98b1668SPhilippe Charnier 		    current_line_number, partition);
1192f46af505SJordan K. Hubbard 	    break;
1193f46af505SJordan K. Hubbard 	}
1194f46af505SJordan K. Hubbard 	/*
1195f46af505SJordan K. Hubbard 	 * Reset active partition
1196f46af505SJordan K. Hubbard 	 */
1197f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1198f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1199f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
12004ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1201f46af505SJordan K. Hubbard 
1202f46af505SJordan K. Hubbard 	status = 1;
1203f46af505SJordan K. Hubbard 	break;
1204f46af505SJordan K. Hubbard     }
1205f46af505SJordan K. Hubbard     return (status);
1206f46af505SJordan K. Hubbard }
1207f46af505SJordan K. Hubbard 
1208f46af505SJordan K. Hubbard 
1209f46af505SJordan K. Hubbard static int
1210041b8b00SPoul-Henning Kamp process_line(char *line)
1211f46af505SJordan K. Hubbard {
1212f46af505SJordan K. Hubbard     CMD		command;
1213f46af505SJordan K. Hubbard     int		status = 1;
1214f46af505SJordan K. Hubbard 
1215041b8b00SPoul-Henning Kamp 	while (1) {
1216f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1217041b8b00SPoul-Henning Kamp 		switch (command.cmd) {
1218f46af505SJordan K. Hubbard 	case 0:
1219f46af505SJordan K. Hubbard 	    /*
1220f46af505SJordan K. Hubbard 	     * Comment or blank line
1221f46af505SJordan K. Hubbard 	     */
1222f46af505SJordan K. Hubbard 	    break;
1223f46af505SJordan K. Hubbard 	case 'g':
1224f46af505SJordan K. Hubbard 	    /*
1225f46af505SJordan K. Hubbard 	     * Set geometry
1226f46af505SJordan K. Hubbard 	     */
1227f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1228f46af505SJordan K. Hubbard 	    break;
1229f46af505SJordan K. Hubbard 	case 'p':
1230f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1231f46af505SJordan K. Hubbard 	    break;
1232f46af505SJordan K. Hubbard 	case 'a':
1233f46af505SJordan K. Hubbard 	    status = process_active(&command);
1234f46af505SJordan K. Hubbard 	    break;
1235f46af505SJordan K. Hubbard 	default:
1236f46af505SJordan K. Hubbard 	    status = 0;
1237f46af505SJordan K. Hubbard 	    break;
1238f46af505SJordan K. Hubbard 	}
1239f46af505SJordan K. Hubbard 	break;
1240f46af505SJordan K. Hubbard     }
1241f46af505SJordan K. Hubbard     return (status);
1242f46af505SJordan K. Hubbard }
1243f46af505SJordan K. Hubbard 
1244f46af505SJordan K. Hubbard 
1245f46af505SJordan K. Hubbard static int
1246041b8b00SPoul-Henning Kamp read_config(char *config_file)
1247f46af505SJordan K. Hubbard {
1248f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1249f46af505SJordan K. Hubbard     int		status = 1;
1250f46af505SJordan K. Hubbard     char	buf[1010];
1251f46af505SJordan K. Hubbard 
1252041b8b00SPoul-Henning Kamp 	while (1) {
1253041b8b00SPoul-Henning Kamp 		if (strcmp(config_file, "-") != 0) {
1254f46af505SJordan K. Hubbard 	    /*
1255f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1256f46af505SJordan K. Hubbard 	     */
1257041b8b00SPoul-Henning Kamp 			if ((fp = fopen(config_file, "r")) == NULL) {
1258f46af505SJordan K. Hubbard 		status = 0;
1259f46af505SJordan K. Hubbard 		break;
1260f46af505SJordan K. Hubbard 	    }
1261041b8b00SPoul-Henning Kamp 		} else {
1262f46af505SJordan K. Hubbard 	    fp = stdin;
1263f46af505SJordan K. Hubbard 	}
1264f46af505SJordan K. Hubbard 	current_line_number = 0;
1265041b8b00SPoul-Henning Kamp 		while (!feof(fp)) {
1266f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1267f46af505SJordan K. Hubbard 		break;
1268f46af505SJordan K. Hubbard 	    ++current_line_number;
1269f46af505SJordan K. Hubbard 	    status = process_line(buf);
1270f46af505SJordan K. Hubbard 	    if (status == 0)
1271f46af505SJordan K. Hubbard 		break;
1272f46af505SJordan K. Hubbard 	    }
1273f46af505SJordan K. Hubbard 	break;
1274f46af505SJordan K. Hubbard     }
1275041b8b00SPoul-Henning Kamp 	if (fp) {
1276f46af505SJordan K. Hubbard 	/*
1277f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1278f46af505SJordan K. Hubbard 	 */
1279f46af505SJordan K. Hubbard 	fclose(fp);
1280f46af505SJordan K. Hubbard     }
1281f46af505SJordan K. Hubbard     return (status);
1282f46af505SJordan K. Hubbard }
1283f46af505SJordan K. Hubbard 
1284f46af505SJordan K. Hubbard 
1285f46af505SJordan K. Hubbard static void
1286f46af505SJordan K. Hubbard reset_boot(void)
1287f46af505SJordan K. Hubbard {
1288f46af505SJordan K. Hubbard     int				i;
1289f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1290f46af505SJordan K. Hubbard 
1291f46af505SJordan K. Hubbard     init_boot();
1292041b8b00SPoul-Henning Kamp 	for (i = 0; i < 4; ++i) {
1293f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1294f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1295f46af505SJordan K. Hubbard     }
1296f46af505SJordan K. Hubbard }
1297b594e0feSJohn Baldwin 
1298b594e0feSJohn Baldwin static int
1299041b8b00SPoul-Henning Kamp sanitize_partition(struct dos_partition *partp)
1300b594e0feSJohn Baldwin {
1301b594e0feSJohn Baldwin     u_int32_t			prev_head_boundary, prev_cyl_boundary;
130263692187SIan Dowse     u_int32_t			max_end, size, start;
1303b594e0feSJohn Baldwin 
130463692187SIan Dowse     start = partp->dp_start;
130563692187SIan Dowse     size = partp->dp_size;
130663692187SIan Dowse     max_end = start + size;
130763692187SIan Dowse     /* Only allow a zero size if the partition is being marked unused. */
130863692187SIan Dowse     if (size == 0) {
130963692187SIan Dowse 	if (start == 0 && partp->dp_typ == 0)
131063692187SIan Dowse 	    return (1);
131163692187SIan Dowse 	warnx("ERROR: size of partition is zero");
131263692187SIan Dowse 	return (0);
131363692187SIan Dowse     }
131463692187SIan Dowse     /* Return if no adjustment is necessary. */
131563692187SIan Dowse     if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
131663692187SIan Dowse 	return (1);
131763692187SIan Dowse 
13187fb7df31SDag-Erling Smørgrav     if (start == 0) {
13197fb7df31SDag-Erling Smørgrav 	    warnx("WARNING: partition overlaps with partition table");
13207fb7df31SDag-Erling Smørgrav 	    if (ok("Correct this automatically?"))
13217fb7df31SDag-Erling Smørgrav 		    start = dos_sectors;
13227fb7df31SDag-Erling Smørgrav     }
132363692187SIan Dowse     if (start % dos_sectors != 0)
132463692187SIan Dowse 	warnx("WARNING: partition does not start on a head boundary");
132563692187SIan Dowse     if ((start  +size) % dos_sectors != 0)
132663692187SIan Dowse 	warnx("WARNING: partition does not end on a cylinder boundary");
132763692187SIan Dowse     warnx("WARNING: this may confuse the BIOS or some operating systems");
132863692187SIan Dowse     if (!ok("Correct this automatically?"))
132963692187SIan Dowse 	return (1);
1330b594e0feSJohn Baldwin 
1331b594e0feSJohn Baldwin     /*
1332d64ada50SJens Schweikhardt      * Adjust start upwards, if necessary, to fall on a head boundary.
1333b594e0feSJohn Baldwin      */
133463692187SIan Dowse     if (start % dos_sectors != 0) {
133563692187SIan Dowse 	prev_head_boundary = start / dos_sectors * dos_sectors;
1336b594e0feSJohn Baldwin 	if (max_end < dos_sectors ||
133763692187SIan Dowse 	    prev_head_boundary >= max_end - dos_sectors) {
1338b594e0feSJohn Baldwin 	    /*
1339b594e0feSJohn Baldwin 	     * Can't go past end of partition
1340b594e0feSJohn Baldwin 	     */
1341b594e0feSJohn Baldwin 	    warnx(
1342b594e0feSJohn Baldwin     "ERROR: unable to adjust start of partition to fall on a head boundary");
1343b594e0feSJohn Baldwin 	    return (0);
1344b594e0feSJohn Baldwin         }
134563692187SIan Dowse 	start = prev_head_boundary + dos_sectors;
1346b594e0feSJohn Baldwin     }
1347b594e0feSJohn Baldwin 
1348b594e0feSJohn Baldwin     /*
1349b594e0feSJohn Baldwin      * Adjust size downwards, if necessary, to fall on a cylinder
1350b594e0feSJohn Baldwin      * boundary.
1351b594e0feSJohn Baldwin      */
135263692187SIan Dowse     prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
135363692187SIan Dowse     if (prev_cyl_boundary > start)
135463692187SIan Dowse 	size = prev_cyl_boundary - start;
135563692187SIan Dowse     else {
1356b594e0feSJohn Baldwin 	warnx("ERROR: could not adjust partition to start on a head boundary\n\
1357b594e0feSJohn Baldwin     and end on a cylinder boundary.");
1358b594e0feSJohn Baldwin 	return (0);
1359b594e0feSJohn Baldwin     }
136063692187SIan Dowse 
136163692187SIan Dowse     /* Finally, commit any changes to partp and return. */
136263692187SIan Dowse     if (start != partp->dp_start) {
136363692187SIan Dowse 	warnx("WARNING: adjusting start offset of partition to %u",
136463692187SIan Dowse 	    (u_int)start);
136563692187SIan Dowse 	partp->dp_start = start;
1366b594e0feSJohn Baldwin     }
136763692187SIan Dowse     if (size != partp->dp_size) {
136863692187SIan Dowse 	warnx("WARNING: adjusting size of partition to %u", (u_int)size);
136963692187SIan Dowse 	partp->dp_size = size;
1370b594e0feSJohn Baldwin     }
1371b594e0feSJohn Baldwin 
1372b594e0feSJohn Baldwin     return (1);
1373b594e0feSJohn Baldwin }
1374df77f711SJoerg Wunsch 
1375df77f711SJoerg Wunsch /*
1376df77f711SJoerg Wunsch  * Try figuring out the root device's canonical disk name.
1377df77f711SJoerg Wunsch  * The following choices are considered:
1378df77f711SJoerg Wunsch  *   /dev/ad0s1a     => /dev/ad0
1379df77f711SJoerg Wunsch  *   /dev/da0a       => /dev/da0
1380df77f711SJoerg Wunsch  *   /dev/vinum/root => /dev/vinum/root
1381df77f711SJoerg Wunsch  */
1382df77f711SJoerg Wunsch static char *
1383df77f711SJoerg Wunsch get_rootdisk(void)
1384df77f711SJoerg Wunsch {
1385df77f711SJoerg Wunsch 	struct statfs rootfs;
1386df77f711SJoerg Wunsch 	regex_t re;
1387df77f711SJoerg Wunsch #define NMATCHES 2
1388df77f711SJoerg Wunsch 	regmatch_t rm[NMATCHES];
1389df77f711SJoerg Wunsch 	char *s;
1390df77f711SJoerg Wunsch 	int rv;
1391df77f711SJoerg Wunsch 
1392df77f711SJoerg Wunsch 	if (statfs("/", &rootfs) == -1)
1393df77f711SJoerg Wunsch 		err(1, "statfs(\"/\")");
1394df77f711SJoerg Wunsch 
1395a8ad364aSPoul-Henning Kamp 	if ((rv = regcomp(&re, "^(/dev/[a-z]+[0-9]+)([sp][0-9]+)?[a-h]?$",
1396df77f711SJoerg Wunsch 		    REG_EXTENDED)) != 0)
1397df77f711SJoerg Wunsch 		errx(1, "regcomp() failed (%d)", rv);
1398df77f711SJoerg Wunsch 	if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1399df77f711SJoerg Wunsch 		errx(1,
1400df77f711SJoerg Wunsch "mounted root fs resource doesn't match expectations (regexec returned %d)",
1401df77f711SJoerg Wunsch 		    rv);
1402df77f711SJoerg Wunsch 	if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1403df77f711SJoerg Wunsch 		errx(1, "out of memory");
1404df77f711SJoerg Wunsch 	memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1405df77f711SJoerg Wunsch 	    rm[1].rm_eo - rm[1].rm_so);
1406df77f711SJoerg Wunsch 	s[rm[1].rm_eo - rm[1].rm_so] = 0;
1407df77f711SJoerg Wunsch 
1408df77f711SJoerg Wunsch 	return s;
1409df77f711SJoerg Wunsch }
1410