xref: /freebsd/sbin/fdisk/fdisk.c (revision c0fdfdba2941a94f7f8581689489fa55f6e9898a)
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 
27d98b1668SPhilippe Charnier #ifndef lint
28d98b1668SPhilippe Charnier static const char rcsid[] =
297f3dea24SPeter Wemm   "$FreeBSD$";
30d98b1668SPhilippe Charnier #endif /* not lint */
31d98b1668SPhilippe Charnier 
323f8ba8b5SPoul-Henning Kamp #include <sys/disk.h>
335b81b6b3SRodney W. Grimes #include <sys/disklabel.h>
343bb24c35SPoul-Henning Kamp #include <sys/diskmbr.h>
35df77f711SJoerg Wunsch #include <sys/param.h>
365b81b6b3SRodney W. Grimes #include <sys/stat.h>
37df77f711SJoerg Wunsch #include <sys/mount.h>
38d98b1668SPhilippe Charnier #include <ctype.h>
395b81b6b3SRodney W. Grimes #include <fcntl.h>
40d98b1668SPhilippe Charnier #include <err.h>
41d98b1668SPhilippe Charnier #include <errno.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 
5585c2cf30SJohn Baldwin #define MBRSIGOFF	510
5685c2cf30SJohn Baldwin 
575b81b6b3SRodney W. Grimes /*
585b81b6b3SRodney W. Grimes  *
595b81b6b3SRodney W. Grimes  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
605b81b6b3SRodney W. Grimes  *
615b81b6b3SRodney W. Grimes  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
625b81b6b3SRodney W. Grimes  *	Copyright (c) 1989	Robert. V. Baron
635b81b6b3SRodney W. Grimes  *	Created.
645b81b6b3SRodney W. Grimes  */
655b81b6b3SRodney W. Grimes 
665b81b6b3SRodney W. Grimes #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
675b81b6b3SRodney W. Grimes 
685b81b6b3SRodney W. Grimes #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
695b81b6b3SRodney W. Grimes 
707cb29d33SSøren Schmidt #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
717cb29d33SSøren Schmidt #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
72041b8b00SPoul-Henning Kamp static int secsize = 0;		/* the sensed sector size */
735b81b6b3SRodney W. Grimes 
74041b8b00SPoul-Henning Kamp static char *disk;
75e3038c6eSJoerg Wunsch 
76041b8b00SPoul-Henning Kamp static int cyls, sectors, heads, cylsecs, disksecs;
775b81b6b3SRodney W. Grimes 
78041b8b00SPoul-Henning Kamp struct mboot {
79d98b1668SPhilippe Charnier 	unsigned char padding[2]; /* force the longs to be long aligned */
8085c2cf30SJohn Baldwin   	unsigned char *bootinst;  /* boot code */
8185c2cf30SJohn Baldwin   	off_t bootinst_size;
825b81b6b3SRodney W. Grimes 	struct	dos_partition parts[4];
835b81b6b3SRodney W. Grimes };
84041b8b00SPoul-Henning Kamp 
85041b8b00SPoul-Henning Kamp static struct mboot mboot;
86f353c761SPoul-Henning Kamp static int fd, fdw;
87f353c761SPoul-Henning Kamp 
885b81b6b3SRodney W. Grimes 
895b81b6b3SRodney W. Grimes #define ACTIVE 0x80
905b81b6b3SRodney W. Grimes #define BOOT_MAGIC 0xAA55
915b81b6b3SRodney W. Grimes 
92041b8b00SPoul-Henning Kamp static uint dos_cyls;
93041b8b00SPoul-Henning Kamp static uint dos_heads;
94041b8b00SPoul-Henning Kamp static uint dos_sectors;
95041b8b00SPoul-Henning Kamp static uint dos_cylsecs;
965b81b6b3SRodney W. Grimes 
975b81b6b3SRodney W. Grimes #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
985b81b6b3SRodney W. Grimes #define DOSCYL(c)	(c & 0xff)
995b81b6b3SRodney W. Grimes 
100f46af505SJordan K. Hubbard #define MAX_ARGS	10
101f46af505SJordan K. Hubbard 
102f46af505SJordan K. Hubbard static int	current_line_number;
103f46af505SJordan K. Hubbard 
104f46af505SJordan K. Hubbard static int	geom_processed = 0;
105f46af505SJordan K. Hubbard static int	part_processed = 0;
106f46af505SJordan K. Hubbard static int	active_processed = 0;
107f46af505SJordan K. Hubbard 
108f46af505SJordan K. Hubbard typedef struct cmd {
109f46af505SJordan K. Hubbard     char		cmd;
110f46af505SJordan K. Hubbard     int			n_args;
111f46af505SJordan K. Hubbard     struct arg {
112f46af505SJordan K. Hubbard 	char	argtype;
113f46af505SJordan K. Hubbard 	int	arg_val;
114f46af505SJordan K. Hubbard     }			args[MAX_ARGS];
115f46af505SJordan K. Hubbard } CMD;
116f46af505SJordan K. Hubbard 
11726721a89SRobert Nordier static int B_flag  = 0;		/* replace boot code */
11810b0ee93SWarner Losh static int I_flag  = 0;		/* use entire disk for FreeBSD */
1195b81b6b3SRodney W. Grimes static int a_flag  = 0;		/* set active partition */
12026721a89SRobert Nordier static char *b_flag = NULL;	/* path to boot code */
1215b81b6b3SRodney W. Grimes static int i_flag  = 0;		/* replace partition data */
1225b81b6b3SRodney W. Grimes static int u_flag  = 0;		/* update partition data */
12310b0ee93SWarner Losh static int s_flag  = 0;		/* Print a summary and exit */
12405a213f1STom Rhodes static int t_flag  = 0;		/* test only */
125f46af505SJordan K. Hubbard static char *f_flag = NULL;	/* Read config info from file */
126f46af505SJordan K. Hubbard static int v_flag  = 0;		/* Be verbose */
1275b81b6b3SRodney W. Grimes 
128041b8b00SPoul-Henning Kamp static struct part_type
1295b81b6b3SRodney W. Grimes {
1305b81b6b3SRodney W. Grimes  unsigned char type;
131041b8b00SPoul-Henning Kamp 	const char *name;
132041b8b00SPoul-Henning Kamp } part_types[] = {
1335b81b6b3SRodney W. Grimes 	 {0x00, "unused"}
1345b81b6b3SRodney W. Grimes 	,{0x01, "Primary DOS with 12 bit FAT"}
1355b81b6b3SRodney W. Grimes 	,{0x02, "XENIX / file system"}
1365b81b6b3SRodney W. Grimes 	,{0x03, "XENIX /usr file system"}
137978d3bfeSJosef Karthauser 	,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
1385b81b6b3SRodney W. Grimes 	,{0x05, "Extended DOS"}
139978d3bfeSJosef Karthauser 	,{0x06, "Primary 'big' DOS (>= 32MB)"}
140691e5f80SGuy Helmer 	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
141978d3bfeSJosef Karthauser 	,{0x08, "AIX file system or SplitDrive"}
1425b81b6b3SRodney W. Grimes 	,{0x09, "AIX boot partition or Coherent"}
143978d3bfeSJosef Karthauser 	,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
14426555b64SAndrey A. Chernov 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
145978d3bfeSJosef Karthauser 	,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
146978d3bfeSJosef Karthauser 	,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
147978d3bfeSJosef Karthauser 	,{0x0F, "Extended DOS (LBA)"}
1485b81b6b3SRodney W. Grimes 	,{0x10, "OPUS"}
149978d3bfeSJosef Karthauser 	,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
150978d3bfeSJosef Karthauser 	,{0x12, "Compaq diagnostics"}
151978d3bfeSJosef Karthauser 	,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
152978d3bfeSJosef Karthauser 	,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
153978d3bfeSJosef Karthauser 	,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
154978d3bfeSJosef Karthauser 	,{0x18, "AST Windows swapfile"}
155978d3bfeSJosef Karthauser 	,{0x24, "NEC DOS"}
156978d3bfeSJosef Karthauser 	,{0x3C, "PartitionMagic recovery"}
15735bfe0c3SBrian Somers 	,{0x39, "plan9"}
1585b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
159978d3bfeSJosef Karthauser 	,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
160978d3bfeSJosef Karthauser 	,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
161978d3bfeSJosef Karthauser 	,{0x43, "Linux native (sharing disk with DRDOS)"}
162691e5f80SGuy Helmer 	,{0x4D, "QNX 4.2 Primary"}
163691e5f80SGuy Helmer 	,{0x4E, "QNX 4.2 Secondary"}
164691e5f80SGuy Helmer 	,{0x4F, "QNX 4.2 Tertiary"}
165978d3bfeSJosef Karthauser 	,{0x50, "DM (disk manager)"}
166978d3bfeSJosef Karthauser 	,{0x51, "DM6 Aux1 (or Novell)"}
1675b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
168978d3bfeSJosef Karthauser 	,{0x53, "DM6 Aux3"}
169978d3bfeSJosef Karthauser 	,{0x54, "DM6"}
170978d3bfeSJosef Karthauser 	,{0x55, "EZ-Drive (disk manager)"}
171978d3bfeSJosef Karthauser 	,{0x56, "Golden Bow (disk manager)"}
172978d3bfeSJosef Karthauser 	,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
173978d3bfeSJosef Karthauser 	,{0x61, "SpeedStor"}
174978d3bfeSJosef Karthauser 	,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
175978d3bfeSJosef Karthauser 	,{0x64, "Novell Netware/286 2.xx"}
176978d3bfeSJosef Karthauser 	,{0x65, "Novell Netware/386 3.xx"}
177978d3bfeSJosef Karthauser 	,{0x70, "DiskSecure Multi-Boot"}
1785b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
179978d3bfeSJosef Karthauser 	,{0x77, "QNX4.x"}
180978d3bfeSJosef Karthauser 	,{0x78, "QNX4.x 2nd part"}
181978d3bfeSJosef Karthauser 	,{0x79, "QNX4.x 3rd part"}
182978d3bfeSJosef Karthauser 	,{0x80, "Minix until 1.4a"}
183041b8b00SPoul-Henning Kamp 	,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
18463ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
185978d3bfeSJosef Karthauser 	,{0x83, "Linux native"}
186978d3bfeSJosef Karthauser 	,{0x84, "OS/2 hidden C: drive"}
187978d3bfeSJosef Karthauser 	,{0x85, "Linux extended"}
188978d3bfeSJosef Karthauser 	,{0x86, "NTFS volume set??"}
189978d3bfeSJosef Karthauser 	,{0x87, "NTFS volume set??"}
1905b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba file system"}
1915b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
192d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
193197ef307SJosef Karthauser 	,{0xA0, "Suspend to Disk"}
19449f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
195e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
196978d3bfeSJosef Karthauser 	,{0xA7, "NeXTSTEP"}
197ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1981254a3baSGreg Lehey 	,{0xAC, "IBM JFS"}
1995b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 file system"}
2005b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
201978d3bfeSJosef Karthauser 	,{0xC1, "DRDOS/sec with 12-bit FAT"}
202978d3bfeSJosef Karthauser 	,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
203978d3bfeSJosef Karthauser 	,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
204978d3bfeSJosef Karthauser 	,{0xC7, "Syrinx"}
205978d3bfeSJosef Karthauser 	,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
206978d3bfeSJosef Karthauser 	,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
207978d3bfeSJosef Karthauser 	,{0xE3, "DOS R/O or SpeedStor"}
208978d3bfeSJosef Karthauser 	,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
209978d3bfeSJosef Karthauser 	,{0xEB, "BeOS file system"}
210a528d318SPeter Wemm 	,{0xEE, "EFI GPT"}
2112175f5f1SBruce Evans 	,{0xEF, "EFI System Partition"}
212978d3bfeSJosef Karthauser 	,{0xF1, "SpeedStor"}
2135b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
214978d3bfeSJosef Karthauser 	,{0xF4, "SpeedStor large partition"}
215978d3bfeSJosef Karthauser 	,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
216978d3bfeSJosef Karthauser 	,{0xFF, "Xenix bad blocks table"}
2175b81b6b3SRodney W. Grimes };
2185b81b6b3SRodney W. Grimes 
2194be1e61bSAlexander Langer static void print_s0(int which);
2204be1e61bSAlexander Langer static void print_part(int i);
2214be1e61bSAlexander Langer static void init_sector0(unsigned long start);
222f46af505SJordan K. Hubbard static void init_boot(void);
2234be1e61bSAlexander Langer static void change_part(int i);
224041b8b00SPoul-Henning Kamp static void print_params(void);
2254be1e61bSAlexander Langer static void change_active(int which);
226041b8b00SPoul-Henning Kamp static void change_code(void);
227041b8b00SPoul-Henning Kamp static void get_params_to_use(void);
228df77f711SJoerg Wunsch static char *get_rootdisk(void);
22963692187SIan Dowse static void dos(struct dos_partition *partp);
230041b8b00SPoul-Henning Kamp static int open_disk(int flag);
2314be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
2324be1e61bSAlexander Langer static ssize_t write_disk(off_t sector, void *buf);
233041b8b00SPoul-Henning Kamp static int get_params(void);
234041b8b00SPoul-Henning Kamp static int read_s0(void);
235041b8b00SPoul-Henning Kamp static int write_s0(void);
236041b8b00SPoul-Henning Kamp static int ok(const char *str);
237041b8b00SPoul-Henning Kamp static int decimal(const char *str, int *num, int deflt);
238041b8b00SPoul-Henning Kamp static const char *get_type(int type);
239f46af505SJordan K. Hubbard static int read_config(char *config_file);
240f46af505SJordan K. Hubbard static void reset_boot(void);
241b594e0feSJohn Baldwin static int sanitize_partition(struct dos_partition *);
242d98b1668SPhilippe Charnier static void usage(void);
2434be1e61bSAlexander Langer 
2444be1e61bSAlexander Langer int
2454be1e61bSAlexander Langer main(int argc, char *argv[])
2465b81b6b3SRodney W. Grimes {
247df77f711SJoerg Wunsch 	struct	stat sb;
24826721a89SRobert Nordier 	int	c, i;
249041b8b00SPoul-Henning Kamp 	int	partition = -1;
250041b8b00SPoul-Henning Kamp 	struct	dos_partition *partp;
2515b81b6b3SRodney W. Grimes 
25210b0ee93SWarner Losh 	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
25326721a89SRobert Nordier 		switch (c) {
25426721a89SRobert Nordier 		case 'B':
25526721a89SRobert Nordier 			B_flag = 1;
2564ddd60b9SBrian Somers 			break;
25710b0ee93SWarner Losh 		case 'I':
25810b0ee93SWarner Losh 			I_flag = 1;
25910b0ee93SWarner Losh 			break;
2605b81b6b3SRodney W. Grimes 		case 'a':
2615b81b6b3SRodney W. Grimes 			a_flag = 1;
2625b81b6b3SRodney W. Grimes 			break;
26312d910e9SRobert Nordier 		case 'b':
26426721a89SRobert Nordier 			b_flag = optarg;
26512d910e9SRobert Nordier 			break;
266f46af505SJordan K. Hubbard 		case 'f':
26726721a89SRobert Nordier 			f_flag = optarg;
268f46af505SJordan K. Hubbard 			break;
2695b81b6b3SRodney W. Grimes 		case 'i':
2705b81b6b3SRodney W. Grimes 			i_flag = 1;
2715b81b6b3SRodney W. Grimes 			break;
27210b0ee93SWarner Losh 		case 's':
27310b0ee93SWarner Losh 			s_flag = 1;
27410b0ee93SWarner Losh 			break;
275f46af505SJordan K. Hubbard 		case 't':
276f46af505SJordan K. Hubbard 			t_flag = 1;
27726721a89SRobert Nordier 			break;
27826721a89SRobert Nordier 		case 'u':
27926721a89SRobert Nordier 			u_flag = 1;
28026721a89SRobert Nordier 			break;
281f46af505SJordan K. Hubbard 		case 'v':
282f46af505SJordan K. Hubbard 			v_flag = 1;
283f46af505SJordan K. Hubbard 			break;
28426721a89SRobert Nordier 		case '1':
28526721a89SRobert Nordier 		case '2':
28626721a89SRobert Nordier 		case '3':
28726721a89SRobert Nordier 		case '4':
28826721a89SRobert Nordier 			partition = c - '0';
28926721a89SRobert Nordier 			break;
2905b81b6b3SRodney W. Grimes 		default:
291d98b1668SPhilippe Charnier 			usage();
2925b81b6b3SRodney W. Grimes 		}
29326721a89SRobert Nordier 	if (f_flag || i_flag)
29426721a89SRobert Nordier 		u_flag = 1;
29526721a89SRobert Nordier 	if (t_flag)
29626721a89SRobert Nordier 		v_flag = 1;
29726721a89SRobert Nordier 	argc -= optind;
29826721a89SRobert Nordier 	argv += optind;
2995b81b6b3SRodney W. Grimes 
300df77f711SJoerg Wunsch 	if (argc == 0) {
301df77f711SJoerg Wunsch 		disk = get_rootdisk();
302df77f711SJoerg Wunsch 	} else {
303df77f711SJoerg Wunsch 		if (stat(argv[0], &sb) == 0) {
304df77f711SJoerg Wunsch 			/* OK, full pathname given */
3055b81b6b3SRodney W. Grimes 			disk = argv[0];
306df77f711SJoerg Wunsch 		} else if (errno == ENOENT) {
307df77f711SJoerg Wunsch 			/* Try prepending "/dev" */
308041b8b00SPoul-Henning Kamp 			asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
309041b8b00SPoul-Henning Kamp 			if (disk == NULL)
310df77f711SJoerg Wunsch 				errx(1, "out of memory");
311df77f711SJoerg Wunsch 		} else {
312df77f711SJoerg Wunsch 			/* other stat error, let it fail below */
313df77f711SJoerg Wunsch 			disk = argv[0];
314e3038c6eSJoerg Wunsch 		}
315df77f711SJoerg Wunsch 	}
3165b81b6b3SRodney W. Grimes 	if (open_disk(u_flag) < 0)
317d98b1668SPhilippe Charnier 		err(1, "cannot open disk %s", disk);
31885c2cf30SJohn Baldwin 
31985c2cf30SJohn Baldwin 	/* (abu)use mboot.bootinst to probe for the sector size */
32085c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
32185c2cf30SJohn Baldwin 		err(1, "cannot allocate buffer to determine disk sector size");
32285c2cf30SJohn Baldwin 	read_disk(0, mboot.bootinst);
323a12de062SJohn Baldwin 	free(mboot.bootinst);
324a12de062SJohn Baldwin 	mboot.bootinst = NULL;
32585c2cf30SJohn Baldwin 
326041b8b00SPoul-Henning Kamp 	if (s_flag) {
32710b0ee93SWarner Losh 		if (read_s0())
32810b0ee93SWarner Losh 			err(1, "read_s0");
32910b0ee93SWarner Losh 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
33010b0ee93SWarner Losh 		    dos_sectors);
33110b0ee93SWarner Losh 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
33210b0ee93SWarner Losh 		for (i = 0; i < NDOSPART; i++) {
33310b0ee93SWarner Losh 			partp = ((struct dos_partition *) &mboot.parts) + i;
33410b0ee93SWarner Losh 			if (partp->dp_start == 0 && partp->dp_size == 0)
33510b0ee93SWarner Losh 				continue;
33610b0ee93SWarner Losh 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
33710b0ee93SWarner Losh 			    (u_long) partp->dp_start,
33810b0ee93SWarner Losh 			    (u_long) partp->dp_size, partp->dp_typ,
33910b0ee93SWarner Losh 			    partp->dp_flag);
34010b0ee93SWarner Losh 		}
34110b0ee93SWarner Losh 		exit(0);
34210b0ee93SWarner Losh 	}
3435b81b6b3SRodney W. Grimes 
3445b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
345f46af505SJordan K. Hubbard 
346041b8b00SPoul-Henning Kamp 	if (I_flag) {
347e6fb3ddeSPoul-Henning Kamp 		read_s0();
348e6fb3ddeSPoul-Henning Kamp 		reset_boot();
349e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
350e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
351e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
352e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
353b594e0feSJohn Baldwin 		partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
354b594e0feSJohn Baldwin 		    dos_sectors;
35563692187SIan Dowse 		dos(partp);
356e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
357e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
35805a213f1STom Rhodes 		if (!t_flag)
359e6fb3ddeSPoul-Henning Kamp 			write_s0();
360e6fb3ddeSPoul-Henning Kamp 		exit(0);
361e6fb3ddeSPoul-Henning Kamp 	}
362041b8b00SPoul-Henning Kamp 	if (f_flag) {
363f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
364f46af505SJordan K. Hubbard 		reset_boot();
365f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
366f46af505SJordan K. Hubbard 		exit(1);
367f46af505SJordan K. Hubbard 	    if (v_flag)
368f46af505SJordan K. Hubbard 		print_s0(-1);
369f46af505SJordan K. Hubbard 	    if (!t_flag)
370f46af505SJordan K. Hubbard 		write_s0();
371041b8b00SPoul-Henning Kamp 	} else {
3725b81b6b3SRodney W. Grimes 	    if(u_flag)
3735b81b6b3SRodney W. Grimes 		get_params_to_use();
3745b81b6b3SRodney W. Grimes 	    else
3755b81b6b3SRodney W. Grimes 		print_params();
3765b81b6b3SRodney W. Grimes 
3775b81b6b3SRodney W. Grimes 	    if (read_s0())
378b594e0feSJohn Baldwin 		init_sector0(dos_sectors);
3795b81b6b3SRodney W. Grimes 
3807cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3815b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3825b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3835b81b6b3SRodney W. Grimes 	    if (partition == -1)
3844ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3855b81b6b3SRodney W. Grimes 		    change_part(i);
3865b81b6b3SRodney W. Grimes 	    else
3875b81b6b3SRodney W. Grimes 		change_part(partition);
3885b81b6b3SRodney W. Grimes 
3895b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3905b81b6b3SRodney W. Grimes 		change_active(partition);
3915b81b6b3SRodney W. Grimes 
39226721a89SRobert Nordier 	    if (B_flag)
39312d910e9SRobert Nordier 		change_code();
39412d910e9SRobert Nordier 
39526721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
396041b8b00SPoul-Henning Kamp 		if (!t_flag) {
3975b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
3985b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
399f46af505SJordan K. Hubbard 		}
4005b81b6b3SRodney W. Grimes 		print_s0(-1);
401041b8b00SPoul-Henning Kamp 		if (!t_flag) {
4025b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
4035b81b6b3SRodney W. Grimes 			write_s0();
404041b8b00SPoul-Henning Kamp 			} else {
405f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
406f46af505SJordan K. Hubbard 		}
407f46af505SJordan K. Hubbard 	    }
408f46af505SJordan K. Hubbard 	}
4095b81b6b3SRodney W. Grimes 
4105b81b6b3SRodney W. Grimes 	exit(0);
411d98b1668SPhilippe Charnier }
4125b81b6b3SRodney W. Grimes 
413d98b1668SPhilippe Charnier static void
414d98b1668SPhilippe Charnier usage()
415d98b1668SPhilippe Charnier {
41626721a89SRobert Nordier 	fprintf(stderr, "%s%s",
417ed1235adSJohn Baldwin 		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
41826721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
419d98b1668SPhilippe Charnier         exit(1);
4205b81b6b3SRodney W. Grimes }
4215b81b6b3SRodney W. Grimes 
4224be1e61bSAlexander Langer static void
4234be1e61bSAlexander Langer print_s0(int which)
4245b81b6b3SRodney W. Grimes {
4255b81b6b3SRodney W. Grimes 	int	i;
4265b81b6b3SRodney W. Grimes 
4275b81b6b3SRodney W. Grimes 	print_params();
4285b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4295b81b6b3SRodney W. Grimes 	if (which == -1)
4304ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4315b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4325b81b6b3SRodney W. Grimes 	else
4335b81b6b3SRodney W. Grimes 		print_part(which);
4345b81b6b3SRodney W. Grimes }
4355b81b6b3SRodney W. Grimes 
436041b8b00SPoul-Henning Kamp static struct dos_partition mtpart;
4375b81b6b3SRodney W. Grimes 
4384be1e61bSAlexander Langer static void
4394be1e61bSAlexander Langer print_part(int i)
4405b81b6b3SRodney W. Grimes {
441637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
442637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4435b81b6b3SRodney W. Grimes 
4444ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4455b81b6b3SRodney W. Grimes 
4465b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4475b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4485b81b6b3SRodney W. Grimes 		return;
4495b81b6b3SRodney W. Grimes 	}
450637fe2f7SJustin T. Gibbs 	/*
451637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
452637fe2f7SJustin T. Gibbs 	 */
453637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
454637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
455637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
456978d3bfeSJosef Karthauser 	printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
457978d3bfeSJosef Karthauser 	    get_type(partp->dp_typ));
458689fee87SBruce Evans 	printf("    start %lu, size %lu (%ju Meg), flag %x%s\n",
459ba198492SBruce Evans 		(u_long)partp->dp_start,
460ba198492SBruce Evans 		(u_long)partp->dp_size,
461689fee87SBruce Evans 		(uintmax_t)part_mb,
462680426beSDavid E. O'Brien 		partp->dp_flag,
463680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4646580291bSDavid E. O'Brien 	printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
4655b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4665b81b6b3SRodney W. Grimes 		,partp->dp_shd
4676580291bSDavid E. O'Brien 		,DPSECT(partp->dp_ssect)
4685b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4696580291bSDavid E. O'Brien 		,partp->dp_ehd
4706580291bSDavid E. O'Brien 		,DPSECT(partp->dp_esect));
4715b81b6b3SRodney W. Grimes }
4725b81b6b3SRodney W. Grimes 
473f46af505SJordan K. Hubbard 
474f46af505SJordan K. Hubbard static void
475f46af505SJordan K. Hubbard init_boot(void)
476f46af505SJordan K. Hubbard {
47729ea697dSPeter Wemm #ifndef __ia64__
47826721a89SRobert Nordier 	const char *fname;
479041b8b00SPoul-Henning Kamp 	int fdesc, n;
48085c2cf30SJohn Baldwin 	struct stat sb;
48126721a89SRobert Nordier 
48226721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
483041b8b00SPoul-Henning Kamp 	if ((fdesc = open(fname, O_RDONLY)) == -1 ||
484041b8b00SPoul-Henning Kamp 	    fstat(fdesc, &sb) == -1)
48585c2cf30SJohn Baldwin 		err(1, "%s", fname);
48685c2cf30SJohn Baldwin 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
48785c2cf30SJohn Baldwin 		errx(1, "%s: length must be a multiple of sector size", fname);
4882b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
4892b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
49085c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
49185c2cf30SJohn Baldwin 		errx(1, "%s: unable to allocate read buffer", fname);
492041b8b00SPoul-Henning Kamp 	if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
493041b8b00SPoul-Henning Kamp 	    close(fdesc))
49426721a89SRobert Nordier 		err(1, "%s", fname);
49585c2cf30SJohn Baldwin 	if (n != mboot.bootinst_size)
49685c2cf30SJohn Baldwin 		errx(1, "%s: short read", fname);
49729ea697dSPeter Wemm #else
49829ea697dSPeter Wemm 	if (mboot.bootinst != NULL)
49929ea697dSPeter Wemm 		free(mboot.bootinst);
50029ea697dSPeter Wemm 	mboot.bootinst_size = secsize;
50129ea697dSPeter Wemm 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
50229ea697dSPeter Wemm 		errx(1, "unable to allocate boot block buffer");
50329ea697dSPeter Wemm 	memset(mboot.bootinst, 0, mboot.bootinst_size);
504689fee87SBruce Evans 	*(uint16_t *)(void *)&mboot.bootinst[MBRSIGOFF] = BOOT_MAGIC;
50529ea697dSPeter Wemm #endif
506f46af505SJordan K. Hubbard }
507f46af505SJordan K. Hubbard 
508f46af505SJordan K. Hubbard 
5094be1e61bSAlexander Langer static void
5104be1e61bSAlexander Langer init_sector0(unsigned long start)
5115b81b6b3SRodney W. Grimes {
5125b81b6b3SRodney W. Grimes 	struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
5135b81b6b3SRodney W. Grimes 
514f46af505SJordan K. Hubbard 	init_boot();
5155b81b6b3SRodney W. Grimes 
5165b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
5175b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
518b594e0feSJohn Baldwin 	start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
51985c2cf30SJohn Baldwin 	if(start == 0)
52085c2cf30SJohn Baldwin 		start = dos_sectors;
5215b81b6b3SRodney W. Grimes 	partp->dp_start = start;
522b594e0feSJohn Baldwin 	partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
5235b81b6b3SRodney W. Grimes 
52463692187SIan Dowse 	dos(partp);
5255b81b6b3SRodney W. Grimes }
5265b81b6b3SRodney W. Grimes 
5274be1e61bSAlexander Langer static void
5284be1e61bSAlexander Langer change_part(int i)
5295b81b6b3SRodney W. Grimes {
5304ddd60b9SBrian Somers 	struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
5315b81b6b3SRodney W. Grimes 
5325b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
5335b81b6b3SRodney W. Grimes     print_part(i);
5345b81b6b3SRodney W. Grimes 
5355b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
5365b81b6b3SRodney W. Grimes 	int tmp;
5375b81b6b3SRodney W. Grimes 
5385b81b6b3SRodney W. Grimes 	if (i_flag) {
5395b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
5404ddd60b9SBrian Somers 		if (i == 4) {
5415b81b6b3SRodney W. Grimes 			init_sector0(1);
5424ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
5435b81b6b3SRodney W. Grimes 			print_part(i);
5445b81b6b3SRodney W. Grimes 		}
5455b81b6b3SRodney W. Grimes 	}
5465b81b6b3SRodney W. Grimes 
5475b81b6b3SRodney W. Grimes 	do {
548680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5495b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5505b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
55163692187SIan Dowse 		if (!sanitize_partition(partp)) {
55263692187SIan Dowse 			warnx("ERROR: failed to adjust; setting sysid to 0");
55363692187SIan Dowse 			partp->dp_typ = 0;
55463692187SIan Dowse 		}
5555b81b6b3SRodney W. Grimes 
5564b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5575b81b6b3SRodney W. Grimes 		{
5585b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5595b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5605b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5615b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5625b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5630469c254SDavid E. O'Brien 			Decimal("beginning head", thd, tmp);
5646580291bSDavid E. O'Brien 			Decimal("beginning sector", tsec, tmp);
5655b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5665b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5675b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5685b81b6b3SRodney W. Grimes 
5695b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5705b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5715b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5725b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5730469c254SDavid E. O'Brien 			Decimal("ending head", thd, tmp);
5746580291bSDavid E. O'Brien 			Decimal("ending sector", tsec, tmp);
5755b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5765b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5775b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
57863692187SIan Dowse 		} else
57963692187SIan Dowse 			dos(partp);
5805b81b6b3SRodney W. Grimes 
5815b81b6b3SRodney W. Grimes 		print_part(i);
5825b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
5835b81b6b3SRodney W. Grimes     }
5845b81b6b3SRodney W. Grimes     }
5855b81b6b3SRodney W. Grimes 
5864be1e61bSAlexander Langer static void
5875b81b6b3SRodney W. Grimes print_params()
5885b81b6b3SRodney W. Grimes {
5895b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
5905b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5915b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
5925b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
5935b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
5945b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
5955b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5965b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
5975b81b6b3SRodney W. Grimes }
5985b81b6b3SRodney W. Grimes 
5994be1e61bSAlexander Langer static void
6004be1e61bSAlexander Langer change_active(int which)
6015b81b6b3SRodney W. Grimes {
60263692187SIan Dowse 	struct dos_partition *partp = &mboot.parts[0];
60363692187SIan Dowse 	int active, i, new, tmp;
6045b81b6b3SRodney W. Grimes 
60563692187SIan Dowse 	active = -1;
60663692187SIan Dowse 	for (i = 0; i < NDOSPART; i++) {
60763692187SIan Dowse 		if ((partp[i].dp_flag & ACTIVE) == 0)
60863692187SIan Dowse 			continue;
60963692187SIan Dowse 		printf("Partition %d is marked active\n", i + 1);
61063692187SIan Dowse 		if (active == -1)
61163692187SIan Dowse 			active = i + 1;
61263692187SIan Dowse 	}
6135b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
6145b81b6b3SRodney W. Grimes 		active = which;
61563692187SIan Dowse 	else if (active == -1)
61663692187SIan Dowse 		active = 1;
61763692187SIan Dowse 
6180b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
6190b461cd7SBruce Evans 		return;
620680426beSDavid E. O'Brien setactive:
621680426beSDavid E. O'Brien 	do {
62263692187SIan Dowse 		new = active;
62363692187SIan Dowse 		Decimal("active partition", new, tmp);
62463692187SIan Dowse 		if (new < 1 || new > 4) {
625680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
626680426beSDavid E. O'Brien 					"  Try again.\n");
627680426beSDavid E. O'Brien 			goto setactive;
628680426beSDavid E. O'Brien 		}
62963692187SIan Dowse 		active = new;
630680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
6315b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
6325b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
6334ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
6344ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
6355b81b6b3SRodney W. Grimes }
6365b81b6b3SRodney W. Grimes 
63712d910e9SRobert Nordier static void
63812d910e9SRobert Nordier change_code()
63912d910e9SRobert Nordier {
64012d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
64112d910e9SRobert Nordier 		init_boot();
64212d910e9SRobert Nordier }
64312d910e9SRobert Nordier 
6444be1e61bSAlexander Langer void
6455b81b6b3SRodney W. Grimes get_params_to_use()
6465b81b6b3SRodney W. Grimes {
6475b81b6b3SRodney W. Grimes 	int	tmp;
6485b81b6b3SRodney W. Grimes 	print_params();
6495b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
6505b81b6b3SRodney W. Grimes 	{
6515b81b6b3SRodney W. Grimes 		do
6525b81b6b3SRodney W. Grimes 		{
6535b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
6545b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
6555b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6565b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6575b81b6b3SRodney W. Grimes 			print_params();
6585b81b6b3SRodney W. Grimes 		}
6595b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6605b81b6b3SRodney W. Grimes 	}
6615b81b6b3SRodney W. Grimes }
6625b81b6b3SRodney W. Grimes 
663f46af505SJordan K. Hubbard 
6645b81b6b3SRodney W. Grimes /***********************************************\
6655b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6665b81b6b3SRodney W. Grimes \***********************************************/
6674be1e61bSAlexander Langer static void
668041b8b00SPoul-Henning Kamp dos(struct dos_partition *partp)
6695b81b6b3SRodney W. Grimes {
67063692187SIan Dowse 	int cy, sec;
67163692187SIan Dowse 	u_int32_t end;
6725b81b6b3SRodney W. Grimes 
673881c9063SIan Dowse 	if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
674881c9063SIan Dowse 		memcpy(partp, &mtpart, sizeof(*partp));
6750b461cd7SBruce Evans 		return;
6760b461cd7SBruce Evans 	}
6770b461cd7SBruce Evans 
67863692187SIan Dowse 	/* Start c/h/s. */
67963692187SIan Dowse 	partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
68063692187SIan Dowse 	cy = partp->dp_start / dos_cylsecs;
68163692187SIan Dowse 	sec = partp->dp_start % dos_sectors + 1;
68263692187SIan Dowse 	partp->dp_scyl = DOSCYL(cy);
68363692187SIan Dowse 	partp->dp_ssect = DOSSECT(sec, cy);
6845b81b6b3SRodney W. Grimes 
68563692187SIan Dowse 	/* End c/h/s. */
68663692187SIan Dowse 	end = partp->dp_start + partp->dp_size - 1;
68763692187SIan Dowse 	partp->dp_ehd = end % dos_cylsecs / dos_sectors;
68863692187SIan Dowse 	cy = end / dos_cylsecs;
68963692187SIan Dowse 	sec = end % dos_sectors + 1;
69063692187SIan Dowse 	partp->dp_ecyl = DOSCYL(cy);
69163692187SIan Dowse 	partp->dp_esect = DOSSECT(sec, cy);
6925b81b6b3SRodney W. Grimes }
6935b81b6b3SRodney W. Grimes 
6944be1e61bSAlexander Langer static int
695041b8b00SPoul-Henning Kamp open_disk(int flag)
6965b81b6b3SRodney W. Grimes {
6975b81b6b3SRodney W. Grimes 	struct stat 	st;
698f353c761SPoul-Henning Kamp 	int rwmode, p;
699f353c761SPoul-Henning Kamp 	char *s;
7005b81b6b3SRodney W. Grimes 
701f353c761SPoul-Henning Kamp 	fdw = -1;
7025b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
7038de9ed22SJoerg Wunsch 		if (errno == ENOENT)
7048de9ed22SJoerg Wunsch 			return -2;
705d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
7065b81b6b3SRodney W. Grimes 		return -1;
707b60eb395SBruce Evans 	}
708b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
709d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
710f353c761SPoul-Henning Kamp 	rwmode = a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY;
711f353c761SPoul-Henning Kamp 	fd = open(disk, rwmode);
712f353c761SPoul-Henning Kamp 	if (fd == -1 && errno == ENXIO)
713e3038c6eSJoerg Wunsch 		return -2;
714f353c761SPoul-Henning Kamp 	if (fd == -1 && errno == EPERM && rwmode == O_RDWR) {
715f353c761SPoul-Henning Kamp 		fd = open(disk, O_RDONLY);
716f353c761SPoul-Henning Kamp 		if (fd == -1)
717f353c761SPoul-Henning Kamp 			return -3;
718f353c761SPoul-Henning Kamp 		for (p = 1; p < 5; p++) {
719f353c761SPoul-Henning Kamp 			asprintf(&s, "%ss%d", disk, p);
720f353c761SPoul-Henning Kamp 			fdw = open(s, O_RDONLY);
721f353c761SPoul-Henning Kamp 			free(s);
722f353c761SPoul-Henning Kamp 			if (fdw == -1)
723f353c761SPoul-Henning Kamp 				continue;
724f353c761SPoul-Henning Kamp 			break;
725f353c761SPoul-Henning Kamp 		}
726f353c761SPoul-Henning Kamp 		if (fdw == -1)
727f353c761SPoul-Henning Kamp 			return -4;
728f353c761SPoul-Henning Kamp 	}
729f353c761SPoul-Henning Kamp 	if (fd == -1) {
730d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
7315b81b6b3SRodney W. Grimes 		return -1;
7325b81b6b3SRodney W. Grimes 	}
733041b8b00SPoul-Henning Kamp 	if (get_params() == -1) {
734d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
7355b81b6b3SRodney W. Grimes 		return -1;
7365b81b6b3SRodney W. Grimes 	}
7375b81b6b3SRodney W. Grimes 	return fd;
7385b81b6b3SRodney W. Grimes }
7395b81b6b3SRodney W. Grimes 
7404be1e61bSAlexander Langer static ssize_t
7414be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
7425b81b6b3SRodney W. Grimes {
7435b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7447cb29d33SSøren Schmidt 	if( secsize == 0 )
7457cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
7467cb29d33SSøren Schmidt 			{
7477cb29d33SSøren Schmidt 			/* try the read */
7487cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
7497cb29d33SSøren Schmidt 			if( size == secsize )
7507cb29d33SSøren Schmidt 				/* it worked so return */
7517cb29d33SSøren Schmidt 				return secsize;
7527cb29d33SSøren Schmidt 			}
7537cb29d33SSøren Schmidt 	else
7547cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
7557cb29d33SSøren Schmidt 
7567cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
7577cb29d33SSøren Schmidt 	return -1;
7585b81b6b3SRodney W. Grimes }
7595b81b6b3SRodney W. Grimes 
7604be1e61bSAlexander Langer static ssize_t
7614be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
7625b81b6b3SRodney W. Grimes {
763f353c761SPoul-Henning Kamp 
764f353c761SPoul-Henning Kamp 	if (fdw != -1) {
765f353c761SPoul-Henning Kamp 		return ioctl(fdw, DIOCSMBR, buf);
766f353c761SPoul-Henning Kamp 	} else {
7675b81b6b3SRodney W. Grimes 		lseek(fd,(sector * 512), 0);
7687cb29d33SSøren Schmidt 		/* write out in the size that the read_disk found worked */
7697cb29d33SSøren Schmidt 		return write(fd, buf, secsize);
7705b81b6b3SRodney W. Grimes 	}
771f353c761SPoul-Henning Kamp }
7725b81b6b3SRodney W. Grimes 
7734be1e61bSAlexander Langer static int
7744be1e61bSAlexander Langer get_params()
7755b81b6b3SRodney W. Grimes {
7763f8ba8b5SPoul-Henning Kamp 	int error;
7773f8ba8b5SPoul-Henning Kamp 	u_int u;
7783f8ba8b5SPoul-Henning Kamp 	off_t o;
7795b81b6b3SRodney W. Grimes 
7807963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGFWSECTORS, &u);
7817963fae6SPoul-Henning Kamp 	if (error == 0)
7827963fae6SPoul-Henning Kamp 		sectors = dos_sectors = u;
783c0fdfdbaSPoul-Henning Kamp 	else
784c0fdfdbaSPoul-Henning Kamp 		sectors = dos_sectors = 63;
785c0fdfdbaSPoul-Henning Kamp 
7867963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGFWHEADS, &u);
7877963fae6SPoul-Henning Kamp 	if (error == 0)
7887963fae6SPoul-Henning Kamp 		heads = dos_heads = u;
789c0fdfdbaSPoul-Henning Kamp 	else
790c0fdfdbaSPoul-Henning Kamp 		heads = dos_heads = 255;
7917963fae6SPoul-Henning Kamp 
7925b81b6b3SRodney W. Grimes 	dos_cylsecs = cylsecs = heads * sectors;
7935b81b6b3SRodney W. Grimes 	disksecs = cyls * heads * sectors;
7945b81b6b3SRodney W. Grimes 
7957963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGSECTORSIZE, &u);
796c0fdfdbaSPoul-Henning Kamp 	if (error != 0 || u == 0)
7977963fae6SPoul-Henning Kamp 		u = 512;
7987963fae6SPoul-Henning Kamp 
7997963fae6SPoul-Henning Kamp 	error = ioctl(fd, DIOCGMEDIASIZE, &o);
8007963fae6SPoul-Henning Kamp 	if (error == 0) {
8017963fae6SPoul-Henning Kamp 		disksecs = o / u;
8027963fae6SPoul-Henning Kamp 		cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
8037963fae6SPoul-Henning Kamp 	}
8047963fae6SPoul-Henning Kamp 
8055b81b6b3SRodney W. Grimes 	return (disksecs);
8065b81b6b3SRodney W. Grimes }
8075b81b6b3SRodney W. Grimes 
8085b81b6b3SRodney W. Grimes 
8094be1e61bSAlexander Langer static int
8105b81b6b3SRodney W. Grimes read_s0()
8115b81b6b3SRodney W. Grimes {
81285c2cf30SJohn Baldwin 	mboot.bootinst_size = secsize;
8132b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
8142b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
81585c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
81685c2cf30SJohn Baldwin 		warnx("unable to allocate buffer to read fdisk "
81785c2cf30SJohn Baldwin 		      "partition table");
81885c2cf30SJohn Baldwin 		return -1;
81985c2cf30SJohn Baldwin 	}
82085c2cf30SJohn Baldwin 	if (read_disk(0, mboot.bootinst) == -1) {
821d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
8225b81b6b3SRodney W. Grimes 		return -1;
8235b81b6b3SRodney W. Grimes 	}
824689fee87SBruce Evans 	if (*(uint16_t *)(void *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
825d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
8265b81b6b3SRodney W. Grimes 		/* So should we initialize things */
8275b81b6b3SRodney W. Grimes 		return -1;
8285b81b6b3SRodney W. Grimes 	}
82985c2cf30SJohn Baldwin 	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
8305b81b6b3SRodney W. Grimes 	return 0;
8315b81b6b3SRodney W. Grimes }
8325b81b6b3SRodney W. Grimes 
8334be1e61bSAlexander Langer static int
8345b81b6b3SRodney W. Grimes write_s0()
8355b81b6b3SRodney W. Grimes {
83685c2cf30SJohn Baldwin 	int	sector;
83785c2cf30SJohn Baldwin 
8385b81b6b3SRodney W. Grimes 	if (iotest) {
8395b81b6b3SRodney W. Grimes 		print_s0(-1);
8405b81b6b3SRodney W. Grimes 		return 0;
8415b81b6b3SRodney W. Grimes 	}
84285c2cf30SJohn Baldwin 	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
8435b81b6b3SRodney W. Grimes 	/*
8445b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
8455b81b6b3SRodney W. Grimes 	 * disable after writing.
8465b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
8475b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
8485b81b6b3SRodney W. Grimes 	 */
84985c2cf30SJohn Baldwin 	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
85085c2cf30SJohn Baldwin 		if (write_disk(sector,
85185c2cf30SJohn Baldwin 			       &mboot.bootinst[sector * secsize]) == -1) {
852e6fb3ddeSPoul-Henning Kamp 			warn("can't write fdisk partition table");
8535b81b6b3SRodney W. Grimes 			return -1;
8545b81b6b3SRodney W. Grimes 		}
8554be1e61bSAlexander Langer 	return(0);
8565b81b6b3SRodney W. Grimes }
8575b81b6b3SRodney W. Grimes 
8585b81b6b3SRodney W. Grimes 
8594be1e61bSAlexander Langer static int
860041b8b00SPoul-Henning Kamp ok(const char *str)
8615b81b6b3SRodney W. Grimes {
8625b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
86363692187SIan Dowse 	fflush(stdout);
86463692187SIan Dowse 	if (fgets(lbuf, LBUF, stdin) == NULL)
86563692187SIan Dowse 		exit(1);
8665b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
8675b81b6b3SRodney W. Grimes 
8685b81b6b3SRodney W. Grimes 	if (*lbuf &&
8695b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
8705b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
8715b81b6b3SRodney W. Grimes 		return 1;
8725b81b6b3SRodney W. Grimes 	else
8735b81b6b3SRodney W. Grimes 		return 0;
8745b81b6b3SRodney W. Grimes }
8755b81b6b3SRodney W. Grimes 
8764be1e61bSAlexander Langer static int
877041b8b00SPoul-Henning Kamp decimal(const char *str, int *num, int deflt)
8785b81b6b3SRodney W. Grimes {
8795b81b6b3SRodney W. Grimes 	int acc = 0, c;
8805b81b6b3SRodney W. Grimes 	char *cp;
8815b81b6b3SRodney W. Grimes 
8825b81b6b3SRodney W. Grimes 	while (1) {
8835b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
88463692187SIan Dowse 		fflush(stdout);
88563692187SIan Dowse 		if (fgets(lbuf, LBUF, stdin) == NULL)
88663692187SIan Dowse 			exit(1);
8875b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8885b81b6b3SRodney W. Grimes 
8895b81b6b3SRodney W. Grimes 		if (!*lbuf)
8905b81b6b3SRodney W. Grimes 			return 0;
8915b81b6b3SRodney W. Grimes 
8925b81b6b3SRodney W. Grimes 		cp = lbuf;
8935b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8945b81b6b3SRodney W. Grimes 		if (!c)
8955b81b6b3SRodney W. Grimes 			return 0;
8964be1e61bSAlexander Langer 		while ((c = *cp++)) {
8975b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8985b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
8995b81b6b3SRodney W. Grimes 			else
9005b81b6b3SRodney W. Grimes 				break;
9015b81b6b3SRodney W. Grimes 		}
9025b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
9035b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9045b81b6b3SRodney W. Grimes 		if (!c) {
9055b81b6b3SRodney W. Grimes 			*num = acc;
9065b81b6b3SRodney W. Grimes 			return 1;
9075b81b6b3SRodney W. Grimes 		} else
908680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
9095b81b6b3SRodney W. Grimes 				lbuf);
9105b81b6b3SRodney W. Grimes 	}
9115b81b6b3SRodney W. Grimes 
9125b81b6b3SRodney W. Grimes }
9135b81b6b3SRodney W. Grimes 
914041b8b00SPoul-Henning Kamp static const char *
9154be1e61bSAlexander Langer get_type(int type)
9165b81b6b3SRodney W. Grimes {
9175b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
9185b81b6b3SRodney W. Grimes 	int	counter = 0;
9195b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
9205b81b6b3SRodney W. Grimes 
9215b81b6b3SRodney W. Grimes 
922041b8b00SPoul-Henning Kamp 	while(counter < numentries) {
9235b81b6b3SRodney W. Grimes 		if(ptr->type == type)
9245b81b6b3SRodney W. Grimes 			return(ptr->name);
9255b81b6b3SRodney W. Grimes 		ptr++;
9265b81b6b3SRodney W. Grimes 		counter++;
9275b81b6b3SRodney W. Grimes 	}
9285b81b6b3SRodney W. Grimes 	return("unknown");
9295b81b6b3SRodney W. Grimes }
930f46af505SJordan K. Hubbard 
931f46af505SJordan K. Hubbard 
932f46af505SJordan K. Hubbard static void
933041b8b00SPoul-Henning Kamp parse_config_line(char *line, CMD *command)
934f46af505SJordan K. Hubbard {
935f46af505SJordan K. Hubbard     char	*cp, *end;
936f46af505SJordan K. Hubbard 
937f46af505SJordan K. Hubbard     cp = line;
938041b8b00SPoul-Henning Kamp     while (1) {
939f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
940f46af505SJordan K. Hubbard 
941f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
942f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
943f46af505SJordan K. Hubbard 	    break;
944f46af505SJordan K. Hubbard 	command->cmd = *cp++;
945f46af505SJordan K. Hubbard 
946f46af505SJordan K. Hubbard 	/*
947f46af505SJordan K. Hubbard 	 * Parse args
948f46af505SJordan K. Hubbard 	 */
949041b8b00SPoul-Henning Kamp 	    while (1) {
950f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
951f46af505SJordan K. Hubbard 	    if (*cp == '#')
952f46af505SJordan K. Hubbard 		break;		/* found comment */
953f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
954f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
955f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
956f46af505SJordan K. Hubbard 		break;		/* assume end of line */
957f46af505SJordan K. Hubbard 	    end = NULL;
958f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
959f46af505SJordan K. Hubbard 	    if (cp == end)
960f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
961f46af505SJordan K. Hubbard 	    cp = end;
962f46af505SJordan K. Hubbard 	    command->n_args++;
963f46af505SJordan K. Hubbard 	}
964f46af505SJordan K. Hubbard 	break;
965f46af505SJordan K. Hubbard     }
966f46af505SJordan K. Hubbard }
967f46af505SJordan K. Hubbard 
968f46af505SJordan K. Hubbard 
969f46af505SJordan K. Hubbard static int
970041b8b00SPoul-Henning Kamp process_geometry(CMD *command)
971f46af505SJordan K. Hubbard {
972f46af505SJordan K. Hubbard     int		status = 1, i;
973f46af505SJordan K. Hubbard 
974041b8b00SPoul-Henning Kamp     while (1) {
975f46af505SJordan K. Hubbard 	geom_processed = 1;
976041b8b00SPoul-Henning Kamp 	    if (part_processed) {
977d98b1668SPhilippe Charnier 	    warnx(
978d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
979d98b1668SPhilippe Charnier     all partition specifications",
980d98b1668SPhilippe Charnier 		    current_line_number);
981f46af505SJordan K. Hubbard 	    status = 0;
982f46af505SJordan K. Hubbard 	    break;
983f46af505SJordan K. Hubbard 	}
984041b8b00SPoul-Henning Kamp 	    if (command->n_args != 3) {
985d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
986d98b1668SPhilippe Charnier 		    current_line_number);
987f46af505SJordan K. Hubbard 	    status = 0;
988f46af505SJordan K. Hubbard 	    break;
989f46af505SJordan K. Hubbard 	}
990041b8b00SPoul-Henning Kamp 	    dos_cyls = 0;
991041b8b00SPoul-Henning Kamp 	    dos_heads = 0;
992041b8b00SPoul-Henning Kamp 	    dos_sectors = 0;
993041b8b00SPoul-Henning Kamp 	    for (i = 0; i < 3; ++i) {
994041b8b00SPoul-Henning Kamp 		    switch (command->args[i].argtype) {
995f46af505SJordan K. Hubbard 	    case 'c':
996f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
997f46af505SJordan K. Hubbard 		break;
998f46af505SJordan K. Hubbard 	    case 'h':
999f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
1000f46af505SJordan K. Hubbard 		break;
1001f46af505SJordan K. Hubbard 	    case 's':
1002f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
1003f46af505SJordan K. Hubbard 		break;
1004f46af505SJordan K. Hubbard 	    default:
1005d98b1668SPhilippe Charnier 		warnx(
1006d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1007d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1008f46af505SJordan K. Hubbard 			command->args[i].argtype);
1009f46af505SJordan K. Hubbard 		status = 0;
1010f46af505SJordan K. Hubbard 		break;
1011f46af505SJordan K. Hubbard 	    }
1012f46af505SJordan K. Hubbard 	}
1013f46af505SJordan K. Hubbard 	if (status == 0)
1014f46af505SJordan K. Hubbard 	    break;
1015f46af505SJordan K. Hubbard 
1016f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1017f46af505SJordan K. Hubbard 
1018f46af505SJordan K. Hubbard 	/*
1019f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1020f46af505SJordan K. Hubbard 	 */
1021041b8b00SPoul-Henning Kamp 	    if (dos_cyls == 0) {
1022d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1023d98b1668SPhilippe Charnier 		    current_line_number);
1024f46af505SJordan K. Hubbard 	    status = 0;
1025f46af505SJordan K. Hubbard 	}
1026041b8b00SPoul-Henning Kamp 	    if (dos_cyls > 1024) {
1027d98b1668SPhilippe Charnier 	    warnx(
1028d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1029f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1030d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1031d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1032f46af505SJordan K. Hubbard 	}
1033f46af505SJordan K. Hubbard 
1034041b8b00SPoul-Henning Kamp 	    if (dos_heads == 0) {
1035d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1036d98b1668SPhilippe Charnier 		    current_line_number);
1037f46af505SJordan K. Hubbard 	    status = 0;
1038041b8b00SPoul-Henning Kamp 	    } else if (dos_heads > 256) {
1039d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1040d98b1668SPhilippe Charnier 		    current_line_number);
1041f46af505SJordan K. Hubbard 	    status = 0;
1042f46af505SJordan K. Hubbard 	}
1043f46af505SJordan K. Hubbard 
1044041b8b00SPoul-Henning Kamp 	    if (dos_sectors == 0) {
1045d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1046d98b1668SPhilippe Charnier 		    current_line_number);
1047f46af505SJordan K. Hubbard 	    status = 0;
1048041b8b00SPoul-Henning Kamp 	    } else if (dos_sectors > 63) {
1049d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1050d98b1668SPhilippe Charnier 		    current_line_number);
1051f46af505SJordan K. Hubbard 	    status = 0;
1052f46af505SJordan K. Hubbard 	}
1053f46af505SJordan K. Hubbard 
1054f46af505SJordan K. Hubbard 	break;
1055f46af505SJordan K. Hubbard     }
1056f46af505SJordan K. Hubbard     return (status);
1057f46af505SJordan K. Hubbard }
1058f46af505SJordan K. Hubbard 
1059f46af505SJordan K. Hubbard 
1060f46af505SJordan K. Hubbard static int
1061041b8b00SPoul-Henning Kamp process_partition(CMD *command)
1062f46af505SJordan K. Hubbard {
1063f46af505SJordan K. Hubbard     int				status = 0, partition;
1064b594e0feSJohn Baldwin     u_int32_t			prev_head_boundary, prev_cyl_boundary;
1065b594e0feSJohn Baldwin     u_int32_t			adj_size, max_end;
1066f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1067f46af505SJordan K. Hubbard 
1068041b8b00SPoul-Henning Kamp 	while (1) {
1069f46af505SJordan K. Hubbard 	part_processed = 1;
1070041b8b00SPoul-Henning Kamp 		if (command->n_args != 4) {
1071d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1072d98b1668SPhilippe Charnier 		    current_line_number);
1073f46af505SJordan K. Hubbard 	    break;
1074f46af505SJordan K. Hubbard 	}
1075f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
1076041b8b00SPoul-Henning Kamp 		if (partition < 1 || partition > 4) {
1077d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1078d98b1668SPhilippe Charnier 		    current_line_number, partition);
1079f46af505SJordan K. Hubbard 	    break;
1080f46af505SJordan K. Hubbard 	}
10814ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1082f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1083f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1084f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1085f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1086f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1087f46af505SJordan K. Hubbard 
1088041b8b00SPoul-Henning Kamp 		if (partp->dp_typ == 0) {
1089f46af505SJordan K. Hubbard 	    /*
1090f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1091f46af505SJordan K. Hubbard 	     */
1092f46af505SJordan K. Hubbard 	    /*
1093f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1094f46af505SJordan K. Hubbard 	     */
1095f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1096f46af505SJordan K. Hubbard 	    status = 1;
1097f46af505SJordan K. Hubbard 	    break;
1098f46af505SJordan K. Hubbard 	}
1099f46af505SJordan K. Hubbard 
1100f46af505SJordan K. Hubbard 	/*
1101d64ada50SJens Schweikhardt 	 * Adjust start upwards, if necessary, to fall on a head boundary.
1102f46af505SJordan K. Hubbard 	 */
1103041b8b00SPoul-Henning Kamp 		if (partp->dp_start % dos_sectors != 0) {
1104b594e0feSJohn Baldwin 	    prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1105b594e0feSJohn Baldwin 	    if (max_end < dos_sectors ||
1106041b8b00SPoul-Henning Kamp 			    prev_head_boundary > max_end - dos_sectors) {
1107f46af505SJordan K. Hubbard 		/*
1108f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1109f46af505SJordan K. Hubbard 		 */
1110d98b1668SPhilippe Charnier 		warnx(
1111d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1112b594e0feSJohn Baldwin     a head boundary",
1113d98b1668SPhilippe Charnier 			current_line_number, partition);
1114f46af505SJordan K. Hubbard 		break;
1115f46af505SJordan K. Hubbard 	    }
1116d98b1668SPhilippe Charnier 	    warnx(
1117b594e0feSJohn Baldwin 	"WARNING: adjusting start offset of partition %d\n\
1118b594e0feSJohn Baldwin     from %u to %u, to fall on a head boundary",
1119b594e0feSJohn Baldwin 		    partition, (u_int)partp->dp_start,
1120b594e0feSJohn Baldwin 		    (u_int)(prev_head_boundary + dos_sectors));
1121b594e0feSJohn Baldwin 	    partp->dp_start = prev_head_boundary + dos_sectors;
1122f46af505SJordan K. Hubbard 	}
1123f46af505SJordan K. Hubbard 
1124f46af505SJordan K. Hubbard 	/*
1125f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1126f46af505SJordan K. Hubbard 	 * boundary.
1127f46af505SJordan K. Hubbard 	 */
1128b594e0feSJohn Baldwin 	prev_cyl_boundary =
1129f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1130b594e0feSJohn Baldwin 	if (prev_cyl_boundary > partp->dp_start)
1131b594e0feSJohn Baldwin 	    adj_size = prev_cyl_boundary - partp->dp_start;
1132041b8b00SPoul-Henning Kamp 		else {
1133b594e0feSJohn Baldwin 	    warnx(
1134b594e0feSJohn Baldwin 	"ERROR: could not adjust partition to start on a head boundary\n\
1135b594e0feSJohn Baldwin     and end on a cylinder boundary.");
1136b594e0feSJohn Baldwin 	    return (0);
1137b594e0feSJohn Baldwin 	}
1138041b8b00SPoul-Henning Kamp 		if (adj_size != partp->dp_size) {
1139d98b1668SPhilippe Charnier 	    warnx(
1140b594e0feSJohn Baldwin 	"WARNING: adjusting size of partition %d from %u to %u\n\
1141b594e0feSJohn Baldwin     to end on a cylinder boundary",
1142b594e0feSJohn Baldwin 		    partition, (u_int)partp->dp_size, (u_int)adj_size);
1143f46af505SJordan K. Hubbard 	    partp->dp_size = adj_size;
1144f46af505SJordan K. Hubbard 	}
1145041b8b00SPoul-Henning Kamp 		if (partp->dp_size == 0) {
114663692187SIan Dowse 	    warnx("ERROR line %d: size of partition %d is zero",
1147d98b1668SPhilippe Charnier 		    current_line_number, partition);
1148f46af505SJordan K. Hubbard 	    break;
1149f46af505SJordan K. Hubbard 	}
1150f46af505SJordan K. Hubbard 
115163692187SIan Dowse 	dos(partp);
1152f46af505SJordan K. Hubbard 	status = 1;
1153f46af505SJordan K. Hubbard 	break;
1154f46af505SJordan K. Hubbard     }
1155f46af505SJordan K. Hubbard     return (status);
1156f46af505SJordan K. Hubbard }
1157f46af505SJordan K. Hubbard 
1158f46af505SJordan K. Hubbard 
1159f46af505SJordan K. Hubbard static int
1160041b8b00SPoul-Henning Kamp process_active(CMD *command)
1161f46af505SJordan K. Hubbard {
1162f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1163f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1164f46af505SJordan K. Hubbard 
1165041b8b00SPoul-Henning Kamp 	while (1) {
1166f46af505SJordan K. Hubbard 	active_processed = 1;
1167041b8b00SPoul-Henning Kamp 		if (command->n_args != 1) {
1168d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1169d98b1668SPhilippe Charnier 		    current_line_number);
1170f46af505SJordan K. Hubbard 	    status = 0;
1171f46af505SJordan K. Hubbard 	    break;
1172f46af505SJordan K. Hubbard 	}
1173f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
1174041b8b00SPoul-Henning Kamp 		if (partition < 1 || partition > 4) {
1175d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1176d98b1668SPhilippe Charnier 		    current_line_number, partition);
1177f46af505SJordan K. Hubbard 	    break;
1178f46af505SJordan K. Hubbard 	}
1179f46af505SJordan K. Hubbard 	/*
1180f46af505SJordan K. Hubbard 	 * Reset active partition
1181f46af505SJordan K. Hubbard 	 */
1182f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1183f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1184f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
11854ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1186f46af505SJordan K. Hubbard 
1187f46af505SJordan K. Hubbard 	status = 1;
1188f46af505SJordan K. Hubbard 	break;
1189f46af505SJordan K. Hubbard     }
1190f46af505SJordan K. Hubbard     return (status);
1191f46af505SJordan K. Hubbard }
1192f46af505SJordan K. Hubbard 
1193f46af505SJordan K. Hubbard 
1194f46af505SJordan K. Hubbard static int
1195041b8b00SPoul-Henning Kamp process_line(char *line)
1196f46af505SJordan K. Hubbard {
1197f46af505SJordan K. Hubbard     CMD		command;
1198f46af505SJordan K. Hubbard     int		status = 1;
1199f46af505SJordan K. Hubbard 
1200041b8b00SPoul-Henning Kamp 	while (1) {
1201f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1202041b8b00SPoul-Henning Kamp 		switch (command.cmd) {
1203f46af505SJordan K. Hubbard 	case 0:
1204f46af505SJordan K. Hubbard 	    /*
1205f46af505SJordan K. Hubbard 	     * Comment or blank line
1206f46af505SJordan K. Hubbard 	     */
1207f46af505SJordan K. Hubbard 	    break;
1208f46af505SJordan K. Hubbard 	case 'g':
1209f46af505SJordan K. Hubbard 	    /*
1210f46af505SJordan K. Hubbard 	     * Set geometry
1211f46af505SJordan K. Hubbard 	     */
1212f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1213f46af505SJordan K. Hubbard 	    break;
1214f46af505SJordan K. Hubbard 	case 'p':
1215f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1216f46af505SJordan K. Hubbard 	    break;
1217f46af505SJordan K. Hubbard 	case 'a':
1218f46af505SJordan K. Hubbard 	    status = process_active(&command);
1219f46af505SJordan K. Hubbard 	    break;
1220f46af505SJordan K. Hubbard 	default:
1221f46af505SJordan K. Hubbard 	    status = 0;
1222f46af505SJordan K. Hubbard 	    break;
1223f46af505SJordan K. Hubbard 	}
1224f46af505SJordan K. Hubbard 	break;
1225f46af505SJordan K. Hubbard     }
1226f46af505SJordan K. Hubbard     return (status);
1227f46af505SJordan K. Hubbard }
1228f46af505SJordan K. Hubbard 
1229f46af505SJordan K. Hubbard 
1230f46af505SJordan K. Hubbard static int
1231041b8b00SPoul-Henning Kamp read_config(char *config_file)
1232f46af505SJordan K. Hubbard {
1233f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1234f46af505SJordan K. Hubbard     int		status = 1;
1235f46af505SJordan K. Hubbard     char	buf[1010];
1236f46af505SJordan K. Hubbard 
1237041b8b00SPoul-Henning Kamp 	while (1) {
1238041b8b00SPoul-Henning Kamp 		if (strcmp(config_file, "-") != 0) {
1239f46af505SJordan K. Hubbard 	    /*
1240f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1241f46af505SJordan K. Hubbard 	     */
1242041b8b00SPoul-Henning Kamp 			if ((fp = fopen(config_file, "r")) == NULL) {
1243f46af505SJordan K. Hubbard 		status = 0;
1244f46af505SJordan K. Hubbard 		break;
1245f46af505SJordan K. Hubbard 	    }
1246041b8b00SPoul-Henning Kamp 		} else {
1247f46af505SJordan K. Hubbard 	    fp = stdin;
1248f46af505SJordan K. Hubbard 	}
1249f46af505SJordan K. Hubbard 	current_line_number = 0;
1250041b8b00SPoul-Henning Kamp 		while (!feof(fp)) {
1251f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1252f46af505SJordan K. Hubbard 		break;
1253f46af505SJordan K. Hubbard 	    ++current_line_number;
1254f46af505SJordan K. Hubbard 	    status = process_line(buf);
1255f46af505SJordan K. Hubbard 	    if (status == 0)
1256f46af505SJordan K. Hubbard 		break;
1257f46af505SJordan K. Hubbard 	    }
1258f46af505SJordan K. Hubbard 	break;
1259f46af505SJordan K. Hubbard     }
1260041b8b00SPoul-Henning Kamp 	if (fp) {
1261f46af505SJordan K. Hubbard 	/*
1262f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1263f46af505SJordan K. Hubbard 	 */
1264f46af505SJordan K. Hubbard 	fclose(fp);
1265f46af505SJordan K. Hubbard     }
1266f46af505SJordan K. Hubbard     return (status);
1267f46af505SJordan K. Hubbard }
1268f46af505SJordan K. Hubbard 
1269f46af505SJordan K. Hubbard 
1270f46af505SJordan K. Hubbard static void
1271f46af505SJordan K. Hubbard reset_boot(void)
1272f46af505SJordan K. Hubbard {
1273f46af505SJordan K. Hubbard     int				i;
1274f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1275f46af505SJordan K. Hubbard 
1276f46af505SJordan K. Hubbard     init_boot();
1277041b8b00SPoul-Henning Kamp 	for (i = 0; i < 4; ++i) {
1278f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1279f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1280f46af505SJordan K. Hubbard     }
1281f46af505SJordan K. Hubbard }
1282b594e0feSJohn Baldwin 
1283b594e0feSJohn Baldwin static int
1284041b8b00SPoul-Henning Kamp sanitize_partition(struct dos_partition *partp)
1285b594e0feSJohn Baldwin {
1286b594e0feSJohn Baldwin     u_int32_t			prev_head_boundary, prev_cyl_boundary;
128763692187SIan Dowse     u_int32_t			max_end, size, start;
1288b594e0feSJohn Baldwin 
128963692187SIan Dowse     start = partp->dp_start;
129063692187SIan Dowse     size = partp->dp_size;
129163692187SIan Dowse     max_end = start + size;
129263692187SIan Dowse     /* Only allow a zero size if the partition is being marked unused. */
129363692187SIan Dowse     if (size == 0) {
129463692187SIan Dowse 	if (start == 0 && partp->dp_typ == 0)
129563692187SIan Dowse 	    return (1);
129663692187SIan Dowse 	warnx("ERROR: size of partition is zero");
129763692187SIan Dowse 	return (0);
129863692187SIan Dowse     }
129963692187SIan Dowse     /* Return if no adjustment is necessary. */
130063692187SIan Dowse     if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
130163692187SIan Dowse 	return (1);
130263692187SIan Dowse 
130363692187SIan Dowse     if (start % dos_sectors != 0)
130463692187SIan Dowse 	warnx("WARNING: partition does not start on a head boundary");
130563692187SIan Dowse     if ((start  +size) % dos_sectors != 0)
130663692187SIan Dowse 	warnx("WARNING: partition does not end on a cylinder boundary");
130763692187SIan Dowse     warnx("WARNING: this may confuse the BIOS or some operating systems");
130863692187SIan Dowse     if (!ok("Correct this automatically?"))
130963692187SIan Dowse 	return (1);
1310b594e0feSJohn Baldwin 
1311b594e0feSJohn Baldwin     /*
1312d64ada50SJens Schweikhardt      * Adjust start upwards, if necessary, to fall on a head boundary.
1313b594e0feSJohn Baldwin      */
131463692187SIan Dowse     if (start % dos_sectors != 0) {
131563692187SIan Dowse 	prev_head_boundary = start / dos_sectors * dos_sectors;
1316b594e0feSJohn Baldwin 	if (max_end < dos_sectors ||
131763692187SIan Dowse 	    prev_head_boundary >= max_end - dos_sectors) {
1318b594e0feSJohn Baldwin 	    /*
1319b594e0feSJohn Baldwin 	     * Can't go past end of partition
1320b594e0feSJohn Baldwin 	     */
1321b594e0feSJohn Baldwin 	    warnx(
1322b594e0feSJohn Baldwin     "ERROR: unable to adjust start of partition to fall on a head boundary");
1323b594e0feSJohn Baldwin 	    return (0);
1324b594e0feSJohn Baldwin         }
132563692187SIan Dowse 	start = prev_head_boundary + dos_sectors;
1326b594e0feSJohn Baldwin     }
1327b594e0feSJohn Baldwin 
1328b594e0feSJohn Baldwin     /*
1329b594e0feSJohn Baldwin      * Adjust size downwards, if necessary, to fall on a cylinder
1330b594e0feSJohn Baldwin      * boundary.
1331b594e0feSJohn Baldwin      */
133263692187SIan Dowse     prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
133363692187SIan Dowse     if (prev_cyl_boundary > start)
133463692187SIan Dowse 	size = prev_cyl_boundary - start;
133563692187SIan Dowse     else {
1336b594e0feSJohn Baldwin 	warnx("ERROR: could not adjust partition to start on a head boundary\n\
1337b594e0feSJohn Baldwin     and end on a cylinder boundary.");
1338b594e0feSJohn Baldwin 	return (0);
1339b594e0feSJohn Baldwin     }
134063692187SIan Dowse 
134163692187SIan Dowse     /* Finally, commit any changes to partp and return. */
134263692187SIan Dowse     if (start != partp->dp_start) {
134363692187SIan Dowse 	warnx("WARNING: adjusting start offset of partition to %u",
134463692187SIan Dowse 	    (u_int)start);
134563692187SIan Dowse 	partp->dp_start = start;
1346b594e0feSJohn Baldwin     }
134763692187SIan Dowse     if (size != partp->dp_size) {
134863692187SIan Dowse 	warnx("WARNING: adjusting size of partition to %u", (u_int)size);
134963692187SIan Dowse 	partp->dp_size = size;
1350b594e0feSJohn Baldwin     }
1351b594e0feSJohn Baldwin 
1352b594e0feSJohn Baldwin     return (1);
1353b594e0feSJohn Baldwin }
1354df77f711SJoerg Wunsch 
1355df77f711SJoerg Wunsch /*
1356df77f711SJoerg Wunsch  * Try figuring out the root device's canonical disk name.
1357df77f711SJoerg Wunsch  * The following choices are considered:
1358df77f711SJoerg Wunsch  *   /dev/ad0s1a     => /dev/ad0
1359df77f711SJoerg Wunsch  *   /dev/da0a       => /dev/da0
1360df77f711SJoerg Wunsch  *   /dev/vinum/root => /dev/vinum/root
1361df77f711SJoerg Wunsch  */
1362df77f711SJoerg Wunsch static char *
1363df77f711SJoerg Wunsch get_rootdisk(void)
1364df77f711SJoerg Wunsch {
1365df77f711SJoerg Wunsch 	struct statfs rootfs;
1366df77f711SJoerg Wunsch 	regex_t re;
1367df77f711SJoerg Wunsch #define NMATCHES 2
1368df77f711SJoerg Wunsch 	regmatch_t rm[NMATCHES];
1369df77f711SJoerg Wunsch 	char *s;
1370df77f711SJoerg Wunsch 	int rv;
1371df77f711SJoerg Wunsch 
1372df77f711SJoerg Wunsch 	if (statfs("/", &rootfs) == -1)
1373df77f711SJoerg Wunsch 		err(1, "statfs(\"/\")");
1374df77f711SJoerg Wunsch 
1375a8ad364aSPoul-Henning Kamp 	if ((rv = regcomp(&re, "^(/dev/[a-z]+[0-9]+)([sp][0-9]+)?[a-h]?$",
1376df77f711SJoerg Wunsch 		    REG_EXTENDED)) != 0)
1377df77f711SJoerg Wunsch 		errx(1, "regcomp() failed (%d)", rv);
1378df77f711SJoerg Wunsch 	if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1379df77f711SJoerg Wunsch 		errx(1,
1380df77f711SJoerg Wunsch "mounted root fs resource doesn't match expectations (regexec returned %d)",
1381df77f711SJoerg Wunsch 		    rv);
1382df77f711SJoerg Wunsch 	if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1383df77f711SJoerg Wunsch 		errx(1, "out of memory");
1384df77f711SJoerg Wunsch 	memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1385df77f711SJoerg Wunsch 	    rm[1].rm_eo - rm[1].rm_so);
1386df77f711SJoerg Wunsch 	s[rm[1].rm_eo - rm[1].rm_so] = 0;
1387df77f711SJoerg Wunsch 
1388df77f711SJoerg Wunsch 	return s;
1389df77f711SJoerg Wunsch }
1390