xref: /freebsd/sbin/fdisk/fdisk.c (revision 691e5f80b58febed9abb6b15d5f0e594c995c603)
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 
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 {
70149938ccSMike Smith   "/dev/ad0", "/dev/wd0", "/dev/da0", "/dev/od0", 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 */
12110b0ee93SWarner Losh static int I_flag  = 0;		/* use entire disk for FreeBSD */
1225b81b6b3SRodney W. Grimes static int a_flag  = 0;		/* set active partition */
12326721a89SRobert Nordier static char *b_flag = NULL;	/* path to boot code */
1245b81b6b3SRodney W. Grimes static int i_flag  = 0;		/* replace partition data */
1255b81b6b3SRodney W. Grimes static int u_flag  = 0;		/* update partition data */
12610b0ee93SWarner Losh static int s_flag  = 0;		/* Print a summary and exit */
127f46af505SJordan K. Hubbard static int t_flag  = 0;		/* test only, if f_flag is given */
128f46af505SJordan K. Hubbard static char *f_flag = NULL;	/* Read config info from file */
129f46af505SJordan K. Hubbard static int v_flag  = 0;		/* Be verbose */
1305b81b6b3SRodney W. Grimes 
1315b81b6b3SRodney W. Grimes struct part_type
1325b81b6b3SRodney W. Grimes {
1335b81b6b3SRodney W. Grimes  unsigned char type;
1345b81b6b3SRodney W. Grimes  char *name;
1355b81b6b3SRodney W. Grimes }part_types[] =
1365b81b6b3SRodney W. Grimes {
1375b81b6b3SRodney W. Grimes 	 {0x00, "unused"}
1385b81b6b3SRodney W. Grimes 	,{0x01, "Primary DOS with 12 bit FAT"}
1395b81b6b3SRodney W. Grimes 	,{0x02, "XENIX / filesystem"}
1405b81b6b3SRodney W. Grimes 	,{0x03, "XENIX /usr filesystem"}
14126555b64SAndrey A. Chernov 	,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
1425b81b6b3SRodney W. Grimes 	,{0x05, "Extended DOS"}
1435b81b6b3SRodney W. Grimes 	,{0x06, "Primary 'big' DOS (> 32MB)"}
144691e5f80SGuy Helmer 	,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
1455b81b6b3SRodney W. Grimes 	,{0x08, "AIX filesystem"}
1465b81b6b3SRodney W. Grimes 	,{0x09, "AIX boot partition or Coherent"}
1475b81b6b3SRodney W. Grimes 	,{0x0A, "OS/2 Boot Manager or OPUS"}
14826555b64SAndrey A. Chernov 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
14926555b64SAndrey A. Chernov 	,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
15026555b64SAndrey A. Chernov 	,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
15126555b64SAndrey A. Chernov 	,{0x0F, "Extended DOS, LBA"}
1525b81b6b3SRodney W. Grimes 	,{0x10, "OPUS"}
1535b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
154691e5f80SGuy Helmer 	,{0x4D, "QNX 4.2 Primary"}
155691e5f80SGuy Helmer 	,{0x4E, "QNX 4.2 Secondary"}
156691e5f80SGuy Helmer 	,{0x4F, "QNX 4.2 Tertiary"}
1575b81b6b3SRodney W. Grimes 	,{0x50, "DM"}
1585b81b6b3SRodney W. Grimes 	,{0x51, "DM"}
1595b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
1605b81b6b3SRodney W. Grimes 	,{0x56, "GB"}
1615b81b6b3SRodney W. Grimes 	,{0x61, "Speed"}
1625b81b6b3SRodney W. Grimes 	,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
1635b81b6b3SRodney W. Grimes 	,{0x64, "Novell Netware 2.xx"}
1645b81b6b3SRodney W. Grimes 	,{0x65, "Novell Netware 3.xx"}
1655b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
1665b81b6b3SRodney W. Grimes 	,{0x80, "Minix 1.1 ... 1.4a"}
1675b81b6b3SRodney W. Grimes 	,{0x81, "Minix 1.4b ... 1.5.10"}
16863ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
16949f7c177SJordan K. Hubbard 	,{0x83, "Linux filesystem"}
1705b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba filesystem"}
1715b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
172d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
17349f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
174e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
1755f0c9424SGary Palmer 	,{0xA7, "NEXTSTEP"}
176ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1775b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 filesystem"}
1785b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
1795b81b6b3SRodney W. Grimes 	,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
1805b81b6b3SRodney W. Grimes 	,{0xE1, "Speed"}
1815b81b6b3SRodney W. Grimes 	,{0xE3, "Speed"}
1825b81b6b3SRodney W. Grimes 	,{0xE4, "Speed"}
1835b81b6b3SRodney W. Grimes 	,{0xF1, "Speed"}
1845b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
1855b81b6b3SRodney W. Grimes 	,{0xF4, "Speed"}
1865b81b6b3SRodney W. Grimes 	,{0xFF, "BBT (Bad Blocks Table)"}
1875b81b6b3SRodney W. Grimes };
1885b81b6b3SRodney W. Grimes 
1894be1e61bSAlexander Langer static void print_s0(int which);
1904be1e61bSAlexander Langer static void print_part(int i);
1914be1e61bSAlexander Langer static void init_sector0(unsigned long start);
192f46af505SJordan K. Hubbard static void init_boot(void);
1934be1e61bSAlexander Langer static void change_part(int i);
1944be1e61bSAlexander Langer static void print_params();
1954be1e61bSAlexander Langer static void change_active(int which);
19612d910e9SRobert Nordier static void change_code();
1974be1e61bSAlexander Langer static void get_params_to_use();
198e2975440SBruce Evans static void dos(int sec, int size, unsigned char *c, unsigned char *s,
199e2975440SBruce Evans 		unsigned char *h);
2004be1e61bSAlexander Langer static int open_disk(int u_flag);
2014be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
2024be1e61bSAlexander Langer static ssize_t write_disk(off_t sector, void *buf);
2034be1e61bSAlexander Langer static int get_params();
2044be1e61bSAlexander Langer static int read_s0();
2054be1e61bSAlexander Langer static int write_s0();
2064be1e61bSAlexander Langer static int ok(char *str);
2074be1e61bSAlexander Langer static int decimal(char *str, int *num, int deflt);
2084be1e61bSAlexander Langer static char *get_type(int type);
209f46af505SJordan K. Hubbard static int read_config(char *config_file);
210f46af505SJordan K. Hubbard static void reset_boot(void);
211d98b1668SPhilippe Charnier static void usage(void);
2124be1e61bSAlexander Langer #if 0
2134be1e61bSAlexander Langer static int hex(char *str, int *num, int deflt);
2144be1e61bSAlexander Langer static int string(char *str, char **ans);
2154be1e61bSAlexander Langer #endif
2165b81b6b3SRodney W. Grimes 
2174be1e61bSAlexander Langer 
2184be1e61bSAlexander Langer int
2194be1e61bSAlexander Langer main(int argc, char *argv[])
2205b81b6b3SRodney W. Grimes {
22126721a89SRobert Nordier 	int	c, i;
2225b81b6b3SRodney W. Grimes 
22310b0ee93SWarner Losh 	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
22426721a89SRobert Nordier 		switch (c) {
22526721a89SRobert Nordier 		case 'B':
22626721a89SRobert Nordier 			B_flag = 1;
2274ddd60b9SBrian Somers 			break;
22810b0ee93SWarner Losh 		case 'I':
22910b0ee93SWarner Losh 			I_flag = 1;
23010b0ee93SWarner Losh 			break;
2315b81b6b3SRodney W. Grimes 		case 'a':
2325b81b6b3SRodney W. Grimes 			a_flag = 1;
2335b81b6b3SRodney W. Grimes 			break;
23412d910e9SRobert Nordier 		case 'b':
23526721a89SRobert Nordier 			b_flag = optarg;
23612d910e9SRobert Nordier 			break;
237f46af505SJordan K. Hubbard 		case 'f':
23826721a89SRobert Nordier 			f_flag = optarg;
239f46af505SJordan K. Hubbard 			break;
2405b81b6b3SRodney W. Grimes 		case 'i':
2415b81b6b3SRodney W. Grimes 			i_flag = 1;
2425b81b6b3SRodney W. Grimes 			break;
24310b0ee93SWarner Losh 		case 's':
24410b0ee93SWarner Losh 			s_flag = 1;
24510b0ee93SWarner Losh 			break;
246f46af505SJordan K. Hubbard 		case 't':
247f46af505SJordan K. Hubbard 			t_flag = 1;
24826721a89SRobert Nordier 			break;
24926721a89SRobert Nordier 		case 'u':
25026721a89SRobert Nordier 			u_flag = 1;
25126721a89SRobert Nordier 			break;
252f46af505SJordan K. Hubbard 		case 'v':
253f46af505SJordan K. Hubbard 			v_flag = 1;
254f46af505SJordan K. Hubbard 			break;
25526721a89SRobert Nordier 		case '1':
25626721a89SRobert Nordier 		case '2':
25726721a89SRobert Nordier 		case '3':
25826721a89SRobert Nordier 		case '4':
25926721a89SRobert Nordier 			partition = c - '0';
26026721a89SRobert Nordier 			break;
2615b81b6b3SRodney W. Grimes 		default:
262d98b1668SPhilippe Charnier 			usage();
2635b81b6b3SRodney W. Grimes 		}
26426721a89SRobert Nordier 	if (f_flag || i_flag)
26526721a89SRobert Nordier 		u_flag = 1;
26626721a89SRobert Nordier 	if (t_flag)
26726721a89SRobert Nordier 		v_flag = 1;
26826721a89SRobert Nordier 	argc -= optind;
26926721a89SRobert Nordier 	argv += optind;
2705b81b6b3SRodney W. Grimes 
2715b81b6b3SRodney W. Grimes 	if (argc > 0)
272e3038c6eSJoerg Wunsch 	{
273e3038c6eSJoerg Wunsch 		static char realname[12];
274e3038c6eSJoerg Wunsch 
275e3038c6eSJoerg Wunsch 		if(strncmp(argv[0], "/dev", 4) == 0)
2765b81b6b3SRodney W. Grimes 			disk = argv[0];
277e3038c6eSJoerg Wunsch 		else
278e3038c6eSJoerg Wunsch 		{
279dc6bbfb6SDavid E. O'Brien 			snprintf(realname, 12, "/dev/%s", argv[0]);
280e3038c6eSJoerg Wunsch 			disk = realname;
281e3038c6eSJoerg Wunsch 		}
2825b81b6b3SRodney W. Grimes 
2835b81b6b3SRodney W. Grimes 		if (open_disk(u_flag) < 0)
284d98b1668SPhilippe Charnier 			err(1, "cannot open disk %s", disk);
285e3038c6eSJoerg Wunsch 	}
286e3038c6eSJoerg Wunsch 	else
287e3038c6eSJoerg Wunsch 	{
28826721a89SRobert Nordier 		int rv = 0;
289e3038c6eSJoerg Wunsch 
290e3038c6eSJoerg Wunsch 		for(i = 0; disks[i]; i++)
291e3038c6eSJoerg Wunsch 		{
292e3038c6eSJoerg Wunsch 			disk = disks[i];
293e3038c6eSJoerg Wunsch 			rv = open_disk(u_flag);
294e3038c6eSJoerg Wunsch 			if(rv != -2) break;
295e3038c6eSJoerg Wunsch 		}
296e3038c6eSJoerg Wunsch 		if(rv < 0)
297d98b1668SPhilippe Charnier 			err(1, "cannot open any disk");
298e3038c6eSJoerg Wunsch 	}
29910b0ee93SWarner Losh 	if (s_flag)
30010b0ee93SWarner Losh 	{
30110b0ee93SWarner Losh 		int i;
30210b0ee93SWarner Losh 		struct dos_partition *partp;
30310b0ee93SWarner Losh 
30410b0ee93SWarner Losh 		if (read_s0())
30510b0ee93SWarner Losh 			err(1, "read_s0");
30610b0ee93SWarner Losh 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
30710b0ee93SWarner Losh 		    dos_sectors);
30810b0ee93SWarner Losh 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
30910b0ee93SWarner Losh 		for (i = 0; i < NDOSPART; i++) {
31010b0ee93SWarner Losh 			partp = ((struct dos_partition *) &mboot.parts) + i;
31110b0ee93SWarner Losh 			if (partp->dp_start == 0 && partp->dp_size == 0)
31210b0ee93SWarner Losh 				continue;
31310b0ee93SWarner Losh 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
31410b0ee93SWarner Losh 			    (u_long) partp->dp_start,
31510b0ee93SWarner Losh 			    (u_long) partp->dp_size, partp->dp_typ,
31610b0ee93SWarner Losh 			    partp->dp_flag);
31710b0ee93SWarner Losh 		}
31810b0ee93SWarner Losh 		exit(0);
31910b0ee93SWarner Losh 	}
3205b81b6b3SRodney W. Grimes 
3215b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
322f46af505SJordan K. Hubbard 
32310b0ee93SWarner Losh 	if (I_flag)
324e6fb3ddeSPoul-Henning Kamp 	{
325e6fb3ddeSPoul-Henning Kamp 		struct dos_partition *partp;
326e6fb3ddeSPoul-Henning Kamp 
327e6fb3ddeSPoul-Henning Kamp 		read_s0();
328e6fb3ddeSPoul-Henning Kamp 		reset_boot();
329e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
330e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
331e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
332e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
333e6fb3ddeSPoul-Henning Kamp 		partp->dp_size = disksecs - dos_sectors;
334e6fb3ddeSPoul-Henning Kamp 
335e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start, partp->dp_size,
336e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
337e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
338e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
339e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
340e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
341e6fb3ddeSPoul-Henning Kamp 		write_s0();
342e6fb3ddeSPoul-Henning Kamp 		exit(0);
343e6fb3ddeSPoul-Henning Kamp 	}
344f46af505SJordan K. Hubbard 	if (f_flag)
345f46af505SJordan K. Hubbard 	{
346f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
347f46af505SJordan K. Hubbard 	    {
348f46af505SJordan K. Hubbard 		reset_boot();
349f46af505SJordan K. Hubbard 	    }
350f46af505SJordan K. Hubbard 
351f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
352f46af505SJordan K. Hubbard 	    {
353f46af505SJordan K. Hubbard 		exit(1);
354f46af505SJordan K. Hubbard 	    }
355f46af505SJordan K. Hubbard 	    if (v_flag)
356f46af505SJordan K. Hubbard 	    {
357f46af505SJordan K. Hubbard 		print_s0(-1);
358f46af505SJordan K. Hubbard 	    }
359f46af505SJordan K. Hubbard 	    if (!t_flag)
360f46af505SJordan K. Hubbard 	    {
361f46af505SJordan K. Hubbard 		write_s0();
362f46af505SJordan K. Hubbard 	    }
363f46af505SJordan K. Hubbard 	}
364f46af505SJordan K. Hubbard 	else
365f46af505SJordan K. Hubbard 	{
3665b81b6b3SRodney W. Grimes 	    if(u_flag)
3675b81b6b3SRodney W. Grimes 	    {
3685b81b6b3SRodney W. Grimes 		get_params_to_use();
3695b81b6b3SRodney W. Grimes 	    }
3705b81b6b3SRodney W. Grimes 	    else
3715b81b6b3SRodney W. Grimes 	    {
3725b81b6b3SRodney W. Grimes 		print_params();
3735b81b6b3SRodney W. Grimes 	    }
3745b81b6b3SRodney W. Grimes 
3755b81b6b3SRodney W. Grimes 	    if (read_s0())
3765b81b6b3SRodney W. Grimes 		init_sector0(1);
3775b81b6b3SRodney W. Grimes 
3787cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3795b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3805b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3815b81b6b3SRodney W. Grimes 	    if (partition == -1)
3824ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3835b81b6b3SRodney W. Grimes 		    change_part(i);
3845b81b6b3SRodney W. Grimes 	    else
3855b81b6b3SRodney W. Grimes 		change_part(partition);
3865b81b6b3SRodney W. Grimes 
3875b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3885b81b6b3SRodney W. Grimes 		change_active(partition);
3895b81b6b3SRodney W. Grimes 
39026721a89SRobert Nordier 	    if (B_flag)
39112d910e9SRobert Nordier 		change_code();
39212d910e9SRobert Nordier 
39326721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
394f46af505SJordan K. Hubbard 		if (!t_flag)
395f46af505SJordan K. Hubbard 		{
3965b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
3975b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
398f46af505SJordan K. Hubbard 		}
3995b81b6b3SRodney W. Grimes 		print_s0(-1);
400f46af505SJordan K. Hubbard 		if (!t_flag)
401f46af505SJordan K. Hubbard 		{
4025b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
4035b81b6b3SRodney W. Grimes 			write_s0();
4045b81b6b3SRodney W. Grimes 		}
405f46af505SJordan K. Hubbard 		else
406f46af505SJordan K. Hubbard 		{
407f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
408f46af505SJordan K. Hubbard 		}
409f46af505SJordan K. Hubbard 	    }
410f46af505SJordan K. Hubbard 	}
4115b81b6b3SRodney W. Grimes 
4125b81b6b3SRodney W. Grimes 	exit(0);
413d98b1668SPhilippe Charnier }
4145b81b6b3SRodney W. Grimes 
415d98b1668SPhilippe Charnier static void
416d98b1668SPhilippe Charnier usage()
417d98b1668SPhilippe Charnier {
41826721a89SRobert Nordier 	fprintf(stderr, "%s%s",
419f10f160dSDoug White 		"usage: fdisk [-Baeitu] [-b bootcode] [-1234] [disk]\n",
42026721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
421d98b1668SPhilippe Charnier         exit(1);
4225b81b6b3SRodney W. Grimes }
4235b81b6b3SRodney W. Grimes 
4244be1e61bSAlexander Langer static void
4254be1e61bSAlexander Langer print_s0(int which)
4265b81b6b3SRodney W. Grimes {
4275b81b6b3SRodney W. Grimes int	i;
4285b81b6b3SRodney W. Grimes 
4295b81b6b3SRodney W. Grimes 	print_params();
4305b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4315b81b6b3SRodney W. Grimes 	if (which == -1)
4324ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4335b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4345b81b6b3SRodney W. Grimes 	else
4355b81b6b3SRodney W. Grimes 		print_part(which);
4365b81b6b3SRodney W. Grimes }
4375b81b6b3SRodney W. Grimes 
4385b81b6b3SRodney W. Grimes static struct dos_partition mtpart = { 0 };
4395b81b6b3SRodney W. Grimes 
4404be1e61bSAlexander Langer static void
4414be1e61bSAlexander Langer print_part(int i)
4425b81b6b3SRodney W. Grimes {
443637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
444637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4455b81b6b3SRodney W. Grimes 
4464ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4475b81b6b3SRodney W. Grimes 
4485b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4495b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4505b81b6b3SRodney W. Grimes 		return;
4515b81b6b3SRodney W. Grimes 	}
452637fe2f7SJustin T. Gibbs 	/*
453637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
454637fe2f7SJustin T. Gibbs 	 */
455637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
456637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
457637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
4585b81b6b3SRodney W. Grimes 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
459ba198492SBruce Evans 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
460ba198492SBruce Evans 		(u_long)partp->dp_start,
461ba198492SBruce Evans 		(u_long)partp->dp_size,
462637fe2f7SJustin T. Gibbs 		part_mb,
463680426beSDavid E. O'Brien 		partp->dp_flag,
464680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4655b81b6b3SRodney W. Grimes 	printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
4665b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4675b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_ssect)
4685b81b6b3SRodney W. Grimes 		,partp->dp_shd
4695b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4705b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_esect)
4715b81b6b3SRodney W. Grimes 		,partp->dp_ehd);
4725b81b6b3SRodney W. Grimes }
4735b81b6b3SRodney W. Grimes 
474f46af505SJordan K. Hubbard 
475f46af505SJordan K. Hubbard static void
476f46af505SJordan K. Hubbard init_boot(void)
477f46af505SJordan K. Hubbard {
47826721a89SRobert Nordier 	const char *fname;
47926721a89SRobert Nordier 	int fd;
48026721a89SRobert Nordier 
48126721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
48226721a89SRobert Nordier 	if ((fd = open(fname, O_RDONLY)) == -1 ||
48326721a89SRobert Nordier 	    read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
48426721a89SRobert Nordier 	    close(fd))
48526721a89SRobert Nordier 		err(1, "%s", fname);
486f46af505SJordan K. Hubbard 	mboot.signature = BOOT_MAGIC;
487f46af505SJordan K. Hubbard }
488f46af505SJordan K. Hubbard 
489f46af505SJordan K. Hubbard 
4904be1e61bSAlexander Langer static void
4914be1e61bSAlexander Langer init_sector0(unsigned long start)
4925b81b6b3SRodney W. Grimes {
4935b81b6b3SRodney W. Grimes struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
4944be1e61bSAlexander Langer unsigned long size = disksecs - start;
4955b81b6b3SRodney W. Grimes 
496f46af505SJordan K. Hubbard 	init_boot();
4975b81b6b3SRodney W. Grimes 
4985b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
4995b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
5005b81b6b3SRodney W. Grimes 	partp->dp_start = start;
5015b81b6b3SRodney W. Grimes 	partp->dp_size = size;
5025b81b6b3SRodney W. Grimes 
503e2975440SBruce Evans 	dos(partp->dp_start, partp->dp_size,
504e2975440SBruce Evans 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
505e2975440SBruce Evans 	dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
506e2975440SBruce Evans 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5075b81b6b3SRodney W. Grimes }
5085b81b6b3SRodney W. Grimes 
5094be1e61bSAlexander Langer static void
5104be1e61bSAlexander Langer change_part(int i)
5115b81b6b3SRodney W. Grimes {
5124ddd60b9SBrian Somers struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
5135b81b6b3SRodney W. Grimes 
5145b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
5155b81b6b3SRodney W. Grimes     print_part(i);
5165b81b6b3SRodney W. Grimes 
5175b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
5185b81b6b3SRodney W. Grimes 	int tmp;
5195b81b6b3SRodney W. Grimes 
5205b81b6b3SRodney W. Grimes 	if (i_flag) {
5215b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
5224ddd60b9SBrian Somers 		if (i == 4) {
5235b81b6b3SRodney W. Grimes 			init_sector0(1);
5244ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
5255b81b6b3SRodney W. Grimes 			print_part(i);
5265b81b6b3SRodney W. Grimes 		}
5275b81b6b3SRodney W. Grimes 	}
5285b81b6b3SRodney W. Grimes 
5295b81b6b3SRodney W. Grimes 	do {
530680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5315b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5325b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
5335b81b6b3SRodney W. Grimes 
5344b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5355b81b6b3SRodney W. Grimes 		{
5365b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5375b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5385b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5395b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5405b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5415b81b6b3SRodney W. Grimes 			Decimal("beginning head", thd, tmp);
5425b81b6b3SRodney W. Grimes 			Decimal("beginning sector", tsec, tmp);
5435b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5445b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5455b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5465b81b6b3SRodney W. Grimes 
5475b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5485b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5495b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5505b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5515b81b6b3SRodney W. Grimes 			Decimal("ending head", thd, tmp);
5525b81b6b3SRodney W. Grimes 			Decimal("ending sector", tsec, tmp);
5535b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5545b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5555b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
5565b81b6b3SRodney W. Grimes 		} else {
557e2975440SBruce Evans 			dos(partp->dp_start, partp->dp_size,
5585b81b6b3SRodney W. Grimes 			    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
559e2975440SBruce Evans 			dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
5605b81b6b3SRodney W. Grimes 			    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5615b81b6b3SRodney W. Grimes 		}
5625b81b6b3SRodney W. Grimes 
5635b81b6b3SRodney W. Grimes 		print_part(i);
5645b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
5655b81b6b3SRodney W. Grimes     }
5665b81b6b3SRodney W. Grimes }
5675b81b6b3SRodney W. Grimes 
5684be1e61bSAlexander Langer static void
5695b81b6b3SRodney W. Grimes print_params()
5705b81b6b3SRodney W. Grimes {
5715b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
5725b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5735b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
5745b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
5755b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
5765b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
5775b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5785b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
5795b81b6b3SRodney W. Grimes }
5805b81b6b3SRodney W. Grimes 
5814be1e61bSAlexander Langer static void
5824be1e61bSAlexander Langer change_active(int which)
5835b81b6b3SRodney W. Grimes {
5845b81b6b3SRodney W. Grimes int i;
5854ddd60b9SBrian Somers int active = 4, tmp;
5865b81b6b3SRodney W. Grimes struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
5875b81b6b3SRodney W. Grimes 
5885b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
5895b81b6b3SRodney W. Grimes 		active = which;
5900b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
5910b461cd7SBruce Evans 		return;
592680426beSDavid E. O'Brien setactive:
593680426beSDavid E. O'Brien 	active = 4;
594680426beSDavid E. O'Brien 	do {
5955b81b6b3SRodney W. Grimes 		Decimal("active partition", active, tmp);
596680426beSDavid E. O'Brien 		if (active < 1 || 4 < active) {
597680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
598680426beSDavid E. O'Brien 					"  Try again.\n");
599680426beSDavid E. O'Brien 			goto setactive;
600680426beSDavid E. O'Brien 		}
601680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
6025b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
6035b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
6044ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
6054ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
6065b81b6b3SRodney W. Grimes }
6075b81b6b3SRodney W. Grimes 
60812d910e9SRobert Nordier static void
60912d910e9SRobert Nordier change_code()
61012d910e9SRobert Nordier {
61112d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
61212d910e9SRobert Nordier 		init_boot();
61312d910e9SRobert Nordier 
61412d910e9SRobert Nordier }
61512d910e9SRobert Nordier 
6164be1e61bSAlexander Langer void
6175b81b6b3SRodney W. Grimes get_params_to_use()
6185b81b6b3SRodney W. Grimes {
6195b81b6b3SRodney W. Grimes 	int	tmp;
6205b81b6b3SRodney W. Grimes 	print_params();
6215b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
6225b81b6b3SRodney W. Grimes 	{
6235b81b6b3SRodney W. Grimes 		do
6245b81b6b3SRodney W. Grimes 		{
6255b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
6265b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
6275b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6285b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6295b81b6b3SRodney W. Grimes 			print_params();
6305b81b6b3SRodney W. Grimes 		}
6315b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6325b81b6b3SRodney W. Grimes 	}
6335b81b6b3SRodney W. Grimes }
6345b81b6b3SRodney W. Grimes 
635f46af505SJordan K. Hubbard 
6365b81b6b3SRodney W. Grimes /***********************************************\
6375b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6385b81b6b3SRodney W. Grimes \***********************************************/
6394be1e61bSAlexander Langer static void
640e2975440SBruce Evans dos(sec, size, c, s, h)
641e2975440SBruce Evans int sec, size;
6425b81b6b3SRodney W. Grimes unsigned char *c, *s, *h;
6435b81b6b3SRodney W. Grimes {
6445b81b6b3SRodney W. Grimes int cy;
6455b81b6b3SRodney W. Grimes int hd;
6465b81b6b3SRodney W. Grimes 
647e2975440SBruce Evans 	if (sec == 0 && size == 0) {
6480b461cd7SBruce Evans 		*s = *c = *h = 0;
6490b461cd7SBruce Evans 		return;
6500b461cd7SBruce Evans 	}
6510b461cd7SBruce Evans 
6525b81b6b3SRodney W. Grimes 	cy = sec / ( dos_cylsecs );
6535b81b6b3SRodney W. Grimes 	sec = sec - cy * ( dos_cylsecs );
6545b81b6b3SRodney W. Grimes 
6555b81b6b3SRodney W. Grimes 	hd = sec / dos_sectors;
6565b81b6b3SRodney W. Grimes 	sec = (sec - hd * dos_sectors) + 1;
6575b81b6b3SRodney W. Grimes 
6585b81b6b3SRodney W. Grimes 	*h = hd;
6595b81b6b3SRodney W. Grimes 	*c = cy & 0xff;
6605b81b6b3SRodney W. Grimes 	*s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
6615b81b6b3SRodney W. Grimes }
6625b81b6b3SRodney W. Grimes 
6635b81b6b3SRodney W. Grimes int fd;
6645b81b6b3SRodney W. Grimes 
6655b81b6b3SRodney W. Grimes 	/* Getting device status */
6665b81b6b3SRodney W. Grimes 
6674be1e61bSAlexander Langer static int
6684be1e61bSAlexander Langer open_disk(int u_flag)
6695b81b6b3SRodney W. Grimes {
6705b81b6b3SRodney W. Grimes struct stat 	st;
6715b81b6b3SRodney W. Grimes 
6725b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
673d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
6745b81b6b3SRodney W. Grimes 		return -1;
675b60eb395SBruce Evans 	}
676b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
677d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
67812d910e9SRobert Nordier 	if ((fd = open(disk,
67910b0ee93SWarner Losh 	    a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
680e3038c6eSJoerg Wunsch 		if(errno == ENXIO)
681e3038c6eSJoerg Wunsch 			return -2;
682d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
6835b81b6b3SRodney W. Grimes 		return -1;
6845b81b6b3SRodney W. Grimes 	}
6855b81b6b3SRodney W. Grimes 	if (get_params(0) == -1) {
686d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
6875b81b6b3SRodney W. Grimes 		return -1;
6885b81b6b3SRodney W. Grimes 	}
6895b81b6b3SRodney W. Grimes 	return fd;
6905b81b6b3SRodney W. Grimes }
6915b81b6b3SRodney W. Grimes 
6924be1e61bSAlexander Langer static ssize_t
6934be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
6945b81b6b3SRodney W. Grimes {
6955b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
6967cb29d33SSøren Schmidt 	if( secsize == 0 )
6977cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
6987cb29d33SSøren Schmidt 			{
6997cb29d33SSøren Schmidt 			/* try the read */
7007cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
7017cb29d33SSøren Schmidt 			if( size == secsize )
7027cb29d33SSøren Schmidt 				/* it worked so return */
7037cb29d33SSøren Schmidt 				return secsize;
7047cb29d33SSøren Schmidt 			}
7057cb29d33SSøren Schmidt 	else
7067cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
7077cb29d33SSøren Schmidt 
7087cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
7097cb29d33SSøren Schmidt 	return -1;
7105b81b6b3SRodney W. Grimes }
7115b81b6b3SRodney W. Grimes 
7124be1e61bSAlexander Langer static ssize_t
7134be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
7145b81b6b3SRodney W. Grimes {
7155b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7167cb29d33SSøren Schmidt 	/* write out in the size that the read_disk found worked */
7177cb29d33SSøren Schmidt 	return write(fd, buf, secsize);
7185b81b6b3SRodney W. Grimes }
7195b81b6b3SRodney W. Grimes 
7204be1e61bSAlexander Langer static int
7214be1e61bSAlexander Langer get_params()
7225b81b6b3SRodney W. Grimes {
7235b81b6b3SRodney W. Grimes 
7245b81b6b3SRodney W. Grimes     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
725d98b1668SPhilippe Charnier 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
726b60eb395SBruce Evans 	dos_cyls = cyls = 1;
727b60eb395SBruce Evans 	dos_heads = heads = 1;
728b60eb395SBruce Evans 	dos_sectors = sectors = 1;
729b60eb395SBruce Evans 	dos_cylsecs = cylsecs = heads * sectors;
730b60eb395SBruce Evans 	disksecs = cyls * heads * sectors;
731b60eb395SBruce Evans 	return disksecs;
7325b81b6b3SRodney W. Grimes     }
7335b81b6b3SRodney W. Grimes 
7345b81b6b3SRodney W. Grimes     dos_cyls = cyls = disklabel.d_ncylinders;
7355b81b6b3SRodney W. Grimes     dos_heads = heads = disklabel.d_ntracks;
7365b81b6b3SRodney W. Grimes     dos_sectors = sectors = disklabel.d_nsectors;
7375b81b6b3SRodney W. Grimes     dos_cylsecs = cylsecs = heads * sectors;
7385b81b6b3SRodney W. Grimes     disksecs = cyls * heads * sectors;
7395b81b6b3SRodney W. Grimes 
7405b81b6b3SRodney W. Grimes     return (disksecs);
7415b81b6b3SRodney W. Grimes }
7425b81b6b3SRodney W. Grimes 
7435b81b6b3SRodney W. Grimes 
7444be1e61bSAlexander Langer static int
7455b81b6b3SRodney W. Grimes read_s0()
7465b81b6b3SRodney W. Grimes {
7475b81b6b3SRodney W. Grimes 	if (read_disk(0, (char *) mboot.bootinst) == -1) {
748d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
7495b81b6b3SRodney W. Grimes 		return -1;
7505b81b6b3SRodney W. Grimes 	}
7515b81b6b3SRodney W. Grimes 	if (mboot.signature != BOOT_MAGIC) {
752d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
7535b81b6b3SRodney W. Grimes 		/* So should we initialize things */
7545b81b6b3SRodney W. Grimes 		return -1;
7555b81b6b3SRodney W. Grimes 	}
7565b81b6b3SRodney W. Grimes 	return 0;
7575b81b6b3SRodney W. Grimes }
7585b81b6b3SRodney W. Grimes 
7594be1e61bSAlexander Langer static int
7605b81b6b3SRodney W. Grimes write_s0()
7615b81b6b3SRodney W. Grimes {
762cefdc4efSBill Fumerola #ifdef NOT_NOW
7635b81b6b3SRodney W. Grimes 	int	flag;
764cefdc4efSBill Fumerola #endif
7655b81b6b3SRodney W. Grimes 	if (iotest) {
7665b81b6b3SRodney W. Grimes 		print_s0(-1);
7675b81b6b3SRodney W. Grimes 		return 0;
7685b81b6b3SRodney W. Grimes 	}
7695b81b6b3SRodney W. Grimes 	/*
7705b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
7715b81b6b3SRodney W. Grimes 	 * disable after writing.
7725b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
7735b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
7745b81b6b3SRodney W. Grimes 	 */
775ba3551dfSJulian Elischer #ifdef NOT_NOW
776e6fb3ddeSPoul-Henning Kamp 	flag = 1;
7775b81b6b3SRodney W. Grimes 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
778d98b1668SPhilippe Charnier 		warn("ioctl DIOCWLABEL");
779ba3551dfSJulian Elischer #endif
7805b81b6b3SRodney W. Grimes 	if (write_disk(0, (char *) mboot.bootinst) == -1) {
781e6fb3ddeSPoul-Henning Kamp 		warn("can't write fdisk partition table");
7825b81b6b3SRodney W. Grimes 		return -1;
783ba3551dfSJulian Elischer #ifdef NOT_NOW
784e6fb3ddeSPoul-Henning Kamp 	flag = 0;
7855b81b6b3SRodney W. Grimes 	(void) ioctl(fd, DIOCWLABEL, &flag);
786ba3551dfSJulian Elischer #endif
7875b81b6b3SRodney W. Grimes 	}
7884be1e61bSAlexander Langer 	return(0);
7895b81b6b3SRodney W. Grimes }
7905b81b6b3SRodney W. Grimes 
7915b81b6b3SRodney W. Grimes 
7924be1e61bSAlexander Langer static int
7935b81b6b3SRodney W. Grimes ok(str)
7945b81b6b3SRodney W. Grimes char *str;
7955b81b6b3SRodney W. Grimes {
7965b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
7975b81b6b3SRodney W. Grimes 	fgets(lbuf, LBUF, stdin);
7985b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
7995b81b6b3SRodney W. Grimes 
8005b81b6b3SRodney W. Grimes 	if (*lbuf &&
8015b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
8025b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
8035b81b6b3SRodney W. Grimes 		return 1;
8045b81b6b3SRodney W. Grimes 	else
8055b81b6b3SRodney W. Grimes 		return 0;
8065b81b6b3SRodney W. Grimes }
8075b81b6b3SRodney W. Grimes 
8084be1e61bSAlexander Langer static int
8094be1e61bSAlexander Langer decimal(char *str, int *num, int deflt)
8105b81b6b3SRodney W. Grimes {
8115b81b6b3SRodney W. Grimes int acc = 0, c;
8125b81b6b3SRodney W. Grimes char *cp;
8135b81b6b3SRodney W. Grimes 
8145b81b6b3SRodney W. Grimes 	while (1) {
8155b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
8165b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8175b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8185b81b6b3SRodney W. Grimes 
8195b81b6b3SRodney W. Grimes 		if (!*lbuf)
8205b81b6b3SRodney W. Grimes 			return 0;
8215b81b6b3SRodney W. Grimes 
8225b81b6b3SRodney W. Grimes 		cp = lbuf;
8235b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8245b81b6b3SRodney W. Grimes 		if (!c)
8255b81b6b3SRodney W. Grimes 			return 0;
8264be1e61bSAlexander Langer 		while ((c = *cp++)) {
8275b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8285b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
8295b81b6b3SRodney W. Grimes 			else
8305b81b6b3SRodney W. Grimes 				break;
8315b81b6b3SRodney W. Grimes 		}
8325b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8335b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8345b81b6b3SRodney W. Grimes 		if (!c) {
8355b81b6b3SRodney W. Grimes 			*num = acc;
8365b81b6b3SRodney W. Grimes 			return 1;
8375b81b6b3SRodney W. Grimes 		} else
838680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
8395b81b6b3SRodney W. Grimes 				lbuf);
8405b81b6b3SRodney W. Grimes 	}
8415b81b6b3SRodney W. Grimes 
8425b81b6b3SRodney W. Grimes }
8435b81b6b3SRodney W. Grimes 
8444be1e61bSAlexander Langer #if 0
8454be1e61bSAlexander Langer static int
8464be1e61bSAlexander Langer hex(char *str, int *num, int deflt)
8475b81b6b3SRodney W. Grimes {
8485b81b6b3SRodney W. Grimes int acc = 0, c;
8495b81b6b3SRodney W. Grimes char *cp;
8505b81b6b3SRodney W. Grimes 
8515b81b6b3SRodney W. Grimes 	while (1) {
8525b81b6b3SRodney W. Grimes 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
8535b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8545b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8555b81b6b3SRodney W. Grimes 
8565b81b6b3SRodney W. Grimes 		if (!*lbuf)
8575b81b6b3SRodney W. Grimes 			return 0;
8585b81b6b3SRodney W. Grimes 
8595b81b6b3SRodney W. Grimes 		cp = lbuf;
8605b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8615b81b6b3SRodney W. Grimes 		if (!c)
8625b81b6b3SRodney W. Grimes 			return 0;
8634be1e61bSAlexander Langer 		while ((c = *cp++)) {
8645b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8655b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - '0';
8665b81b6b3SRodney W. Grimes 			else if (c <= 'f' && c >= 'a')
8675b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'a' + 10;
8685b81b6b3SRodney W. Grimes 			else if (c <= 'F' && c >= 'A')
8695b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'A' + 10;
8705b81b6b3SRodney W. Grimes 			else
8715b81b6b3SRodney W. Grimes 				break;
8725b81b6b3SRodney W. Grimes 		}
8735b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8745b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8755b81b6b3SRodney W. Grimes 		if (!c) {
8765b81b6b3SRodney W. Grimes 			*num = acc;
8775b81b6b3SRodney W. Grimes 			return 1;
8785b81b6b3SRodney W. Grimes 		} else
879680426beSDavid E. O'Brien 			printf("%s is an invalid hex number.  Try again.\n",
8805b81b6b3SRodney W. Grimes 				lbuf);
8815b81b6b3SRodney W. Grimes 	}
8825b81b6b3SRodney W. Grimes 
8835b81b6b3SRodney W. Grimes }
8845b81b6b3SRodney W. Grimes 
8854be1e61bSAlexander Langer static int
8864be1e61bSAlexander Langer string(char *str, char **ans)
8875b81b6b3SRodney W. Grimes {
8885b81b6b3SRodney W. Grimes int c;
8895b81b6b3SRodney W. Grimes char *cp = lbuf;
8905b81b6b3SRodney W. Grimes 
8915b81b6b3SRodney W. Grimes 	while (1) {
8925b81b6b3SRodney W. Grimes 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
8935b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8945b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8955b81b6b3SRodney W. Grimes 
8965b81b6b3SRodney W. Grimes 		if (!*lbuf)
8975b81b6b3SRodney W. Grimes 			return 0;
8985b81b6b3SRodney W. Grimes 
8995b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9005b81b6b3SRodney W. Grimes 		if (c == '"') {
9015b81b6b3SRodney W. Grimes 			c = *++cp;
9025b81b6b3SRodney W. Grimes 			*ans = cp;
9035b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != '"') cp++;
9045b81b6b3SRodney W. Grimes 		} else {
9055b81b6b3SRodney W. Grimes 			*ans = cp;
9065b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
9075b81b6b3SRodney W. Grimes 		}
9085b81b6b3SRodney W. Grimes 
9095b81b6b3SRodney W. Grimes 		if (c)
9105b81b6b3SRodney W. Grimes 			*cp = 0;
9115b81b6b3SRodney W. Grimes 		return 1;
9125b81b6b3SRodney W. Grimes 	}
9135b81b6b3SRodney W. Grimes }
9144be1e61bSAlexander Langer #endif
9155b81b6b3SRodney W. Grimes 
9164be1e61bSAlexander Langer static char *
9174be1e61bSAlexander Langer get_type(int type)
9185b81b6b3SRodney W. Grimes {
9195b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
9205b81b6b3SRodney W. Grimes 	int	counter = 0;
9215b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
9225b81b6b3SRodney W. Grimes 
9235b81b6b3SRodney W. Grimes 
9245b81b6b3SRodney W. Grimes 	while(counter < numentries)
9255b81b6b3SRodney W. Grimes 	{
9265b81b6b3SRodney W. Grimes 		if(ptr->type == type)
9275b81b6b3SRodney W. Grimes 		{
9285b81b6b3SRodney W. Grimes 			return(ptr->name);
9295b81b6b3SRodney W. Grimes 		}
9305b81b6b3SRodney W. Grimes 		ptr++;
9315b81b6b3SRodney W. Grimes 		counter++;
9325b81b6b3SRodney W. Grimes 	}
9335b81b6b3SRodney W. Grimes 	return("unknown");
9345b81b6b3SRodney W. Grimes }
935f46af505SJordan K. Hubbard 
936f46af505SJordan K. Hubbard 
937f46af505SJordan K. Hubbard static void
938f46af505SJordan K. Hubbard parse_config_line(line, command)
939f46af505SJordan K. Hubbard     char	*line;
940f46af505SJordan K. Hubbard     CMD		*command;
941f46af505SJordan K. Hubbard {
942f46af505SJordan K. Hubbard     char	*cp, *end;
943f46af505SJordan K. Hubbard 
944f46af505SJordan K. Hubbard     cp = line;
945f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
946f46af505SJordan K. Hubbard 		   function */
947f46af505SJordan K. Hubbard     {
948f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
949f46af505SJordan K. Hubbard 
950f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
951f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
952f46af505SJordan K. Hubbard 	{
953f46af505SJordan K. Hubbard 	    break;
954f46af505SJordan K. Hubbard 	}
955f46af505SJordan K. Hubbard 	command->cmd = *cp++;
956f46af505SJordan K. Hubbard 
957f46af505SJordan K. Hubbard 	/*
958f46af505SJordan K. Hubbard 	 * Parse args
959f46af505SJordan K. Hubbard 	 */
960f46af505SJordan K. Hubbard 	while (1)
961f46af505SJordan K. Hubbard 	{
962f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
963f46af505SJordan K. Hubbard 	    if (*cp == '#')
964f46af505SJordan K. Hubbard 	    {
965f46af505SJordan K. Hubbard 		break;		/* found comment */
966f46af505SJordan K. Hubbard 	    }
967f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
968f46af505SJordan K. Hubbard 	    {
969f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
970f46af505SJordan K. Hubbard 	    }
971f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
972f46af505SJordan K. Hubbard 	    {
973f46af505SJordan K. Hubbard 		break;		/* assume end of line */
974f46af505SJordan K. Hubbard 	    }
975f46af505SJordan K. Hubbard 	    end = NULL;
976f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
977f46af505SJordan K. Hubbard 	    if (cp == end)
978f46af505SJordan K. Hubbard 	    {
979f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
980f46af505SJordan K. Hubbard 	    }
981f46af505SJordan K. Hubbard 	    cp = end;
982f46af505SJordan K. Hubbard 	    command->n_args++;
983f46af505SJordan K. Hubbard 	}
984f46af505SJordan K. Hubbard 	break;
985f46af505SJordan K. Hubbard     }
986f46af505SJordan K. Hubbard }
987f46af505SJordan K. Hubbard 
988f46af505SJordan K. Hubbard 
989f46af505SJordan K. Hubbard static int
990f46af505SJordan K. Hubbard process_geometry(command)
991f46af505SJordan K. Hubbard     CMD		*command;
992f46af505SJordan K. Hubbard {
993f46af505SJordan K. Hubbard     int		status = 1, i;
994f46af505SJordan K. Hubbard 
995f46af505SJordan K. Hubbard     while (1)
996f46af505SJordan K. Hubbard     {
997f46af505SJordan K. Hubbard 	geom_processed = 1;
998f46af505SJordan K. Hubbard 	if (part_processed)
999f46af505SJordan K. Hubbard 	{
1000d98b1668SPhilippe Charnier 	    warnx(
1001d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
1002d98b1668SPhilippe Charnier     all partition specifications",
1003d98b1668SPhilippe Charnier 		    current_line_number);
1004f46af505SJordan K. Hubbard 	    status = 0;
1005f46af505SJordan K. Hubbard 	    break;
1006f46af505SJordan K. Hubbard 	}
1007f46af505SJordan K. Hubbard 	if (command->n_args != 3)
1008f46af505SJordan K. Hubbard 	{
1009d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
1010d98b1668SPhilippe Charnier 		    current_line_number);
1011f46af505SJordan K. Hubbard 	    status = 0;
1012f46af505SJordan K. Hubbard 	    break;
1013f46af505SJordan K. Hubbard 	}
1014f46af505SJordan K. Hubbard 	dos_cyls = -1;
1015f46af505SJordan K. Hubbard 	dos_heads = -1;
1016f46af505SJordan K. Hubbard 	dos_sectors = -1;
1017f46af505SJordan K. Hubbard 	for (i = 0; i < 3; ++i)
1018f46af505SJordan K. Hubbard 	{
1019f46af505SJordan K. Hubbard 	    switch (command->args[i].argtype)
1020f46af505SJordan K. Hubbard 	    {
1021f46af505SJordan K. Hubbard 	    case 'c':
1022f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
1023f46af505SJordan K. Hubbard 		break;
1024f46af505SJordan K. Hubbard 	    case 'h':
1025f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
1026f46af505SJordan K. Hubbard 		break;
1027f46af505SJordan K. Hubbard 	    case 's':
1028f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
1029f46af505SJordan K. Hubbard 		break;
1030f46af505SJordan K. Hubbard 	    default:
1031d98b1668SPhilippe Charnier 		warnx(
1032d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1033d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1034f46af505SJordan K. Hubbard 			command->args[i].argtype);
1035f46af505SJordan K. Hubbard 		status = 0;
1036f46af505SJordan K. Hubbard 		break;
1037f46af505SJordan K. Hubbard 	    }
1038f46af505SJordan K. Hubbard 	}
1039f46af505SJordan K. Hubbard 	if (status == 0)
1040f46af505SJordan K. Hubbard 	{
1041f46af505SJordan K. Hubbard 	    break;
1042f46af505SJordan K. Hubbard 	}
1043f46af505SJordan K. Hubbard 
1044f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1045f46af505SJordan K. Hubbard 
1046f46af505SJordan K. Hubbard 	/*
1047f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1048f46af505SJordan K. Hubbard 	 */
1049f46af505SJordan K. Hubbard 	if (dos_cyls < 0)
1050f46af505SJordan K. Hubbard 	{
1051d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1052d98b1668SPhilippe Charnier 		    current_line_number);
1053f46af505SJordan K. Hubbard 	    status = 0;
1054f46af505SJordan K. Hubbard 	}
1055f46af505SJordan K. Hubbard 	if (dos_cyls == 0 || dos_cyls > 1024)
1056f46af505SJordan K. Hubbard 	{
1057d98b1668SPhilippe Charnier 	    warnx(
1058d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1059f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1060d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1061d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1062f46af505SJordan K. Hubbard 	}
1063f46af505SJordan K. Hubbard 
1064f46af505SJordan K. Hubbard 	if (dos_heads < 0)
1065f46af505SJordan K. Hubbard 	{
1066d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1067d98b1668SPhilippe Charnier 		    current_line_number);
1068f46af505SJordan K. Hubbard 	    status = 0;
1069f46af505SJordan K. Hubbard 	}
1070f46af505SJordan K. Hubbard 	else if (dos_heads < 1 || dos_heads > 256)
1071f46af505SJordan K. Hubbard 	{
1072d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1073d98b1668SPhilippe Charnier 		    current_line_number);
1074f46af505SJordan K. Hubbard 	    status = 0;
1075f46af505SJordan K. Hubbard 	}
1076f46af505SJordan K. Hubbard 
1077f46af505SJordan K. Hubbard 	if (dos_sectors < 0)
1078f46af505SJordan K. Hubbard 	{
1079d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1080d98b1668SPhilippe Charnier 		    current_line_number);
1081f46af505SJordan K. Hubbard 	    status = 0;
1082f46af505SJordan K. Hubbard 	}
1083f46af505SJordan K. Hubbard 	else if (dos_sectors < 1 || dos_sectors > 63)
1084f46af505SJordan K. Hubbard 	{
1085d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1086d98b1668SPhilippe Charnier 		    current_line_number);
1087f46af505SJordan K. Hubbard 	    status = 0;
1088f46af505SJordan K. Hubbard 	}
1089f46af505SJordan K. Hubbard 
1090f46af505SJordan K. Hubbard 	break;
1091f46af505SJordan K. Hubbard     }
1092f46af505SJordan K. Hubbard     return (status);
1093f46af505SJordan K. Hubbard }
1094f46af505SJordan K. Hubbard 
1095f46af505SJordan K. Hubbard 
1096f46af505SJordan K. Hubbard static int
1097f46af505SJordan K. Hubbard process_partition(command)
1098f46af505SJordan K. Hubbard     CMD		*command;
1099f46af505SJordan K. Hubbard {
1100f46af505SJordan K. Hubbard     int				status = 0, partition;
1101f46af505SJordan K. Hubbard     unsigned long		chunks, adj_size, max_end;
1102f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1103f46af505SJordan K. Hubbard 
1104f46af505SJordan K. Hubbard     while (1)
1105f46af505SJordan K. Hubbard     {
1106f46af505SJordan K. Hubbard 	part_processed = 1;
1107f46af505SJordan K. Hubbard 	if (command->n_args != 4)
1108f46af505SJordan K. Hubbard 	{
1109d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1110d98b1668SPhilippe Charnier 		    current_line_number);
1111f46af505SJordan K. Hubbard 	    break;
1112f46af505SJordan K. Hubbard 	}
1113f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
11144ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1115f46af505SJordan K. Hubbard 	{
1116d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1117d98b1668SPhilippe Charnier 		    current_line_number, partition);
1118f46af505SJordan K. Hubbard 	    break;
1119f46af505SJordan K. Hubbard 	}
11204ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1121f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1122f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1123f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1124f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1125f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1126f46af505SJordan K. Hubbard 
1127f46af505SJordan K. Hubbard 	if (partp->dp_typ == 0)
1128f46af505SJordan K. Hubbard 	{
1129f46af505SJordan K. Hubbard 	    /*
1130f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1131f46af505SJordan K. Hubbard 	     */
1132f46af505SJordan K. Hubbard 	    /*
1133f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1134f46af505SJordan K. Hubbard 	     */
1135f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1136f46af505SJordan K. Hubbard 	    status = 1;
1137f46af505SJordan K. Hubbard 	    break;
1138f46af505SJordan K. Hubbard 	}
1139f46af505SJordan K. Hubbard 
1140f46af505SJordan K. Hubbard 	/*
1141f46af505SJordan K. Hubbard 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1142f46af505SJordan K. Hubbard 	 */
1143f46af505SJordan K. Hubbard 	if (partp->dp_start % dos_sectors != 0)
1144f46af505SJordan K. Hubbard 	{
1145f46af505SJordan K. Hubbard 	    adj_size =
1146f46af505SJordan K. Hubbard 		(partp->dp_start / dos_sectors + 1) * dos_sectors;
1147f46af505SJordan K. Hubbard 	    if (adj_size > max_end)
1148f46af505SJordan K. Hubbard 	    {
1149f46af505SJordan K. Hubbard 		/*
1150f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1151f46af505SJordan K. Hubbard 		 */
1152d98b1668SPhilippe Charnier 		warnx(
1153d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1154d98b1668SPhilippe Charnier     a cylinder boundary",
1155d98b1668SPhilippe Charnier 			current_line_number, partition);
1156f46af505SJordan K. Hubbard 		break;
1157f46af505SJordan K. Hubbard 	    }
1158d98b1668SPhilippe Charnier 	    warnx(
1159d98b1668SPhilippe Charnier 	"WARNING: adjusting start offset of partition '%d' from %lu\n\
1160d98b1668SPhilippe Charnier     to %lu, to round to an head boundary",
1161d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_start, adj_size);
1162f46af505SJordan K. Hubbard 	    partp->dp_start = adj_size;
1163f46af505SJordan K. Hubbard 	}
1164f46af505SJordan K. Hubbard 
1165f46af505SJordan K. Hubbard 	/*
1166f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1167f46af505SJordan K. Hubbard 	 * boundary.
1168f46af505SJordan K. Hubbard 	 */
1169f46af505SJordan K. Hubbard 	chunks =
1170f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1171f46af505SJordan K. Hubbard 	adj_size = chunks - partp->dp_start;
1172f46af505SJordan K. Hubbard 	if (adj_size != partp->dp_size)
1173f46af505SJordan K. Hubbard 	{
1174d98b1668SPhilippe Charnier 	    warnx(
1175d98b1668SPhilippe Charnier 	"WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1176d98b1668SPhilippe Charnier     to round to a cylinder boundary",
1177d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_size, adj_size);
1178f46af505SJordan K. Hubbard 	    if (chunks > 0)
1179f46af505SJordan K. Hubbard 	    {
1180f46af505SJordan K. Hubbard 		partp->dp_size = adj_size;
1181f46af505SJordan K. Hubbard 	    }
1182f46af505SJordan K. Hubbard 	    else
1183f46af505SJordan K. Hubbard 	    {
1184f46af505SJordan K. Hubbard 		partp->dp_size = 0;
1185f46af505SJordan K. Hubbard 	    }
1186f46af505SJordan K. Hubbard 	}
1187f46af505SJordan K. Hubbard 	if (partp->dp_size < 1)
1188f46af505SJordan K. Hubbard 	{
1189d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: size for partition '%d' is zero",
1190d98b1668SPhilippe Charnier 		    current_line_number, partition);
1191f46af505SJordan K. Hubbard 	    break;
1192f46af505SJordan K. Hubbard 	}
1193f46af505SJordan K. Hubbard 
1194f46af505SJordan K. Hubbard 	dos(partp->dp_start, partp->dp_size,
1195f46af505SJordan K. Hubbard 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1196f46af505SJordan K. Hubbard 	dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1197f46af505SJordan K. Hubbard 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1198f46af505SJordan K. Hubbard 	status = 1;
1199f46af505SJordan K. Hubbard 	break;
1200f46af505SJordan K. Hubbard     }
1201f46af505SJordan K. Hubbard     return (status);
1202f46af505SJordan K. Hubbard }
1203f46af505SJordan K. Hubbard 
1204f46af505SJordan K. Hubbard 
1205f46af505SJordan K. Hubbard static int
1206f46af505SJordan K. Hubbard process_active(command)
1207f46af505SJordan K. Hubbard     CMD		*command;
1208f46af505SJordan K. Hubbard {
1209f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1210f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1211f46af505SJordan K. Hubbard 
1212f46af505SJordan K. Hubbard     while (1)
1213f46af505SJordan K. Hubbard     {
1214f46af505SJordan K. Hubbard 	active_processed = 1;
1215f46af505SJordan K. Hubbard 	if (command->n_args != 1)
1216f46af505SJordan K. Hubbard 	{
1217d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1218d98b1668SPhilippe Charnier 		    current_line_number);
1219f46af505SJordan K. Hubbard 	    status = 0;
1220f46af505SJordan K. Hubbard 	    break;
1221f46af505SJordan K. Hubbard 	}
1222f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
12234ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1224f46af505SJordan K. Hubbard 	{
1225d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1226d98b1668SPhilippe Charnier 		    current_line_number, partition);
1227f46af505SJordan K. Hubbard 	    break;
1228f46af505SJordan K. Hubbard 	}
1229f46af505SJordan K. Hubbard 	/*
1230f46af505SJordan K. Hubbard 	 * Reset active partition
1231f46af505SJordan K. Hubbard 	 */
1232f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1233f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1234f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
12354ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1236f46af505SJordan K. Hubbard 
1237f46af505SJordan K. Hubbard 	status = 1;
1238f46af505SJordan K. Hubbard 	break;
1239f46af505SJordan K. Hubbard     }
1240f46af505SJordan K. Hubbard     return (status);
1241f46af505SJordan K. Hubbard }
1242f46af505SJordan K. Hubbard 
1243f46af505SJordan K. Hubbard 
1244f46af505SJordan K. Hubbard static int
1245f46af505SJordan K. Hubbard process_line(line)
1246f46af505SJordan K. Hubbard     char	*line;
1247f46af505SJordan K. Hubbard {
1248f46af505SJordan K. Hubbard     CMD		command;
1249f46af505SJordan K. Hubbard     int		status = 1;
1250f46af505SJordan K. Hubbard 
1251f46af505SJordan K. Hubbard     while (1)
1252f46af505SJordan K. Hubbard     {
1253f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1254f46af505SJordan K. Hubbard 	switch (command.cmd)
1255f46af505SJordan K. Hubbard 	{
1256f46af505SJordan K. Hubbard 	case 0:
1257f46af505SJordan K. Hubbard 	    /*
1258f46af505SJordan K. Hubbard 	     * Comment or blank line
1259f46af505SJordan K. Hubbard 	     */
1260f46af505SJordan K. Hubbard 	    break;
1261f46af505SJordan K. Hubbard 	case 'g':
1262f46af505SJordan K. Hubbard 	    /*
1263f46af505SJordan K. Hubbard 	     * Set geometry
1264f46af505SJordan K. Hubbard 	     */
1265f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1266f46af505SJordan K. Hubbard 	    break;
1267f46af505SJordan K. Hubbard 	case 'p':
1268f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1269f46af505SJordan K. Hubbard 	    break;
1270f46af505SJordan K. Hubbard 	case 'a':
1271f46af505SJordan K. Hubbard 	    status = process_active(&command);
1272f46af505SJordan K. Hubbard 	    break;
1273f46af505SJordan K. Hubbard 	default:
1274f46af505SJordan K. Hubbard 	    status = 0;
1275f46af505SJordan K. Hubbard 	    break;
1276f46af505SJordan K. Hubbard 	}
1277f46af505SJordan K. Hubbard 	break;
1278f46af505SJordan K. Hubbard     }
1279f46af505SJordan K. Hubbard     return (status);
1280f46af505SJordan K. Hubbard }
1281f46af505SJordan K. Hubbard 
1282f46af505SJordan K. Hubbard 
1283f46af505SJordan K. Hubbard static int
1284f46af505SJordan K. Hubbard read_config(config_file)
1285f46af505SJordan K. Hubbard     char *config_file;
1286f46af505SJordan K. Hubbard {
1287f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1288f46af505SJordan K. Hubbard     int		status = 1;
1289f46af505SJordan K. Hubbard     char	buf[1010];
1290f46af505SJordan K. Hubbard 
1291f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1292f46af505SJordan K. Hubbard 		   function */
1293f46af505SJordan K. Hubbard     {
1294f46af505SJordan K. Hubbard 	if (strcmp(config_file, "-") != 0)
1295f46af505SJordan K. Hubbard 	{
1296f46af505SJordan K. Hubbard 	    /*
1297f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1298f46af505SJordan K. Hubbard 	     */
1299f46af505SJordan K. Hubbard 	    if ((fp = fopen(config_file, "r")) == NULL)
1300f46af505SJordan K. Hubbard 	    {
1301f46af505SJordan K. Hubbard 		status = 0;
1302f46af505SJordan K. Hubbard 		break;
1303f46af505SJordan K. Hubbard 	    }
1304f46af505SJordan K. Hubbard 	}
1305f46af505SJordan K. Hubbard 	else
1306f46af505SJordan K. Hubbard 	{
1307f46af505SJordan K. Hubbard 	    fp = stdin;
1308f46af505SJordan K. Hubbard 	}
1309f46af505SJordan K. Hubbard 	current_line_number = 0;
1310f46af505SJordan K. Hubbard 	while (!feof(fp))
1311f46af505SJordan K. Hubbard 	{
1312f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1313f46af505SJordan K. Hubbard 	    {
1314f46af505SJordan K. Hubbard 		break;
1315f46af505SJordan K. Hubbard 	    }
1316f46af505SJordan K. Hubbard 	    ++current_line_number;
1317f46af505SJordan K. Hubbard 	    status = process_line(buf);
1318f46af505SJordan K. Hubbard 	    if (status == 0)
1319f46af505SJordan K. Hubbard 	    {
1320f46af505SJordan K. Hubbard 		break;
1321f46af505SJordan K. Hubbard 	    }
1322f46af505SJordan K. Hubbard 	}
1323f46af505SJordan K. Hubbard 	break;
1324f46af505SJordan K. Hubbard     }
1325f46af505SJordan K. Hubbard     if (fp)
1326f46af505SJordan K. Hubbard     {
1327f46af505SJordan K. Hubbard 	/*
1328f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1329f46af505SJordan K. Hubbard 	 */
1330f46af505SJordan K. Hubbard 	fclose(fp);
1331f46af505SJordan K. Hubbard     }
1332f46af505SJordan K. Hubbard     return (status);
1333f46af505SJordan K. Hubbard }
1334f46af505SJordan K. Hubbard 
1335f46af505SJordan K. Hubbard 
1336f46af505SJordan K. Hubbard static void
1337f46af505SJordan K. Hubbard reset_boot(void)
1338f46af505SJordan K. Hubbard {
1339f46af505SJordan K. Hubbard     int				i;
1340f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1341f46af505SJordan K. Hubbard 
1342f46af505SJordan K. Hubbard     init_boot();
1343f46af505SJordan K. Hubbard     for (i = 0; i < 4; ++i)
1344f46af505SJordan K. Hubbard     {
1345f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1346f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1347f46af505SJordan K. Hubbard     }
1348f46af505SJordan K. Hubbard }
1349