xref: /freebsd/sbin/fdisk/fdisk.c (revision 85c2cf303dbd843c7927692db7f414ae1199168a)
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 
4885c2cf30SJohn Baldwin #define MBRSIGOFF	510
4985c2cf30SJohn Baldwin 
505b81b6b3SRodney W. Grimes /*
515b81b6b3SRodney W. Grimes  *
525b81b6b3SRodney W. Grimes  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
535b81b6b3SRodney W. Grimes  *
545b81b6b3SRodney W. Grimes  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
555b81b6b3SRodney W. Grimes  *	Copyright (c) 1989	Robert. V. Baron
565b81b6b3SRodney W. Grimes  *	Created.
575b81b6b3SRodney W. Grimes  */
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
605b81b6b3SRodney W. Grimes #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
615b81b6b3SRodney W. Grimes #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
625b81b6b3SRodney W. Grimes 
635b81b6b3SRodney W. Grimes #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
645b81b6b3SRodney W. Grimes 
657cb29d33SSøren Schmidt #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
667cb29d33SSøren Schmidt #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
677cb29d33SSøren Schmidt int secsize = 0;		/* the sensed sector size */
685b81b6b3SRodney W. Grimes 
69e3038c6eSJoerg Wunsch const char *disk;
70e3038c6eSJoerg Wunsch const char *disks[] =
71e3038c6eSJoerg Wunsch {
72149938ccSMike Smith   "/dev/ad0", "/dev/wd0", "/dev/da0", "/dev/od0", 0
73e3038c6eSJoerg Wunsch };
74e3038c6eSJoerg Wunsch 
755b81b6b3SRodney W. Grimes struct disklabel disklabel;		/* disk parameters */
765b81b6b3SRodney W. Grimes 
775b81b6b3SRodney W. Grimes int cyls, sectors, heads, cylsecs, disksecs;
785b81b6b3SRodney W. Grimes 
795b81b6b3SRodney W. Grimes struct mboot
805b81b6b3SRodney W. Grimes {
81d98b1668SPhilippe Charnier 	unsigned char padding[2]; /* force the longs to be long aligned */
8285c2cf30SJohn Baldwin   	unsigned char *bootinst;  /* boot code */
8385c2cf30SJohn Baldwin   	off_t bootinst_size;
845b81b6b3SRodney W. Grimes 	struct	dos_partition parts[4];
855b81b6b3SRodney W. Grimes };
8685c2cf30SJohn Baldwin struct mboot mboot = {{0}, NULL, 0};
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"}
15335bfe0c3SBrian Somers 	,{0x39, "plan9"}
1545b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
155691e5f80SGuy Helmer 	,{0x4D, "QNX 4.2 Primary"}
156691e5f80SGuy Helmer 	,{0x4E, "QNX 4.2 Secondary"}
157691e5f80SGuy Helmer 	,{0x4F, "QNX 4.2 Tertiary"}
1585b81b6b3SRodney W. Grimes 	,{0x50, "DM"}
1595b81b6b3SRodney W. Grimes 	,{0x51, "DM"}
1605b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
1615b81b6b3SRodney W. Grimes 	,{0x56, "GB"}
1625b81b6b3SRodney W. Grimes 	,{0x61, "Speed"}
1635b81b6b3SRodney W. Grimes 	,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
1645b81b6b3SRodney W. Grimes 	,{0x64, "Novell Netware 2.xx"}
1655b81b6b3SRodney W. Grimes 	,{0x65, "Novell Netware 3.xx"}
1665b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
1675b81b6b3SRodney W. Grimes 	,{0x80, "Minix 1.1 ... 1.4a"}
1685b81b6b3SRodney W. Grimes 	,{0x81, "Minix 1.4b ... 1.5.10"}
16963ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
17049f7c177SJordan K. Hubbard 	,{0x83, "Linux filesystem"}
1715b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba filesystem"}
1725b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
173d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
17449f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
175e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
1765f0c9424SGary Palmer 	,{0xA7, "NEXTSTEP"}
177ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1785b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 filesystem"}
1795b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
1805b81b6b3SRodney W. Grimes 	,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
1815b81b6b3SRodney W. Grimes 	,{0xE1, "Speed"}
1825b81b6b3SRodney W. Grimes 	,{0xE3, "Speed"}
1835b81b6b3SRodney W. Grimes 	,{0xE4, "Speed"}
1845b81b6b3SRodney W. Grimes 	,{0xF1, "Speed"}
1855b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
1865b81b6b3SRodney W. Grimes 	,{0xF4, "Speed"}
1875b81b6b3SRodney W. Grimes 	,{0xFF, "BBT (Bad Blocks Table)"}
1885b81b6b3SRodney W. Grimes };
1895b81b6b3SRodney W. Grimes 
1904be1e61bSAlexander Langer static void print_s0(int which);
1914be1e61bSAlexander Langer static void print_part(int i);
1924be1e61bSAlexander Langer static void init_sector0(unsigned long start);
193f46af505SJordan K. Hubbard static void init_boot(void);
1944be1e61bSAlexander Langer static void change_part(int i);
1954be1e61bSAlexander Langer static void print_params();
1964be1e61bSAlexander Langer static void change_active(int which);
19712d910e9SRobert Nordier static void change_code();
1984be1e61bSAlexander Langer static void get_params_to_use();
199e2975440SBruce Evans static void dos(int sec, int size, unsigned char *c, unsigned char *s,
200e2975440SBruce Evans 		unsigned char *h);
2014be1e61bSAlexander Langer static int open_disk(int u_flag);
2024be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
2034be1e61bSAlexander Langer static ssize_t write_disk(off_t sector, void *buf);
2044be1e61bSAlexander Langer static int get_params();
2054be1e61bSAlexander Langer static int read_s0();
2064be1e61bSAlexander Langer static int write_s0();
2074be1e61bSAlexander Langer static int ok(char *str);
2084be1e61bSAlexander Langer static int decimal(char *str, int *num, int deflt);
2094be1e61bSAlexander Langer static char *get_type(int type);
210f46af505SJordan K. Hubbard static int read_config(char *config_file);
211f46af505SJordan K. Hubbard static void reset_boot(void);
212d98b1668SPhilippe Charnier static void usage(void);
2134be1e61bSAlexander Langer #if 0
2144be1e61bSAlexander Langer static int hex(char *str, int *num, int deflt);
2154be1e61bSAlexander Langer static int string(char *str, char **ans);
2164be1e61bSAlexander Langer #endif
2175b81b6b3SRodney W. Grimes 
2184be1e61bSAlexander Langer 
2194be1e61bSAlexander Langer int
2204be1e61bSAlexander Langer main(int argc, char *argv[])
2215b81b6b3SRodney W. Grimes {
22226721a89SRobert Nordier 	int	c, i;
2235b81b6b3SRodney W. Grimes 
22410b0ee93SWarner Losh 	while ((c = getopt(argc, argv, "BIab:f:istuv1234")) != -1)
22526721a89SRobert Nordier 		switch (c) {
22626721a89SRobert Nordier 		case 'B':
22726721a89SRobert Nordier 			B_flag = 1;
2284ddd60b9SBrian Somers 			break;
22910b0ee93SWarner Losh 		case 'I':
23010b0ee93SWarner Losh 			I_flag = 1;
23110b0ee93SWarner Losh 			break;
2325b81b6b3SRodney W. Grimes 		case 'a':
2335b81b6b3SRodney W. Grimes 			a_flag = 1;
2345b81b6b3SRodney W. Grimes 			break;
23512d910e9SRobert Nordier 		case 'b':
23626721a89SRobert Nordier 			b_flag = optarg;
23712d910e9SRobert Nordier 			break;
238f46af505SJordan K. Hubbard 		case 'f':
23926721a89SRobert Nordier 			f_flag = optarg;
240f46af505SJordan K. Hubbard 			break;
2415b81b6b3SRodney W. Grimes 		case 'i':
2425b81b6b3SRodney W. Grimes 			i_flag = 1;
2435b81b6b3SRodney W. Grimes 			break;
24410b0ee93SWarner Losh 		case 's':
24510b0ee93SWarner Losh 			s_flag = 1;
24610b0ee93SWarner Losh 			break;
247f46af505SJordan K. Hubbard 		case 't':
248f46af505SJordan K. Hubbard 			t_flag = 1;
24926721a89SRobert Nordier 			break;
25026721a89SRobert Nordier 		case 'u':
25126721a89SRobert Nordier 			u_flag = 1;
25226721a89SRobert Nordier 			break;
253f46af505SJordan K. Hubbard 		case 'v':
254f46af505SJordan K. Hubbard 			v_flag = 1;
255f46af505SJordan K. Hubbard 			break;
25626721a89SRobert Nordier 		case '1':
25726721a89SRobert Nordier 		case '2':
25826721a89SRobert Nordier 		case '3':
25926721a89SRobert Nordier 		case '4':
26026721a89SRobert Nordier 			partition = c - '0';
26126721a89SRobert Nordier 			break;
2625b81b6b3SRodney W. Grimes 		default:
263d98b1668SPhilippe Charnier 			usage();
2645b81b6b3SRodney W. Grimes 		}
26526721a89SRobert Nordier 	if (f_flag || i_flag)
26626721a89SRobert Nordier 		u_flag = 1;
26726721a89SRobert Nordier 	if (t_flag)
26826721a89SRobert Nordier 		v_flag = 1;
26926721a89SRobert Nordier 	argc -= optind;
27026721a89SRobert Nordier 	argv += optind;
2715b81b6b3SRodney W. Grimes 
2725b81b6b3SRodney W. Grimes 	if (argc > 0)
273e3038c6eSJoerg Wunsch 	{
274e3038c6eSJoerg Wunsch 		static char realname[12];
275e3038c6eSJoerg Wunsch 
276e3038c6eSJoerg Wunsch 		if(strncmp(argv[0], "/dev", 4) == 0)
2775b81b6b3SRodney W. Grimes 			disk = argv[0];
278e3038c6eSJoerg Wunsch 		else
279e3038c6eSJoerg Wunsch 		{
280dc6bbfb6SDavid E. O'Brien 			snprintf(realname, 12, "/dev/%s", argv[0]);
281e3038c6eSJoerg Wunsch 			disk = realname;
282e3038c6eSJoerg Wunsch 		}
2835b81b6b3SRodney W. Grimes 
2845b81b6b3SRodney W. Grimes 		if (open_disk(u_flag) < 0)
285d98b1668SPhilippe Charnier 			err(1, "cannot open disk %s", disk);
286e3038c6eSJoerg Wunsch 	}
287e3038c6eSJoerg Wunsch 	else
288e3038c6eSJoerg Wunsch 	{
28926721a89SRobert Nordier 		int rv = 0;
290e3038c6eSJoerg Wunsch 
291e3038c6eSJoerg Wunsch 		for(i = 0; disks[i]; i++)
292e3038c6eSJoerg Wunsch 		{
293e3038c6eSJoerg Wunsch 			disk = disks[i];
294e3038c6eSJoerg Wunsch 			rv = open_disk(u_flag);
295e3038c6eSJoerg Wunsch 			if(rv != -2) break;
296e3038c6eSJoerg Wunsch 		}
297e3038c6eSJoerg Wunsch 		if(rv < 0)
298d98b1668SPhilippe Charnier 			err(1, "cannot open any disk");
299e3038c6eSJoerg Wunsch 	}
30085c2cf30SJohn Baldwin 
30185c2cf30SJohn Baldwin 	/* (abu)use mboot.bootinst to probe for the sector size */
30285c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
30385c2cf30SJohn Baldwin 		err(1, "cannot allocate buffer to determine disk sector size");
30485c2cf30SJohn Baldwin 	read_disk(0, mboot.bootinst);
30585c2cf30SJohn Baldwin 	free(mboot.bootinst);
30685c2cf30SJohn Baldwin 
30710b0ee93SWarner Losh 	if (s_flag)
30810b0ee93SWarner Losh 	{
30910b0ee93SWarner Losh 		int i;
31010b0ee93SWarner Losh 		struct dos_partition *partp;
31110b0ee93SWarner Losh 
31210b0ee93SWarner Losh 		if (read_s0())
31310b0ee93SWarner Losh 			err(1, "read_s0");
31410b0ee93SWarner Losh 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
31510b0ee93SWarner Losh 		    dos_sectors);
31610b0ee93SWarner Losh 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
31710b0ee93SWarner Losh 		for (i = 0; i < NDOSPART; i++) {
31810b0ee93SWarner Losh 			partp = ((struct dos_partition *) &mboot.parts) + i;
31910b0ee93SWarner Losh 			if (partp->dp_start == 0 && partp->dp_size == 0)
32010b0ee93SWarner Losh 				continue;
32110b0ee93SWarner Losh 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
32210b0ee93SWarner Losh 			    (u_long) partp->dp_start,
32310b0ee93SWarner Losh 			    (u_long) partp->dp_size, partp->dp_typ,
32410b0ee93SWarner Losh 			    partp->dp_flag);
32510b0ee93SWarner Losh 		}
32610b0ee93SWarner Losh 		exit(0);
32710b0ee93SWarner Losh 	}
3285b81b6b3SRodney W. Grimes 
3295b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
330f46af505SJordan K. Hubbard 
33110b0ee93SWarner Losh 	if (I_flag)
332e6fb3ddeSPoul-Henning Kamp 	{
333e6fb3ddeSPoul-Henning Kamp 		struct dos_partition *partp;
334e6fb3ddeSPoul-Henning Kamp 
335e6fb3ddeSPoul-Henning Kamp 		read_s0();
336e6fb3ddeSPoul-Henning Kamp 		reset_boot();
337e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
338e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
339e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
340e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
34185c2cf30SJohn Baldwin 		partp->dp_size = ((disksecs - dos_sectors) / dos_cylsecs) *
34285c2cf30SJohn Baldwin 			dos_cylsecs;
343e6fb3ddeSPoul-Henning Kamp 
344e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start, partp->dp_size,
345e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
346e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
347e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
348e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
349e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
350e6fb3ddeSPoul-Henning Kamp 		write_s0();
351e6fb3ddeSPoul-Henning Kamp 		exit(0);
352e6fb3ddeSPoul-Henning Kamp 	}
353f46af505SJordan K. Hubbard 	if (f_flag)
354f46af505SJordan K. Hubbard 	{
355f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
356f46af505SJordan K. Hubbard 	    {
357f46af505SJordan K. Hubbard 		reset_boot();
358f46af505SJordan K. Hubbard 	    }
359f46af505SJordan K. Hubbard 
360f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
361f46af505SJordan K. Hubbard 	    {
362f46af505SJordan K. Hubbard 		exit(1);
363f46af505SJordan K. Hubbard 	    }
364f46af505SJordan K. Hubbard 	    if (v_flag)
365f46af505SJordan K. Hubbard 	    {
366f46af505SJordan K. Hubbard 		print_s0(-1);
367f46af505SJordan K. Hubbard 	    }
368f46af505SJordan K. Hubbard 	    if (!t_flag)
369f46af505SJordan K. Hubbard 	    {
370f46af505SJordan K. Hubbard 		write_s0();
371f46af505SJordan K. Hubbard 	    }
372f46af505SJordan K. Hubbard 	}
373f46af505SJordan K. Hubbard 	else
374f46af505SJordan K. Hubbard 	{
3755b81b6b3SRodney W. Grimes 	    if(u_flag)
3765b81b6b3SRodney W. Grimes 	    {
3775b81b6b3SRodney W. Grimes 		get_params_to_use();
3785b81b6b3SRodney W. Grimes 	    }
3795b81b6b3SRodney W. Grimes 	    else
3805b81b6b3SRodney W. Grimes 	    {
3815b81b6b3SRodney W. Grimes 		print_params();
3825b81b6b3SRodney W. Grimes 	    }
3835b81b6b3SRodney W. Grimes 
3845b81b6b3SRodney W. Grimes 	    if (read_s0())
3855b81b6b3SRodney W. Grimes 		init_sector0(1);
3865b81b6b3SRodney W. Grimes 
3877cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3885b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3895b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3905b81b6b3SRodney W. Grimes 	    if (partition == -1)
3914ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3925b81b6b3SRodney W. Grimes 		    change_part(i);
3935b81b6b3SRodney W. Grimes 	    else
3945b81b6b3SRodney W. Grimes 		change_part(partition);
3955b81b6b3SRodney W. Grimes 
3965b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3975b81b6b3SRodney W. Grimes 		change_active(partition);
3985b81b6b3SRodney W. Grimes 
39926721a89SRobert Nordier 	    if (B_flag)
40012d910e9SRobert Nordier 		change_code();
40112d910e9SRobert Nordier 
40226721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
403f46af505SJordan K. Hubbard 		if (!t_flag)
404f46af505SJordan K. Hubbard 		{
4055b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
4065b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
407f46af505SJordan K. Hubbard 		}
4085b81b6b3SRodney W. Grimes 		print_s0(-1);
409f46af505SJordan K. Hubbard 		if (!t_flag)
410f46af505SJordan K. Hubbard 		{
4115b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
4125b81b6b3SRodney W. Grimes 			write_s0();
4135b81b6b3SRodney W. Grimes 		}
414f46af505SJordan K. Hubbard 		else
415f46af505SJordan K. Hubbard 		{
416f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
417f46af505SJordan K. Hubbard 		}
418f46af505SJordan K. Hubbard 	    }
419f46af505SJordan K. Hubbard 	}
4205b81b6b3SRodney W. Grimes 
4215b81b6b3SRodney W. Grimes 	exit(0);
422d98b1668SPhilippe Charnier }
4235b81b6b3SRodney W. Grimes 
424d98b1668SPhilippe Charnier static void
425d98b1668SPhilippe Charnier usage()
426d98b1668SPhilippe Charnier {
42726721a89SRobert Nordier 	fprintf(stderr, "%s%s",
428ed1235adSJohn Baldwin 		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
42926721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
430d98b1668SPhilippe Charnier         exit(1);
4315b81b6b3SRodney W. Grimes }
4325b81b6b3SRodney W. Grimes 
4334be1e61bSAlexander Langer static void
4344be1e61bSAlexander Langer print_s0(int which)
4355b81b6b3SRodney W. Grimes {
4365b81b6b3SRodney W. Grimes int	i;
4375b81b6b3SRodney W. Grimes 
4385b81b6b3SRodney W. Grimes 	print_params();
4395b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4405b81b6b3SRodney W. Grimes 	if (which == -1)
4414ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4425b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4435b81b6b3SRodney W. Grimes 	else
4445b81b6b3SRodney W. Grimes 		print_part(which);
4455b81b6b3SRodney W. Grimes }
4465b81b6b3SRodney W. Grimes 
4475b81b6b3SRodney W. Grimes static struct dos_partition mtpart = { 0 };
4485b81b6b3SRodney W. Grimes 
4494be1e61bSAlexander Langer static void
4504be1e61bSAlexander Langer print_part(int i)
4515b81b6b3SRodney W. Grimes {
452637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
453637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4545b81b6b3SRodney W. Grimes 
4554ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4565b81b6b3SRodney W. Grimes 
4575b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4585b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4595b81b6b3SRodney W. Grimes 		return;
4605b81b6b3SRodney W. Grimes 	}
461637fe2f7SJustin T. Gibbs 	/*
462637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
463637fe2f7SJustin T. Gibbs 	 */
464637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
465637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
466637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
4675b81b6b3SRodney W. Grimes 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
468ba198492SBruce Evans 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
469ba198492SBruce Evans 		(u_long)partp->dp_start,
470ba198492SBruce Evans 		(u_long)partp->dp_size,
471637fe2f7SJustin T. Gibbs 		part_mb,
472680426beSDavid E. O'Brien 		partp->dp_flag,
473680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4745b81b6b3SRodney W. Grimes 	printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
4755b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4765b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_ssect)
4775b81b6b3SRodney W. Grimes 		,partp->dp_shd
4785b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4795b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_esect)
4805b81b6b3SRodney W. Grimes 		,partp->dp_ehd);
4815b81b6b3SRodney W. Grimes }
4825b81b6b3SRodney W. Grimes 
483f46af505SJordan K. Hubbard 
484f46af505SJordan K. Hubbard static void
485f46af505SJordan K. Hubbard init_boot(void)
486f46af505SJordan K. Hubbard {
48726721a89SRobert Nordier 	const char *fname;
48885c2cf30SJohn Baldwin 	int fd, n;
48985c2cf30SJohn Baldwin 	struct stat sb;
49026721a89SRobert Nordier 
49126721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
49226721a89SRobert Nordier 	if ((fd = open(fname, O_RDONLY)) == -1 ||
49385c2cf30SJohn Baldwin 	    fstat(fd, &sb) == -1)
49485c2cf30SJohn Baldwin 		err(1, "%s", fname);
49585c2cf30SJohn Baldwin 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
49685c2cf30SJohn Baldwin 		errx(1, "%s: length must be a multiple of sector size", fname);
49785c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
49885c2cf30SJohn Baldwin 		errx(1, "%s: unable to allocate read buffer", fname);
49985c2cf30SJohn Baldwin 	if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
50026721a89SRobert Nordier 	    close(fd))
50126721a89SRobert Nordier 		err(1, "%s", fname);
50285c2cf30SJohn Baldwin 	if (n != mboot.bootinst_size)
50385c2cf30SJohn Baldwin 		errx(1, "%s: short read", fname);
504f46af505SJordan K. Hubbard }
505f46af505SJordan K. Hubbard 
506f46af505SJordan K. Hubbard 
5074be1e61bSAlexander Langer static void
5084be1e61bSAlexander Langer init_sector0(unsigned long start)
5095b81b6b3SRodney W. Grimes {
5105b81b6b3SRodney W. Grimes struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
51185c2cf30SJohn Baldwin unsigned long size;
5125b81b6b3SRodney W. Grimes 
513f46af505SJordan K. Hubbard 	init_boot();
5145b81b6b3SRodney W. Grimes 
5155b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
5165b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
51785c2cf30SJohn Baldwin 	/* ensure cylinder boundaries */
51885c2cf30SJohn Baldwin 	start = (start / dos_sectors) * dos_sectors;
51985c2cf30SJohn Baldwin 	if(start == 0)
52085c2cf30SJohn Baldwin 		start = dos_sectors;
52185c2cf30SJohn Baldwin 	size = ((disksecs - start) / dos_cylsecs) * dos_cylsecs;
5225b81b6b3SRodney W. Grimes 	partp->dp_start = start;
5235b81b6b3SRodney W. Grimes 	partp->dp_size = size;
5245b81b6b3SRodney W. Grimes 
525e2975440SBruce Evans 	dos(partp->dp_start, partp->dp_size,
526e2975440SBruce Evans 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
527e2975440SBruce Evans 	dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
528e2975440SBruce Evans 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5295b81b6b3SRodney W. Grimes }
5305b81b6b3SRodney W. Grimes 
5314be1e61bSAlexander Langer static void
5324be1e61bSAlexander Langer change_part(int i)
5335b81b6b3SRodney W. Grimes {
5344ddd60b9SBrian Somers struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
5355b81b6b3SRodney W. Grimes 
5365b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
5375b81b6b3SRodney W. Grimes     print_part(i);
5385b81b6b3SRodney W. Grimes 
5395b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
5405b81b6b3SRodney W. Grimes 	int tmp;
5415b81b6b3SRodney W. Grimes 
5425b81b6b3SRodney W. Grimes 	if (i_flag) {
5435b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
5444ddd60b9SBrian Somers 		if (i == 4) {
5455b81b6b3SRodney W. Grimes 			init_sector0(1);
5464ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
5475b81b6b3SRodney W. Grimes 			print_part(i);
5485b81b6b3SRodney W. Grimes 		}
5495b81b6b3SRodney W. Grimes 	}
5505b81b6b3SRodney W. Grimes 
5515b81b6b3SRodney W. Grimes 	do {
552680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5535b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5545b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
5555b81b6b3SRodney W. Grimes 
5564b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5575b81b6b3SRodney W. Grimes 		{
5585b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5595b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5605b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5615b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5625b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5635b81b6b3SRodney W. Grimes 			Decimal("beginning head", thd, tmp);
5645b81b6b3SRodney W. Grimes 			Decimal("beginning sector", tsec, tmp);
5655b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5665b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5675b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5685b81b6b3SRodney W. Grimes 
5695b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5705b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5715b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5725b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5735b81b6b3SRodney W. Grimes 			Decimal("ending head", thd, tmp);
5745b81b6b3SRodney W. Grimes 			Decimal("ending sector", tsec, tmp);
5755b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5765b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5775b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
5785b81b6b3SRodney W. Grimes 		} else {
57985c2cf30SJohn Baldwin 			if(partp->dp_start % dos_sectors != 0) {
58085c2cf30SJohn Baldwin 				printf("Adjusting partition to start at a "
58185c2cf30SJohn Baldwin 				   "cylinder boundary\n");
58285c2cf30SJohn Baldwin 				partp->dp_start =
58385c2cf30SJohn Baldwin 				    (partp->dp_start / dos_sectors) *
58485c2cf30SJohn Baldwin 				    dos_sectors;
58585c2cf30SJohn Baldwin 			}
58685c2cf30SJohn Baldwin 			if(partp->dp_size % dos_cylsecs != 0) {
58785c2cf30SJohn Baldwin 				printf("Adjusting partition to end at a "
58885c2cf30SJohn Baldwin 				    "cylinder boundary\n");
58985c2cf30SJohn Baldwin 				partp->dp_size =
59085c2cf30SJohn Baldwin 				    (partp->dp_size / dos_cylsecs) *
59185c2cf30SJohn Baldwin 				    dos_cylsecs;
59285c2cf30SJohn Baldwin 			}
593e2975440SBruce Evans 			dos(partp->dp_start, partp->dp_size,
5945b81b6b3SRodney W. Grimes 			    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
595e2975440SBruce Evans 			dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
5965b81b6b3SRodney W. Grimes 			    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5975b81b6b3SRodney W. Grimes 		}
5985b81b6b3SRodney W. Grimes 
5995b81b6b3SRodney W. Grimes 		print_part(i);
6005b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
6015b81b6b3SRodney W. Grimes     }
6025b81b6b3SRodney W. Grimes }
6035b81b6b3SRodney W. Grimes 
6044be1e61bSAlexander Langer static void
6055b81b6b3SRodney W. Grimes print_params()
6065b81b6b3SRodney W. Grimes {
6075b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
6085b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
6095b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
6105b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
6115b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
6125b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
6135b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
6145b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
6155b81b6b3SRodney W. Grimes }
6165b81b6b3SRodney W. Grimes 
6174be1e61bSAlexander Langer static void
6184be1e61bSAlexander Langer change_active(int which)
6195b81b6b3SRodney W. Grimes {
6205b81b6b3SRodney W. Grimes int i;
6214ddd60b9SBrian Somers int active = 4, tmp;
6225b81b6b3SRodney W. Grimes struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
6235b81b6b3SRodney W. Grimes 
6245b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
6255b81b6b3SRodney W. Grimes 		active = which;
6260b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
6270b461cd7SBruce Evans 		return;
628680426beSDavid E. O'Brien setactive:
629680426beSDavid E. O'Brien 	active = 4;
630680426beSDavid E. O'Brien 	do {
6315b81b6b3SRodney W. Grimes 		Decimal("active partition", active, tmp);
632680426beSDavid E. O'Brien 		if (active < 1 || 4 < active) {
633680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
634680426beSDavid E. O'Brien 					"  Try again.\n");
635680426beSDavid E. O'Brien 			goto setactive;
636680426beSDavid E. O'Brien 		}
637680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
6385b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
6395b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
6404ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
6414ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
6425b81b6b3SRodney W. Grimes }
6435b81b6b3SRodney W. Grimes 
64412d910e9SRobert Nordier static void
64512d910e9SRobert Nordier change_code()
64612d910e9SRobert Nordier {
64712d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
64812d910e9SRobert Nordier 		init_boot();
64912d910e9SRobert Nordier }
65012d910e9SRobert Nordier 
6514be1e61bSAlexander Langer void
6525b81b6b3SRodney W. Grimes get_params_to_use()
6535b81b6b3SRodney W. Grimes {
6545b81b6b3SRodney W. Grimes 	int	tmp;
6555b81b6b3SRodney W. Grimes 	print_params();
6565b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
6575b81b6b3SRodney W. Grimes 	{
6585b81b6b3SRodney W. Grimes 		do
6595b81b6b3SRodney W. Grimes 		{
6605b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
6615b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
6625b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6635b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6645b81b6b3SRodney W. Grimes 			print_params();
6655b81b6b3SRodney W. Grimes 		}
6665b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6675b81b6b3SRodney W. Grimes 	}
6685b81b6b3SRodney W. Grimes }
6695b81b6b3SRodney W. Grimes 
670f46af505SJordan K. Hubbard 
6715b81b6b3SRodney W. Grimes /***********************************************\
6725b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6735b81b6b3SRodney W. Grimes \***********************************************/
6744be1e61bSAlexander Langer static void
675e2975440SBruce Evans dos(sec, size, c, s, h)
676e2975440SBruce Evans int sec, size;
6775b81b6b3SRodney W. Grimes unsigned char *c, *s, *h;
6785b81b6b3SRodney W. Grimes {
6795b81b6b3SRodney W. Grimes int cy;
6805b81b6b3SRodney W. Grimes int hd;
6815b81b6b3SRodney W. Grimes 
682e2975440SBruce Evans 	if (sec == 0 && size == 0) {
6830b461cd7SBruce Evans 		*s = *c = *h = 0;
6840b461cd7SBruce Evans 		return;
6850b461cd7SBruce Evans 	}
6860b461cd7SBruce Evans 
6875b81b6b3SRodney W. Grimes 	cy = sec / ( dos_cylsecs );
6885b81b6b3SRodney W. Grimes 	sec = sec - cy * ( dos_cylsecs );
6895b81b6b3SRodney W. Grimes 
6905b81b6b3SRodney W. Grimes 	hd = sec / dos_sectors;
6915b81b6b3SRodney W. Grimes 	sec = (sec - hd * dos_sectors) + 1;
6925b81b6b3SRodney W. Grimes 
6935b81b6b3SRodney W. Grimes 	*h = hd;
6945b81b6b3SRodney W. Grimes 	*c = cy & 0xff;
6955b81b6b3SRodney W. Grimes 	*s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
6965b81b6b3SRodney W. Grimes }
6975b81b6b3SRodney W. Grimes 
6985b81b6b3SRodney W. Grimes int fd;
6995b81b6b3SRodney W. Grimes 
7005b81b6b3SRodney W. Grimes 	/* Getting device status */
7015b81b6b3SRodney W. Grimes 
7024be1e61bSAlexander Langer static int
7034be1e61bSAlexander Langer open_disk(int u_flag)
7045b81b6b3SRodney W. Grimes {
7055b81b6b3SRodney W. Grimes struct stat 	st;
7065b81b6b3SRodney W. Grimes 
7075b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
708d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
7095b81b6b3SRodney W. Grimes 		return -1;
710b60eb395SBruce Evans 	}
711b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
712d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
71312d910e9SRobert Nordier 	if ((fd = open(disk,
71410b0ee93SWarner Losh 	    a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
715e3038c6eSJoerg Wunsch 		if(errno == ENXIO)
716e3038c6eSJoerg Wunsch 			return -2;
717d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
7185b81b6b3SRodney W. Grimes 		return -1;
7195b81b6b3SRodney W. Grimes 	}
7205b81b6b3SRodney W. Grimes 	if (get_params(0) == -1) {
721d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
7225b81b6b3SRodney W. Grimes 		return -1;
7235b81b6b3SRodney W. Grimes 	}
7245b81b6b3SRodney W. Grimes 	return fd;
7255b81b6b3SRodney W. Grimes }
7265b81b6b3SRodney W. Grimes 
7274be1e61bSAlexander Langer static ssize_t
7284be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
7295b81b6b3SRodney W. Grimes {
7305b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7317cb29d33SSøren Schmidt 	if( secsize == 0 )
7327cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
7337cb29d33SSøren Schmidt 			{
7347cb29d33SSøren Schmidt 			/* try the read */
7357cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
7367cb29d33SSøren Schmidt 			if( size == secsize )
7377cb29d33SSøren Schmidt 				/* it worked so return */
7387cb29d33SSøren Schmidt 				return secsize;
7397cb29d33SSøren Schmidt 			}
7407cb29d33SSøren Schmidt 	else
7417cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
7427cb29d33SSøren Schmidt 
7437cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
7447cb29d33SSøren Schmidt 	return -1;
7455b81b6b3SRodney W. Grimes }
7465b81b6b3SRodney W. Grimes 
7474be1e61bSAlexander Langer static ssize_t
7484be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
7495b81b6b3SRodney W. Grimes {
7505b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7517cb29d33SSøren Schmidt 	/* write out in the size that the read_disk found worked */
7527cb29d33SSøren Schmidt 	return write(fd, buf, secsize);
7535b81b6b3SRodney W. Grimes }
7545b81b6b3SRodney W. Grimes 
7554be1e61bSAlexander Langer static int
7564be1e61bSAlexander Langer get_params()
7575b81b6b3SRodney W. Grimes {
7585b81b6b3SRodney W. Grimes 
7595b81b6b3SRodney W. Grimes     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
760d98b1668SPhilippe Charnier 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
761b60eb395SBruce Evans 	dos_cyls = cyls = 1;
762b60eb395SBruce Evans 	dos_heads = heads = 1;
763b60eb395SBruce Evans 	dos_sectors = sectors = 1;
764b60eb395SBruce Evans 	dos_cylsecs = cylsecs = heads * sectors;
765b60eb395SBruce Evans 	disksecs = cyls * heads * sectors;
766b60eb395SBruce Evans 	return disksecs;
7675b81b6b3SRodney W. Grimes     }
7685b81b6b3SRodney W. Grimes 
7695b81b6b3SRodney W. Grimes     dos_cyls = cyls = disklabel.d_ncylinders;
7705b81b6b3SRodney W. Grimes     dos_heads = heads = disklabel.d_ntracks;
7715b81b6b3SRodney W. Grimes     dos_sectors = sectors = disklabel.d_nsectors;
7725b81b6b3SRodney W. Grimes     dos_cylsecs = cylsecs = heads * sectors;
7735b81b6b3SRodney W. Grimes     disksecs = cyls * heads * sectors;
7745b81b6b3SRodney W. Grimes 
7755b81b6b3SRodney W. Grimes     return (disksecs);
7765b81b6b3SRodney W. Grimes }
7775b81b6b3SRodney W. Grimes 
7785b81b6b3SRodney W. Grimes 
7794be1e61bSAlexander Langer static int
7805b81b6b3SRodney W. Grimes read_s0()
7815b81b6b3SRodney W. Grimes {
78285c2cf30SJohn Baldwin 	if (mboot.bootinst != NULL) {
78385c2cf30SJohn Baldwin 		free(mboot.bootinst);
78485c2cf30SJohn Baldwin 		mboot.bootinst_size = secsize;
78585c2cf30SJohn Baldwin 		if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
78685c2cf30SJohn Baldwin 			warnx("unable to allocate buffer to read fdisk "
78785c2cf30SJohn Baldwin 			      "partition table");
78885c2cf30SJohn Baldwin 			return -1;
78985c2cf30SJohn Baldwin 		}
79085c2cf30SJohn Baldwin 	}
79185c2cf30SJohn Baldwin 	if (read_disk(0, mboot.bootinst) == -1) {
792d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
7935b81b6b3SRodney W. Grimes 		return -1;
7945b81b6b3SRodney W. Grimes 	}
79585c2cf30SJohn Baldwin 	if (*(int *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
796d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
7975b81b6b3SRodney W. Grimes 		/* So should we initialize things */
7985b81b6b3SRodney W. Grimes 		return -1;
7995b81b6b3SRodney W. Grimes 	}
80085c2cf30SJohn Baldwin 	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
8015b81b6b3SRodney W. Grimes 	return 0;
8025b81b6b3SRodney W. Grimes }
8035b81b6b3SRodney W. Grimes 
8044be1e61bSAlexander Langer static int
8055b81b6b3SRodney W. Grimes write_s0()
8065b81b6b3SRodney W. Grimes {
807cefdc4efSBill Fumerola #ifdef NOT_NOW
8085b81b6b3SRodney W. Grimes 	int	flag;
809cefdc4efSBill Fumerola #endif
81085c2cf30SJohn Baldwin 	int	sector;
81185c2cf30SJohn Baldwin 
8125b81b6b3SRodney W. Grimes 	if (iotest) {
8135b81b6b3SRodney W. Grimes 		print_s0(-1);
8145b81b6b3SRodney W. Grimes 		return 0;
8155b81b6b3SRodney W. Grimes 	}
81685c2cf30SJohn Baldwin 	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
8175b81b6b3SRodney W. Grimes 	/*
8185b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
8195b81b6b3SRodney W. Grimes 	 * disable after writing.
8205b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
8215b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
8225b81b6b3SRodney W. Grimes 	 */
823ba3551dfSJulian Elischer #ifdef NOT_NOW
824e6fb3ddeSPoul-Henning Kamp 	flag = 1;
8255b81b6b3SRodney W. Grimes 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
826d98b1668SPhilippe Charnier 		warn("ioctl DIOCWLABEL");
827ba3551dfSJulian Elischer #endif
82885c2cf30SJohn Baldwin 	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
82985c2cf30SJohn Baldwin 		if (write_disk(sector,
83085c2cf30SJohn Baldwin 			       &mboot.bootinst[sector * secsize]) == -1) {
831e6fb3ddeSPoul-Henning Kamp 			warn("can't write fdisk partition table");
8325b81b6b3SRodney W. Grimes 			return -1;
833ba3551dfSJulian Elischer #ifdef NOT_NOW
834e6fb3ddeSPoul-Henning Kamp 			flag = 0;
8355b81b6b3SRodney W. Grimes 			(void) ioctl(fd, DIOCWLABEL, &flag);
836ba3551dfSJulian Elischer #endif
8375b81b6b3SRodney W. Grimes 		}
83885c2cf30SJohn Baldwin #ifdef NOT_NOW
83985c2cf30SJohn Baldwin 	flag = 0;
84085c2cf30SJohn Baldwin 	(void) ioctl(fd, DIOCWLABEL, &flag);
84185c2cf30SJohn Baldwin #endif
8424be1e61bSAlexander Langer 	return(0);
8435b81b6b3SRodney W. Grimes }
8445b81b6b3SRodney W. Grimes 
8455b81b6b3SRodney W. Grimes 
8464be1e61bSAlexander Langer static int
8475b81b6b3SRodney W. Grimes ok(str)
8485b81b6b3SRodney W. Grimes char *str;
8495b81b6b3SRodney W. Grimes {
8505b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
8515b81b6b3SRodney W. Grimes 	fgets(lbuf, LBUF, stdin);
8525b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
8535b81b6b3SRodney W. Grimes 
8545b81b6b3SRodney W. Grimes 	if (*lbuf &&
8555b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
8565b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
8575b81b6b3SRodney W. Grimes 		return 1;
8585b81b6b3SRodney W. Grimes 	else
8595b81b6b3SRodney W. Grimes 		return 0;
8605b81b6b3SRodney W. Grimes }
8615b81b6b3SRodney W. Grimes 
8624be1e61bSAlexander Langer static int
8634be1e61bSAlexander Langer decimal(char *str, int *num, int deflt)
8645b81b6b3SRodney W. Grimes {
8655b81b6b3SRodney W. Grimes int acc = 0, c;
8665b81b6b3SRodney W. Grimes char *cp;
8675b81b6b3SRodney W. Grimes 
8685b81b6b3SRodney W. Grimes 	while (1) {
8695b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
8705b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8715b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8725b81b6b3SRodney W. Grimes 
8735b81b6b3SRodney W. Grimes 		if (!*lbuf)
8745b81b6b3SRodney W. Grimes 			return 0;
8755b81b6b3SRodney W. Grimes 
8765b81b6b3SRodney W. Grimes 		cp = lbuf;
8775b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8785b81b6b3SRodney W. Grimes 		if (!c)
8795b81b6b3SRodney W. Grimes 			return 0;
8804be1e61bSAlexander Langer 		while ((c = *cp++)) {
8815b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8825b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
8835b81b6b3SRodney W. Grimes 			else
8845b81b6b3SRodney W. Grimes 				break;
8855b81b6b3SRodney W. Grimes 		}
8865b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8875b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8885b81b6b3SRodney W. Grimes 		if (!c) {
8895b81b6b3SRodney W. Grimes 			*num = acc;
8905b81b6b3SRodney W. Grimes 			return 1;
8915b81b6b3SRodney W. Grimes 		} else
892680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
8935b81b6b3SRodney W. Grimes 				lbuf);
8945b81b6b3SRodney W. Grimes 	}
8955b81b6b3SRodney W. Grimes 
8965b81b6b3SRodney W. Grimes }
8975b81b6b3SRodney W. Grimes 
8984be1e61bSAlexander Langer #if 0
8994be1e61bSAlexander Langer static int
9004be1e61bSAlexander Langer hex(char *str, int *num, int deflt)
9015b81b6b3SRodney W. Grimes {
9025b81b6b3SRodney W. Grimes int acc = 0, c;
9035b81b6b3SRodney W. Grimes char *cp;
9045b81b6b3SRodney W. Grimes 
9055b81b6b3SRodney W. Grimes 	while (1) {
9065b81b6b3SRodney W. Grimes 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
9075b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
9085b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
9095b81b6b3SRodney W. Grimes 
9105b81b6b3SRodney W. Grimes 		if (!*lbuf)
9115b81b6b3SRodney W. Grimes 			return 0;
9125b81b6b3SRodney W. Grimes 
9135b81b6b3SRodney W. Grimes 		cp = lbuf;
9145b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9155b81b6b3SRodney W. Grimes 		if (!c)
9165b81b6b3SRodney W. Grimes 			return 0;
9174be1e61bSAlexander Langer 		while ((c = *cp++)) {
9185b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
9195b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - '0';
9205b81b6b3SRodney W. Grimes 			else if (c <= 'f' && c >= 'a')
9215b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'a' + 10;
9225b81b6b3SRodney W. Grimes 			else if (c <= 'F' && c >= 'A')
9235b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'A' + 10;
9245b81b6b3SRodney W. Grimes 			else
9255b81b6b3SRodney W. Grimes 				break;
9265b81b6b3SRodney W. Grimes 		}
9275b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
9285b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9295b81b6b3SRodney W. Grimes 		if (!c) {
9305b81b6b3SRodney W. Grimes 			*num = acc;
9315b81b6b3SRodney W. Grimes 			return 1;
9325b81b6b3SRodney W. Grimes 		} else
933680426beSDavid E. O'Brien 			printf("%s is an invalid hex number.  Try again.\n",
9345b81b6b3SRodney W. Grimes 				lbuf);
9355b81b6b3SRodney W. Grimes 	}
9365b81b6b3SRodney W. Grimes 
9375b81b6b3SRodney W. Grimes }
9385b81b6b3SRodney W. Grimes 
9394be1e61bSAlexander Langer static int
9404be1e61bSAlexander Langer string(char *str, char **ans)
9415b81b6b3SRodney W. Grimes {
9425b81b6b3SRodney W. Grimes int c;
9435b81b6b3SRodney W. Grimes char *cp = lbuf;
9445b81b6b3SRodney W. Grimes 
9455b81b6b3SRodney W. Grimes 	while (1) {
9465b81b6b3SRodney W. Grimes 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
9475b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
9485b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
9495b81b6b3SRodney W. Grimes 
9505b81b6b3SRodney W. Grimes 		if (!*lbuf)
9515b81b6b3SRodney W. Grimes 			return 0;
9525b81b6b3SRodney W. Grimes 
9535b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9545b81b6b3SRodney W. Grimes 		if (c == '"') {
9555b81b6b3SRodney W. Grimes 			c = *++cp;
9565b81b6b3SRodney W. Grimes 			*ans = cp;
9575b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != '"') cp++;
9585b81b6b3SRodney W. Grimes 		} else {
9595b81b6b3SRodney W. Grimes 			*ans = cp;
9605b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
9615b81b6b3SRodney W. Grimes 		}
9625b81b6b3SRodney W. Grimes 
9635b81b6b3SRodney W. Grimes 		if (c)
9645b81b6b3SRodney W. Grimes 			*cp = 0;
9655b81b6b3SRodney W. Grimes 		return 1;
9665b81b6b3SRodney W. Grimes 	}
9675b81b6b3SRodney W. Grimes }
9684be1e61bSAlexander Langer #endif
9695b81b6b3SRodney W. Grimes 
9704be1e61bSAlexander Langer static char *
9714be1e61bSAlexander Langer get_type(int type)
9725b81b6b3SRodney W. Grimes {
9735b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
9745b81b6b3SRodney W. Grimes 	int	counter = 0;
9755b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
9765b81b6b3SRodney W. Grimes 
9775b81b6b3SRodney W. Grimes 
9785b81b6b3SRodney W. Grimes 	while(counter < numentries)
9795b81b6b3SRodney W. Grimes 	{
9805b81b6b3SRodney W. Grimes 		if(ptr->type == type)
9815b81b6b3SRodney W. Grimes 		{
9825b81b6b3SRodney W. Grimes 			return(ptr->name);
9835b81b6b3SRodney W. Grimes 		}
9845b81b6b3SRodney W. Grimes 		ptr++;
9855b81b6b3SRodney W. Grimes 		counter++;
9865b81b6b3SRodney W. Grimes 	}
9875b81b6b3SRodney W. Grimes 	return("unknown");
9885b81b6b3SRodney W. Grimes }
989f46af505SJordan K. Hubbard 
990f46af505SJordan K. Hubbard 
991f46af505SJordan K. Hubbard static void
992f46af505SJordan K. Hubbard parse_config_line(line, command)
993f46af505SJordan K. Hubbard     char	*line;
994f46af505SJordan K. Hubbard     CMD		*command;
995f46af505SJordan K. Hubbard {
996f46af505SJordan K. Hubbard     char	*cp, *end;
997f46af505SJordan K. Hubbard 
998f46af505SJordan K. Hubbard     cp = line;
999f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1000f46af505SJordan K. Hubbard 		   function */
1001f46af505SJordan K. Hubbard     {
1002f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
1003f46af505SJordan K. Hubbard 
1004f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
1005f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
1006f46af505SJordan K. Hubbard 	{
1007f46af505SJordan K. Hubbard 	    break;
1008f46af505SJordan K. Hubbard 	}
1009f46af505SJordan K. Hubbard 	command->cmd = *cp++;
1010f46af505SJordan K. Hubbard 
1011f46af505SJordan K. Hubbard 	/*
1012f46af505SJordan K. Hubbard 	 * Parse args
1013f46af505SJordan K. Hubbard 	 */
1014f46af505SJordan K. Hubbard 	while (1)
1015f46af505SJordan K. Hubbard 	{
1016f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
1017f46af505SJordan K. Hubbard 	    if (*cp == '#')
1018f46af505SJordan K. Hubbard 	    {
1019f46af505SJordan K. Hubbard 		break;		/* found comment */
1020f46af505SJordan K. Hubbard 	    }
1021f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
1022f46af505SJordan K. Hubbard 	    {
1023f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
1024f46af505SJordan K. Hubbard 	    }
1025f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
1026f46af505SJordan K. Hubbard 	    {
1027f46af505SJordan K. Hubbard 		break;		/* assume end of line */
1028f46af505SJordan K. Hubbard 	    }
1029f46af505SJordan K. Hubbard 	    end = NULL;
1030f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1031f46af505SJordan K. Hubbard 	    if (cp == end)
1032f46af505SJordan K. Hubbard 	    {
1033f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
1034f46af505SJordan K. Hubbard 	    }
1035f46af505SJordan K. Hubbard 	    cp = end;
1036f46af505SJordan K. Hubbard 	    command->n_args++;
1037f46af505SJordan K. Hubbard 	}
1038f46af505SJordan K. Hubbard 	break;
1039f46af505SJordan K. Hubbard     }
1040f46af505SJordan K. Hubbard }
1041f46af505SJordan K. Hubbard 
1042f46af505SJordan K. Hubbard 
1043f46af505SJordan K. Hubbard static int
1044f46af505SJordan K. Hubbard process_geometry(command)
1045f46af505SJordan K. Hubbard     CMD		*command;
1046f46af505SJordan K. Hubbard {
1047f46af505SJordan K. Hubbard     int		status = 1, i;
1048f46af505SJordan K. Hubbard 
1049f46af505SJordan K. Hubbard     while (1)
1050f46af505SJordan K. Hubbard     {
1051f46af505SJordan K. Hubbard 	geom_processed = 1;
1052f46af505SJordan K. Hubbard 	if (part_processed)
1053f46af505SJordan K. Hubbard 	{
1054d98b1668SPhilippe Charnier 	    warnx(
1055d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
1056d98b1668SPhilippe Charnier     all partition specifications",
1057d98b1668SPhilippe Charnier 		    current_line_number);
1058f46af505SJordan K. Hubbard 	    status = 0;
1059f46af505SJordan K. Hubbard 	    break;
1060f46af505SJordan K. Hubbard 	}
1061f46af505SJordan K. Hubbard 	if (command->n_args != 3)
1062f46af505SJordan K. Hubbard 	{
1063d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
1064d98b1668SPhilippe Charnier 		    current_line_number);
1065f46af505SJordan K. Hubbard 	    status = 0;
1066f46af505SJordan K. Hubbard 	    break;
1067f46af505SJordan K. Hubbard 	}
1068f46af505SJordan K. Hubbard 	dos_cyls = -1;
1069f46af505SJordan K. Hubbard 	dos_heads = -1;
1070f46af505SJordan K. Hubbard 	dos_sectors = -1;
1071f46af505SJordan K. Hubbard 	for (i = 0; i < 3; ++i)
1072f46af505SJordan K. Hubbard 	{
1073f46af505SJordan K. Hubbard 	    switch (command->args[i].argtype)
1074f46af505SJordan K. Hubbard 	    {
1075f46af505SJordan K. Hubbard 	    case 'c':
1076f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
1077f46af505SJordan K. Hubbard 		break;
1078f46af505SJordan K. Hubbard 	    case 'h':
1079f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
1080f46af505SJordan K. Hubbard 		break;
1081f46af505SJordan K. Hubbard 	    case 's':
1082f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
1083f46af505SJordan K. Hubbard 		break;
1084f46af505SJordan K. Hubbard 	    default:
1085d98b1668SPhilippe Charnier 		warnx(
1086d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1087d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1088f46af505SJordan K. Hubbard 			command->args[i].argtype);
1089f46af505SJordan K. Hubbard 		status = 0;
1090f46af505SJordan K. Hubbard 		break;
1091f46af505SJordan K. Hubbard 	    }
1092f46af505SJordan K. Hubbard 	}
1093f46af505SJordan K. Hubbard 	if (status == 0)
1094f46af505SJordan K. Hubbard 	{
1095f46af505SJordan K. Hubbard 	    break;
1096f46af505SJordan K. Hubbard 	}
1097f46af505SJordan K. Hubbard 
1098f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1099f46af505SJordan K. Hubbard 
1100f46af505SJordan K. Hubbard 	/*
1101f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1102f46af505SJordan K. Hubbard 	 */
1103f46af505SJordan K. Hubbard 	if (dos_cyls < 0)
1104f46af505SJordan K. Hubbard 	{
1105d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1106d98b1668SPhilippe Charnier 		    current_line_number);
1107f46af505SJordan K. Hubbard 	    status = 0;
1108f46af505SJordan K. Hubbard 	}
1109f46af505SJordan K. Hubbard 	if (dos_cyls == 0 || dos_cyls > 1024)
1110f46af505SJordan K. Hubbard 	{
1111d98b1668SPhilippe Charnier 	    warnx(
1112d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1113f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1114d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1115d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1116f46af505SJordan K. Hubbard 	}
1117f46af505SJordan K. Hubbard 
1118f46af505SJordan K. Hubbard 	if (dos_heads < 0)
1119f46af505SJordan K. Hubbard 	{
1120d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1121d98b1668SPhilippe Charnier 		    current_line_number);
1122f46af505SJordan K. Hubbard 	    status = 0;
1123f46af505SJordan K. Hubbard 	}
1124f46af505SJordan K. Hubbard 	else if (dos_heads < 1 || dos_heads > 256)
1125f46af505SJordan K. Hubbard 	{
1126d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1127d98b1668SPhilippe Charnier 		    current_line_number);
1128f46af505SJordan K. Hubbard 	    status = 0;
1129f46af505SJordan K. Hubbard 	}
1130f46af505SJordan K. Hubbard 
1131f46af505SJordan K. Hubbard 	if (dos_sectors < 0)
1132f46af505SJordan K. Hubbard 	{
1133d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1134d98b1668SPhilippe Charnier 		    current_line_number);
1135f46af505SJordan K. Hubbard 	    status = 0;
1136f46af505SJordan K. Hubbard 	}
1137f46af505SJordan K. Hubbard 	else if (dos_sectors < 1 || dos_sectors > 63)
1138f46af505SJordan K. Hubbard 	{
1139d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1140d98b1668SPhilippe Charnier 		    current_line_number);
1141f46af505SJordan K. Hubbard 	    status = 0;
1142f46af505SJordan K. Hubbard 	}
1143f46af505SJordan K. Hubbard 
1144f46af505SJordan K. Hubbard 	break;
1145f46af505SJordan K. Hubbard     }
1146f46af505SJordan K. Hubbard     return (status);
1147f46af505SJordan K. Hubbard }
1148f46af505SJordan K. Hubbard 
1149f46af505SJordan K. Hubbard 
1150f46af505SJordan K. Hubbard static int
1151f46af505SJordan K. Hubbard process_partition(command)
1152f46af505SJordan K. Hubbard     CMD		*command;
1153f46af505SJordan K. Hubbard {
1154f46af505SJordan K. Hubbard     int				status = 0, partition;
1155f46af505SJordan K. Hubbard     unsigned long		chunks, adj_size, max_end;
1156f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1157f46af505SJordan K. Hubbard 
1158f46af505SJordan K. Hubbard     while (1)
1159f46af505SJordan K. Hubbard     {
1160f46af505SJordan K. Hubbard 	part_processed = 1;
1161f46af505SJordan K. Hubbard 	if (command->n_args != 4)
1162f46af505SJordan K. Hubbard 	{
1163d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1164d98b1668SPhilippe Charnier 		    current_line_number);
1165f46af505SJordan K. Hubbard 	    break;
1166f46af505SJordan K. Hubbard 	}
1167f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
11684ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1169f46af505SJordan K. Hubbard 	{
1170d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1171d98b1668SPhilippe Charnier 		    current_line_number, partition);
1172f46af505SJordan K. Hubbard 	    break;
1173f46af505SJordan K. Hubbard 	}
11744ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1175f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1176f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1177f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1178f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1179f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1180f46af505SJordan K. Hubbard 
1181f46af505SJordan K. Hubbard 	if (partp->dp_typ == 0)
1182f46af505SJordan K. Hubbard 	{
1183f46af505SJordan K. Hubbard 	    /*
1184f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1185f46af505SJordan K. Hubbard 	     */
1186f46af505SJordan K. Hubbard 	    /*
1187f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1188f46af505SJordan K. Hubbard 	     */
1189f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1190f46af505SJordan K. Hubbard 	    status = 1;
1191f46af505SJordan K. Hubbard 	    break;
1192f46af505SJordan K. Hubbard 	}
1193f46af505SJordan K. Hubbard 
1194f46af505SJordan K. Hubbard 	/*
1195f46af505SJordan K. Hubbard 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1196f46af505SJordan K. Hubbard 	 */
1197f46af505SJordan K. Hubbard 	if (partp->dp_start % dos_sectors != 0)
1198f46af505SJordan K. Hubbard 	{
1199f46af505SJordan K. Hubbard 	    adj_size =
1200f46af505SJordan K. Hubbard 		(partp->dp_start / dos_sectors + 1) * dos_sectors;
1201f46af505SJordan K. Hubbard 	    if (adj_size > max_end)
1202f46af505SJordan K. Hubbard 	    {
1203f46af505SJordan K. Hubbard 		/*
1204f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1205f46af505SJordan K. Hubbard 		 */
1206d98b1668SPhilippe Charnier 		warnx(
1207d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1208d98b1668SPhilippe Charnier     a cylinder boundary",
1209d98b1668SPhilippe Charnier 			current_line_number, partition);
1210f46af505SJordan K. Hubbard 		break;
1211f46af505SJordan K. Hubbard 	    }
1212d98b1668SPhilippe Charnier 	    warnx(
1213d98b1668SPhilippe Charnier 	"WARNING: adjusting start offset of partition '%d' from %lu\n\
1214d98b1668SPhilippe Charnier     to %lu, to round to an head boundary",
1215d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_start, adj_size);
1216f46af505SJordan K. Hubbard 	    partp->dp_start = adj_size;
1217f46af505SJordan K. Hubbard 	}
1218f46af505SJordan K. Hubbard 
1219f46af505SJordan K. Hubbard 	/*
1220f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1221f46af505SJordan K. Hubbard 	 * boundary.
1222f46af505SJordan K. Hubbard 	 */
1223f46af505SJordan K. Hubbard 	chunks =
1224f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1225f46af505SJordan K. Hubbard 	adj_size = chunks - partp->dp_start;
1226f46af505SJordan K. Hubbard 	if (adj_size != partp->dp_size)
1227f46af505SJordan K. Hubbard 	{
1228d98b1668SPhilippe Charnier 	    warnx(
1229d98b1668SPhilippe Charnier 	"WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1230d98b1668SPhilippe Charnier     to round to a cylinder boundary",
1231d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_size, adj_size);
1232f46af505SJordan K. Hubbard 	    if (chunks > 0)
1233f46af505SJordan K. Hubbard 	    {
1234f46af505SJordan K. Hubbard 		partp->dp_size = adj_size;
1235f46af505SJordan K. Hubbard 	    }
1236f46af505SJordan K. Hubbard 	    else
1237f46af505SJordan K. Hubbard 	    {
1238f46af505SJordan K. Hubbard 		partp->dp_size = 0;
1239f46af505SJordan K. Hubbard 	    }
1240f46af505SJordan K. Hubbard 	}
1241f46af505SJordan K. Hubbard 	if (partp->dp_size < 1)
1242f46af505SJordan K. Hubbard 	{
1243d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: size for partition '%d' is zero",
1244d98b1668SPhilippe Charnier 		    current_line_number, partition);
1245f46af505SJordan K. Hubbard 	    break;
1246f46af505SJordan K. Hubbard 	}
1247f46af505SJordan K. Hubbard 
1248f46af505SJordan K. Hubbard 	dos(partp->dp_start, partp->dp_size,
1249f46af505SJordan K. Hubbard 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1250f46af505SJordan K. Hubbard 	dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1251f46af505SJordan K. Hubbard 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1252f46af505SJordan K. Hubbard 	status = 1;
1253f46af505SJordan K. Hubbard 	break;
1254f46af505SJordan K. Hubbard     }
1255f46af505SJordan K. Hubbard     return (status);
1256f46af505SJordan K. Hubbard }
1257f46af505SJordan K. Hubbard 
1258f46af505SJordan K. Hubbard 
1259f46af505SJordan K. Hubbard static int
1260f46af505SJordan K. Hubbard process_active(command)
1261f46af505SJordan K. Hubbard     CMD		*command;
1262f46af505SJordan K. Hubbard {
1263f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1264f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1265f46af505SJordan K. Hubbard 
1266f46af505SJordan K. Hubbard     while (1)
1267f46af505SJordan K. Hubbard     {
1268f46af505SJordan K. Hubbard 	active_processed = 1;
1269f46af505SJordan K. Hubbard 	if (command->n_args != 1)
1270f46af505SJordan K. Hubbard 	{
1271d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1272d98b1668SPhilippe Charnier 		    current_line_number);
1273f46af505SJordan K. Hubbard 	    status = 0;
1274f46af505SJordan K. Hubbard 	    break;
1275f46af505SJordan K. Hubbard 	}
1276f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
12774ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1278f46af505SJordan K. Hubbard 	{
1279d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1280d98b1668SPhilippe Charnier 		    current_line_number, partition);
1281f46af505SJordan K. Hubbard 	    break;
1282f46af505SJordan K. Hubbard 	}
1283f46af505SJordan K. Hubbard 	/*
1284f46af505SJordan K. Hubbard 	 * Reset active partition
1285f46af505SJordan K. Hubbard 	 */
1286f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1287f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1288f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
12894ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1290f46af505SJordan K. Hubbard 
1291f46af505SJordan K. Hubbard 	status = 1;
1292f46af505SJordan K. Hubbard 	break;
1293f46af505SJordan K. Hubbard     }
1294f46af505SJordan K. Hubbard     return (status);
1295f46af505SJordan K. Hubbard }
1296f46af505SJordan K. Hubbard 
1297f46af505SJordan K. Hubbard 
1298f46af505SJordan K. Hubbard static int
1299f46af505SJordan K. Hubbard process_line(line)
1300f46af505SJordan K. Hubbard     char	*line;
1301f46af505SJordan K. Hubbard {
1302f46af505SJordan K. Hubbard     CMD		command;
1303f46af505SJordan K. Hubbard     int		status = 1;
1304f46af505SJordan K. Hubbard 
1305f46af505SJordan K. Hubbard     while (1)
1306f46af505SJordan K. Hubbard     {
1307f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1308f46af505SJordan K. Hubbard 	switch (command.cmd)
1309f46af505SJordan K. Hubbard 	{
1310f46af505SJordan K. Hubbard 	case 0:
1311f46af505SJordan K. Hubbard 	    /*
1312f46af505SJordan K. Hubbard 	     * Comment or blank line
1313f46af505SJordan K. Hubbard 	     */
1314f46af505SJordan K. Hubbard 	    break;
1315f46af505SJordan K. Hubbard 	case 'g':
1316f46af505SJordan K. Hubbard 	    /*
1317f46af505SJordan K. Hubbard 	     * Set geometry
1318f46af505SJordan K. Hubbard 	     */
1319f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1320f46af505SJordan K. Hubbard 	    break;
1321f46af505SJordan K. Hubbard 	case 'p':
1322f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1323f46af505SJordan K. Hubbard 	    break;
1324f46af505SJordan K. Hubbard 	case 'a':
1325f46af505SJordan K. Hubbard 	    status = process_active(&command);
1326f46af505SJordan K. Hubbard 	    break;
1327f46af505SJordan K. Hubbard 	default:
1328f46af505SJordan K. Hubbard 	    status = 0;
1329f46af505SJordan K. Hubbard 	    break;
1330f46af505SJordan K. Hubbard 	}
1331f46af505SJordan K. Hubbard 	break;
1332f46af505SJordan K. Hubbard     }
1333f46af505SJordan K. Hubbard     return (status);
1334f46af505SJordan K. Hubbard }
1335f46af505SJordan K. Hubbard 
1336f46af505SJordan K. Hubbard 
1337f46af505SJordan K. Hubbard static int
1338f46af505SJordan K. Hubbard read_config(config_file)
1339f46af505SJordan K. Hubbard     char *config_file;
1340f46af505SJordan K. Hubbard {
1341f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1342f46af505SJordan K. Hubbard     int		status = 1;
1343f46af505SJordan K. Hubbard     char	buf[1010];
1344f46af505SJordan K. Hubbard 
1345f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1346f46af505SJordan K. Hubbard 		   function */
1347f46af505SJordan K. Hubbard     {
1348f46af505SJordan K. Hubbard 	if (strcmp(config_file, "-") != 0)
1349f46af505SJordan K. Hubbard 	{
1350f46af505SJordan K. Hubbard 	    /*
1351f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1352f46af505SJordan K. Hubbard 	     */
1353f46af505SJordan K. Hubbard 	    if ((fp = fopen(config_file, "r")) == NULL)
1354f46af505SJordan K. Hubbard 	    {
1355f46af505SJordan K. Hubbard 		status = 0;
1356f46af505SJordan K. Hubbard 		break;
1357f46af505SJordan K. Hubbard 	    }
1358f46af505SJordan K. Hubbard 	}
1359f46af505SJordan K. Hubbard 	else
1360f46af505SJordan K. Hubbard 	{
1361f46af505SJordan K. Hubbard 	    fp = stdin;
1362f46af505SJordan K. Hubbard 	}
1363f46af505SJordan K. Hubbard 	current_line_number = 0;
1364f46af505SJordan K. Hubbard 	while (!feof(fp))
1365f46af505SJordan K. Hubbard 	{
1366f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1367f46af505SJordan K. Hubbard 	    {
1368f46af505SJordan K. Hubbard 		break;
1369f46af505SJordan K. Hubbard 	    }
1370f46af505SJordan K. Hubbard 	    ++current_line_number;
1371f46af505SJordan K. Hubbard 	    status = process_line(buf);
1372f46af505SJordan K. Hubbard 	    if (status == 0)
1373f46af505SJordan K. Hubbard 	    {
1374f46af505SJordan K. Hubbard 		break;
1375f46af505SJordan K. Hubbard 	    }
1376f46af505SJordan K. Hubbard 	}
1377f46af505SJordan K. Hubbard 	break;
1378f46af505SJordan K. Hubbard     }
1379f46af505SJordan K. Hubbard     if (fp)
1380f46af505SJordan K. Hubbard     {
1381f46af505SJordan K. Hubbard 	/*
1382f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1383f46af505SJordan K. Hubbard 	 */
1384f46af505SJordan K. Hubbard 	fclose(fp);
1385f46af505SJordan K. Hubbard     }
1386f46af505SJordan K. Hubbard     return (status);
1387f46af505SJordan K. Hubbard }
1388f46af505SJordan K. Hubbard 
1389f46af505SJordan K. Hubbard 
1390f46af505SJordan K. Hubbard static void
1391f46af505SJordan K. Hubbard reset_boot(void)
1392f46af505SJordan K. Hubbard {
1393f46af505SJordan K. Hubbard     int				i;
1394f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1395f46af505SJordan K. Hubbard 
1396f46af505SJordan K. Hubbard     init_boot();
1397f46af505SJordan K. Hubbard     for (i = 0; i < 4; ++i)
1398f46af505SJordan K. Hubbard     {
1399f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1400f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1401f46af505SJordan K. Hubbard     }
1402f46af505SJordan K. Hubbard }
1403