source: trunk/minix/commands/ibm/fdisk.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

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