xref: /freebsd/sbin/fdisk/fdisk.c (revision a12de06299afd0a4d87da41d2f2df72f02c1bb20)
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);
305a12de062SJohn Baldwin 	free(mboot.bootinst);
306a12de062SJohn Baldwin 	mboot.bootinst = NULL;
30785c2cf30SJohn Baldwin 
30810b0ee93SWarner Losh 	if (s_flag)
30910b0ee93SWarner Losh 	{
31010b0ee93SWarner Losh 		int i;
31110b0ee93SWarner Losh 		struct dos_partition *partp;
31210b0ee93SWarner Losh 
31310b0ee93SWarner Losh 		if (read_s0())
31410b0ee93SWarner Losh 			err(1, "read_s0");
31510b0ee93SWarner Losh 		printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
31610b0ee93SWarner Losh 		    dos_sectors);
31710b0ee93SWarner Losh 		printf("Part  %11s %11s Type Flags\n", "Start", "Size");
31810b0ee93SWarner Losh 		for (i = 0; i < NDOSPART; i++) {
31910b0ee93SWarner Losh 			partp = ((struct dos_partition *) &mboot.parts) + i;
32010b0ee93SWarner Losh 			if (partp->dp_start == 0 && partp->dp_size == 0)
32110b0ee93SWarner Losh 				continue;
32210b0ee93SWarner Losh 			printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
32310b0ee93SWarner Losh 			    (u_long) partp->dp_start,
32410b0ee93SWarner Losh 			    (u_long) partp->dp_size, partp->dp_typ,
32510b0ee93SWarner Losh 			    partp->dp_flag);
32610b0ee93SWarner Losh 		}
32710b0ee93SWarner Losh 		exit(0);
32810b0ee93SWarner Losh 	}
3295b81b6b3SRodney W. Grimes 
3305b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
331f46af505SJordan K. Hubbard 
33210b0ee93SWarner Losh 	if (I_flag)
333e6fb3ddeSPoul-Henning Kamp 	{
334e6fb3ddeSPoul-Henning Kamp 		struct dos_partition *partp;
335e6fb3ddeSPoul-Henning Kamp 
336e6fb3ddeSPoul-Henning Kamp 		read_s0();
337e6fb3ddeSPoul-Henning Kamp 		reset_boot();
338e6fb3ddeSPoul-Henning Kamp 		partp = (struct dos_partition *) (&mboot.parts[0]);
339e6fb3ddeSPoul-Henning Kamp 		partp->dp_typ = DOSPTYP_386BSD;
340e6fb3ddeSPoul-Henning Kamp 		partp->dp_flag = ACTIVE;
341e6fb3ddeSPoul-Henning Kamp 		partp->dp_start = dos_sectors;
34285c2cf30SJohn Baldwin 		partp->dp_size = ((disksecs - dos_sectors) / dos_cylsecs) *
34385c2cf30SJohn Baldwin 			dos_cylsecs;
344e6fb3ddeSPoul-Henning Kamp 
345e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start, partp->dp_size,
346e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
347e6fb3ddeSPoul-Henning Kamp 		dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
348e6fb3ddeSPoul-Henning Kamp 		    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
349e6fb3ddeSPoul-Henning Kamp 		if (v_flag)
350e6fb3ddeSPoul-Henning Kamp 			print_s0(-1);
351e6fb3ddeSPoul-Henning Kamp 		write_s0();
352e6fb3ddeSPoul-Henning Kamp 		exit(0);
353e6fb3ddeSPoul-Henning Kamp 	}
354f46af505SJordan K. Hubbard 	if (f_flag)
355f46af505SJordan K. Hubbard 	{
356f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
357f46af505SJordan K. Hubbard 	    {
358f46af505SJordan K. Hubbard 		reset_boot();
359f46af505SJordan K. Hubbard 	    }
360f46af505SJordan K. Hubbard 
361f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
362f46af505SJordan K. Hubbard 	    {
363f46af505SJordan K. Hubbard 		exit(1);
364f46af505SJordan K. Hubbard 	    }
365f46af505SJordan K. Hubbard 	    if (v_flag)
366f46af505SJordan K. Hubbard 	    {
367f46af505SJordan K. Hubbard 		print_s0(-1);
368f46af505SJordan K. Hubbard 	    }
369f46af505SJordan K. Hubbard 	    if (!t_flag)
370f46af505SJordan K. Hubbard 	    {
371f46af505SJordan K. Hubbard 		write_s0();
372f46af505SJordan K. Hubbard 	    }
373f46af505SJordan K. Hubbard 	}
374f46af505SJordan K. Hubbard 	else
375f46af505SJordan K. Hubbard 	{
3765b81b6b3SRodney W. Grimes 	    if(u_flag)
3775b81b6b3SRodney W. Grimes 	    {
3785b81b6b3SRodney W. Grimes 		get_params_to_use();
3795b81b6b3SRodney W. Grimes 	    }
3805b81b6b3SRodney W. Grimes 	    else
3815b81b6b3SRodney W. Grimes 	    {
3825b81b6b3SRodney W. Grimes 		print_params();
3835b81b6b3SRodney W. Grimes 	    }
3845b81b6b3SRodney W. Grimes 
3855b81b6b3SRodney W. Grimes 	    if (read_s0())
3865b81b6b3SRodney W. Grimes 		init_sector0(1);
3875b81b6b3SRodney W. Grimes 
3887cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3895b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3905b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3915b81b6b3SRodney W. Grimes 	    if (partition == -1)
3924ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3935b81b6b3SRodney W. Grimes 		    change_part(i);
3945b81b6b3SRodney W. Grimes 	    else
3955b81b6b3SRodney W. Grimes 		change_part(partition);
3965b81b6b3SRodney W. Grimes 
3975b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3985b81b6b3SRodney W. Grimes 		change_active(partition);
3995b81b6b3SRodney W. Grimes 
40026721a89SRobert Nordier 	    if (B_flag)
40112d910e9SRobert Nordier 		change_code();
40212d910e9SRobert Nordier 
40326721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
404f46af505SJordan K. Hubbard 		if (!t_flag)
405f46af505SJordan K. Hubbard 		{
4065b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
4075b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
408f46af505SJordan K. Hubbard 		}
4095b81b6b3SRodney W. Grimes 		print_s0(-1);
410f46af505SJordan K. Hubbard 		if (!t_flag)
411f46af505SJordan K. Hubbard 		{
4125b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
4135b81b6b3SRodney W. Grimes 			write_s0();
4145b81b6b3SRodney W. Grimes 		}
415f46af505SJordan K. Hubbard 		else
416f46af505SJordan K. Hubbard 		{
417f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
418f46af505SJordan K. Hubbard 		}
419f46af505SJordan K. Hubbard 	    }
420f46af505SJordan K. Hubbard 	}
4215b81b6b3SRodney W. Grimes 
4225b81b6b3SRodney W. Grimes 	exit(0);
423d98b1668SPhilippe Charnier }
4245b81b6b3SRodney W. Grimes 
425d98b1668SPhilippe Charnier static void
426d98b1668SPhilippe Charnier usage()
427d98b1668SPhilippe Charnier {
42826721a89SRobert Nordier 	fprintf(stderr, "%s%s",
429ed1235adSJohn Baldwin 		"usage: fdisk [-BIaistu] [-b bootcode] [-1234] [disk]\n",
43026721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
431d98b1668SPhilippe Charnier         exit(1);
4325b81b6b3SRodney W. Grimes }
4335b81b6b3SRodney W. Grimes 
4344be1e61bSAlexander Langer static void
4354be1e61bSAlexander Langer print_s0(int which)
4365b81b6b3SRodney W. Grimes {
4375b81b6b3SRodney W. Grimes int	i;
4385b81b6b3SRodney W. Grimes 
4395b81b6b3SRodney W. Grimes 	print_params();
4405b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
4415b81b6b3SRodney W. Grimes 	if (which == -1)
4424ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
4435b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
4445b81b6b3SRodney W. Grimes 	else
4455b81b6b3SRodney W. Grimes 		print_part(which);
4465b81b6b3SRodney W. Grimes }
4475b81b6b3SRodney W. Grimes 
4485b81b6b3SRodney W. Grimes static struct dos_partition mtpart = { 0 };
4495b81b6b3SRodney W. Grimes 
4504be1e61bSAlexander Langer static void
4514be1e61bSAlexander Langer print_part(int i)
4525b81b6b3SRodney W. Grimes {
453637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
454637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
4555b81b6b3SRodney W. Grimes 
4564ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4575b81b6b3SRodney W. Grimes 
4585b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
4595b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
4605b81b6b3SRodney W. Grimes 		return;
4615b81b6b3SRodney W. Grimes 	}
462637fe2f7SJustin T. Gibbs 	/*
463637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
464637fe2f7SJustin T. Gibbs 	 */
465637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
466637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
467637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
4685b81b6b3SRodney W. Grimes 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
469ba198492SBruce Evans 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
470ba198492SBruce Evans 		(u_long)partp->dp_start,
471ba198492SBruce Evans 		(u_long)partp->dp_size,
472637fe2f7SJustin T. Gibbs 		part_mb,
473680426beSDavid E. O'Brien 		partp->dp_flag,
474680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4755b81b6b3SRodney W. Grimes 	printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
4765b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4775b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_ssect)
4785b81b6b3SRodney W. Grimes 		,partp->dp_shd
4795b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4805b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_esect)
4815b81b6b3SRodney W. Grimes 		,partp->dp_ehd);
4825b81b6b3SRodney W. Grimes }
4835b81b6b3SRodney W. Grimes 
484f46af505SJordan K. Hubbard 
485f46af505SJordan K. Hubbard static void
486f46af505SJordan K. Hubbard init_boot(void)
487f46af505SJordan K. Hubbard {
48826721a89SRobert Nordier 	const char *fname;
48985c2cf30SJohn Baldwin 	int fd, n;
49085c2cf30SJohn Baldwin 	struct stat sb;
49126721a89SRobert Nordier 
49226721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
49326721a89SRobert Nordier 	if ((fd = open(fname, O_RDONLY)) == -1 ||
49485c2cf30SJohn Baldwin 	    fstat(fd, &sb) == -1)
49585c2cf30SJohn Baldwin 		err(1, "%s", fname);
49685c2cf30SJohn Baldwin 	if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
49785c2cf30SJohn Baldwin 		errx(1, "%s: length must be a multiple of sector size", fname);
4982b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
4992b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
50085c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
50185c2cf30SJohn Baldwin 		errx(1, "%s: unable to allocate read buffer", fname);
50285c2cf30SJohn Baldwin 	if ((n = read(fd, mboot.bootinst, mboot.bootinst_size)) == -1 ||
50326721a89SRobert Nordier 	    close(fd))
50426721a89SRobert Nordier 		err(1, "%s", fname);
50585c2cf30SJohn Baldwin 	if (n != mboot.bootinst_size)
50685c2cf30SJohn Baldwin 		errx(1, "%s: short read", fname);
507f46af505SJordan K. Hubbard }
508f46af505SJordan K. Hubbard 
509f46af505SJordan K. Hubbard 
5104be1e61bSAlexander Langer static void
5114be1e61bSAlexander Langer init_sector0(unsigned long start)
5125b81b6b3SRodney W. Grimes {
5135b81b6b3SRodney W. Grimes struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
51485c2cf30SJohn Baldwin unsigned long size;
5155b81b6b3SRodney W. Grimes 
516f46af505SJordan K. Hubbard 	init_boot();
5175b81b6b3SRodney W. Grimes 
5185b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
5195b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
52085c2cf30SJohn Baldwin 	/* ensure cylinder boundaries */
52185c2cf30SJohn Baldwin 	start = (start / dos_sectors) * dos_sectors;
52285c2cf30SJohn Baldwin 	if(start == 0)
52385c2cf30SJohn Baldwin 		start = dos_sectors;
52485c2cf30SJohn Baldwin 	size = ((disksecs - start) / dos_cylsecs) * dos_cylsecs;
5255b81b6b3SRodney W. Grimes 	partp->dp_start = start;
5265b81b6b3SRodney W. Grimes 	partp->dp_size = size;
5275b81b6b3SRodney W. Grimes 
528e2975440SBruce Evans 	dos(partp->dp_start, partp->dp_size,
529e2975440SBruce Evans 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
530e2975440SBruce Evans 	dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
531e2975440SBruce Evans 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5325b81b6b3SRodney W. Grimes }
5335b81b6b3SRodney W. Grimes 
5344be1e61bSAlexander Langer static void
5354be1e61bSAlexander Langer change_part(int i)
5365b81b6b3SRodney W. Grimes {
5374ddd60b9SBrian Somers struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
5385b81b6b3SRodney W. Grimes 
5395b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
5405b81b6b3SRodney W. Grimes     print_part(i);
5415b81b6b3SRodney W. Grimes 
5425b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
5435b81b6b3SRodney W. Grimes 	int tmp;
5445b81b6b3SRodney W. Grimes 
5455b81b6b3SRodney W. Grimes 	if (i_flag) {
5465b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
5474ddd60b9SBrian Somers 		if (i == 4) {
5485b81b6b3SRodney W. Grimes 			init_sector0(1);
5494ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
5505b81b6b3SRodney W. Grimes 			print_part(i);
5515b81b6b3SRodney W. Grimes 		}
5525b81b6b3SRodney W. Grimes 	}
5535b81b6b3SRodney W. Grimes 
5545b81b6b3SRodney W. Grimes 	do {
555680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
5565b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
5575b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
5585b81b6b3SRodney W. Grimes 
5594b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
5605b81b6b3SRodney W. Grimes 		{
5615b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
5625b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
5635b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
5645b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
5655b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
5665b81b6b3SRodney W. Grimes 			Decimal("beginning head", thd, tmp);
5675b81b6b3SRodney W. Grimes 			Decimal("beginning sector", tsec, tmp);
5685b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
5695b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
5705b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
5715b81b6b3SRodney W. Grimes 
5725b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
5735b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
5745b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
5755b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
5765b81b6b3SRodney W. Grimes 			Decimal("ending head", thd, tmp);
5775b81b6b3SRodney W. Grimes 			Decimal("ending sector", tsec, tmp);
5785b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5795b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5805b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
5815b81b6b3SRodney W. Grimes 		} else {
58285c2cf30SJohn Baldwin 			if(partp->dp_start % dos_sectors != 0) {
58385c2cf30SJohn Baldwin 				printf("Adjusting partition to start at a "
58485c2cf30SJohn Baldwin 				   "cylinder boundary\n");
58585c2cf30SJohn Baldwin 				partp->dp_start =
58685c2cf30SJohn Baldwin 				    (partp->dp_start / dos_sectors) *
58785c2cf30SJohn Baldwin 				    dos_sectors;
58885c2cf30SJohn Baldwin 			}
58985c2cf30SJohn Baldwin 			if(partp->dp_size % dos_cylsecs != 0) {
59085c2cf30SJohn Baldwin 				printf("Adjusting partition to end at a "
59185c2cf30SJohn Baldwin 				    "cylinder boundary\n");
59285c2cf30SJohn Baldwin 				partp->dp_size =
59385c2cf30SJohn Baldwin 				    (partp->dp_size / dos_cylsecs) *
59485c2cf30SJohn Baldwin 				    dos_cylsecs;
59585c2cf30SJohn Baldwin 			}
596e2975440SBruce Evans 			dos(partp->dp_start, partp->dp_size,
5975b81b6b3SRodney W. Grimes 			    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
598e2975440SBruce Evans 			dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
5995b81b6b3SRodney W. Grimes 			    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
6005b81b6b3SRodney W. Grimes 		}
6015b81b6b3SRodney W. Grimes 
6025b81b6b3SRodney W. Grimes 		print_part(i);
6035b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
6045b81b6b3SRodney W. Grimes     }
6055b81b6b3SRodney W. Grimes }
6065b81b6b3SRodney W. Grimes 
6074be1e61bSAlexander Langer static void
6085b81b6b3SRodney W. Grimes print_params()
6095b81b6b3SRodney W. Grimes {
6105b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
6115b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
6125b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
6135b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
6145b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
6155b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
6165b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
6175b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
6185b81b6b3SRodney W. Grimes }
6195b81b6b3SRodney W. Grimes 
6204be1e61bSAlexander Langer static void
6214be1e61bSAlexander Langer change_active(int which)
6225b81b6b3SRodney W. Grimes {
6235b81b6b3SRodney W. Grimes int i;
6244ddd60b9SBrian Somers int active = 4, tmp;
6255b81b6b3SRodney W. Grimes struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
6265b81b6b3SRodney W. Grimes 
6275b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
6285b81b6b3SRodney W. Grimes 		active = which;
6290b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
6300b461cd7SBruce Evans 		return;
631680426beSDavid E. O'Brien setactive:
632680426beSDavid E. O'Brien 	active = 4;
633680426beSDavid E. O'Brien 	do {
6345b81b6b3SRodney W. Grimes 		Decimal("active partition", active, tmp);
635680426beSDavid E. O'Brien 		if (active < 1 || 4 < active) {
636680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
637680426beSDavid E. O'Brien 					"  Try again.\n");
638680426beSDavid E. O'Brien 			goto setactive;
639680426beSDavid E. O'Brien 		}
640680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
6415b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
6425b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
6434ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
6444ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
6455b81b6b3SRodney W. Grimes }
6465b81b6b3SRodney W. Grimes 
64712d910e9SRobert Nordier static void
64812d910e9SRobert Nordier change_code()
64912d910e9SRobert Nordier {
65012d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
65112d910e9SRobert Nordier 		init_boot();
65212d910e9SRobert Nordier }
65312d910e9SRobert Nordier 
6544be1e61bSAlexander Langer void
6555b81b6b3SRodney W. Grimes get_params_to_use()
6565b81b6b3SRodney W. Grimes {
6575b81b6b3SRodney W. Grimes 	int	tmp;
6585b81b6b3SRodney W. Grimes 	print_params();
6595b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
6605b81b6b3SRodney W. Grimes 	{
6615b81b6b3SRodney W. Grimes 		do
6625b81b6b3SRodney W. Grimes 		{
6635b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
6645b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
6655b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
6665b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
6675b81b6b3SRodney W. Grimes 			print_params();
6685b81b6b3SRodney W. Grimes 		}
6695b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
6705b81b6b3SRodney W. Grimes 	}
6715b81b6b3SRodney W. Grimes }
6725b81b6b3SRodney W. Grimes 
673f46af505SJordan K. Hubbard 
6745b81b6b3SRodney W. Grimes /***********************************************\
6755b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
6765b81b6b3SRodney W. Grimes \***********************************************/
6774be1e61bSAlexander Langer static void
678e2975440SBruce Evans dos(sec, size, c, s, h)
679e2975440SBruce Evans int sec, size;
6805b81b6b3SRodney W. Grimes unsigned char *c, *s, *h;
6815b81b6b3SRodney W. Grimes {
6825b81b6b3SRodney W. Grimes int cy;
6835b81b6b3SRodney W. Grimes int hd;
6845b81b6b3SRodney W. Grimes 
685e2975440SBruce Evans 	if (sec == 0 && size == 0) {
6860b461cd7SBruce Evans 		*s = *c = *h = 0;
6870b461cd7SBruce Evans 		return;
6880b461cd7SBruce Evans 	}
6890b461cd7SBruce Evans 
6905b81b6b3SRodney W. Grimes 	cy = sec / ( dos_cylsecs );
6915b81b6b3SRodney W. Grimes 	sec = sec - cy * ( dos_cylsecs );
6925b81b6b3SRodney W. Grimes 
6935b81b6b3SRodney W. Grimes 	hd = sec / dos_sectors;
6945b81b6b3SRodney W. Grimes 	sec = (sec - hd * dos_sectors) + 1;
6955b81b6b3SRodney W. Grimes 
6965b81b6b3SRodney W. Grimes 	*h = hd;
6975b81b6b3SRodney W. Grimes 	*c = cy & 0xff;
6985b81b6b3SRodney W. Grimes 	*s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
6995b81b6b3SRodney W. Grimes }
7005b81b6b3SRodney W. Grimes 
7015b81b6b3SRodney W. Grimes int fd;
7025b81b6b3SRodney W. Grimes 
7035b81b6b3SRodney W. Grimes 	/* Getting device status */
7045b81b6b3SRodney W. Grimes 
7054be1e61bSAlexander Langer static int
7064be1e61bSAlexander Langer open_disk(int u_flag)
7075b81b6b3SRodney W. Grimes {
7085b81b6b3SRodney W. Grimes struct stat 	st;
7095b81b6b3SRodney W. Grimes 
7105b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
711d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
7125b81b6b3SRodney W. Grimes 		return -1;
713b60eb395SBruce Evans 	}
714b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
715d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
71612d910e9SRobert Nordier 	if ((fd = open(disk,
71710b0ee93SWarner Losh 	    a_flag || I_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
718e3038c6eSJoerg Wunsch 		if(errno == ENXIO)
719e3038c6eSJoerg Wunsch 			return -2;
720d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
7215b81b6b3SRodney W. Grimes 		return -1;
7225b81b6b3SRodney W. Grimes 	}
7235b81b6b3SRodney W. Grimes 	if (get_params(0) == -1) {
724d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
7255b81b6b3SRodney W. Grimes 		return -1;
7265b81b6b3SRodney W. Grimes 	}
7275b81b6b3SRodney W. Grimes 	return fd;
7285b81b6b3SRodney W. Grimes }
7295b81b6b3SRodney W. Grimes 
7304be1e61bSAlexander Langer static ssize_t
7314be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
7325b81b6b3SRodney W. Grimes {
7335b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7347cb29d33SSøren Schmidt 	if( secsize == 0 )
7357cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
7367cb29d33SSøren Schmidt 			{
7377cb29d33SSøren Schmidt 			/* try the read */
7387cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
7397cb29d33SSøren Schmidt 			if( size == secsize )
7407cb29d33SSøren Schmidt 				/* it worked so return */
7417cb29d33SSøren Schmidt 				return secsize;
7427cb29d33SSøren Schmidt 			}
7437cb29d33SSøren Schmidt 	else
7447cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
7457cb29d33SSøren Schmidt 
7467cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
7477cb29d33SSøren Schmidt 	return -1;
7485b81b6b3SRodney W. Grimes }
7495b81b6b3SRodney W. Grimes 
7504be1e61bSAlexander Langer static ssize_t
7514be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
7525b81b6b3SRodney W. Grimes {
7535b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
7547cb29d33SSøren Schmidt 	/* write out in the size that the read_disk found worked */
7557cb29d33SSøren Schmidt 	return write(fd, buf, secsize);
7565b81b6b3SRodney W. Grimes }
7575b81b6b3SRodney W. Grimes 
7584be1e61bSAlexander Langer static int
7594be1e61bSAlexander Langer get_params()
7605b81b6b3SRodney W. Grimes {
7615b81b6b3SRodney W. Grimes 
7625b81b6b3SRodney W. Grimes     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
763d98b1668SPhilippe Charnier 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
764b60eb395SBruce Evans 	dos_cyls = cyls = 1;
765b60eb395SBruce Evans 	dos_heads = heads = 1;
766b60eb395SBruce Evans 	dos_sectors = sectors = 1;
767b60eb395SBruce Evans 	dos_cylsecs = cylsecs = heads * sectors;
768b60eb395SBruce Evans 	disksecs = cyls * heads * sectors;
769b60eb395SBruce Evans 	return disksecs;
7705b81b6b3SRodney W. Grimes     }
7715b81b6b3SRodney W. Grimes 
7725b81b6b3SRodney W. Grimes     dos_cyls = cyls = disklabel.d_ncylinders;
7735b81b6b3SRodney W. Grimes     dos_heads = heads = disklabel.d_ntracks;
7745b81b6b3SRodney W. Grimes     dos_sectors = sectors = disklabel.d_nsectors;
7755b81b6b3SRodney W. Grimes     dos_cylsecs = cylsecs = heads * sectors;
7765b81b6b3SRodney W. Grimes     disksecs = cyls * heads * sectors;
7775b81b6b3SRodney W. Grimes 
7785b81b6b3SRodney W. Grimes     return (disksecs);
7795b81b6b3SRodney W. Grimes }
7805b81b6b3SRodney W. Grimes 
7815b81b6b3SRodney W. Grimes 
7824be1e61bSAlexander Langer static int
7835b81b6b3SRodney W. Grimes read_s0()
7845b81b6b3SRodney W. Grimes {
78585c2cf30SJohn Baldwin 	mboot.bootinst_size = secsize;
7862b5ce8a9SAndrey A. Chernov 	if (mboot.bootinst != NULL)
7872b5ce8a9SAndrey A. Chernov 		free(mboot.bootinst);
78885c2cf30SJohn Baldwin 	if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
78985c2cf30SJohn Baldwin 		warnx("unable to allocate buffer to read fdisk "
79085c2cf30SJohn Baldwin 		      "partition table");
79185c2cf30SJohn Baldwin 		return -1;
79285c2cf30SJohn Baldwin 	}
79385c2cf30SJohn Baldwin 	if (read_disk(0, mboot.bootinst) == -1) {
794d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
7955b81b6b3SRodney W. Grimes 		return -1;
7965b81b6b3SRodney W. Grimes 	}
79785c2cf30SJohn Baldwin 	if (*(int *)&mboot.bootinst[MBRSIGOFF] != BOOT_MAGIC) {
798d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
7995b81b6b3SRodney W. Grimes 		/* So should we initialize things */
8005b81b6b3SRodney W. Grimes 		return -1;
8015b81b6b3SRodney W. Grimes 	}
80285c2cf30SJohn Baldwin 	memcpy(mboot.parts, &mboot.bootinst[DOSPARTOFF], sizeof(mboot.parts));
8035b81b6b3SRodney W. Grimes 	return 0;
8045b81b6b3SRodney W. Grimes }
8055b81b6b3SRodney W. Grimes 
8064be1e61bSAlexander Langer static int
8075b81b6b3SRodney W. Grimes write_s0()
8085b81b6b3SRodney W. Grimes {
809cefdc4efSBill Fumerola #ifdef NOT_NOW
8105b81b6b3SRodney W. Grimes 	int	flag;
811cefdc4efSBill Fumerola #endif
81285c2cf30SJohn Baldwin 	int	sector;
81385c2cf30SJohn Baldwin 
8145b81b6b3SRodney W. Grimes 	if (iotest) {
8155b81b6b3SRodney W. Grimes 		print_s0(-1);
8165b81b6b3SRodney W. Grimes 		return 0;
8175b81b6b3SRodney W. Grimes 	}
81885c2cf30SJohn Baldwin 	memcpy(&mboot.bootinst[DOSPARTOFF], mboot.parts, sizeof(mboot.parts));
8195b81b6b3SRodney W. Grimes 	/*
8205b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
8215b81b6b3SRodney W. Grimes 	 * disable after writing.
8225b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
8235b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
8245b81b6b3SRodney W. Grimes 	 */
825ba3551dfSJulian Elischer #ifdef NOT_NOW
826e6fb3ddeSPoul-Henning Kamp 	flag = 1;
8275b81b6b3SRodney W. Grimes 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
828d98b1668SPhilippe Charnier 		warn("ioctl DIOCWLABEL");
829ba3551dfSJulian Elischer #endif
83085c2cf30SJohn Baldwin 	for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
83185c2cf30SJohn Baldwin 		if (write_disk(sector,
83285c2cf30SJohn Baldwin 			       &mboot.bootinst[sector * secsize]) == -1) {
833e6fb3ddeSPoul-Henning Kamp 			warn("can't write fdisk partition table");
8345b81b6b3SRodney W. Grimes 			return -1;
835ba3551dfSJulian Elischer #ifdef NOT_NOW
836e6fb3ddeSPoul-Henning Kamp 			flag = 0;
8375b81b6b3SRodney W. Grimes 			(void) ioctl(fd, DIOCWLABEL, &flag);
838ba3551dfSJulian Elischer #endif
8395b81b6b3SRodney W. Grimes 		}
84085c2cf30SJohn Baldwin #ifdef NOT_NOW
84185c2cf30SJohn Baldwin 	flag = 0;
84285c2cf30SJohn Baldwin 	(void) ioctl(fd, DIOCWLABEL, &flag);
84385c2cf30SJohn Baldwin #endif
8444be1e61bSAlexander Langer 	return(0);
8455b81b6b3SRodney W. Grimes }
8465b81b6b3SRodney W. Grimes 
8475b81b6b3SRodney W. Grimes 
8484be1e61bSAlexander Langer static int
8495b81b6b3SRodney W. Grimes ok(str)
8505b81b6b3SRodney W. Grimes char *str;
8515b81b6b3SRodney W. Grimes {
8525b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
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 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
8585b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
8595b81b6b3SRodney W. Grimes 		return 1;
8605b81b6b3SRodney W. Grimes 	else
8615b81b6b3SRodney W. Grimes 		return 0;
8625b81b6b3SRodney W. Grimes }
8635b81b6b3SRodney W. Grimes 
8644be1e61bSAlexander Langer static int
8654be1e61bSAlexander Langer decimal(char *str, int *num, int deflt)
8665b81b6b3SRodney W. Grimes {
8675b81b6b3SRodney W. Grimes int acc = 0, c;
8685b81b6b3SRodney W. Grimes char *cp;
8695b81b6b3SRodney W. Grimes 
8705b81b6b3SRodney W. Grimes 	while (1) {
8715b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
8725b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8735b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8745b81b6b3SRodney W. Grimes 
8755b81b6b3SRodney W. Grimes 		if (!*lbuf)
8765b81b6b3SRodney W. Grimes 			return 0;
8775b81b6b3SRodney W. Grimes 
8785b81b6b3SRodney W. Grimes 		cp = lbuf;
8795b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8805b81b6b3SRodney W. Grimes 		if (!c)
8815b81b6b3SRodney W. Grimes 			return 0;
8824be1e61bSAlexander Langer 		while ((c = *cp++)) {
8835b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8845b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
8855b81b6b3SRodney W. Grimes 			else
8865b81b6b3SRodney W. Grimes 				break;
8875b81b6b3SRodney W. Grimes 		}
8885b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8895b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8905b81b6b3SRodney W. Grimes 		if (!c) {
8915b81b6b3SRodney W. Grimes 			*num = acc;
8925b81b6b3SRodney W. Grimes 			return 1;
8935b81b6b3SRodney W. Grimes 		} else
894680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
8955b81b6b3SRodney W. Grimes 				lbuf);
8965b81b6b3SRodney W. Grimes 	}
8975b81b6b3SRodney W. Grimes 
8985b81b6b3SRodney W. Grimes }
8995b81b6b3SRodney W. Grimes 
9004be1e61bSAlexander Langer #if 0
9014be1e61bSAlexander Langer static int
9024be1e61bSAlexander Langer hex(char *str, int *num, int deflt)
9035b81b6b3SRodney W. Grimes {
9045b81b6b3SRodney W. Grimes int acc = 0, c;
9055b81b6b3SRodney W. Grimes char *cp;
9065b81b6b3SRodney W. Grimes 
9075b81b6b3SRodney W. Grimes 	while (1) {
9085b81b6b3SRodney W. Grimes 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
9095b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
9105b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
9115b81b6b3SRodney W. Grimes 
9125b81b6b3SRodney W. Grimes 		if (!*lbuf)
9135b81b6b3SRodney W. Grimes 			return 0;
9145b81b6b3SRodney W. Grimes 
9155b81b6b3SRodney W. Grimes 		cp = lbuf;
9165b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9175b81b6b3SRodney W. Grimes 		if (!c)
9185b81b6b3SRodney W. Grimes 			return 0;
9194be1e61bSAlexander Langer 		while ((c = *cp++)) {
9205b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
9215b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - '0';
9225b81b6b3SRodney W. Grimes 			else if (c <= 'f' && c >= 'a')
9235b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'a' + 10;
9245b81b6b3SRodney W. Grimes 			else if (c <= 'F' && c >= 'A')
9255b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'A' + 10;
9265b81b6b3SRodney W. Grimes 			else
9275b81b6b3SRodney W. Grimes 				break;
9285b81b6b3SRodney W. Grimes 		}
9295b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
9305b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9315b81b6b3SRodney W. Grimes 		if (!c) {
9325b81b6b3SRodney W. Grimes 			*num = acc;
9335b81b6b3SRodney W. Grimes 			return 1;
9345b81b6b3SRodney W. Grimes 		} else
935680426beSDavid E. O'Brien 			printf("%s is an invalid hex number.  Try again.\n",
9365b81b6b3SRodney W. Grimes 				lbuf);
9375b81b6b3SRodney W. Grimes 	}
9385b81b6b3SRodney W. Grimes 
9395b81b6b3SRodney W. Grimes }
9405b81b6b3SRodney W. Grimes 
9414be1e61bSAlexander Langer static int
9424be1e61bSAlexander Langer string(char *str, char **ans)
9435b81b6b3SRodney W. Grimes {
9445b81b6b3SRodney W. Grimes int c;
9455b81b6b3SRodney W. Grimes char *cp = lbuf;
9465b81b6b3SRodney W. Grimes 
9475b81b6b3SRodney W. Grimes 	while (1) {
9485b81b6b3SRodney W. Grimes 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
9495b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
9505b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
9515b81b6b3SRodney W. Grimes 
9525b81b6b3SRodney W. Grimes 		if (!*lbuf)
9535b81b6b3SRodney W. Grimes 			return 0;
9545b81b6b3SRodney W. Grimes 
9555b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
9565b81b6b3SRodney W. Grimes 		if (c == '"') {
9575b81b6b3SRodney W. Grimes 			c = *++cp;
9585b81b6b3SRodney W. Grimes 			*ans = cp;
9595b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != '"') cp++;
9605b81b6b3SRodney W. Grimes 		} else {
9615b81b6b3SRodney W. Grimes 			*ans = cp;
9625b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
9635b81b6b3SRodney W. Grimes 		}
9645b81b6b3SRodney W. Grimes 
9655b81b6b3SRodney W. Grimes 		if (c)
9665b81b6b3SRodney W. Grimes 			*cp = 0;
9675b81b6b3SRodney W. Grimes 		return 1;
9685b81b6b3SRodney W. Grimes 	}
9695b81b6b3SRodney W. Grimes }
9704be1e61bSAlexander Langer #endif
9715b81b6b3SRodney W. Grimes 
9724be1e61bSAlexander Langer static char *
9734be1e61bSAlexander Langer get_type(int type)
9745b81b6b3SRodney W. Grimes {
9755b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
9765b81b6b3SRodney W. Grimes 	int	counter = 0;
9775b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
9785b81b6b3SRodney W. Grimes 
9795b81b6b3SRodney W. Grimes 
9805b81b6b3SRodney W. Grimes 	while(counter < numentries)
9815b81b6b3SRodney W. Grimes 	{
9825b81b6b3SRodney W. Grimes 		if(ptr->type == type)
9835b81b6b3SRodney W. Grimes 		{
9845b81b6b3SRodney W. Grimes 			return(ptr->name);
9855b81b6b3SRodney W. Grimes 		}
9865b81b6b3SRodney W. Grimes 		ptr++;
9875b81b6b3SRodney W. Grimes 		counter++;
9885b81b6b3SRodney W. Grimes 	}
9895b81b6b3SRodney W. Grimes 	return("unknown");
9905b81b6b3SRodney W. Grimes }
991f46af505SJordan K. Hubbard 
992f46af505SJordan K. Hubbard 
993f46af505SJordan K. Hubbard static void
994f46af505SJordan K. Hubbard parse_config_line(line, command)
995f46af505SJordan K. Hubbard     char	*line;
996f46af505SJordan K. Hubbard     CMD		*command;
997f46af505SJordan K. Hubbard {
998f46af505SJordan K. Hubbard     char	*cp, *end;
999f46af505SJordan K. Hubbard 
1000f46af505SJordan K. Hubbard     cp = line;
1001f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1002f46af505SJordan K. Hubbard 		   function */
1003f46af505SJordan K. Hubbard     {
1004f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
1005f46af505SJordan K. Hubbard 
1006f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
1007f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
1008f46af505SJordan K. Hubbard 	{
1009f46af505SJordan K. Hubbard 	    break;
1010f46af505SJordan K. Hubbard 	}
1011f46af505SJordan K. Hubbard 	command->cmd = *cp++;
1012f46af505SJordan K. Hubbard 
1013f46af505SJordan K. Hubbard 	/*
1014f46af505SJordan K. Hubbard 	 * Parse args
1015f46af505SJordan K. Hubbard 	 */
1016f46af505SJordan K. Hubbard 	while (1)
1017f46af505SJordan K. Hubbard 	{
1018f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
1019f46af505SJordan K. Hubbard 	    if (*cp == '#')
1020f46af505SJordan K. Hubbard 	    {
1021f46af505SJordan K. Hubbard 		break;		/* found comment */
1022f46af505SJordan K. Hubbard 	    }
1023f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
1024f46af505SJordan K. Hubbard 	    {
1025f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
1026f46af505SJordan K. Hubbard 	    }
1027f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
1028f46af505SJordan K. Hubbard 	    {
1029f46af505SJordan K. Hubbard 		break;		/* assume end of line */
1030f46af505SJordan K. Hubbard 	    }
1031f46af505SJordan K. Hubbard 	    end = NULL;
1032f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1033f46af505SJordan K. Hubbard 	    if (cp == end)
1034f46af505SJordan K. Hubbard 	    {
1035f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
1036f46af505SJordan K. Hubbard 	    }
1037f46af505SJordan K. Hubbard 	    cp = end;
1038f46af505SJordan K. Hubbard 	    command->n_args++;
1039f46af505SJordan K. Hubbard 	}
1040f46af505SJordan K. Hubbard 	break;
1041f46af505SJordan K. Hubbard     }
1042f46af505SJordan K. Hubbard }
1043f46af505SJordan K. Hubbard 
1044f46af505SJordan K. Hubbard 
1045f46af505SJordan K. Hubbard static int
1046f46af505SJordan K. Hubbard process_geometry(command)
1047f46af505SJordan K. Hubbard     CMD		*command;
1048f46af505SJordan K. Hubbard {
1049f46af505SJordan K. Hubbard     int		status = 1, i;
1050f46af505SJordan K. Hubbard 
1051f46af505SJordan K. Hubbard     while (1)
1052f46af505SJordan K. Hubbard     {
1053f46af505SJordan K. Hubbard 	geom_processed = 1;
1054f46af505SJordan K. Hubbard 	if (part_processed)
1055f46af505SJordan K. Hubbard 	{
1056d98b1668SPhilippe Charnier 	    warnx(
1057d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
1058d98b1668SPhilippe Charnier     all partition specifications",
1059d98b1668SPhilippe Charnier 		    current_line_number);
1060f46af505SJordan K. Hubbard 	    status = 0;
1061f46af505SJordan K. Hubbard 	    break;
1062f46af505SJordan K. Hubbard 	}
1063f46af505SJordan K. Hubbard 	if (command->n_args != 3)
1064f46af505SJordan K. Hubbard 	{
1065d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
1066d98b1668SPhilippe Charnier 		    current_line_number);
1067f46af505SJordan K. Hubbard 	    status = 0;
1068f46af505SJordan K. Hubbard 	    break;
1069f46af505SJordan K. Hubbard 	}
1070f46af505SJordan K. Hubbard 	dos_cyls = -1;
1071f46af505SJordan K. Hubbard 	dos_heads = -1;
1072f46af505SJordan K. Hubbard 	dos_sectors = -1;
1073f46af505SJordan K. Hubbard 	for (i = 0; i < 3; ++i)
1074f46af505SJordan K. Hubbard 	{
1075f46af505SJordan K. Hubbard 	    switch (command->args[i].argtype)
1076f46af505SJordan K. Hubbard 	    {
1077f46af505SJordan K. Hubbard 	    case 'c':
1078f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
1079f46af505SJordan K. Hubbard 		break;
1080f46af505SJordan K. Hubbard 	    case 'h':
1081f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
1082f46af505SJordan K. Hubbard 		break;
1083f46af505SJordan K. Hubbard 	    case 's':
1084f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
1085f46af505SJordan K. Hubbard 		break;
1086f46af505SJordan K. Hubbard 	    default:
1087d98b1668SPhilippe Charnier 		warnx(
1088d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1089d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
1090f46af505SJordan K. Hubbard 			command->args[i].argtype);
1091f46af505SJordan K. Hubbard 		status = 0;
1092f46af505SJordan K. Hubbard 		break;
1093f46af505SJordan K. Hubbard 	    }
1094f46af505SJordan K. Hubbard 	}
1095f46af505SJordan K. Hubbard 	if (status == 0)
1096f46af505SJordan K. Hubbard 	{
1097f46af505SJordan K. Hubbard 	    break;
1098f46af505SJordan K. Hubbard 	}
1099f46af505SJordan K. Hubbard 
1100f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
1101f46af505SJordan K. Hubbard 
1102f46af505SJordan K. Hubbard 	/*
1103f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
1104f46af505SJordan K. Hubbard 	 */
1105f46af505SJordan K. Hubbard 	if (dos_cyls < 0)
1106f46af505SJordan K. Hubbard 	{
1107d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
1108d98b1668SPhilippe Charnier 		    current_line_number);
1109f46af505SJordan K. Hubbard 	    status = 0;
1110f46af505SJordan K. Hubbard 	}
1111f46af505SJordan K. Hubbard 	if (dos_cyls == 0 || dos_cyls > 1024)
1112f46af505SJordan K. Hubbard 	{
1113d98b1668SPhilippe Charnier 	    warnx(
1114d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1115f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1116d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1117d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1118f46af505SJordan K. Hubbard 	}
1119f46af505SJordan K. Hubbard 
1120f46af505SJordan K. Hubbard 	if (dos_heads < 0)
1121f46af505SJordan K. Hubbard 	{
1122d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1123d98b1668SPhilippe Charnier 		    current_line_number);
1124f46af505SJordan K. Hubbard 	    status = 0;
1125f46af505SJordan K. Hubbard 	}
1126f46af505SJordan K. Hubbard 	else if (dos_heads < 1 || dos_heads > 256)
1127f46af505SJordan K. Hubbard 	{
1128d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1129d98b1668SPhilippe Charnier 		    current_line_number);
1130f46af505SJordan K. Hubbard 	    status = 0;
1131f46af505SJordan K. Hubbard 	}
1132f46af505SJordan K. Hubbard 
1133f46af505SJordan K. Hubbard 	if (dos_sectors < 0)
1134f46af505SJordan K. Hubbard 	{
1135d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1136d98b1668SPhilippe Charnier 		    current_line_number);
1137f46af505SJordan K. Hubbard 	    status = 0;
1138f46af505SJordan K. Hubbard 	}
1139f46af505SJordan K. Hubbard 	else if (dos_sectors < 1 || dos_sectors > 63)
1140f46af505SJordan K. Hubbard 	{
1141d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1142d98b1668SPhilippe Charnier 		    current_line_number);
1143f46af505SJordan K. Hubbard 	    status = 0;
1144f46af505SJordan K. Hubbard 	}
1145f46af505SJordan K. Hubbard 
1146f46af505SJordan K. Hubbard 	break;
1147f46af505SJordan K. Hubbard     }
1148f46af505SJordan K. Hubbard     return (status);
1149f46af505SJordan K. Hubbard }
1150f46af505SJordan K. Hubbard 
1151f46af505SJordan K. Hubbard 
1152f46af505SJordan K. Hubbard static int
1153f46af505SJordan K. Hubbard process_partition(command)
1154f46af505SJordan K. Hubbard     CMD		*command;
1155f46af505SJordan K. Hubbard {
1156f46af505SJordan K. Hubbard     int				status = 0, partition;
1157f46af505SJordan K. Hubbard     unsigned long		chunks, adj_size, max_end;
1158f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1159f46af505SJordan K. Hubbard 
1160f46af505SJordan K. Hubbard     while (1)
1161f46af505SJordan K. Hubbard     {
1162f46af505SJordan K. Hubbard 	part_processed = 1;
1163f46af505SJordan K. Hubbard 	if (command->n_args != 4)
1164f46af505SJordan K. Hubbard 	{
1165d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1166d98b1668SPhilippe Charnier 		    current_line_number);
1167f46af505SJordan K. Hubbard 	    break;
1168f46af505SJordan K. Hubbard 	}
1169f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
11704ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1171f46af505SJordan K. Hubbard 	{
1172d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1173d98b1668SPhilippe Charnier 		    current_line_number, partition);
1174f46af505SJordan K. Hubbard 	    break;
1175f46af505SJordan K. Hubbard 	}
11764ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1177f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1178f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1179f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1180f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1181f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1182f46af505SJordan K. Hubbard 
1183f46af505SJordan K. Hubbard 	if (partp->dp_typ == 0)
1184f46af505SJordan K. Hubbard 	{
1185f46af505SJordan K. Hubbard 	    /*
1186f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1187f46af505SJordan K. Hubbard 	     */
1188f46af505SJordan K. Hubbard 	    /*
1189f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1190f46af505SJordan K. Hubbard 	     */
1191f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1192f46af505SJordan K. Hubbard 	    status = 1;
1193f46af505SJordan K. Hubbard 	    break;
1194f46af505SJordan K. Hubbard 	}
1195f46af505SJordan K. Hubbard 
1196f46af505SJordan K. Hubbard 	/*
1197f46af505SJordan K. Hubbard 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1198f46af505SJordan K. Hubbard 	 */
1199f46af505SJordan K. Hubbard 	if (partp->dp_start % dos_sectors != 0)
1200f46af505SJordan K. Hubbard 	{
1201f46af505SJordan K. Hubbard 	    adj_size =
1202f46af505SJordan K. Hubbard 		(partp->dp_start / dos_sectors + 1) * dos_sectors;
1203f46af505SJordan K. Hubbard 	    if (adj_size > max_end)
1204f46af505SJordan K. Hubbard 	    {
1205f46af505SJordan K. Hubbard 		/*
1206f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1207f46af505SJordan K. Hubbard 		 */
1208d98b1668SPhilippe Charnier 		warnx(
1209d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1210d98b1668SPhilippe Charnier     a cylinder boundary",
1211d98b1668SPhilippe Charnier 			current_line_number, partition);
1212f46af505SJordan K. Hubbard 		break;
1213f46af505SJordan K. Hubbard 	    }
1214d98b1668SPhilippe Charnier 	    warnx(
1215d98b1668SPhilippe Charnier 	"WARNING: adjusting start offset of partition '%d' from %lu\n\
1216d98b1668SPhilippe Charnier     to %lu, to round to an head boundary",
1217d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_start, adj_size);
1218f46af505SJordan K. Hubbard 	    partp->dp_start = adj_size;
1219f46af505SJordan K. Hubbard 	}
1220f46af505SJordan K. Hubbard 
1221f46af505SJordan K. Hubbard 	/*
1222f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1223f46af505SJordan K. Hubbard 	 * boundary.
1224f46af505SJordan K. Hubbard 	 */
1225f46af505SJordan K. Hubbard 	chunks =
1226f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1227f46af505SJordan K. Hubbard 	adj_size = chunks - partp->dp_start;
1228f46af505SJordan K. Hubbard 	if (adj_size != partp->dp_size)
1229f46af505SJordan K. Hubbard 	{
1230d98b1668SPhilippe Charnier 	    warnx(
1231d98b1668SPhilippe Charnier 	"WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1232d98b1668SPhilippe Charnier     to round to a cylinder boundary",
1233d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_size, adj_size);
1234f46af505SJordan K. Hubbard 	    if (chunks > 0)
1235f46af505SJordan K. Hubbard 	    {
1236f46af505SJordan K. Hubbard 		partp->dp_size = adj_size;
1237f46af505SJordan K. Hubbard 	    }
1238f46af505SJordan K. Hubbard 	    else
1239f46af505SJordan K. Hubbard 	    {
1240f46af505SJordan K. Hubbard 		partp->dp_size = 0;
1241f46af505SJordan K. Hubbard 	    }
1242f46af505SJordan K. Hubbard 	}
1243f46af505SJordan K. Hubbard 	if (partp->dp_size < 1)
1244f46af505SJordan K. Hubbard 	{
1245d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: size for partition '%d' is zero",
1246d98b1668SPhilippe Charnier 		    current_line_number, partition);
1247f46af505SJordan K. Hubbard 	    break;
1248f46af505SJordan K. Hubbard 	}
1249f46af505SJordan K. Hubbard 
1250f46af505SJordan K. Hubbard 	dos(partp->dp_start, partp->dp_size,
1251f46af505SJordan K. Hubbard 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1252f46af505SJordan K. Hubbard 	dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1253f46af505SJordan K. Hubbard 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1254f46af505SJordan K. Hubbard 	status = 1;
1255f46af505SJordan K. Hubbard 	break;
1256f46af505SJordan K. Hubbard     }
1257f46af505SJordan K. Hubbard     return (status);
1258f46af505SJordan K. Hubbard }
1259f46af505SJordan K. Hubbard 
1260f46af505SJordan K. Hubbard 
1261f46af505SJordan K. Hubbard static int
1262f46af505SJordan K. Hubbard process_active(command)
1263f46af505SJordan K. Hubbard     CMD		*command;
1264f46af505SJordan K. Hubbard {
1265f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1266f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1267f46af505SJordan K. Hubbard 
1268f46af505SJordan K. Hubbard     while (1)
1269f46af505SJordan K. Hubbard     {
1270f46af505SJordan K. Hubbard 	active_processed = 1;
1271f46af505SJordan K. Hubbard 	if (command->n_args != 1)
1272f46af505SJordan K. Hubbard 	{
1273d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1274d98b1668SPhilippe Charnier 		    current_line_number);
1275f46af505SJordan K. Hubbard 	    status = 0;
1276f46af505SJordan K. Hubbard 	    break;
1277f46af505SJordan K. Hubbard 	}
1278f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
12794ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1280f46af505SJordan K. Hubbard 	{
1281d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1282d98b1668SPhilippe Charnier 		    current_line_number, partition);
1283f46af505SJordan K. Hubbard 	    break;
1284f46af505SJordan K. Hubbard 	}
1285f46af505SJordan K. Hubbard 	/*
1286f46af505SJordan K. Hubbard 	 * Reset active partition
1287f46af505SJordan K. Hubbard 	 */
1288f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1289f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1290f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
12914ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1292f46af505SJordan K. Hubbard 
1293f46af505SJordan K. Hubbard 	status = 1;
1294f46af505SJordan K. Hubbard 	break;
1295f46af505SJordan K. Hubbard     }
1296f46af505SJordan K. Hubbard     return (status);
1297f46af505SJordan K. Hubbard }
1298f46af505SJordan K. Hubbard 
1299f46af505SJordan K. Hubbard 
1300f46af505SJordan K. Hubbard static int
1301f46af505SJordan K. Hubbard process_line(line)
1302f46af505SJordan K. Hubbard     char	*line;
1303f46af505SJordan K. Hubbard {
1304f46af505SJordan K. Hubbard     CMD		command;
1305f46af505SJordan K. Hubbard     int		status = 1;
1306f46af505SJordan K. Hubbard 
1307f46af505SJordan K. Hubbard     while (1)
1308f46af505SJordan K. Hubbard     {
1309f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1310f46af505SJordan K. Hubbard 	switch (command.cmd)
1311f46af505SJordan K. Hubbard 	{
1312f46af505SJordan K. Hubbard 	case 0:
1313f46af505SJordan K. Hubbard 	    /*
1314f46af505SJordan K. Hubbard 	     * Comment or blank line
1315f46af505SJordan K. Hubbard 	     */
1316f46af505SJordan K. Hubbard 	    break;
1317f46af505SJordan K. Hubbard 	case 'g':
1318f46af505SJordan K. Hubbard 	    /*
1319f46af505SJordan K. Hubbard 	     * Set geometry
1320f46af505SJordan K. Hubbard 	     */
1321f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1322f46af505SJordan K. Hubbard 	    break;
1323f46af505SJordan K. Hubbard 	case 'p':
1324f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1325f46af505SJordan K. Hubbard 	    break;
1326f46af505SJordan K. Hubbard 	case 'a':
1327f46af505SJordan K. Hubbard 	    status = process_active(&command);
1328f46af505SJordan K. Hubbard 	    break;
1329f46af505SJordan K. Hubbard 	default:
1330f46af505SJordan K. Hubbard 	    status = 0;
1331f46af505SJordan K. Hubbard 	    break;
1332f46af505SJordan K. Hubbard 	}
1333f46af505SJordan K. Hubbard 	break;
1334f46af505SJordan K. Hubbard     }
1335f46af505SJordan K. Hubbard     return (status);
1336f46af505SJordan K. Hubbard }
1337f46af505SJordan K. Hubbard 
1338f46af505SJordan K. Hubbard 
1339f46af505SJordan K. Hubbard static int
1340f46af505SJordan K. Hubbard read_config(config_file)
1341f46af505SJordan K. Hubbard     char *config_file;
1342f46af505SJordan K. Hubbard {
1343f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1344f46af505SJordan K. Hubbard     int		status = 1;
1345f46af505SJordan K. Hubbard     char	buf[1010];
1346f46af505SJordan K. Hubbard 
1347f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1348f46af505SJordan K. Hubbard 		   function */
1349f46af505SJordan K. Hubbard     {
1350f46af505SJordan K. Hubbard 	if (strcmp(config_file, "-") != 0)
1351f46af505SJordan K. Hubbard 	{
1352f46af505SJordan K. Hubbard 	    /*
1353f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1354f46af505SJordan K. Hubbard 	     */
1355f46af505SJordan K. Hubbard 	    if ((fp = fopen(config_file, "r")) == NULL)
1356f46af505SJordan K. Hubbard 	    {
1357f46af505SJordan K. Hubbard 		status = 0;
1358f46af505SJordan K. Hubbard 		break;
1359f46af505SJordan K. Hubbard 	    }
1360f46af505SJordan K. Hubbard 	}
1361f46af505SJordan K. Hubbard 	else
1362f46af505SJordan K. Hubbard 	{
1363f46af505SJordan K. Hubbard 	    fp = stdin;
1364f46af505SJordan K. Hubbard 	}
1365f46af505SJordan K. Hubbard 	current_line_number = 0;
1366f46af505SJordan K. Hubbard 	while (!feof(fp))
1367f46af505SJordan K. Hubbard 	{
1368f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1369f46af505SJordan K. Hubbard 	    {
1370f46af505SJordan K. Hubbard 		break;
1371f46af505SJordan K. Hubbard 	    }
1372f46af505SJordan K. Hubbard 	    ++current_line_number;
1373f46af505SJordan K. Hubbard 	    status = process_line(buf);
1374f46af505SJordan K. Hubbard 	    if (status == 0)
1375f46af505SJordan K. Hubbard 	    {
1376f46af505SJordan K. Hubbard 		break;
1377f46af505SJordan K. Hubbard 	    }
1378f46af505SJordan K. Hubbard 	}
1379f46af505SJordan K. Hubbard 	break;
1380f46af505SJordan K. Hubbard     }
1381f46af505SJordan K. Hubbard     if (fp)
1382f46af505SJordan K. Hubbard     {
1383f46af505SJordan K. Hubbard 	/*
1384f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1385f46af505SJordan K. Hubbard 	 */
1386f46af505SJordan K. Hubbard 	fclose(fp);
1387f46af505SJordan K. Hubbard     }
1388f46af505SJordan K. Hubbard     return (status);
1389f46af505SJordan K. Hubbard }
1390f46af505SJordan K. Hubbard 
1391f46af505SJordan K. Hubbard 
1392f46af505SJordan K. Hubbard static void
1393f46af505SJordan K. Hubbard reset_boot(void)
1394f46af505SJordan K. Hubbard {
1395f46af505SJordan K. Hubbard     int				i;
1396f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1397f46af505SJordan K. Hubbard 
1398f46af505SJordan K. Hubbard     init_boot();
1399f46af505SJordan K. Hubbard     for (i = 0; i < 4; ++i)
1400f46af505SJordan K. Hubbard     {
1401f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1402f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1403f46af505SJordan K. Hubbard     }
1404f46af505SJordan K. Hubbard }
1405