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