1 /* vi: set sw=4 ts=4: */
2 /* fdisk.c -- Partition table manipulator for Linux.
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
5 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10 #include <assert.h> /* assert */
14 /* Looks like someone forgot to add this to config system */
15 #ifndef ENABLE_FEATURE_FDISK_BLKSIZE
16 # define ENABLE_FEATURE_FDISK_BLKSIZE 0
17 # define USE_FEATURE_FDISK_BLKSIZE(a)
20 #define DEFAULT_SECTOR_SIZE 512
21 #define MAX_SECTOR_SIZE 2048
22 #define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
23 #define MAXIMUM_PARTS 60
25 #define ACTIVE_FLAG 0x80
28 #define WIN98_EXTENDED 0x0f
29 #define LINUX_PARTITION 0x81
30 #define LINUX_SWAP 0x82
31 #define LINUX_NATIVE 0x83
32 #define LINUX_EXTENDED 0x85
33 #define LINUX_LVM 0x8e
34 #define LINUX_RAID 0xfd
36 #define IS_EXTENDED(i) \
37 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
39 #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
41 #define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
42 #define scround(x) (((x)+units_per_sector-1)/units_per_sector)
46 unsigned char sectors;
47 unsigned short cylinders;
51 #define HDIO_GETGEO 0x0301 /* get device geometry */
57 static unsigned sector_size = DEFAULT_SECTOR_SIZE;
58 static unsigned user_set_sector_size;
59 static unsigned sector_offset = 1;
62 * Raw disk label. For DOS-type partition tables the MBR,
63 * with descriptions of the primary partitions.
65 #if (MAX_SECTOR_SIZE) > (BUFSIZ+1)
66 static char MBRbuffer[MAX_SECTOR_SIZE];
68 # define MBRbuffer bb_common_bufsiz1
71 #if ENABLE_FEATURE_OSF_LABEL
72 static int possibly_osf_label;
75 static unsigned heads, sectors, cylinders;
76 static void update_units(void);
80 * return partition name - uses static storage unless buf is supplied
83 partname(const char *dev, int pno, int lth)
85 static char buffer[80];
92 bufsiz = sizeof(buffer);
97 if (isdigit(dev[w-1]))
100 /* devfs kludge - note: fdisk partition names are not supposed
101 to equal kernel names, so there is no reason to do this */
102 if (strcmp(dev + w - 4, "disc") == 0) {
110 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
111 lth-wp-2, w, dev, p, pno);
113 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
119 unsigned char boot_ind; /* 0x80 - active */
120 unsigned char head; /* starting head */
121 unsigned char sector; /* starting sector */
122 unsigned char cyl; /* starting cylinder */
123 unsigned char sys_ind; /* What partition type */
124 unsigned char end_head; /* end head */
125 unsigned char end_sector; /* end sector */
126 unsigned char end_cyl; /* end cylinder */
127 unsigned char start4[4]; /* starting sector counting from 0 */
128 unsigned char size4[4]; /* nr of sectors in partition */
132 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
137 label_dos, label_sun, label_sgi, label_aix, label_osf
139 #define LABEL_IS_DOS (label_dos == current_label_type)
141 #if ENABLE_FEATURE_SUN_LABEL
142 #define LABEL_IS_SUN (label_sun == current_label_type)
143 #define STATIC_SUN static
145 #define LABEL_IS_SUN 0
146 #define STATIC_SUN extern
149 #if ENABLE_FEATURE_SGI_LABEL
150 #define LABEL_IS_SGI (label_sgi == current_label_type)
151 #define STATIC_SGI static
153 #define LABEL_IS_SGI 0
154 #define STATIC_SGI extern
157 #if ENABLE_FEATURE_AIX_LABEL
158 #define LABEL_IS_AIX (label_aix == current_label_type)
159 #define STATIC_AIX static
161 #define LABEL_IS_AIX 0
162 #define STATIC_AIX extern
165 #if ENABLE_FEATURE_OSF_LABEL
166 #define LABEL_IS_OSF (label_osf == current_label_type)
167 #define STATIC_OSF static
169 #define LABEL_IS_OSF 0
170 #define STATIC_OSF extern
173 enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
175 static enum label_type current_label_type;
177 static const char *disk_device;
178 static int fd; /* the disk */
179 static int partitions = 4; /* maximum partition + 1 */
180 static int display_in_cyl_units = 1;
181 static unsigned units_per_sector = 1;
182 #if ENABLE_FEATURE_FDISK_WRITABLE
183 static void change_units(void);
184 static void reread_partition_table(int leave);
185 static void delete_partition(int i);
186 static int get_partition(int warn, int max);
187 static void list_types(const struct systypes *sys);
188 static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg);
190 static const char *partition_type(unsigned char type);
191 static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
192 static void get_geometry(void);
193 static int get_boot(enum action what);
198 #define hex_val(c) ({ \
200 isdigit(_c) ? _c - '0' : \
201 tolower(_c) + 10 - 'a'; \
205 #define LINE_LENGTH 800
206 #define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
207 (n) * sizeof(struct partition)))
208 #define sector(s) ((s) & 0x3f)
209 #define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
211 #define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
212 ((h) + heads * cylinder(s,c)))
213 #define set_hsc(h,s,c,sector) { \
214 s = sector % sectors + 1; \
216 h = sector % heads; \
219 s |= (sector >> 2) & 0xc0; \
223 static int32_t get_start_sect(const struct partition *p);
224 static int32_t get_nr_sects(const struct partition *p);
227 * per partition table entry data
229 * The four primary partitions have the same sectorbuffer (MBRbuffer)
230 * and have NULL ext_pointer.
231 * Each logical partition table entry has two pointers, one for the
232 * partition and one link to the next one.
235 struct partition *part_table; /* points into sectorbuffer */
236 struct partition *ext_pointer; /* points into sectorbuffer */
237 #if ENABLE_FEATURE_FDISK_WRITABLE
238 char changed; /* boolean */
240 off_t offset; /* disk sector number */
241 char *sectorbuffer; /* disk sector contents */
242 } ptes[MAXIMUM_PARTS];
245 #if ENABLE_FEATURE_FDISK_WRITABLE
247 set_all_unchanged(void)
251 for (i = 0; i < MAXIMUM_PARTS; i++)
260 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
262 extern inline struct partition *
263 get_part_table(int i)
265 return ptes[i].part_table;
270 { /* n==1: use singular */
272 return display_in_cyl_units ? _("cylinder") : _("sector");
274 return display_in_cyl_units ? _("cylinders") : _("sectors");
278 valid_part_table_flag(const char *mbuffer)
280 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
283 #if ENABLE_FEATURE_FDISK_WRITABLE
285 write_part_table_flag(char *b)
291 static char line_buffer[LINE_LENGTH];
292 static char *line_ptr;
294 /* read line; return 0 or first char */
298 fflush(stdout); /* requested by niles@scyld.com */
299 line_ptr = line_buffer;
300 if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
302 bb_error_msg_and_die("\ngot EOF, exiting");
304 while (*line_ptr && !isgraph(*line_ptr))
310 read_nonempty(const char *mesg)
314 } while (!read_line());
319 read_maybe_empty(const char *mesg)
323 line_ptr = line_buffer;
331 read_hex(const struct systypes *sys)
334 read_nonempty(_("Hex code (type L to list codes): "));
335 if (*line_ptr == 'l' || *line_ptr == 'L')
337 else if (isxdigit(*line_ptr)) {
338 return strtoul(line_ptr, NULL, 16);
342 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
344 #include "fdisk_aix.c"
347 unsigned char info[128]; /* Informative text string */
348 unsigned char spare0[14];
350 unsigned char spare1;
352 unsigned char spare2;
355 unsigned char spare1[246]; /* Boot information etc. */
356 unsigned short rspeed; /* Disk rotational speed */
357 unsigned short pcylcount; /* Physical cylinder count */
358 unsigned short sparecyl; /* extra sects per cylinder */
359 unsigned char spare2[4]; /* More magic... */
360 unsigned short ilfact; /* Interleave factor */
361 unsigned short ncyl; /* Data cylinder count */
362 unsigned short nacyl; /* Alt. cylinder count */
363 unsigned short ntrks; /* Tracks per cylinder */
364 unsigned short nsect; /* Sectors per track */
365 unsigned char spare3[4]; /* Even more magic... */
366 struct sun_partinfo {
367 uint32_t start_cylinder;
368 uint32_t num_sectors;
370 unsigned short magic; /* Magic number */
371 unsigned short csum; /* Label xor'd checksum */
373 #define sunlabel ((sun_partition *)MBRbuffer)
375 #define SUN_WHOLE_DISK 5
376 STATIC_OSF void bsd_select(void);
377 STATIC_OSF void xbsd_print_disklabel(int);
378 #include "fdisk_osf.c"
380 #define SGI_VOLHDR 0x00
381 /* 1 and 2 were used for drive types no longer supported by SGI */
382 #define SGI_SWAP 0x03
383 /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
384 #define SGI_VOLUME 0x06
386 #define SGI_LVOL 0x08
387 #define SGI_RLVOL 0x09
389 #define SGI_XFSLOG 0x0b
392 #define SGI_ENTIRE_DISK SGI_VOLUME
393 #if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
397 return (x << 8) | (x >> 8);
404 ((x & 0xFF00) << 8) |
405 ((x & 0xFF0000) >> 8) |
410 STATIC_SGI const struct systypes sgi_sys_types[];
411 STATIC_SGI unsigned sgi_get_num_sectors(int i);
412 STATIC_SGI int sgi_get_sysid(int i);
413 STATIC_SGI void sgi_delete_partition(int i);
414 STATIC_SGI void sgi_change_sysid(int i, int sys);
415 STATIC_SGI void sgi_list_table(int xtra);
416 STATIC_SGI void sgi_set_xcyl(void);
417 STATIC_SGI int verify_sgi(int verbose);
418 STATIC_SGI void sgi_add_partition(int n, int sys);
419 STATIC_SGI void sgi_set_swappartition(int i);
420 STATIC_SGI const char *sgi_get_bootfile(void);
421 STATIC_SGI void sgi_set_bootfile(const char* aFile);
422 STATIC_SGI void create_sgiinfo(void);
423 STATIC_SGI void sgi_write_table(void);
424 STATIC_SGI void sgi_set_bootpartition(int i);
426 #include "fdisk_sgi.c"
428 STATIC_SUN const struct systypes sun_sys_types[];
429 STATIC_SUN void sun_delete_partition(int i);
430 STATIC_SUN void sun_change_sysid(int i, int sys);
431 STATIC_SUN void sun_list_table(int xtra);
432 STATIC_SUN void sun_set_xcyl(void);
433 STATIC_SUN void add_sun_partition(int n, int sys);
434 STATIC_SUN void sun_set_alt_cyl(void);
435 STATIC_SUN void sun_set_ncyl(int cyl);
436 STATIC_SUN void sun_set_xcyl(void);
437 STATIC_SUN void sun_set_ilfact(void);
438 STATIC_SUN void sun_set_rspeed(void);
439 STATIC_SUN void sun_set_pcylcount(void);
440 STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
441 STATIC_SUN void verify_sun(void);
442 STATIC_SUN void sun_write_table(void);
443 #include "fdisk_sun.c"
445 /* DOS partition types */
447 static const struct systypes i386_sys_types[] = {
450 { "\x04" "FAT16 <32M" },
451 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
452 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
453 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
454 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
455 { "\x0b" "Win95 FAT32" },
456 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
457 { "\x0e" "Win95 FAT16 (LBA)" },
458 { "\x0f" "Win95 Ext'd (LBA)" },
459 { "\x11" "Hidden FAT12" },
460 { "\x12" "Compaq diagnostics" },
461 { "\x14" "Hidden FAT16 <32M" },
462 { "\x16" "Hidden FAT16" },
463 { "\x17" "Hidden HPFS/NTFS" },
464 { "\x1b" "Hidden Win95 FAT32" },
465 { "\x1c" "Hidden Win95 FAT32 (LBA)" },
466 { "\x1e" "Hidden Win95 FAT16 (LBA)" },
467 { "\x3c" "PartitionMagic recovery" },
468 { "\x41" "PPC PReP Boot" },
470 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
471 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
472 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
473 { "\x82" "Linux swap" }, /* also Solaris */
475 { "\x84" "OS/2 hidden C: drive" },
476 { "\x85" "Linux extended" },
477 { "\x86" "NTFS volume set" },
478 { "\x87" "NTFS volume set" },
479 { "\x8e" "Linux LVM" },
480 { "\x9f" "BSD/OS" }, /* BSDI */
481 { "\xa0" "IBM Thinkpad hibernation" },
482 { "\xa5" "FreeBSD" }, /* various BSD flavours */
483 { "\xa6" "OpenBSD" },
484 { "\xa8" "Darwin UFS" },
486 { "\xab" "Darwin boot" },
487 { "\xb7" "BSDI fs" },
488 { "\xb8" "BSDI swap" },
489 { "\xbe" "Solaris boot" },
490 { "\xeb" "BeOS fs" },
491 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
492 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
493 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
494 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
495 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
496 autodetect using persistent
498 #if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
499 { "\x02" "XENIX root" },
500 { "\x03" "XENIX usr" },
501 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
502 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
504 { "\x18" "AST SmartSleep" },
505 { "\x24" "NEC DOS" },
507 { "\x40" "Venix 80286" },
509 { "\x4e" "QNX4.x 2nd part" },
510 { "\x4f" "QNX4.x 3rd part" },
511 { "\x50" "OnTrack DM" },
512 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
513 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
514 { "\x53" "OnTrack DM6 Aux3" },
515 { "\x54" "OnTrackDM6" },
516 { "\x55" "EZ-Drive" },
517 { "\x56" "Golden Bow" },
518 { "\x5c" "Priam Edisk" },
519 { "\x61" "SpeedStor" },
520 { "\x64" "Novell Netware 286" },
521 { "\x65" "Novell Netware 386" },
522 { "\x70" "DiskSecure Multi-Boot" },
525 { "\x94" "Amoeba BBT" }, /* (bad block table) */
526 { "\xa7" "NeXTSTEP" },
527 { "\xbb" "Boot Wizard hidden" },
528 { "\xc1" "DRDOS/sec (FAT-12)" },
529 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
530 { "\xc6" "DRDOS/sec (FAT-16)" },
532 { "\xda" "Non-FS data" },
533 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
534 Concurrent DOS or CTOS */
535 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
536 { "\xdf" "BootIt" }, /* BootIt EMBRM */
537 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
538 extended partition */
539 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
540 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
541 partition < 1024 cyl. */
542 { "\xf1" "SpeedStor" },
543 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
544 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
545 { "\xff" "BBT" }, /* Xenix Bad Block Table */
551 #if ENABLE_FEATURE_FDISK_WRITABLE
552 /* start_sect and nr_sects are stored little endian on all machines */
553 /* moreover, they are not aligned correctly */
555 store4_little_endian(unsigned char *cp, unsigned val)
562 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
565 read4_little_endian(const unsigned char *cp)
567 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
570 #if ENABLE_FEATURE_FDISK_WRITABLE
572 set_start_sect(struct partition *p, unsigned start_sect)
574 store4_little_endian(p->start4, start_sect);
579 get_start_sect(const struct partition *p)
581 return read4_little_endian(p->start4);
584 #if ENABLE_FEATURE_FDISK_WRITABLE
586 set_nr_sects(struct partition *p, int32_t nr_sects)
588 store4_little_endian(p->size4, nr_sects);
593 get_nr_sects(const struct partition *p)
595 return read4_little_endian(p->size4);
598 /* normally O_RDWR, -l option gives O_RDONLY */
599 static int type_open = O_RDWR;
602 static int ext_index; /* the prime extended partition */
603 static int listing; /* no aborts for fdisk -l */
604 static int dos_compatible_flag = ~0;
605 #if ENABLE_FEATURE_FDISK_WRITABLE
606 static int dos_changed;
607 static int nowarn; /* no warnings for fdisk -l/-s */
612 static unsigned user_cylinders, user_heads, user_sectors;
613 static unsigned pt_heads, pt_sectors;
614 static unsigned kern_heads, kern_sectors;
616 static off_t extended_offset; /* offset of link pointers */
618 static unsigned long long total_number_of_sectors;
621 static jmp_buf listingbuf;
623 static void fdisk_fatal(enum failure why)
629 longjmp(listingbuf, 1);
634 message = "\nUnable to open %s";
637 message = "\nUnable to read %s";
640 message = "\nUnable to seek on %s";
642 case unable_to_write:
643 message = "\nUnable to write %s";
646 message = "\nBLKGETSIZE ioctl failed on %s";
649 message = "\nFatal error";
652 bb_error_msg_and_die(message, disk_device);
656 seek_sector(off_t secno)
658 off_t offset = secno * sector_size;
659 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
660 fdisk_fatal(unable_to_seek);
663 #if ENABLE_FEATURE_FDISK_WRITABLE
665 write_sector(off_t secno, char *buf)
668 if (write(fd, buf, sector_size) != sector_size)
669 fdisk_fatal(unable_to_write);
673 /* Allocate a buffer and read a partition table sector */
675 read_pte(struct pte *pe, off_t offset)
678 pe->sectorbuffer = (char *) xmalloc(sector_size);
680 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
681 fdisk_fatal(unable_to_read);
682 #if ENABLE_FEATURE_FDISK_WRITABLE
685 pe->part_table = pe->ext_pointer = NULL;
689 get_partition_start(const struct pte *pe)
691 return pe->offset + get_start_sect(pe->part_table);
694 #if ENABLE_FEATURE_FDISK_WRITABLE
696 * Avoid warning about DOS partitions when no DOS partition was changed.
697 * Here a heuristic "is probably dos partition".
698 * We might also do the opposite and warn in all cases except
699 * for "is probably nondos partition".
702 is_dos_partition(int t)
704 return (t == 1 || t == 4 || t == 6 ||
705 t == 0x0b || t == 0x0c || t == 0x0e ||
706 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
707 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
708 t == 0xc1 || t == 0xc4 || t == 0xc6);
715 puts(_("Command action"));
716 puts(_("\ta\ttoggle a read only flag")); /* sun */
717 puts(_("\tb\tedit bsd disklabel"));
718 puts(_("\tc\ttoggle the mountable flag")); /* sun */
719 puts(_("\td\tdelete a partition"));
720 puts(_("\tl\tlist known partition types"));
721 puts(_("\tm\tprint this menu"));
722 puts(_("\tn\tadd a new partition"));
723 puts(_("\to\tcreate a new empty DOS partition table"));
724 puts(_("\tp\tprint the partition table"));
725 puts(_("\tq\tquit without saving changes"));
726 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
727 puts(_("\tt\tchange a partition's system id"));
728 puts(_("\tu\tchange display/entry units"));
729 puts(_("\tv\tverify the partition table"));
730 puts(_("\tw\twrite table to disk and exit"));
731 #if ENABLE_FEATURE_FDISK_ADVANCED
732 puts(_("\tx\textra functionality (experts only)"));
736 puts(_("Command action"));
737 puts(_("\ta\tselect bootable partition")); /* sgi flavour */
738 puts(_("\tb\tedit bootfile entry")); /* sgi */
739 puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
740 puts(_("\td\tdelete a partition"));
741 puts(_("\tl\tlist known partition types"));
742 puts(_("\tm\tprint this menu"));
743 puts(_("\tn\tadd a new partition"));
744 puts(_("\to\tcreate a new empty DOS partition table"));
745 puts(_("\tp\tprint the partition table"));
746 puts(_("\tq\tquit without saving changes"));
747 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
748 puts(_("\tt\tchange a partition's system id"));
749 puts(_("\tu\tchange display/entry units"));
750 puts(_("\tv\tverify the partition table"));
751 puts(_("\tw\twrite table to disk and exit"));
754 puts(_("Command action"));
755 puts(_("\tm\tprint this menu"));
756 puts(_("\to\tcreate a new empty DOS partition table"));
757 puts(_("\tq\tquit without saving changes"));
758 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
761 puts(_("Command action"));
762 puts(_("\ta\ttoggle a bootable flag"));
763 puts(_("\tb\tedit bsd disklabel"));
764 puts(_("\tc\ttoggle the dos compatibility flag"));
765 puts(_("\td\tdelete a partition"));
766 puts(_("\tl\tlist known partition types"));
767 puts(_("\tm\tprint this menu"));
768 puts(_("\tn\tadd a new partition"));
769 puts(_("\to\tcreate a new empty DOS partition table"));
770 puts(_("\tp\tprint the partition table"));
771 puts(_("\tq\tquit without saving changes"));
772 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
773 puts(_("\tt\tchange a partition's system id"));
774 puts(_("\tu\tchange display/entry units"));
775 puts(_("\tv\tverify the partition table"));
776 puts(_("\tw\twrite table to disk and exit"));
777 #if ENABLE_FEATURE_FDISK_ADVANCED
778 puts(_("\tx\textra functionality (experts only)"));
782 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
785 #if ENABLE_FEATURE_FDISK_ADVANCED
790 puts(_("Command action"));
791 puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
792 puts(_("\tc\tchange number of cylinders"));
793 puts(_("\td\tprint the raw data in the partition table"));
794 puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
795 puts(_("\th\tchange number of heads"));
796 puts(_("\ti\tchange interleave factor")); /*sun*/
797 puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
798 puts(_("\tm\tprint this menu"));
799 puts(_("\tp\tprint the partition table"));
800 puts(_("\tq\tquit without saving changes"));
801 puts(_("\tr\treturn to main menu"));
802 puts(_("\ts\tchange number of sectors/track"));
803 puts(_("\tv\tverify the partition table"));
804 puts(_("\tw\twrite table to disk and exit"));
805 puts(_("\ty\tchange number of physical cylinders")); /*sun*/
808 puts(_("Command action"));
809 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
810 puts(_("\tc\tchange number of cylinders"));
811 puts(_("\td\tprint the raw data in the partition table"));
812 puts(_("\te\tlist extended partitions")); /* !sun */
813 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
814 puts(_("\th\tchange number of heads"));
815 puts(_("\tm\tprint this menu"));
816 puts(_("\tp\tprint the partition table"));
817 puts(_("\tq\tquit without saving changes"));
818 puts(_("\tr\treturn to main menu"));
819 puts(_("\ts\tchange number of sectors/track"));
820 puts(_("\tv\tverify the partition table"));
821 puts(_("\tw\twrite table to disk and exit"));
824 puts(_("Command action"));
825 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
826 puts(_("\tc\tchange number of cylinders"));
827 puts(_("\td\tprint the raw data in the partition table"));
828 puts(_("\te\tlist extended partitions")); /* !sun */
829 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
830 puts(_("\th\tchange number of heads"));
831 puts(_("\tm\tprint this menu"));
832 puts(_("\tp\tprint the partition table"));
833 puts(_("\tq\tquit without saving changes"));
834 puts(_("\tr\treturn to main menu"));
835 puts(_("\ts\tchange number of sectors/track"));
836 puts(_("\tv\tverify the partition table"));
837 puts(_("\tw\twrite table to disk and exit"));
839 puts(_("Command action"));
840 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
841 puts(_("\tc\tchange number of cylinders"));
842 puts(_("\td\tprint the raw data in the partition table"));
843 puts(_("\te\tlist extended partitions")); /* !sun */
844 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
845 #if ENABLE_FEATURE_SGI_LABEL
846 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
848 puts(_("\th\tchange number of heads"));
849 puts(_("\tm\tprint this menu"));
850 puts(_("\tp\tprint the partition table"));
851 puts(_("\tq\tquit without saving changes"));
852 puts(_("\tr\treturn to main menu"));
853 puts(_("\ts\tchange number of sectors/track"));
854 puts(_("\tv\tverify the partition table"));
855 puts(_("\tw\twrite table to disk and exit"));
858 #endif /* ADVANCED mode */
860 #if ENABLE_FEATURE_FDISK_WRITABLE
861 static const struct systypes *
865 LABEL_IS_SUN ? sun_sys_types :
866 LABEL_IS_SGI ? sgi_sys_types :
870 #define get_sys_types() i386_sys_types
871 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
873 static const char *partition_type(unsigned char type)
876 const struct systypes *types = get_sys_types();
878 for (i = 0; types[i].name; i++)
879 if ((unsigned char )types[i].name[0] == type)
880 return types[i].name + 1;
886 #if ENABLE_FEATURE_FDISK_WRITABLE
890 return LABEL_IS_SUN ? sunlabel->infos[i].id :
891 (LABEL_IS_SGI ? sgi_get_sysid(i) :
892 ptes[i].part_table->sys_ind);
895 void list_types(const struct systypes *sys)
897 unsigned last[4], done = 0, next = 0, size;
900 for (i = 0; sys[i].name; i++);
903 for (i = 3; i >= 0; i--)
904 last[3 - i] = done += (size + i - done) / (i + 1);
908 printf("%c%2x %-15.15s", i ? ' ' : '\n',
909 (unsigned char)sys[next].name[0],
910 partition_type((unsigned char)sys[next].name[0]));
911 next = last[i++] + done;
912 if (i > 3 || next >= last[i]) {
916 } while (done < last[0]);
919 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
922 is_cleared_partition(const struct partition *p)
924 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
925 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
926 get_start_sect(p) || get_nr_sects(p));
930 clear_partition(struct partition *p)
934 memset(p, 0, sizeof(struct partition));
937 #if ENABLE_FEATURE_FDISK_WRITABLE
939 set_partition(int i, int doext, off_t start, off_t stop, int sysid)
945 p = ptes[i].ext_pointer;
946 offset = extended_offset;
948 p = ptes[i].part_table;
949 offset = ptes[i].offset;
953 set_start_sect(p, start - offset);
954 set_nr_sects(p, stop - start + 1);
955 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
956 start = heads*sectors*1024 - 1;
957 set_hsc(p->head, p->sector, p->cyl, start);
958 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
959 stop = heads*sectors*1024 - 1;
960 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
966 test_c(const char **m, const char *mesg)
970 printf(_("You must set"));
982 const char *m = NULL;
986 prev = test_c(&m, _("heads"));
988 prev = test_c(&m, _("sectors"));
990 prev = test_c(&m, _("cylinders"));
995 #if ENABLE_FEATURE_FDISK_WRITABLE
996 "You can do this from the extra functions menu.\n"
998 , prev ? _(" and ") : " ", m);
1003 static void update_units(void)
1005 int cyl_units = heads * sectors;
1007 if (display_in_cyl_units && cyl_units)
1008 units_per_sector = cyl_units;
1010 units_per_sector = 1; /* in sectors */
1013 #if ENABLE_FEATURE_FDISK_WRITABLE
1015 warn_cylinders(void)
1017 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1019 "The number of cylinders for this disk is set to %d.\n"
1020 "There is nothing wrong with that, but this is larger than 1024,\n"
1021 "and could in certain setups cause problems with:\n"
1022 "1) software that runs at boot time (e.g., old versions of LILO)\n"
1023 "2) booting and partitioning software from other OSs\n"
1024 " (e.g., DOS FDISK, OS/2 FDISK)\n"),
1030 read_extended(int ext)
1034 struct partition *p, *q;
1038 pex->ext_pointer = pex->part_table;
1040 p = pex->part_table;
1041 if (!get_start_sect(p)) {
1042 printf(_("Bad offset in primary extended partition\n"));
1046 while (IS_EXTENDED(p->sys_ind)) {
1047 struct pte *pe = &ptes[partitions];
1049 if (partitions >= MAXIMUM_PARTS) {
1050 /* This is not a Linux restriction, but
1051 this program uses arrays of size MAXIMUM_PARTS.
1052 Do not try to 'improve' this test. */
1053 struct pte *pre = &ptes[partitions-1];
1054 #if ENABLE_FEATURE_FDISK_WRITABLE
1055 printf(_("Warning: deleting partitions after %d\n"),
1059 clear_partition(pre->ext_pointer);
1063 read_pte(pe, extended_offset + get_start_sect(p));
1065 if (!extended_offset)
1066 extended_offset = get_start_sect(p);
1068 q = p = pt_offset(pe->sectorbuffer, 0);
1069 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
1070 if (IS_EXTENDED(p->sys_ind)) {
1071 if (pe->ext_pointer)
1072 printf(_("Warning: extra link "
1073 "pointer in partition table"
1074 " %d\n"), partitions + 1);
1076 pe->ext_pointer = p;
1077 } else if (p->sys_ind) {
1079 printf(_("Warning: ignoring extra "
1080 "data in partition table"
1081 " %d\n"), partitions + 1);
1087 /* very strange code here... */
1088 if (!pe->part_table) {
1089 if (q != pe->ext_pointer)
1092 pe->part_table = q + 1;
1094 if (!pe->ext_pointer) {
1095 if (q != pe->part_table)
1096 pe->ext_pointer = q;
1098 pe->ext_pointer = q + 1;
1101 p = pe->ext_pointer;
1105 #if ENABLE_FEATURE_FDISK_WRITABLE
1106 /* remove empty links */
1108 for (i = 4; i < partitions; i++) {
1109 struct pte *pe = &ptes[i];
1111 if (!get_nr_sects(pe->part_table) &&
1112 (partitions > 5 || ptes[4].part_table->sys_ind)) {
1113 printf("omitting empty partition (%d)\n", i+1);
1114 delete_partition(i);
1115 goto remove; /* numbering changed */
1121 #if ENABLE_FEATURE_FDISK_WRITABLE
1123 create_doslabel(void)
1128 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1129 "until you decide to write them. After that, of course, the previous\n"
1130 "content won't be recoverable.\n\n"));
1132 current_label_type = label_dos;
1134 #if ENABLE_FEATURE_OSF_LABEL
1135 possibly_osf_label = 0;
1139 for (i = 510-64; i < 510; i++)
1141 write_part_table_flag(MBRbuffer);
1142 extended_offset = 0;
1143 set_all_unchanged();
1145 get_boot(create_empty_dos);
1147 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1150 get_sectorsize(void)
1152 if (!user_set_sector_size) {
1154 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1156 if (sector_size != DEFAULT_SECTOR_SIZE)
1157 printf(_("Note: sector size is %d (not %d)\n"),
1158 sector_size, DEFAULT_SECTOR_SIZE);
1163 get_kernel_geometry(void)
1165 struct hd_geometry geometry;
1167 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1168 kern_heads = geometry.heads;
1169 kern_sectors = geometry.sectors;
1170 /* never use geometry.cylinders - it is truncated */
1175 get_partition_table_geometry(void)
1177 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
1178 struct partition *p;
1179 int i, h, s, hh, ss;
1183 if (!(valid_part_table_flag((char*)bufp)))
1187 for (i = 0; i < 4; i++) {
1188 p = pt_offset(bufp, i);
1189 if (p->sys_ind != 0) {
1190 h = p->end_head + 1;
1191 s = (p->end_sector & 077);
1196 } else if (hh != h || ss != s)
1201 if (!first && !bad) {
1211 unsigned long long bytes; /* really u64 */
1214 sec_fac = sector_size / 512;
1215 #if ENABLE_FEATURE_SUN_LABEL
1216 guess_device_type();
1218 heads = cylinders = sectors = 0;
1219 kern_heads = kern_sectors = 0;
1220 pt_heads = pt_sectors = 0;
1222 get_kernel_geometry();
1223 get_partition_table_geometry();
1225 heads = user_heads ? user_heads :
1226 pt_heads ? pt_heads :
1227 kern_heads ? kern_heads : 255;
1228 sectors = user_sectors ? user_sectors :
1229 pt_sectors ? pt_sectors :
1230 kern_sectors ? kern_sectors : 63;
1231 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1234 unsigned long longsectors;
1236 if (ioctl(fd, BLKGETSIZE, &longsectors))
1238 bytes = ((unsigned long long) longsectors) << 9;
1241 total_number_of_sectors = (bytes >> 9);
1244 if (dos_compatible_flag)
1245 sector_offset = sectors;
1247 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
1249 cylinders = user_cylinders;
1253 * Read MBR. Returns:
1254 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1255 * 0: found or created label
1259 get_boot(enum action what)
1265 for (i = 0; i < 4; i++) {
1266 struct pte *pe = &ptes[i];
1268 pe->part_table = pt_offset(MBRbuffer, i);
1269 pe->ext_pointer = NULL;
1271 pe->sectorbuffer = MBRbuffer;
1272 #if ENABLE_FEATURE_FDISK_WRITABLE
1273 pe->changed = (what == create_empty_dos);
1277 #if ENABLE_FEATURE_SUN_LABEL
1278 if (what == create_empty_sun && check_sun_label())
1282 memset(MBRbuffer, 0, 512);
1284 #if ENABLE_FEATURE_FDISK_WRITABLE
1285 if (what == create_empty_dos)
1286 goto got_dos_table; /* skip reading disk */
1288 if ((fd = open(disk_device, type_open)) < 0) {
1289 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1290 if (what == try_only)
1292 fdisk_fatal(unable_to_open);
1294 printf(_("You will not be able to write "
1295 "the partition table.\n"));
1298 if (512 != read(fd, MBRbuffer, 512)) {
1299 if (what == try_only)
1301 fdisk_fatal(unable_to_read);
1304 if ((fd = open(disk_device, O_RDONLY)) < 0)
1306 if (512 != read(fd, MBRbuffer, 512))
1314 #if ENABLE_FEATURE_SUN_LABEL
1315 if (check_sun_label())
1319 #if ENABLE_FEATURE_SGI_LABEL
1320 if (check_sgi_label())
1324 #if ENABLE_FEATURE_AIX_LABEL
1325 if (check_aix_label())
1329 #if ENABLE_FEATURE_OSF_LABEL
1330 if (check_osf_label()) {
1331 possibly_osf_label = 1;
1332 if (!valid_part_table_flag(MBRbuffer)) {
1333 current_label_type = label_osf;
1336 printf(_("This disk has both DOS and BSD magic.\n"
1337 "Give the 'b' command to go to BSD mode.\n"));
1341 #if ENABLE_FEATURE_FDISK_WRITABLE
1345 if (!valid_part_table_flag(MBRbuffer)) {
1346 #ifndef CONFIG_FEATURE_FDISK_WRITABLE
1351 printf(_("Device contains neither a valid DOS "
1352 "partition table, nor Sun, SGI or OSF "
1355 #if ENABLE_FEATURE_SUN_LABEL
1364 case create_empty_dos:
1365 #if ENABLE_FEATURE_SUN_LABEL
1366 case create_empty_sun:
1370 bb_error_msg_and_die(_("internal error"));
1372 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1375 #if ENABLE_FEATURE_FDISK_WRITABLE
1380 for (i = 0; i < 4; i++) {
1381 struct pte *pe = &ptes[i];
1383 if (IS_EXTENDED(pe->part_table->sys_ind)) {
1384 if (partitions != 4)
1385 printf(_("Ignoring extra extended "
1386 "partition %d\n"), i + 1);
1392 for (i = 3; i < partitions; i++) {
1393 struct pte *pe = &ptes[i];
1395 if (!valid_part_table_flag(pe->sectorbuffer)) {
1396 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
1397 "table %d will be corrected by w(rite)\n"),
1398 pe->sectorbuffer[510],
1399 pe->sectorbuffer[511],
1401 #if ENABLE_FEATURE_FDISK_WRITABLE
1410 #if ENABLE_FEATURE_FDISK_WRITABLE
1412 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1413 * If the user hits Enter, DFLT is returned.
1414 * Answers like +10 are interpreted as offsets from BASE.
1416 * There is no default if DFLT is not between LOW and HIGH.
1419 read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
1423 const char *fmt = "%s (%u-%u, default %u): ";
1425 if (dflt < low || dflt > high) {
1426 fmt = "%s (%u-%u): ";
1431 int use_default = default_ok;
1433 /* ask question and read answer */
1435 printf(fmt, mesg, low, high, dflt);
1436 read_maybe_empty("");
1437 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1438 && *line_ptr != '-' && *line_ptr != '+');
1440 if (*line_ptr == '+' || *line_ptr == '-') {
1441 int minus = (*line_ptr == '-');
1444 i = atoi(line_ptr + 1);
1446 while (isdigit(*++line_ptr))
1449 switch (*line_ptr) {
1452 if (!display_in_cyl_units)
1453 i *= heads * sectors;
1467 absolute = 1000000000;
1473 unsigned long long bytes;
1476 bytes = (unsigned long long) i * absolute;
1477 unit = sector_size * units_per_sector;
1478 bytes += unit/2; /* round */
1487 while (isdigit(*line_ptr)) {
1493 printf(_("Using default value %u\n"), i = dflt);
1494 if (i >= low && i <= high)
1497 printf(_("Value is out of range\n"));
1503 get_partition(int warn, int max)
1508 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1512 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1513 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1514 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1516 printf(_("Warning: partition %d has empty type\n"), i+1);
1523 get_existing_partition(int warn, int max)
1528 for (i = 0; i < max; i++) {
1529 struct pte *pe = &ptes[i];
1530 struct partition *p = pe->part_table;
1532 if (p && !is_cleared_partition(p)) {
1539 printf(_("Selected partition %d\n"), pno+1);
1542 printf(_("No partition is defined yet!\n"));
1546 return get_partition(warn, max);
1550 get_nonexisting_partition(int warn, int max)
1555 for (i = 0; i < max; i++) {
1556 struct pte *pe = &ptes[i];
1557 struct partition *p = pe->part_table;
1559 if (p && is_cleared_partition(p)) {
1566 printf(_("Selected partition %d\n"), pno+1);
1569 printf(_("All primary partitions have been defined already!\n"));
1573 return get_partition(warn, max);
1580 display_in_cyl_units = !display_in_cyl_units;
1582 printf(_("Changing display/entry units to %s\n"),
1587 toggle_active(int i)
1589 struct pte *pe = &ptes[i];
1590 struct partition *p = pe->part_table;
1592 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
1593 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
1594 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1599 toggle_dos_compatibility_flag(void)
1601 dos_compatible_flag = ~dos_compatible_flag;
1602 if (dos_compatible_flag) {
1603 sector_offset = sectors;
1604 printf(_("DOS Compatibility flag is set\n"));
1608 printf(_("DOS Compatibility flag is not set\n"));
1613 delete_partition(int i)
1615 struct pte *pe = &ptes[i];
1616 struct partition *p = pe->part_table;
1617 struct partition *q = pe->ext_pointer;
1619 /* Note that for the fifth partition (i == 4) we don't actually
1620 * decrement partitions.
1623 if (warn_geometry())
1624 return; /* C/H/S not set */
1628 sun_delete_partition(i);
1632 sgi_delete_partition(i);
1637 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
1639 ptes[ext_index].ext_pointer = NULL;
1640 extended_offset = 0;
1646 if (!q->sys_ind && i > 4) {
1647 /* the last one in the chain - just delete */
1650 clear_partition(ptes[i].ext_pointer);
1651 ptes[i].changed = 1;
1653 /* not the last one - further ones will be moved down */
1655 /* delete this link in the chain */
1656 p = ptes[i-1].ext_pointer;
1658 set_start_sect(p, get_start_sect(q));
1659 set_nr_sects(p, get_nr_sects(q));
1660 ptes[i-1].changed = 1;
1661 } else if (partitions > 5) { /* 5 will be moved to 4 */
1662 /* the first logical in a longer chain */
1665 if (pe->part_table) /* prevent SEGFAULT */
1666 set_start_sect(pe->part_table,
1667 get_partition_start(pe) -
1669 pe->offset = extended_offset;
1673 if (partitions > 5) {
1675 while (i < partitions) {
1676 ptes[i] = ptes[i+1];
1680 /* the only logical: clear only */
1681 clear_partition(ptes[i].part_table);
1688 int i, sys, origsys;
1689 struct partition *p;
1691 /* If sgi_label then don't use get_existing_partition,
1692 let the user select a partition, since get_existing_partition()
1693 only works for Linux like partition tables. */
1694 if (!LABEL_IS_SGI) {
1695 i = get_existing_partition(0, partitions);
1697 i = get_partition(0, partitions);
1701 p = ptes[i].part_table;
1702 origsys = sys = get_sysid(i);
1704 /* if changing types T to 0 is allowed, then
1705 the reverse change must be allowed, too */
1706 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
1707 printf(_("Partition %d does not exist yet!\n"), i + 1);
1711 sys = read_hex (get_sys_types());
1713 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
1714 printf(_("Type 0 means free space to many systems\n"
1715 "(but not to Linux). Having partitions of\n"
1716 "type 0 is probably unwise. You can delete\n"
1717 "a partition using the 'd' command.\n"));
1721 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
1722 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
1723 printf(_("You cannot change a partition into"
1724 " an extended one or vice versa\n"
1725 "Delete it first.\n"));
1731 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
1732 printf(_("Consider leaving partition 3 "
1733 "as Whole disk (5),\n"
1734 "as SunOS/Solaris expects it and "
1735 "even Linux likes it.\n\n"));
1738 (i == 10 && sys != SGI_ENTIRE_DISK) ||
1739 (i == 8 && sys != 0)
1742 printf(_("Consider leaving partition 9 "
1743 "as volume header (0),\nand "
1744 "partition 11 as entire volume (6)"
1745 "as IRIX expects it.\n\n"));
1750 sun_change_sysid(i, sys);
1751 } else if (LABEL_IS_SGI) {
1752 sgi_change_sysid(i, sys);
1756 printf(_("Changed system type of partition %d "
1757 "to %x (%s)\n"), i + 1, sys,
1758 partition_type(sys));
1759 ptes[i].changed = 1;
1760 if (is_dos_partition(origsys) ||
1761 is_dos_partition(sys))
1767 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1770 /* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1771 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1772 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1773 * Lubkin Oct. 1991). */
1776 long2chs(ulong ls, unsigned *c, unsigned *h, unsigned *s)
1778 int spc = heads * sectors;
1783 *s = ls % sectors + 1; /* sectors count from 1 */
1787 check_consistency(const struct partition *p, int partition)
1789 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1790 unsigned pec, peh, pes; /* physical ending c, h, s */
1791 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1792 unsigned lec, leh, les; /* logical ending c, h, s */
1794 if (!heads || !sectors || (partition >= 4))
1795 return; /* do not check extended partitions */
1797 /* physical beginning c, h, s */
1798 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1800 pbs = p->sector & 0x3f;
1802 /* physical ending c, h, s */
1803 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1805 pes = p->end_sector & 0x3f;
1807 /* compute logical beginning (c, h, s) */
1808 long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1810 /* compute logical ending (c, h, s) */
1811 long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1813 /* Same physical / logical beginning? */
1814 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1815 printf(_("Partition %d has different physical/logical "
1816 "beginnings (non-Linux?):\n"), partition + 1);
1817 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1818 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1821 /* Same physical / logical ending? */
1822 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1823 printf(_("Partition %d has different physical/logical "
1824 "endings:\n"), partition + 1);
1825 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1826 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1829 /* Ending on cylinder boundary? */
1830 if (peh != (heads - 1) || pes != sectors) {
1831 printf(_("Partition %i does not end on cylinder boundary.\n"),
1837 list_disk_geometry(void)
1839 long long bytes = (total_number_of_sectors << 9);
1840 long megabytes = bytes/1000000;
1842 if (megabytes < 10000)
1843 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
1844 disk_device, megabytes, bytes);
1846 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
1847 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1848 printf(_("%d heads, %d sectors/track, %d cylinders"),
1849 heads, sectors, cylinders);
1850 if (units_per_sector == 1)
1851 printf(_(", total %llu sectors"),
1852 total_number_of_sectors / (sector_size/512));
1853 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
1855 units_per_sector, sector_size, units_per_sector * sector_size);
1859 * Check whether partition entries are ordered by their starting positions.
1860 * Return 0 if OK. Return i if partition i should have been earlier.
1861 * Two separate checks: primary and logical partitions.
1864 wrong_p_order(int *prev)
1866 const struct pte *pe;
1867 const struct partition *p;
1868 off_t last_p_start_pos = 0, p_start_pos;
1871 for (i = 0 ; i < partitions; i++) {
1874 last_p_start_pos = 0;
1877 if ((p = pe->part_table)->sys_ind) {
1878 p_start_pos = get_partition_start(pe);
1880 if (last_p_start_pos > p_start_pos) {
1886 last_p_start_pos = p_start_pos;
1893 #if ENABLE_FEATURE_FDISK_ADVANCED
1895 * Fix the chain of logicals.
1896 * extended_offset is unchanged, the set of sectors used is unchanged
1897 * The chain is sorted so that sectors increase, and so that
1898 * starting sectors increase.
1900 * After this it may still be that cfdisk doesnt like the table.
1901 * (This is because cfdisk considers expanded parts, from link to
1902 * end of partition, and these may still overlap.)
1904 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1908 fix_chain_of_logicals(void)
1910 int j, oj, ojj, sj, sjj;
1911 struct partition *pj,*pjj,tmp;
1913 /* Stage 1: sort sectors but leave sector of part 4 */
1914 /* (Its sector is the global extended_offset.) */
1916 for (j = 5; j < partitions-1; j++) {
1917 oj = ptes[j].offset;
1918 ojj = ptes[j+1].offset;
1920 ptes[j].offset = ojj;
1921 ptes[j+1].offset = oj;
1922 pj = ptes[j].part_table;
1923 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1924 pjj = ptes[j+1].part_table;
1925 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1926 set_start_sect(ptes[j-1].ext_pointer,
1927 ojj-extended_offset);
1928 set_start_sect(ptes[j].ext_pointer,
1929 oj-extended_offset);
1934 /* Stage 2: sort starting sectors */
1936 for (j = 4; j < partitions-1; j++) {
1937 pj = ptes[j].part_table;
1938 pjj = ptes[j+1].part_table;
1939 sj = get_start_sect(pj);
1940 sjj = get_start_sect(pjj);
1941 oj = ptes[j].offset;
1942 ojj = ptes[j+1].offset;
1943 if (oj+sj > ojj+sjj) {
1947 set_start_sect(pj, ojj+sjj-oj);
1948 set_start_sect(pjj, oj+sj-ojj);
1953 /* Probably something was changed */
1954 for (j = 4; j < partitions; j++)
1955 ptes[j].changed = 1;
1960 fix_partition_table_order(void)
1962 struct pte *pei, *pek;
1965 if (!wrong_p_order(NULL)) {
1966 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1970 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1971 /* partition i should have come earlier, move it */
1972 /* We have to move data in the MBR */
1973 struct partition *pi, *pk, *pe, pbuf;
1977 pe = pei->ext_pointer;
1978 pei->ext_pointer = pek->ext_pointer;
1979 pek->ext_pointer = pe;
1981 pi = pei->part_table;
1982 pk = pek->part_table;
1984 memmove(&pbuf, pi, sizeof(struct partition));
1985 memmove(pi, pk, sizeof(struct partition));
1986 memmove(pk, &pbuf, sizeof(struct partition));
1988 pei->changed = pek->changed = 1;
1992 fix_chain_of_logicals();
2000 list_table(int xtra)
2002 const struct partition *p;
2006 sun_list_table(xtra);
2010 sgi_list_table(xtra);
2014 list_disk_geometry();
2017 xbsd_print_disklabel(xtra);
2021 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2022 but if the device name ends in a digit, say /dev/foo1,
2023 then the partition is called /dev/foo1p3. */
2024 w = strlen(disk_device);
2025 if (w && isdigit(disk_device[w-1]))
2030 // 1 12345678901 12345678901 12345678901 12
2031 printf(_("%*s Boot Start End Blocks Id System\n"),
2034 for (i = 0; i < partitions; i++) {
2035 const struct pte *pe = &ptes[i];
2041 if (!p || is_cleared_partition(p))
2044 psects = get_nr_sects(p);
2048 if (sector_size < 1024) {
2049 pblocks /= (1024 / sector_size);
2050 podd = psects % (1024 / sector_size);
2052 if (sector_size > 1024)
2053 pblocks *= (sector_size / 1024);
2055 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2056 partname(disk_device, i+1, w+2),
2057 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2059 (unsigned long long) cround(get_partition_start(pe)), /* start */
2060 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2061 - (psects ? 1 : 0)),
2062 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2063 p->sys_ind, /* type id */
2064 partition_type(p->sys_ind)); /* type name */
2066 check_consistency(p, i);
2069 /* Is partition table in disk order? It need not be, but... */
2070 /* partition table entries are not checked for correct order if this
2071 is a sgi, sun or aix labeled disk... */
2072 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
2074 printf(_("\nPartition table entries are not in disk order\n"));
2078 #if ENABLE_FEATURE_FDISK_ADVANCED
2080 x_list_table(int extend)
2082 const struct pte *pe;
2083 const struct partition *p;
2086 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2087 disk_device, heads, sectors, cylinders);
2088 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
2089 for (i = 0 ; i < partitions; i++) {
2091 p = (extend ? pe->ext_pointer : pe->part_table);
2093 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
2094 i + 1, p->boot_ind, p->head,
2096 cylinder(p->sector, p->cyl), p->end_head,
2097 sector(p->end_sector),
2098 cylinder(p->end_sector, p->end_cyl),
2099 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2101 check_consistency(p, i);
2107 #if ENABLE_FEATURE_FDISK_WRITABLE
2109 fill_bounds(off_t *first, off_t *last)
2112 const struct pte *pe = &ptes[0];
2113 const struct partition *p;
2115 for (i = 0; i < partitions; pe++,i++) {
2117 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
2118 first[i] = 0xffffffff;
2121 first[i] = get_partition_start(pe);
2122 last[i] = first[i] + get_nr_sects(p) - 1;
2128 check(int n, unsigned h, unsigned s, unsigned c, off_t start)
2130 off_t total, real_s, real_c;
2132 real_s = sector(s) - 1;
2133 real_c = cylinder(s, c);
2134 total = (real_c * sectors + real_s) * heads + h;
2136 printf(_("Warning: partition %d contains sector 0\n"), n);
2138 printf(_("Partition %d: head %d greater than maximum %d\n"),
2140 if (real_s >= sectors)
2141 printf(_("Partition %d: sector %d greater than "
2142 "maximum %d\n"), n, s, sectors);
2143 if (real_c >= cylinders)
2144 printf(_("Partitions %d: cylinder %llu greater than "
2145 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
2146 if (cylinders <= 1024 && start != total)
2147 printf(_("Partition %d: previous sectors %llu disagrees with "
2148 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
2156 off_t first[partitions], last[partitions];
2157 struct partition *p;
2159 if (warn_geometry())
2171 fill_bounds(first, last);
2172 for (i = 0; i < partitions; i++) {
2173 struct pte *pe = &ptes[i];
2176 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
2177 check_consistency(p, i);
2178 if (get_partition_start(pe) < first[i])
2179 printf(_("Warning: bad start-of-data in "
2180 "partition %d\n"), i + 1);
2181 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2183 total += last[i] + 1 - first[i];
2184 for (j = 0; j < i; j++)
2185 if ((first[i] >= first[j] && first[i] <= last[j])
2186 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2187 printf(_("Warning: partition %d overlaps "
2188 "partition %d.\n"), j + 1, i + 1);
2189 total += first[i] >= first[j] ?
2190 first[i] : first[j];
2191 total -= last[i] <= last[j] ?
2197 if (extended_offset) {
2198 struct pte *pex = &ptes[ext_index];
2199 off_t e_last = get_start_sect(pex->part_table) +
2200 get_nr_sects(pex->part_table) - 1;
2202 for (i = 4; i < partitions; i++) {
2204 p = ptes[i].part_table;
2206 if (i != 4 || i + 1 < partitions)
2207 printf(_("Warning: partition %d "
2208 "is empty\n"), i + 1);
2210 else if (first[i] < extended_offset ||
2212 printf(_("Logical partition %d not entirely in "
2213 "partition %d\n"), i + 1, ext_index + 1);
2217 if (total > heads * sectors * cylinders)
2218 printf(_("Total allocated sectors %d greater than the maximum "
2219 "%d\n"), total, heads * sectors * cylinders);
2220 else if ((total = heads * sectors * cylinders - total) != 0)
2221 printf(_("%d unallocated sectors\n"), total);
2225 add_partition(int n, int sys)
2227 char mesg[256]; /* 48 does not suffice in Japanese */
2228 int i, num_read = 0;
2229 struct partition *p = ptes[n].part_table;
2230 struct partition *q = ptes[ext_index].part_table;
2232 off_t start, stop = 0, limit, temp,
2233 first[partitions], last[partitions];
2235 if (p && p->sys_ind) {
2236 printf(_("Partition %d is already defined. Delete "
2237 "it before re-adding it.\n"), n + 1);
2240 fill_bounds(first, last);
2242 start = sector_offset;
2243 if (display_in_cyl_units || !total_number_of_sectors)
2244 llimit = heads * sectors * cylinders - 1;
2246 llimit = total_number_of_sectors - 1;
2248 if (limit != llimit)
2250 if (extended_offset) {
2251 first[ext_index] = extended_offset;
2252 last[ext_index] = get_start_sect(q) +
2253 get_nr_sects(q) - 1;
2256 start = extended_offset + sector_offset;
2257 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2259 if (display_in_cyl_units)
2260 for (i = 0; i < partitions; i++)
2261 first[i] = (cround(first[i]) - 1) * units_per_sector;
2263 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2266 for (i = 0; i < partitions; i++) {
2269 if (start == ptes[i].offset)
2270 start += sector_offset;
2271 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
2272 if (start >= first[i] && start <= lastplusoff)
2273 start = lastplusoff + 1;
2277 if (start >= temp+units_per_sector && num_read) {
2278 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
2282 if (!num_read && start == temp) {
2285 saved_start = start;
2286 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2288 if (display_in_cyl_units) {
2289 start = (start - 1) * units_per_sector;
2290 if (start < saved_start) start = saved_start;
2294 } while (start != temp || !num_read);
2295 if (n > 4) { /* NOT for fifth partition */
2296 struct pte *pe = &ptes[n];
2298 pe->offset = start - sector_offset;
2299 if (pe->offset == extended_offset) { /* must be corrected */
2301 if (sector_offset == 1)
2306 for (i = 0; i < partitions; i++) {
2307 struct pte *pe = &ptes[i];
2309 if (start < pe->offset && limit >= pe->offset)
2310 limit = pe->offset - 1;
2311 if (start < first[i] && limit >= first[i])
2312 limit = first[i] - 1;
2314 if (start > limit) {
2315 printf(_("No free sectors available\n"));
2320 if (cround(start) == cround(limit)) {
2323 snprintf(mesg, sizeof(mesg),
2324 _("Last %s or +size or +sizeM or +sizeK"),
2325 str_units(SINGULAR));
2326 stop = read_int(cround(start), cround(limit), cround(limit),
2327 cround(start), mesg);
2328 if (display_in_cyl_units) {
2329 stop = stop * units_per_sector - 1;
2335 set_partition(n, 0, start, stop, sys);
2337 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2339 if (IS_EXTENDED(sys)) {
2340 struct pte *pe4 = &ptes[4];
2341 struct pte *pen = &ptes[n];
2344 pen->ext_pointer = p;
2345 pe4->offset = extended_offset = start;
2346 pe4->sectorbuffer = xzalloc(sector_size);
2347 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2348 pe4->ext_pointer = pe4->part_table + 1;
2357 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2358 struct pte *pe = &ptes[partitions];
2360 pe->sectorbuffer = xzalloc(sector_size);
2361 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2362 pe->ext_pointer = pe->part_table + 1;
2367 add_partition(partitions - 1, LINUX_NATIVE);
2373 int i, free_primary = 0;
2375 if (warn_geometry())
2379 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2383 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2387 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2388 "\n\tIf you want to add DOS-type partitions, create"
2389 "\n\ta new empty DOS partition table first. (Use o.)"
2391 "This will destroy the present disk contents.\n"));
2395 for (i = 0; i < 4; i++)
2396 free_primary += !ptes[i].part_table->sys_ind;
2398 if (!free_primary && partitions >= MAXIMUM_PARTS) {
2399 printf(_("The maximum number of partitions has been created\n"));
2403 if (!free_primary) {
2404 if (extended_offset)
2407 printf(_("You must delete some partition and add "
2408 "an extended partition first\n"));
2410 char c, line[LINE_LENGTH];
2411 snprintf(line, sizeof(line), "%s\n %s\n p primary "
2412 "partition (1-4)\n",
2413 "Command action", (extended_offset ?
2414 "l logical (5 or over)" : "e extended"));
2416 c = read_nonempty(line);
2417 if (c == 'p' || c == 'P') {
2418 i = get_nonexisting_partition(0, 4);
2420 add_partition(i, LINUX_NATIVE);
2423 else if (c == 'l' && extended_offset) {
2427 else if (c == 'e' && !extended_offset) {
2428 i = get_nonexisting_partition(0, 4);
2430 add_partition(i, EXTENDED);
2434 printf(_("Invalid partition number "
2435 "for type '%c'\n"), c);
2446 for (i = 0; i < 3; i++)
2447 if (ptes[i].changed)
2448 ptes[3].changed = 1;
2449 for (i = 3; i < partitions; i++) {
2450 struct pte *pe = &ptes[i];
2453 write_part_table_flag(pe->sectorbuffer);
2454 write_sector(pe->offset, pe->sectorbuffer);
2458 else if (LABEL_IS_SGI) {
2459 /* no test on change? the printf below might be mistaken */
2462 else if (LABEL_IS_SUN) {
2465 for (i = 0; i < 8; i++)
2466 if (ptes[i].changed)
2472 printf(_("The partition table has been altered!\n\n"));
2473 reread_partition_table(1);
2477 reread_partition_table(int leave)
2482 printf(_("Calling ioctl() to re-read partition table.\n"));
2485 if ((i = ioctl(fd, BLKRRPART)) != 0) {
2488 /* some kernel versions (1.2.x) seem to have trouble
2489 rereading the partition table, but if asked to do it
2490 twice, the second time works. - biro@yggdrasil.com */
2493 if ((i = ioctl(fd, BLKRRPART)) != 0)
2498 printf(_("\nWARNING: Re-reading the partition table "
2499 "failed with error %d: %s.\n"
2500 "The kernel still uses the old table.\n"
2501 "The new table will be used "
2502 "at the next reboot.\n"),
2503 error, strerror(error));
2508 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2509 "partitions, please see the fdisk manual page for additional\n"
2515 printf(_("Syncing disks.\n"));
2517 sleep(4); /* for sync() */
2521 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */
2523 #if ENABLE_FEATURE_FDISK_ADVANCED
2524 #define MAX_PER_LINE 16
2526 print_buffer(char *pbuffer)
2530 for (i = 0, l = 0; i < sector_size; i++, l++) {
2532 printf("0x%03X:", i);
2533 printf(" %02X", (unsigned char) pbuffer[i]);
2534 if (l == MAX_PER_LINE - 1) {
2550 printf(_("Device: %s\n"), disk_device);
2551 if (LABEL_IS_SGI || LABEL_IS_SUN)
2552 print_buffer(MBRbuffer);
2554 for (i = 3; i < partitions; i++)
2555 print_buffer(ptes[i].sectorbuffer);
2562 struct pte *pe = &ptes[i];
2563 struct partition *p = pe->part_table;
2566 if (warn_geometry())
2568 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
2569 printf(_("Partition %d has no data area\n"), i + 1);
2572 first = get_partition_start(pe);
2573 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
2574 _("New beginning of data")) - pe->offset;
2576 if (new != get_nr_sects(p)) {
2577 first = get_nr_sects(p) + get_start_sect(p) - new;
2578 set_nr_sects(p, first);
2579 set_start_sect(p, new);
2591 c = tolower(read_nonempty(_("Expert command (m for help): ")));
2599 move_begin(get_partition(0, partitions));
2602 user_cylinders = cylinders =
2603 read_int(1, cylinders, 1048576, 0,
2604 _("Number of cylinders"));
2606 sun_set_ncyl(cylinders);
2616 else if (LABEL_IS_SUN)
2618 else if (LABEL_IS_DOS)
2623 fix_partition_table_order();
2626 #if ENABLE_FEATURE_SGI_LABEL
2631 user_heads = heads = read_int(1, heads, 256, 0,
2632 _("Number of heads"));
2656 user_sectors = sectors = read_int(1, sectors, 63, 0,
2657 _("Number of sectors"));
2658 if (dos_compatible_flag) {
2659 sector_offset = sectors;
2660 printf(_("Warning: setting sector offset for DOS "
2669 write_table(); /* does not return */
2673 sun_set_pcylcount();
2680 #endif /* ADVANCED mode */
2683 is_ide_cdrom_or_tape(const char *device)
2687 struct stat statbuf;
2690 /* No device was given explicitly, and we are trying some
2691 likely things. But opening /dev/hdc may produce errors like
2692 "hdc: tray open or drive not ready"
2693 if it happens to be a CD-ROM drive. It even happens that
2694 the process hangs on the attempt to read a music CD.
2695 So try to be careful. This only works since 2.1.73. */
2697 if (strncmp("/dev/hd", device, 7))
2700 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2701 procf = fopen(buf, "r");
2702 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2703 is_ide = (!strncmp(buf, "cdrom", 5) ||
2704 !strncmp(buf, "tape", 4));
2706 /* Now when this proc file does not exist, skip the
2707 device when it is read-only. */
2708 if (stat(device, &statbuf) == 0)
2709 is_ide = ((statbuf.st_mode & 0222) == 0);
2718 try(const char *device, int user_specified)
2722 disk_device = device;
2723 if (setjmp(listingbuf))
2725 if (!user_specified)
2726 if (is_ide_cdrom_or_tape(device))
2728 if ((fd = open(disk_device, type_open)) >= 0) {
2729 gb = get_boot(try_only);
2730 if (gb > 0) { /* I/O error */
2732 } else if (gb < 0) { /* no DOS signature */
2733 list_disk_geometry();
2737 #if ENABLE_FEATURE_OSF_LABEL
2738 if (btrydev(device) < 0)
2740 printf(_("Disk %s doesn't contain a valid "
2741 "partition table\n"), device);
2746 #if ENABLE_FEATURE_FDISK_WRITABLE
2747 if (!LABEL_IS_SUN && partitions > 4){
2748 delete_partition(ext_index);
2753 /* Ignore other errors, since we try IDE
2754 and SCSI hard disks which may not be
2755 installed on the system. */
2756 if (errno == EACCES) {
2757 printf(_("Cannot open %s\n"), device);
2763 /* for fdisk -l: try all things in /proc/partitions
2764 that look like a partition name (do not end in a digit) */
2769 char line[100], ptname[100], devname[120], *s;
2772 procpt = fopen_or_warn("/proc/partitions", "r");
2774 while (fgets(line, sizeof(line), procpt)) {
2775 if (sscanf(line, " %d %d %d %[^\n ]",
2776 &ma, &mi, &sz, ptname) != 4)
2778 for (s = ptname; *s; s++);
2781 sprintf(devname, "/dev/%s", ptname);
2784 #if ENABLE_FEATURE_CLEAN_UP
2789 #if ENABLE_FEATURE_FDISK_WRITABLE
2791 unknown_command(int c)
2793 printf(_("%c: unknown command\n"), c);
2797 int fdisk_main(int argc, char **argv)
2799 char *str_b, *str_C, *str_H, *str_S;
2804 * fdisk -l [-b sectorsize] [-u] device ...
2805 * fdisk -s [partition] ...
2806 * fdisk [-b sectorsize] [-u] device
2808 * Options -C, -H, -S set the geometry.
2817 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2819 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2820 &str_b, &str_C, &str_H, &str_S);
2823 if (opt & OPT_b) { // -b
2824 /* Ugly: this sector size is really per device,
2825 so cannot be combined with multiple disks,
2826 and the same goes for the C/H/S options.
2828 sector_size = xatoi_u(str_b);
2829 if (sector_size != 512 && sector_size != 1024 &&
2830 sector_size != 2048)
2833 user_set_sector_size = 1;
2835 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2836 if (opt & OPT_H) { // -H
2837 user_heads = xatoi_u(str_H);
2838 if (user_heads <= 0 || user_heads >= 256)
2841 //if (opt & OPT_l) // -l
2842 if (opt & OPT_S) { // -S
2843 user_sectors = xatoi_u(str_S);
2844 if (user_sectors <= 0 || user_sectors >= 64)
2847 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2848 //if (opt & OPT_s) // -s
2850 if (user_set_sector_size && argc != 1)
2851 printf(_("Warning: the -b (set sector size) option should"
2852 " be used with one specified device\n"));
2854 #if ENABLE_FEATURE_FDISK_WRITABLE
2858 type_open = O_RDONLY;
2862 /* avoid gcc warning:
2863 variable `k' might be clobbered by `longjmp' */
2867 for (k = 0; k < argc; k++)
2870 /* we no longer have default device names */
2871 /* but, we can use /proc/partitions instead */
2875 #if ENABLE_FEATURE_FDISK_WRITABLE
2879 #if ENABLE_FEATURE_FDISK_BLKSIZE
2885 type_open = O_RDONLY;
2890 for (j = 0; j < argc; j++) {
2891 disk_device = argv[j];
2892 fd = open(disk_device, type_open);
2894 fdisk_fatal(unable_to_open);
2895 if (ioctl(fd, BLKGETSIZE, &size))
2896 fdisk_fatal(ioctl_error);
2899 printf("%ld\n", size/2);
2901 printf("%s: %ld\n", argv[j], size/2);
2907 #if ENABLE_FEATURE_FDISK_WRITABLE
2911 disk_device = argv[0];
2915 /* OSF label, and no DOS label */
2916 printf(_("Detected an OSF/1 disklabel on %s, entering "
2917 "disklabel mode.\n"), disk_device);
2919 /*Why do we do this? It seems to be counter-intuitive*/
2920 current_label_type = label_dos;
2921 /* If we return we may want to make an empty DOS label? */
2926 c = tolower(read_nonempty(_("Command (m for help): ")));
2930 toggle_active(get_partition(1, partitions));
2931 else if (LABEL_IS_SUN)
2932 toggle_sunflags(get_partition(1, partitions),
2934 else if (LABEL_IS_SGI)
2935 sgi_set_bootpartition(
2936 get_partition(1, partitions));
2942 printf(_("\nThe current boot file is: %s\n"),
2943 sgi_get_bootfile());
2944 if (read_maybe_empty(_("Please enter the name of the "
2945 "new boot file: ")) == '\n')
2946 printf(_("Boot file unchanged\n"));
2948 sgi_set_bootfile(line_ptr);
2950 #if ENABLE_FEATURE_OSF_LABEL
2957 toggle_dos_compatibility_flag();
2958 else if (LABEL_IS_SUN)
2959 toggle_sunflags(get_partition(1, partitions),
2961 else if (LABEL_IS_SGI)
2962 sgi_set_swappartition(
2963 get_partition(1, partitions));
2970 /* If sgi_label then don't use get_existing_partition,
2971 let the user select a partition, since
2972 get_existing_partition() only works for Linux-like
2974 if (!LABEL_IS_SGI) {
2975 j = get_existing_partition(1, partitions);
2977 j = get_partition(1, partitions);
2980 delete_partition(j);
2989 list_types(get_sys_types());
3008 #if ENABLE_FEATURE_SUN_LABEL
3022 write_table(); /* does not return */
3024 #if ENABLE_FEATURE_FDISK_ADVANCED
3027 printf(_("\n\tSorry, no experts menu for SGI "
3028 "partition tables available.\n\n"));
3039 #endif /* CONFIG_FEATURE_FDISK_WRITABLE */