xref: /minix3/minix/commands/fdisk/fdisk.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /* fdisk - partition a hard disk	Author: Jakob Schripsema */
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc /* Run this with:
4*433d6423SLionel Sambuc  *
5*433d6423SLionel Sambuc  *	fdisk [-hheads] [-ssectors] [device]
6*433d6423SLionel Sambuc  *
7*433d6423SLionel Sambuc  * e.g.,
8*433d6423SLionel Sambuc  *
9*433d6423SLionel Sambuc  *	fdisk				(to get the default)
10*433d6423SLionel Sambuc  *	fdisk -h4 -s17 /dev/hd0		(MINIX default)
11*433d6423SLionel Sambuc  *	fdisk -h4 -s17 c:		(DOS default)
12*433d6423SLionel Sambuc  *	fdisk -h6 -s25 /dev/hd5		(second drive, probably RLL)
13*433d6423SLionel Sambuc  *	fdisk junkfile			(to experiment safely)
14*433d6423SLionel Sambuc  *
15*433d6423SLionel Sambuc  * The device is opened in read-only mode if the file permissions do not
16*433d6423SLionel Sambuc  * permit read-write mode, so it is convenient to use a login account with
17*433d6423SLionel Sambuc  * only read permission to look at the partition table safely.
18*433d6423SLionel Sambuc  *
19*433d6423SLionel Sambuc  * Compile with:
20*433d6423SLionel Sambuc  *
21*433d6423SLionel Sambuc  *	cc -i -o fdisk fdisk.c		(MINIX)
22*433d6423SLionel Sambuc  *	cl -DDOS fdisk.c		(DOS with MS C compiler)
23*433d6423SLionel Sambuc  *
24*433d6423SLionel Sambuc  * This was modified extensively by Bruce Evans 28 Dec 89.
25*433d6423SLionel Sambuc  * The new version has not been tried with DOS.  The open modes are suspect
26*433d6423SLionel Sambuc  * (everyone should convert to use fcntl.h).
27*433d6423SLionel Sambuc  *
28*433d6423SLionel Sambuc  * Changed 18 Dec 92 by Kees J. Bot: Bootstrap code and geometry from device.
29*433d6423SLionel Sambuc  *
30*433d6423SLionel Sambuc  * modified 01 March 95 by asw: updated list of known partition types. Also
31*433d6423SLionel Sambuc  * changed display format slightly to allow for partition type names of
32*433d6423SLionel Sambuc  * up to 9 chars (previous format allowed for 7, but there were already
33*433d6423SLionel Sambuc  * some 8 char names in the list).
34*433d6423SLionel Sambuc */
35*433d6423SLionel Sambuc 
36*433d6423SLionel Sambuc #include <sys/types.h>
37*433d6423SLionel Sambuc #include <machine/partition.h>
38*433d6423SLionel Sambuc #include <minix/partition.h>
39*433d6423SLionel Sambuc #include <sys/ioctl.h>
40*433d6423SLionel Sambuc #include <sys/stat.h>
41*433d6423SLionel Sambuc #include <fcntl.h>
42*433d6423SLionel Sambuc #include <string.h>
43*433d6423SLionel Sambuc #include <stdlib.h>
44*433d6423SLionel Sambuc #include <unistd.h>
45*433d6423SLionel Sambuc #include <stdio.h>
46*433d6423SLionel Sambuc #include <errno.h>
47*433d6423SLionel Sambuc 
48*433d6423SLionel Sambuc #ifdef DOS
49*433d6423SLionel Sambuc #include <dos.h>
50*433d6423SLionel Sambuc #define DEFAULT_DEV	"c:"
51*433d6423SLionel Sambuc #define LOAD_OPEN_MODE	0x8000
52*433d6423SLionel Sambuc #define SAVE_CREAT_MODE	0644
53*433d6423SLionel Sambuc #else
54*433d6423SLionel Sambuc #define DEFAULT_DEV	"/dev/hd0"
55*433d6423SLionel Sambuc #define LOAD_OPEN_MODE	0
56*433d6423SLionel Sambuc #define SAVE_CREAT_MODE	0644
57*433d6423SLionel Sambuc #define UNIX			/* for MINIX */
58*433d6423SLionel Sambuc #endif
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc /* Constants */
61*433d6423SLionel Sambuc 
62*433d6423SLionel Sambuc #define	DEFAULT_NHEAD	4	/* # heads		 */
63*433d6423SLionel Sambuc #define	DEFAULT_NSEC	17	/* sectors / track	 */
64*433d6423SLionel Sambuc #define SECSIZE		512	/* sector size		 */
65*433d6423SLionel Sambuc #define	OK		0
66*433d6423SLionel Sambuc #define	ERR		1
67*433d6423SLionel Sambuc 
68*433d6423SLionel Sambuc #define CYL_MASK	0xc0	/* mask to extract cyl bits from sec field */
69*433d6423SLionel Sambuc #define CYL_SHIFT	2	/* shift to extract cyl bits from sec field */
70*433d6423SLionel Sambuc #define SEC_MASK	0x3f	/* mask to extract sec bits from sec field */
71*433d6423SLionel Sambuc 
72*433d6423SLionel Sambuc /* Globals  */
73*433d6423SLionel Sambuc char rawsecbuf[SECSIZE + sizeof(long)];
74*433d6423SLionel Sambuc char *secbuf;
75*433d6423SLionel Sambuc int badbases;
76*433d6423SLionel Sambuc int badsizes;
77*433d6423SLionel Sambuc int badorders;
78*433d6423SLionel Sambuc char *dev_name;
79*433d6423SLionel Sambuc int nhead;
80*433d6423SLionel Sambuc int nsec;
81*433d6423SLionel Sambuc int ncyl = 1024;
82*433d6423SLionel Sambuc int readonly;
83*433d6423SLionel Sambuc int override= 0;
84*433d6423SLionel Sambuc 
85*433d6423SLionel Sambuc int main(int argc, char *argv []);
86*433d6423SLionel Sambuc void getgeom(void);
87*433d6423SLionel Sambuc void getboot(char *buffer);
88*433d6423SLionel Sambuc void putboot(char *buffer);
89*433d6423SLionel Sambuc void load_from_file(void);
90*433d6423SLionel Sambuc void save_to_file(void);
91*433d6423SLionel Sambuc void dpl_partitions(int rawflag);
92*433d6423SLionel Sambuc int chk_table(void);
93*433d6423SLionel Sambuc void sec_to_hst(long logsec, unsigned char *hd, unsigned char *sec,
94*433d6423SLionel Sambuc 	unsigned char *cyl);
95*433d6423SLionel Sambuc void mark_partition(struct part_entry *pe);
96*433d6423SLionel Sambuc void change_partition(struct part_entry *entry);
97*433d6423SLionel Sambuc char get_a_char(void);
98*433d6423SLionel Sambuc void print_menu(void);
99*433d6423SLionel Sambuc void adj_base(struct part_entry *pe);
100*433d6423SLionel Sambuc void adj_size(struct part_entry *pe);
101*433d6423SLionel Sambuc struct part_entry *ask_partition(void);
102*433d6423SLionel Sambuc void footnotes(void);
103*433d6423SLionel Sambuc int get_an_int(char *prompt, int *intptr);
104*433d6423SLionel Sambuc void list_part_types(void);
105*433d6423SLionel Sambuc void mark_npartition(struct part_entry *pe);
106*433d6423SLionel Sambuc int mygets(char *buf, int length);
107*433d6423SLionel Sambuc char *systype(int type);
108*433d6423SLionel Sambuc void toggle_active(struct part_entry *pe);
109*433d6423SLionel Sambuc void usage(void);
110*433d6423SLionel Sambuc 
111*433d6423SLionel Sambuc /* One featureful master bootstrap. */
112*433d6423SLionel Sambuc char bootstrap[] = {
113*433d6423SLionel Sambuc 0353,0001,0000,0061,0300,0216,0330,0216,0300,0372,0216,0320,0274,0000,0174,0373,
114*433d6423SLionel Sambuc 0275,0276,0007,0211,0346,0126,0277,0000,0006,0271,0000,0001,0374,0363,0245,0352,
115*433d6423SLionel Sambuc 0044,0006,0000,0000,0264,0002,0315,0026,0250,0010,0164,0033,0350,0071,0001,0174,
116*433d6423SLionel Sambuc 0007,0060,0344,0315,0026,0242,0205,0007,0054,0060,0074,0012,0163,0363,0120,0350,
117*433d6423SLionel Sambuc 0046,0001,0205,0007,0130,0353,0012,0240,0002,0006,0204,0300,0165,0003,0351,0147,
118*433d6423SLionel Sambuc 0000,0230,0262,0005,0366,0362,0262,0200,0000,0302,0210,0340,0120,0350,0234,0000,
119*433d6423SLionel Sambuc 0163,0003,0351,0147,0000,0130,0054,0001,0175,0003,0351,0141,0000,0276,0276,0175,
120*433d6423SLionel Sambuc 0211,0357,0271,0040,0000,0363,0245,0200,0301,0004,0211,0356,0215,0174,0020,0070,
121*433d6423SLionel Sambuc 0154,0004,0164,0016,0213,0135,0010,0053,0134,0010,0213,0135,0012,0033,0134,0012,
122*433d6423SLionel Sambuc 0163,0014,0212,0044,0206,0144,0020,0210,0044,0106,0071,0376,0162,0364,0211,0376,
123*433d6423SLionel Sambuc 0201,0376,0356,0007,0162,0326,0342,0322,0211,0356,0264,0020,0366,0344,0001,0306,
124*433d6423SLionel Sambuc 0200,0174,0004,0001,0162,0026,0353,0021,0204,0322,0175,0041,0211,0356,0200,0174,
125*433d6423SLionel Sambuc 0004,0000,0164,0013,0366,0004,0200,0164,0006,0350,0070,0000,0162,0053,0303,0203,
126*433d6423SLionel Sambuc 0306,0020,0201,0376,0376,0007,0162,0346,0350,0215,0000,0211,0007,0376,0302,0204,
127*433d6423SLionel Sambuc 0322,0174,0023,0315,0021,0321,0340,0321,0340,0200,0344,0003,0070,0342,0167,0355,
128*433d6423SLionel Sambuc 0350,0011,0000,0162,0350,0303,0350,0003,0000,0162,0146,0303,0211,0356,0214,0134,
129*433d6423SLionel Sambuc 0010,0214,0134,0012,0277,0003,0000,0122,0006,0127,0264,0010,0315,0023,0137,0007,
130*433d6423SLionel Sambuc 0200,0341,0077,0376,0306,0210,0310,0366,0346,0211,0303,0213,0104,0010,0213,0124,
131*433d6423SLionel Sambuc 0012,0367,0363,0222,0210,0325,0366,0361,0060,0322,0321,0352,0321,0352,0010,0342,
132*433d6423SLionel Sambuc 0210,0321,0376,0301,0132,0210,0306,0273,0000,0174,0270,0001,0002,0315,0023,0163,
133*433d6423SLionel Sambuc 0020,0200,0374,0200,0164,0011,0117,0174,0006,0060,0344,0315,0023,0163,0270,0371,
134*433d6423SLionel Sambuc 0303,0201,0076,0376,0175,0125,0252,0165,0001,0303,0350,0013,0000,0243,0007,0353,
135*433d6423SLionel Sambuc 0005,0350,0004,0000,0227,0007,0353,0376,0136,0255,0126,0211,0306,0254,0204,0300,
136*433d6423SLionel Sambuc 0164,0011,0264,0016,0273,0001,0000,0315,0020,0353,0362,0303,0057,0144,0145,0166,
137*433d6423SLionel Sambuc 0057,0150,0144,0077,0010,0000,0015,0012,0000,0116,0157,0156,0145,0040,0141,0143,
138*433d6423SLionel Sambuc 0164,0151,0166,0145,0015,0012,0000,0122,0145,0141,0144,0040,0145,0162,0162,0157,
139*433d6423SLionel Sambuc 0162,0040,0000,0116,0157,0164,0040,0142,0157,0157,0164,0141,0142,0154,0145,0040,
140*433d6423SLionel Sambuc 0000,0000,
141*433d6423SLionel Sambuc };
142*433d6423SLionel Sambuc 
143*433d6423SLionel Sambuc int main(int argc, char *argv[])
144*433d6423SLionel Sambuc {
145*433d6423SLionel Sambuc   int argn;
146*433d6423SLionel Sambuc   char *argp;
147*433d6423SLionel Sambuc   int ch;
148*433d6423SLionel Sambuc 
149*433d6423SLionel Sambuc   /* Init */
150*433d6423SLionel Sambuc 
151*433d6423SLionel Sambuc   nhead = DEFAULT_NHEAD;
152*433d6423SLionel Sambuc   nsec = DEFAULT_NSEC;
153*433d6423SLionel Sambuc   for (argn = 1; argn < argc && (argp = argv[argn])[0] == '-'; ++argn) {
154*433d6423SLionel Sambuc 	if (argp[1] == 'h')
155*433d6423SLionel Sambuc 		nhead = atoi(argp + 2);
156*433d6423SLionel Sambuc 	else
157*433d6423SLionel Sambuc 		if (argp[1] == 's') nsec = atoi(argp + 2);
158*433d6423SLionel Sambuc 	else
159*433d6423SLionel Sambuc 		usage();
160*433d6423SLionel Sambuc 	override= 1;
161*433d6423SLionel Sambuc   }
162*433d6423SLionel Sambuc 
163*433d6423SLionel Sambuc   if (argn == argc)
164*433d6423SLionel Sambuc 	dev_name = DEFAULT_DEV;
165*433d6423SLionel Sambuc   else if (argn == argc - 1)
166*433d6423SLionel Sambuc 	dev_name = argv[argn];
167*433d6423SLionel Sambuc   else
168*433d6423SLionel Sambuc 	usage();
169*433d6423SLionel Sambuc 
170*433d6423SLionel Sambuc   /* Align the sector buffer in such a way that the partition table is at
171*433d6423SLionel Sambuc    * a mod 4 offset in memory.  Some weird people add alignment checks to
172*433d6423SLionel Sambuc    * their Minix!
173*433d6423SLionel Sambuc    */
174*433d6423SLionel Sambuc   secbuf = rawsecbuf;
175*433d6423SLionel Sambuc   while ((long)(secbuf + PART_TABLE_OFF) % sizeof(long) != 0) secbuf++;
176*433d6423SLionel Sambuc 
177*433d6423SLionel Sambuc   getgeom();
178*433d6423SLionel Sambuc   getboot(secbuf);
179*433d6423SLionel Sambuc   chk_table();
180*433d6423SLionel Sambuc 
181*433d6423SLionel Sambuc   do {
182*433d6423SLionel Sambuc 	putchar('\n');
183*433d6423SLionel Sambuc 	dpl_partitions(0);
184*433d6423SLionel Sambuc 	printf(
185*433d6423SLionel Sambuc 	  "\n(Enter 'h' for help.  A null line will abort any operation) ");
186*433d6423SLionel Sambuc 	ch = get_a_char();
187*433d6423SLionel Sambuc 	putchar('\n');
188*433d6423SLionel Sambuc 	switch (ch) {
189*433d6423SLionel Sambuc 	    case '+':	footnotes();			break;
190*433d6423SLionel Sambuc 	    case 'a':	toggle_active(ask_partition());	break;
191*433d6423SLionel Sambuc 	    case 'B':	adj_base(ask_partition()); 	break;
192*433d6423SLionel Sambuc 	    case 'c':	change_partition(ask_partition());	break;
193*433d6423SLionel Sambuc 	    case 'h':	print_menu();			break;
194*433d6423SLionel Sambuc 	    case 'l':	load_from_file();	  	break;
195*433d6423SLionel Sambuc 	    case 'm':	mark_partition(ask_partition());	break;
196*433d6423SLionel Sambuc 	    case 'n':	mark_npartition(ask_partition());	break;
197*433d6423SLionel Sambuc 	    case 'p':	dpl_partitions(1);  		break;
198*433d6423SLionel Sambuc 	    case 0:
199*433d6423SLionel Sambuc 	    case 'q':	exit(0);
200*433d6423SLionel Sambuc 	    case 'S':	adj_size(ask_partition()); 	break;
201*433d6423SLionel Sambuc 	    case 's':	save_to_file();	  		break;
202*433d6423SLionel Sambuc 	    case 't':	list_part_types();	 	break;
203*433d6423SLionel Sambuc 	    case 'v':
204*433d6423SLionel Sambuc 		printf("Partition table is %svalid\n",
205*433d6423SLionel Sambuc 			chk_table() == OK ? "" : "in");
206*433d6423SLionel Sambuc 		break;
207*433d6423SLionel Sambuc 	    case 'w':
208*433d6423SLionel Sambuc 		if (readonly)
209*433d6423SLionel Sambuc 			printf("Write disabled\n");
210*433d6423SLionel Sambuc 		else if(chk_table() == OK) {
211*433d6423SLionel Sambuc 			putboot(secbuf);
212*433d6423SLionel Sambuc 			printf(
213*433d6423SLionel Sambuc 	"Partition table has been updated and the file system synced.\n");
214*433d6423SLionel Sambuc 			printf("Please reboot now.\n");
215*433d6423SLionel Sambuc 			exit(0);
216*433d6423SLionel Sambuc 		} else
217*433d6423SLionel Sambuc 			printf("Not written\n");
218*433d6423SLionel Sambuc 		break;
219*433d6423SLionel Sambuc 	    default:	printf(" %c ????\n", ch);	break;
220*433d6423SLionel Sambuc   	}
221*433d6423SLionel Sambuc   }
222*433d6423SLionel Sambuc   while (1);
223*433d6423SLionel Sambuc }
224*433d6423SLionel Sambuc 
225*433d6423SLionel Sambuc 
226*433d6423SLionel Sambuc #ifdef UNIX
227*433d6423SLionel Sambuc 
228*433d6423SLionel Sambuc void getgeom(void)
229*433d6423SLionel Sambuc {
230*433d6423SLionel Sambuc   struct part_geom geom;
231*433d6423SLionel Sambuc   int fd, r;
232*433d6423SLionel Sambuc 
233*433d6423SLionel Sambuc   if (override) return;
234*433d6423SLionel Sambuc 
235*433d6423SLionel Sambuc   if ((fd= open(dev_name, O_RDONLY)) < 0) return;
236*433d6423SLionel Sambuc 
237*433d6423SLionel Sambuc   r = ioctl(fd, DIOCGETP, &geom);
238*433d6423SLionel Sambuc   close(fd);
239*433d6423SLionel Sambuc   if (r < 0) return;
240*433d6423SLionel Sambuc 
241*433d6423SLionel Sambuc   nhead = geom.heads;
242*433d6423SLionel Sambuc   nsec = geom.sectors;
243*433d6423SLionel Sambuc   ncyl = geom.cylinders;
244*433d6423SLionel Sambuc 
245*433d6423SLionel Sambuc   printf("Geometry of %s: %dx%dx%d\n", dev_name, ncyl, nhead, nsec);
246*433d6423SLionel Sambuc }
247*433d6423SLionel Sambuc 
248*433d6423SLionel Sambuc static int devfd;
249*433d6423SLionel Sambuc 
250*433d6423SLionel Sambuc void getboot(char *buffer)
251*433d6423SLionel Sambuc {
252*433d6423SLionel Sambuc   devfd = open(dev_name, 2);
253*433d6423SLionel Sambuc   if (devfd < 0) {
254*433d6423SLionel Sambuc 	printf("No write permission on %s\n", dev_name);
255*433d6423SLionel Sambuc 	readonly = 1;
256*433d6423SLionel Sambuc 	devfd = open(dev_name, 0);
257*433d6423SLionel Sambuc   }
258*433d6423SLionel Sambuc   if (devfd < 0) {
259*433d6423SLionel Sambuc 	printf("Cannot open device %s\n", dev_name);
260*433d6423SLionel Sambuc 	exit(1);
261*433d6423SLionel Sambuc   }
262*433d6423SLionel Sambuc   if (read(devfd, buffer, SECSIZE) != SECSIZE) {
263*433d6423SLionel Sambuc 	printf("Cannot read boot sector\n");
264*433d6423SLionel Sambuc 	exit(1);
265*433d6423SLionel Sambuc   }
266*433d6423SLionel Sambuc   if (* (unsigned short *) &buffer[510] != 0xAA55) {
267*433d6423SLionel Sambuc 	printf("Invalid boot sector on %s.\n", dev_name);
268*433d6423SLionel Sambuc 	printf("Partition table reset and boot code installed.\n");
269*433d6423SLionel Sambuc 	memset(buffer, 0, 512);
270*433d6423SLionel Sambuc 	memcpy(buffer, bootstrap, sizeof(bootstrap));
271*433d6423SLionel Sambuc 	* (unsigned short *) &buffer[510] = 0xAA55;
272*433d6423SLionel Sambuc   }
273*433d6423SLionel Sambuc }
274*433d6423SLionel Sambuc 
275*433d6423SLionel Sambuc void putboot(char *buffer)
276*433d6423SLionel Sambuc {
277*433d6423SLionel Sambuc   if (lseek(devfd, 0L, 0) < 0) {
278*433d6423SLionel Sambuc 	printf("Seek error during write\n");
279*433d6423SLionel Sambuc 	exit(1);
280*433d6423SLionel Sambuc   }
281*433d6423SLionel Sambuc   if (write(devfd, buffer, SECSIZE) != SECSIZE) {
282*433d6423SLionel Sambuc 	printf("Write error\n");
283*433d6423SLionel Sambuc 	exit(1);
284*433d6423SLionel Sambuc   }
285*433d6423SLionel Sambuc   sync();
286*433d6423SLionel Sambuc }
287*433d6423SLionel Sambuc 
288*433d6423SLionel Sambuc #endif
289*433d6423SLionel Sambuc 
290*433d6423SLionel Sambuc 
291*433d6423SLionel Sambuc void load_from_file(void)
292*433d6423SLionel Sambuc {
293*433d6423SLionel Sambuc /* Load buffer from file  */
294*433d6423SLionel Sambuc 
295*433d6423SLionel Sambuc   char file[80];
296*433d6423SLionel Sambuc   int fd;
297*433d6423SLionel Sambuc 
298*433d6423SLionel Sambuc   printf("Enter name of file to load from: ");
299*433d6423SLionel Sambuc   if (!mygets(file, (int) sizeof file)) return;
300*433d6423SLionel Sambuc   fd = open(file, LOAD_OPEN_MODE);
301*433d6423SLionel Sambuc   if (fd < 0) {
302*433d6423SLionel Sambuc 	printf("Cannot open %s\n", file);
303*433d6423SLionel Sambuc 	return;
304*433d6423SLionel Sambuc   }
305*433d6423SLionel Sambuc   if (read(fd, secbuf, SECSIZE) != SECSIZE || close(fd) != 0) {
306*433d6423SLionel Sambuc 	printf("Read error\n");
307*433d6423SLionel Sambuc 	exit(1);
308*433d6423SLionel Sambuc   }
309*433d6423SLionel Sambuc   printf("Loaded from %s OK\n", file);
310*433d6423SLionel Sambuc   chk_table();
311*433d6423SLionel Sambuc }
312*433d6423SLionel Sambuc 
313*433d6423SLionel Sambuc 
314*433d6423SLionel Sambuc void save_to_file(void)
315*433d6423SLionel Sambuc {
316*433d6423SLionel Sambuc /* Save to file  */
317*433d6423SLionel Sambuc 
318*433d6423SLionel Sambuc   char file[80];
319*433d6423SLionel Sambuc   int fd;
320*433d6423SLionel Sambuc 
321*433d6423SLionel Sambuc   printf("Enter name of file to save to: ");
322*433d6423SLionel Sambuc   if (!mygets(file, (int) sizeof file)) return;
323*433d6423SLionel Sambuc   if(chk_table() != OK) printf("Saving anyway\n");
324*433d6423SLionel Sambuc   fd = creat(file, SAVE_CREAT_MODE);
325*433d6423SLionel Sambuc #ifdef DOS
326*433d6423SLionel Sambuc   if (fd < 0) {
327*433d6423SLionel Sambuc 	printf("Cannot creat %s\n", file);
328*433d6423SLionel Sambuc 	return;
329*433d6423SLionel Sambuc   }
330*433d6423SLionel Sambuc   close(fd);
331*433d6423SLionel Sambuc   fd = open(file, 0x8001);
332*433d6423SLionel Sambuc #endif
333*433d6423SLionel Sambuc   if (fd < 0)
334*433d6423SLionel Sambuc 	printf("Cannot open %s\n", file);
335*433d6423SLionel Sambuc   else if (write(fd, secbuf, SECSIZE) != SECSIZE || close(fd) != 0)
336*433d6423SLionel Sambuc 	printf("Write error\n");
337*433d6423SLionel Sambuc   else
338*433d6423SLionel Sambuc 	printf("Saved to %s OK\n", file);
339*433d6423SLionel Sambuc }
340*433d6423SLionel Sambuc 
341*433d6423SLionel Sambuc 
342*433d6423SLionel Sambuc void dpl_partitions(int rawflag)
343*433d6423SLionel Sambuc {
344*433d6423SLionel Sambuc /* Display partition table */
345*433d6423SLionel Sambuc 
346*433d6423SLionel Sambuc   char active[5];
347*433d6423SLionel Sambuc   char basefootnote;
348*433d6423SLionel Sambuc   int cyl_mask;
349*433d6423SLionel Sambuc   int devnum;
350*433d6423SLionel Sambuc   char *format;
351*433d6423SLionel Sambuc   int i;
352*433d6423SLionel Sambuc   int i1;
353*433d6423SLionel Sambuc   char orderfootnote;
354*433d6423SLionel Sambuc   struct part_entry *pe;
355*433d6423SLionel Sambuc   struct part_entry *pe1;
356*433d6423SLionel Sambuc   int sec_mask;
357*433d6423SLionel Sambuc   char sizefootnote;
358*433d6423SLionel Sambuc   char type[10];
359*433d6423SLionel Sambuc 
360*433d6423SLionel Sambuc   badbases = 0;
361*433d6423SLionel Sambuc   badsizes = 0;
362*433d6423SLionel Sambuc   badorders = 0;
363*433d6423SLionel Sambuc   if (rawflag) {
364*433d6423SLionel Sambuc 	cyl_mask = 0;		/* no contribution of cyl to sec */
365*433d6423SLionel Sambuc 	sec_mask = 0xff;
366*433d6423SLionel Sambuc         format =
367*433d6423SLionel Sambuc "%2d   %3d%c  %4s %-9s  x%02x %3d  x%02x   x%02x %3d  x%02x %7ld%c%7ld %7ld%c\n";
368*433d6423SLionel Sambuc   } else {
369*433d6423SLionel Sambuc 	cyl_mask = CYL_MASK;
370*433d6423SLionel Sambuc 	sec_mask = SEC_MASK;
371*433d6423SLionel Sambuc 	format =
372*433d6423SLionel Sambuc "%2d   %3d%c  %4s %-9s %4d %3d %3d   %4d %3d  %3d %7ld%c%7ld %7ld%c\n";
373*433d6423SLionel Sambuc   }
374*433d6423SLionel Sambuc   printf(
375*433d6423SLionel Sambuc "                          ----first----  -----last----  --------sectors-------\n"
376*433d6423SLionel Sambuc 	);
377*433d6423SLionel Sambuc   printf(
378*433d6423SLionel Sambuc "Num Sorted Act  Type     Cyl Head Sec   Cyl Head Sec    Base    Last    Size\n"
379*433d6423SLionel Sambuc 	);
380*433d6423SLionel Sambuc   pe = (struct part_entry *) &secbuf[PART_TABLE_OFF];
381*433d6423SLionel Sambuc   for (i = 1; i <= NR_PARTITIONS; i++, pe++) {
382*433d6423SLionel Sambuc 	if (rawflag) {
383*433d6423SLionel Sambuc 		sprintf(active, "0x%02x", pe->bootind);
384*433d6423SLionel Sambuc 		sprintf(type, "0x%02x", pe->sysind);
385*433d6423SLionel Sambuc 	} else {
386*433d6423SLionel Sambuc 		sprintf(active, "%s", pe->bootind == ACTIVE_FLAG ? "A  " : "");
387*433d6423SLionel Sambuc 		sprintf(type, "%s", systype(pe->sysind));
388*433d6423SLionel Sambuc 	}
389*433d6423SLionel Sambuc 
390*433d6423SLionel Sambuc 	/* Prepare warnings about confusing setups from old versions. */
391*433d6423SLionel Sambuc 	basefootnote = orderfootnote = sizefootnote = ' ';
392*433d6423SLionel Sambuc 	if (pe->sysind == MINIX_PART && pe->lowsec & 1) {
393*433d6423SLionel Sambuc 		basefootnote = '+';
394*433d6423SLionel Sambuc 		++badbases;
395*433d6423SLionel Sambuc 	}
396*433d6423SLionel Sambuc 	if (pe->size & 1) {
397*433d6423SLionel Sambuc 		sizefootnote = '-';
398*433d6423SLionel Sambuc 		++badsizes;
399*433d6423SLionel Sambuc 	}
400*433d6423SLionel Sambuc 
401*433d6423SLionel Sambuc 	/* Calculate the "device numbers" resulting from the misguided sorting
402*433d6423SLionel Sambuc 	 * in the wini drivers.  The drivers use this conditional for
403*433d6423SLionel Sambuc 	 * swapping wn[j] > wn[j+1]:
404*433d6423SLionel Sambuc 	 *
405*433d6423SLionel Sambuc 	 *	if ((wn[j].wn_low == 0 && wn[j+1].wn_low != 0) ||
406*433d6423SLionel Sambuc 	 *	    (wn[j].wn_low > wn[j+1].wn_low && wn[j+1].wn_low != 0)) {
407*433d6423SLionel Sambuc 	 *
408*433d6423SLionel Sambuc 	 * which simplifies to:
409*433d6423SLionel Sambuc 	 *
410*433d6423SLionel Sambuc 	 *	if (wn[j+1].wn_low != 0 &&
411*433d6423SLionel Sambuc 	 *	    (wn[j].wn_low == 0 || wn[j].wn_low > wn[j+1].wn_low)) {
412*433d6423SLionel Sambuc 	 */
413*433d6423SLionel Sambuc 	devnum = 1;
414*433d6423SLionel Sambuc 	for (i1 = 1, pe1 = (struct part_entry *) &secbuf[PART_TABLE_OFF];
415*433d6423SLionel Sambuc 	     i1 <= NR_PARTITIONS; ++i1, ++pe1)
416*433d6423SLionel Sambuc 		if ((pe1->lowsec == 0 && pe->lowsec == 0 && pe1 < pe) ||
417*433d6423SLionel Sambuc 		    (pe1->lowsec != 0 &&
418*433d6423SLionel Sambuc 		     (pe->lowsec == 0 || pe->lowsec > pe1->lowsec)))
419*433d6423SLionel Sambuc 			++devnum;	/* pe1 contents < pe contents */
420*433d6423SLionel Sambuc 	if (devnum != i) {
421*433d6423SLionel Sambuc 		orderfootnote = '#';
422*433d6423SLionel Sambuc 		++badorders;
423*433d6423SLionel Sambuc 	}
424*433d6423SLionel Sambuc 
425*433d6423SLionel Sambuc 	printf(format,
426*433d6423SLionel Sambuc 		i,
427*433d6423SLionel Sambuc 		devnum,
428*433d6423SLionel Sambuc 		orderfootnote,
429*433d6423SLionel Sambuc 		active,
430*433d6423SLionel Sambuc 		type,
431*433d6423SLionel Sambuc 		pe->start_cyl + ((pe->start_sec & cyl_mask) << CYL_SHIFT),
432*433d6423SLionel Sambuc 		pe->start_head,
433*433d6423SLionel Sambuc 		pe->start_sec & sec_mask,
434*433d6423SLionel Sambuc 		pe->last_cyl + ((pe->last_sec & cyl_mask) << CYL_SHIFT),
435*433d6423SLionel Sambuc 		pe->last_head,
436*433d6423SLionel Sambuc 		pe->last_sec & sec_mask,
437*433d6423SLionel Sambuc 		pe->lowsec,
438*433d6423SLionel Sambuc 		basefootnote,
439*433d6423SLionel Sambuc 		pe->lowsec + (pe->size == 0 ? 0 : pe->size - 1),
440*433d6423SLionel Sambuc 		pe->size,
441*433d6423SLionel Sambuc 		sizefootnote);
442*433d6423SLionel Sambuc   }
443*433d6423SLionel Sambuc }
444*433d6423SLionel Sambuc 
445*433d6423SLionel Sambuc 
446*433d6423SLionel Sambuc int chk_table(void)
447*433d6423SLionel Sambuc {
448*433d6423SLionel Sambuc /* Check partition table */
449*433d6423SLionel Sambuc 
450*433d6423SLionel Sambuc   int active;
451*433d6423SLionel Sambuc   unsigned char cylinder;
452*433d6423SLionel Sambuc   unsigned char head;
453*433d6423SLionel Sambuc   int i;
454*433d6423SLionel Sambuc   int i1;
455*433d6423SLionel Sambuc   int maxhead;
456*433d6423SLionel Sambuc   int maxsec;
457*433d6423SLionel Sambuc   struct part_entry *pe;
458*433d6423SLionel Sambuc   struct part_entry *pe1;
459*433d6423SLionel Sambuc   unsigned char sector;
460*433d6423SLionel Sambuc   int seenpart;
461*433d6423SLionel Sambuc   int status;
462*433d6423SLionel Sambuc 
463*433d6423SLionel Sambuc   active = 0;
464*433d6423SLionel Sambuc   maxhead = 0;
465*433d6423SLionel Sambuc   maxsec = 0;
466*433d6423SLionel Sambuc   pe = (struct part_entry *) &secbuf[PART_TABLE_OFF];
467*433d6423SLionel Sambuc   seenpart = 0;
468*433d6423SLionel Sambuc   status = OK;
469*433d6423SLionel Sambuc   for (i = 1; i <= NR_PARTITIONS; i++, ++pe) {
470*433d6423SLionel Sambuc 	if (pe->bootind == ACTIVE_FLAG) active++;
471*433d6423SLionel Sambuc 	sec_to_hst(pe->lowsec, &head, &sector, &cylinder);
472*433d6423SLionel Sambuc 	if (pe->size == 0 && pe->lowsec == 0) sector = 0;
473*433d6423SLionel Sambuc 	if (head != pe->start_head || sector != pe->start_sec ||
474*433d6423SLionel Sambuc 	    cylinder != pe->start_cyl) {
475*433d6423SLionel Sambuc 		printf("Inconsistent base in partition %d.\n", i);
476*433d6423SLionel Sambuc 		printf("Suspect head and sector parameters.\n");
477*433d6423SLionel Sambuc 		status = ERR;
478*433d6423SLionel Sambuc 	}
479*433d6423SLionel Sambuc 	if (pe->size != 0 || pe->lowsec != 0)
480*433d6423SLionel Sambuc 	      sec_to_hst(pe->lowsec + pe->size - 1, &head, &sector, &cylinder);
481*433d6423SLionel Sambuc 	if (head != pe->last_head || sector != pe->last_sec ||
482*433d6423SLionel Sambuc 	    cylinder != pe->last_cyl) {
483*433d6423SLionel Sambuc 		printf("Inconsistent size in partition %d.\n", i);
484*433d6423SLionel Sambuc 		printf("Suspect head and sector parameters.\n");
485*433d6423SLionel Sambuc 		status = ERR;
486*433d6423SLionel Sambuc 	}
487*433d6423SLionel Sambuc 	if (pe->size == 0) continue;
488*433d6423SLionel Sambuc 	seenpart = 1;
489*433d6423SLionel Sambuc 	for (i1 = i + 1, pe1 = pe + 1; i1 <= NR_PARTITIONS; ++i1, ++pe1) {
490*433d6423SLionel Sambuc 		if ((pe->lowsec >= pe1->lowsec &&
491*433d6423SLionel Sambuc 		     pe->lowsec < pe1->lowsec + pe1->size) ||
492*433d6423SLionel Sambuc 		    (pe->lowsec + pe->size - 1 >= pe1->lowsec &&
493*433d6423SLionel Sambuc 		    pe->lowsec + pe->size - 1 < pe1->lowsec + pe1->size))
494*433d6423SLionel Sambuc 		{
495*433d6423SLionel Sambuc 			printf("Overlap between partitions %d and %d\n",
496*433d6423SLionel Sambuc 				i, i1);
497*433d6423SLionel Sambuc 			status = ERR;
498*433d6423SLionel Sambuc 		}
499*433d6423SLionel Sambuc 	}
500*433d6423SLionel Sambuc 	if (pe->lowsec + pe->size < pe->lowsec) {
501*433d6423SLionel Sambuc 		printf("Overflow from preposterous size in partition %d.\n",
502*433d6423SLionel Sambuc 			i);
503*433d6423SLionel Sambuc 		status = ERR;
504*433d6423SLionel Sambuc 	}
505*433d6423SLionel Sambuc 	if (maxhead < pe->start_head) maxhead = pe->start_head;
506*433d6423SLionel Sambuc 	if (maxhead < pe->last_head) maxhead = pe->last_head;
507*433d6423SLionel Sambuc 	if (maxsec < (pe->start_sec & SEC_MASK))
508*433d6423SLionel Sambuc 		maxsec = (pe->start_sec & SEC_MASK);
509*433d6423SLionel Sambuc 	if (maxsec < (pe->last_sec & SEC_MASK))
510*433d6423SLionel Sambuc 		maxsec = (pe->last_sec & SEC_MASK);
511*433d6423SLionel Sambuc   }
512*433d6423SLionel Sambuc   if (seenpart) {
513*433d6423SLionel Sambuc 	if (maxhead + 1 != nhead || maxsec != nsec) {
514*433d6423SLionel Sambuc 		printf(
515*433d6423SLionel Sambuc 	"Disk appears to have mis-specified number of heads or sectors.\n");
516*433d6423SLionel Sambuc 		printf("Try  fdisk -h%d -s%d %s  instead of\n",
517*433d6423SLionel Sambuc 			maxhead + 1, maxsec, dev_name);
518*433d6423SLionel Sambuc 		printf("     fdisk -h%d -s%d %s\n", nhead, nsec, dev_name);
519*433d6423SLionel Sambuc 		seenpart = 0;
520*433d6423SLionel Sambuc 	}
521*433d6423SLionel Sambuc   } else {
522*433d6423SLionel Sambuc 	printf(
523*433d6423SLionel Sambuc 	"Empty table - skipping test on number of heads and sectors.\n");
524*433d6423SLionel Sambuc 	printf("Assuming %d heads and %d sectors.\n", nhead, nsec);
525*433d6423SLionel Sambuc   }
526*433d6423SLionel Sambuc   if (!seenpart) printf("Do not write the table if you are not sure!.\n");
527*433d6423SLionel Sambuc   if (active > 1) {
528*433d6423SLionel Sambuc 	printf("%d active partitions\n", active);
529*433d6423SLionel Sambuc 	status = ERR;
530*433d6423SLionel Sambuc   }
531*433d6423SLionel Sambuc   return(status);
532*433d6423SLionel Sambuc }
533*433d6423SLionel Sambuc 
534*433d6423SLionel Sambuc void sec_to_hst(long logsec, unsigned char *hd, unsigned char *sec,
535*433d6423SLionel Sambuc 	unsigned char *cyl)
536*433d6423SLionel Sambuc {
537*433d6423SLionel Sambuc /* Convert a logical sector number to  head / sector / cylinder */
538*433d6423SLionel Sambuc 
539*433d6423SLionel Sambuc   int bigcyl;
540*433d6423SLionel Sambuc 
541*433d6423SLionel Sambuc   bigcyl = logsec / (nhead * nsec);
542*433d6423SLionel Sambuc   *sec = (logsec % nsec) + 1 + ((bigcyl >> CYL_SHIFT) & CYL_MASK);
543*433d6423SLionel Sambuc   *cyl = bigcyl;
544*433d6423SLionel Sambuc   *hd = (logsec % (nhead * nsec)) / nsec;
545*433d6423SLionel Sambuc }
546*433d6423SLionel Sambuc 
547*433d6423SLionel Sambuc void mark_partition(struct part_entry *pe)
548*433d6423SLionel Sambuc {
549*433d6423SLionel Sambuc /* Mark a partition as being of type MINIX. */
550*433d6423SLionel Sambuc 
551*433d6423SLionel Sambuc   if (pe != NULL) {
552*433d6423SLionel Sambuc 	pe->sysind = MINIX_PART;
553*433d6423SLionel Sambuc 	printf("Partition type is now MINIX\n");
554*433d6423SLionel Sambuc   }
555*433d6423SLionel Sambuc }
556*433d6423SLionel Sambuc 
557*433d6423SLionel Sambuc void change_partition(struct part_entry *entry)
558*433d6423SLionel Sambuc {
559*433d6423SLionel Sambuc /* Get partition info : first & last cylinder */
560*433d6423SLionel Sambuc 
561*433d6423SLionel Sambuc   int first, last;
562*433d6423SLionel Sambuc   long low, high;
563*433d6423SLionel Sambuc   int ch;
564*433d6423SLionel Sambuc 
565*433d6423SLionel Sambuc   if (entry == NULL) return;
566*433d6423SLionel Sambuc   while (1) {
567*433d6423SLionel Sambuc 	if (!get_an_int("\tEnter first cylinder (an integer >= 0): ", &first))
568*433d6423SLionel Sambuc 		return;
569*433d6423SLionel Sambuc 	if (first >= 0) break;
570*433d6423SLionel Sambuc 	printf("\t\tThat looks like %d which is negative\n", first);
571*433d6423SLionel Sambuc   }
572*433d6423SLionel Sambuc   while (1) {
573*433d6423SLionel Sambuc 	if (!get_an_int(
574*433d6423SLionel Sambuc 	"\tEnter last cylinder (an integer >= the first cylinder): ", &last))
575*433d6423SLionel Sambuc 		return;
576*433d6423SLionel Sambuc 	if (last >= first) break;
577*433d6423SLionel Sambuc 	printf("\t\tThat looks like %d which is too small\n", last);
578*433d6423SLionel Sambuc   }
579*433d6423SLionel Sambuc   if (first == 0 && last == 0) {
580*433d6423SLionel Sambuc 	entry->bootind = 0;
581*433d6423SLionel Sambuc 	entry->start_head = 0;
582*433d6423SLionel Sambuc 	entry->start_sec = 0;
583*433d6423SLionel Sambuc 	entry->start_cyl = 0;
584*433d6423SLionel Sambuc 	entry->sysind = NO_PART;
585*433d6423SLionel Sambuc 	entry->last_head = 0;
586*433d6423SLionel Sambuc 	entry->last_sec = 0;
587*433d6423SLionel Sambuc 	entry->last_cyl = 0;
588*433d6423SLionel Sambuc 	entry->lowsec = 0;
589*433d6423SLionel Sambuc 	entry->size = 0;
590*433d6423SLionel Sambuc 	printf("Partition deleted\n");
591*433d6423SLionel Sambuc 	return;
592*433d6423SLionel Sambuc   }
593*433d6423SLionel Sambuc   low = first & 0xffff;
594*433d6423SLionel Sambuc   low = low * nsec * nhead;
595*433d6423SLionel Sambuc   if (low == 0) low = 1;	/* sec0 is master boot record */
596*433d6423SLionel Sambuc   high = last & 0xffff;
597*433d6423SLionel Sambuc   high = (high + 1) * nsec * nhead - 1;
598*433d6423SLionel Sambuc   entry->lowsec = low;
599*433d6423SLionel Sambuc   entry->size = high - low + 1;
600*433d6423SLionel Sambuc   if (entry->size & 1) {
601*433d6423SLionel Sambuc 	/* Adjust size to even since Minix works with blocks of 2 sectors. */
602*433d6423SLionel Sambuc 	--high;
603*433d6423SLionel Sambuc 	--entry->size;
604*433d6423SLionel Sambuc 	printf("Size reduced by 1 to make it even\n");
605*433d6423SLionel Sambuc   }
606*433d6423SLionel Sambuc   sec_to_hst(low, &entry->start_head, &entry->start_sec, &entry->start_cyl);
607*433d6423SLionel Sambuc   sec_to_hst(high, &entry->last_head, &entry->last_sec, &entry->last_cyl);
608*433d6423SLionel Sambuc   printf("Base of partition changed to %ld, size changed to %ld\n",
609*433d6423SLionel Sambuc 	 entry->lowsec, entry->size);
610*433d6423SLionel Sambuc 
611*433d6423SLionel Sambuc   /* Accept the MINIX partition type.  Usually ignore foreign types, so this
612*433d6423SLionel Sambuc    * fdisk can be used on foreign partitions.  Don't allow NO_PART, because
613*433d6423SLionel Sambuc    * many DOS fdisks crash on it.
614*433d6423SLionel Sambuc    */
615*433d6423SLionel Sambuc   if (entry->sysind == NO_PART) {
616*433d6423SLionel Sambuc 	entry->sysind = MINIX_PART;
617*433d6423SLionel Sambuc 	printf("Partition type changed from None to MINIX\n");
618*433d6423SLionel Sambuc   } else if (entry->sysind == MINIX_PART)
619*433d6423SLionel Sambuc 	printf("Leaving partition type as MINIX\n");
620*433d6423SLionel Sambuc   else while (1) {
621*433d6423SLionel Sambuc 	printf("\tChange partition type from %s to MINIX? (y/n) ",
622*433d6423SLionel Sambuc 		systype(entry->sysind));
623*433d6423SLionel Sambuc 	ch = get_a_char();
624*433d6423SLionel Sambuc 	if (ch == 0 || ch == 'n') {
625*433d6423SLionel Sambuc 		printf("Leaving partition type as %s\n",
626*433d6423SLionel Sambuc 			systype(entry->sysind));
627*433d6423SLionel Sambuc 		break;
628*433d6423SLionel Sambuc 	} else if (ch == 'y') {
629*433d6423SLionel Sambuc 		entry->sysind = MINIX_PART;
630*433d6423SLionel Sambuc 		printf("Partition type changed from %s to MINIX\n",
631*433d6423SLionel Sambuc 			systype(entry->sysind));
632*433d6423SLionel Sambuc 		break;
633*433d6423SLionel Sambuc 	}
634*433d6423SLionel Sambuc   }
635*433d6423SLionel Sambuc 
636*433d6423SLionel Sambuc   if (entry->bootind == ACTIVE_FLAG)
637*433d6423SLionel Sambuc 	printf("Leaving partition active\n");
638*433d6423SLionel Sambuc   else while (1) {
639*433d6423SLionel Sambuc 	printf("\tChange partition to active? (y/n) ");
640*433d6423SLionel Sambuc 	ch = get_a_char();
641*433d6423SLionel Sambuc 	if (ch == 0 || ch == 'n') {
642*433d6423SLionel Sambuc 		printf("Leaving partition inactive\n");
643*433d6423SLionel Sambuc 		break;
644*433d6423SLionel Sambuc 	} else if (ch == 'y') {
645*433d6423SLionel Sambuc 		toggle_active(entry);
646*433d6423SLionel Sambuc 		break;
647*433d6423SLionel Sambuc 	}
648*433d6423SLionel Sambuc   }
649*433d6423SLionel Sambuc }
650*433d6423SLionel Sambuc 
651*433d6423SLionel Sambuc char get_a_char(void)
652*433d6423SLionel Sambuc {
653*433d6423SLionel Sambuc /* Read 1 character and discard rest of line */
654*433d6423SLionel Sambuc 
655*433d6423SLionel Sambuc   char buf[80];
656*433d6423SLionel Sambuc 
657*433d6423SLionel Sambuc   if (!mygets(buf, (int) sizeof buf)) return(0);
658*433d6423SLionel Sambuc   return(*buf);
659*433d6423SLionel Sambuc }
660*433d6423SLionel Sambuc 
661*433d6423SLionel Sambuc void print_menu(void)
662*433d6423SLionel Sambuc {
663*433d6423SLionel Sambuc   printf("Type a command letter, then a carriage return:\n");
664*433d6423SLionel Sambuc   printf("   + - explain any footnotes (+, -, #)\n");
665*433d6423SLionel Sambuc   printf("   a - toggle an active flag\n");
666*433d6423SLionel Sambuc   printf("   B - adjust a base sector\n");
667*433d6423SLionel Sambuc   printf("   c - change a partition\n");
668*433d6423SLionel Sambuc   printf("   l - load boot block (including partition table) from a file\n");
669*433d6423SLionel Sambuc   printf("   m - mark a partition as a MINIX partition\n");
670*433d6423SLionel Sambuc   printf("   n - mark a partition as a non-MINIX partition\n");
671*433d6423SLionel Sambuc   printf("   p - print raw partition table\n");
672*433d6423SLionel Sambuc   printf("   q - quit without making any changes\n");
673*433d6423SLionel Sambuc   printf("   S - adjust a size (by changing the last sector)\n");
674*433d6423SLionel Sambuc   printf("   s - save boot block (including partition table) on a file\n");
675*433d6423SLionel Sambuc   printf("   t - print known partition types\n");
676*433d6423SLionel Sambuc   printf("   v - verify partition table\n");
677*433d6423SLionel Sambuc  if (readonly)
678*433d6423SLionel Sambuc   printf("   w - write (disabled)\n");
679*433d6423SLionel Sambuc  else
680*433d6423SLionel Sambuc   printf("   w - write changed partition table back to disk and exit\n");
681*433d6423SLionel Sambuc }
682*433d6423SLionel Sambuc 
683*433d6423SLionel Sambuc 
684*433d6423SLionel Sambuc /* Here are the DOS routines for reading and writing the boot sector. */
685*433d6423SLionel Sambuc 
686*433d6423SLionel Sambuc #ifdef DOS
687*433d6423SLionel Sambuc 
688*433d6423SLionel Sambuc union REGS regs;
689*433d6423SLionel Sambuc struct SREGS sregs;
690*433d6423SLionel Sambuc int drivenum;
691*433d6423SLionel Sambuc 
692*433d6423SLionel Sambuc void getboot(char *buffer)
693*433d6423SLionel Sambuc {
694*433d6423SLionel Sambuc /* Read boot sector  */
695*433d6423SLionel Sambuc 
696*433d6423SLionel Sambuc   segread(&sregs);		/* get ds */
697*433d6423SLionel Sambuc 
698*433d6423SLionel Sambuc   if (dev_name[1] != ':') {
699*433d6423SLionel Sambuc 	printf("Invalid drive %s\n", dev_name);
700*433d6423SLionel Sambuc 	exit(1);
701*433d6423SLionel Sambuc   }
702*433d6423SLionel Sambuc   if (*dev_name >= 'a') *dev_name += 'A' - 'a';
703*433d6423SLionel Sambuc   drivenum = (*dev_name - 'C') & 0xff;
704*433d6423SLionel Sambuc   if (drivenum < 0 || drivenum > 7) {
705*433d6423SLionel Sambuc 	printf("Funny drive number %d\n", drivenum);
706*433d6423SLionel Sambuc 	exit(1);
707*433d6423SLionel Sambuc   }
708*433d6423SLionel Sambuc   regs.x.ax = 0x201;		/* read 1 sectors	 */
709*433d6423SLionel Sambuc   regs.h.ch = 0;		/* cylinder		 */
710*433d6423SLionel Sambuc   regs.h.cl = 1;		/* first sector = 1	 */
711*433d6423SLionel Sambuc   regs.h.dh = 0;		/* head = 0		 */
712*433d6423SLionel Sambuc   regs.h.dl = 0x80 + drivenum;	/* drive = 0		 */
713*433d6423SLionel Sambuc   sregs.es = sregs.ds;		/* buffer address	 */
714*433d6423SLionel Sambuc   regs.x.bx = (int) buffer;
715*433d6423SLionel Sambuc 
716*433d6423SLionel Sambuc   int86x(0x13, &regs, &regs, &sregs);
717*433d6423SLionel Sambuc   if (regs.x.cflag) {
718*433d6423SLionel Sambuc 	printf("Cannot read boot sector\n");
719*433d6423SLionel Sambuc 	exit(1);
720*433d6423SLionel Sambuc   }
721*433d6423SLionel Sambuc }
722*433d6423SLionel Sambuc 
723*433d6423SLionel Sambuc 
724*433d6423SLionel Sambuc void putboot(char *buffer)
725*433d6423SLionel Sambuc {
726*433d6423SLionel Sambuc /* Write boot sector  */
727*433d6423SLionel Sambuc 
728*433d6423SLionel Sambuc   regs.x.ax = 0x301;		/* read 1 sectors	 */
729*433d6423SLionel Sambuc   regs.h.ch = 0;		/* cylinder		 */
730*433d6423SLionel Sambuc   regs.h.cl = 1;		/* first sector = 1	 */
731*433d6423SLionel Sambuc   regs.h.dh = 0;		/* head = 0		 */
732*433d6423SLionel Sambuc   regs.h.dl = 0x80 + drivenum;	/* drive = 0		 */
733*433d6423SLionel Sambuc   sregs.es = sregs.ds;		/* buffer address	 */
734*433d6423SLionel Sambuc   regs.x.bx = (int) buffer;
735*433d6423SLionel Sambuc 
736*433d6423SLionel Sambuc   int86x(0x13, &regs, &regs, &sregs);
737*433d6423SLionel Sambuc   if (regs.x.cflag) {
738*433d6423SLionel Sambuc 	printf("Cannot write boot sector\n");
739*433d6423SLionel Sambuc 	exit(1);
740*433d6423SLionel Sambuc   }
741*433d6423SLionel Sambuc }
742*433d6423SLionel Sambuc 
743*433d6423SLionel Sambuc #endif
744*433d6423SLionel Sambuc 
745*433d6423SLionel Sambuc void adj_base(struct part_entry *pe)
746*433d6423SLionel Sambuc {
747*433d6423SLionel Sambuc /* Adjust base sector of partition, usually to make it even. */
748*433d6423SLionel Sambuc 
749*433d6423SLionel Sambuc   int adj;
750*433d6423SLionel Sambuc 
751*433d6423SLionel Sambuc   if (pe == NULL) return;
752*433d6423SLionel Sambuc   while (1) {
753*433d6423SLionel Sambuc 
754*433d6423SLionel Sambuc 	if (!get_an_int("\tEnter adjustment to base (an integer): ", &adj))
755*433d6423SLionel Sambuc 		return;
756*433d6423SLionel Sambuc 	if (pe->lowsec + adj < 1)
757*433d6423SLionel Sambuc 		printf(
758*433d6423SLionel Sambuc     "\t\tThat would make the base %lu and too small\n", pe->lowsec + adj);
759*433d6423SLionel Sambuc 	else if (pe->size - adj < 1)
760*433d6423SLionel Sambuc 		printf(
761*433d6423SLionel Sambuc     "\t\tThat would make the size %lu and too small\n", pe->size - adj);
762*433d6423SLionel Sambuc 	else
763*433d6423SLionel Sambuc 		break;
764*433d6423SLionel Sambuc   }
765*433d6423SLionel Sambuc   pe->lowsec += adj;
766*433d6423SLionel Sambuc   pe->size -= adj;
767*433d6423SLionel Sambuc   sec_to_hst(pe->lowsec, &pe->start_head, &pe->start_sec, &pe->start_cyl);
768*433d6423SLionel Sambuc   printf("Base of partition adjusted to %ld, size adjusted to %ld\n",
769*433d6423SLionel Sambuc 	 pe->lowsec, pe->size);
770*433d6423SLionel Sambuc }
771*433d6423SLionel Sambuc 
772*433d6423SLionel Sambuc void adj_size(struct part_entry *pe)
773*433d6423SLionel Sambuc {
774*433d6423SLionel Sambuc /* Adjust size of partition by reducing high sector. */
775*433d6423SLionel Sambuc 
776*433d6423SLionel Sambuc   int adj;
777*433d6423SLionel Sambuc 
778*433d6423SLionel Sambuc   if (pe == NULL) return;
779*433d6423SLionel Sambuc   while (1) {
780*433d6423SLionel Sambuc 	if (!get_an_int("\tEnter adjustment to size (an integer): ", &adj))
781*433d6423SLionel Sambuc 		return;
782*433d6423SLionel Sambuc 	if (pe->size + adj >= 1) break;
783*433d6423SLionel Sambuc 	printf("\t\tThat would make the size %lu and too small \n",
784*433d6423SLionel Sambuc 		pe->size + adj);
785*433d6423SLionel Sambuc   }
786*433d6423SLionel Sambuc   pe->size += adj;
787*433d6423SLionel Sambuc   sec_to_hst(pe->lowsec + pe->size - 1,
788*433d6423SLionel Sambuc 	     &pe->last_head, &pe->last_sec, &pe->last_cyl);
789*433d6423SLionel Sambuc   printf("Size of partition adjusted to %ld\n", pe->size);
790*433d6423SLionel Sambuc }
791*433d6423SLionel Sambuc 
792*433d6423SLionel Sambuc struct part_entry *ask_partition()
793*433d6423SLionel Sambuc {
794*433d6423SLionel Sambuc /* Ask for a valid partition number and return its entry. */
795*433d6423SLionel Sambuc 
796*433d6423SLionel Sambuc   int num;
797*433d6423SLionel Sambuc 
798*433d6423SLionel Sambuc   while (1) {
799*433d6423SLionel Sambuc 
800*433d6423SLionel Sambuc 	if (!get_an_int("Enter partition number (1 to 4): ", &num))
801*433d6423SLionel Sambuc 		return(NULL);
802*433d6423SLionel Sambuc 	if (num >= 1 && num <= NR_PARTITIONS) break;
803*433d6423SLionel Sambuc 	printf("\tThat does not look like 1 to 4\n");
804*433d6423SLionel Sambuc   }
805*433d6423SLionel Sambuc   printf("Partition %d\n", num);
806*433d6423SLionel Sambuc   return((struct part_entry *) &secbuf[PART_TABLE_OFF] + (num - 1));
807*433d6423SLionel Sambuc }
808*433d6423SLionel Sambuc 
809*433d6423SLionel Sambuc void footnotes(void)
810*433d6423SLionel Sambuc {
811*433d6423SLionel Sambuc /* Explain the footnotes. */
812*433d6423SLionel Sambuc 
813*433d6423SLionel Sambuc   if (badbases != 0) {
814*433d6423SLionel Sambuc 	printf(
815*433d6423SLionel Sambuc "+ The old Minix wini drivers (before V1.5) discarded odd base sectors.\n");
816*433d6423SLionel Sambuc 	printf(
817*433d6423SLionel Sambuc "  This causes some old (Minix) file systems to be offset by 1 sector.\n");
818*433d6423SLionel Sambuc 	printf(
819*433d6423SLionel Sambuc "  To use these with the new drivers, increase the base by 1 using 'B'.\n");
820*433d6423SLionel Sambuc   }
821*433d6423SLionel Sambuc 
822*433d6423SLionel Sambuc   if (badsizes != 0) {
823*433d6423SLionel Sambuc 	if (badbases != 0) putchar('\n');
824*433d6423SLionel Sambuc 	printf(
825*433d6423SLionel Sambuc "- Minix cannot access the last sector on an odd-sized partition.  This\n");
826*433d6423SLionel Sambuc 	printf(
827*433d6423SLionel Sambuc "  causes trouble for programs like dosread.  This program will by default\n");
828*433d6423SLionel Sambuc 	printf(
829*433d6423SLionel Sambuc "  only create partitions with even sizes.  If possible, the current odd\n");
830*433d6423SLionel Sambuc 	printf(
831*433d6423SLionel Sambuc "  sizes should be decreased by 1 using 'S'.  This is safe for all Minix\n");
832*433d6423SLionel Sambuc 	printf(
833*433d6423SLionel Sambuc "  partitions, and may be safe for other partitions which are about to be\n");
834*433d6423SLionel Sambuc 	printf(
835*433d6423SLionel Sambuc "  reformatted.\n");
836*433d6423SLionel Sambuc   }
837*433d6423SLionel Sambuc 
838*433d6423SLionel Sambuc   if (badorders!= 0 ) {
839*433d6423SLionel Sambuc 	if (badbases != 0 || badsizes != 0) putchar('\n');
840*433d6423SLionel Sambuc 	printf(
841*433d6423SLionel Sambuc "# The partitions are in a funny order. This is normal if they were created\n");
842*433d6423SLionel Sambuc 	printf(
843*433d6423SLionel Sambuc "  by DOS fdisks prior to DOS 3.3.  The Minix wini drivers further confuse\n");
844*433d6423SLionel Sambuc 	printf(
845*433d6423SLionel Sambuc "  the order by sorting the partitions on their base.  Be careful if the\n");
846*433d6423SLionel Sambuc 	printf(
847*433d6423SLionel Sambuc "  device numbers of unchanged partitions have changed.\n");
848*433d6423SLionel Sambuc   }
849*433d6423SLionel Sambuc }
850*433d6423SLionel Sambuc 
851*433d6423SLionel Sambuc int get_an_int(char *prompt, int *intptr)
852*433d6423SLionel Sambuc {
853*433d6423SLionel Sambuc /* Read an int from the start of line of stdin, discard rest of line. */
854*433d6423SLionel Sambuc 
855*433d6423SLionel Sambuc   char buf[80];
856*433d6423SLionel Sambuc 
857*433d6423SLionel Sambuc   while (1) {
858*433d6423SLionel Sambuc 	printf("%s", prompt);
859*433d6423SLionel Sambuc 	if (!mygets(buf, (int) sizeof buf)) return(0);
860*433d6423SLionel Sambuc 	if ((sscanf(buf, "%d", intptr)) == 1) return(1);
861*433d6423SLionel Sambuc 	printf("\t\tThat does not look like an integer\n");
862*433d6423SLionel Sambuc   }
863*433d6423SLionel Sambuc }
864*433d6423SLionel Sambuc 
865*433d6423SLionel Sambuc void list_part_types(void)
866*433d6423SLionel Sambuc {
867*433d6423SLionel Sambuc /* Print all known partition types. */
868*433d6423SLionel Sambuc 
869*433d6423SLionel Sambuc   int column;
870*433d6423SLionel Sambuc   int type;
871*433d6423SLionel Sambuc 
872*433d6423SLionel Sambuc   for (column = 0, type = 0; type < 0x100; ++type)
873*433d6423SLionel Sambuc 	if (strcmp(systype(type), "Unknown") != 0) {
874*433d6423SLionel Sambuc 		printf("0x%02x: %-9s", type, systype(type));
875*433d6423SLionel Sambuc 		column += 16;
876*433d6423SLionel Sambuc 		if (column < 80)
877*433d6423SLionel Sambuc 			putchar(' ');
878*433d6423SLionel Sambuc 		else {
879*433d6423SLionel Sambuc 			putchar('\n');
880*433d6423SLionel Sambuc 			column = 0;
881*433d6423SLionel Sambuc 		}
882*433d6423SLionel Sambuc 	}
883*433d6423SLionel Sambuc   if (column != 0) putchar('\n');
884*433d6423SLionel Sambuc }
885*433d6423SLionel Sambuc 
886*433d6423SLionel Sambuc void mark_npartition(struct part_entry *pe)
887*433d6423SLionel Sambuc {
888*433d6423SLionel Sambuc /* Mark a partition with arbitrary type. */
889*433d6423SLionel Sambuc 
890*433d6423SLionel Sambuc   char buf[80];
891*433d6423SLionel Sambuc   unsigned type;
892*433d6423SLionel Sambuc 
893*433d6423SLionel Sambuc   if (pe == NULL) return;
894*433d6423SLionel Sambuc   printf("\nKnown partition types are:\n\n");
895*433d6423SLionel Sambuc   list_part_types();
896*433d6423SLionel Sambuc   while (1) {
897*433d6423SLionel Sambuc 	printf("\nEnter partition type (in 2-digit hex): ");
898*433d6423SLionel Sambuc 	if (!mygets(buf, (int) sizeof buf)) return;
899*433d6423SLionel Sambuc 	if (sscanf(buf, "%x", &type) != 1)
900*433d6423SLionel Sambuc 		printf("Invalid hex number\n");
901*433d6423SLionel Sambuc   	else if (type >= 0x100)
902*433d6423SLionel Sambuc 		printf("Hex number too large\n");
903*433d6423SLionel Sambuc 	else
904*433d6423SLionel Sambuc 		break;
905*433d6423SLionel Sambuc   }
906*433d6423SLionel Sambuc   pe->sysind = type;
907*433d6423SLionel Sambuc   printf("Partition type changed to 0x%02x (%s)\n", type, systype(type));
908*433d6423SLionel Sambuc }
909*433d6423SLionel Sambuc 
910*433d6423SLionel Sambuc int mygets(char *buf, int length)
911*433d6423SLionel Sambuc {
912*433d6423SLionel Sambuc /* Get a non-empty line of maximum length 'length'. */
913*433d6423SLionel Sambuc 
914*433d6423SLionel Sambuc   while (1) {
915*433d6423SLionel Sambuc 	fflush(stdout);
916*433d6423SLionel Sambuc 	if (fgets(buf, length, stdin) == NULL) {
917*433d6423SLionel Sambuc 		putchar('\n');
918*433d6423SLionel Sambuc 		return(0);
919*433d6423SLionel Sambuc 	}
920*433d6423SLionel Sambuc 	if (strrchr(buf, '\n') != NULL) *strrchr(buf, '\n') = 0;
921*433d6423SLionel Sambuc 	if (*buf != 0) return(1);
922*433d6423SLionel Sambuc 	printf("Use the EOF character to create a null line.\n");
923*433d6423SLionel Sambuc 	printf("Otherwise, please type something before the newline: ");
924*433d6423SLionel Sambuc   }
925*433d6423SLionel Sambuc }
926*433d6423SLionel Sambuc 
927*433d6423SLionel Sambuc char *systype(int type)
928*433d6423SLionel Sambuc {
929*433d6423SLionel Sambuc /* Convert system indicator into system name. */
930*433d6423SLionel Sambuc /* asw 01.03.95: added types based on info in kjb's part.c and output
931*433d6423SLionel Sambuc  * from Linux (1.0.8) fdisk. Note comments here, there are disagreements.
932*433d6423SLionel Sambuc */
933*433d6423SLionel Sambuc   switch(type) {
934*433d6423SLionel Sambuc 	case NO_PART:
935*433d6423SLionel Sambuc 	           return("None");
936*433d6423SLionel Sambuc 	case 1:    return("DOS-12");
937*433d6423SLionel Sambuc 	case 2:    return("XENIX");
938*433d6423SLionel Sambuc 	case 3:    return("XENIX usr");
939*433d6423SLionel Sambuc 	case 4:    return("DOS-16");
940*433d6423SLionel Sambuc 	case 5:    return("DOS-EXT");
941*433d6423SLionel Sambuc 	case 6:    return("DOS-BIG");
942*433d6423SLionel Sambuc 	case 7:    return("HPFS");
943*433d6423SLionel Sambuc 	case 8:    return("AIX");
944*433d6423SLionel Sambuc 	case 9:    return("COHERENT");	/* LINUX says AIX bootable */
945*433d6423SLionel Sambuc 	case 0x0a: return("OS/2");	/* LINUX says OPUS */
946*433d6423SLionel Sambuc 	case 0x10: return("OPUS");
947*433d6423SLionel Sambuc 	case 0x40: return("VENIX286");
948*433d6423SLionel Sambuc 	case 0x51: return("NOVELL?");
949*433d6423SLionel Sambuc 	case 0x52: return("MICROPORT");
950*433d6423SLionel Sambuc 	case 0x63: return("386/IX");	/*LINUX calls this GNU HURD */
951*433d6423SLionel Sambuc 	case 0x64: return("NOVELL286");
952*433d6423SLionel Sambuc 	case 0x65: return("NOVELL386");
953*433d6423SLionel Sambuc 	case 0x75: return("PC/IX");
954*433d6423SLionel Sambuc 	case 0x80: return("MINIX old");
955*433d6423SLionel Sambuc 	case 0x81: return("MINIX");
956*433d6423SLionel Sambuc 	case 0x82: return("LINUXswap");
957*433d6423SLionel Sambuc 	case 0x83: return("LINUX");
958*433d6423SLionel Sambuc 	case 0x93: return("AMOEBA");
959*433d6423SLionel Sambuc 	case 0x94: return("AMOEBAbad");
960*433d6423SLionel Sambuc 	case 0xa5: return("386BSD");
961*433d6423SLionel Sambuc 	case 0xb7: return("BSDI");
962*433d6423SLionel Sambuc 	case 0xb8: return("BSDIswap");
963*433d6423SLionel Sambuc 	case 0xc7: return("Syrinx");
964*433d6423SLionel Sambuc 	case 0xDB: return("CP/M");
965*433d6423SLionel Sambuc 	case 0xe1: return("DOS acc");
966*433d6423SLionel Sambuc 	case 0xe3: return("DOS r/o");
967*433d6423SLionel Sambuc 	case 0xf2: return("DOS 2ary");
968*433d6423SLionel Sambuc 	case 0xFF: return("Badblocks");
969*433d6423SLionel Sambuc 	default:   return("Unknown");
970*433d6423SLionel Sambuc   }
971*433d6423SLionel Sambuc }
972*433d6423SLionel Sambuc 
973*433d6423SLionel Sambuc void toggle_active(struct part_entry *pe)
974*433d6423SLionel Sambuc {
975*433d6423SLionel Sambuc /* Toggle active flag of a partition. */
976*433d6423SLionel Sambuc 
977*433d6423SLionel Sambuc   if (pe == NULL) return;
978*433d6423SLionel Sambuc   pe->bootind = (pe->bootind == ACTIVE_FLAG) ? 0 : ACTIVE_FLAG;
979*433d6423SLionel Sambuc   printf("Partition changed to %sactive\n", pe->bootind ? "" : "in");
980*433d6423SLionel Sambuc }
981*433d6423SLionel Sambuc 
982*433d6423SLionel Sambuc void usage(void)
983*433d6423SLionel Sambuc {
984*433d6423SLionel Sambuc /* Print usage message and exit. */
985*433d6423SLionel Sambuc 
986*433d6423SLionel Sambuc   printf("Usage: fdisk [-hheads] [-ssectors] [device]\n");
987*433d6423SLionel Sambuc   exit(1);
988*433d6423SLionel Sambuc }
989*433d6423SLionel Sambuc 
990