xref: /freebsd/sbin/fdisk/fdisk.c (revision 26721a89ffd340c4bb267fb533b0d8598a873367)
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[] =
2926721a89SRobert Nordier 	"$Id: fdisk.c,v 1.28 1999/01/22 11:54:17 rnordier Exp $";
30d98b1668SPhilippe Charnier #endif /* not lint */
31d98b1668SPhilippe Charnier 
325b81b6b3SRodney W. Grimes #include <sys/disklabel.h>
335b81b6b3SRodney W. Grimes #include <sys/stat.h>
34d98b1668SPhilippe Charnier #include <ctype.h>
355b81b6b3SRodney W. Grimes #include <fcntl.h>
36d98b1668SPhilippe Charnier #include <err.h>
37d98b1668SPhilippe Charnier #include <errno.h>
38d98b1668SPhilippe Charnier #include <stdio.h>
39d98b1668SPhilippe Charnier #include <stdlib.h>
40d98b1668SPhilippe Charnier #include <string.h>
414be1e61bSAlexander Langer #include <unistd.h>
425b81b6b3SRodney W. Grimes 
435b81b6b3SRodney W. Grimes int iotest;
445b81b6b3SRodney W. Grimes 
455b81b6b3SRodney W. Grimes #define LBUF 100
465b81b6b3SRodney W. Grimes static char lbuf[LBUF];
475b81b6b3SRodney W. Grimes 
485b81b6b3SRodney W. Grimes /*
495b81b6b3SRodney W. Grimes  *
505b81b6b3SRodney W. Grimes  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
515b81b6b3SRodney W. Grimes  *
525b81b6b3SRodney W. Grimes  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
535b81b6b3SRodney W. Grimes  *	Copyright (c) 1989	Robert. V. Baron
545b81b6b3SRodney W. Grimes  *	Created.
555b81b6b3SRodney W. Grimes  */
565b81b6b3SRodney W. Grimes 
575b81b6b3SRodney W. Grimes #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
585b81b6b3SRodney W. Grimes #define Hex(str, ans, tmp) if (hex(str, &tmp, ans)) ans = tmp
595b81b6b3SRodney W. Grimes #define String(str, ans, len) {char *z = ans; char **dflt = &z; if (string(str, dflt)) strncpy(ans, *dflt, len); }
605b81b6b3SRodney W. Grimes 
615b81b6b3SRodney W. Grimes #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
625b81b6b3SRodney W. Grimes 
637cb29d33SSøren Schmidt #define MAX_SEC_SIZE 2048	/* maximum section size that is supported */
647cb29d33SSøren Schmidt #define MIN_SEC_SIZE 512	/* the sector size to start sensing at */
657cb29d33SSøren Schmidt int secsize = 0;		/* the sensed sector size */
665b81b6b3SRodney W. Grimes 
67e3038c6eSJoerg Wunsch const char *disk;
68e3038c6eSJoerg Wunsch const char *disks[] =
69e3038c6eSJoerg Wunsch {
70698b4defSJoerg Wunsch   "/dev/rwd0", "/dev/rda0", "/dev/rod0", 0
71e3038c6eSJoerg Wunsch };
72e3038c6eSJoerg Wunsch 
735b81b6b3SRodney W. Grimes struct disklabel disklabel;		/* disk parameters */
745b81b6b3SRodney W. Grimes 
755b81b6b3SRodney W. Grimes int cyls, sectors, heads, cylsecs, disksecs;
765b81b6b3SRodney W. Grimes 
775b81b6b3SRodney W. Grimes struct mboot
785b81b6b3SRodney W. Grimes {
79d98b1668SPhilippe Charnier 	unsigned char padding[2]; /* force the longs to be long aligned */
805b81b6b3SRodney W. Grimes 	unsigned char bootinst[DOSPARTOFF];
815b81b6b3SRodney W. Grimes 	struct	dos_partition parts[4];
825b81b6b3SRodney W. Grimes 	unsigned short int	signature;
837cb29d33SSøren Schmidt 	/* room to read in MBRs that are bigger then DEV_BSIZE */
847cb29d33SSøren Schmidt 	unsigned char large_sector_overflow[MAX_SEC_SIZE-MIN_SEC_SIZE];
855b81b6b3SRodney W. Grimes };
865b81b6b3SRodney W. Grimes struct mboot mboot;
875b81b6b3SRodney W. Grimes 
885b81b6b3SRodney W. Grimes #define ACTIVE 0x80
895b81b6b3SRodney W. Grimes #define BOOT_MAGIC 0xAA55
905b81b6b3SRodney W. Grimes 
915b81b6b3SRodney W. Grimes int dos_cyls;
925b81b6b3SRodney W. Grimes int dos_heads;
935b81b6b3SRodney W. Grimes int dos_sectors;
945b81b6b3SRodney W. Grimes int dos_cylsecs;
955b81b6b3SRodney W. Grimes 
965b81b6b3SRodney W. Grimes #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
975b81b6b3SRodney W. Grimes #define DOSCYL(c)	(c & 0xff)
985b81b6b3SRodney W. Grimes static int partition = -1;
995b81b6b3SRodney W. Grimes 
1005b81b6b3SRodney W. Grimes 
101f46af505SJordan K. Hubbard #define MAX_ARGS	10
102f46af505SJordan K. Hubbard 
103f46af505SJordan K. Hubbard static int	current_line_number;
104f46af505SJordan K. Hubbard 
105f46af505SJordan K. Hubbard static int	geom_processed = 0;
106f46af505SJordan K. Hubbard static int	part_processed = 0;
107f46af505SJordan K. Hubbard static int	active_processed = 0;
108f46af505SJordan K. Hubbard 
109f46af505SJordan K. Hubbard 
110f46af505SJordan K. Hubbard typedef struct cmd {
111f46af505SJordan K. Hubbard     char		cmd;
112f46af505SJordan K. Hubbard     int			n_args;
113f46af505SJordan K. Hubbard     struct arg {
114f46af505SJordan K. Hubbard 	char	argtype;
115f46af505SJordan K. Hubbard 	int	arg_val;
116f46af505SJordan K. Hubbard     }			args[MAX_ARGS];
117f46af505SJordan K. Hubbard } CMD;
118f46af505SJordan K. Hubbard 
119f46af505SJordan K. Hubbard 
12026721a89SRobert Nordier static int B_flag  = 0;		/* replace boot code */
1215b81b6b3SRodney W. Grimes static int a_flag  = 0;		/* set active partition */
12226721a89SRobert Nordier static char *b_flag = NULL;	/* path to boot code */
1235b81b6b3SRodney W. Grimes static int i_flag  = 0;		/* replace partition data */
1245b81b6b3SRodney W. Grimes static int u_flag  = 0;		/* update partition data */
125f46af505SJordan K. Hubbard static int t_flag  = 0;		/* test only, if f_flag is given */
126f46af505SJordan K. Hubbard static char *f_flag = NULL;	/* Read config info from file */
127f46af505SJordan K. Hubbard static int v_flag  = 0;		/* Be verbose */
1285b81b6b3SRodney W. Grimes 
1295b81b6b3SRodney W. Grimes struct part_type
1305b81b6b3SRodney W. Grimes {
1315b81b6b3SRodney W. Grimes  unsigned char type;
1325b81b6b3SRodney W. Grimes  char *name;
1335b81b6b3SRodney W. Grimes }part_types[] =
1345b81b6b3SRodney W. Grimes {
1355b81b6b3SRodney W. Grimes 	 {0x00, "unused"}
1365b81b6b3SRodney W. Grimes 	,{0x01, "Primary DOS with 12 bit FAT"}
1375b81b6b3SRodney W. Grimes 	,{0x02, "XENIX / filesystem"}
1385b81b6b3SRodney W. Grimes 	,{0x03, "XENIX /usr filesystem"}
13926555b64SAndrey A. Chernov 	,{0x04, "Primary DOS with 16 bit FAT (<= 32MB)"}
1405b81b6b3SRodney W. Grimes 	,{0x05, "Extended DOS"}
1415b81b6b3SRodney W. Grimes 	,{0x06, "Primary 'big' DOS (> 32MB)"}
142680426beSDavid E. O'Brien 	,{0x07, "OS/2 HPFS, NTFS, QNX or Advanced UNIX"}
1435b81b6b3SRodney W. Grimes 	,{0x08, "AIX filesystem"}
1445b81b6b3SRodney W. Grimes 	,{0x09, "AIX boot partition or Coherent"}
1455b81b6b3SRodney W. Grimes 	,{0x0A, "OS/2 Boot Manager or OPUS"}
14626555b64SAndrey A. Chernov 	,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
14726555b64SAndrey A. Chernov 	,{0x0C, "DOS or Windows 95 with 32 bit FAT, LBA"}
14826555b64SAndrey A. Chernov 	,{0x0E, "Primary 'big' DOS (> 32MB, LBA)"}
14926555b64SAndrey A. Chernov 	,{0x0F, "Extended DOS, LBA"}
1505b81b6b3SRodney W. Grimes 	,{0x10, "OPUS"}
1515b81b6b3SRodney W. Grimes 	,{0x40, "VENIX 286"}
1525b81b6b3SRodney W. Grimes 	,{0x50, "DM"}
1535b81b6b3SRodney W. Grimes 	,{0x51, "DM"}
1545b81b6b3SRodney W. Grimes 	,{0x52, "CP/M or Microport SysV/AT"}
1555b81b6b3SRodney W. Grimes 	,{0x56, "GB"}
1565b81b6b3SRodney W. Grimes 	,{0x61, "Speed"}
1575b81b6b3SRodney W. Grimes 	,{0x63, "ISC UNIX, other System V/386, GNU HURD or Mach"}
1585b81b6b3SRodney W. Grimes 	,{0x64, "Novell Netware 2.xx"}
1595b81b6b3SRodney W. Grimes 	,{0x65, "Novell Netware 3.xx"}
1605b81b6b3SRodney W. Grimes 	,{0x75, "PCIX"}
1615b81b6b3SRodney W. Grimes 	,{0x80, "Minix 1.1 ... 1.4a"}
1625b81b6b3SRodney W. Grimes 	,{0x81, "Minix 1.4b ... 1.5.10"}
16363ab6f1cSDavid E. O'Brien 	,{0x82, "Linux swap or Solaris x86"}
16449f7c177SJordan K. Hubbard 	,{0x83, "Linux filesystem"}
1655b81b6b3SRodney W. Grimes 	,{0x93, "Amoeba filesystem"}
1665b81b6b3SRodney W. Grimes 	,{0x94, "Amoeba bad block table"}
167d1b7313fSJoseph Koshy 	,{0x9F, "BSD/OS"}
16849f7c177SJordan K. Hubbard 	,{0xA5, "FreeBSD/NetBSD/386BSD"}
169e37a137dSWarner Losh 	,{0xA6, "OpenBSD"}
1705f0c9424SGary Palmer 	,{0xA7, "NEXTSTEP"}
171ef80de33SAlexander Langer 	,{0xA9, "NetBSD"}
1725b81b6b3SRodney W. Grimes 	,{0xB7, "BSDI BSD/386 filesystem"}
1735b81b6b3SRodney W. Grimes 	,{0xB8, "BSDI BSD/386 swap"}
1745b81b6b3SRodney W. Grimes 	,{0xDB, "Concurrent CPM or C.DOS or CTOS"}
1755b81b6b3SRodney W. Grimes 	,{0xE1, "Speed"}
1765b81b6b3SRodney W. Grimes 	,{0xE3, "Speed"}
1775b81b6b3SRodney W. Grimes 	,{0xE4, "Speed"}
1785b81b6b3SRodney W. Grimes 	,{0xF1, "Speed"}
1795b81b6b3SRodney W. Grimes 	,{0xF2, "DOS 3.3+ Secondary"}
1805b81b6b3SRodney W. Grimes 	,{0xF4, "Speed"}
1815b81b6b3SRodney W. Grimes 	,{0xFF, "BBT (Bad Blocks Table)"}
1825b81b6b3SRodney W. Grimes };
1835b81b6b3SRodney W. Grimes 
1844be1e61bSAlexander Langer static void print_s0(int which);
1854be1e61bSAlexander Langer static void print_part(int i);
1864be1e61bSAlexander Langer static void init_sector0(unsigned long start);
187f46af505SJordan K. Hubbard static void init_boot(void);
1884be1e61bSAlexander Langer static void change_part(int i);
1894be1e61bSAlexander Langer static void print_params();
1904be1e61bSAlexander Langer static void change_active(int which);
19112d910e9SRobert Nordier static void change_code();
1924be1e61bSAlexander Langer static void get_params_to_use();
193e2975440SBruce Evans static void dos(int sec, int size, unsigned char *c, unsigned char *s,
194e2975440SBruce Evans 		unsigned char *h);
1954be1e61bSAlexander Langer static int open_disk(int u_flag);
1964be1e61bSAlexander Langer static ssize_t read_disk(off_t sector, void *buf);
1974be1e61bSAlexander Langer static ssize_t write_disk(off_t sector, void *buf);
1984be1e61bSAlexander Langer static int get_params();
1994be1e61bSAlexander Langer static int read_s0();
2004be1e61bSAlexander Langer static int write_s0();
2014be1e61bSAlexander Langer static int ok(char *str);
2024be1e61bSAlexander Langer static int decimal(char *str, int *num, int deflt);
2034be1e61bSAlexander Langer static char *get_type(int type);
204f46af505SJordan K. Hubbard static int read_config(char *config_file);
205f46af505SJordan K. Hubbard static void reset_boot(void);
206d98b1668SPhilippe Charnier static void usage(void);
2074be1e61bSAlexander Langer #if 0
2084be1e61bSAlexander Langer static int hex(char *str, int *num, int deflt);
2094be1e61bSAlexander Langer static int string(char *str, char **ans);
2104be1e61bSAlexander Langer #endif
2115b81b6b3SRodney W. Grimes 
2124be1e61bSAlexander Langer 
2134be1e61bSAlexander Langer int
2144be1e61bSAlexander Langer main(int argc, char *argv[])
2155b81b6b3SRodney W. Grimes {
21626721a89SRobert Nordier 	int	c, i;
2175b81b6b3SRodney W. Grimes 
21826721a89SRobert Nordier 	while ((c = getopt(argc, argv, "Bab:f:ituv1234")) != -1)
21926721a89SRobert Nordier 		switch (c) {
22026721a89SRobert Nordier 		case 'B':
22126721a89SRobert Nordier 			B_flag = 1;
2224ddd60b9SBrian Somers 			break;
2235b81b6b3SRodney W. Grimes 		case 'a':
2245b81b6b3SRodney W. Grimes 			a_flag = 1;
2255b81b6b3SRodney W. Grimes 			break;
22612d910e9SRobert Nordier 		case 'b':
22726721a89SRobert Nordier 			b_flag = optarg;
22812d910e9SRobert Nordier 			break;
229f46af505SJordan K. Hubbard 		case 'f':
23026721a89SRobert Nordier 			f_flag = optarg;
231f46af505SJordan K. Hubbard 			break;
2325b81b6b3SRodney W. Grimes 		case 'i':
2335b81b6b3SRodney W. Grimes 			i_flag = 1;
2345b81b6b3SRodney W. Grimes 			break;
235f46af505SJordan K. Hubbard 		case 't':
236f46af505SJordan K. Hubbard 			t_flag = 1;
23726721a89SRobert Nordier 			break;
23826721a89SRobert Nordier 		case 'u':
23926721a89SRobert Nordier 			u_flag = 1;
24026721a89SRobert Nordier 			break;
241f46af505SJordan K. Hubbard 		case 'v':
242f46af505SJordan K. Hubbard 			v_flag = 1;
243f46af505SJordan K. Hubbard 			break;
24426721a89SRobert Nordier 		case '1':
24526721a89SRobert Nordier 		case '2':
24626721a89SRobert Nordier 		case '3':
24726721a89SRobert Nordier 		case '4':
24826721a89SRobert Nordier 			partition = c - '0';
24926721a89SRobert Nordier 			break;
2505b81b6b3SRodney W. Grimes 		default:
251d98b1668SPhilippe Charnier 			usage();
2525b81b6b3SRodney W. Grimes 		}
25326721a89SRobert Nordier 	if (f_flag || i_flag)
25426721a89SRobert Nordier 		u_flag = 1;
25526721a89SRobert Nordier 	if (t_flag)
25626721a89SRobert Nordier 		v_flag = 1;
25726721a89SRobert Nordier 	argc -= optind;
25826721a89SRobert Nordier 	argv += optind;
2595b81b6b3SRodney W. Grimes 
2605b81b6b3SRodney W. Grimes 	if (argc > 0)
261e3038c6eSJoerg Wunsch 	{
262e3038c6eSJoerg Wunsch 		static char realname[12];
263e3038c6eSJoerg Wunsch 
264e3038c6eSJoerg Wunsch 		if(strncmp(argv[0], "/dev", 4) == 0)
2655b81b6b3SRodney W. Grimes 			disk = argv[0];
266e3038c6eSJoerg Wunsch 		else
267e3038c6eSJoerg Wunsch 		{
268e3038c6eSJoerg Wunsch 			snprintf(realname, 12, "/dev/r%s", argv[0]);
269e3038c6eSJoerg Wunsch 			disk = realname;
270e3038c6eSJoerg Wunsch 		}
2715b81b6b3SRodney W. Grimes 
2725b81b6b3SRodney W. Grimes 		if (open_disk(u_flag) < 0)
273d98b1668SPhilippe Charnier 			err(1, "cannot open disk %s", disk);
274e3038c6eSJoerg Wunsch 	}
275e3038c6eSJoerg Wunsch 	else
276e3038c6eSJoerg Wunsch 	{
27726721a89SRobert Nordier 		int rv = 0;
278e3038c6eSJoerg Wunsch 
279e3038c6eSJoerg Wunsch 		for(i = 0; disks[i]; i++)
280e3038c6eSJoerg Wunsch 		{
281e3038c6eSJoerg Wunsch 			disk = disks[i];
282e3038c6eSJoerg Wunsch 			rv = open_disk(u_flag);
283e3038c6eSJoerg Wunsch 			if(rv != -2) break;
284e3038c6eSJoerg Wunsch 		}
285e3038c6eSJoerg Wunsch 		if(rv < 0)
286d98b1668SPhilippe Charnier 			err(1, "cannot open any disk");
287e3038c6eSJoerg Wunsch 	}
2885b81b6b3SRodney W. Grimes 
2895b81b6b3SRodney W. Grimes 	printf("******* Working on device %s *******\n",disk);
290f46af505SJordan K. Hubbard 
291f46af505SJordan K. Hubbard 	if (f_flag)
292f46af505SJordan K. Hubbard 	{
293f46af505SJordan K. Hubbard 	    if (read_s0() || i_flag)
294f46af505SJordan K. Hubbard 	    {
295f46af505SJordan K. Hubbard 		reset_boot();
296f46af505SJordan K. Hubbard 	    }
297f46af505SJordan K. Hubbard 
298f46af505SJordan K. Hubbard 	    if (!read_config(f_flag))
299f46af505SJordan K. Hubbard 	    {
300f46af505SJordan K. Hubbard 		exit(1);
301f46af505SJordan K. Hubbard 	    }
302f46af505SJordan K. Hubbard 	    if (v_flag)
303f46af505SJordan K. Hubbard 	    {
304f46af505SJordan K. Hubbard 		print_s0(-1);
305f46af505SJordan K. Hubbard 	    }
306f46af505SJordan K. Hubbard 	    if (!t_flag)
307f46af505SJordan K. Hubbard 	    {
308f46af505SJordan K. Hubbard 		write_s0();
309f46af505SJordan K. Hubbard 	    }
310f46af505SJordan K. Hubbard 	}
311f46af505SJordan K. Hubbard 	else
312f46af505SJordan K. Hubbard 	{
3135b81b6b3SRodney W. Grimes 	    if(u_flag)
3145b81b6b3SRodney W. Grimes 	    {
3155b81b6b3SRodney W. Grimes 		get_params_to_use();
3165b81b6b3SRodney W. Grimes 	    }
3175b81b6b3SRodney W. Grimes 	    else
3185b81b6b3SRodney W. Grimes 	    {
3195b81b6b3SRodney W. Grimes 		print_params();
3205b81b6b3SRodney W. Grimes 	    }
3215b81b6b3SRodney W. Grimes 
3225b81b6b3SRodney W. Grimes 	    if (read_s0())
3235b81b6b3SRodney W. Grimes 		init_sector0(1);
3245b81b6b3SRodney W. Grimes 
3257cb29d33SSøren Schmidt 	    printf("Media sector size is %d\n", secsize);
3265b81b6b3SRodney W. Grimes 	    printf("Warning: BIOS sector numbering starts with sector 1\n");
3275b81b6b3SRodney W. Grimes 	    printf("Information from DOS bootblock is:\n");
3285b81b6b3SRodney W. Grimes 	    if (partition == -1)
3294ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3305b81b6b3SRodney W. Grimes 		    change_part(i);
3315b81b6b3SRodney W. Grimes 	    else
3325b81b6b3SRodney W. Grimes 		change_part(partition);
3335b81b6b3SRodney W. Grimes 
3345b81b6b3SRodney W. Grimes 	    if (u_flag || a_flag)
3355b81b6b3SRodney W. Grimes 		change_active(partition);
3365b81b6b3SRodney W. Grimes 
33726721a89SRobert Nordier 	    if (B_flag)
33812d910e9SRobert Nordier 		change_code();
33912d910e9SRobert Nordier 
34026721a89SRobert Nordier 	    if (u_flag || a_flag || B_flag) {
341f46af505SJordan K. Hubbard 		if (!t_flag)
342f46af505SJordan K. Hubbard 		{
3435b81b6b3SRodney W. Grimes 		    printf("\nWe haven't changed the partition table yet.  ");
3445b81b6b3SRodney W. Grimes 		    printf("This is your last chance.\n");
345f46af505SJordan K. Hubbard 		}
3465b81b6b3SRodney W. Grimes 		print_s0(-1);
347f46af505SJordan K. Hubbard 		if (!t_flag)
348f46af505SJordan K. Hubbard 		{
3495b81b6b3SRodney W. Grimes 		    if (ok("Should we write new partition table?"))
3505b81b6b3SRodney W. Grimes 			write_s0();
3515b81b6b3SRodney W. Grimes 		}
352f46af505SJordan K. Hubbard 		else
353f46af505SJordan K. Hubbard 		{
354f46af505SJordan K. Hubbard 		    printf("\n-t flag specified -- partition table not written.\n");
355f46af505SJordan K. Hubbard 		}
356f46af505SJordan K. Hubbard 	    }
357f46af505SJordan K. Hubbard 	}
3585b81b6b3SRodney W. Grimes 
3595b81b6b3SRodney W. Grimes 	exit(0);
360d98b1668SPhilippe Charnier }
3615b81b6b3SRodney W. Grimes 
362d98b1668SPhilippe Charnier static void
363d98b1668SPhilippe Charnier usage()
364d98b1668SPhilippe Charnier {
36526721a89SRobert Nordier 	fprintf(stderr, "%s%s",
36626721a89SRobert Nordier 		"usage: fdisk [-Baitu] [-b bootcode] [-1234] [disk]\n",
36726721a89SRobert Nordier  		"       fdisk -f configfile [-itv] [disk]\n");
368d98b1668SPhilippe Charnier         exit(1);
3695b81b6b3SRodney W. Grimes }
3705b81b6b3SRodney W. Grimes 
3714be1e61bSAlexander Langer static void
3724be1e61bSAlexander Langer print_s0(int which)
3735b81b6b3SRodney W. Grimes {
3745b81b6b3SRodney W. Grimes int	i;
3755b81b6b3SRodney W. Grimes 
3765b81b6b3SRodney W. Grimes 	print_params();
3775b81b6b3SRodney W. Grimes 	printf("Information from DOS bootblock is:\n");
3785b81b6b3SRodney W. Grimes 	if (which == -1)
3794ddd60b9SBrian Somers 		for (i = 1; i <= NDOSPART; i++)
3805b81b6b3SRodney W. Grimes 			printf("%d: ", i), print_part(i);
3815b81b6b3SRodney W. Grimes 	else
3825b81b6b3SRodney W. Grimes 		print_part(which);
3835b81b6b3SRodney W. Grimes }
3845b81b6b3SRodney W. Grimes 
3855b81b6b3SRodney W. Grimes static struct dos_partition mtpart = { 0 };
3865b81b6b3SRodney W. Grimes 
3874be1e61bSAlexander Langer static void
3884be1e61bSAlexander Langer print_part(int i)
3895b81b6b3SRodney W. Grimes {
390637fe2f7SJustin T. Gibbs 	struct	  dos_partition *partp;
391637fe2f7SJustin T. Gibbs 	u_int64_t part_mb;
3925b81b6b3SRodney W. Grimes 
3934ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + i - 1;
3945b81b6b3SRodney W. Grimes 
3955b81b6b3SRodney W. Grimes 	if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
3965b81b6b3SRodney W. Grimes 		printf("<UNUSED>\n");
3975b81b6b3SRodney W. Grimes 		return;
3985b81b6b3SRodney W. Grimes 	}
399637fe2f7SJustin T. Gibbs 	/*
400637fe2f7SJustin T. Gibbs 	 * Be careful not to overflow.
401637fe2f7SJustin T. Gibbs 	 */
402637fe2f7SJustin T. Gibbs 	part_mb = partp->dp_size;
403637fe2f7SJustin T. Gibbs 	part_mb *= secsize;
404637fe2f7SJustin T. Gibbs 	part_mb /= (1024 * 1024);
4055b81b6b3SRodney W. Grimes 	printf("sysid %d,(%s)\n", partp->dp_typ, get_type(partp->dp_typ));
406ba198492SBruce Evans 	printf("    start %lu, size %lu (%qd Meg), flag %x%s\n",
407ba198492SBruce Evans 		(u_long)partp->dp_start,
408ba198492SBruce Evans 		(u_long)partp->dp_size,
409637fe2f7SJustin T. Gibbs 		part_mb,
410680426beSDavid E. O'Brien 		partp->dp_flag,
411680426beSDavid E. O'Brien 		partp->dp_flag == ACTIVE ? " (active)" : "");
4125b81b6b3SRodney W. Grimes 	printf("\tbeg: cyl %d/ sector %d/ head %d;\n\tend: cyl %d/ sector %d/ head %d\n"
4135b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_scyl, partp->dp_ssect)
4145b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_ssect)
4155b81b6b3SRodney W. Grimes 		,partp->dp_shd
4165b81b6b3SRodney W. Grimes 		,DPCYL(partp->dp_ecyl, partp->dp_esect)
4175b81b6b3SRodney W. Grimes 		,DPSECT(partp->dp_esect)
4185b81b6b3SRodney W. Grimes 		,partp->dp_ehd);
4195b81b6b3SRodney W. Grimes }
4205b81b6b3SRodney W. Grimes 
421f46af505SJordan K. Hubbard 
422f46af505SJordan K. Hubbard static void
423f46af505SJordan K. Hubbard init_boot(void)
424f46af505SJordan K. Hubbard {
42526721a89SRobert Nordier 	const char *fname;
42626721a89SRobert Nordier 	int fd;
42726721a89SRobert Nordier 
42826721a89SRobert Nordier 	fname = b_flag ? b_flag : "/boot/mbr";
42926721a89SRobert Nordier 	if ((fd = open(fname, O_RDONLY)) == -1 ||
43026721a89SRobert Nordier 	    read(fd, mboot.bootinst, DOSPARTOFF) == -1 ||
43126721a89SRobert Nordier 	    close(fd))
43226721a89SRobert Nordier 		err(1, "%s", fname);
433f46af505SJordan K. Hubbard 	mboot.signature = BOOT_MAGIC;
434f46af505SJordan K. Hubbard }
435f46af505SJordan K. Hubbard 
436f46af505SJordan K. Hubbard 
4374be1e61bSAlexander Langer static void
4384be1e61bSAlexander Langer init_sector0(unsigned long start)
4395b81b6b3SRodney W. Grimes {
4405b81b6b3SRodney W. Grimes struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[3]);
4414be1e61bSAlexander Langer unsigned long size = disksecs - start;
4425b81b6b3SRodney W. Grimes 
443f46af505SJordan K. Hubbard 	init_boot();
4445b81b6b3SRodney W. Grimes 
4455b81b6b3SRodney W. Grimes 	partp->dp_typ = DOSPTYP_386BSD;
4465b81b6b3SRodney W. Grimes 	partp->dp_flag = ACTIVE;
4475b81b6b3SRodney W. Grimes 	partp->dp_start = start;
4485b81b6b3SRodney W. Grimes 	partp->dp_size = size;
4495b81b6b3SRodney W. Grimes 
450e2975440SBruce Evans 	dos(partp->dp_start, partp->dp_size,
451e2975440SBruce Evans 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
452e2975440SBruce Evans 	dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
453e2975440SBruce Evans 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
4545b81b6b3SRodney W. Grimes }
4555b81b6b3SRodney W. Grimes 
4564be1e61bSAlexander Langer static void
4574be1e61bSAlexander Langer change_part(int i)
4585b81b6b3SRodney W. Grimes {
4594ddd60b9SBrian Somers struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
4605b81b6b3SRodney W. Grimes 
4615b81b6b3SRodney W. Grimes     printf("The data for partition %d is:\n", i);
4625b81b6b3SRodney W. Grimes     print_part(i);
4635b81b6b3SRodney W. Grimes 
4645b81b6b3SRodney W. Grimes     if (u_flag && ok("Do you want to change it?")) {
4655b81b6b3SRodney W. Grimes 	int tmp;
4665b81b6b3SRodney W. Grimes 
4675b81b6b3SRodney W. Grimes 	if (i_flag) {
4685b81b6b3SRodney W. Grimes 		bzero((char *)partp, sizeof (struct dos_partition));
4694ddd60b9SBrian Somers 		if (i == 4) {
4705b81b6b3SRodney W. Grimes 			init_sector0(1);
4714ddd60b9SBrian Somers 			printf("\nThe static data for the DOS partition 4 has been reinitialized to:\n");
4725b81b6b3SRodney W. Grimes 			print_part(i);
4735b81b6b3SRodney W. Grimes 		}
4745b81b6b3SRodney W. Grimes 	}
4755b81b6b3SRodney W. Grimes 
4765b81b6b3SRodney W. Grimes 	do {
477680426beSDavid E. O'Brien 		Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
4785b81b6b3SRodney W. Grimes 		Decimal("start", partp->dp_start, tmp);
4795b81b6b3SRodney W. Grimes 		Decimal("size", partp->dp_size, tmp);
4805b81b6b3SRodney W. Grimes 
4814b3b45a7SJames Raynard 		if (ok("Explicitly specify beg/end address ?"))
4825b81b6b3SRodney W. Grimes 		{
4835b81b6b3SRodney W. Grimes 			int	tsec,tcyl,thd;
4845b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
4855b81b6b3SRodney W. Grimes 			thd = partp->dp_shd;
4865b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_ssect);
4875b81b6b3SRodney W. Grimes 			Decimal("beginning cylinder", tcyl, tmp);
4885b81b6b3SRodney W. Grimes 			Decimal("beginning head", thd, tmp);
4895b81b6b3SRodney W. Grimes 			Decimal("beginning sector", tsec, tmp);
4905b81b6b3SRodney W. Grimes 			partp->dp_scyl = DOSCYL(tcyl);
4915b81b6b3SRodney W. Grimes 			partp->dp_ssect = DOSSECT(tsec,tcyl);
4925b81b6b3SRodney W. Grimes 			partp->dp_shd = thd;
4935b81b6b3SRodney W. Grimes 
4945b81b6b3SRodney W. Grimes 			tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
4955b81b6b3SRodney W. Grimes 			thd = partp->dp_ehd;
4965b81b6b3SRodney W. Grimes 			tsec = DPSECT(partp->dp_esect);
4975b81b6b3SRodney W. Grimes 			Decimal("ending cylinder", tcyl, tmp);
4985b81b6b3SRodney W. Grimes 			Decimal("ending head", thd, tmp);
4995b81b6b3SRodney W. Grimes 			Decimal("ending sector", tsec, tmp);
5005b81b6b3SRodney W. Grimes 			partp->dp_ecyl = DOSCYL(tcyl);
5015b81b6b3SRodney W. Grimes 			partp->dp_esect = DOSSECT(tsec,tcyl);
5025b81b6b3SRodney W. Grimes 			partp->dp_ehd = thd;
5035b81b6b3SRodney W. Grimes 		} else {
504e2975440SBruce Evans 			dos(partp->dp_start, partp->dp_size,
5055b81b6b3SRodney W. Grimes 			    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
506e2975440SBruce Evans 			dos(partp->dp_start + partp->dp_size - 1, partp->dp_size,
5075b81b6b3SRodney W. Grimes 			    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
5085b81b6b3SRodney W. Grimes 		}
5095b81b6b3SRodney W. Grimes 
5105b81b6b3SRodney W. Grimes 		print_part(i);
5115b81b6b3SRodney W. Grimes 	} while (!ok("Are we happy with this entry?"));
5125b81b6b3SRodney W. Grimes     }
5135b81b6b3SRodney W. Grimes }
5145b81b6b3SRodney W. Grimes 
5154be1e61bSAlexander Langer static void
5165b81b6b3SRodney W. Grimes print_params()
5175b81b6b3SRodney W. Grimes {
5185b81b6b3SRodney W. Grimes 	printf("parameters extracted from in-core disklabel are:\n");
5195b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5205b81b6b3SRodney W. Grimes 			,cyls,heads,sectors,cylsecs);
5215b81b6b3SRodney W. Grimes 	if((dos_sectors > 63) || (dos_cyls > 1023) || (dos_heads > 255))
5225b81b6b3SRodney W. Grimes 		printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
5235b81b6b3SRodney W. Grimes 	printf("parameters to be used for BIOS calculations are:\n");
5245b81b6b3SRodney W. Grimes 	printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
5255b81b6b3SRodney W. Grimes 		,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
5265b81b6b3SRodney W. Grimes }
5275b81b6b3SRodney W. Grimes 
5284be1e61bSAlexander Langer static void
5294be1e61bSAlexander Langer change_active(int which)
5305b81b6b3SRodney W. Grimes {
5315b81b6b3SRodney W. Grimes int i;
5324ddd60b9SBrian Somers int active = 4, tmp;
5335b81b6b3SRodney W. Grimes struct dos_partition *partp = ((struct dos_partition *) &mboot.parts);
5345b81b6b3SRodney W. Grimes 
5355b81b6b3SRodney W. Grimes 	if (a_flag && which != -1)
5365b81b6b3SRodney W. Grimes 		active = which;
5370b461cd7SBruce Evans 	if (!ok("Do you want to change the active partition?"))
5380b461cd7SBruce Evans 		return;
539680426beSDavid E. O'Brien setactive:
540680426beSDavid E. O'Brien 	active = 4;
541680426beSDavid E. O'Brien 	do {
5425b81b6b3SRodney W. Grimes 		Decimal("active partition", active, tmp);
543680426beSDavid E. O'Brien 		if (active < 1 || 4 < active) {
544680426beSDavid E. O'Brien 			printf("Active partition number must be in range 1-4."
545680426beSDavid E. O'Brien 					"  Try again.\n");
546680426beSDavid E. O'Brien 			goto setactive;
547680426beSDavid E. O'Brien 		}
548680426beSDavid E. O'Brien 	} while (!ok("Are you happy with this choice"));
5495b81b6b3SRodney W. Grimes 	for (i = 0; i < NDOSPART; i++)
5505b81b6b3SRodney W. Grimes 		partp[i].dp_flag = 0;
5514ddd60b9SBrian Somers 	if (active > 0 && active <= NDOSPART)
5524ddd60b9SBrian Somers 		partp[active-1].dp_flag = ACTIVE;
5535b81b6b3SRodney W. Grimes }
5545b81b6b3SRodney W. Grimes 
55512d910e9SRobert Nordier static void
55612d910e9SRobert Nordier change_code()
55712d910e9SRobert Nordier {
55812d910e9SRobert Nordier 	if (ok("Do you want to change the boot code?"))
55912d910e9SRobert Nordier 		init_boot();
56012d910e9SRobert Nordier 
56112d910e9SRobert Nordier }
56212d910e9SRobert Nordier 
5634be1e61bSAlexander Langer void
5645b81b6b3SRodney W. Grimes get_params_to_use()
5655b81b6b3SRodney W. Grimes {
5665b81b6b3SRodney W. Grimes 	int	tmp;
5675b81b6b3SRodney W. Grimes 	print_params();
5685b81b6b3SRodney W. Grimes 	if (ok("Do you want to change our idea of what BIOS thinks ?"))
5695b81b6b3SRodney W. Grimes 	{
5705b81b6b3SRodney W. Grimes 		do
5715b81b6b3SRodney W. Grimes 		{
5725b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
5735b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #heads", dos_heads, tmp);
5745b81b6b3SRodney W. Grimes 			Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
5755b81b6b3SRodney W. Grimes 			dos_cylsecs = dos_heads * dos_sectors;
5765b81b6b3SRodney W. Grimes 			print_params();
5775b81b6b3SRodney W. Grimes 		}
5785b81b6b3SRodney W. Grimes 		while(!ok("Are you happy with this choice"));
5795b81b6b3SRodney W. Grimes 	}
5805b81b6b3SRodney W. Grimes }
5815b81b6b3SRodney W. Grimes 
582f46af505SJordan K. Hubbard 
5835b81b6b3SRodney W. Grimes /***********************************************\
5845b81b6b3SRodney W. Grimes * Change real numbers into strange dos numbers	*
5855b81b6b3SRodney W. Grimes \***********************************************/
5864be1e61bSAlexander Langer static void
587e2975440SBruce Evans dos(sec, size, c, s, h)
588e2975440SBruce Evans int sec, size;
5895b81b6b3SRodney W. Grimes unsigned char *c, *s, *h;
5905b81b6b3SRodney W. Grimes {
5915b81b6b3SRodney W. Grimes int cy;
5925b81b6b3SRodney W. Grimes int hd;
5935b81b6b3SRodney W. Grimes 
594e2975440SBruce Evans 	if (sec == 0 && size == 0) {
5950b461cd7SBruce Evans 		*s = *c = *h = 0;
5960b461cd7SBruce Evans 		return;
5970b461cd7SBruce Evans 	}
5980b461cd7SBruce Evans 
5995b81b6b3SRodney W. Grimes 	cy = sec / ( dos_cylsecs );
6005b81b6b3SRodney W. Grimes 	sec = sec - cy * ( dos_cylsecs );
6015b81b6b3SRodney W. Grimes 
6025b81b6b3SRodney W. Grimes 	hd = sec / dos_sectors;
6035b81b6b3SRodney W. Grimes 	sec = (sec - hd * dos_sectors) + 1;
6045b81b6b3SRodney W. Grimes 
6055b81b6b3SRodney W. Grimes 	*h = hd;
6065b81b6b3SRodney W. Grimes 	*c = cy & 0xff;
6075b81b6b3SRodney W. Grimes 	*s = (sec & 0x3f) | ( (cy & 0x300) >> 2);
6085b81b6b3SRodney W. Grimes }
6095b81b6b3SRodney W. Grimes 
6105b81b6b3SRodney W. Grimes int fd;
6115b81b6b3SRodney W. Grimes 
6125b81b6b3SRodney W. Grimes 	/* Getting device status */
6135b81b6b3SRodney W. Grimes 
6144be1e61bSAlexander Langer static int
6154be1e61bSAlexander Langer open_disk(int u_flag)
6165b81b6b3SRodney W. Grimes {
6175b81b6b3SRodney W. Grimes struct stat 	st;
6185b81b6b3SRodney W. Grimes 
6195b81b6b3SRodney W. Grimes 	if (stat(disk, &st) == -1) {
620d98b1668SPhilippe Charnier 		warnx("can't get file status of %s", disk);
6215b81b6b3SRodney W. Grimes 		return -1;
622b60eb395SBruce Evans 	}
623b60eb395SBruce Evans 	if ( !(st.st_mode & S_IFCHR) )
624d98b1668SPhilippe Charnier 		warnx("device %s is not character special", disk);
62512d910e9SRobert Nordier 	if ((fd = open(disk,
62626721a89SRobert Nordier 	    a_flag || B_flag || u_flag ? O_RDWR : O_RDONLY)) == -1) {
627e3038c6eSJoerg Wunsch 		if(errno == ENXIO)
628e3038c6eSJoerg Wunsch 			return -2;
629d98b1668SPhilippe Charnier 		warnx("can't open device %s", disk);
6305b81b6b3SRodney W. Grimes 		return -1;
6315b81b6b3SRodney W. Grimes 	}
6325b81b6b3SRodney W. Grimes 	if (get_params(0) == -1) {
633d98b1668SPhilippe Charnier 		warnx("can't get disk parameters on %s", disk);
6345b81b6b3SRodney W. Grimes 		return -1;
6355b81b6b3SRodney W. Grimes 	}
6365b81b6b3SRodney W. Grimes 	return fd;
6375b81b6b3SRodney W. Grimes }
6385b81b6b3SRodney W. Grimes 
6394be1e61bSAlexander Langer static ssize_t
6404be1e61bSAlexander Langer read_disk(off_t sector, void *buf)
6415b81b6b3SRodney W. Grimes {
6425b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
6437cb29d33SSøren Schmidt 	if( secsize == 0 )
6447cb29d33SSøren Schmidt 		for( secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE; secsize *= 2 )
6457cb29d33SSøren Schmidt 			{
6467cb29d33SSøren Schmidt 			/* try the read */
6477cb29d33SSøren Schmidt 			int size = read(fd, buf, secsize);
6487cb29d33SSøren Schmidt 			if( size == secsize )
6497cb29d33SSøren Schmidt 				/* it worked so return */
6507cb29d33SSøren Schmidt 				return secsize;
6517cb29d33SSøren Schmidt 			}
6527cb29d33SSøren Schmidt 	else
6537cb29d33SSøren Schmidt 		return read( fd, buf, secsize );
6547cb29d33SSøren Schmidt 
6557cb29d33SSøren Schmidt 	/* we failed to read at any of the sizes */
6567cb29d33SSøren Schmidt 	return -1;
6575b81b6b3SRodney W. Grimes }
6585b81b6b3SRodney W. Grimes 
6594be1e61bSAlexander Langer static ssize_t
6604be1e61bSAlexander Langer write_disk(off_t sector, void *buf)
6615b81b6b3SRodney W. Grimes {
6625b81b6b3SRodney W. Grimes 	lseek(fd,(sector * 512), 0);
6637cb29d33SSøren Schmidt 	/* write out in the size that the read_disk found worked */
6647cb29d33SSøren Schmidt 	return write(fd, buf, secsize);
6655b81b6b3SRodney W. Grimes }
6665b81b6b3SRodney W. Grimes 
6674be1e61bSAlexander Langer static int
6684be1e61bSAlexander Langer get_params()
6695b81b6b3SRodney W. Grimes {
6705b81b6b3SRodney W. Grimes 
6715b81b6b3SRodney W. Grimes     if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
672d98b1668SPhilippe Charnier 	warnx("can't get disk parameters on %s; supplying dummy ones", disk);
673b60eb395SBruce Evans 	dos_cyls = cyls = 1;
674b60eb395SBruce Evans 	dos_heads = heads = 1;
675b60eb395SBruce Evans 	dos_sectors = sectors = 1;
676b60eb395SBruce Evans 	dos_cylsecs = cylsecs = heads * sectors;
677b60eb395SBruce Evans 	disksecs = cyls * heads * sectors;
678b60eb395SBruce Evans 	return disksecs;
6795b81b6b3SRodney W. Grimes     }
6805b81b6b3SRodney W. Grimes 
6815b81b6b3SRodney W. Grimes     dos_cyls = cyls = disklabel.d_ncylinders;
6825b81b6b3SRodney W. Grimes     dos_heads = heads = disklabel.d_ntracks;
6835b81b6b3SRodney W. Grimes     dos_sectors = sectors = disklabel.d_nsectors;
6845b81b6b3SRodney W. Grimes     dos_cylsecs = cylsecs = heads * sectors;
6855b81b6b3SRodney W. Grimes     disksecs = cyls * heads * sectors;
6865b81b6b3SRodney W. Grimes 
6875b81b6b3SRodney W. Grimes     return (disksecs);
6885b81b6b3SRodney W. Grimes }
6895b81b6b3SRodney W. Grimes 
6905b81b6b3SRodney W. Grimes 
6914be1e61bSAlexander Langer static int
6925b81b6b3SRodney W. Grimes read_s0()
6935b81b6b3SRodney W. Grimes {
6945b81b6b3SRodney W. Grimes 	if (read_disk(0, (char *) mboot.bootinst) == -1) {
695d98b1668SPhilippe Charnier 		warnx("can't read fdisk partition table");
6965b81b6b3SRodney W. Grimes 		return -1;
6975b81b6b3SRodney W. Grimes 	}
6985b81b6b3SRodney W. Grimes 	if (mboot.signature != BOOT_MAGIC) {
699d98b1668SPhilippe Charnier 		warnx("invalid fdisk partition table found");
7005b81b6b3SRodney W. Grimes 		/* So should we initialize things */
7015b81b6b3SRodney W. Grimes 		return -1;
7025b81b6b3SRodney W. Grimes 	}
7035b81b6b3SRodney W. Grimes 	return 0;
7045b81b6b3SRodney W. Grimes }
7055b81b6b3SRodney W. Grimes 
7064be1e61bSAlexander Langer static int
7075b81b6b3SRodney W. Grimes write_s0()
7085b81b6b3SRodney W. Grimes {
7095b81b6b3SRodney W. Grimes 	int	flag;
7105b81b6b3SRodney W. Grimes 	if (iotest) {
7115b81b6b3SRodney W. Grimes 		print_s0(-1);
7125b81b6b3SRodney W. Grimes 		return 0;
7135b81b6b3SRodney W. Grimes 	}
7145b81b6b3SRodney W. Grimes 	/*
7155b81b6b3SRodney W. Grimes 	 * write enable label sector before write (if necessary),
7165b81b6b3SRodney W. Grimes 	 * disable after writing.
7175b81b6b3SRodney W. Grimes 	 * needed if the disklabel protected area also protects
7185b81b6b3SRodney W. Grimes 	 * sector 0. (e.g. empty disk)
7195b81b6b3SRodney W. Grimes 	 */
7205b81b6b3SRodney W. Grimes 	flag = 1;
721ba3551dfSJulian Elischer #ifdef NOT_NOW
7225b81b6b3SRodney W. Grimes 	if (ioctl(fd, DIOCWLABEL, &flag) < 0)
723d98b1668SPhilippe Charnier 		warn("ioctl DIOCWLABEL");
724ba3551dfSJulian Elischer #endif
7255b81b6b3SRodney W. Grimes 	if (write_disk(0, (char *) mboot.bootinst) == -1) {
726d98b1668SPhilippe Charnier 		warnx("can't write fdisk partition table");
7275b81b6b3SRodney W. Grimes 		return -1;
7285b81b6b3SRodney W. Grimes 	flag = 0;
729ba3551dfSJulian Elischer #ifdef NOT_NOW
7305b81b6b3SRodney W. Grimes 	(void) ioctl(fd, DIOCWLABEL, &flag);
731ba3551dfSJulian Elischer #endif
7325b81b6b3SRodney W. Grimes 	}
7334be1e61bSAlexander Langer 	return(0);
7345b81b6b3SRodney W. Grimes }
7355b81b6b3SRodney W. Grimes 
7365b81b6b3SRodney W. Grimes 
7374be1e61bSAlexander Langer static int
7385b81b6b3SRodney W. Grimes ok(str)
7395b81b6b3SRodney W. Grimes char *str;
7405b81b6b3SRodney W. Grimes {
7415b81b6b3SRodney W. Grimes 	printf("%s [n] ", str);
7425b81b6b3SRodney W. Grimes 	fgets(lbuf, LBUF, stdin);
7435b81b6b3SRodney W. Grimes 	lbuf[strlen(lbuf)-1] = 0;
7445b81b6b3SRodney W. Grimes 
7455b81b6b3SRodney W. Grimes 	if (*lbuf &&
7465b81b6b3SRodney W. Grimes 		(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
7475b81b6b3SRodney W. Grimes 		 !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
7485b81b6b3SRodney W. Grimes 		return 1;
7495b81b6b3SRodney W. Grimes 	else
7505b81b6b3SRodney W. Grimes 		return 0;
7515b81b6b3SRodney W. Grimes }
7525b81b6b3SRodney W. Grimes 
7534be1e61bSAlexander Langer static int
7544be1e61bSAlexander Langer decimal(char *str, int *num, int deflt)
7555b81b6b3SRodney W. Grimes {
7565b81b6b3SRodney W. Grimes int acc = 0, c;
7575b81b6b3SRodney W. Grimes char *cp;
7585b81b6b3SRodney W. Grimes 
7595b81b6b3SRodney W. Grimes 	while (1) {
7605b81b6b3SRodney W. Grimes 		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
7615b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
7625b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
7635b81b6b3SRodney W. Grimes 
7645b81b6b3SRodney W. Grimes 		if (!*lbuf)
7655b81b6b3SRodney W. Grimes 			return 0;
7665b81b6b3SRodney W. Grimes 
7675b81b6b3SRodney W. Grimes 		cp = lbuf;
7685b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
7695b81b6b3SRodney W. Grimes 		if (!c)
7705b81b6b3SRodney W. Grimes 			return 0;
7714be1e61bSAlexander Langer 		while ((c = *cp++)) {
7725b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
7735b81b6b3SRodney W. Grimes 				acc = acc * 10 + c - '0';
7745b81b6b3SRodney W. Grimes 			else
7755b81b6b3SRodney W. Grimes 				break;
7765b81b6b3SRodney W. Grimes 		}
7775b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
7785b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
7795b81b6b3SRodney W. Grimes 		if (!c) {
7805b81b6b3SRodney W. Grimes 			*num = acc;
7815b81b6b3SRodney W. Grimes 			return 1;
7825b81b6b3SRodney W. Grimes 		} else
783680426beSDavid E. O'Brien 			printf("%s is an invalid decimal number.  Try again.\n",
7845b81b6b3SRodney W. Grimes 				lbuf);
7855b81b6b3SRodney W. Grimes 	}
7865b81b6b3SRodney W. Grimes 
7875b81b6b3SRodney W. Grimes }
7885b81b6b3SRodney W. Grimes 
7894be1e61bSAlexander Langer #if 0
7904be1e61bSAlexander Langer static int
7914be1e61bSAlexander Langer hex(char *str, int *num, int deflt)
7925b81b6b3SRodney W. Grimes {
7935b81b6b3SRodney W. Grimes int acc = 0, c;
7945b81b6b3SRodney W. Grimes char *cp;
7955b81b6b3SRodney W. Grimes 
7965b81b6b3SRodney W. Grimes 	while (1) {
7975b81b6b3SRodney W. Grimes 		printf("Supply a hex value for \"%s\" [%x] ", str, deflt);
7985b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
7995b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8005b81b6b3SRodney W. Grimes 
8015b81b6b3SRodney W. Grimes 		if (!*lbuf)
8025b81b6b3SRodney W. Grimes 			return 0;
8035b81b6b3SRodney W. Grimes 
8045b81b6b3SRodney W. Grimes 		cp = lbuf;
8055b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8065b81b6b3SRodney W. Grimes 		if (!c)
8075b81b6b3SRodney W. Grimes 			return 0;
8084be1e61bSAlexander Langer 		while ((c = *cp++)) {
8095b81b6b3SRodney W. Grimes 			if (c <= '9' && c >= '0')
8105b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - '0';
8115b81b6b3SRodney W. Grimes 			else if (c <= 'f' && c >= 'a')
8125b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'a' + 10;
8135b81b6b3SRodney W. Grimes 			else if (c <= 'F' && c >= 'A')
8145b81b6b3SRodney W. Grimes 				acc = (acc << 4) + c - 'A' + 10;
8155b81b6b3SRodney W. Grimes 			else
8165b81b6b3SRodney W. Grimes 				break;
8175b81b6b3SRodney W. Grimes 		}
8185b81b6b3SRodney W. Grimes 		if (c == ' ' || c == '\t')
8195b81b6b3SRodney W. Grimes 			while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8205b81b6b3SRodney W. Grimes 		if (!c) {
8215b81b6b3SRodney W. Grimes 			*num = acc;
8225b81b6b3SRodney W. Grimes 			return 1;
8235b81b6b3SRodney W. Grimes 		} else
824680426beSDavid E. O'Brien 			printf("%s is an invalid hex number.  Try again.\n",
8255b81b6b3SRodney W. Grimes 				lbuf);
8265b81b6b3SRodney W. Grimes 	}
8275b81b6b3SRodney W. Grimes 
8285b81b6b3SRodney W. Grimes }
8295b81b6b3SRodney W. Grimes 
8304be1e61bSAlexander Langer static int
8314be1e61bSAlexander Langer string(char *str, char **ans)
8325b81b6b3SRodney W. Grimes {
8335b81b6b3SRodney W. Grimes int c;
8345b81b6b3SRodney W. Grimes char *cp = lbuf;
8355b81b6b3SRodney W. Grimes 
8365b81b6b3SRodney W. Grimes 	while (1) {
8375b81b6b3SRodney W. Grimes 		printf("Supply a string value for \"%s\" [%s] ", str, *ans);
8385b81b6b3SRodney W. Grimes 		fgets(lbuf, LBUF, stdin);
8395b81b6b3SRodney W. Grimes 		lbuf[strlen(lbuf)-1] = 0;
8405b81b6b3SRodney W. Grimes 
8415b81b6b3SRodney W. Grimes 		if (!*lbuf)
8425b81b6b3SRodney W. Grimes 			return 0;
8435b81b6b3SRodney W. Grimes 
8445b81b6b3SRodney W. Grimes 		while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
8455b81b6b3SRodney W. Grimes 		if (c == '"') {
8465b81b6b3SRodney W. Grimes 			c = *++cp;
8475b81b6b3SRodney W. Grimes 			*ans = cp;
8485b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != '"') cp++;
8495b81b6b3SRodney W. Grimes 		} else {
8505b81b6b3SRodney W. Grimes 			*ans = cp;
8515b81b6b3SRodney W. Grimes 			while ((c = *cp) && c != ' ' && c != '\t') cp++;
8525b81b6b3SRodney W. Grimes 		}
8535b81b6b3SRodney W. Grimes 
8545b81b6b3SRodney W. Grimes 		if (c)
8555b81b6b3SRodney W. Grimes 			*cp = 0;
8565b81b6b3SRodney W. Grimes 		return 1;
8575b81b6b3SRodney W. Grimes 	}
8585b81b6b3SRodney W. Grimes }
8594be1e61bSAlexander Langer #endif
8605b81b6b3SRodney W. Grimes 
8614be1e61bSAlexander Langer static char *
8624be1e61bSAlexander Langer get_type(int type)
8635b81b6b3SRodney W. Grimes {
8645b81b6b3SRodney W. Grimes 	int	numentries = (sizeof(part_types)/sizeof(struct part_type));
8655b81b6b3SRodney W. Grimes 	int	counter = 0;
8665b81b6b3SRodney W. Grimes 	struct	part_type *ptr = part_types;
8675b81b6b3SRodney W. Grimes 
8685b81b6b3SRodney W. Grimes 
8695b81b6b3SRodney W. Grimes 	while(counter < numentries)
8705b81b6b3SRodney W. Grimes 	{
8715b81b6b3SRodney W. Grimes 		if(ptr->type == type)
8725b81b6b3SRodney W. Grimes 		{
8735b81b6b3SRodney W. Grimes 			return(ptr->name);
8745b81b6b3SRodney W. Grimes 		}
8755b81b6b3SRodney W. Grimes 		ptr++;
8765b81b6b3SRodney W. Grimes 		counter++;
8775b81b6b3SRodney W. Grimes 	}
8785b81b6b3SRodney W. Grimes 	return("unknown");
8795b81b6b3SRodney W. Grimes }
880f46af505SJordan K. Hubbard 
881f46af505SJordan K. Hubbard 
882f46af505SJordan K. Hubbard static void
883f46af505SJordan K. Hubbard parse_config_line(line, command)
884f46af505SJordan K. Hubbard     char	*line;
885f46af505SJordan K. Hubbard     CMD		*command;
886f46af505SJordan K. Hubbard {
887f46af505SJordan K. Hubbard     char	*cp, *end;
888f46af505SJordan K. Hubbard 
889f46af505SJordan K. Hubbard     cp = line;
890f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
891f46af505SJordan K. Hubbard 		   function */
892f46af505SJordan K. Hubbard     {
893f46af505SJordan K. Hubbard 	memset(command, 0, sizeof(*command));
894f46af505SJordan K. Hubbard 
895f46af505SJordan K. Hubbard 	while (isspace(*cp)) ++cp;
896f46af505SJordan K. Hubbard 	if (*cp == '\0' || *cp == '#')
897f46af505SJordan K. Hubbard 	{
898f46af505SJordan K. Hubbard 	    break;
899f46af505SJordan K. Hubbard 	}
900f46af505SJordan K. Hubbard 	command->cmd = *cp++;
901f46af505SJordan K. Hubbard 
902f46af505SJordan K. Hubbard 	/*
903f46af505SJordan K. Hubbard 	 * Parse args
904f46af505SJordan K. Hubbard 	 */
905f46af505SJordan K. Hubbard 	while (1)
906f46af505SJordan K. Hubbard 	{
907f46af505SJordan K. Hubbard 	    while (isspace(*cp)) ++cp;
908f46af505SJordan K. Hubbard 	    if (*cp == '#')
909f46af505SJordan K. Hubbard 	    {
910f46af505SJordan K. Hubbard 		break;		/* found comment */
911f46af505SJordan K. Hubbard 	    }
912f46af505SJordan K. Hubbard 	    if (isalpha(*cp))
913f46af505SJordan K. Hubbard 	    {
914f46af505SJordan K. Hubbard 		command->args[command->n_args].argtype = *cp++;
915f46af505SJordan K. Hubbard 	    }
916f46af505SJordan K. Hubbard 	    if (!isdigit(*cp))
917f46af505SJordan K. Hubbard 	    {
918f46af505SJordan K. Hubbard 		break;		/* assume end of line */
919f46af505SJordan K. Hubbard 	    }
920f46af505SJordan K. Hubbard 	    end = NULL;
921f46af505SJordan K. Hubbard 	    command->args[command->n_args].arg_val = strtol(cp, &end, 0);
922f46af505SJordan K. Hubbard 	    if (cp == end)
923f46af505SJordan K. Hubbard 	    {
924f46af505SJordan K. Hubbard 		break;		/* couldn't parse number */
925f46af505SJordan K. Hubbard 	    }
926f46af505SJordan K. Hubbard 	    cp = end;
927f46af505SJordan K. Hubbard 	    command->n_args++;
928f46af505SJordan K. Hubbard 	}
929f46af505SJordan K. Hubbard 	break;
930f46af505SJordan K. Hubbard     }
931f46af505SJordan K. Hubbard }
932f46af505SJordan K. Hubbard 
933f46af505SJordan K. Hubbard 
934f46af505SJordan K. Hubbard static int
935f46af505SJordan K. Hubbard process_geometry(command)
936f46af505SJordan K. Hubbard     CMD		*command;
937f46af505SJordan K. Hubbard {
938f46af505SJordan K. Hubbard     int		status = 1, i;
939f46af505SJordan K. Hubbard 
940f46af505SJordan K. Hubbard     while (1)
941f46af505SJordan K. Hubbard     {
942f46af505SJordan K. Hubbard 	geom_processed = 1;
943f46af505SJordan K. Hubbard 	if (part_processed)
944f46af505SJordan K. Hubbard 	{
945d98b1668SPhilippe Charnier 	    warnx(
946d98b1668SPhilippe Charnier 	"ERROR line %d: the geometry specification line must occur before\n\
947d98b1668SPhilippe Charnier     all partition specifications",
948d98b1668SPhilippe Charnier 		    current_line_number);
949f46af505SJordan K. Hubbard 	    status = 0;
950f46af505SJordan K. Hubbard 	    break;
951f46af505SJordan K. Hubbard 	}
952f46af505SJordan K. Hubbard 	if (command->n_args != 3)
953f46af505SJordan K. Hubbard 	{
954d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of geometry args",
955d98b1668SPhilippe Charnier 		    current_line_number);
956f46af505SJordan K. Hubbard 	    status = 0;
957f46af505SJordan K. Hubbard 	    break;
958f46af505SJordan K. Hubbard 	}
959f46af505SJordan K. Hubbard 	dos_cyls = -1;
960f46af505SJordan K. Hubbard 	dos_heads = -1;
961f46af505SJordan K. Hubbard 	dos_sectors = -1;
962f46af505SJordan K. Hubbard 	for (i = 0; i < 3; ++i)
963f46af505SJordan K. Hubbard 	{
964f46af505SJordan K. Hubbard 	    switch (command->args[i].argtype)
965f46af505SJordan K. Hubbard 	    {
966f46af505SJordan K. Hubbard 	    case 'c':
967f46af505SJordan K. Hubbard 		dos_cyls = command->args[i].arg_val;
968f46af505SJordan K. Hubbard 		break;
969f46af505SJordan K. Hubbard 	    case 'h':
970f46af505SJordan K. Hubbard 		dos_heads = command->args[i].arg_val;
971f46af505SJordan K. Hubbard 		break;
972f46af505SJordan K. Hubbard 	    case 's':
973f46af505SJordan K. Hubbard 		dos_sectors = command->args[i].arg_val;
974f46af505SJordan K. Hubbard 		break;
975f46af505SJordan K. Hubbard 	    default:
976d98b1668SPhilippe Charnier 		warnx(
977d98b1668SPhilippe Charnier 		"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
978d98b1668SPhilippe Charnier 			current_line_number, command->args[i].argtype,
979f46af505SJordan K. Hubbard 			command->args[i].argtype);
980f46af505SJordan K. Hubbard 		status = 0;
981f46af505SJordan K. Hubbard 		break;
982f46af505SJordan K. Hubbard 	    }
983f46af505SJordan K. Hubbard 	}
984f46af505SJordan K. Hubbard 	if (status == 0)
985f46af505SJordan K. Hubbard 	{
986f46af505SJordan K. Hubbard 	    break;
987f46af505SJordan K. Hubbard 	}
988f46af505SJordan K. Hubbard 
989f46af505SJordan K. Hubbard 	dos_cylsecs = dos_heads * dos_sectors;
990f46af505SJordan K. Hubbard 
991f46af505SJordan K. Hubbard 	/*
992f46af505SJordan K. Hubbard 	 * Do sanity checks on parameter values
993f46af505SJordan K. Hubbard 	 */
994f46af505SJordan K. Hubbard 	if (dos_cyls < 0)
995f46af505SJordan K. Hubbard 	{
996d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of cylinders not specified",
997d98b1668SPhilippe Charnier 		    current_line_number);
998f46af505SJordan K. Hubbard 	    status = 0;
999f46af505SJordan K. Hubbard 	}
1000f46af505SJordan K. Hubbard 	if (dos_cyls == 0 || dos_cyls > 1024)
1001f46af505SJordan K. Hubbard 	{
1002d98b1668SPhilippe Charnier 	    warnx(
1003d98b1668SPhilippe Charnier 	"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1004f46af505SJordan K. Hubbard     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1005d98b1668SPhilippe Charnier     is dedicated to FreeBSD)",
1006d98b1668SPhilippe Charnier 		    current_line_number, dos_cyls);
1007f46af505SJordan K. Hubbard 	}
1008f46af505SJordan K. Hubbard 
1009f46af505SJordan K. Hubbard 	if (dos_heads < 0)
1010f46af505SJordan K. Hubbard 	{
1011d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads not specified",
1012d98b1668SPhilippe Charnier 		    current_line_number);
1013f46af505SJordan K. Hubbard 	    status = 0;
1014f46af505SJordan K. Hubbard 	}
1015f46af505SJordan K. Hubbard 	else if (dos_heads < 1 || dos_heads > 256)
1016f46af505SJordan K. Hubbard 	{
1017d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of heads must be within (1-256)",
1018d98b1668SPhilippe Charnier 		    current_line_number);
1019f46af505SJordan K. Hubbard 	    status = 0;
1020f46af505SJordan K. Hubbard 	}
1021f46af505SJordan K. Hubbard 
1022f46af505SJordan K. Hubbard 	if (dos_sectors < 0)
1023f46af505SJordan K. Hubbard 	{
1024d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors not specified",
1025d98b1668SPhilippe Charnier 		    current_line_number);
1026f46af505SJordan K. Hubbard 	    status = 0;
1027f46af505SJordan K. Hubbard 	}
1028f46af505SJordan K. Hubbard 	else if (dos_sectors < 1 || dos_sectors > 63)
1029f46af505SJordan K. Hubbard 	{
1030d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: number of sectors must be within (1-63)",
1031d98b1668SPhilippe Charnier 		    current_line_number);
1032f46af505SJordan K. Hubbard 	    status = 0;
1033f46af505SJordan K. Hubbard 	}
1034f46af505SJordan K. Hubbard 
1035f46af505SJordan K. Hubbard 	break;
1036f46af505SJordan K. Hubbard     }
1037f46af505SJordan K. Hubbard     return (status);
1038f46af505SJordan K. Hubbard }
1039f46af505SJordan K. Hubbard 
1040f46af505SJordan K. Hubbard 
1041f46af505SJordan K. Hubbard static int
1042f46af505SJordan K. Hubbard process_partition(command)
1043f46af505SJordan K. Hubbard     CMD		*command;
1044f46af505SJordan K. Hubbard {
1045f46af505SJordan K. Hubbard     int				status = 0, partition;
1046f46af505SJordan K. Hubbard     unsigned long		chunks, adj_size, max_end;
1047f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1048f46af505SJordan K. Hubbard 
1049f46af505SJordan K. Hubbard     while (1)
1050f46af505SJordan K. Hubbard     {
1051f46af505SJordan K. Hubbard 	part_processed = 1;
1052f46af505SJordan K. Hubbard 	if (command->n_args != 4)
1053f46af505SJordan K. Hubbard 	{
1054d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of partition args",
1055d98b1668SPhilippe Charnier 		    current_line_number);
1056f46af505SJordan K. Hubbard 	    break;
1057f46af505SJordan K. Hubbard 	}
1058f46af505SJordan K. Hubbard 	partition = command->args[0].arg_val;
10594ddd60b9SBrian Somers 	if (partition < 1 || partition > 4)
1060f46af505SJordan K. Hubbard 	{
1061d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: invalid partition number %d",
1062d98b1668SPhilippe Charnier 		    current_line_number, partition);
1063f46af505SJordan K. Hubbard 	    break;
1064f46af505SJordan K. Hubbard 	}
10654ddd60b9SBrian Somers 	partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1066f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1067f46af505SJordan K. Hubbard 	partp->dp_typ = command->args[1].arg_val;
1068f46af505SJordan K. Hubbard 	partp->dp_start = command->args[2].arg_val;
1069f46af505SJordan K. Hubbard 	partp->dp_size = command->args[3].arg_val;
1070f46af505SJordan K. Hubbard 	max_end = partp->dp_start + partp->dp_size;
1071f46af505SJordan K. Hubbard 
1072f46af505SJordan K. Hubbard 	if (partp->dp_typ == 0)
1073f46af505SJordan K. Hubbard 	{
1074f46af505SJordan K. Hubbard 	    /*
1075f46af505SJordan K. Hubbard 	     * Get out, the partition is marked as unused.
1076f46af505SJordan K. Hubbard 	     */
1077f46af505SJordan K. Hubbard 	    /*
1078f46af505SJordan K. Hubbard 	     * Insure that it's unused.
1079f46af505SJordan K. Hubbard 	     */
1080f46af505SJordan K. Hubbard 	    bzero((char *)partp, sizeof (struct dos_partition));
1081f46af505SJordan K. Hubbard 	    status = 1;
1082f46af505SJordan K. Hubbard 	    break;
1083f46af505SJordan K. Hubbard 	}
1084f46af505SJordan K. Hubbard 
1085f46af505SJordan K. Hubbard 	/*
1086f46af505SJordan K. Hubbard 	 * Adjust start upwards, if necessary, to fall on an head boundary.
1087f46af505SJordan K. Hubbard 	 */
1088f46af505SJordan K. Hubbard 	if (partp->dp_start % dos_sectors != 0)
1089f46af505SJordan K. Hubbard 	{
1090f46af505SJordan K. Hubbard 	    adj_size =
1091f46af505SJordan K. Hubbard 		(partp->dp_start / dos_sectors + 1) * dos_sectors;
1092f46af505SJordan K. Hubbard 	    if (adj_size > max_end)
1093f46af505SJordan K. Hubbard 	    {
1094f46af505SJordan K. Hubbard 		/*
1095f46af505SJordan K. Hubbard 		 * Can't go past end of partition
1096f46af505SJordan K. Hubbard 		 */
1097d98b1668SPhilippe Charnier 		warnx(
1098d98b1668SPhilippe Charnier 	"ERROR line %d: unable to adjust start of partition %d to fall on\n\
1099d98b1668SPhilippe Charnier     a cylinder boundary",
1100d98b1668SPhilippe Charnier 			current_line_number, partition);
1101f46af505SJordan K. Hubbard 		break;
1102f46af505SJordan K. Hubbard 	    }
1103d98b1668SPhilippe Charnier 	    warnx(
1104d98b1668SPhilippe Charnier 	"WARNING: adjusting start offset of partition '%d' from %lu\n\
1105d98b1668SPhilippe Charnier     to %lu, to round to an head boundary",
1106d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_start, adj_size);
1107f46af505SJordan K. Hubbard 	    partp->dp_start = adj_size;
1108f46af505SJordan K. Hubbard 	}
1109f46af505SJordan K. Hubbard 
1110f46af505SJordan K. Hubbard 	/*
1111f46af505SJordan K. Hubbard 	 * Adjust size downwards, if necessary, to fall on a cylinder
1112f46af505SJordan K. Hubbard 	 * boundary.
1113f46af505SJordan K. Hubbard 	 */
1114f46af505SJordan K. Hubbard 	chunks =
1115f46af505SJordan K. Hubbard 	    ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1116f46af505SJordan K. Hubbard 	adj_size = chunks - partp->dp_start;
1117f46af505SJordan K. Hubbard 	if (adj_size != partp->dp_size)
1118f46af505SJordan K. Hubbard 	{
1119d98b1668SPhilippe Charnier 	    warnx(
1120d98b1668SPhilippe Charnier 	"WARNING: adjusting size of partition '%d' from %lu to %lu,\n\
1121d98b1668SPhilippe Charnier     to round to a cylinder boundary",
1122d98b1668SPhilippe Charnier 		    partition, (u_long)partp->dp_size, adj_size);
1123f46af505SJordan K. Hubbard 	    if (chunks > 0)
1124f46af505SJordan K. Hubbard 	    {
1125f46af505SJordan K. Hubbard 		partp->dp_size = adj_size;
1126f46af505SJordan K. Hubbard 	    }
1127f46af505SJordan K. Hubbard 	    else
1128f46af505SJordan K. Hubbard 	    {
1129f46af505SJordan K. Hubbard 		partp->dp_size = 0;
1130f46af505SJordan K. Hubbard 	    }
1131f46af505SJordan K. Hubbard 	}
1132f46af505SJordan K. Hubbard 	if (partp->dp_size < 1)
1133f46af505SJordan K. Hubbard 	{
1134d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: size for partition '%d' is zero",
1135d98b1668SPhilippe Charnier 		    current_line_number, partition);
1136f46af505SJordan K. Hubbard 	    break;
1137f46af505SJordan K. Hubbard 	}
1138f46af505SJordan K. Hubbard 
1139f46af505SJordan K. Hubbard 	dos(partp->dp_start, partp->dp_size,
1140f46af505SJordan K. Hubbard 	    &partp->dp_scyl, &partp->dp_ssect, &partp->dp_shd);
1141f46af505SJordan K. Hubbard 	dos(partp->dp_start+partp->dp_size - 1, partp->dp_size,
1142f46af505SJordan K. Hubbard 	    &partp->dp_ecyl, &partp->dp_esect, &partp->dp_ehd);
1143f46af505SJordan K. Hubbard 	status = 1;
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_active(command)
1152f46af505SJordan K. Hubbard     CMD		*command;
1153f46af505SJordan K. Hubbard {
1154f46af505SJordan K. Hubbard     int				status = 0, partition, i;
1155f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1156f46af505SJordan K. Hubbard 
1157f46af505SJordan K. Hubbard     while (1)
1158f46af505SJordan K. Hubbard     {
1159f46af505SJordan K. Hubbard 	active_processed = 1;
1160f46af505SJordan K. Hubbard 	if (command->n_args != 1)
1161f46af505SJordan K. Hubbard 	{
1162d98b1668SPhilippe Charnier 	    warnx("ERROR line %d: incorrect number of active args",
1163d98b1668SPhilippe Charnier 		    current_line_number);
1164f46af505SJordan K. Hubbard 	    status = 0;
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 	}
1174f46af505SJordan K. Hubbard 	/*
1175f46af505SJordan K. Hubbard 	 * Reset active partition
1176f46af505SJordan K. Hubbard 	 */
1177f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts);
1178f46af505SJordan K. Hubbard 	for (i = 0; i < NDOSPART; i++)
1179f46af505SJordan K. Hubbard 	    partp[i].dp_flag = 0;
11804ddd60b9SBrian Somers 	partp[partition-1].dp_flag = ACTIVE;
1181f46af505SJordan K. Hubbard 
1182f46af505SJordan K. Hubbard 	status = 1;
1183f46af505SJordan K. Hubbard 	break;
1184f46af505SJordan K. Hubbard     }
1185f46af505SJordan K. Hubbard     return (status);
1186f46af505SJordan K. Hubbard }
1187f46af505SJordan K. Hubbard 
1188f46af505SJordan K. Hubbard 
1189f46af505SJordan K. Hubbard static int
1190f46af505SJordan K. Hubbard process_line(line)
1191f46af505SJordan K. Hubbard     char	*line;
1192f46af505SJordan K. Hubbard {
1193f46af505SJordan K. Hubbard     CMD		command;
1194f46af505SJordan K. Hubbard     int		status = 1;
1195f46af505SJordan K. Hubbard 
1196f46af505SJordan K. Hubbard     while (1)
1197f46af505SJordan K. Hubbard     {
1198f46af505SJordan K. Hubbard 	parse_config_line(line, &command);
1199f46af505SJordan K. Hubbard 	switch (command.cmd)
1200f46af505SJordan K. Hubbard 	{
1201f46af505SJordan K. Hubbard 	case 0:
1202f46af505SJordan K. Hubbard 	    /*
1203f46af505SJordan K. Hubbard 	     * Comment or blank line
1204f46af505SJordan K. Hubbard 	     */
1205f46af505SJordan K. Hubbard 	    break;
1206f46af505SJordan K. Hubbard 	case 'g':
1207f46af505SJordan K. Hubbard 	    /*
1208f46af505SJordan K. Hubbard 	     * Set geometry
1209f46af505SJordan K. Hubbard 	     */
1210f46af505SJordan K. Hubbard 	    status = process_geometry(&command);
1211f46af505SJordan K. Hubbard 	    break;
1212f46af505SJordan K. Hubbard 	case 'p':
1213f46af505SJordan K. Hubbard 	    status = process_partition(&command);
1214f46af505SJordan K. Hubbard 	    break;
1215f46af505SJordan K. Hubbard 	case 'a':
1216f46af505SJordan K. Hubbard 	    status = process_active(&command);
1217f46af505SJordan K. Hubbard 	    break;
1218f46af505SJordan K. Hubbard 	default:
1219f46af505SJordan K. Hubbard 	    status = 0;
1220f46af505SJordan K. Hubbard 	    break;
1221f46af505SJordan K. Hubbard 	}
1222f46af505SJordan K. Hubbard 	break;
1223f46af505SJordan K. Hubbard     }
1224f46af505SJordan K. Hubbard     return (status);
1225f46af505SJordan K. Hubbard }
1226f46af505SJordan K. Hubbard 
1227f46af505SJordan K. Hubbard 
1228f46af505SJordan K. Hubbard static int
1229f46af505SJordan K. Hubbard read_config(config_file)
1230f46af505SJordan K. Hubbard     char *config_file;
1231f46af505SJordan K. Hubbard {
1232f46af505SJordan K. Hubbard     FILE	*fp = NULL;
1233f46af505SJordan K. Hubbard     int		status = 1;
1234f46af505SJordan K. Hubbard     char	buf[1010];
1235f46af505SJordan K. Hubbard 
1236f46af505SJordan K. Hubbard     while (1)	/* dirty trick used to insure one exit point for this
1237f46af505SJordan K. Hubbard 		   function */
1238f46af505SJordan K. Hubbard     {
1239f46af505SJordan K. Hubbard 	if (strcmp(config_file, "-") != 0)
1240f46af505SJordan K. Hubbard 	{
1241f46af505SJordan K. Hubbard 	    /*
1242f46af505SJordan K. Hubbard 	     * We're not reading from stdin
1243f46af505SJordan K. Hubbard 	     */
1244f46af505SJordan K. Hubbard 	    if ((fp = fopen(config_file, "r")) == NULL)
1245f46af505SJordan K. Hubbard 	    {
1246f46af505SJordan K. Hubbard 		status = 0;
1247f46af505SJordan K. Hubbard 		break;
1248f46af505SJordan K. Hubbard 	    }
1249f46af505SJordan K. Hubbard 	}
1250f46af505SJordan K. Hubbard 	else
1251f46af505SJordan K. Hubbard 	{
1252f46af505SJordan K. Hubbard 	    fp = stdin;
1253f46af505SJordan K. Hubbard 	}
1254f46af505SJordan K. Hubbard 	current_line_number = 0;
1255f46af505SJordan K. Hubbard 	while (!feof(fp))
1256f46af505SJordan K. Hubbard 	{
1257f46af505SJordan K. Hubbard 	    if (fgets(buf, sizeof(buf), fp) == NULL)
1258f46af505SJordan K. Hubbard 	    {
1259f46af505SJordan K. Hubbard 		break;
1260f46af505SJordan K. Hubbard 	    }
1261f46af505SJordan K. Hubbard 	    ++current_line_number;
1262f46af505SJordan K. Hubbard 	    status = process_line(buf);
1263f46af505SJordan K. Hubbard 	    if (status == 0)
1264f46af505SJordan K. Hubbard 	    {
1265f46af505SJordan K. Hubbard 		break;
1266f46af505SJordan K. Hubbard 	    }
1267f46af505SJordan K. Hubbard 	}
1268f46af505SJordan K. Hubbard 	break;
1269f46af505SJordan K. Hubbard     }
1270f46af505SJordan K. Hubbard     if (fp)
1271f46af505SJordan K. Hubbard     {
1272f46af505SJordan K. Hubbard 	/*
1273f46af505SJordan K. Hubbard 	 * It doesn't matter if we're reading from stdin, as we've reached EOF
1274f46af505SJordan K. Hubbard 	 */
1275f46af505SJordan K. Hubbard 	fclose(fp);
1276f46af505SJordan K. Hubbard     }
1277f46af505SJordan K. Hubbard     return (status);
1278f46af505SJordan K. Hubbard }
1279f46af505SJordan K. Hubbard 
1280f46af505SJordan K. Hubbard 
1281f46af505SJordan K. Hubbard static void
1282f46af505SJordan K. Hubbard reset_boot(void)
1283f46af505SJordan K. Hubbard {
1284f46af505SJordan K. Hubbard     int				i;
1285f46af505SJordan K. Hubbard     struct dos_partition	*partp;
1286f46af505SJordan K. Hubbard 
1287f46af505SJordan K. Hubbard     init_boot();
1288f46af505SJordan K. Hubbard     for (i = 0; i < 4; ++i)
1289f46af505SJordan K. Hubbard     {
1290f46af505SJordan K. Hubbard 	partp = ((struct dos_partition *) &mboot.parts) + i;
1291f46af505SJordan K. Hubbard 	bzero((char *)partp, sizeof (struct dos_partition));
1292f46af505SJordan K. Hubbard     }
1293f46af505SJordan K. Hubbard }
1294