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, const 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 80
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 unsigned get_start_sect(const struct partition *p);
224 static unsigned 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++)
255 static ATTRIBUTE_ALWAYS_INLINE void
260 #endif /* FEATURE_FDISK_WRITABLE */
262 static ATTRIBUTE_ALWAYS_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
284 static ATTRIBUTE_ALWAYS_INLINE void
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 printable char */
296 read_line(const char *prompt)
300 sz = read_line_input(prompt, line_buffer, LINE_LENGTH, NULL);
302 exit(0); /* Ctrl-D or Ctrl-C */
304 if (line_buffer[sz-1] == '\n')
305 line_buffer[--sz] = '\0';
307 line_ptr = line_buffer;
308 while (*line_ptr && !isgraph(*line_ptr))
314 read_nonempty(const char *mesg)
316 while (!read_line(mesg)) /* repeat */;
321 read_maybe_empty(const char *mesg)
323 if (!read_line(mesg)) {
324 line_ptr = line_buffer;
332 read_hex(const struct systypes *sys)
336 read_nonempty(_("Hex code (type L to list codes): "));
337 if (*line_ptr == 'l' || *line_ptr == 'L') {
341 v = bb_strtoul(line_ptr, NULL, 16);
343 /* Bad input also triggers this */
348 #endif /* FEATURE_FDISK_WRITABLE */
350 #include "fdisk_aix.c"
353 unsigned char info[128]; /* Informative text string */
354 unsigned char spare0[14];
356 unsigned char spare1;
358 unsigned char spare2;
361 unsigned char spare1[246]; /* Boot information etc. */
362 unsigned short rspeed; /* Disk rotational speed */
363 unsigned short pcylcount; /* Physical cylinder count */
364 unsigned short sparecyl; /* extra sects per cylinder */
365 unsigned char spare2[4]; /* More magic... */
366 unsigned short ilfact; /* Interleave factor */
367 unsigned short ncyl; /* Data cylinder count */
368 unsigned short nacyl; /* Alt. cylinder count */
369 unsigned short ntrks; /* Tracks per cylinder */
370 unsigned short nsect; /* Sectors per track */
371 unsigned char spare3[4]; /* Even more magic... */
372 struct sun_partinfo {
373 uint32_t start_cylinder;
374 uint32_t num_sectors;
376 unsigned short magic; /* Magic number */
377 unsigned short csum; /* Label xor'd checksum */
379 #define sunlabel ((sun_partition *)MBRbuffer)
381 #define SUN_WHOLE_DISK 5
382 STATIC_OSF void bsd_select(void);
383 STATIC_OSF void xbsd_print_disklabel(int);
384 #include "fdisk_osf.c"
386 #define SGI_VOLHDR 0x00
387 /* 1 and 2 were used for drive types no longer supported by SGI */
388 #define SGI_SWAP 0x03
389 /* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
390 #define SGI_VOLUME 0x06
392 #define SGI_LVOL 0x08
393 #define SGI_RLVOL 0x09
395 #define SGI_XFSLOG 0x0b
398 #define SGI_ENTIRE_DISK SGI_VOLUME
399 #if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
401 fdisk_swap16(uint16_t x)
403 return (x << 8) | (x >> 8);
407 fdisk_swap32(uint32_t x)
410 ((x & 0xFF00) << 8) |
411 ((x & 0xFF0000) >> 8) |
416 STATIC_SGI const struct systypes sgi_sys_types[];
417 STATIC_SGI unsigned sgi_get_num_sectors(int i);
418 STATIC_SGI int sgi_get_sysid(int i);
419 STATIC_SGI void sgi_delete_partition(int i);
420 STATIC_SGI void sgi_change_sysid(int i, int sys);
421 STATIC_SGI void sgi_list_table(int xtra);
422 #if ENABLE_FEATURE_FDISK_ADVANCED
423 STATIC_SGI void sgi_set_xcyl(void);
425 STATIC_SGI int verify_sgi(int verbose);
426 STATIC_SGI void sgi_add_partition(int n, int sys);
427 STATIC_SGI void sgi_set_swappartition(int i);
428 STATIC_SGI const char *sgi_get_bootfile(void);
429 STATIC_SGI void sgi_set_bootfile(const char* aFile);
430 STATIC_SGI void create_sgiinfo(void);
431 STATIC_SGI void sgi_write_table(void);
432 STATIC_SGI void sgi_set_bootpartition(int i);
433 #include "fdisk_sgi.c"
435 STATIC_SUN const struct systypes sun_sys_types[];
436 STATIC_SUN void sun_delete_partition(int i);
437 STATIC_SUN void sun_change_sysid(int i, int sys);
438 STATIC_SUN void sun_list_table(int xtra);
439 STATIC_SUN void add_sun_partition(int n, int sys);
440 #if ENABLE_FEATURE_FDISK_ADVANCED
441 STATIC_SUN void sun_set_alt_cyl(void);
442 STATIC_SUN void sun_set_ncyl(int cyl);
443 STATIC_SUN void sun_set_xcyl(void);
444 STATIC_SUN void sun_set_ilfact(void);
445 STATIC_SUN void sun_set_rspeed(void);
446 STATIC_SUN void sun_set_pcylcount(void);
448 STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
449 STATIC_SUN void verify_sun(void);
450 STATIC_SUN void sun_write_table(void);
451 #include "fdisk_sun.c"
453 /* DOS partition types */
455 static const struct systypes i386_sys_types[] = {
458 { "\x04" "FAT16 <32M" },
459 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
460 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
461 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
462 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
463 { "\x0b" "Win95 FAT32" },
464 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
465 { "\x0e" "Win95 FAT16 (LBA)" },
466 { "\x0f" "Win95 Ext'd (LBA)" },
467 { "\x11" "Hidden FAT12" },
468 { "\x12" "Compaq diagnostics" },
469 { "\x14" "Hidden FAT16 <32M" },
470 { "\x16" "Hidden FAT16" },
471 { "\x17" "Hidden HPFS/NTFS" },
472 { "\x1b" "Hidden Win95 FAT32" },
473 { "\x1c" "Hidden W95 FAT32 (LBA)" },
474 { "\x1e" "Hidden W95 FAT16 (LBA)" },
475 { "\x3c" "Part.Magic recovery" },
476 { "\x41" "PPC PReP Boot" },
478 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
479 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
480 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
481 { "\x82" "Linux swap" }, /* also Solaris */
483 { "\x84" "OS/2 hidden C: drive" },
484 { "\x85" "Linux extended" },
485 { "\x86" "NTFS volume set" },
486 { "\x87" "NTFS volume set" },
487 { "\x8e" "Linux LVM" },
488 { "\x9f" "BSD/OS" }, /* BSDI */
489 { "\xa0" "Thinkpad hibernation" },
490 { "\xa5" "FreeBSD" }, /* various BSD flavours */
491 { "\xa6" "OpenBSD" },
492 { "\xa8" "Darwin UFS" },
494 { "\xab" "Darwin boot" },
495 { "\xb7" "BSDI fs" },
496 { "\xb8" "BSDI swap" },
497 { "\xbe" "Solaris boot" },
498 { "\xeb" "BeOS fs" },
499 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
500 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
501 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
502 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
503 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
504 autodetect using persistent
506 #if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
507 { "\x02" "XENIX root" },
508 { "\x03" "XENIX usr" },
509 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
510 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
512 { "\x18" "AST SmartSleep" },
513 { "\x24" "NEC DOS" },
515 { "\x40" "Venix 80286" },
517 { "\x4e" "QNX4.x 2nd part" },
518 { "\x4f" "QNX4.x 3rd part" },
519 { "\x50" "OnTrack DM" },
520 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
521 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
522 { "\x53" "OnTrack DM6 Aux3" },
523 { "\x54" "OnTrackDM6" },
524 { "\x55" "EZ-Drive" },
525 { "\x56" "Golden Bow" },
526 { "\x5c" "Priam Edisk" },
527 { "\x61" "SpeedStor" },
528 { "\x64" "Novell Netware 286" },
529 { "\x65" "Novell Netware 386" },
530 { "\x70" "DiskSecure Multi-Boot" },
533 { "\x94" "Amoeba BBT" }, /* (bad block table) */
534 { "\xa7" "NeXTSTEP" },
535 { "\xbb" "Boot Wizard hidden" },
536 { "\xc1" "DRDOS/sec (FAT-12)" },
537 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
538 { "\xc6" "DRDOS/sec (FAT-16)" },
540 { "\xda" "Non-FS data" },
541 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
542 Concurrent DOS or CTOS */
543 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
544 { "\xdf" "BootIt" }, /* BootIt EMBRM */
545 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
546 extended partition */
547 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
548 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
549 partition < 1024 cyl. */
550 { "\xf1" "SpeedStor" },
551 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
552 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
553 { "\xff" "BBT" }, /* Xenix Bad Block Table */
559 #if ENABLE_FEATURE_FDISK_WRITABLE
560 /* start_sect and nr_sects are stored little endian on all machines */
561 /* moreover, they are not aligned correctly */
563 store4_little_endian(unsigned char *cp, unsigned val)
570 #endif /* FEATURE_FDISK_WRITABLE */
573 read4_little_endian(const unsigned char *cp)
575 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
578 #if ENABLE_FEATURE_FDISK_WRITABLE
580 set_start_sect(struct partition *p, unsigned start_sect)
582 store4_little_endian(p->start4, start_sect);
587 get_start_sect(const struct partition *p)
589 return read4_little_endian(p->start4);
592 #if ENABLE_FEATURE_FDISK_WRITABLE
594 set_nr_sects(struct partition *p, unsigned nr_sects)
596 store4_little_endian(p->size4, nr_sects);
601 get_nr_sects(const struct partition *p)
603 return read4_little_endian(p->size4);
606 /* normally O_RDWR, -l option gives O_RDONLY */
607 static int type_open = O_RDWR;
610 static int ext_index; /* the prime extended partition */
611 static int listing; /* no aborts for fdisk -l */
612 static int dos_compatible_flag = ~0;
613 #if ENABLE_FEATURE_FDISK_WRITABLE
614 static int dos_changed;
615 static int nowarn; /* no warnings for fdisk -l/-s */
620 static unsigned user_cylinders, user_heads, user_sectors;
621 static unsigned pt_heads, pt_sectors;
622 static unsigned kern_heads, kern_sectors;
624 static off_t extended_offset; /* offset of link pointers */
626 static unsigned long long total_number_of_sectors;
629 static jmp_buf listingbuf;
631 static void fdisk_fatal(enum failure why)
637 longjmp(listingbuf, 1);
642 message = "\nUnable to open %s";
645 message = "\nUnable to read %s";
648 message = "\nUnable to seek on %s";
650 case unable_to_write:
651 message = "\nUnable to write %s";
654 message = "\nBLKGETSIZE ioctl failed on %s";
657 message = "\nFatal error";
660 bb_error_msg_and_die(message, disk_device);
664 seek_sector(off_t secno)
666 off_t offset = secno * sector_size;
667 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
668 fdisk_fatal(unable_to_seek);
671 #if ENABLE_FEATURE_FDISK_WRITABLE
673 write_sector(off_t secno, char *buf)
676 if (write(fd, buf, sector_size) != sector_size)
677 fdisk_fatal(unable_to_write);
681 /* Allocate a buffer and read a partition table sector */
683 read_pte(struct pte *pe, off_t offset)
686 pe->sectorbuffer = xmalloc(sector_size);
688 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
689 fdisk_fatal(unable_to_read);
690 #if ENABLE_FEATURE_FDISK_WRITABLE
693 pe->part_table = pe->ext_pointer = NULL;
697 get_partition_start(const struct pte *pe)
699 return pe->offset + get_start_sect(pe->part_table);
702 #if ENABLE_FEATURE_FDISK_WRITABLE
704 * Avoid warning about DOS partitions when no DOS partition was changed.
705 * Here a heuristic "is probably dos partition".
706 * We might also do the opposite and warn in all cases except
707 * for "is probably nondos partition".
710 is_dos_partition(int t)
712 return (t == 1 || t == 4 || t == 6 ||
713 t == 0x0b || t == 0x0c || t == 0x0e ||
714 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
715 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
716 t == 0xc1 || t == 0xc4 || t == 0xc6);
722 puts(_("Command Action"));
724 puts(_("a\ttoggle a read only flag")); /* sun */
725 puts(_("b\tedit bsd disklabel"));
726 puts(_("c\ttoggle the mountable flag")); /* sun */
727 puts(_("d\tdelete a partition"));
728 puts(_("l\tlist known partition types"));
729 puts(_("n\tadd a new partition"));
730 puts(_("o\tcreate a new empty DOS partition table"));
731 puts(_("p\tprint the partition table"));
732 puts(_("q\tquit without saving changes"));
733 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
734 puts(_("t\tchange a partition's system id"));
735 puts(_("u\tchange display/entry units"));
736 puts(_("v\tverify the partition table"));
737 puts(_("w\twrite table to disk and exit"));
738 #if ENABLE_FEATURE_FDISK_ADVANCED
739 puts(_("x\textra functionality (experts only)"));
741 } else if (LABEL_IS_SGI) {
742 puts(_("a\tselect bootable partition")); /* sgi flavour */
743 puts(_("b\tedit bootfile entry")); /* sgi */
744 puts(_("c\tselect sgi swap partition")); /* sgi flavour */
745 puts(_("d\tdelete a partition"));
746 puts(_("l\tlist known partition types"));
747 puts(_("n\tadd a new partition"));
748 puts(_("o\tcreate a new empty DOS partition table"));
749 puts(_("p\tprint the partition table"));
750 puts(_("q\tquit without saving changes"));
751 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
752 puts(_("t\tchange a partition's system id"));
753 puts(_("u\tchange display/entry units"));
754 puts(_("v\tverify the partition table"));
755 puts(_("w\twrite table to disk and exit"));
756 } else if (LABEL_IS_AIX) {
757 puts(_("o\tcreate a new empty DOS partition table"));
758 puts(_("q\tquit without saving changes"));
759 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
761 puts(_("a\ttoggle a bootable flag"));
762 puts(_("b\tedit bsd disklabel"));
763 puts(_("c\ttoggle the dos compatibility flag"));
764 puts(_("d\tdelete a partition"));
765 puts(_("l\tlist known partition types"));
766 puts(_("n\tadd a new partition"));
767 puts(_("o\tcreate a new empty DOS partition table"));
768 puts(_("p\tprint the partition table"));
769 puts(_("q\tquit without saving changes"));
770 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
771 puts(_("t\tchange a partition's system id"));
772 puts(_("u\tchange display/entry units"));
773 puts(_("v\tverify the partition table"));
774 puts(_("w\twrite table to disk and exit"));
775 #if ENABLE_FEATURE_FDISK_ADVANCED
776 puts(_("x\textra functionality (experts only)"));
780 #endif /* FEATURE_FDISK_WRITABLE */
783 #if ENABLE_FEATURE_FDISK_ADVANCED
787 puts(_("Command Action"));
789 puts(_("a\tchange number of alternate cylinders")); /*sun*/
790 puts(_("c\tchange number of cylinders"));
791 puts(_("d\tprint the raw data in the partition table"));
792 puts(_("e\tchange number of extra sectors per cylinder"));/*sun*/
793 puts(_("h\tchange number of heads"));
794 puts(_("i\tchange interleave factor")); /*sun*/
795 puts(_("o\tchange rotation speed (rpm)")); /*sun*/
796 puts(_("p\tprint the partition table"));
797 puts(_("q\tquit without saving changes"));
798 puts(_("r\treturn to main menu"));
799 puts(_("s\tchange number of sectors/track"));
800 puts(_("v\tverify the partition table"));
801 puts(_("w\twrite table to disk and exit"));
802 puts(_("y\tchange number of physical cylinders")); /*sun*/
803 } else if (LABEL_IS_SGI) {
804 puts(_("b\tmove beginning of data in a partition")); /* !sun */
805 puts(_("c\tchange number of cylinders"));
806 puts(_("d\tprint the raw data in the partition table"));
807 puts(_("e\tlist extended partitions")); /* !sun */
808 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
809 puts(_("h\tchange number of heads"));
810 puts(_("p\tprint the partition table"));
811 puts(_("q\tquit without saving changes"));
812 puts(_("r\treturn to main menu"));
813 puts(_("s\tchange number of sectors/track"));
814 puts(_("v\tverify the partition table"));
815 puts(_("w\twrite table to disk and exit"));
816 } else if (LABEL_IS_AIX) {
817 puts(_("b\tmove beginning of data in a partition")); /* !sun */
818 puts(_("c\tchange number of cylinders"));
819 puts(_("d\tprint the raw data in the partition table"));
820 puts(_("e\tlist extended partitions")); /* !sun */
821 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
822 puts(_("h\tchange number of heads"));
823 puts(_("p\tprint the partition table"));
824 puts(_("q\tquit without saving changes"));
825 puts(_("r\treturn to main menu"));
826 puts(_("s\tchange number of sectors/track"));
827 puts(_("v\tverify the partition table"));
828 puts(_("w\twrite table to disk and exit"));
830 puts(_("b\tmove beginning of data in a partition")); /* !sun */
831 puts(_("c\tchange number of cylinders"));
832 puts(_("d\tprint the raw data in the partition table"));
833 puts(_("e\tlist extended partitions")); /* !sun */
834 puts(_("f\tfix partition order")); /* !sun, !aix, !sgi */
835 #if ENABLE_FEATURE_SGI_LABEL
836 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
838 puts(_("h\tchange number of heads"));
839 puts(_("p\tprint the partition table"));
840 puts(_("q\tquit without saving changes"));
841 puts(_("r\treturn to main menu"));
842 puts(_("s\tchange number of sectors/track"));
843 puts(_("v\tverify the partition table"));
844 puts(_("w\twrite table to disk and exit"));
847 #endif /* ADVANCED mode */
849 #if ENABLE_FEATURE_FDISK_WRITABLE
850 static const struct systypes *
854 LABEL_IS_SUN ? sun_sys_types :
855 LABEL_IS_SGI ? sgi_sys_types :
859 #define get_sys_types() i386_sys_types
860 #endif /* FEATURE_FDISK_WRITABLE */
862 static const char *partition_type(unsigned char type)
865 const struct systypes *types = get_sys_types();
867 for (i = 0; types[i].name; i++)
868 if ((unsigned char)types[i].name[0] == type)
869 return types[i].name + 1;
875 #if ENABLE_FEATURE_FDISK_WRITABLE
879 return LABEL_IS_SUN ? sunlabel->infos[i].id :
880 (LABEL_IS_SGI ? sgi_get_sysid(i) :
881 ptes[i].part_table->sys_ind);
884 static void list_types(const struct systypes *sys)
889 unsigned done, next, size;
892 for (size = 0; sys[size].name; size++) /* */;
895 for (i = COLS-1; i >= 0; i--) {
896 done += (size + i - done) / (i + 1);
897 last[COLS-1 - i] = done;
902 printf("%c%2x %-22.22s", i ? ' ' : '\n',
903 (unsigned char)sys[next].name[0],
905 next = last[i++] + done;
906 if (i >= COLS || next >= last[i]) {
910 } while (done < last[0]);
913 #endif /* FEATURE_FDISK_WRITABLE */
916 is_cleared_partition(const struct partition *p)
918 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
919 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
920 get_start_sect(p) || get_nr_sects(p));
924 clear_partition(struct partition *p)
928 memset(p, 0, sizeof(struct partition));
931 #if ENABLE_FEATURE_FDISK_WRITABLE
933 set_partition(int i, int doext, off_t start, off_t stop, int sysid)
939 p = ptes[i].ext_pointer;
940 offset = extended_offset;
942 p = ptes[i].part_table;
943 offset = ptes[i].offset;
947 set_start_sect(p, start - offset);
948 set_nr_sects(p, stop - start + 1);
949 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
950 start = heads*sectors*1024 - 1;
951 set_hsc(p->head, p->sector, p->cyl, start);
952 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
953 stop = heads*sectors*1024 - 1;
954 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
960 test_c(const char **m, const char *mesg)
964 printf(_("You must set"));
976 const char *m = NULL;
980 prev = test_c(&m, _("heads"));
982 prev = test_c(&m, _("sectors"));
984 prev = test_c(&m, _("cylinders"));
989 #if ENABLE_FEATURE_FDISK_WRITABLE
990 "You can do this from the extra functions menu.\n"
992 , prev ? _(" and ") : " ", m);
997 static void update_units(void)
999 int cyl_units = heads * sectors;
1001 if (display_in_cyl_units && cyl_units)
1002 units_per_sector = cyl_units;
1004 units_per_sector = 1; /* in sectors */
1007 #if ENABLE_FEATURE_FDISK_WRITABLE
1009 warn_cylinders(void)
1011 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1013 "The number of cylinders for this disk is set to %d.\n"
1014 "There is nothing wrong with that, but this is larger than 1024,\n"
1015 "and could in certain setups cause problems with:\n"
1016 "1) software that runs at boot time (e.g., old versions of LILO)\n"
1017 "2) booting and partitioning software from other OSs\n"
1018 " (e.g., DOS FDISK, OS/2 FDISK)\n"),
1024 read_extended(int ext)
1028 struct partition *p, *q;
1032 pex->ext_pointer = pex->part_table;
1034 p = pex->part_table;
1035 if (!get_start_sect(p)) {
1036 printf(_("Bad offset in primary extended partition\n"));
1040 while (IS_EXTENDED(p->sys_ind)) {
1041 struct pte *pe = &ptes[partitions];
1043 if (partitions >= MAXIMUM_PARTS) {
1044 /* This is not a Linux restriction, but
1045 this program uses arrays of size MAXIMUM_PARTS.
1046 Do not try to 'improve' this test. */
1047 struct pte *pre = &ptes[partitions-1];
1048 #if ENABLE_FEATURE_FDISK_WRITABLE
1049 printf(_("Warning: deleting partitions after %d\n"),
1053 clear_partition(pre->ext_pointer);
1057 read_pte(pe, extended_offset + get_start_sect(p));
1059 if (!extended_offset)
1060 extended_offset = get_start_sect(p);
1062 q = p = pt_offset(pe->sectorbuffer, 0);
1063 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
1064 if (IS_EXTENDED(p->sys_ind)) {
1065 if (pe->ext_pointer)
1066 printf(_("Warning: extra link "
1067 "pointer in partition table"
1068 " %d\n"), partitions + 1);
1070 pe->ext_pointer = p;
1071 } else if (p->sys_ind) {
1073 printf(_("Warning: ignoring extra "
1074 "data in partition table"
1075 " %d\n"), partitions + 1);
1081 /* very strange code here... */
1082 if (!pe->part_table) {
1083 if (q != pe->ext_pointer)
1086 pe->part_table = q + 1;
1088 if (!pe->ext_pointer) {
1089 if (q != pe->part_table)
1090 pe->ext_pointer = q;
1092 pe->ext_pointer = q + 1;
1095 p = pe->ext_pointer;
1099 #if ENABLE_FEATURE_FDISK_WRITABLE
1100 /* remove empty links */
1102 for (i = 4; i < partitions; i++) {
1103 struct pte *pe = &ptes[i];
1105 if (!get_nr_sects(pe->part_table) &&
1106 (partitions > 5 || ptes[4].part_table->sys_ind)) {
1107 printf("omitting empty partition (%d)\n", i+1);
1108 delete_partition(i);
1109 goto remove; /* numbering changed */
1115 #if ENABLE_FEATURE_FDISK_WRITABLE
1117 create_doslabel(void)
1122 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1123 "until you decide to write them. After that, of course, the previous\n"
1124 "content won't be recoverable.\n\n"));
1126 current_label_type = label_dos;
1128 #if ENABLE_FEATURE_OSF_LABEL
1129 possibly_osf_label = 0;
1133 for (i = 510-64; i < 510; i++)
1135 write_part_table_flag(MBRbuffer);
1136 extended_offset = 0;
1137 set_all_unchanged();
1139 get_boot(create_empty_dos);
1141 #endif /* FEATURE_FDISK_WRITABLE */
1144 get_sectorsize(void)
1146 if (!user_set_sector_size) {
1148 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1150 if (sector_size != DEFAULT_SECTOR_SIZE)
1151 printf(_("Note: sector size is %d (not %d)\n"),
1152 sector_size, DEFAULT_SECTOR_SIZE);
1157 get_kernel_geometry(void)
1159 struct hd_geometry geometry;
1161 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1162 kern_heads = geometry.heads;
1163 kern_sectors = geometry.sectors;
1164 /* never use geometry.cylinders - it is truncated */
1169 get_partition_table_geometry(void)
1171 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
1172 struct partition *p;
1173 int i, h, s, hh, ss;
1177 if (!(valid_part_table_flag((char*)bufp)))
1181 for (i = 0; i < 4; i++) {
1182 p = pt_offset(bufp, i);
1183 if (p->sys_ind != 0) {
1184 h = p->end_head + 1;
1185 s = (p->end_sector & 077);
1190 } else if (hh != h || ss != s)
1195 if (!first && !bad) {
1205 unsigned long long bytes; /* really u64 */
1208 sec_fac = sector_size / 512;
1209 #if ENABLE_FEATURE_SUN_LABEL
1210 guess_device_type();
1212 heads = cylinders = sectors = 0;
1213 kern_heads = kern_sectors = 0;
1214 pt_heads = pt_sectors = 0;
1216 get_kernel_geometry();
1217 get_partition_table_geometry();
1219 heads = user_heads ? user_heads :
1220 pt_heads ? pt_heads :
1221 kern_heads ? kern_heads : 255;
1222 sectors = user_sectors ? user_sectors :
1223 pt_sectors ? pt_sectors :
1224 kern_sectors ? kern_sectors : 63;
1225 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1228 unsigned long longsectors;
1230 if (ioctl(fd, BLKGETSIZE, &longsectors))
1232 bytes = ((unsigned long long) longsectors) << 9;
1235 total_number_of_sectors = (bytes >> 9);
1238 if (dos_compatible_flag)
1239 sector_offset = sectors;
1241 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
1243 cylinders = user_cylinders;
1247 * Read MBR. Returns:
1248 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1249 * 0: found or created label
1253 get_boot(enum action what)
1259 for (i = 0; i < 4; i++) {
1260 struct pte *pe = &ptes[i];
1262 pe->part_table = pt_offset(MBRbuffer, i);
1263 pe->ext_pointer = NULL;
1265 pe->sectorbuffer = MBRbuffer;
1266 #if ENABLE_FEATURE_FDISK_WRITABLE
1267 pe->changed = (what == create_empty_dos);
1271 #if ENABLE_FEATURE_SUN_LABEL
1272 if (what == create_empty_sun && check_sun_label())
1276 memset(MBRbuffer, 0, 512);
1278 #if ENABLE_FEATURE_FDISK_WRITABLE
1279 if (what == create_empty_dos)
1280 goto got_dos_table; /* skip reading disk */
1282 if ((fd = open(disk_device, type_open)) < 0) {
1283 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1284 if (what == try_only)
1286 fdisk_fatal(unable_to_open);
1288 printf(_("You will not be able to write "
1289 "the partition table.\n"));
1292 if (512 != read(fd, MBRbuffer, 512)) {
1293 if (what == try_only)
1295 fdisk_fatal(unable_to_read);
1298 if ((fd = open(disk_device, O_RDONLY)) < 0)
1300 if (512 != read(fd, MBRbuffer, 512))
1308 #if ENABLE_FEATURE_SUN_LABEL
1309 if (check_sun_label())
1313 #if ENABLE_FEATURE_SGI_LABEL
1314 if (check_sgi_label())
1318 #if ENABLE_FEATURE_AIX_LABEL
1319 if (check_aix_label())
1323 #if ENABLE_FEATURE_OSF_LABEL
1324 if (check_osf_label()) {
1325 possibly_osf_label = 1;
1326 if (!valid_part_table_flag(MBRbuffer)) {
1327 current_label_type = label_osf;
1330 printf(_("This disk has both DOS and BSD magic.\n"
1331 "Give the 'b' command to go to BSD mode.\n"));
1335 #if ENABLE_FEATURE_FDISK_WRITABLE
1339 if (!valid_part_table_flag(MBRbuffer)) {
1340 #if !ENABLE_FEATURE_FDISK_WRITABLE
1345 printf(_("Device contains neither a valid DOS "
1346 "partition table, nor Sun, SGI or OSF "
1349 #if ENABLE_FEATURE_SUN_LABEL
1358 case create_empty_dos:
1359 #if ENABLE_FEATURE_SUN_LABEL
1360 case create_empty_sun:
1364 bb_error_msg_and_die(_("internal error"));
1366 #endif /* FEATURE_FDISK_WRITABLE */
1369 #if ENABLE_FEATURE_FDISK_WRITABLE
1374 for (i = 0; i < 4; i++) {
1375 struct pte *pe = &ptes[i];
1377 if (IS_EXTENDED(pe->part_table->sys_ind)) {
1378 if (partitions != 4)
1379 printf(_("Ignoring extra extended "
1380 "partition %d\n"), i + 1);
1386 for (i = 3; i < partitions; i++) {
1387 struct pte *pe = &ptes[i];
1389 if (!valid_part_table_flag(pe->sectorbuffer)) {
1390 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
1391 "table %d will be corrected by w(rite)\n"),
1392 pe->sectorbuffer[510],
1393 pe->sectorbuffer[511],
1395 #if ENABLE_FEATURE_FDISK_WRITABLE
1404 #if ENABLE_FEATURE_FDISK_WRITABLE
1406 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1407 * If the user hits Enter, DFLT is returned.
1408 * Answers like +10 are interpreted as offsets from BASE.
1410 * There is no default if DFLT is not between LOW and HIGH.
1413 read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
1417 const char *fmt = "%s (%u-%u, default %u): ";
1419 if (dflt < low || dflt > high) {
1420 fmt = "%s (%u-%u): ";
1425 int use_default = default_ok;
1427 /* ask question and read answer */
1429 printf(fmt, mesg, low, high, dflt);
1430 read_maybe_empty("");
1431 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1432 && *line_ptr != '-' && *line_ptr != '+');
1434 if (*line_ptr == '+' || *line_ptr == '-') {
1435 int minus = (*line_ptr == '-');
1438 i = atoi(line_ptr + 1);
1440 while (isdigit(*++line_ptr))
1443 switch (*line_ptr) {
1446 if (!display_in_cyl_units)
1447 i *= heads * sectors;
1461 absolute = 1000000000;
1467 unsigned long long bytes;
1470 bytes = (unsigned long long) i * absolute;
1471 unit = sector_size * units_per_sector;
1472 bytes += unit/2; /* round */
1481 while (isdigit(*line_ptr)) {
1487 printf(_("Using default value %u\n"), i = dflt);
1488 if (i >= low && i <= high)
1491 printf(_("Value is out of range\n"));
1497 get_partition(int warn, int max)
1502 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1506 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1507 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1508 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1510 printf(_("Warning: partition %d has empty type\n"), i+1);
1517 get_existing_partition(int warn, int max)
1522 for (i = 0; i < max; i++) {
1523 struct pte *pe = &ptes[i];
1524 struct partition *p = pe->part_table;
1526 if (p && !is_cleared_partition(p)) {
1533 printf(_("Selected partition %d\n"), pno+1);
1536 printf(_("No partition is defined yet!\n"));
1540 return get_partition(warn, max);
1544 get_nonexisting_partition(int warn, int max)
1549 for (i = 0; i < max; i++) {
1550 struct pte *pe = &ptes[i];
1551 struct partition *p = pe->part_table;
1553 if (p && is_cleared_partition(p)) {
1560 printf(_("Selected partition %d\n"), pno+1);
1563 printf(_("All primary partitions have been defined already!\n"));
1567 return get_partition(warn, max);
1574 display_in_cyl_units = !display_in_cyl_units;
1576 printf(_("Changing display/entry units to %s\n"),
1581 toggle_active(int i)
1583 struct pte *pe = &ptes[i];
1584 struct partition *p = pe->part_table;
1586 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
1587 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
1588 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1593 toggle_dos_compatibility_flag(void)
1595 dos_compatible_flag = ~dos_compatible_flag;
1596 if (dos_compatible_flag) {
1597 sector_offset = sectors;
1598 printf(_("DOS Compatibility flag is set\n"));
1602 printf(_("DOS Compatibility flag is not set\n"));
1607 delete_partition(int i)
1609 struct pte *pe = &ptes[i];
1610 struct partition *p = pe->part_table;
1611 struct partition *q = pe->ext_pointer;
1613 /* Note that for the fifth partition (i == 4) we don't actually
1614 * decrement partitions.
1617 if (warn_geometry())
1618 return; /* C/H/S not set */
1622 sun_delete_partition(i);
1626 sgi_delete_partition(i);
1631 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
1633 ptes[ext_index].ext_pointer = NULL;
1634 extended_offset = 0;
1640 if (!q->sys_ind && i > 4) {
1641 /* the last one in the chain - just delete */
1644 clear_partition(ptes[i].ext_pointer);
1645 ptes[i].changed = 1;
1647 /* not the last one - further ones will be moved down */
1649 /* delete this link in the chain */
1650 p = ptes[i-1].ext_pointer;
1652 set_start_sect(p, get_start_sect(q));
1653 set_nr_sects(p, get_nr_sects(q));
1654 ptes[i-1].changed = 1;
1655 } else if (partitions > 5) { /* 5 will be moved to 4 */
1656 /* the first logical in a longer chain */
1659 if (pe->part_table) /* prevent SEGFAULT */
1660 set_start_sect(pe->part_table,
1661 get_partition_start(pe) -
1663 pe->offset = extended_offset;
1667 if (partitions > 5) {
1669 while (i < partitions) {
1670 ptes[i] = ptes[i+1];
1674 /* the only logical: clear only */
1675 clear_partition(ptes[i].part_table);
1682 int i, sys, origsys;
1683 struct partition *p;
1685 /* If sgi_label then don't use get_existing_partition,
1686 let the user select a partition, since get_existing_partition()
1687 only works for Linux like partition tables. */
1688 if (!LABEL_IS_SGI) {
1689 i = get_existing_partition(0, partitions);
1691 i = get_partition(0, partitions);
1695 p = ptes[i].part_table;
1696 origsys = sys = get_sysid(i);
1698 /* if changing types T to 0 is allowed, then
1699 the reverse change must be allowed, too */
1700 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
1701 printf(_("Partition %d does not exist yet!\n"), i + 1);
1705 sys = read_hex (get_sys_types());
1707 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
1708 printf(_("Type 0 means free space to many systems\n"
1709 "(but not to Linux). Having partitions of\n"
1710 "type 0 is probably unwise. You can delete\n"
1711 "a partition using the 'd' command.\n"));
1715 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
1716 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
1717 printf(_("You cannot change a partition into"
1718 " an extended one or vice versa\n"
1719 "Delete it first.\n"));
1725 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
1726 printf(_("Consider leaving partition 3 "
1727 "as Whole disk (5),\n"
1728 "as SunOS/Solaris expects it and "
1729 "even Linux likes it.\n\n"));
1732 (i == 10 && sys != SGI_ENTIRE_DISK) ||
1733 (i == 8 && sys != 0)
1736 printf(_("Consider leaving partition 9 "
1737 "as volume header (0),\nand "
1738 "partition 11 as entire volume (6)"
1739 "as IRIX expects it.\n\n"));
1744 sun_change_sysid(i, sys);
1745 } else if (LABEL_IS_SGI) {
1746 sgi_change_sysid(i, sys);
1750 printf(_("Changed system type of partition %d "
1751 "to %x (%s)\n"), i + 1, sys,
1752 partition_type(sys));
1753 ptes[i].changed = 1;
1754 if (is_dos_partition(origsys) ||
1755 is_dos_partition(sys))
1761 #endif /* FEATURE_FDISK_WRITABLE */
1764 /* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
1765 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1766 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1767 * Lubkin Oct. 1991). */
1770 linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
1772 int spc = heads * sectors;
1777 *s = ls % sectors + 1; /* sectors count from 1 */
1781 check_consistency(const struct partition *p, int partition)
1783 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1784 unsigned pec, peh, pes; /* physical ending c, h, s */
1785 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1786 unsigned lec, leh, les; /* logical ending c, h, s */
1788 if (!heads || !sectors || (partition >= 4))
1789 return; /* do not check extended partitions */
1791 /* physical beginning c, h, s */
1792 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1794 pbs = p->sector & 0x3f;
1796 /* physical ending c, h, s */
1797 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1799 pes = p->end_sector & 0x3f;
1801 /* compute logical beginning (c, h, s) */
1802 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1804 /* compute logical ending (c, h, s) */
1805 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1807 /* Same physical / logical beginning? */
1808 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1809 printf(_("Partition %d has different physical/logical "
1810 "beginnings (non-Linux?):\n"), partition + 1);
1811 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1812 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1815 /* Same physical / logical ending? */
1816 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1817 printf(_("Partition %d has different physical/logical "
1818 "endings:\n"), partition + 1);
1819 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1820 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1823 /* Ending on cylinder boundary? */
1824 if (peh != (heads - 1) || pes != sectors) {
1825 printf(_("Partition %i does not end on cylinder boundary.\n"),
1831 list_disk_geometry(void)
1833 long long bytes = (total_number_of_sectors << 9);
1834 long megabytes = bytes/1000000;
1836 if (megabytes < 10000)
1837 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
1838 disk_device, megabytes, bytes);
1840 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
1841 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1842 printf(_("%d heads, %d sectors/track, %d cylinders"),
1843 heads, sectors, cylinders);
1844 if (units_per_sector == 1)
1845 printf(_(", total %llu sectors"),
1846 total_number_of_sectors / (sector_size/512));
1847 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
1849 units_per_sector, sector_size, units_per_sector * sector_size);
1853 * Check whether partition entries are ordered by their starting positions.
1854 * Return 0 if OK. Return i if partition i should have been earlier.
1855 * Two separate checks: primary and logical partitions.
1858 wrong_p_order(int *prev)
1860 const struct pte *pe;
1861 const struct partition *p;
1862 off_t last_p_start_pos = 0, p_start_pos;
1865 for (i = 0 ; i < partitions; i++) {
1868 last_p_start_pos = 0;
1871 if ((p = pe->part_table)->sys_ind) {
1872 p_start_pos = get_partition_start(pe);
1874 if (last_p_start_pos > p_start_pos) {
1880 last_p_start_pos = p_start_pos;
1887 #if ENABLE_FEATURE_FDISK_ADVANCED
1889 * Fix the chain of logicals.
1890 * extended_offset is unchanged, the set of sectors used is unchanged
1891 * The chain is sorted so that sectors increase, and so that
1892 * starting sectors increase.
1894 * After this it may still be that cfdisk doesnt like the table.
1895 * (This is because cfdisk considers expanded parts, from link to
1896 * end of partition, and these may still overlap.)
1898 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1902 fix_chain_of_logicals(void)
1904 int j, oj, ojj, sj, sjj;
1905 struct partition *pj,*pjj,tmp;
1907 /* Stage 1: sort sectors but leave sector of part 4 */
1908 /* (Its sector is the global extended_offset.) */
1910 for (j = 5; j < partitions-1; j++) {
1911 oj = ptes[j].offset;
1912 ojj = ptes[j+1].offset;
1914 ptes[j].offset = ojj;
1915 ptes[j+1].offset = oj;
1916 pj = ptes[j].part_table;
1917 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1918 pjj = ptes[j+1].part_table;
1919 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1920 set_start_sect(ptes[j-1].ext_pointer,
1921 ojj-extended_offset);
1922 set_start_sect(ptes[j].ext_pointer,
1923 oj-extended_offset);
1928 /* Stage 2: sort starting sectors */
1930 for (j = 4; j < partitions-1; j++) {
1931 pj = ptes[j].part_table;
1932 pjj = ptes[j+1].part_table;
1933 sj = get_start_sect(pj);
1934 sjj = get_start_sect(pjj);
1935 oj = ptes[j].offset;
1936 ojj = ptes[j+1].offset;
1937 if (oj+sj > ojj+sjj) {
1941 set_start_sect(pj, ojj+sjj-oj);
1942 set_start_sect(pjj, oj+sj-ojj);
1947 /* Probably something was changed */
1948 for (j = 4; j < partitions; j++)
1949 ptes[j].changed = 1;
1954 fix_partition_table_order(void)
1956 struct pte *pei, *pek;
1959 if (!wrong_p_order(NULL)) {
1960 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1964 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1965 /* partition i should have come earlier, move it */
1966 /* We have to move data in the MBR */
1967 struct partition *pi, *pk, *pe, pbuf;
1971 pe = pei->ext_pointer;
1972 pei->ext_pointer = pek->ext_pointer;
1973 pek->ext_pointer = pe;
1975 pi = pei->part_table;
1976 pk = pek->part_table;
1978 memmove(&pbuf, pi, sizeof(struct partition));
1979 memmove(pi, pk, sizeof(struct partition));
1980 memmove(pk, &pbuf, sizeof(struct partition));
1982 pei->changed = pek->changed = 1;
1986 fix_chain_of_logicals();
1994 list_table(int xtra)
1996 const struct partition *p;
2000 sun_list_table(xtra);
2004 sgi_list_table(xtra);
2008 list_disk_geometry();
2011 xbsd_print_disklabel(xtra);
2015 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2016 but if the device name ends in a digit, say /dev/foo1,
2017 then the partition is called /dev/foo1p3. */
2018 w = strlen(disk_device);
2019 if (w && isdigit(disk_device[w-1]))
2024 // 1 12345678901 12345678901 12345678901 12
2025 printf(_("%*s Boot Start End Blocks Id System\n"),
2028 for (i = 0; i < partitions; i++) {
2029 const struct pte *pe = &ptes[i];
2035 if (!p || is_cleared_partition(p))
2038 psects = get_nr_sects(p);
2042 if (sector_size < 1024) {
2043 pblocks /= (1024 / sector_size);
2044 podd = psects % (1024 / sector_size);
2046 if (sector_size > 1024)
2047 pblocks *= (sector_size / 1024);
2049 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2050 partname(disk_device, i+1, w+2),
2051 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2053 (unsigned long long) cround(get_partition_start(pe)), /* start */
2054 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2055 - (psects ? 1 : 0)),
2056 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2057 p->sys_ind, /* type id */
2058 partition_type(p->sys_ind)); /* type name */
2060 check_consistency(p, i);
2063 /* Is partition table in disk order? It need not be, but... */
2064 /* partition table entries are not checked for correct order if this
2065 is a sgi, sun or aix labeled disk... */
2066 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
2068 printf(_("\nPartition table entries are not in disk order\n"));
2072 #if ENABLE_FEATURE_FDISK_ADVANCED
2074 x_list_table(int extend)
2076 const struct pte *pe;
2077 const struct partition *p;
2080 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2081 disk_device, heads, sectors, cylinders);
2082 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
2083 for (i = 0 ; i < partitions; i++) {
2085 p = (extend ? pe->ext_pointer : pe->part_table);
2087 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
2088 i + 1, p->boot_ind, p->head,
2090 cylinder(p->sector, p->cyl), p->end_head,
2091 sector(p->end_sector),
2092 cylinder(p->end_sector, p->end_cyl),
2093 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2095 check_consistency(p, i);
2101 #if ENABLE_FEATURE_FDISK_WRITABLE
2103 fill_bounds(off_t *first, off_t *last)
2106 const struct pte *pe = &ptes[0];
2107 const struct partition *p;
2109 for (i = 0; i < partitions; pe++,i++) {
2111 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
2112 first[i] = 0xffffffff;
2115 first[i] = get_partition_start(pe);
2116 last[i] = first[i] + get_nr_sects(p) - 1;
2122 check(int n, unsigned h, unsigned s, unsigned c, off_t start)
2124 off_t total, real_s, real_c;
2126 real_s = sector(s) - 1;
2127 real_c = cylinder(s, c);
2128 total = (real_c * sectors + real_s) * heads + h;
2130 printf(_("Warning: partition %d contains sector 0\n"), n);
2132 printf(_("Partition %d: head %d greater than maximum %d\n"),
2134 if (real_s >= sectors)
2135 printf(_("Partition %d: sector %d greater than "
2136 "maximum %d\n"), n, s, sectors);
2137 if (real_c >= cylinders)
2138 printf(_("Partitions %d: cylinder %llu greater than "
2139 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
2140 if (cylinders <= 1024 && start != total)
2141 printf(_("Partition %d: previous sectors %llu disagrees with "
2142 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
2150 off_t first[partitions], last[partitions];
2151 struct partition *p;
2153 if (warn_geometry())
2165 fill_bounds(first, last);
2166 for (i = 0; i < partitions; i++) {
2167 struct pte *pe = &ptes[i];
2170 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
2171 check_consistency(p, i);
2172 if (get_partition_start(pe) < first[i])
2173 printf(_("Warning: bad start-of-data in "
2174 "partition %d\n"), i + 1);
2175 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2177 total += last[i] + 1 - first[i];
2178 for (j = 0; j < i; j++)
2179 if ((first[i] >= first[j] && first[i] <= last[j])
2180 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2181 printf(_("Warning: partition %d overlaps "
2182 "partition %d.\n"), j + 1, i + 1);
2183 total += first[i] >= first[j] ?
2184 first[i] : first[j];
2185 total -= last[i] <= last[j] ?
2191 if (extended_offset) {
2192 struct pte *pex = &ptes[ext_index];
2193 off_t e_last = get_start_sect(pex->part_table) +
2194 get_nr_sects(pex->part_table) - 1;
2196 for (i = 4; i < partitions; i++) {
2198 p = ptes[i].part_table;
2200 if (i != 4 || i + 1 < partitions)
2201 printf(_("Warning: partition %d "
2202 "is empty\n"), i + 1);
2204 else if (first[i] < extended_offset ||
2206 printf(_("Logical partition %d not entirely in "
2207 "partition %d\n"), i + 1, ext_index + 1);
2211 if (total > heads * sectors * cylinders)
2212 printf(_("Total allocated sectors %d greater than the maximum "
2213 "%d\n"), total, heads * sectors * cylinders);
2214 else if ((total = heads * sectors * cylinders - total) != 0)
2215 printf(_("%d unallocated sectors\n"), total);
2219 add_partition(int n, int sys)
2221 char mesg[256]; /* 48 does not suffice in Japanese */
2222 int i, num_read = 0;
2223 struct partition *p = ptes[n].part_table;
2224 struct partition *q = ptes[ext_index].part_table;
2226 off_t start, stop = 0, limit, temp,
2227 first[partitions], last[partitions];
2229 if (p && p->sys_ind) {
2230 printf(_("Partition %d is already defined. Delete "
2231 "it before re-adding it.\n"), n + 1);
2234 fill_bounds(first, last);
2236 start = sector_offset;
2237 if (display_in_cyl_units || !total_number_of_sectors)
2238 llimit = heads * sectors * cylinders - 1;
2240 llimit = total_number_of_sectors - 1;
2242 if (limit != llimit)
2244 if (extended_offset) {
2245 first[ext_index] = extended_offset;
2246 last[ext_index] = get_start_sect(q) +
2247 get_nr_sects(q) - 1;
2250 start = extended_offset + sector_offset;
2251 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2253 if (display_in_cyl_units)
2254 for (i = 0; i < partitions; i++)
2255 first[i] = (cround(first[i]) - 1) * units_per_sector;
2257 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2260 for (i = 0; i < partitions; i++) {
2263 if (start == ptes[i].offset)
2264 start += sector_offset;
2265 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
2266 if (start >= first[i] && start <= lastplusoff)
2267 start = lastplusoff + 1;
2271 if (start >= temp+units_per_sector && num_read) {
2272 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
2276 if (!num_read && start == temp) {
2279 saved_start = start;
2280 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2282 if (display_in_cyl_units) {
2283 start = (start - 1) * units_per_sector;
2284 if (start < saved_start) start = saved_start;
2288 } while (start != temp || !num_read);
2289 if (n > 4) { /* NOT for fifth partition */
2290 struct pte *pe = &ptes[n];
2292 pe->offset = start - sector_offset;
2293 if (pe->offset == extended_offset) { /* must be corrected */
2295 if (sector_offset == 1)
2300 for (i = 0; i < partitions; i++) {
2301 struct pte *pe = &ptes[i];
2303 if (start < pe->offset && limit >= pe->offset)
2304 limit = pe->offset - 1;
2305 if (start < first[i] && limit >= first[i])
2306 limit = first[i] - 1;
2308 if (start > limit) {
2309 printf(_("No free sectors available\n"));
2314 if (cround(start) == cround(limit)) {
2317 snprintf(mesg, sizeof(mesg),
2318 _("Last %s or +size or +sizeM or +sizeK"),
2319 str_units(SINGULAR));
2320 stop = read_int(cround(start), cround(limit), cround(limit),
2321 cround(start), mesg);
2322 if (display_in_cyl_units) {
2323 stop = stop * units_per_sector - 1;
2329 set_partition(n, 0, start, stop, sys);
2331 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2333 if (IS_EXTENDED(sys)) {
2334 struct pte *pe4 = &ptes[4];
2335 struct pte *pen = &ptes[n];
2338 pen->ext_pointer = p;
2339 pe4->offset = extended_offset = start;
2340 pe4->sectorbuffer = xzalloc(sector_size);
2341 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2342 pe4->ext_pointer = pe4->part_table + 1;
2351 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2352 struct pte *pe = &ptes[partitions];
2354 pe->sectorbuffer = xzalloc(sector_size);
2355 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2356 pe->ext_pointer = pe->part_table + 1;
2361 add_partition(partitions - 1, LINUX_NATIVE);
2367 int i, free_primary = 0;
2369 if (warn_geometry())
2373 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2377 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2381 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2382 "\n\tIf you want to add DOS-type partitions, create"
2383 "\n\ta new empty DOS partition table first. (Use o.)"
2385 "This will destroy the present disk contents.\n"));
2389 for (i = 0; i < 4; i++)
2390 free_primary += !ptes[i].part_table->sys_ind;
2392 if (!free_primary && partitions >= MAXIMUM_PARTS) {
2393 printf(_("The maximum number of partitions has been created\n"));
2397 if (!free_primary) {
2398 if (extended_offset)
2401 printf(_("You must delete some partition and add "
2402 "an extended partition first\n"));
2404 char c, line[LINE_LENGTH];
2405 snprintf(line, sizeof(line),
2408 " p primary partition (1-4)\n",
2410 "l logical (5 or over)" : "e extended"));
2412 c = read_nonempty(line);
2413 if (c == 'p' || c == 'P') {
2414 i = get_nonexisting_partition(0, 4);
2416 add_partition(i, LINUX_NATIVE);
2419 else if (c == 'l' && extended_offset) {
2423 else if (c == 'e' && !extended_offset) {
2424 i = get_nonexisting_partition(0, 4);
2426 add_partition(i, EXTENDED);
2430 printf(_("Invalid partition number "
2431 "for type '%c'\n"), c);
2442 for (i = 0; i < 3; i++)
2443 if (ptes[i].changed)
2444 ptes[3].changed = 1;
2445 for (i = 3; i < partitions; i++) {
2446 struct pte *pe = &ptes[i];
2449 write_part_table_flag(pe->sectorbuffer);
2450 write_sector(pe->offset, pe->sectorbuffer);
2454 else if (LABEL_IS_SGI) {
2455 /* no test on change? the printf below might be mistaken */
2458 else if (LABEL_IS_SUN) {
2461 for (i = 0; i < 8; i++)
2462 if (ptes[i].changed)
2468 printf(_("The partition table has been altered!\n\n"));
2469 reread_partition_table(1);
2473 reread_partition_table(int leave)
2477 printf(_("Calling ioctl() to re-read partition table\n"));
2479 sleep(2); /* Huh? */
2480 i = ioctl(fd, BLKRRPART);
2483 /* some kernel versions (1.2.x) seem to have trouble
2484 rereading the partition table, but if asked to do it
2485 twice, the second time works. - biro@yggdrasil.com */
2488 i = ioctl(fd, BLKRRPART);
2493 bb_perror_msg("WARNING: rereading partition table "
2494 "failed, kernel still uses old table");
2500 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2501 "partitions, please see the fdisk manual page for additional\n"
2506 if (ENABLE_FEATURE_CLEAN_UP)
2511 #endif /* FEATURE_FDISK_WRITABLE */
2513 #if ENABLE_FEATURE_FDISK_ADVANCED
2514 #define MAX_PER_LINE 16
2516 print_buffer(char *pbuffer)
2520 for (i = 0, l = 0; i < sector_size; i++, l++) {
2522 printf("0x%03X:", i);
2523 printf(" %02X", (unsigned char) pbuffer[i]);
2524 if (l == MAX_PER_LINE - 1) {
2539 printf(_("Device: %s\n"), disk_device);
2540 if (LABEL_IS_SGI || LABEL_IS_SUN)
2541 print_buffer(MBRbuffer);
2543 for (i = 3; i < partitions; i++)
2544 print_buffer(ptes[i].sectorbuffer);
2551 struct pte *pe = &ptes[i];
2552 struct partition *p = pe->part_table;
2555 if (warn_geometry())
2557 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
2558 printf(_("Partition %d has no data area\n"), i + 1);
2561 first = get_partition_start(pe);
2562 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
2563 _("New beginning of data")) - pe->offset;
2565 if (new != get_nr_sects(p)) {
2566 first = get_nr_sects(p) + get_start_sect(p) - new;
2567 set_nr_sects(p, first);
2568 set_start_sect(p, new);
2580 c = tolower(read_nonempty(_("Expert command (m for help): ")));
2588 move_begin(get_partition(0, partitions));
2591 user_cylinders = cylinders =
2592 read_int(1, cylinders, 1048576, 0,
2593 _("Number of cylinders"));
2595 sun_set_ncyl(cylinders);
2605 else if (LABEL_IS_SUN)
2607 else if (LABEL_IS_DOS)
2612 fix_partition_table_order();
2615 #if ENABLE_FEATURE_SGI_LABEL
2620 user_heads = heads = read_int(1, heads, 256, 0,
2621 _("Number of heads"));
2645 user_sectors = sectors = read_int(1, sectors, 63, 0,
2646 _("Number of sectors"));
2647 if (dos_compatible_flag) {
2648 sector_offset = sectors;
2649 printf(_("Warning: setting sector offset for DOS "
2658 write_table(); /* does not return */
2662 sun_set_pcylcount();
2669 #endif /* ADVANCED mode */
2672 is_ide_cdrom_or_tape(const char *device)
2676 struct stat statbuf;
2679 /* No device was given explicitly, and we are trying some
2680 likely things. But opening /dev/hdc may produce errors like
2681 "hdc: tray open or drive not ready"
2682 if it happens to be a CD-ROM drive. It even happens that
2683 the process hangs on the attempt to read a music CD.
2684 So try to be careful. This only works since 2.1.73. */
2686 if (strncmp("/dev/hd", device, 7))
2689 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2690 procf = fopen(buf, "r");
2691 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2692 is_ide = (!strncmp(buf, "cdrom", 5) ||
2693 !strncmp(buf, "tape", 4));
2695 /* Now when this proc file does not exist, skip the
2696 device when it is read-only. */
2697 if (stat(device, &statbuf) == 0)
2698 is_ide = ((statbuf.st_mode & 0222) == 0);
2707 trydev(const char *device, int user_specified)
2711 disk_device = device;
2712 if (setjmp(listingbuf))
2714 if (!user_specified)
2715 if (is_ide_cdrom_or_tape(device))
2717 fd = open(disk_device, type_open);
2719 gb = get_boot(try_only);
2720 if (gb > 0) { /* I/O error */
2722 } else if (gb < 0) { /* no DOS signature */
2723 list_disk_geometry();
2727 #if ENABLE_FEATURE_OSF_LABEL
2728 if (bsd_trydev(device) < 0)
2730 printf(_("Disk %s doesn't contain a valid "
2731 "partition table\n"), device);
2736 #if ENABLE_FEATURE_FDISK_WRITABLE
2737 if (!LABEL_IS_SUN && partitions > 4){
2738 delete_partition(ext_index);
2743 /* Ignore other errors, since we try IDE
2744 and SCSI hard disks which may not be
2745 installed on the system. */
2746 if (errno == EACCES) {
2747 printf(_("Cannot open %s\n"), device);
2753 /* for fdisk -l: try all things in /proc/partitions
2754 that look like a partition name (do not end in a digit) */
2759 char line[100], ptname[100], devname[120], *s;
2762 procpt = fopen_or_warn("/proc/partitions", "r");
2764 while (fgets(line, sizeof(line), procpt)) {
2765 if (sscanf(line, " %d %d %d %[^\n ]",
2766 &ma, &mi, &sz, ptname) != 4)
2768 for (s = ptname; *s; s++);
2771 sprintf(devname, "/dev/%s", ptname);
2774 #if ENABLE_FEATURE_CLEAN_UP
2779 #if ENABLE_FEATURE_FDISK_WRITABLE
2781 unknown_command(int c)
2783 printf(_("%c: unknown command\n"), c);
2787 int fdisk_main(int argc, char **argv);
2788 int fdisk_main(int argc, char **argv)
2790 char *str_b, *str_C, *str_H, *str_S;
2794 * fdisk -l [-b sectorsize] [-u] device ...
2795 * fdisk -s [partition] ...
2796 * fdisk [-b sectorsize] [-u] device
2798 * Options -C, -H, -S set the geometry.
2807 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2809 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2810 &str_b, &str_C, &str_H, &str_S);
2813 if (opt & OPT_b) { // -b
2814 /* Ugly: this sector size is really per device,
2815 so cannot be combined with multiple disks,
2816 and the same goes for the C/H/S options.
2818 sector_size = xatoi_u(str_b);
2819 if (sector_size != 512 && sector_size != 1024 &&
2820 sector_size != 2048)
2823 user_set_sector_size = 1;
2825 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2826 if (opt & OPT_H) { // -H
2827 user_heads = xatoi_u(str_H);
2828 if (user_heads <= 0 || user_heads >= 256)
2831 //if (opt & OPT_l) // -l
2832 if (opt & OPT_S) { // -S
2833 user_sectors = xatoi_u(str_S);
2834 if (user_sectors <= 0 || user_sectors >= 64)
2837 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2838 //if (opt & OPT_s) // -s
2840 if (user_set_sector_size && argc != 1)
2841 printf(_("Warning: the -b (set sector size) option should"
2842 " be used with one specified device\n"));
2844 #if ENABLE_FEATURE_FDISK_WRITABLE
2848 type_open = O_RDONLY;
2851 #if defined(__GNUC__)
2852 /* avoid gcc warning:
2853 variable `k' might be clobbered by `longjmp' */
2857 for (k = 0; k < argc; k++)
2860 /* we no longer have default device names */
2861 /* but, we can use /proc/partitions instead */
2865 #if ENABLE_FEATURE_FDISK_WRITABLE
2869 #if ENABLE_FEATURE_FDISK_BLKSIZE
2875 type_open = O_RDONLY;
2880 for (j = 0; j < argc; j++) {
2881 disk_device = argv[j];
2882 fd = open(disk_device, type_open);
2884 fdisk_fatal(unable_to_open);
2885 if (ioctl(fd, BLKGETSIZE, &size))
2886 fdisk_fatal(ioctl_error);
2889 printf("%ld\n", size/2);
2891 printf("%s: %ld\n", argv[j], size/2);
2897 #if ENABLE_FEATURE_FDISK_WRITABLE
2901 disk_device = argv[0];
2905 /* OSF label, and no DOS label */
2906 printf(_("Detected an OSF/1 disklabel on %s, entering "
2907 "disklabel mode.\n"), disk_device);
2909 /*Why do we do this? It seems to be counter-intuitive*/
2910 current_label_type = label_dos;
2911 /* If we return we may want to make an empty DOS label? */
2917 c = tolower(read_nonempty(_("Command (m for help): ")));
2921 toggle_active(get_partition(1, partitions));
2922 else if (LABEL_IS_SUN)
2923 toggle_sunflags(get_partition(1, partitions),
2925 else if (LABEL_IS_SGI)
2926 sgi_set_bootpartition(
2927 get_partition(1, partitions));
2933 printf(_("\nThe current boot file is: %s\n"),
2934 sgi_get_bootfile());
2935 if (read_maybe_empty(_("Please enter the name of the "
2936 "new boot file: ")) == '\n')
2937 printf(_("Boot file unchanged\n"));
2939 sgi_set_bootfile(line_ptr);
2941 #if ENABLE_FEATURE_OSF_LABEL
2948 toggle_dos_compatibility_flag();
2949 else if (LABEL_IS_SUN)
2950 toggle_sunflags(get_partition(1, partitions),
2952 else if (LABEL_IS_SGI)
2953 sgi_set_swappartition(
2954 get_partition(1, partitions));
2961 /* If sgi_label then don't use get_existing_partition,
2962 let the user select a partition, since
2963 get_existing_partition() only works for Linux-like
2965 if (!LABEL_IS_SGI) {
2966 j = get_existing_partition(1, partitions);
2968 j = get_partition(1, partitions);
2971 delete_partition(j);
2980 list_types(get_sys_types());
2999 #if ENABLE_FEATURE_SUN_LABEL
3013 write_table(); /* does not return */
3015 #if ENABLE_FEATURE_FDISK_ADVANCED
3018 printf(_("\n\tSorry, no experts menu for SGI "
3019 "partition tables available.\n\n"));
3030 #endif /* FEATURE_FDISK_WRITABLE */