xref: /freebsd/sbin/fdisk/fdisk.c (revision f10f160d82479f0f47e4af7f9a378111b09b5eeb)
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[] =
29f10f160dSDoug White 	"$Id: fdisk.c,v 1.30 1999/08/23 11:06:19 phk Exp $";
30d98b1668SPhilippe Charnier #endif /* not lint */
31d98b1668SPhilippe Charnier 
325b81b6b3SRodney W. Grimes #include <sys/disklabel.h>
335b81b6b3SRodney W. Grimes #include <sys/stat.h>
34d98b1668SPhilippe Charnier #include <ctype.h>
355b81b6b3SRodney W. Grimes #include <fcntl.h>
36d98b1668SPhilippe Charnier #include <err.h>
37d98b1668SPhilippe Charnier #include <errno.h>
38d98b1668SPhilippe Charnier #include <stdio.h>
39d98b1668SPhilippe Charnier #include <stdlib.h>
40d98b1668SPhilippe Charnier #include <string.h>
414be1e61bSAlexander Langer #include <unistd.h>
425b81b6b3SRodney W. Grimes 
435b81b6b3SRodney W. Grimes int iotest;
445b81b6b3SRodney W. Grimes 
455b81b6b3SRodney W. Grimes #define LBUF 100
465b81b6b3SRodney W. Grimes static char lbuf[LBUF];
475b81b6b3SRodney W. Grimes 
485b81b6b3SRodney W. Grimes /*
495b81b6b3SRodney W. Grimes  *
505b81b6b3SRodney W. Grimes  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
515b81b6b3SRodney W. Grimes  *
525b81b6b3SRodney W. Grimes  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
535b81b6b3SRodney W. Grimes  *	Copyright (c) 1989	Robert. V. Baron
545b81b6b3SRodney W. Grimes  *	Created.
555b81b6b3SRodney W. Grimes  */
565b81b6b3SRodney W. Grimes 
575b81b6b3SRodney W. Grimes #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
585b81b6b3SRodney W. Grimes #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
595b81b6b3SRodney W. Grimes #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
605b81b6b3SRodney W. Grimes 
615b81b6b3SRodney W. Grimes #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
625b81b6b3SRodney W. Grimes 
637cb29d33SSøren Schmidt #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
647cb29d33SSøren Schmidt #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
657cb29d33SSøren Schmidt int secsize = 0;		/* the sensed sector size */
665b81b6b3SRodney W. Grimes 
67e3038c6eSJoerg Wunsch const char *disk;
68e3038c6eSJoerg Wunsch const char *disks[] =
69e3038c6eSJoerg Wunsch {
70698b4defSJoerg Wunsch   "/dev/rwd0", "/dev/rda0", "/dev/rod0", 0
71e3038c6eSJoerg Wunsch };
72e3038c6eSJoerg Wunsch 
735b81b6b3SRodney W. Grimes struct disklabel disklabel;		/* disk parameters */
745b81b6b3SRodney W. Grimes 
755b81b6b3SRodney W. Grimes int cyls, sectors, heads, cylsecs, disksecs;
765b81b6b3SRodney W. Grimes 
775b81b6b3SRodney W. Grimes struct mboot
785b81b6b3SRodney W. Grimes {
79d98b1668SPhilippe Charnier 	unsigned char padding[2]; /* force the longs to be long aligned */
805b81b6b3SRodney W. Grimes 	unsigned char bootinst[DOSPARTOFF];
815b81b6b3SRodney W. Grimes 	struct	dos_partition parts[4];
825b81b6b3SRodney W. Grimes 	unsigned short int	signature;
837cb29d33SSøren Schmidt 	/* room to read in MBRs that are bigger then DEV_BSIZE */
847cb29d33SSøren Schmidt 	unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
855b81b6b3SRodney W. Grimes };
865b81b6b3SRodney W. Grimes struct mboot mboot;
875b81b6b3SRodney W. Grimes 
885b81b6b3SRodney W. Grimes #define ACTIVE 0x80
895b81b6b3SRodney W. Grimes #define BOOT_MAGIC 0xAA55
905b81b6b3SRodney W. Grimes 
915b81b6b3SRodney W. Grimes int dos_cyls;
925b81b6b3SRodney W. Grimes int dos_heads;
935b81b6b3SRodney W. Grimes int dos_sectors;
945b81b6b3SRodney W. Grimes int dos_cylsecs;
955b81b6b3SRodney W. Grimes 
965b81b6b3SRodney W. Grimes #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
975b81b6b3SRodney W. Grimes #define DOSCYL(c)	(c & 0xff)
985b81b6b3SRodney W. Grimes static int partition = -1;
995b81b6b3SRodney W. Grimes 
1005b81b6b3SRodney W. Grimes 
101f46af505SJordan K. Hubbard #define MAX_ARGS	10
102f46af505SJordan K. Hubbard 
103f46af505SJordan K. Hubbard static int	current_line_number;
104f46af505SJordan K. Hubbard 
105f46af505SJordan K. Hubbard static int	geom_processed = 0;
106f46af505SJordan K. Hubbard static int	part_processed = 0;
107f46af505SJordan K. Hubbard static int	active_processed = 0;
108f46af505SJordan K. Hubbard 
109f46af505SJordan K. Hubbard 
110f46af505SJordan K. Hubbard typedef struct cmd {
111f46af505SJordan K. Hubbard     char		cmd;
112f46af505SJordan K. Hubbard     int			n_args;
113f46af505SJordan K. Hubbard     struct arg {
114f46af505SJordan K. Hubbard 	char	argtype;
115f46af505SJordan K. Hubbard 	int	arg_val;
116f46af505SJordan K. Hubbard     }			args[MAX_ARGS];
117f46af505SJordan K. Hubbard } CMD;
118f46af505SJordan K. Hubbard 
119f46af505SJordan K. Hubbard 
12026721a89SRobert Nordier static int B_flag  = 0;		/* replace boot code */
1215b81b6b3SRodney W. Grimes static int a_flag  = 0;		/* set active partition */
12226721a89SRobert Nordier static char *b_flag = NULL;	/* path to boot code */
123e6fb3ddeSPoul-Henning Kamp static int e_flag  = 0;		/* use entire disk for FreeBSD */
1245b81b6b3SRodney W. Grimes static int i_flag  = 0;		/* replace partition data */
1255b81b6b3SRodney W. Grimes static int u_flag  = 0;		/* update partition data */
126f46af505SJordan K. Hubbard static int t_flag  = 0;		/* test only, if f_flag is given */
127f46af505SJordan K. Hubbard static char *f_flag = NULL;	/* Read config info from file */
128f46af505SJordan K. Hubbard static int v_flag  = 0;		/* Be verbose */
1295b81b6b3SRodney W. Grimes 
1305b81b6b3SRodney W. Grimes struct part_type
1315b81b6b3SRodney W. Grimes {
1325b81b6b3SRodney W. Grimes  unsigned char type;
1335b81b6b3SRodney W. Grimes  char *name;
1345b81b6b3SRodney W. Grimes }part_types[] =
1355b81b6b3SRodney W. Grimes {
1365b81b6b3SRodney W. Grimes 	 {0x00, "unused"}
1375b81b6b3SRodney W. Grimes 	,{0x01, "Primary DOS with 12 bit FAT"}
1385b81b6b3SRodney W. Grimes 	,{0x02, "XENIX / filesystem"}
1395b81b6b3SRodney W. Grimes 	,{0x03, "XENIX /usr filesystem"}
14026555b64SAndrey A. Chernov 	,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
1415b81b6b3SRodney W. Grimes 	,{0x05, "Extended DOS"}
1425b81b6b3SRodney W. Grimes 	,{0x06, "Primary 'big' DOS (> 32MB)"}
143680426beSDavid E. O'Brien 	,{0x07, "OS/2 HPFS, NTFS, QNX or Advanced UNIX"}
1445b81b6b3SRodney W. Grimes 	,{0x08, "AIX filesystem"}
1455b81b6b3SRodney W. Grimes 	,{0x09, "AIX boot partition or Coherent"}
1465b81b6b3SRodney W. Grimes 	,{0x0A, "OS/2 Boot Manager or OPUS"}
14726555b64SAndrey A. Chernov 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
14826555b64SAndrey A. Chernov 	,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
14926555b64SAndrey A. Chernov 	,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
15026555b64SAndrey A. Chernov 	,{0x0F, "Extended DOS, LBA"}
1515b81b6b3SRodney W. Grimes 	,{0x10, "OPUS"}
1525b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
1535b81b6b3SRodney W. Grimes 	,{0x50, "DM"}
1545b81b6b3SRodney W. Grimes 	,{0x51, "DM"}
1555b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
1565b81b6b3SRodney W. Grimes 	,{0x56, "GB"}
1575b81b6b3SRodney W. Grimes 	,{0x61, "Speed"}
1585b81b6b3SRodney W. Grimes 	,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
1595b81b6b3SRodney W. Grimes 	,{0x64, "Novell Netware 2.xx"}
1605b81b6b3SRodney W. Grimes 	,{0x65, "Novell Netware 3.xx"}
1615b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
1625b81b6b3SRodney W. Grimes 	,{0x80, "Minix 1.1 ... 1.4a"}
1635b81b6b3SRodney W. Grimes 	,{0x81, "Minix 1.4b ... 1.5.10"}
16463ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
16549f7c177SJordan K. Hubbard 	,{0x83, "Linux filesystem"}
1665b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba filesystem"}
1675b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
168d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
16949f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
170e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
1715f0c9424SGary Palmer 	,{0xA7, "NEXTSTEP"}
172ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1735b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 filesystem"}
1745b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
1755b81b6b3SRodney W. Grimes 	,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
1765b81b6b3SRodney W. Grimes 	,{0xE1, "Speed"}
1775b81b6b3SRodney W. Grimes 	,{0xE3, "Speed"}
1785b81b6b3SRodney W. Grimes 	,{0xE4, "Speed"}
1795b81b6b3SRodney W. Grimes 	,{0xF1, "Speed"}
1805b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
1815b81b6b3SRodney W. Grimes 	,{0xF4, "Speed"}
1825b81b6b3SRodney W. Grimes 	,{0xFF, "BBT (Bad Blocks Table)"}
1835b81b6b3SRodney W. Grimes };
1845b81b6b3SRodney W. Grimes 
1854be1e61bSAlexander Langer static void print_s0(int which);
1864be1e61bSAlexander Langer static void print_part(int i);
1874be1e61bSAlexander Langer static void init_sector0(unsigned long start);
188f46af505SJordan K. Hubbard static void init_boot(void);
1894be1e61bSAlexander Langer static void change_part(int i);
1904be1e61bSAlexander Langer static void print_params();
1914be1e61bSAlexander Langer static void change_active(int which);
19212d910e9SRobert Nordier static void change_code();
1934be1e61bSAlexander Langer static void get_params_to_use();
194e2975440SBruce Evans static void dos(int sec, int size, unsigned char *c, unsigned char *s,
195e2975440SBruce Evans 		unsigned char *h);
1964be1e61bSAlexander Langer static int open_disk(int u_flag);
1974be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
1984be1e61bSAlexander Langer static ssize_t write_disk(off_t sector, void *buf);
1994be1e61bSAlexander Langer static int get_params();
2004be1e61bSAlexander Langer static int read_s0();
2014be1e61bSAlexander Langer static int write_s0();
2024be1e61bSAlexander Langer static int ok(char *str);
2034be1e61bSAlexander Langer static int decimal(char *str, int *num, int deflt);
2044be1e61bSAlexander Langer static char *get_type(int type);
205f46af505SJordan K. Hubbard static int read_config(char *config_file);
206f46af505SJordan K. Hubbard static void reset_boot(void);
207d98b1668SPhilippe Charnier static void usage(void);
2084be1e61bSAlexander Langer #if 0
2094be1e61bSAlexander Langer static int hex(char *str, int *num, int deflt);
2104be1e61bSAlexander Langer static int string(char *str, char **ans);
2114be1e61bSAlexander Langer #endif
2125b81b6b3SRodney W. Grimes 
2134be1e61bSAlexander Langer 
2144be1e61bSAlexander Langer int
2154be1e61bSAlexander Langer main(int argc, char *argv[])
2165b81b6b3SRodney W. Grimes {
21726721a89SRobert Nordier 	int	c, i;
2185b81b6b3SRodney W. Grimes 
219e6fb3ddeSPoul-Henning Kamp 	while ((c = getopt(argc, argv, "Bab:ef:ituv1234")) != -1)
22026721a89SRobert Nordier 		switch (c) {
22126721a89SRobert Nordier 		case 'B':
22226721a89SRobert Nordier 			B_flag = 1;
2234ddd60b9SBrian Somers 			break;
2245b81b6b3SRodney W. Grimes 		case 'a':
2255b81b6b3SRodney W. Grimes 			a_flag = 1;
2265b81b6b3SRodney W. Grimes 			break;
22712d910e9SRobert Nordier 		case 'b':
22826721a89SRobert Nordier 			b_flag = optarg;
22912d910e9SRobert Nordier 			break;
230e6fb3ddeSPoul-Henning Kamp 		case 'e':
231e6fb3ddeSPoul-Henning Kamp 			e_flag = 1;
232e6fb3ddeSPoul-Henning Kamp 			break;
233f46af505SJordan K. Hubbard 		case 'f':
23426721a89SRobert Nordier 			f_flag = optarg;
235f46af505SJordan K. Hubbard 			break;
2365b81b6b3SRodney W. Grimes 		case 'i':
2375b81b6b3SRodney W. Grimes 			i_flag = 1;
2385b81b6b3SRodney W. Grimes 			break;
239f46af505SJordan K. Hubbard 		case 't':
240f46af505SJordan K. Hubbard 			t_flag = 1;
24126721a89SRobert Nordier 			break;
24226721a89SRobert Nordier 		case 'u':
24326721a89SRobert Nordier 			u_flag = 1;
24426721a89SRobert Nordier 			break;
245f46af505SJordan K. Hubbard 		case 'v':
246f46af505SJordan K. Hubbard 			v_flag = 1;
247f46af505SJordan K. Hubbard 			break;
24826721a89SRobert Nordier 		case '1':
24926721a89SRobert Nordier 		case '2':
25026721a89SRobert Nordier 		case '3':
25126721a89SRobert Nordier 		case '4':
25226721a89SRobert Nordier 			partition = c - '0';
25326721a89SRobert Nordier 			break;
2545b81b6b3SRodney W. Grimes 		default:
255d98b1668SPhilippe Charnier 			usage();
2565b81b6b3SRodney W. Grimes 		}
25726721a89SRobert Nordier 	if (f_flag || i_flag)
25826721a89SRobert Nordier 		u_flag = 1;
25926721a89SRobert Nordier 	if (t_flag)
26026721a89SRobert Nordier 		v_flag = 1;
26126721a89SRobert Nordier 	argc -= optind;
26226721a89SRobert Nordier 	argv += optind;
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes 	if (argc > 0)
265e3038c6eSJoerg Wunsch 	{
266e3038c6eSJoerg Wunsch 		static char realname[12];
267e3038c6eSJoerg Wunsch 
268e3038c6eSJoerg Wunsch 		if(strncmp(argv[0], "/dev", 4) == 0)
2695b81b6b3SRodney W. Grimes 			disk = argv[0];
270e3038c6eSJoerg Wunsch 		else
271e3038c6eSJoerg Wunsch 		{
272e3038c6eSJoerg Wunsch 			snprintf(realname, 12, "/dev/r%s", argv[0]);
273e3038c6eSJoerg Wunsch 			disk = realname;
274e3038c6eSJoerg Wunsch 		}
2755b81b6b3SRodney W. Grimes 
2765b81b6b3SRodney W. Grimes 		if (open_disk(u_flag) < 0)
277d98b1668SPhilippe Charnier 			err(1, "cannot open disk %s", disk);
278e3038c6eSJoerg Wunsch 	}
279e3038c6eSJoerg Wunsch 	else
280e3038c6eSJoerg Wunsch 	{
28126721a89SRobert Nordier 		int rv = 0;
282e3038c6eSJoerg Wunsch 
283e3038c6eSJoerg Wunsch 		for(i = 0; disks[i]; i++)
284e3038c6eSJoerg Wunsch 		{
285e3038c6eSJoerg Wunsch 			disk = disks[i];
286e3038c6eSJoerg Wunsch 			rv = open_disk(u_flag);
287e3038c6eSJoerg Wunsch 			if(rv != -2) break;
288e3038c6eSJoerg Wunsch 		}
289e3038c6eSJoerg Wunsch 		if(rv < 0)
290d98b1668SPhilippe Charnier 			err(1, "cannot open any disk");
291e3038c6eSJoerg Wunsch 	}
2925b81b6b3SRodney W. Grimes 
2935b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
294f46af505SJordan K. Hubbard 
295e6fb3ddeSPoul-Henning Kamp 	if (e_flag)
296e6fb3ddeSPoul-Henning Kamp 	{
297e6fb3ddeSPoul-Henning Kamp 		struct dos_partition *partp;
298e6fb3ddeSPoul-Henning Kamp 
299e6fb3ddeSPoul-Henning Kamp 		read_s0();
300e6fb3ddeSPoul-Henning Kamp 		reset_boot();
301e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
302e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
303e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
304e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
305e6fb3ddeSPoul-Henning Kamp 		partp->dp_size = disksecs - dos_sectors;
306e6fb3ddeSPoul-Henning Kamp 
307e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start, partp->dp_size,
308e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
309e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
310e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
311e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
312e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
313e6fb3ddeSPoul-Henning Kamp 		write_s0();
314e6fb3ddeSPoul-Henning Kamp 		exit(0);
315e6fb3ddeSPoul-Henning Kamp 	}
316f46af505SJordan K. Hubbard 	if (f_flag)
317f46af505SJordan K. Hubbard 	{
318f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
319f46af505SJordan K. Hubbard 	    {
320f46af505SJordan K. Hubbard 		reset_boot();
321f46af505SJordan K. Hubbard 	    }
322f46af505SJordan K. Hubbard 
323f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
324f46af505SJordan K. Hubbard 	    {
325f46af505SJordan K. Hubbard 		exit(1);
326f46af505SJordan K. Hubbard 	    }
327f46af505SJordan K. Hubbard 	    if (v_flag)
328f46af505SJordan K. Hubbard 	    {
329f46af505SJordan K. Hubbard 		print_s0(-1);
330f46af505SJordan K. Hubbard 	    }
331f46af505SJordan K. Hubbard 	    if (!t_flag)
332f46af505SJordan K. Hubbard 	    {
333f46af505SJordan K. Hubbard 		write_s0();
334f46af505SJordan K. Hubbard 	    }
335f46af505SJordan K. Hubbard 	}
336f46af505SJordan K. Hubbard 	else
337f46af505SJordan K. Hubbard 	{
3385b81b6b3SRodney W. Grimes 	    if(u_flag)
3395b81b6b3SRodney W. Grimes 	    {
3405b81b6b3SRodney W. Grimes 		get_params_to_use();
3415b81b6b3SRodney W. Grimes 	    }
3425b81b6b3SRodney W. Grimes 	    else
3435b81b6b3SRodney W. Grimes 	    {
3445b81b6b3SRodney W. Grimes 		print_params();
3455b81b6b3SRodney W. Grimes 	    }
3465b81b6b3SRodney W. Grimes 
3475b81b6b3SRodney W. Grimes 	    if (read_s0())
3485b81b6b3SRodney W. Grimes 		init_sector0(1);
3495b81b6b3SRodney W. Grimes 
3507cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3515b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3525b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3535b81b6b3SRodney W. Grimes 	    if (partition == -1)
3544ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3555b81b6b3SRodney W. Grimes 		    change_part(i);
3565b81b6b3SRodney W. Grimes 	    else
3575b81b6b3SRodney W. Grimes 		change_part(partition);
3585b81b6b3SRodney W. Grimes 
3595b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3605b81b6b3SRodney W. Grimes 		change_active(partition);
3615b81b6b3SRodney W. Grimes 
36226721a89SRobert Nordier 	    if (B_flag)
36312d910e9SRobert Nordier 		change_code();
36412d910e9SRobert Nordier 
36526721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
366f46af505SJordan K. Hubbard 		if (!t_flag)
367f46af505SJordan K. Hubbard 		{
3685b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
3695b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
370f46af505SJordan K. Hubbard 		}
3715b81b6b3SRodney W. Grimes 		print_s0(-1);
372f46af505SJordan K. Hubbard 		if (!t_flag)
373f46af505SJordan K. Hubbard 		{
3745b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
3755b81b6b3SRodney W. Grimes 			write_s0();
3765b81b6b3SRodney W. Grimes 		}
377f46af505SJordan K. Hubbard 		else
378f46af505SJordan K. Hubbard 		{
379f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
380f46af505SJordan K. Hubbard 		}
381f46af505SJordan K. Hubbard 	    }
382f46af505SJordan K. Hubbard 	}
3835b81b6b3SRodney W. Grimes 
3845b81b6b3SRodney W. Grimes 	exit(0);
385d98b1668SPhilippe Charnier }
3865b81b6b3SRodney W. Grimes 
387d98b1668SPhilippe Charnier static void
388d98b1668SPhilippe Charnier usage()
389d98b1668SPhilippe Charnier {
39026721a89SRobert Nordier 	fprintf(stderr, "%s%s",
391f10f160dSDoug White 		"usage: fdisk [-Baeitu] [-b bootcode] [-1234] [disk]\n",
39226721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
393d98b1668SPhilippe Charnier         exit(1);
3945b81b6b3SRodney W. Grimes }
3955b81b6b3SRodney W. Grimes 
3964be1e61bSAlexander Langer static void
3974be1e61bSAlexander Langer print_s0(int which)
3985b81b6b3SRodney W. Grimes {
3995b81b6b3SRodney W. Grimes int	i;
4005b81b6b3SRodney W. Grimes 
4015b81b6b3SRodney W. Grimes 	print_params();
4025b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4035b81b6b3SRodney W. Grimes 	if (which == -1)
4044ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4055b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4065b81b6b3SRodney W. Grimes 	else
4075b81b6b3SRodney W. Grimes 		print_part(which);
4085b81b6b3SRodney W. Grimes }
4095b81b6b3SRodney W. Grimes 
4105b81b6b3SRodney W. Grimes static struct dos_partition mtpart = { 0 };
4115b81b6b3SRodney W. Grimes 
4124be1e61bSAlexander Langer static void
4134be1e61bSAlexander Langer print_part(int i)
4145b81b6b3SRodney W. Grimes {
415637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
416637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4175b81b6b3SRodney W. Grimes 
4184ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4195b81b6b3SRodney W. Grimes 
4205b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4215b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4225b81b6b3SRodney W. Grimes 		return;
4235b81b6b3SRodney W. Grimes 	}
424637fe2f7SJustin T. Gibbs 	/*
425637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
426637fe2f7SJustin T. Gibbs 	 */
427637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
428637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
429637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
4305b81b6b3SRodney W. Grimes 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
431ba198492SBruce Evans 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
432ba198492SBruce Evans 		(u_long)partp->dp_start,
433ba198492SBruce Evans 		(u_long)partp->dp_size,
434637fe2f7SJustin T. Gibbs 		part_mb,
435680426beSDavid E. O'Brien 		partp->dp_flag,
436680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4375b81b6b3SRodney W. Grimes 	printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
4385b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4395b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_ssect)
4405b81b6b3SRodney W. Grimes 		,partp->dp_shd
4415b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4425b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_esect)
4435b81b6b3SRodney W. Grimes 		,partp->dp_ehd);
4445b81b6b3SRodney W. Grimes }
4455b81b6b3SRodney W. Grimes 
446f46af505SJordan K. Hubbard 
447f46af505SJordan K. Hubbard static void
448f46af505SJordan K. Hubbard init_boot(void)
449f46af505SJordan K. Hubbard {
45026721a89SRobert Nordier 	const char *fname;
45126721a89SRobert Nordier 	int fd;
45226721a89SRobert Nordier 
45326721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
45426721a89SRobert Nordier 	if ((fd = open(fname, O_RDONLY)) == -1 ||
45526721a89SRobert Nordier 	    read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
45626721a89SRobert Nordier 	    close(fd))
45726721a89SRobert Nordier 		err(1, "%s", fname);
458f46af505SJordan K. Hubbard 	mboot.signature = BOOT_MAGIC;
459f46af505SJordan K. Hubbard }
460f46af505SJordan K. Hubbard 
461f46af505SJordan K. Hubbard 
4624be1e61bSAlexander Langer static void
4634be1e61bSAlexander Langer init_sector0(unsigned long start)
4645b81b6b3SRodney W. Grimes {
4655b81b6b3SRodney W. Grimes struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
4664be1e61bSAlexander Langer unsigned long size = disksecs - start;
4675b81b6b3SRodney W. Grimes 
468f46af505SJordan K. Hubbard 	init_boot();
4695b81b6b3SRodney W. Grimes 
4705b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
4715b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
4725b81b6b3SRodney W. Grimes 	partp->dp_start = start;
4735b81b6b3SRodney W. Grimes 	partp->dp_size = size;
4745b81b6b3SRodney W. Grimes 
475e2975440SBruce Evans 	dos(partp->dp_start, partp->dp_size,
476e2975440SBruce Evans 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
477e2975440SBruce Evans 	dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
478e2975440SBruce Evans 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
4795b81b6b3SRodney W. Grimes }
4805b81b6b3SRodney W. Grimes 
4814be1e61bSAlexander Langer static void
4824be1e61bSAlexander Langer change_part(int i)
4835b81b6b3SRodney W. Grimes {
4844ddd60b9SBrian Somers struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4855b81b6b3SRodney W. Grimes 
4865b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
4875b81b6b3SRodney W. Grimes     print_part(i);
4885b81b6b3SRodney W. Grimes 
4895b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
4905b81b6b3SRodney W. Grimes 	int tmp;
4915b81b6b3SRodney W. Grimes 
4925b81b6b3SRodney W. Grimes 	if (i_flag) {
4935b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
4944ddd60b9SBrian Somers 		if (i == 4) {
4955b81b6b3SRodney W. Grimes 			init_sector0(1);
4964ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
4975b81b6b3SRodney W. Grimes 			print_part(i);
4985b81b6b3SRodney W. Grimes 		}
4995b81b6b3SRodney W. Grimes 	}
5005b81b6b3SRodney W. Grimes 
5015b81b6b3SRodney W. Grimes 	do {
502680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5035b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5045b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
5055b81b6b3SRodney W. Grimes 
5064b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5075b81b6b3SRodney W. Grimes 		{
5085b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5095b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5105b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5115b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5125b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5135b81b6b3SRodney W. Grimes 			Decimal("beginning head", thd, tmp);
5145b81b6b3SRodney W. Grimes 			Decimal("beginning sector", tsec, tmp);
5155b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5165b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5175b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5185b81b6b3SRodney W. Grimes 
5195b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5205b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5215b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5225b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5235b81b6b3SRodney W. Grimes 			Decimal("ending head", thd, tmp);
5245b81b6b3SRodney W. Grimes 			Decimal("ending sector", tsec, tmp);
5255b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5265b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5275b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
5285b81b6b3SRodney W. Grimes 		} else {
529e2975440SBruce Evans 			dos(partp->dp_start, partp->dp_size,
5305b81b6b3SRodney W. Grimes 			    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
531e2975440SBruce Evans 			dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
5325b81b6b3SRodney W. Grimes 			    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5335b81b6b3SRodney W. Grimes 		}
5345b81b6b3SRodney W. Grimes 
5355b81b6b3SRodney W. Grimes 		print_part(i);
5365b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
5375b81b6b3SRodney W. Grimes     }
5385b81b6b3SRodney W. Grimes }
5395b81b6b3SRodney W. Grimes 
5404be1e61bSAlexander Langer static void
5415b81b6b3SRodney W. Grimes print_params()
5425b81b6b3SRodney W. Grimes {
5435b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
5445b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5455b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
5465b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
5475b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
5485b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
5495b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5505b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
5515b81b6b3SRodney W. Grimes }
5525b81b6b3SRodney W. Grimes 
5534be1e61bSAlexander Langer static void
5544be1e61bSAlexander Langer change_active(int which)
5555b81b6b3SRodney W. Grimes {
5565b81b6b3SRodney W. Grimes int i;
5574ddd60b9SBrian Somers int active = 4, tmp;
5585b81b6b3SRodney W. Grimes struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
5595b81b6b3SRodney W. Grimes 
5605b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
5615b81b6b3SRodney W. Grimes 		active = which;
5620b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
5630b461cd7SBruce Evans 		return;
564680426beSDavid E. O'Brien setactive:
565680426beSDavid E. O'Brien 	active = 4;
566680426beSDavid E. O'Brien 	do {
5675b81b6b3SRodney W. Grimes 		Decimal("active partition", active, tmp);
568680426beSDavid E. O'Brien 		if (active < 1 || 4 < active) {
569680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
570680426beSDavid E. O'Brien 					"  Try again.\n");
571680426beSDavid E. O'Brien 			goto setactive;
572680426beSDavid E. O'Brien 		}
573680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
5745b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
5755b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
5764ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
5774ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
5785b81b6b3SRodney W. Grimes }
5795b81b6b3SRodney W. Grimes 
58012d910e9SRobert Nordier static void
58112d910e9SRobert Nordier change_code()
58212d910e9SRobert Nordier {
58312d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
58412d910e9SRobert Nordier 		init_boot();
58512d910e9SRobert Nordier 
58612d910e9SRobert Nordier }
58712d910e9SRobert Nordier 
5884be1e61bSAlexander Langer void
5895b81b6b3SRodney W. Grimes get_params_to_use()
5905b81b6b3SRodney W. Grimes {
5915b81b6b3SRodney W. Grimes 	int	tmp;
5925b81b6b3SRodney W. Grimes 	print_params();
5935b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
5945b81b6b3SRodney W. Grimes 	{
5955b81b6b3SRodney W. Grimes 		do
5965b81b6b3SRodney W. Grimes 		{
5975b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
5985b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
5995b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6005b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6015b81b6b3SRodney W. Grimes 			print_params();
6025b81b6b3SRodney W. Grimes 		}
6035b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6045b81b6b3SRodney W. Grimes 	}
6055b81b6b3SRodney W. Grimes }
6065b81b6b3SRodney W. Grimes 
607f46af505SJordan K. Hubbard 
6085b81b6b3SRodney W. Grimes /***********************************************\
6095b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6105b81b6b3SRodney W. Grimes \***********************************************/
6114be1e61bSAlexander Langer static void
612e2975440SBruce Evans dos(sec, size, c, s, h)
613e2975440SBruce Evans int sec, size;
6145b81b6b3SRodney W. Grimes unsigned char *c, *s, *h;
6155b81b6b3SRodney W. Grimes {
6165b81b6b3SRodney W. Grimes int cy;
6175b81b6b3SRodney W. Grimes int hd;
6185b81b6b3SRodney W. Grimes 
619e2975440SBruce Evans 	if (sec == 0 && size == 0) {
6200b461cd7SBruce Evans 		*s = *c = *h = 0;
6210b461cd7SBruce Evans 		return;
6220b461cd7SBruce Evans 	}
6230b461cd7SBruce Evans 
6245b81b6b3SRodney W. Grimes 	cy = sec / ( dos_cylsecs );
6255b81b6b3SRodney W. Grimes 	sec = sec - cy * ( dos_cylsecs );
6265b81b6b3SRodney W. Grimes 
6275b81b6b3SRodney W. Grimes 	hd = sec / dos_sectors;
6285b81b6b3SRodney W. Grimes 	sec = (sec - hd * dos_sectors) + 1;
6295b81b6b3SRodney W. Grimes 
6305b81b6b3SRodney W. Grimes 	*h = hd;
6315b81b6b3SRodney W. Grimes 	*c = cy & 0xff;
6325b81b6b3SRodney W. Grimes 	*s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
6335b81b6b3SRodney W. Grimes }
6345b81b6b3SRodney W. Grimes 
6355b81b6b3SRodney W. Grimes int fd;
6365b81b6b3SRodney W. Grimes 
6375b81b6b3SRodney W. Grimes 	/* Getting device status */
6385b81b6b3SRodney W. Grimes 
6394be1e61bSAlexander Langer static int
6404be1e61bSAlexander Langer open_disk(int u_flag)
6415b81b6b3SRodney W. Grimes {
6425b81b6b3SRodney W. Grimes struct stat 	st;
6435b81b6b3SRodney W. Grimes 
6445b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
645d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
6465b81b6b3SRodney W. Grimes 		return -1;
647b60eb395SBruce Evans 	}
648b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
649d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
65012d910e9SRobert Nordier 	if ((fd = open(disk,
651e6fb3ddeSPoul-Henning Kamp 	    a_flag || e_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
652e3038c6eSJoerg Wunsch 		if(errno == ENXIO)
653e3038c6eSJoerg Wunsch 			return -2;
654d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
6555b81b6b3SRodney W. Grimes 		return -1;
6565b81b6b3SRodney W. Grimes 	}
6575b81b6b3SRodney W. Grimes 	if (get_params(0) == -1) {
658d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
6595b81b6b3SRodney W. Grimes 		return -1;
6605b81b6b3SRodney W. Grimes 	}
6615b81b6b3SRodney W. Grimes 	return fd;
6625b81b6b3SRodney W. Grimes }
6635b81b6b3SRodney W. Grimes 
6644be1e61bSAlexander Langer static ssize_t
6654be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
6665b81b6b3SRodney W. Grimes {
6675b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
6687cb29d33SSøren Schmidt 	if( secsize == 0 )
6697cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
6707cb29d33SSøren Schmidt 			{
6717cb29d33SSøren Schmidt 			/* try the read */
6727cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
6737cb29d33SSøren Schmidt 			if( size == secsize )
6747cb29d33SSøren Schmidt 				/* it worked so return */
6757cb29d33SSøren Schmidt 				return secsize;
6767cb29d33SSøren Schmidt 			}
6777cb29d33SSøren Schmidt 	else
6787cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
6797cb29d33SSøren Schmidt 
6807cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
6817cb29d33SSøren Schmidt 	return -1;
6825b81b6b3SRodney W. Grimes }
6835b81b6b3SRodney W. Grimes 
6844be1e61bSAlexander Langer static ssize_t
6854be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
6865b81b6b3SRodney W. Grimes {
6875b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
6887cb29d33SSøren Schmidt 	/* write out in the size that the read_disk found worked */
6897cb29d33SSøren Schmidt 	return write(fd, buf, secsize);
6905b81b6b3SRodney W. Grimes }
6915b81b6b3SRodney W. Grimes 
6924be1e61bSAlexander Langer static int
6934be1e61bSAlexander Langer get_params()
6945b81b6b3SRodney W. Grimes {
6955b81b6b3SRodney W. Grimes 
6965b81b6b3SRodney W. Grimes     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
697d98b1668SPhilippe Charnier 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
698b60eb395SBruce Evans 	dos_cyls = cyls = 1;
699b60eb395SBruce Evans 	dos_heads = heads = 1;
700b60eb395SBruce Evans 	dos_sectors = sectors = 1;
701b60eb395SBruce Evans 	dos_cylsecs = cylsecs = heads * sectors;
702b60eb395SBruce Evans 	disksecs = cyls * heads * sectors;
703b60eb395SBruce Evans 	return disksecs;
7045b81b6b3SRodney W. Grimes     }
7055b81b6b3SRodney W. Grimes 
7065b81b6b3SRodney W. Grimes     dos_cyls = cyls = disklabel.d_ncylinders;
7075b81b6b3SRodney W. Grimes     dos_heads = heads = disklabel.d_ntracks;
7085b81b6b3SRodney W. Grimes     dos_sectors = sectors = disklabel.d_nsectors;
7095b81b6b3SRodney W. Grimes     dos_cylsecs = cylsecs = heads * sectors;
7105b81b6b3SRodney W. Grimes     disksecs = cyls * heads * sectors;
7115b81b6b3SRodney W. Grimes 
7125b81b6b3SRodney W. Grimes     return (disksecs);
7135b81b6b3SRodney W. Grimes }
7145b81b6b3SRodney W. Grimes 
7155b81b6b3SRodney W. Grimes 
7164be1e61bSAlexander Langer static int
7175b81b6b3SRodney W. Grimes read_s0()
7185b81b6b3SRodney W. Grimes {
7195b81b6b3SRodney W. Grimes 	if (read_disk(0, (char *) mboot.bootinst) == -1) {
720d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
7215b81b6b3SRodney W. Grimes 		return -1;
7225b81b6b3SRodney W. Grimes 	}
7235b81b6b3SRodney W. Grimes 	if (mboot.signature != BOOT_MAGIC) {
724d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
7255b81b6b3SRodney W. Grimes 		/* So should we initialize things */
7265b81b6b3SRodney W. Grimes 		return -1;
7275b81b6b3SRodney W. Grimes 	}
7285b81b6b3SRodney W. Grimes 	return 0;
7295b81b6b3SRodney W. Grimes }
7305b81b6b3SRodney W. Grimes 
7314be1e61bSAlexander Langer static int
7325b81b6b3SRodney W. Grimes write_s0()
7335b81b6b3SRodney W. Grimes {
7345b81b6b3SRodney W. Grimes 	int	flag;
7355b81b6b3SRodney W. Grimes 	if (iotest) {
7365b81b6b3SRodney W. Grimes 		print_s0(-1);
7375b81b6b3SRodney W. Grimes 		return 0;
7385b81b6b3SRodney W. Grimes 	}
7395b81b6b3SRodney W. Grimes 	/*
7405b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
7415b81b6b3SRodney W. Grimes 	 * disable after writing.
7425b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
7435b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
7445b81b6b3SRodney W. Grimes 	 */
745ba3551dfSJulian Elischer #ifdef NOT_NOW
746e6fb3ddeSPoul-Henning Kamp 	flag = 1;
7475b81b6b3SRodney W. Grimes 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
748d98b1668SPhilippe Charnier 		warn("ioctl DIOCWLABEL");
749ba3551dfSJulian Elischer #endif
7505b81b6b3SRodney W. Grimes 	if (write_disk(0, (char *) mboot.bootinst) == -1) {
751e6fb3ddeSPoul-Henning Kamp 		warn("can't write fdisk partition table");
7525b81b6b3SRodney W. Grimes 		return -1;
753ba3551dfSJulian Elischer #ifdef NOT_NOW
754e6fb3ddeSPoul-Henning Kamp 	flag = 0;
7555b81b6b3SRodney W. Grimes 	(void) ioctl(fd, DIOCWLABEL, &flag);
756ba3551dfSJulian Elischer #endif
7575b81b6b3SRodney W. Grimes 	}
7584be1e61bSAlexander Langer 	return(0);
7595b81b6b3SRodney W. Grimes }
7605b81b6b3SRodney W. Grimes 
7615b81b6b3SRodney W. Grimes 
7624be1e61bSAlexander Langer static int
7635b81b6b3SRodney W. Grimes ok(str)
7645b81b6b3SRodney W. Grimes char *str;
7655b81b6b3SRodney W. Grimes {
7665b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
7675b81b6b3SRodney W. Grimes 	fgets(lbuf, LBUF, stdin);
7685b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
7695b81b6b3SRodney W. Grimes 
7705b81b6b3SRodney W. Grimes 	if (*lbuf &&
7715b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
7725b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
7735b81b6b3SRodney W. Grimes 		return 1;
7745b81b6b3SRodney W. Grimes 	else
7755b81b6b3SRodney W. Grimes 		return 0;
7765b81b6b3SRodney W. Grimes }
7775b81b6b3SRodney W. Grimes 
7784be1e61bSAlexander Langer static int
7794be1e61bSAlexander Langer decimal(char *str, int *num, int deflt)
7805b81b6b3SRodney W. Grimes {
7815b81b6b3SRodney W. Grimes int acc = 0, c;
7825b81b6b3SRodney W. Grimes char *cp;
7835b81b6b3SRodney W. Grimes 
7845b81b6b3SRodney W. Grimes 	while (1) {
7855b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
7865b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
7875b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
7885b81b6b3SRodney W. Grimes 
7895b81b6b3SRodney W. Grimes 		if (!*lbuf)
7905b81b6b3SRodney W. Grimes 			return 0;
7915b81b6b3SRodney W. Grimes 
7925b81b6b3SRodney W. Grimes 		cp = lbuf;
7935b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
7945b81b6b3SRodney W. Grimes 		if (!c)
7955b81b6b3SRodney W. Grimes 			return 0;
7964be1e61bSAlexander Langer 		while ((c = *cp++)) {
7975b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
7985b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
7995b81b6b3SRodney W. Grimes 			else
8005b81b6b3SRodney W. Grimes 				break;
8015b81b6b3SRodney W. Grimes 		}
8025b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8035b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8045b81b6b3SRodney W. Grimes 		if (!c) {
8055b81b6b3SRodney W. Grimes 			*num = acc;
8065b81b6b3SRodney W. Grimes 			return 1;
8075b81b6b3SRodney W. Grimes 		} else
808680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
8095b81b6b3SRodney W. Grimes 				lbuf);
8105b81b6b3SRodney W. Grimes 	}
8115b81b6b3SRodney W. Grimes 
8125b81b6b3SRodney W. Grimes }
8135b81b6b3SRodney W. Grimes 
8144be1e61bSAlexander Langer #if 0
8154be1e61bSAlexander Langer static int
8164be1e61bSAlexander Langer hex(char *str, int *num, int deflt)
8175b81b6b3SRodney W. Grimes {
8185b81b6b3SRodney W. Grimes int acc = 0, c;
8195b81b6b3SRodney W. Grimes char *cp;
8205b81b6b3SRodney W. Grimes 
8215b81b6b3SRodney W. Grimes 	while (1) {
8225b81b6b3SRodney W. Grimes 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
8235b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8245b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8255b81b6b3SRodney W. Grimes 
8265b81b6b3SRodney W. Grimes 		if (!*lbuf)
8275b81b6b3SRodney W. Grimes 			return 0;
8285b81b6b3SRodney W. Grimes 
8295b81b6b3SRodney W. Grimes 		cp = lbuf;
8305b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8315b81b6b3SRodney W. Grimes 		if (!c)
8325b81b6b3SRodney W. Grimes 			return 0;
8334be1e61bSAlexander Langer 		while ((c = *cp++)) {
8345b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8355b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - '0';
8365b81b6b3SRodney W. Grimes 			else if (c <= 'f' && c >= 'a')
8375b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'a' + 10;
8385b81b6b3SRodney W. Grimes 			else if (c <= 'F' && c >= 'A')
8395b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'A' + 10;
8405b81b6b3SRodney W. Grimes 			else
8415b81b6b3SRodney W. Grimes 				break;
8425b81b6b3SRodney W. Grimes 		}
8435b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8445b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8455b81b6b3SRodney W. Grimes 		if (!c) {
8465b81b6b3SRodney W. Grimes 			*num = acc;
8475b81b6b3SRodney W. Grimes 			return 1;
8485b81b6b3SRodney W. Grimes 		} else
849680426beSDavid E. O'Brien 			printf("%s is an invalid hex number.  Try again.\n",
8505b81b6b3SRodney W. Grimes 				lbuf);
8515b81b6b3SRodney W. Grimes 	}
8525b81b6b3SRodney W. Grimes 
8535b81b6b3SRodney W. Grimes }
8545b81b6b3SRodney W. Grimes 
8554be1e61bSAlexander Langer static int
8564be1e61bSAlexander Langer string(char *str, char **ans)
8575b81b6b3SRodney W. Grimes {
8585b81b6b3SRodney W. Grimes int c;
8595b81b6b3SRodney W. Grimes char *cp = lbuf;
8605b81b6b3SRodney W. Grimes 
8615b81b6b3SRodney W. Grimes 	while (1) {
8625b81b6b3SRodney W. Grimes 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
8635b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8645b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8655b81b6b3SRodney W. Grimes 
8665b81b6b3SRodney W. Grimes 		if (!*lbuf)
8675b81b6b3SRodney W. Grimes 			return 0;
8685b81b6b3SRodney W. Grimes 
8695b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8705b81b6b3SRodney W. Grimes 		if (c == '"') {
8715b81b6b3SRodney W. Grimes 			c = *++cp;
8725b81b6b3SRodney W. Grimes 			*ans = cp;
8735b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != '"') cp++;
8745b81b6b3SRodney W. Grimes 		} else {
8755b81b6b3SRodney W. Grimes 			*ans = cp;
8765b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
8775b81b6b3SRodney W. Grimes 		}
8785b81b6b3SRodney W. Grimes 
8795b81b6b3SRodney W. Grimes 		if (c)
8805b81b6b3SRodney W. Grimes 			*cp = 0;
8815b81b6b3SRodney W. Grimes 		return 1;
8825b81b6b3SRodney W. Grimes 	}
8835b81b6b3SRodney W. Grimes }
8844be1e61bSAlexander Langer #endif
8855b81b6b3SRodney W. Grimes 
8864be1e61bSAlexander Langer static char *
8874be1e61bSAlexander Langer get_type(int type)
8885b81b6b3SRodney W. Grimes {
8895b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
8905b81b6b3SRodney W. Grimes 	int	counter = 0;
8915b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
8925b81b6b3SRodney W. Grimes 
8935b81b6b3SRodney W. Grimes 
8945b81b6b3SRodney W. Grimes 	while(counter < numentries)
8955b81b6b3SRodney W. Grimes 	{
8965b81b6b3SRodney W. Grimes 		if(ptr->type == type)
8975b81b6b3SRodney W. Grimes 		{
8985b81b6b3SRodney W. Grimes 			return(ptr->name);
8995b81b6b3SRodney W. Grimes 		}
9005b81b6b3SRodney W. Grimes 		ptr++;
9015b81b6b3SRodney W. Grimes 		counter++;
9025b81b6b3SRodney W. Grimes 	}
9035b81b6b3SRodney W. Grimes 	return("unknown");
9045b81b6b3SRodney W. Grimes }
905f46af505SJordan K. Hubbard 
906f46af505SJordan K. Hubbard 
907f46af505SJordan K. Hubbard static void
908f46af505SJordan K. Hubbard parse_config_line(line, command)
909f46af505SJordan K. Hubbard     char	*line;
910f46af505SJordan K. Hubbard     CMD		*command;
911f46af505SJordan K. Hubbard {
912f46af505SJordan K. Hubbard     char	*cp, *end;
913f46af505SJordan K. Hubbard 
914f46af505SJordan K. Hubbard     cp = line;
915f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
916f46af505SJordan K. Hubbard 		   function */
917f46af505SJordan K. Hubbard     {
918f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
919f46af505SJordan K. Hubbard 
920f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
921f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
922f46af505SJordan K. Hubbard 	{
923f46af505SJordan K. Hubbard 	    break;
924f46af505SJordan K. Hubbard 	}
925f46af505SJordan K. Hubbard 	command->cmd = *cp++;
926f46af505SJordan K. Hubbard 
927f46af505SJordan K. Hubbard 	/*
928f46af505SJordan K. Hubbard 	 * Parse args
929f46af505SJordan K. Hubbard 	 */
930f46af505SJordan K. Hubbard 	while (1)
931f46af505SJordan K. Hubbard 	{
932f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
933f46af505SJordan K. Hubbard 	    if (*cp == '#')
934f46af505SJordan K. Hubbard 	    {
935f46af505SJordan K. Hubbard 		break;		/* found comment */
936f46af505SJordan K. Hubbard 	    }
937f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
938f46af505SJordan K. Hubbard 	    {
939f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
940f46af505SJordan K. Hubbard 	    }
941f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
942f46af505SJordan K. Hubbard 	    {
943f46af505SJordan K. Hubbard 		break;		/* assume end of line */
944f46af505SJordan K. Hubbard 	    }
945f46af505SJordan K. Hubbard 	    end = NULL;
946f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
947f46af505SJordan K. Hubbard 	    if (cp == end)
948f46af505SJordan K. Hubbard 	    {
949f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
950f46af505SJordan K. Hubbard 	    }
951f46af505SJordan K. Hubbard 	    cp = end;
952f46af505SJordan K. Hubbard 	    command->n_args++;
953f46af505SJordan K. Hubbard 	}
954f46af505SJordan K. Hubbard 	break;
955f46af505SJordan K. Hubbard     }
956f46af505SJordan K. Hubbard }
957f46af505SJordan K. Hubbard 
958f46af505SJordan K. Hubbard 
959f46af505SJordan K. Hubbard static int
960f46af505SJordan K. Hubbard process_geometry(command)
961f46af505SJordan K. Hubbard     CMD		*command;
962f46af505SJordan K. Hubbard {
963f46af505SJordan K. Hubbard     int		status = 1, i;
964f46af505SJordan K. Hubbard 
965f46af505SJordan K. Hubbard     while (1)
966f46af505SJordan K. Hubbard     {
967f46af505SJordan K. Hubbard 	geom_processed = 1;
968f46af505SJordan K. Hubbard 	if (part_processed)
969f46af505SJordan K. Hubbard 	{
970d98b1668SPhilippe Charnier 	    warnx(
971d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
972d98b1668SPhilippe Charnier     all partition specifications",
973d98b1668SPhilippe Charnier 		    current_line_number);
974f46af505SJordan K. Hubbard 	    status = 0;
975f46af505SJordan K. Hubbard 	    break;
976f46af505SJordan K. Hubbard 	}
977f46af505SJordan K. Hubbard 	if (command->n_args != 3)
978f46af505SJordan K. Hubbard 	{
979d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
980d98b1668SPhilippe Charnier 		    current_line_number);
981f46af505SJordan K. Hubbard 	    status = 0;
982f46af505SJordan K. Hubbard 	    break;
983f46af505SJordan K. Hubbard 	}
984f46af505SJordan K. Hubbard 	dos_cyls = -1;
985f46af505SJordan K. Hubbard 	dos_heads = -1;
986f46af505SJordan K. Hubbard 	dos_sectors = -1;
987f46af505SJordan K. Hubbard 	for (i = 0; i < 3; ++i)
988f46af505SJordan K. Hubbard 	{
989f46af505SJordan K. Hubbard 	    switch (command->args[i].argtype)
990f46af505SJordan K. Hubbard 	    {
991f46af505SJordan K. Hubbard 	    case 'c':
992f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
993f46af505SJordan K. Hubbard 		break;
994f46af505SJordan K. Hubbard 	    case 'h':
995f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
996f46af505SJordan K. Hubbard 		break;
997f46af505SJordan K. Hubbard 	    case 's':
998f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
999f46af505SJordan K. Hubbard 		break;
1000f46af505SJordan K. Hubbard 	    default:
1001d98b1668SPhilippe Charnier 		warnx(
1002d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1003d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1004f46af505SJordan K. Hubbard 			command->args[i].argtype);
1005f46af505SJordan K. Hubbard 		status = 0;
1006f46af505SJordan K. Hubbard 		break;
1007f46af505SJordan K. Hubbard 	    }
1008f46af505SJordan K. Hubbard 	}
1009f46af505SJordan K. Hubbard 	if (status == 0)
1010f46af505SJordan K. Hubbard 	{
1011f46af505SJordan K. Hubbard 	    break;
1012f46af505SJordan K. Hubbard 	}
1013f46af505SJordan K. Hubbard 
1014f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1015f46af505SJordan K. Hubbard 
1016f46af505SJordan K. Hubbard 	/*
1017f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1018f46af505SJordan K. Hubbard 	 */
1019f46af505SJordan K. Hubbard 	if (dos_cyls < 0)
1020f46af505SJordan K. Hubbard 	{
1021d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1022d98b1668SPhilippe Charnier 		    current_line_number);
1023f46af505SJordan K. Hubbard 	    status = 0;
1024f46af505SJordan K. Hubbard 	}
1025f46af505SJordan K. Hubbard 	if (dos_cyls == 0 || dos_cyls > 1024)
1026f46af505SJordan K. Hubbard 	{
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 
1034f46af505SJordan K. Hubbard 	if (dos_heads < 0)
1035f46af505SJordan K. Hubbard 	{
1036d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1037d98b1668SPhilippe Charnier 		    current_line_number);
1038f46af505SJordan K. Hubbard 	    status = 0;
1039f46af505SJordan K. Hubbard 	}
1040f46af505SJordan K. Hubbard 	else if (dos_heads < 1 || dos_heads > 256)
1041f46af505SJordan K. Hubbard 	{
1042d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1043d98b1668SPhilippe Charnier 		    current_line_number);
1044f46af505SJordan K. Hubbard 	    status = 0;
1045f46af505SJordan K. Hubbard 	}
1046f46af505SJordan K. Hubbard 
1047f46af505SJordan K. Hubbard 	if (dos_sectors < 0)
1048f46af505SJordan K. Hubbard 	{
1049d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1050d98b1668SPhilippe Charnier 		    current_line_number);
1051f46af505SJordan K. Hubbard 	    status = 0;
1052f46af505SJordan K. Hubbard 	}
1053f46af505SJordan K. Hubbard 	else if (dos_sectors < 1 || dos_sectors > 63)
1054f46af505SJordan K. Hubbard 	{
1055d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1056d98b1668SPhilippe Charnier 		    current_line_number);
1057f46af505SJordan K. Hubbard 	    status = 0;
1058f46af505SJordan K. Hubbard 	}
1059f46af505SJordan K. Hubbard 
1060f46af505SJordan K. Hubbard 	break;
1061f46af505SJordan K. Hubbard     }
1062f46af505SJordan K. Hubbard     return (status);
1063f46af505SJordan K. Hubbard }
1064f46af505SJordan K. Hubbard 
1065f46af505SJordan K. Hubbard 
1066f46af505SJordan K. Hubbard static int
1067f46af505SJordan K. Hubbard process_partition(command)
1068f46af505SJordan K. Hubbard     CMD		*command;
1069f46af505SJordan K. Hubbard {
1070f46af505SJordan K. Hubbard     int				status = 0, partition;
1071f46af505SJordan K. Hubbard     unsigned long		chunks, adj_size, max_end;
1072f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1073f46af505SJordan K. Hubbard 
1074f46af505SJordan K. Hubbard     while (1)
1075f46af505SJordan K. Hubbard     {
1076f46af505SJordan K. Hubbard 	part_processed = 1;
1077f46af505SJordan K. Hubbard 	if (command->n_args != 4)
1078f46af505SJordan K. Hubbard 	{
1079d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1080d98b1668SPhilippe Charnier 		    current_line_number);
1081f46af505SJordan K. Hubbard 	    break;
1082f46af505SJordan K. Hubbard 	}
1083f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
10844ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1085f46af505SJordan K. Hubbard 	{
1086d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1087d98b1668SPhilippe Charnier 		    current_line_number, partition);
1088f46af505SJordan K. Hubbard 	    break;
1089f46af505SJordan K. Hubbard 	}
10904ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1091f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1092f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1093f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1094f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1095f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1096f46af505SJordan K. Hubbard 
1097f46af505SJordan K. Hubbard 	if (partp->dp_typ == 0)
1098f46af505SJordan K. Hubbard 	{
1099f46af505SJordan K. Hubbard 	    /*
1100f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1101f46af505SJordan K. Hubbard 	     */
1102f46af505SJordan K. Hubbard 	    /*
1103f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1104f46af505SJordan K. Hubbard 	     */
1105f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1106f46af505SJordan K. Hubbard 	    status = 1;
1107f46af505SJordan K. Hubbard 	    break;
1108f46af505SJordan K. Hubbard 	}
1109f46af505SJordan K. Hubbard 
1110f46af505SJordan K. Hubbard 	/*
1111f46af505SJordan K. Hubbard 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1112f46af505SJordan K. Hubbard 	 */
1113f46af505SJordan K. Hubbard 	if (partp->dp_start % dos_sectors != 0)
1114f46af505SJordan K. Hubbard 	{
1115f46af505SJordan K. Hubbard 	    adj_size =
1116f46af505SJordan K. Hubbard 		(partp->dp_start / dos_sectors + 1) * dos_sectors;
1117f46af505SJordan K. Hubbard 	    if (adj_size > max_end)
1118f46af505SJordan K. Hubbard 	    {
1119f46af505SJordan K. Hubbard 		/*
1120f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1121f46af505SJordan K. Hubbard 		 */
1122d98b1668SPhilippe Charnier 		warnx(
1123d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1124d98b1668SPhilippe Charnier     a cylinder boundary",
1125d98b1668SPhilippe Charnier 			current_line_number, partition);
1126f46af505SJordan K. Hubbard 		break;
1127f46af505SJordan K. Hubbard 	    }
1128d98b1668SPhilippe Charnier 	    warnx(
1129d98b1668SPhilippe Charnier 	"WARNING: adjusting start offset of partition '%d' from %lu\n\
1130d98b1668SPhilippe Charnier     to %lu, to round to an head boundary",
1131d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_start, adj_size);
1132f46af505SJordan K. Hubbard 	    partp->dp_start = adj_size;
1133f46af505SJordan K. Hubbard 	}
1134f46af505SJordan K. Hubbard 
1135f46af505SJordan K. Hubbard 	/*
1136f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1137f46af505SJordan K. Hubbard 	 * boundary.
1138f46af505SJordan K. Hubbard 	 */
1139f46af505SJordan K. Hubbard 	chunks =
1140f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1141f46af505SJordan K. Hubbard 	adj_size = chunks - partp->dp_start;
1142f46af505SJordan K. Hubbard 	if (adj_size != partp->dp_size)
1143f46af505SJordan K. Hubbard 	{
1144d98b1668SPhilippe Charnier 	    warnx(
1145d98b1668SPhilippe Charnier 	"WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1146d98b1668SPhilippe Charnier     to round to a cylinder boundary",
1147d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_size, adj_size);
1148f46af505SJordan K. Hubbard 	    if (chunks > 0)
1149f46af505SJordan K. Hubbard 	    {
1150f46af505SJordan K. Hubbard 		partp->dp_size = adj_size;
1151f46af505SJordan K. Hubbard 	    }
1152f46af505SJordan K. Hubbard 	    else
1153f46af505SJordan K. Hubbard 	    {
1154f46af505SJordan K. Hubbard 		partp->dp_size = 0;
1155f46af505SJordan K. Hubbard 	    }
1156f46af505SJordan K. Hubbard 	}
1157f46af505SJordan K. Hubbard 	if (partp->dp_size < 1)
1158f46af505SJordan K. Hubbard 	{
1159d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: size for partition '%d' is zero",
1160d98b1668SPhilippe Charnier 		    current_line_number, partition);
1161f46af505SJordan K. Hubbard 	    break;
1162f46af505SJordan K. Hubbard 	}
1163f46af505SJordan K. Hubbard 
1164f46af505SJordan K. Hubbard 	dos(partp->dp_start, partp->dp_size,
1165f46af505SJordan K. Hubbard 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1166f46af505SJordan K. Hubbard 	dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1167f46af505SJordan K. Hubbard 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1168f46af505SJordan K. Hubbard 	status = 1;
1169f46af505SJordan K. Hubbard 	break;
1170f46af505SJordan K. Hubbard     }
1171f46af505SJordan K. Hubbard     return (status);
1172f46af505SJordan K. Hubbard }
1173f46af505SJordan K. Hubbard 
1174f46af505SJordan K. Hubbard 
1175f46af505SJordan K. Hubbard static int
1176f46af505SJordan K. Hubbard process_active(command)
1177f46af505SJordan K. Hubbard     CMD		*command;
1178f46af505SJordan K. Hubbard {
1179f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1180f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1181f46af505SJordan K. Hubbard 
1182f46af505SJordan K. Hubbard     while (1)
1183f46af505SJordan K. Hubbard     {
1184f46af505SJordan K. Hubbard 	active_processed = 1;
1185f46af505SJordan K. Hubbard 	if (command->n_args != 1)
1186f46af505SJordan K. Hubbard 	{
1187d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1188d98b1668SPhilippe Charnier 		    current_line_number);
1189f46af505SJordan K. Hubbard 	    status = 0;
1190f46af505SJordan K. Hubbard 	    break;
1191f46af505SJordan K. Hubbard 	}
1192f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
11934ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1194f46af505SJordan K. Hubbard 	{
1195d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1196d98b1668SPhilippe Charnier 		    current_line_number, partition);
1197f46af505SJordan K. Hubbard 	    break;
1198f46af505SJordan K. Hubbard 	}
1199f46af505SJordan K. Hubbard 	/*
1200f46af505SJordan K. Hubbard 	 * Reset active partition
1201f46af505SJordan K. Hubbard 	 */
1202f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1203f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1204f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
12054ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1206f46af505SJordan K. Hubbard 
1207f46af505SJordan K. Hubbard 	status = 1;
1208f46af505SJordan K. Hubbard 	break;
1209f46af505SJordan K. Hubbard     }
1210f46af505SJordan K. Hubbard     return (status);
1211f46af505SJordan K. Hubbard }
1212f46af505SJordan K. Hubbard 
1213f46af505SJordan K. Hubbard 
1214f46af505SJordan K. Hubbard static int
1215f46af505SJordan K. Hubbard process_line(line)
1216f46af505SJordan K. Hubbard     char	*line;
1217f46af505SJordan K. Hubbard {
1218f46af505SJordan K. Hubbard     CMD		command;
1219f46af505SJordan K. Hubbard     int		status = 1;
1220f46af505SJordan K. Hubbard 
1221f46af505SJordan K. Hubbard     while (1)
1222f46af505SJordan K. Hubbard     {
1223f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1224f46af505SJordan K. Hubbard 	switch (command.cmd)
1225f46af505SJordan K. Hubbard 	{
1226f46af505SJordan K. Hubbard 	case 0:
1227f46af505SJordan K. Hubbard 	    /*
1228f46af505SJordan K. Hubbard 	     * Comment or blank line
1229f46af505SJordan K. Hubbard 	     */
1230f46af505SJordan K. Hubbard 	    break;
1231f46af505SJordan K. Hubbard 	case 'g':
1232f46af505SJordan K. Hubbard 	    /*
1233f46af505SJordan K. Hubbard 	     * Set geometry
1234f46af505SJordan K. Hubbard 	     */
1235f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1236f46af505SJordan K. Hubbard 	    break;
1237f46af505SJordan K. Hubbard 	case 'p':
1238f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1239f46af505SJordan K. Hubbard 	    break;
1240f46af505SJordan K. Hubbard 	case 'a':
1241f46af505SJordan K. Hubbard 	    status = process_active(&command);
1242f46af505SJordan K. Hubbard 	    break;
1243f46af505SJordan K. Hubbard 	default:
1244f46af505SJordan K. Hubbard 	    status = 0;
1245f46af505SJordan K. Hubbard 	    break;
1246f46af505SJordan K. Hubbard 	}
1247f46af505SJordan K. Hubbard 	break;
1248f46af505SJordan K. Hubbard     }
1249f46af505SJordan K. Hubbard     return (status);
1250f46af505SJordan K. Hubbard }
1251f46af505SJordan K. Hubbard 
1252f46af505SJordan K. Hubbard 
1253f46af505SJordan K. Hubbard static int
1254f46af505SJordan K. Hubbard read_config(config_file)
1255f46af505SJordan K. Hubbard     char *config_file;
1256f46af505SJordan K. Hubbard {
1257f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1258f46af505SJordan K. Hubbard     int		status = 1;
1259f46af505SJordan K. Hubbard     char	buf[1010];
1260f46af505SJordan K. Hubbard 
1261f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1262f46af505SJordan K. Hubbard 		   function */
1263f46af505SJordan K. Hubbard     {
1264f46af505SJordan K. Hubbard 	if (strcmp(config_file, "-") != 0)
1265f46af505SJordan K. Hubbard 	{
1266f46af505SJordan K. Hubbard 	    /*
1267f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1268f46af505SJordan K. Hubbard 	     */
1269f46af505SJordan K. Hubbard 	    if ((fp = fopen(config_file, "r")) == NULL)
1270f46af505SJordan K. Hubbard 	    {
1271f46af505SJordan K. Hubbard 		status = 0;
1272f46af505SJordan K. Hubbard 		break;
1273f46af505SJordan K. Hubbard 	    }
1274f46af505SJordan K. Hubbard 	}
1275f46af505SJordan K. Hubbard 	else
1276f46af505SJordan K. Hubbard 	{
1277f46af505SJordan K. Hubbard 	    fp = stdin;
1278f46af505SJordan K. Hubbard 	}
1279f46af505SJordan K. Hubbard 	current_line_number = 0;
1280f46af505SJordan K. Hubbard 	while (!feof(fp))
1281f46af505SJordan K. Hubbard 	{
1282f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1283f46af505SJordan K. Hubbard 	    {
1284f46af505SJordan K. Hubbard 		break;
1285f46af505SJordan K. Hubbard 	    }
1286f46af505SJordan K. Hubbard 	    ++current_line_number;
1287f46af505SJordan K. Hubbard 	    status = process_line(buf);
1288f46af505SJordan K. Hubbard 	    if (status == 0)
1289f46af505SJordan K. Hubbard 	    {
1290f46af505SJordan K. Hubbard 		break;
1291f46af505SJordan K. Hubbard 	    }
1292f46af505SJordan K. Hubbard 	}
1293f46af505SJordan K. Hubbard 	break;
1294f46af505SJordan K. Hubbard     }
1295f46af505SJordan K. Hubbard     if (fp)
1296f46af505SJordan K. Hubbard     {
1297f46af505SJordan K. Hubbard 	/*
1298f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1299f46af505SJordan K. Hubbard 	 */
1300f46af505SJordan K. Hubbard 	fclose(fp);
1301f46af505SJordan K. Hubbard     }
1302f46af505SJordan K. Hubbard     return (status);
1303f46af505SJordan K. Hubbard }
1304f46af505SJordan K. Hubbard 
1305f46af505SJordan K. Hubbard 
1306f46af505SJordan K. Hubbard static void
1307f46af505SJordan K. Hubbard reset_boot(void)
1308f46af505SJordan K. Hubbard {
1309f46af505SJordan K. Hubbard     int				i;
1310f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1311f46af505SJordan K. Hubbard 
1312f46af505SJordan K. Hubbard     init_boot();
1313f46af505SJordan K. Hubbard     for (i = 0; i < 4; ++i)
1314f46af505SJordan K. Hubbard     {
1315f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1316f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1317f46af505SJordan K. Hubbard     }
1318f46af505SJordan K. Hubbard }
1319