Debian Codegraph @

fdisk.c

raw ◀ ▶
Source reconstructed at patch state 0 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 /*
2 * Changelog:
3 * 25/03/1998 RMK Added option for selecting partitioning scheme
4 */
5
6 #include <assert.h>
7 #include <ctype.h>
8 #include <setjmp.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <linux/hdreg.h>
15 #include <linux/fs.h>
16 #include <sys/fcntl.h>
17 #include <sys/ioctl.h>
18
19 #include "part/part.h"
20 #include "util/error.h"
21
22 #define NR_DEVICES 12
23 #define DEFAULT_DEVICE "/dev/hda"
24 #define ALTERNATE_DEVICE "/dev/sda"
25
26 static const char *part_devs[NR_DEVICES] = {
27 "/dev/hda", "/dev/hdb", "/dev/hdc", "/dev/hdd",
28 "/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd",
29 "/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh"
30 };
31
32 #if 1
33 #include <getopt.h>
34
35 static const char *device;
36 static const char *progname;
37 static const char *dev_part_type = "";
38 static part_t *part;
39
40 typedef enum {
41 mode_partition,
42 mode_version,
43 mode_help,
44 mode_list,
45 mode_size,
46 mode_illegal
47 } prog_mode_t;
48
49 static struct option options[] = {
50 { "help", no_argument, NULL, 'h' },
51 { "list", no_argument, NULL, 'l' },
52 { "size", required_argument, NULL, 's' },
53 { "type", required_argument, NULL, 't' },
54 { "version", no_argument, NULL, 'v' },
55 { NULL, no_argument, NULL, 0 }
56 };
57
58 #define is_extended(x) ((x) == ptyp_dos_extended || (x) == ptyp_linux_extended)
59
60 static prog_mode_t
61 parse_args(int argc, char *argv[])
62 {
63 prog_mode_t mode = mode_partition;
64 int opt;
65
66 do {
67 opt = getopt_long(argc, argv, "s:l", options, NULL);
68
69 switch(opt) {
70 case 'h':
71 mode = mode_help;
72 opt = EOF;
73 break;
74
75 case 'v':
76 mode = mode_version;
77 opt = EOF;
78 break;
79
80 case 'l':
81 mode = mode_list;
82 break;
83
84 case 's':
85 mode = mode_size;
86 device = optarg;
87 break;
88
89 case 't':
90 dev_part_type = optarg;
91 break;
92
93 case EOF:
94 break;
95
96 default:
97 mode = mode_illegal;
98 opt = EOF;
99 break;
100 }
101 } while (opt != EOF);
102
103 return mode;
104 }
105
106 static char buf[16];
107
108 static const char *
109 read_line(const char *msg, ...)
110 {
111 va_list ap;
112 char *p;
113
114 buf[0] = '\0';
115
116 do {
117 va_start(ap, msg);
118 vprintf(msg, ap);
119 va_end(ap);
120 fgets(buf, sizeof(buf) - 1, stdin);
121 } while (!feof(stdin) && buf[0] == '\0');
122
123 p = strchr(buf, '\n');
124 if (p)
125 *p = '\0';
126
127 return buf;
128 }
129
130 static char
131 read_char(const char *msg)
132 {
133 return read_line(msg)[0];
134 }
135
136 static void
137 list_types(u_int parn, ptyp_t next)
138 {
139 ptyp_t types[256], cur;
140 u_int idx = 0, last[4], done = 0, nxt;
141 int i;
142
143 do {
144 cur = next;
145 next = part_nexttype(part, parn, cur, -1);
146 } while (cur != next);
147
148 do {
149 cur = next;
150 types[idx++] = cur;
151 next = part_nexttype(part, parn, cur, +1);
152 } while (cur != next);
153
154 for (i = 3; i >= 0; i--)
155 last[3 - i] = done += (idx + i - done) / (i + 1);
156
157 i = nxt = done = 0;
158
159 do {
160 printf("%c%3x %-15.15s", i ? ' ' : '\n',
161 types[nxt], part_typename(part, types[nxt]));
162 nxt = last[i++] + done;
163 if (i > 3 || nxt >= last[i]) {
164 i = 0;
165 nxt = ++done;
166 }
167 } while (done < last[0]);
168 putchar('\n');
169
170 }
171
172 static ptyp_t
173 read_type(u_int parn, ptyp_t old_type, const char *prompt)
174 {
175 int hex;
176
177 while (1) {
178 const char *line = read_line(prompt);
179 if (tolower(line[0]) == 'l')
180 list_types(parn, old_type);
181 else if (isxdigit(line[0])) {
182 hex = (int)strtoul(line, NULL, 16);
183 break;
184 }
185 }
186 return (ptyp_t)hex;
187 }
188
189
190 static u_int
191 get_part_nr(void)
192 {
193 buf[0] = '\0';
194
195 do {
196 fputs("Which partition? ", stdout);
197 fgets(buf, sizeof(buf) - 1, stdin);
198 } while (!feof(stdin) && buf[0] == '\0');
199
200 return atoi(buf);
201 }
202
203 static u_int display_factor = 1; /* in units/sector */
204 static u_int full_bits = 0; /* 1024 cylinders in sectors */
205 static bool_t unit_flag = 1;
206
207 static u_int
208 rounded(u_int calcul, u_int start)
209 {
210 u_int i;
211
212 if (!full_bits)
213 return calcul;
214 while ((i = calcul + full_bits) <= start)
215 calcul = i;
216 return calcul;
217 }
218
219 static void
220 update_units(part_t *part)
221 {
222 geometry_t geo;
223
224 part_getgeometry(part, &geo);
225
226 full_bits = 1024 * geo.heads * geo.sectors;
227 if (unit_flag && full_bits)
228 display_factor = full_bits >> 10;
229 else
230 display_factor = 1;
231 }
232
233 static void
234 change_units(part_t *part)
235 {
236 unit_flag = !unit_flag;
237 if (part)
238 update_units(part);
239 printf("Changing display/entry units to %ss\n",
240 unit_flag ? "cylinder" : "sector");
241 }
242
243 static u_int
244 sectors_to_units(u_int sectors)
245 {
246 return (sectors + (unit_flag ? display_factor - 1 : 0)) / display_factor;
247 }
248
249 static u_int
250 units_to_sectors(u_int units)
251 {
252 return units * display_factor;
253 }
254
255 static inline u_int
256 calculate(part_t *part, u_int head, u_int sect, u_int cyl)
257 {
258 geometry_t geo;
259
260 part_getgeometry(part, &geo);
261
262 return sect - 1 + geo.sectors * (head + geo.heads * cyl);
263 }
264
265 static void
266 delete_partition(void)
267 {
268 partinfo_t info;
269 u_int part_no, found = 0, partition = get_part_nr();
270
271 for (part_no = 0; part_getpartinfo(part, part_no, &info); part_no += 1)
272 if (info.kern_part_no == partition) {
273 found = 1;
274 break;
275 }
276
277 if (!found) {
278 printf("Bad partition number `%d'\n", partition);
279 return;
280 }
281
282 if (!part_validops(part, part_no, &info) & VALIDOPS_DELETE) {
283 printf("Partition %d cannot be deleted.\n",
284 info.kern_part_no);
285 return;
286 }
287
288 if (!part_delete(part, part_no))
289 printf("Unable to delete partition `%d': %s\n",
290 partition, get_error());
291 else
292 printf("Partition `%d' deleted\n", partition);
293 }
294
295 struct regions {
296 struct {
297 u_int start;
298 u_int end;
299 } region[16];
300 u_int nr;
301 };
302
303 static void
304 add_region(struct regions *region, partinfo_t *info)
305 {
306 u_int i;
307
308 for (i = 0; i < region->nr; i++) {
309 if (region->region[i].start > info->blk_start) {
310 memmove(&region->region[i + 1],
311 &region->region[i],
312 (region->nr - i) * sizeof(region->region[0]));
313 region->region[i].start = info->blk_start;
314 region->region[i].end = info->blk_end + 1;
315 region->nr += 1;
316 goto added;
317 }
318 }
319
320 region->region[region->nr].start = info->blk_start;
321 region->region[region->nr].end = info->blk_end + 1;
322 region->nr += 1;
323
324 added:
325 for (i = 1; i < region->nr;) {
326 if (region->region[i].start == region->region[i - 1].end) {
327 region->region[i - 1].end = region->region[i].end;
328 memmove(&region->region[i],
329 &region->region[i+1],
330 (region->nr - i) * sizeof(region->region[0]));
331 region->nr -= 1;
332 } else
333 i += 1;
334 }
335 }
336
337 static void
338 get_regions(struct regions *region)
339 {
340 partinfo_t info;
341 u_int parn = 0;
342
343 region->nr = 0;
344
345 while (part_getpartinfo(part, parn++, &info)) {
346 if (!info.type)
347 continue;
348
349 add_region(region, &info);
350 }
351 }
352
353 static void
354 new_partition(void)
355 {
356 partinfo_t info;
357 geometry_t geo;
358 struct regions regions;
359 u_int parn, total, first = 1, i;
360 char *units;
361
362 update_units(part);
363
364 part_getgeometry(part, &geo);
365 total = geo.sectors * geo.heads * geo.cylinders;
366
367 if (unit_flag)
368 units = "cylinder";
369 else
370 units = "sector";
371
372 printf("\nUnits are %ss of %d * %d bytes\nDisk space used:",
373 units, display_factor, 512);
374
375 get_regions(&regions);
376
377 for (i = 0; i < regions.nr; i++)
378 printf("%s %d - %d",
379 i ? "," : "",
380 sectors_to_units(regions.region[i].start),
381 sectors_to_units(regions.region[i].end));
382
383 printf("\nTotal disk: %d\n\n", sectors_to_units(total));
384
385 parn = part_allocate(part, &info);
386
387 if (parn == PARN_ERROR) {
388 printf("Unable to allocate a new partition slot: %s\n", get_error());
389 return;
390 }
391
392 printf("Allocating partition %s%d\n\n", device, info.kern_part_no);
393
394 do {
395 const char *l;
396
397 l = read_line(first ? "Start %s : " : "Start %s (%u) : ", units,
398 sectors_to_units(info.blk_start));
399
400 if (isdigit(l[0]))
401 info.blk_start = units_to_sectors(strtoul(l, NULL, 10));
402 else if (first || l[0] == 'q') {
403 printf("\nPartition not created\n");
404 return;
405 }
406
407 l = read_line(first ? "End %s : " : "End %s (%u) : ", units,
408 sectors_to_units(info.blk_end + 1));
409
410 if (l[0] == '+')
411 info.blk_end = info.blk_start +
412 units_to_sectors(strtoul(l + 1, NULL, 10)) - 1;
413 else if (isdigit(l[0]))
414 info.blk_end = units_to_sectors(strtoul(l, NULL, 10)) - 1;
415 else if (first || l[0] == 'q') {
416 printf("\nPartition not created\n");
417 return;
418 }
419
420 info.chs_valid = 0;
421 first = 0;
422
423 if (info.blk_end > info.blk_start && info.blk_end < total) {
424 if (part_create(part, parn, &info)) {
425 printf("\nPartition %d created\n", parn);
426 return;
427 } else
428 printf("\nUnable to create partition: %s\n\n",
429 get_error());
430 } else
431 printf("\nInvalid partition start/end\n\n");
432 } while (1);
433 }
434
435 static void
436 change_type(void)
437 {
438 partinfo_t info;
439 u_int part_no, found = 0, partition = get_part_nr();
440 ptyp_t old_type, new_type;
441
442 for (part_no = 0; part_getpartinfo(part, part_no, &info); part_no += 1)
443 if (info.kern_part_no == partition) {
444 found = 1;
445 break;
446 }
447
448 if (!found) {
449 printf("Bad partition number `%d'\n", partition);
450 return;
451 }
452
453 if (!part_validops(part, part_no, &info) & VALIDOPS_UPDATE) {
454 printf("Partition %d cannot be altered.\n", info.kern_part_no);
455 return;
456 }
457
458 old_type = info.type;
459 if (is_extended(info.type)) {
460 printf("Partition %d is extended. Delete it\n", info.kern_part_no);
461 return;
462 }
463 new_type = read_type(part_no, old_type, "Hex code (type L to list codes):");
464 if (is_extended(new_type)) {
465 printf("You may not change a partition "
466 "to be an extended partition\n");
467 return;
468 }
469 info.type = new_type;
470
471 if (!part_setpartinfo(part, part_no, &info))
472 printf("Unable to change partition %d type: %s\n",
473 partition, get_error());
474 else
475 printf("Partition `%d' type changed from %X (%s) to %X (%s)\n",
476 partition,
477 old_type, part_typename(part, old_type),
478 new_type, part_typename(part, new_type));
479 }
480
481 typedef enum {
482 LIST_NORMAL,
483 LIST_EXTENDED_NORMAL,
484 LIST_EXTENDED_EXTENDED
485 } list_t;
486
487 static inline int
488 is_displayable_partition(ptyp_t type)
489 {
490 return type != ptyp_none && type != ptyp_pc_table;
491 }
492
493 static void
494 list_table(list_t list_type)
495 {
496 partinfo_t info;
497 geometry_t geo;
498 int i, w = 0;
499
500 update_units(part);
501 part_getgeometry(part, &geo);
502
503 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n",
504 device, geo.heads, geo.sectors, geo.cylinders);
505
506 if (list_type == LIST_NORMAL) {
507 printf("Units = %ss of ",
508 unit_flag ? "cylinder" : "sector");
509 if (display_factor > 1)
510 printf("%d * ", display_factor);
511 printf("512 bytes\n\n");
512
513 w = strlen(device) + 1;
514 if (w < 6)
515 w = 6;
516
517 printf("%*s Boot Begin Start End Blocks Id System\n",
518 w, "Device");
519 } else
520 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
521
522 i = 0;
523 while (part_getpartinfo(part, i++, &info)) {
524 if (list_type == LIST_NORMAL && !is_displayable_partition(info.type))
525 continue;
526
527 if (list_type == LIST_NORMAL) {
528 printf("%*s%-2d %c%9d%9d%9d%9d%c %3x %s\n",
529 w - 1, device, info.kern_part_no, ' ',
530 sectors_to_units(calculate(part, info.chs_start.head,
531 info.chs_start.sector,
532 info.chs_start.cylinder)),
533 sectors_to_units(info.blk_start),
534 sectors_to_units(info.blk_end + (info.blk_end & 1 ? 0 : 1)),
535 (info.blk_end - info.blk_start + 1) / 2,
536 (info.blk_end - info.blk_start + 1) & 1 ? '+' : ' ',
537 info.type,
538 part_typename(part, info.type));
539 // check_consistency (part, p);
540 } else {
541 printf("%2d %02x%4d%4d%5d%4d%4d%5d%8d%8d %03x\n",
542 info.kern_part_no, 0, info.chs_start.head,
543 info.chs_start.sector, info.chs_start.cylinder,
544 info.chs_end.head, info.chs_end.sector,
545 info.chs_end.cylinder, info.blk_start,
546 info.blk_end - info.blk_start + 1, info.type);
547 // if (info.type != ptyp_none)
548 // check_consistency (part, p);
549 }
550 }
551 }
552
553 typedef enum {
554 pps_ret_ok,
555 pps_ret_open_error,
556 pps_ret_not_found
557 } pps_ret_t;
558
559 static pps_ret_t
560 print_part_size(const char *device)
561 {
562 pps_ret_t ret = pps_ret_not_found;
563 char dev_nam[16], *p;
564 part_t *part;
565 int partition;
566
567 strncpy(dev_nam, device, 15);
568 dev_nam[15] = '\0';
569
570 for (p = dev_nam; *p && !isdigit(*p); p++);
571
572 partition = atoi(p);
573 *p = '\0';
574
575 part = part_open(dev_nam, dev_part_type);
576 if (part) {
577 int i = 0;
578 partinfo_t info;
579
580 while(part_getpartinfo(part, i, &info)) {
581 if (info.kern_part_no == partition) {
582 printf("%d\n", (info.blk_end - info.blk_start + 1) / 2);
583 ret = pps_ret_ok;
584 break;
585 }
586 i += 1;
587 }
588 part = part_close(part);
589 } else
590 ret = pps_ret_not_found;
591
592 return ret;
593 }
594
595 /* Prototype: int print_part_tables(int nr, const char **parts)
596 * Purpose : Display a list of partition tables
597 * Params : nr - number of partition tables to display
598 * : parts - pointers to partition table names
599 * Returns : 0 if success
600 */
601 static int
602 print_part_tables(int nr, const char **parts)
603 {
604 int i, ret = 1;
605
606 for(i = 0; i < nr; i++) {
607 device = parts[i];
608 part = part_open(device, dev_part_type);
609 if (part) {
610 list_table(LIST_NORMAL);
611 part = part_close(part);
612 ret = 0;
613 }
614 }
615
616 return ret;
617 }
618
619 static void
620 write_tables(void)
621 {
622 const char *l;
623 int i, error = 0;
624
625 printf("\nWriting the partition tables is dangerous and"
626 "\ncan result in loss of data. Please confirm"
627 "\nthat you really want to do this by typing 'YES'"
628 "\nexactly as shown\n\n");
629
630 l = read_line("Confirm: ");
631
632 if (strcmp(l, "YES")) {
633 printf("\nCONFIRMATION NOT GIVEN - not updating partition table\n");
634 return;
635 }
636
637 if (!part_sync(part)) {
638 printf("\nERROR occurred while updating partition tables: %s\n", get_error());
639 return;
640 }
641
642 printf("The partition table has been altered!\n\n");
643 printf("Syncing disks...");
644 fflush(stdout);
645 sync();
646 sleep(2);
647 sync();
648
649 printf("OK\nRe-reading the partition table...");
650 fflush(stdout);
651
652 i = ioctl(part->blkio->fd, BLKRRPART);
653 if (i != 0)
654 error = errno;
655 else {
656 printf(" resyncing...");
657 fflush(stdout);
658 sync();
659 sleep(2);
660 i = ioctl(part->blkio->fd, BLKRRPART);
661 if (i != 0)
662 error = errno;
663 }
664
665 if (i < 0)
666 printf("FAILED!\n");
667
668 printf("Syncing disks...");
669 fflush(stdout);
670 sync();
671 sleep(4);
672 printf("OK\n");
673
674 if (i < 0)
675 printf("\n*** Re-read table failed with error %d: %s ***\n"
676 " Reboot your system to ensure the partition table is\n"
677 " updated.\n", error, strerror(error));
678 exit(0);
679 }
680
681 static void
682 show_main_menu (void)
683 {
684 printf("\nCommand action\n"
685 " d delete a partition\n"
686 // " l list known partition types\n"
687 " m print this menu\n"
688 " n add a new partition\n"
689 " p print the partition table\n"
690 " q quit without saving changes\n"
691 " r reopen partition, specifing type\n"
692 " t change a partition's system id\n"
693 " u change display/entry units\n"
694 // " v verify the partition table\n"
695 " w write table to disk and exit\n"
696 // " x extra functionality (experts only)\n"
697 );
698 }
699
700 /* Prototype: void main_menu(void)
701 * Purpose : display main menu & process user selections
702 */
703 static void
704 main_menu(void)
705 {
706 int quit = 0;
707 part_t *new_part;
708 const char *s;
709 static char *scheme;
710
711 show_main_menu();
712
713 do {
714 putchar('\n');
715 switch (tolower(read_char("Command (m for help): "))) {
716 case 'd':
717 if (part)
718 delete_partition();
719 else
720 goto unavailable;
721 break;
722 case 'e':
723 if (part)
724 list_table(LIST_EXTENDED_NORMAL);
725 else
726 goto unavailable;
727 break;
728 // case 'l':
729 // if (part)
730 // list_types();
731 // else
732 // goto unavailable;
733 // break;
734 case 'm':
735 default:
736 show_main_menu();
737 break;
738 case 'n':
739 if (part)
740 new_partition();
741 else
742 goto unavailable;
743 break;
744 case 'p':
745 if (part)
746 list_table(LIST_NORMAL);
747 else
748 goto unavailable;
749 break;
750 case 'q':
751 quit = 1;
752 break;
753 case 'r':
754 s = read_line("Partitioning scheme: ");
755 new_part = part_open(device, s);
756 if (new_part) {
757 if (scheme)
758 free(scheme);
759 part_close(part);
760 part = new_part;
761 dev_part_type = scheme = strdup(s);
762 fprintf(stderr, "%s successfully reopened using %s scheme\n",
763 device, dev_part_type);
764 } else if (part)
765 fprintf(stderr, "%s: %s: unable to reopen partition as %s,\n"
766 "using %s instead.\n", progname, device, s, dev_part_type);
767 else
768 fprintf(stderr, "%s; %s: unable to reopen partition as %s\n",
769 progname, device, s);
770 break;
771 case 't':
772 if (part)
773 change_type();
774 else
775 goto unavailable;
776 break;
777 case 'u':
778 change_units(part);
779 break;
780 case 'w':
781 if (part) {
782 write_tables();
783 break;
784 }
785 unavailable:
786 fprintf(stderr, "%s: %s: option ignored - please re-open the device\n",
787 progname, device);
788 break;
789 }
790 } while (!quit);
791 }
792
793 /* Prototype: u_int edit_device(const char *dev_nam)
794 * Purpose : Edit partition tables on a device
795 * Params : dev_nam - special device name to edit
796 * Returns : 0 if success
797 */
798 static u_int
799 edit_device(const char *dev_nam)
800 {
801 u_int use_default = dev_nam == NULL;
802
803 if (use_default) {
804 device = DEFAULT_DEVICE;
805 part = part_open(device, dev_part_type);
806 if (!part) {
807 device = ALTERNATE_DEVICE;
808 part = part_open(ALTERNATE_DEVICE, dev_part_type);
809 }
810 } else {
811 device = dev_nam;
812 part = part_open(device, dev_part_type);
813 }
814
815 if (use_default)
816 printf("\nUsing %s as default device\n", device);
817
818 if (!part)
819 printf("Unable to open device %s: %s\n\n"
820 " Please use 'r' to open the partition as a specific type\n\n",
821 device, get_error());
822 else
823 printf("\nDevice %s is partitioned using %s scheme\n",
824 device, part_getscheme(part));
825
826 main_menu();
827
828 return 0;
829 }
830
831 int main(int argc, char *argv[])
832 {
833 prog_mode_t mode;
834 u_int ret = 1;
835
836 progname = argv[0];
837 mode = parse_args(argc, argv);
838
839 switch(mode) {
840 case mode_version:
841 printf("arm-fdisk v"VERSION"\n");
842 break;
843
844 case mode_help:
845 printf("Usage: %s [OPTION] [DEVICE]...\n", progname);
846 printf("Display or edit partition tables on block devices.\n\n");
847 printf(" --type SCHEME specify the partitioning scheme to use on the device\n");
848 printf(" -l, --list DEVICE... list partition information on devices\n");
849 printf(" or all devices\n");
850 printf(" -s, --size DEVICE display size of partition\n");
851 printf(" --help display this help and exit\n");
852 printf(" --version output version information and exit\n\n");
853 printf("With no DEVICE, partition /dev/hda or /dev/sda\n");
854 break;
855
856 case mode_size:
857 switch(print_part_size(device)) {
858 case pps_ret_ok:
859 ret = 0;
860 break;
861
862 case pps_ret_open_error:
863 fprintf(stderr, "Cannot open %s: %s\n", device, get_error());
864 break;
865
866 case pps_ret_not_found:
867 ret = 1;
868 break;
869 }
870 break;
871
872 case mode_list:
873 if (optind < argc)
874 ret = print_part_tables(argc - optind, argv + optind);
875 else
876 ret = print_part_tables(NR_DEVICES, part_devs);
877 break;
878
879 case mode_partition:
880 if (optind < argc - 1) {
881 fprintf(stderr, "Partitioning is only allowed on a single device\n");
882 goto bad;
883 }
884 if (optind == argc)
885 ret = edit_device(NULL);
886 else
887 ret = edit_device(argv[optind]);
888 break;
889
890 bad:
891 case mode_illegal:
892 fprintf(stderr, "Try `%s --help` for more information.\n", progname);
893 ret = 1;
894 break;
895 }
896
897 return ret;
898 }
899
900
901
902
903
904
905
906 #else
907 #include "partitions.h"
908
909 #define LINE_LENGTH 80
910 #define NR_DEF_DEVS 2
911
912 static const char *default_devs[NR_DEF_DEVS] = {
913 DEFAULT_DEVICE, ALTERNATE_DEVICE
914 };
915
916 static const char *usage = "Usage: fdisk [-l [device...]] [-v] [-s /dev/hdxn] [/dev/hdx]\n";
917 static char *prog_name;
918 static bool errorjump_set;
919 static jmp_buf errorjump;
920 unsigned int sector_offset = 1;
921 bool dos_compatible_flag = TRUE;
922
923 /*
924 * Sundry functions...
925 */
926 #ifdef __GNUC__
927 static volatile void fatal (const char *message, ...)
928 __attribute__((noreturn));
929 #endif
930 static volatile void fatal (const char *message, ...)
931 {
932 va_list ap;
933
934 if (errorjump_set)
935 longjmp (errorjump, 1);
936
937 va_start (ap, message);
938 fprintf (stderr, "%s: ", prog_name);
939 vfprintf (stderr, message, ap);
940 va_end (ap);
941 exit (1);
942 }
943
944 static char line_buffer[LINE_LENGTH];
945 static char *line_ptr; /* interactive input */
946
947 char read_line (void)
948 {
949 if (!fgets (line_buffer, LINE_LENGTH, stdin))
950 return 0;
951 line_ptr = line_buffer;
952 while (*line_ptr && !isgraph (*line_ptr))
953 line_ptr ++;
954 return *line_ptr;
955 }
956
957 char read_char (char *mesg)
958 {
959 do
960 fputs (mesg, stdout);
961 while (!read_line ());
962 return *line_ptr;
963 }
964
965 #define hex_val(c) ({ char _c = (c); isdigit(_c) ? _c - '0' : tolower(_c) + 10 - 'a'; })
966
967 int read_hex (struct systypes *sys, int size)
968 {
969 int hex;
970
971 while (1) {
972 read_char ("Hex code (type L to list codes): ");
973 if (tolower (*line_ptr) == 'l')
974 part_list_types (sys, size);
975 else if (isxdigit (*line_ptr)) {
976 hex = 0;
977 do
978 hex = hex << 4 | hex_val (*line_ptr++);
979 while (isxdigit (*line_ptr));
980 return hex;
981 }
982 }
983 }
984
985 enum offset {
986 BASE_IGNORE, BASE_LOWER, BASE_UPPER, BASE_DEFAULT
987 };
988
989 int read_int (Partition_t *part, unsigned int low,
990 unsigned int dflt,
991 unsigned int high, enum offset base, char *mesg)
992 {
993 unsigned int i, use_default = 1;
994 char ms[70];
995
996 switch (base) {
997 case BASE_LOWER:
998 sprintf (ms, "%s ([%d]-%d): ", mesg, low, high);
999 break;
1000 case BASE_UPPER:
1001 sprintf (ms, "%s (%d-[%d]): ", mesg, low, high);
1002 break;
1003 case BASE_DEFAULT:
1004 sprintf (ms, "%s: (%d-[%d]-%d): ", mesg, low, dflt, high);
1005 break;
1006 default:
1007 sprintf (ms, "%s: (%d-%d): ", mesg, low, high);
1008 break;
1009 }
1010
1011 while (1) {
1012 while (!isdigit (read_char (ms)) && (*line_ptr != '-' && *line_ptr != '+'))
1013 continue;
1014 if (*line_ptr == '+' || *line_ptr == '-') {
1015 if (*line_ptr == '+')
1016 line_ptr ++;
1017 i = atoi (line_ptr);
1018 while (isdigit (*line_ptr)) {
1019 line_ptr ++;
1020 use_default = 0;
1021 }
1022 switch (*line_ptr) {
1023 case 'c': case 'C':
1024 if (!unit_flag)
1025 i *= part->geo.heads * part->geo.sectors;
1026 break;
1027 case 'k': case 'K':
1028 i = (i << 1) / display_factor;
1029 break;
1030 case 'm': case 'M':
1031 i = (i << 11) / display_factor;
1032 break;
1033 default:
1034 break;
1035 }
1036 switch (base) {
1037 case BASE_LOWER:
1038 i += low;
1039 break;
1040 case BASE_UPPER:
1041 i += high;
1042 break;
1043 case BASE_DEFAULT:
1044 i += dflt;
1045 default:
1046 break;
1047 }
1048 } else {
1049 i = atoi (line_ptr);
1050 while (isdigit (*line_ptr)) {
1051 line_ptr ++;
1052 use_default = 0;
1053 }
1054 }
1055 if (use_default)
1056 printf ("Using default value %d\n", i = dflt);
1057 if (i >= low && i <= high)
1058 break;
1059 else
1060 printf ("Value out of range.\n");
1061 }
1062 return i;
1063 }
1064
1065 int select_partition (Partition_t *partitions, bool warn, int min, int max)
1066 {
1067 int i;
1068
1069 if (!max) {
1070 min = 1;
1071 for (i = 0; i < partitions->nr_parts; i++)
1072 if (max < partitions->parts[i].dev_index)
1073 max = partitions->parts[i].dev_index;
1074 }
1075
1076 max = read_int (partitions, min, max, max, BASE_IGNORE, "Partition number");
1077
1078 if (warn) {
1079 for (i = 0; i < partitions->nr_parts; i++)
1080 if (max == partitions->parts[i].dev_index)
1081 break;
1082 if (i >= partitions->nr_parts)
1083 printf ("Warning: partition %d has empty type\n", max);
1084 }
1085 return max;
1086 }
1087
1088 void log2chs (Partition_t *part, unsigned long ls, unsigned int *c, unsigned int *h, unsigned int *s)
1089 {
1090 int spc = part->geo.heads * part->geo.sectors;
1091
1092 *c = ls / spc;
1093 ls = ls % spc;
1094 *h = ls / part->geo.sectors;
1095 *s = ls % part->geo.sectors + 1;
1096 }
1097
1098 void check_bounds (Partition_t *part, unsigned long *first, unsigned long *last)
1099 {
1100 int i;
1101
1102 assert (part != NULL);
1103 assert (first != NULL);
1104 assert (last != NULL);
1105
1106 for (i = 0; i < part->nr_parts; i++) {
1107 Part_t *p = &part->parts[i];
1108
1109 first[i] = calculate (part, p->begin.heads, p->begin.sectors, p->begin.cylinders);
1110 last[i] = p->start + p->size - 1;
1111 }
1112 }
1113
1114 static void check_consistency (Partition_t *part, Part_t *p)
1115 {
1116 unsigned int pbc, pbh, pbs;
1117 unsigned int pec, peh, pes;
1118 unsigned int lbc, lbh, lbs;
1119 unsigned int lec, leh, les;
1120
1121 /* physical beginning CHS */
1122 pbc = p->begin.cylinders;
1123 pbh = p->begin.heads;
1124 pbs = p->begin.sectors;
1125 /* physical end CHS */
1126 pec = p->end.cylinders;
1127 peh = p->end.heads;
1128 pes = p->end.sectors;
1129 /* logical beginning CHS */
1130 log2chs (part, p->start, &lbc, &lbh, &lbs);
1131 /* logical end CHS */
1132 log2chs (part, p->start + p->size - 1, &lec, &leh, &les);
1133
1134 if (part->geo.cylinders <= 1024) {
1135 if (pbc != lbc || pbh != lbh || pbs != lbs) {
1136 printf ("Partition %d has different physical/logical "
1137 "beginnings (non-Linux?):\n", p->dev_index);
1138 printf (" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1139 printf (" logi=(%d, %d, %d)\n", lbc, lbh, lbs);
1140 }
1141
1142 if (pec != lec || peh != leh || pes != les) {
1143 printf ("Partition %d has different physical/logical "
1144 "endings:\n", p->dev_index);
1145 printf (" phys=(%d, %d, %d) ", pec, peh, pes);
1146 printf (" logi=(%d, %d, %d)\n", lec, leh, les);
1147 }
1148 }
1149
1150 if (p->must_start_cyl && (pbh != 0/*!pbc*/ || pbs != 1)) {
1151 printf ("Partition %d does not start on a cylinder boundary:\n",
1152 p->dev_index);
1153 printf (" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1154 printf ("should be (%d, %d, 1)\n", pbc, 0/*!pbc*/);
1155 }
1156
1157 if (p->must_end_cyl && (peh != part->geo.heads - 1 || pes != part->geo.sectors)) {
1158 printf ("Partition %d does not end on a cylinder boundary:\n",
1159 p->dev_index);
1160 printf (" phys=(%d, %d, %d) ", pec, peh, pes);
1161 printf ("should be (%d, %d, %d)\n", pec, part->geo.heads - 1,
1162 part->geo.sectors);
1163 }
1164
1165 if (p->must_1mb && p->size % 1048576)
1166 printf ("Partition %d does not contain a whole number of MB\n",
1167 p->dev_index);
1168 }
1169
1170 void check (Partition_t *part, Part_t *p, unsigned long start)
1171 {
1172 /* if (!total)
1173 printf ("Warning: partition %d contains sector 0\n", p->dev_index);
1174 */
1175 if (p->end.heads >= part->geo.heads)
1176 printf ("Partition %d: head %d is greater than maximum %d\n",
1177 p->dev_index, p->end.heads, part->geo.heads - 1);
1178
1179 if (p->end.sectors > part->geo.sectors)
1180 printf ("Partition %d; sector %d is greater than maximum %d\n",
1181 p->dev_index, p->end.sectors, part->geo.sectors);
1182
1183 if (p->end.cylinders >= part->geo.cylinders)
1184 printf ("Partition %d: cylinder %d is greater than maximum %d\n",
1185 p->dev_index, p->end.cylinders, part->geo.cylinders - 1);
1186 }
1187
1188 Part_t *get_part (Partition_t *part, int dev_index)
1189 {
1190 int i;
1191
1192 assert (part != NULL);
1193
1194 for (i = 0; i < part->nr_parts; i++)
1195 if (part->parts[i].dev_index == dev_index)
1196 break;
1197
1198 if (i >= part->nr_parts)
1199 return NULL;
1200
1201 return &part->parts[i];
1202 }
1203
1204 /*
1205 * Other functions...
1206 */
1207
1208 static void move_begin (Partition_t *part, int dev_index)
1209 {
1210 Part_t *p = get_part (part, dev_index);
1211 unsigned long first, new_first;
1212
1213 if (!p)
1214 return;
1215
1216 if (!p->sysid || !p->size || p->sysid == SYSID_EXTENDED) {
1217 printf ("Partition %d has no data area\n", dev_index);
1218 return;
1219 }
1220
1221 first = calculate (part, p->begin.heads, p->begin.sectors, p->begin.cylinders);
1222 new_first = read_int (part, first, first, p->start + p->size - 1,
1223 BASE_LOWER, "New beginning of data");
1224 if (new_first != p->size) {
1225 first = p->size + p->start - new_first;
1226 p->size = first;
1227 p->start = new_first;
1228 p->changed = TRUE;
1229 }
1230 }
1231
1232 static void print_raw (Partition_t *part)
1233 {
1234 printf ("Device: %s\n", part->device);
1235 part_dump (part);
1236 }
1237
1238 static void toggle_active (Partition_t *part, int dev_index)
1239 {
1240 Part_t *p = get_part (part, dev_index);
1241
1242 if (!p)
1243 return;
1244
1245 if (p->boot_flags)
1246 p->boot_flags = 0;
1247 else
1248 p->boot_flags = ACTIVE_FLAG;
1249 p->changed = TRUE;
1250 }
1251
1252 static void toggle_dos (Partition_t *part)
1253 {
1254 if ((dos_compatible_flag = !dos_compatible_flag))
1255 sector_offset = part->geo.sectors;
1256 else
1257 sector_offset = 1;
1258
1259 printf ("DOS compatibility flag is %sset", dos_compatible_flag ? "" : "not ");
1260 }
1261
1262 static int delete_partition (Partition_t *part, int dev_index)
1263 {
1264 int i, j;
1265
1266 assert (part != NULL);
1267
1268 for (j = 0; j < part->nr_parts; j++)
1269 if (part->parts[j].dev_index == dev_index)
1270 break;
1271 if (j >= part->nr_parts)
1272 return 0;
1273
1274 if (part->parts[j].dev_index_owner == part->parts[j].dev_index) {
1275 printf ("Cannot delete partition %d\n", dev_index);
1276 return 1;
1277 }
1278
1279 for (i = 0; i < part->nr_parts; i++)
1280 if (part->parts[i].dev_index_owner == dev_index &&
1281 delete_partition (part, part->parts[i].dev_index))
1282 return 1;
1283
1284 part->parts[j].changed = TRUE;
1285 part->parts[j].boot_flags = 0;
1286 part->parts[j].begin.sectors = 0;
1287 part->parts[j].begin.heads = 0;
1288 part->parts[j].begin.cylinders = 0;
1289 part->parts[j].end.sectors = 0;
1290 part->parts[j].end.heads = 0;
1291 part->parts[j].end.cylinders = 0;
1292 part->parts[j].start = 0;
1293 part->parts[j].size = 0;
1294 part->parts[j].sysid = SYSID_EMPTY;
1295 return 0;
1296 }
1297
1298 static int add_partition (Partition_t *part, int dev_index, int dev_index_owner, PType_t type)
1299 {
1300 Part_t *p = get_part (part, dev_index);
1301 unsigned long *first, *last, start, stop = 0, limit;
1302 unsigned long temp;
1303 char mesg[48];
1304 int read, i;
1305
1306 if (p && p->sysid) {
1307 printf ("Partition %d is already defined. Delete "
1308 "it before adding it.\n", dev_index);
1309 return 0;
1310 }
1311
1312 first = malloc (sizeof (unsigned long) * part->nr_parts);
1313 if (!first)
1314 return -1;
1315 last = malloc (sizeof (unsigned long) * part->nr_parts);
1316 if (!last)
1317 return -1;
1318
1319 check_bounds (part, first, last);
1320 if (unit_flag)
1321 for (i = 0; i < part->nr_parts; i++)
1322 first[i] = (cround (first[i]) - 1) * display_factor;
1323
1324 start = sector_offset;
1325 limit = part->geo.heads * part->geo.sectors * part->geo.cylinders - 1;
1326 if (type == PT_LOGICAL) {
1327 for (i = 0; i < part->nr_parts; i++)
1328 if (part->parts[i].dev_index == dev_index_owner) {
1329 start = part->parts[i].start + 1;
1330 limit = part->parts[i].start + part->parts[i].size - 1;
1331 }
1332 }
1333
1334 sprintf (mesg, "First %s", unit_flag ? "cylinder" : "sector");
1335
1336 read = 0;
1337 do {
1338 temp = start;
1339 for (i = 0; i < part->nr_parts; i++)
1340 if (start >= first[i] && start <= last[i] &&
1341 (part->parts[i].sysid != SYSID_EXTENDED || type != PT_LOGICAL))
1342 start = last[i] + 1;
1343 if (start > limit)
1344 break;
1345 if (start != temp && read) {
1346 printf ("Sector %ld is already allocated\n", temp);
1347 temp = start = stop;
1348 read = 0;
1349 }
1350 if (!read && start == temp) {
1351 unsigned long i;
1352 i = stop = start;
1353 start = read_int (part,
1354 cround (i),
1355 cround (i),
1356 cround (limit),
1357 BASE_IGNORE, mesg);
1358 if (unit_flag) {
1359 start = (start - 1) * display_factor;
1360 if (start < i)
1361 start = i;
1362 }
1363 read = 1;
1364 }
1365 } while (start != temp || !read);
1366
1367 for (i = 0; i < part->nr_parts; i++) {
1368 if (start < first[i] && limit >= first[i] &&
1369 (part->parts[i].sysid != SYSID_EXTENDED || type != PT_LOGICAL))
1370 limit = first[i] - 1;
1371 }
1372 if (start > limit) {
1373 printf ("No free sectors available\n");
1374 return 0;
1375 }
1376 if (cround (start) == cround (limit))
1377 stop = start;
1378 else {
1379 sprintf (mesg, "Last %s or +size or +sizeM or +sizeK",
1380 unit_flag ? "cylinder" : "sector");
1381 stop = read_int (part, cround (start), cround (limit), cround (limit),
1382 BASE_LOWER, mesg);
1383 if (unit_flag) {
1384 stop = stop * display_factor - 1;
1385 if (stop > limit)
1386 stop = limit;
1387 }
1388 }
1389
1390 if (!p) {
1391 p = realloc (part->parts, sizeof (Part_t) * (part->nr_parts + 1));
1392 if (!p)
1393 return -1;
1394
1395 part->parts = p;
1396 p = &part->parts[part->nr_parts++];
1397 }
1398 p->dev_index = dev_index;
1399 p->dev_index_owner = dev_index_owner;
1400 p->boot_flags = 0;
1401 p->sysid = type == PT_EXTENDED ? SYSID_EXTENDED : SYSID_LINUXNAT;
1402 p->start = start;
1403 p->size = stop - start + 1;
1404 p->begin.sectors = start % part->geo.sectors + 1;
1405 start /= part->geo.sectors;
1406 p->begin.heads = start % part->geo.heads;
1407 p->begin.cylinders = start / part->geo.heads;
1408 p->end.sectors = stop % part->geo.sectors + 1;
1409 stop /= part->geo.sectors;
1410 p->end.heads = stop % part->geo.heads;
1411 p->end.cylinders = stop / part->geo.heads;
1412 p->changed = TRUE;
1413
1414 return 0;
1415 }
1416
1417 static void new_partition (Partition_t *part)
1418 {
1419 int partition_types, i;
1420 PType_t types[3];
1421
1422 partition_types = part_getavailabletypes (part);
1423 if (!partition_types)
1424 printf ("All partitions are used. Please delete some partitions "
1425 "first\n");
1426 else {
1427 int lower, upper;
1428 i = 0;
1429 if (partition_types & 1)
1430 types[i++] = PT_PRIMARY;
1431 if (partition_types & 2)
1432 types[i++] = PT_EXTENDED;
1433 if (partition_types & 4)
1434 types[i++] = PT_LOGICAL;
1435 if (i != 1) {
1436 int j;
1437 printf ("Command action\n");
1438 for (j = 0; j < i; j++)
1439 switch (types[j]) {
1440 case PT_PRIMARY:
1441 printf (" p primary\n");
1442 break;
1443 case PT_EXTENDED:
1444 printf (" e extended\n");
1445 break;
1446 case PT_LOGICAL:
1447 printf (" l logical\n");
1448 break;
1449 }
1450 i = 0;
1451 do {
1452 char c;
1453 c = tolower (read_char ("\nPartition type: "));
1454 switch (c) {
1455 case 'p':
1456 if (partition_types & 1)
1457 types[i++] = PT_PRIMARY;
1458 break;
1459 case 'e':
1460 if (partition_types & 2)
1461 types[i++] = PT_EXTENDED;
1462 break;
1463 case 'l':
1464 if (partition_types & 4)
1465 types[i++] = PT_LOGICAL;
1466 }
1467 } while (!i);
1468 }
1469
1470 part_getpartrange (part, types[0], &lower, &upper);
1471 add_partition (part, select_partition (part, FALSE, lower, upper), 2, types[0]);
1472 }
1473 }
1474
1475 static void change_sysid (Partition_t *part, int dev_index)
1476 {
1477 Part_t *p = get_part (part, dev_index);
1478 int sys;
1479
1480 if (!p)
1481 return;
1482
1483 if (p->sysid == SYSID_EXTENDED)
1484 printf ("Partition %d is extended. Delete it\n", dev_index);
1485 else while (1) {
1486 sys = read_hex (NULL, 0);
1487
1488 if (!sys) {
1489 delete_partition (part, dev_index);
1490 break;
1491 } else if (sys == SYSID_EXTENDED) {
1492 printf ("You may not change a partition "
1493 "to be an extended partition\n");
1494 break;
1495 } else if (sys != p->sysid) {
1496 p->sysid = sys;
1497 printf ("Changed system type of partition %d to %x (%s)\n",
1498 dev_index, sys, part_type (p->sysid));
1499 p->changed = TRUE;
1500 part->changed = 1;
1501 break;
1502 }
1503 }
1504 }
1505
1506 static void change_units (Partition_t *part)
1507 {
1508 if ((unit_flag = !unit_flag))
1509 display_factor = 1;
1510 else
1511 display_factor = part->geo.heads * part->geo.sectors;
1512 update_units (part);
1513 printf ("Changing display/entry units to %ss\n",
1514 unit_flag ? "cylinder" : "sector");
1515 }
1516
1517 static int verify (Partition_t *part)
1518 {
1519 unsigned int total = 1;
1520 unsigned long *first, *last;
1521 int i, j;
1522
1523 first = malloc (sizeof (unsigned long) * part->nr_parts);
1524 if (!first)
1525 return -1;
1526 last = malloc (sizeof (unsigned long) * part->nr_parts);
1527 if (!last)
1528 return -1;
1529
1530 check_bounds (part, first, last);
1531
1532 for (i = 0; i < part->nr_parts; i++) {
1533 Part_t *p = &part->parts[i];
1534 if (p->sysid != SYSID_EMPTY) {
1535 if (p->sysid != SYSID_EXTENDED) {
1536 check_consistency (part, p);
1537 if (p->start < first[i])
1538 printf ("Warning: bad start-of-data in partition %d.\n",
1539 p->dev_index);
1540 check (part, p, last[i]);
1541 total += last[i] + 1 - first[i];
1542 if (p->dev_index_owner != p->dev_index) {
1543 for (j = 0; j < part->nr_parts; j++)
1544 if (p->dev_index_owner == part->parts[j].dev_index) {
1545 if (first[i] <= first[j] + 1 ||
1546 last[i] > last[j])
1547 printf ("Logical partition %d not "
1548 "entirely in partition %d\n",
1549 p->dev_index, p->dev_index_owner);
1550 break;
1551 }
1552 if (j >= part->nr_parts)
1553 printf ("Logical partition %d does not have a valid "
1554 "parent\n", p->dev_index);
1555 }
1556 for (j = 0; j < i; j++) {
1557 if (part->parts[j].sysid == SYSID_EXTENDED)
1558 continue;
1559 if ((first[i] >= first[j] && first[i] <= last[j]) ||
1560 (last[i] <= last[j] && last[i] >= first[j])) {
1561 printf ("Warning: partition %d overlaps partition %d.\n",
1562 part->parts[i].dev_index, part->parts[j].dev_index);
1563 total += first[i] >= first[j] ? first[i] : first[j];
1564 total -= last[i] <= last[j] ? last[i] : last[j];
1565 }
1566 }
1567 } else {
1568 total += 1;
1569 }
1570 }
1571 }
1572
1573 if (total > part->geo.heads * part->geo.sectors * part->geo.cylinders)
1574 printf ("Total allocated sectors %d greater than the maximum %d\n",
1575 total, part->geo.heads * part->geo.sectors * part->geo.cylinders);
1576 else if ((total = part->geo.heads * part->geo.sectors * part->geo.cylinders - total))
1577 printf ("%d unallocated sectors\n", total);
1578 return 0;
1579 }
1580
1581 static bool write_table (Partition_t *part)
1582 {
1583 return part_write (part) >= 0;
1584 }
1585
1586 #define REPORT_ERRORS 0
1587 #define IGNORE_ERRORS 1
1588 static void print_part_table (const char *dev, int errorstatus)
1589 {
1590 static Partition_t *partitions;
1591
1592 assert (dev != NULL);
1593
1594 if (!errorjump_set && errorstatus == IGNORE_ERRORS) {
1595 errorjump_set = TRUE;
1596 if (setjmp (errorjump)) {
1597 part_close (partitions);
1598 partitions = NULL;
1599 return;
1600 }
1601 }
1602
1603 partitions = part_open (dev, READ_ONLY);
1604 if (!partitions)
1605 fatal ("Cannot open %s: %s\n", dev, get_error());
1606
1607 list_table (partitions, LIST_NORMAL);
1608 part_close (partitions);
1609 partitions = NULL;
1610 }
1611
1612 static int print_part_size (const char *dev)
1613 {
1614 Partition_t *partitions;
1615 char device[9];
1616 int part_no, i;
1617
1618 assert (dev != NULL);
1619
1620 if (strlen (dev) <= 8 || (!(part_no = atoi (dev + 8))))
1621 fatal (usage);
1622
1623 strncpy (device, dev, 8);
1624 device[8] = '\0';
1625
1626 partitions = part_open (device, READ_ONLY);
1627 if (!partitions)
1628 fatal ("Cannot open %s: %s\n", device, get_error());
1629
1630 for (i = 0; i < partitions->nr_parts; i++)
1631 if (partitions->parts[i].dev_index == part_no) {
1632 printf ("%ld\n", partitions->parts[i].size / 2);
1633 break;
1634 }
1635 i = i < partitions->nr_parts ? 0 : 1;
1636
1637 part_close (partitions);
1638
1639 return i;
1640 }
1641
1642 static void show_extra_menu (void)
1643 {
1644 puts ("\nCommand action\n"
1645 " b move beginning of data in a partition\n"
1646 " c change number of cylinders\n"
1647 " d print the raw data in the partition table\n"
1648 " e list extended partitions\n"
1649 " h change number of heads\n"
1650 " m print this menu\n"
1651 " p print the partition table\n"
1652 " q quit without saving changes\n"
1653 " r return to main menu\n"
1654 " s change number of sectors\n"
1655 " v verify the partition table\n"
1656 " w write table to disk and exit");
1657 }
1658
1659 static bool extra_menu (Partition_t *partitions)
1660 {
1661 bool quit = FALSE;
1662 static bool first = TRUE;
1663
1664 if (first)
1665 show_extra_menu ();
1666 first = FALSE;
1667
1668 do {
1669 putchar ('\n');
1670 switch (tolower (read_char ("Expert command (m for help): "))) {
1671 case 'b':
1672 move_begin (partitions, select_partition (partitions, TRUE, 0, 0));
1673 break;
1674 case 'c':
1675 partitions->geo.cylinders = read_int (partitions, 1,
1676 partitions->geo.cylinders, 65535,
1677 BASE_DEFAULT, "Number of cylinders");
1678 break;
1679 case 'd':
1680 print_raw (partitions);
1681 break;
1682 case 'e':
1683 list_table (partitions, LIST_EXTENDED_EXTENDED);
1684 break;
1685 case 'h':
1686 partitions->geo.heads = read_int (partitions, 1,
1687 partitions->geo.heads, 256, BASE_DEFAULT,
1688 "Number of heads");
1689 update_units (partitions);
1690 break;
1691 case 'm':
1692 default:
1693 show_extra_menu ();
1694 break;
1695 case 'p':
1696 list_table (partitions, LIST_EXTENDED_NORMAL);
1697 break;
1698 case 'q':
1699 quit = TRUE;
1700 break;
1701 case 'r':
1702 return FALSE;
1703 case 's':
1704 partitions->geo.sectors = read_int (partitions, 1,
1705 partitions->geo.sectors, 63, BASE_DEFAULT,
1706 "Number of sectors");
1707 if (dos_compatible_flag) {
1708 sector_offset = partitions->geo.sectors;
1709 printf ("Warning: setting sector offset for DOS "
1710 "compatability\n");
1711 }
1712 update_units (partitions);
1713 break;
1714 case 'v':
1715 verify (partitions);
1716 break;
1717 case 'w':
1718 quit = write_table (partitions);
1719 break;
1720 }
1721 } while (!quit);
1722
1723 return TRUE;
1724 }
1725
1726 static void show_main_menu (void)
1727 {
1728 puts ("\nCommand action\n"
1729 " a toggle a bootable flag\n"
1730 " b edit bsd disklabel\n" /* bf */
1731 " c toggle the dos compatiblity flag\n"
1732 " d delete a partition\n"
1733 " l list known partition types\n"
1734 " m print this menu\n"
1735 " n add a new partition\n"
1736 " p print the partition table\n"
1737 " q quit without saving changes\n"
1738 " t change a partition's system id\n"
1739 " u change display/entry units\n"
1740 " v verify the partition table\n"
1741 " w write table to disk and exit\n"
1742 " x extra functionality (experts only)");
1743 }
1744
1745 static void main_menu (const char *dev, part_open_mode_t open_mode)
1746 {
1747 Partition_t *partitions;
1748 bool quit = FALSE;
1749
1750 assert (dev != NULL);
1751
1752 partitions = part_open (dev, open_mode);
1753 if (!partitions)
1754 fatal ("Cannot open %s: %s\n", dev, get_error());
1755
1756 if (dos_compatible_flag)
1757 sector_offset = partitions->geo.sectors;
1758
1759 update_units (partitions);
1760
1761 show_main_menu ();
1762
1763 do {
1764 putchar ('\n');
1765 switch (tolower (read_char ("Command (m for help): "))) {
1766 case 'a':
1767 toggle_active (partitions, select_partition (partitions, TRUE, 0, 0));
1768 break;
1769 case 'b':
1770 break;
1771 case 'c':
1772 toggle_dos (partitions);
1773 break;
1774 case 'd':
1775 delete_partition (partitions, select_partition (partitions, FALSE, 0, 0));
1776 break;
1777 case 'l':
1778 part_list_types (NULL, 0);
1779 break;
1780 case 'm':
1781 default:
1782 show_main_menu ();
1783 break;
1784 case 'n':
1785 new_partition (partitions);
1786 break;
1787 case 'p':
1788 list_table (partitions, LIST_NORMAL);
1789 break;
1790 case 'q':
1791 quit = TRUE;
1792 break;
1793 case 't':
1794 change_sysid (partitions, select_partition (partitions, TRUE, 0, 0));
1795 break;
1796 case 'u':
1797 change_units (partitions);
1798 break;
1799 case 'v':
1800 verify (partitions);
1801 break;
1802 case 'w':
1803 quit = write_table (partitions);
1804 break;
1805 case 'x':
1806 quit = extra_menu (partitions);
1807 break;
1808 }
1809 } while (!quit);
1810
1811 part_close (partitions);
1812 }
1813
1814 int main (int argc, char *argv[])
1815 {
1816 const char *device = NULL;
1817 part_open_mode_t open_mode = READ_WRITE;
1818 int i;
1819
1820 prog_name = strrchr (argv[0], '/');
1821 if (prog_name)
1822 prog_name ++;
1823 else
1824 prog_name = argv[0];
1825 if (argc > 1 && *argv[1] == '-') {
1826 switch (argv[1][1]) {
1827 case 'v':
1828 printf ("fdisk v" VERSION "\n");
1829 exit (0);
1830 case 'l':
1831 if (argc > 2) {
1832 for (i = 2; i < argc; i++)
1833 print_part_table (argv[i], REPORT_ERRORS);
1834 } else {
1835 for (i = 0; i < NR_DEVICES; i++)
1836 print_part_table (part_devs[i], IGNORE_ERRORS);
1837 }
1838 return 0;
1839 case 's':
1840 if (argc < 3)
1841 fatal (usage);
1842 return print_part_size (argv[2]);
1843 default:
1844 fprintf (stderr, "Option -%c not recognised\n", argv[1][1]);
1845 exit (1);
1846 }
1847 }
1848
1849 if (argc > 3)
1850 fatal (usage);
1851
1852 if (argc > 1) {
1853 int fd;
1854
1855 device = argv[argc - 1];
1856
1857 fd = open (device, O_RDWR);
1858 if (fd < 0) {
1859 open_mode = READ_ONLY;
1860 fd = open (device, O_RDONLY);
1861 if (fd < 0)
1862 fatal ("can't open device '%s': %s\n", device, strerror (errno));
1863 }
1864 close (fd);
1865 } else {
1866 int fd, mode;
1867
1868 for (mode = 0; mode < 2; mode++) {
1869 for (i = 0; i < NR_DEF_DEVS; i++) {
1870 fd = open (default_devs[i], open_mode == READ_WRITE ? O_RDWR : O_RDONLY);
1871 if (fd >= 0)
1872 break;
1873 }
1874 if (fd >= 0) {
1875 device = default_devs[i];
1876 close (fd);
1877 break;
1878 }
1879 open_mode = READ_ONLY;
1880 }
1881
1882 if (!device)
1883 fatal ("can't open any device: %s\n", strerror (errno));
1884
1885 printf ("Using %s as default device\n", device);
1886 }
1887
1888 if (open_mode == READ_ONLY)
1889 printf ("Warning: can't get write permission on device '%s'\n", device);
1890
1891 main_menu (device, open_mode);
1892 exit (0);
1893 }
1894 #endif