Big cleanup in config help and description
[oweals/busybox.git] / util-linux / fdisk.c
1 /* vi: set sw=4 ts=4: */
2 /* fdisk.c -- Partition table manipulator for Linux.
3  *
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)
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 //config:config FDISK
10 //config:       bool "fdisk"
11 //config:       default y
12 //config:       select PLATFORM_LINUX
13 //config:       help
14 //config:         The fdisk utility is used to divide hard disks into one or more
15 //config:         logical disks, which are generally called partitions. This utility
16 //config:         can be used to list and edit the set of partitions or BSD style
17 //config:         'disk slices' that are defined on a hard drive.
18 //config:
19 //config:config FDISK_SUPPORT_LARGE_DISKS
20 //config:       bool "Support over 4GB disks"
21 //config:       default y
22 //config:       depends on FDISK
23 //config:       depends on !LFS   # with LFS no special code is needed
24 //config:
25 //config:config FEATURE_FDISK_WRITABLE
26 //config:       bool "Write support"
27 //config:       default y
28 //config:       depends on FDISK
29 //config:       help
30 //config:         Enabling this option allows you to create or change a partition table
31 //config:         and write those changes out to disk. If you leave this option
32 //config:         disabled, you will only be able to view the partition table.
33 //config:
34 //config:config FEATURE_AIX_LABEL
35 //config:       bool "Support AIX disklabels"
36 //config:       default n
37 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
38 //config:       help
39 //config:         Enabling this option allows you to create or change AIX disklabels.
40 //config:         Most people can safely leave this option disabled.
41 //config:
42 //config:config FEATURE_SGI_LABEL
43 //config:       bool "Support SGI disklabels"
44 //config:       default n
45 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
46 //config:       help
47 //config:         Enabling this option allows you to create or change SGI disklabels.
48 //config:         Most people can safely leave this option disabled.
49 //config:
50 //config:config FEATURE_SUN_LABEL
51 //config:       bool "Support SUN disklabels"
52 //config:       default n
53 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
54 //config:       help
55 //config:         Enabling this option allows you to create or change SUN disklabels.
56 //config:         Most people can safely leave this option disabled.
57 //config:
58 //config:config FEATURE_OSF_LABEL
59 //config:       bool "Support BSD disklabels"
60 //config:       default n
61 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
62 //config:       help
63 //config:         Enabling this option allows you to create or change BSD disklabels
64 //config:         and define and edit BSD disk slices.
65 //config:
66 //config:config FEATURE_GPT_LABEL
67 //config:       bool "Support GPT disklabels"
68 //config:       default n
69 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
70 //config:       help
71 //config:         Enabling this option allows you to view GUID Partition Table
72 //config:         disklabels.
73 //config:
74 //config:config FEATURE_FDISK_ADVANCED
75 //config:       bool "Support expert mode"
76 //config:       default y
77 //config:       depends on FDISK && FEATURE_FDISK_WRITABLE
78 //config:       help
79 //config:         Enabling this option allows you to do terribly unsafe things like
80 //config:         define arbitrary drive geometry, move the beginning of data in a
81 //config:         partition, and similarly evil things. Unless you have a very good
82 //config:         reason you would be wise to leave this disabled.
83
84 //applet:IF_FDISK(APPLET(fdisk, BB_DIR_SBIN, BB_SUID_DROP))
85
86 //kbuild:lib-$(CONFIG_FDISK) += fdisk.o
87
88 /* Looks like someone forgot to add this to config system */
89 //usage:#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
90 //usage:# define ENABLE_FEATURE_FDISK_BLKSIZE 0
91 //usage:# define IF_FEATURE_FDISK_BLKSIZE(a)
92 //usage:#endif
93 //usage:
94 //usage:#define fdisk_trivial_usage
95 //usage:       "[-ul" IF_FEATURE_FDISK_BLKSIZE("s") "] "
96 //usage:       "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK"
97 //usage:#define fdisk_full_usage "\n\n"
98 //usage:       "Change partition table\n"
99 //usage:     "\n        -u              Start and End are in sectors (instead of cylinders)"
100 //usage:     "\n        -l              Show partition table for each DISK, then exit"
101 //usage:        IF_FEATURE_FDISK_BLKSIZE(
102 //usage:     "\n        -s              Show partition sizes in kb for each DISK, then exit"
103 //usage:        )
104 //usage:     "\n        -b 2048         (for certain MO disks) use 2048-byte sectors"
105 //usage:     "\n        -C CYLINDERS    Set number of cylinders/heads/sectors"
106 //usage:     "\n        -H HEADS"
107 //usage:     "\n        -S SECTORS"
108
109 #ifndef _LARGEFILE64_SOURCE
110 /* For lseek64 */
111 # define _LARGEFILE64_SOURCE
112 #endif
113 #include <assert.h>             /* assert */
114 #include <sys/mount.h>
115 #if !defined(BLKSSZGET)
116 # define BLKSSZGET _IO(0x12, 104)
117 #endif
118 #if !defined(BLKGETSIZE64)
119 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
120 #endif
121 #include "libbb.h"
122 #include "unicode.h"
123
124 #if BB_LITTLE_ENDIAN
125 # define inline_if_little_endian ALWAYS_INLINE
126 #else
127 # define inline_if_little_endian /* nothing */
128 #endif
129
130
131 /* Looks like someone forgot to add this to config system */
132 #ifndef ENABLE_FEATURE_FDISK_BLKSIZE
133 # define ENABLE_FEATURE_FDISK_BLKSIZE 0
134 # define IF_FEATURE_FDISK_BLKSIZE(a)
135 #endif
136
137 #define DEFAULT_SECTOR_SIZE      512
138 #define DEFAULT_SECTOR_SIZE_STR "512"
139 #define MAX_SECTOR_SIZE         2048
140 #define SECTOR_SIZE              512 /* still used in osf/sgi/sun code */
141 #define MAXIMUM_PARTS             60
142
143 #define ACTIVE_FLAG             0x80
144
145 #define EXTENDED                0x05
146 #define WIN98_EXTENDED          0x0f
147 #define LINUX_PARTITION         0x81
148 #define LINUX_SWAP              0x82
149 #define LINUX_NATIVE            0x83
150 #define LINUX_EXTENDED          0x85
151 #define LINUX_LVM               0x8e
152 #define LINUX_RAID              0xfd
153
154
155 enum {
156         OPT_b = 1 << 0,
157         OPT_C = 1 << 1,
158         OPT_H = 1 << 2,
159         OPT_l = 1 << 3,
160         OPT_S = 1 << 4,
161         OPT_u = 1 << 5,
162         OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
163 };
164
165
166 typedef unsigned long long ullong;
167 /* Used for sector numbers. Partition formats we know
168  * do not support more than 2^32 sectors
169  */
170 typedef uint32_t sector_t;
171 #if UINT_MAX == 4294967295
172 # define SECT_FMT ""
173 #elif ULONG_MAX == 4294967295
174 # define SECT_FMT "l"
175 #else
176 # error Cant detect sizeof(uint32_t)
177 #endif
178
179 struct hd_geometry {
180         unsigned char heads;
181         unsigned char sectors;
182         unsigned short cylinders;
183         unsigned long start;
184 };
185
186 #define HDIO_GETGEO     0x0301  /* get device geometry */
187
188 static const char msg_building_new_label[] ALIGN1 =
189 "Building a new %s. Changes will remain in memory only,\n"
190 "until you decide to write them. After that the previous content\n"
191 "won't be recoverable.\n\n";
192
193 static const char msg_part_already_defined[] ALIGN1 =
194 "Partition %u is already defined, delete it before re-adding\n";
195
196
197 struct partition {
198         unsigned char boot_ind;         /* 0x80 - active */
199         unsigned char head;             /* starting head */
200         unsigned char sector;           /* starting sector */
201         unsigned char cyl;              /* starting cylinder */
202         unsigned char sys_ind;          /* what partition type */
203         unsigned char end_head;         /* end head */
204         unsigned char end_sector;       /* end sector */
205         unsigned char end_cyl;          /* end cylinder */
206         unsigned char start4[4];        /* starting sector counting from 0 */
207         unsigned char size4[4];         /* nr of sectors in partition */
208 } PACKED;
209
210 /*
211  * per partition table entry data
212  *
213  * The four primary partitions have the same sectorbuffer (MBRbuffer)
214  * and have NULL ext_pointer.
215  * Each logical partition table entry has two pointers, one for the
216  * partition and one link to the next one.
217  */
218 struct pte {
219         struct partition *part_table;   /* points into sectorbuffer */
220         struct partition *ext_pointer;  /* points into sectorbuffer */
221         sector_t offset_from_dev_start; /* disk sector number */
222         char *sectorbuffer;             /* disk sector contents */
223 #if ENABLE_FEATURE_FDISK_WRITABLE
224         char changed;                   /* boolean */
225 #endif
226 };
227
228 #define unable_to_open "can't open '%s'"
229 #define unable_to_read "can't read from %s"
230 #define unable_to_seek "can't seek on %s"
231
232 enum label_type {
233         LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF, LABEL_GPT
234 };
235
236 #define LABEL_IS_DOS    (LABEL_DOS == current_label_type)
237
238 #if ENABLE_FEATURE_SUN_LABEL
239 #define LABEL_IS_SUN    (LABEL_SUN == current_label_type)
240 #define STATIC_SUN static
241 #else
242 #define LABEL_IS_SUN    0
243 #define STATIC_SUN extern
244 #endif
245
246 #if ENABLE_FEATURE_SGI_LABEL
247 #define LABEL_IS_SGI    (LABEL_SGI == current_label_type)
248 #define STATIC_SGI static
249 #else
250 #define LABEL_IS_SGI    0
251 #define STATIC_SGI extern
252 #endif
253
254 #if ENABLE_FEATURE_AIX_LABEL
255 #define LABEL_IS_AIX    (LABEL_AIX == current_label_type)
256 #define STATIC_AIX static
257 #else
258 #define LABEL_IS_AIX    0
259 #define STATIC_AIX extern
260 #endif
261
262 #if ENABLE_FEATURE_OSF_LABEL
263 #define LABEL_IS_OSF    (LABEL_OSF == current_label_type)
264 #define STATIC_OSF static
265 #else
266 #define LABEL_IS_OSF    0
267 #define STATIC_OSF extern
268 #endif
269
270 #if ENABLE_FEATURE_GPT_LABEL
271 #define LABEL_IS_GPT    (LABEL_GPT == current_label_type)
272 #define STATIC_GPT static
273 #else
274 #define LABEL_IS_GPT    0
275 #define STATIC_GPT extern
276 #endif
277
278 enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
279
280 static void update_units(void);
281 #if ENABLE_FEATURE_FDISK_WRITABLE
282 static void change_units(void);
283 static void reread_partition_table(int leave);
284 static void delete_partition(int i);
285 static unsigned get_partition(int warn, unsigned max);
286 static void list_types(const char *const *sys);
287 static sector_t read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg);
288 #endif
289 static const char *partition_type(unsigned char type);
290 static void get_geometry(void);
291 static void read_pte(struct pte *pe, sector_t offset);
292 #if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
293 static int get_boot(enum action what);
294 #else
295 static int get_boot(void);
296 #endif
297
298 #define PLURAL   0
299 #define SINGULAR 1
300
301 static sector_t get_start_sect(const struct partition *p);
302 static sector_t get_nr_sects(const struct partition *p);
303
304 /* DOS partition types */
305
306 static const char *const i386_sys_types[] = {
307         "\x00" "Empty",
308         "\x01" "FAT12",
309         "\x04" "FAT16 <32M",
310         "\x05" "Extended",         /* DOS 3.3+ extended partition */
311         "\x06" "FAT16",            /* DOS 16-bit >=32M */
312         "\x07" "HPFS/NTFS",        /* OS/2 IFS, eg, HPFS or NTFS or QNX */
313         "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
314         "\x0b" "Win95 FAT32",
315         "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
316         "\x0e" "Win95 FAT16 (LBA)",
317         "\x0f" "Win95 Ext'd (LBA)",
318         "\x11" "Hidden FAT12",
319         "\x12" "Compaq diagnostics",
320         "\x14" "Hidden FAT16 <32M",
321         "\x16" "Hidden FAT16",
322         "\x17" "Hidden HPFS/NTFS",
323         "\x1b" "Hidden Win95 FAT32",
324         "\x1c" "Hidden W95 FAT32 (LBA)",
325         "\x1e" "Hidden W95 FAT16 (LBA)",
326         "\x3c" "Part.Magic recovery",
327         "\x41" "PPC PReP Boot",
328         "\x42" "SFS",
329         "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
330         "\x80" "Old Minix",        /* Minix 1.4a and earlier */
331         "\x81" "Minix / old Linux",/* Minix 1.4b and later */
332         "\x82" "Linux swap",       /* also Solaris */
333         "\x83" "Linux",
334         "\x84" "OS/2 hidden C: drive",
335         "\x85" "Linux extended",
336         "\x86" "NTFS volume set",
337         "\x87" "NTFS volume set",
338         "\x8e" "Linux LVM",
339         "\x9f" "BSD/OS",           /* BSDI */
340         "\xa0" "Thinkpad hibernation",
341         "\xa5" "FreeBSD",          /* various BSD flavours */
342         "\xa6" "OpenBSD",
343         "\xa8" "Darwin UFS",
344         "\xa9" "NetBSD",
345         "\xab" "Darwin boot",
346         "\xb7" "BSDI fs",
347         "\xb8" "BSDI swap",
348         "\xbe" "Solaris boot",
349         "\xeb" "BeOS fs",
350         "\xee" "EFI GPT",                    /* Intel EFI GUID Partition Table */
351         "\xef" "EFI (FAT-12/16/32)",         /* Intel EFI System Partition */
352         "\xf0" "Linux/PA-RISC boot",         /* Linux/PA-RISC boot loader */
353         "\xf2" "DOS secondary",              /* DOS 3.3+ secondary */
354         "\xfd" "Linux raid autodetect",      /* New (2.2.x) raid partition with
355                                                 autodetect using persistent
356                                                 superblock */
357 #if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
358         "\x02" "XENIX root",
359         "\x03" "XENIX usr",
360         "\x08" "AIX",              /* AIX boot (AIX -- PS/2 port) or SplitDrive */
361         "\x09" "AIX bootable",     /* AIX data or Coherent */
362         "\x10" "OPUS",
363         "\x18" "AST SmartSleep",
364         "\x24" "NEC DOS",
365         "\x39" "Plan 9",
366         "\x40" "Venix 80286",
367         "\x4d" "QNX4.x",
368         "\x4e" "QNX4.x 2nd part",
369         "\x4f" "QNX4.x 3rd part",
370         "\x50" "OnTrack DM",
371         "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
372         "\x52" "CP/M",             /* CP/M or Microport SysV/AT */
373         "\x53" "OnTrack DM6 Aux3",
374         "\x54" "OnTrackDM6",
375         "\x55" "EZ-Drive",
376         "\x56" "Golden Bow",
377         "\x5c" "Priam Edisk",
378         "\x61" "SpeedStor",
379         "\x64" "Novell Netware 286",
380         "\x65" "Novell Netware 386",
381         "\x70" "DiskSecure Multi-Boot",
382         "\x75" "PC/IX",
383         "\x93" "Amoeba",
384         "\x94" "Amoeba BBT",       /* (bad block table) */
385         "\xa7" "NeXTSTEP",
386         "\xbb" "Boot Wizard hidden",
387         "\xc1" "DRDOS/sec (FAT-12)",
388         "\xc4" "DRDOS/sec (FAT-16 < 32M)",
389         "\xc6" "DRDOS/sec (FAT-16)",
390         "\xc7" "Syrinx",
391         "\xda" "Non-FS data",
392         "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
393                                       Concurrent DOS or CTOS */
394         "\xde" "Dell Utility",     /* Dell PowerEdge Server utilities */
395         "\xdf" "BootIt",           /* BootIt EMBRM */
396         "\xe1" "DOS access",       /* DOS access or SpeedStor 12-bit FAT
397                                       extended partition */
398         "\xe3" "DOS R/O",          /* DOS R/O or SpeedStor */
399         "\xe4" "SpeedStor",        /* SpeedStor 16-bit FAT extended
400                                       partition < 1024 cyl. */
401         "\xf1" "SpeedStor",
402         "\xf4" "SpeedStor",        /* SpeedStor large partition */
403         "\xfe" "LANstep",          /* SpeedStor >1024 cyl. or LANstep */
404         "\xff" "BBT",              /* Xenix Bad Block Table */
405 #endif
406         NULL
407 };
408
409 enum {
410         dev_fd = 3                  /* the disk */
411 };
412
413 /* Globals */
414 struct globals {
415         char *line_ptr;
416
417         const char *disk_device;
418         int g_partitions; // = 4;       /* maximum partition + 1 */
419         unsigned units_per_sector; // = 1;
420         unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
421         unsigned user_set_sector_size;
422         unsigned sector_offset; // = 1;
423         unsigned g_heads, g_sectors, g_cylinders;
424         smallint /* enum label_type */ current_label_type;
425         smallint display_in_cyl_units; // = 1;
426 #if ENABLE_FEATURE_OSF_LABEL
427         smallint possibly_osf_label;
428 #endif
429
430         smallint listing;               /* no aborts for fdisk -l */
431         smallint dos_compatible_flag; // = 1;
432 #if ENABLE_FEATURE_FDISK_WRITABLE
433         //int dos_changed;
434         smallint nowarn;                /* no warnings for fdisk -l/-s */
435 #endif
436         int ext_index;                  /* the prime extended partition */
437         unsigned user_cylinders, user_heads, user_sectors;
438         unsigned pt_heads, pt_sectors;
439         unsigned kern_heads, kern_sectors;
440         sector_t extended_offset;       /* offset of link pointers */
441         sector_t total_number_of_sectors;
442
443         jmp_buf listingbuf;
444         char line_buffer[80];
445         /* Raw disk label. For DOS-type partition tables the MBR,
446          * with descriptions of the primary partitions. */
447         char MBRbuffer[MAX_SECTOR_SIZE];
448         /* Partition tables */
449         struct pte ptes[MAXIMUM_PARTS];
450 };
451 #define G (*ptr_to_globals)
452 #define line_ptr             (G.line_ptr            )
453 #define disk_device          (G.disk_device         )
454 #define g_partitions         (G.g_partitions        )
455 #define units_per_sector     (G.units_per_sector    )
456 #define sector_size          (G.sector_size         )
457 #define user_set_sector_size (G.user_set_sector_size)
458 #define sector_offset        (G.sector_offset       )
459 #define g_heads              (G.g_heads             )
460 #define g_sectors            (G.g_sectors           )
461 #define g_cylinders          (G.g_cylinders         )
462 #define current_label_type   (G.current_label_type  )
463 #define display_in_cyl_units (G.display_in_cyl_units)
464 #define possibly_osf_label   (G.possibly_osf_label  )
465 #define listing                 (G.listing                )
466 #define dos_compatible_flag     (G.dos_compatible_flag    )
467 #define nowarn                  (G.nowarn                 )
468 #define ext_index               (G.ext_index              )
469 #define user_cylinders          (G.user_cylinders         )
470 #define user_heads              (G.user_heads             )
471 #define user_sectors            (G.user_sectors           )
472 #define pt_heads                (G.pt_heads               )
473 #define pt_sectors              (G.pt_sectors             )
474 #define kern_heads              (G.kern_heads             )
475 #define kern_sectors            (G.kern_sectors           )
476 #define extended_offset         (G.extended_offset        )
477 #define total_number_of_sectors (G.total_number_of_sectors)
478 #define listingbuf      (G.listingbuf     )
479 #define line_buffer     (G.line_buffer    )
480 #define MBRbuffer       (G.MBRbuffer      )
481 #define ptes            (G.ptes           )
482 #define INIT_G() do { \
483         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
484         sector_size = DEFAULT_SECTOR_SIZE; \
485         sector_offset = 1; \
486         g_partitions = 4; \
487         display_in_cyl_units = 1; \
488         units_per_sector = 1; \
489         dos_compatible_flag = 1; \
490 } while (0)
491
492
493 /* TODO: move to libbb? */
494 /* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
495  * disks > 2^32 sectors
496  */
497 static sector_t bb_BLKGETSIZE_sectors(int fd)
498 {
499         uint64_t v64;
500         unsigned long longsectors;
501
502         if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
503                 /* Got bytes, convert to 512 byte sectors */
504                 v64 >>= 9;
505                 if (v64 != (sector_t)v64) {
506  ret_trunc:
507                         /* Not only DOS, but all other partition tables
508                          * we support can't record more than 32 bit
509                          * sector counts or offsets
510                          */
511                         bb_error_msg("device has more than 2^32 sectors, can't use all of them");
512                         v64 = (uint32_t)-1L;
513                 }
514                 return v64;
515         }
516         /* Needs temp of type long */
517         if (ioctl(fd, BLKGETSIZE, &longsectors)) {
518                 /* Perhaps this is a disk image */
519                 off_t sz = lseek(fd, 0, SEEK_END);
520                 longsectors = 0;
521                 if (sz > 0)
522                         longsectors = (uoff_t)sz / sector_size;
523                 lseek(fd, 0, SEEK_SET);
524         }
525         if (sizeof(long) > sizeof(sector_t)
526          && longsectors != (sector_t)longsectors
527         ) {
528                 goto ret_trunc;
529         }
530         return longsectors;
531 }
532
533
534 #define IS_EXTENDED(i) \
535         ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
536
537 #define cround(n)       (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
538
539 #define scround(x)      (((x)+units_per_sector-1)/units_per_sector)
540
541 #define pt_offset(b, n) \
542         ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
543
544 #define sector(s)       ((s) & 0x3f)
545
546 #define cylinder(s, c)  ((c) | (((s) & 0xc0) << 2))
547
548 static void
549 close_dev_fd(void)
550 {
551         /* Not really closing, but making sure it is open, and to harmless place */
552         xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
553 }
554
555 /* Return partition name */
556 static const char *
557 partname(const char *dev, int pno, int lth)
558 {
559         const char *p;
560         int w, wp;
561         int bufsiz;
562         char *bufp;
563
564         bufp = auto_string(xzalloc(80));
565         bufsiz = 80;
566
567         w = strlen(dev);
568         p = "";
569
570         if (isdigit(dev[w-1]))
571                 p = "p";
572
573         /* devfs kludge - note: fdisk partition names are not supposed
574            to equal kernel names, so there is no reason to do this */
575         if (strcmp(dev + w - 4, "disc") == 0) {
576                 w -= 4;
577                 p = "part";
578         }
579
580         wp = strlen(p);
581
582         if (lth) {
583                 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
584                         lth-wp-2, w, dev, p, pno);
585         } else {
586                 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
587         }
588         return bufp;
589 }
590
591 static ALWAYS_INLINE struct partition *
592 get_part_table(int i)
593 {
594         return ptes[i].part_table;
595 }
596
597 static const char *
598 str_units(int n)
599 {      /* n==1: use singular */
600         if (n == 1)
601                 return display_in_cyl_units ? "cylinder" : "sector";
602         return display_in_cyl_units ? "cylinders" : "sectors";
603 }
604
605 static int
606 valid_part_table_flag(const char *mbuffer)
607 {
608         return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
609 }
610
611 static void fdisk_fatal(const char *why)
612 {
613         if (listing) {
614                 close_dev_fd();
615                 longjmp(listingbuf, 1);
616         }
617         bb_error_msg_and_die(why, disk_device);
618 }
619
620 static void
621 seek_sector(sector_t secno)
622 {
623 #if ENABLE_FDISK_SUPPORT_LARGE_DISKS
624         off64_t off = (off64_t)secno * sector_size;
625         if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
626                 fdisk_fatal(unable_to_seek);
627 #else
628         uint64_t off = (uint64_t)secno * sector_size;
629         if (off > MAXINT(off_t)
630          || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
631         ) {
632                 fdisk_fatal(unable_to_seek);
633         }
634 #endif
635 }
636
637 #if ENABLE_FEATURE_FDISK_WRITABLE
638 /* Read line; return 0 or first printable char */
639 static int
640 read_line(const char *prompt)
641 {
642         int sz;
643
644         sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer), /*timeout*/ -1);
645         if (sz <= 0)
646                 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
647
648         if (line_buffer[sz-1] == '\n')
649                 line_buffer[--sz] = '\0';
650
651         line_ptr = line_buffer;
652         while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
653                 line_ptr++;
654         return *line_ptr;
655 }
656
657 static void
658 set_all_unchanged(void)
659 {
660         int i;
661
662         for (i = 0; i < MAXIMUM_PARTS; i++)
663                 ptes[i].changed = 0;
664 }
665
666 static ALWAYS_INLINE void
667 set_changed(int i)
668 {
669         ptes[i].changed = 1;
670 }
671
672 static ALWAYS_INLINE void
673 write_part_table_flag(char *b)
674 {
675         b[510] = 0x55;
676         b[511] = 0xaa;
677 }
678
679 static char
680 read_nonempty(const char *mesg)
681 {
682         while (!read_line(mesg))
683                 continue;
684         return *line_ptr;
685 }
686
687 static char
688 read_maybe_empty(const char *mesg)
689 {
690         if (!read_line(mesg)) {
691                 line_ptr = line_buffer;
692                 line_ptr[0] = '\n';
693                 line_ptr[1] = '\0';
694         }
695         return line_ptr[0];
696 }
697
698 static int
699 read_hex(const char *const *sys)
700 {
701         unsigned long v;
702         while (1) {
703                 read_nonempty("Hex code (type L to list codes): ");
704                 if ((line_ptr[0] | 0x20) == 'l') {
705                         list_types(sys);
706                         continue;
707                 }
708                 v = bb_strtoul(line_ptr, NULL, 16);
709                 if (v <= 0xff)
710                         return v;
711         }
712 }
713
714 static void
715 write_sector(sector_t secno, const void *buf)
716 {
717         seek_sector(secno);
718         xwrite(dev_fd, buf, sector_size);
719 }
720 #endif /* FEATURE_FDISK_WRITABLE */
721
722
723 #include "fdisk_aix.c"
724
725 struct sun_partition {
726         unsigned char info[128];   /* Informative text string */
727         unsigned char spare0[14];
728         struct sun_info {
729                 unsigned char spare1;
730                 unsigned char id;
731                 unsigned char spare2;
732                 unsigned char flags;
733         } infos[8];
734         unsigned char spare1[246]; /* Boot information etc. */
735         unsigned short rspeed;     /* Disk rotational speed */
736         unsigned short pcylcount;  /* Physical cylinder count */
737         unsigned short sparecyl;   /* extra sects per cylinder */
738         unsigned char spare2[4];   /* More magic... */
739         unsigned short ilfact;     /* Interleave factor */
740         unsigned short ncyl;       /* Data cylinder count */
741         unsigned short nacyl;      /* Alt. cylinder count */
742         unsigned short ntrks;      /* Tracks per cylinder */
743         unsigned short nsect;      /* Sectors per track */
744         unsigned char spare3[4];   /* Even more magic... */
745         struct sun_partinfo {
746                 uint32_t start_cylinder;
747                 uint32_t num_sectors;
748         } partitions[8];
749         unsigned short magic;      /* Magic number */
750         unsigned short csum;       /* Label xor'd checksum */
751 } FIX_ALIASING;
752 typedef struct sun_partition sun_partition;
753 #define sunlabel ((sun_partition *)MBRbuffer)
754 STATIC_OSF void bsd_select(void);
755 STATIC_OSF void xbsd_print_disklabel(int);
756 #include "fdisk_osf.c"
757
758 STATIC_GPT void gpt_list_table(int xtra);
759 #include "fdisk_gpt.c"
760
761 #if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
762 static uint16_t
763 fdisk_swap16(uint16_t x)
764 {
765         return (x << 8) | (x >> 8);
766 }
767
768 static uint32_t
769 fdisk_swap32(uint32_t x)
770 {
771         return (x << 24) |
772                ((x & 0xFF00) << 8) |
773                ((x & 0xFF0000) >> 8) |
774                (x >> 24);
775 }
776 #endif
777
778 STATIC_SGI const char *const sgi_sys_types[];
779 STATIC_SGI unsigned sgi_get_num_sectors(int i);
780 STATIC_SGI int sgi_get_sysid(int i);
781 STATIC_SGI void sgi_delete_partition(int i);
782 STATIC_SGI void sgi_change_sysid(int i, int sys);
783 STATIC_SGI void sgi_list_table(int xtra);
784 #if ENABLE_FEATURE_FDISK_ADVANCED
785 STATIC_SGI void sgi_set_xcyl(void);
786 #endif
787 STATIC_SGI int verify_sgi(int verbose);
788 STATIC_SGI void sgi_add_partition(int n, int sys);
789 STATIC_SGI void sgi_set_swappartition(int i);
790 STATIC_SGI const char *sgi_get_bootfile(void);
791 STATIC_SGI void sgi_set_bootfile(const char* aFile);
792 STATIC_SGI void create_sgiinfo(void);
793 STATIC_SGI void sgi_write_table(void);
794 STATIC_SGI void sgi_set_bootpartition(int i);
795 #include "fdisk_sgi.c"
796
797 STATIC_SUN const char *const sun_sys_types[];
798 STATIC_SUN void sun_delete_partition(int i);
799 STATIC_SUN void sun_change_sysid(int i, int sys);
800 STATIC_SUN void sun_list_table(int xtra);
801 STATIC_SUN void add_sun_partition(int n, int sys);
802 #if ENABLE_FEATURE_FDISK_ADVANCED
803 STATIC_SUN void sun_set_alt_cyl(void);
804 STATIC_SUN void sun_set_ncyl(int cyl);
805 STATIC_SUN void sun_set_xcyl(void);
806 STATIC_SUN void sun_set_ilfact(void);
807 STATIC_SUN void sun_set_rspeed(void);
808 STATIC_SUN void sun_set_pcylcount(void);
809 #endif
810 STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
811 STATIC_SUN void verify_sun(void);
812 STATIC_SUN void sun_write_table(void);
813 #include "fdisk_sun.c"
814
815
816 static inline_if_little_endian unsigned
817 read4_little_endian(const unsigned char *cp)
818 {
819         uint32_t v;
820         move_from_unaligned32(v, cp);
821         return SWAP_LE32(v);
822 }
823
824 static sector_t
825 get_start_sect(const struct partition *p)
826 {
827         return read4_little_endian(p->start4);
828 }
829
830 static sector_t
831 get_nr_sects(const struct partition *p)
832 {
833         return read4_little_endian(p->size4);
834 }
835
836 #if ENABLE_FEATURE_FDISK_WRITABLE
837 /* start_sect and nr_sects are stored little endian on all machines */
838 /* moreover, they are not aligned correctly */
839 static inline_if_little_endian void
840 store4_little_endian(unsigned char *cp, unsigned val)
841 {
842         uint32_t v = SWAP_LE32(val);
843         move_to_unaligned32(cp, v);
844 }
845
846 static void
847 set_start_sect(struct partition *p, unsigned start_sect)
848 {
849         store4_little_endian(p->start4, start_sect);
850 }
851
852 static void
853 set_nr_sects(struct partition *p, unsigned nr_sects)
854 {
855         store4_little_endian(p->size4, nr_sects);
856 }
857 #endif
858
859 /* Allocate a buffer and read a partition table sector */
860 static void
861 read_pte(struct pte *pe, sector_t offset)
862 {
863         pe->offset_from_dev_start = offset;
864         pe->sectorbuffer = xzalloc(sector_size);
865         seek_sector(offset);
866         /* xread would make us abort - bad for fdisk -l */
867         if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
868                 fdisk_fatal(unable_to_read);
869 #if ENABLE_FEATURE_FDISK_WRITABLE
870         pe->changed = 0;
871 #endif
872         pe->part_table = pe->ext_pointer = NULL;
873 }
874
875 static sector_t
876 get_partition_start_from_dev_start(const struct pte *pe)
877 {
878         return pe->offset_from_dev_start + get_start_sect(pe->part_table);
879 }
880
881 #if ENABLE_FEATURE_FDISK_WRITABLE
882 /*
883  * Avoid warning about DOS partitions when no DOS partition was changed.
884  * Here a heuristic "is probably dos partition".
885  * We might also do the opposite and warn in all cases except
886  * for "is probably nondos partition".
887  */
888 #ifdef UNUSED
889 static int
890 is_dos_partition(int t)
891 {
892         return (t == 1 || t == 4 || t == 6 ||
893                 t == 0x0b || t == 0x0c || t == 0x0e ||
894                 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
895                 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
896                 t == 0xc1 || t == 0xc4 || t == 0xc6);
897 }
898 #endif
899
900 static void
901 menu(void)
902 {
903         puts("Command Action");
904         if (LABEL_IS_SUN) {
905                 puts("a\ttoggle a read only flag");           /* sun */
906                 puts("b\tedit bsd disklabel");
907                 puts("c\ttoggle the mountable flag");         /* sun */
908                 puts("d\tdelete a partition");
909                 puts("l\tlist known partition types");
910                 puts("n\tadd a new partition");
911                 puts("o\tcreate a new empty DOS partition table");
912                 puts("p\tprint the partition table");
913                 puts("q\tquit without saving changes");
914                 puts("s\tcreate a new empty Sun disklabel");  /* sun */
915                 puts("t\tchange a partition's system id");
916                 puts("u\tchange display/entry units");
917                 puts("v\tverify the partition table");
918                 puts("w\twrite table to disk and exit");
919 #if ENABLE_FEATURE_FDISK_ADVANCED
920                 puts("x\textra functionality (experts only)");
921 #endif
922         } else if (LABEL_IS_SGI) {
923                 puts("a\tselect bootable partition");    /* sgi flavour */
924                 puts("b\tedit bootfile entry");          /* sgi */
925                 puts("c\tselect sgi swap partition");    /* sgi flavour */
926                 puts("d\tdelete a partition");
927                 puts("l\tlist known partition types");
928                 puts("n\tadd a new partition");
929                 puts("o\tcreate a new empty DOS partition table");
930                 puts("p\tprint the partition table");
931                 puts("q\tquit without saving changes");
932                 puts("s\tcreate a new empty Sun disklabel");  /* sun */
933                 puts("t\tchange a partition's system id");
934                 puts("u\tchange display/entry units");
935                 puts("v\tverify the partition table");
936                 puts("w\twrite table to disk and exit");
937         } else if (LABEL_IS_AIX) {
938                 puts("o\tcreate a new empty DOS partition table");
939                 puts("q\tquit without saving changes");
940                 puts("s\tcreate a new empty Sun disklabel");  /* sun */
941         } else if (LABEL_IS_GPT) {
942                 puts("o\tcreate a new empty DOS partition table");
943                 puts("p\tprint the partition table");
944                 puts("q\tquit without saving changes");
945                 puts("s\tcreate a new empty Sun disklabel");  /* sun */
946         } else {
947                 puts("a\ttoggle a bootable flag");
948                 puts("b\tedit bsd disklabel");
949                 puts("c\ttoggle the dos compatibility flag");
950                 puts("d\tdelete a partition");
951                 puts("l\tlist known partition types");
952                 puts("n\tadd a new partition");
953                 puts("o\tcreate a new empty DOS partition table");
954                 puts("p\tprint the partition table");
955                 puts("q\tquit without saving changes");
956                 puts("s\tcreate a new empty Sun disklabel");  /* sun */
957                 puts("t\tchange a partition's system id");
958                 puts("u\tchange display/entry units");
959                 puts("v\tverify the partition table");
960                 puts("w\twrite table to disk and exit");
961 #if ENABLE_FEATURE_FDISK_ADVANCED
962                 puts("x\textra functionality (experts only)");
963 #endif
964         }
965 }
966 #endif /* FEATURE_FDISK_WRITABLE */
967
968
969 #if ENABLE_FEATURE_FDISK_ADVANCED
970 static void
971 xmenu(void)
972 {
973         puts("Command Action");
974         if (LABEL_IS_SUN) {
975                 puts("a\tchange number of alternate cylinders");      /*sun*/
976                 puts("c\tchange number of cylinders");
977                 puts("d\tprint the raw data in the partition table");
978                 puts("e\tchange number of extra sectors per cylinder");/*sun*/
979                 puts("h\tchange number of heads");
980                 puts("i\tchange interleave factor");                  /*sun*/
981                 puts("o\tchange rotation speed (rpm)");               /*sun*/
982                 puts("p\tprint the partition table");
983                 puts("q\tquit without saving changes");
984                 puts("r\treturn to main menu");
985                 puts("s\tchange number of sectors/track");
986                 puts("v\tverify the partition table");
987                 puts("w\twrite table to disk and exit");
988                 puts("y\tchange number of physical cylinders");       /*sun*/
989         } else if (LABEL_IS_SGI) {
990                 puts("b\tmove beginning of data in a partition"); /* !sun */
991                 puts("c\tchange number of cylinders");
992                 puts("d\tprint the raw data in the partition table");
993                 puts("e\tlist extended partitions");          /* !sun */
994                 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
995                 puts("h\tchange number of heads");
996                 puts("p\tprint the partition table");
997                 puts("q\tquit without saving changes");
998                 puts("r\treturn to main menu");
999                 puts("s\tchange number of sectors/track");
1000                 puts("v\tverify the partition table");
1001                 puts("w\twrite table to disk and exit");
1002         } else if (LABEL_IS_AIX) {
1003                 puts("b\tmove beginning of data in a partition"); /* !sun */
1004                 puts("c\tchange number of cylinders");
1005                 puts("d\tprint the raw data in the partition table");
1006                 puts("e\tlist extended partitions");          /* !sun */
1007                 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
1008                 puts("h\tchange number of heads");
1009                 puts("p\tprint the partition table");
1010                 puts("q\tquit without saving changes");
1011                 puts("r\treturn to main menu");
1012                 puts("s\tchange number of sectors/track");
1013                 puts("v\tverify the partition table");
1014                 puts("w\twrite table to disk and exit");
1015         } else {
1016                 puts("b\tmove beginning of data in a partition"); /* !sun */
1017                 puts("c\tchange number of cylinders");
1018                 puts("d\tprint the raw data in the partition table");
1019                 puts("e\tlist extended partitions");          /* !sun */
1020                 puts("f\tfix partition order");               /* !sun, !aix, !sgi */
1021 #if ENABLE_FEATURE_SGI_LABEL
1022                 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
1023 #endif
1024                 puts("h\tchange number of heads");
1025                 puts("p\tprint the partition table");
1026                 puts("q\tquit without saving changes");
1027                 puts("r\treturn to main menu");
1028                 puts("s\tchange number of sectors/track");
1029                 puts("v\tverify the partition table");
1030                 puts("w\twrite table to disk and exit");
1031         }
1032 }
1033 #endif /* ADVANCED mode */
1034
1035 #if ENABLE_FEATURE_FDISK_WRITABLE
1036 static const char *const *
1037 get_sys_types(void)
1038 {
1039         return (
1040                 LABEL_IS_SUN ? sun_sys_types :
1041                 LABEL_IS_SGI ? sgi_sys_types :
1042                 i386_sys_types);
1043 }
1044 #else
1045 #define get_sys_types() i386_sys_types
1046 #endif
1047
1048 static const char *
1049 partition_type(unsigned char type)
1050 {
1051         int i;
1052         const char *const *types = get_sys_types();
1053
1054         for (i = 0; types[i]; i++)
1055                 if ((unsigned char)types[i][0] == type)
1056                         return types[i] + 1;
1057
1058         return "Unknown";
1059 }
1060
1061 static int
1062 is_cleared_partition(const struct partition *p)
1063 {
1064         /* We consider partition "cleared" only if it has only zeros */
1065         const char *cp = (const char *)p;
1066         int cnt = sizeof(*p);
1067         char bits = 0;
1068         while (--cnt >= 0)
1069                 bits |= *cp++;
1070         return (bits == 0);
1071 }
1072
1073 static void
1074 clear_partition(struct partition *p)
1075 {
1076         if (p)
1077                 memset(p, 0, sizeof(*p));
1078 }
1079
1080 #if ENABLE_FEATURE_FDISK_WRITABLE
1081 static int
1082 get_sysid(int i)
1083 {
1084         return LABEL_IS_SUN ? sunlabel->infos[i].id :
1085                         (LABEL_IS_SGI ? sgi_get_sysid(i) :
1086                                 ptes[i].part_table->sys_ind);
1087 }
1088
1089 static void
1090 list_types(const char *const *sys)
1091 {
1092         enum { COLS = 3 };
1093
1094         unsigned last[COLS];
1095         unsigned done, next, size;
1096         int i;
1097
1098         for (size = 0; sys[size]; size++)
1099                 continue;
1100
1101         done = 0;
1102         for (i = COLS-1; i >= 0; i--) {
1103                 done += (size + i - done) / (i + 1);
1104                 last[COLS-1 - i] = done;
1105         }
1106
1107         i = done = next = 0;
1108         do {
1109                 printf("%c%2x %-22.22s", i ? ' ' : '\n',
1110                         (unsigned char)sys[next][0],
1111                         sys[next] + 1);
1112                 next = last[i++] + done;
1113                 if (i >= COLS || next >= last[i]) {
1114                         i = 0;
1115                         next = ++done;
1116                 }
1117         } while (done < last[0]);
1118         bb_putchar('\n');
1119 }
1120
1121 #define set_hsc(h, s, c, sector) do \
1122 { \
1123         s = sector % g_sectors + 1;  \
1124         sector /= g_sectors;         \
1125         h = sector % g_heads;        \
1126         sector /= g_heads;           \
1127         c = sector & 0xff;           \
1128         s |= (sector >> 2) & 0xc0;   \
1129 } while (0)
1130
1131 static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1132 {
1133         if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1134                 start = g_heads * g_sectors * 1024 - 1;
1135         set_hsc(p->head, p->sector, p->cyl, start);
1136
1137         if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1138                 stop = g_heads * g_sectors * 1024 - 1;
1139         set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1140 }
1141
1142 static void
1143 set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
1144 {
1145         struct partition *p;
1146         sector_t offset;
1147
1148         if (doext) {
1149                 p = ptes[i].ext_pointer;
1150                 offset = extended_offset;
1151         } else {
1152                 p = ptes[i].part_table;
1153                 offset = ptes[i].offset_from_dev_start;
1154         }
1155         p->boot_ind = 0;
1156         p->sys_ind = sysid;
1157         set_start_sect(p, start - offset);
1158         set_nr_sects(p, stop - start + 1);
1159         set_hsc_start_end(p, start, stop);
1160         ptes[i].changed = 1;
1161 }
1162 #endif
1163
1164 static int
1165 warn_geometry(void)
1166 {
1167         if (g_heads && g_sectors && g_cylinders)
1168                 return 0;
1169
1170         printf("Unknown value(s) for:");
1171         if (!g_heads)
1172                 printf(" heads");
1173         if (!g_sectors)
1174                 printf(" sectors");
1175         if (!g_cylinders)
1176                 printf(" cylinders");
1177 #if ENABLE_FEATURE_FDISK_WRITABLE
1178         puts(" (settable in the extra functions menu)");
1179 #else
1180         bb_putchar('\n');
1181 #endif
1182         return 1;
1183 }
1184
1185 static void
1186 update_units(void)
1187 {
1188         int cyl_units = g_heads * g_sectors;
1189
1190         if (display_in_cyl_units && cyl_units)
1191                 units_per_sector = cyl_units;
1192         else
1193                 units_per_sector = 1;   /* in sectors */
1194 }
1195
1196 #if ENABLE_FEATURE_FDISK_WRITABLE
1197 static void
1198 warn_cylinders(void)
1199 {
1200         if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
1201                 printf("\n"
1202 "The number of cylinders for this disk is set to %u.\n"
1203 "There is nothing wrong with that, but this is larger than 1024,\n"
1204 "and could in certain setups cause problems with:\n"
1205 "1) software that runs at boot time (e.g., old versions of LILO)\n"
1206 "2) booting and partitioning software from other OSs\n"
1207 "   (e.g., DOS FDISK, OS/2 FDISK)\n",
1208                         g_cylinders);
1209 }
1210 #endif
1211
1212 static void
1213 read_extended(int ext)
1214 {
1215         int i;
1216         struct pte *pex;
1217         struct partition *p, *q;
1218
1219         ext_index = ext;
1220         pex = &ptes[ext];
1221         pex->ext_pointer = pex->part_table;
1222
1223         p = pex->part_table;
1224         if (!get_start_sect(p)) {
1225                 puts("Bad offset in primary extended partition");
1226                 return;
1227         }
1228
1229         while (IS_EXTENDED(p->sys_ind)) {
1230                 struct pte *pe = &ptes[g_partitions];
1231
1232                 if (g_partitions >= MAXIMUM_PARTS) {
1233                         /* This is not a Linux restriction, but
1234                            this program uses arrays of size MAXIMUM_PARTS.
1235                            Do not try to 'improve' this test. */
1236                         struct pte *pre = &ptes[g_partitions - 1];
1237 #if ENABLE_FEATURE_FDISK_WRITABLE
1238                         printf("Warning: deleting partitions after %u\n",
1239                                 g_partitions);
1240                         pre->changed = 1;
1241 #endif
1242                         clear_partition(pre->ext_pointer);
1243                         return;
1244                 }
1245
1246                 read_pte(pe, extended_offset + get_start_sect(p));
1247
1248                 if (!extended_offset)
1249                         extended_offset = get_start_sect(p);
1250
1251                 q = p = pt_offset(pe->sectorbuffer, 0);
1252                 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
1253                         if (IS_EXTENDED(p->sys_ind)) {
1254                                 if (pe->ext_pointer)
1255                                         printf("Warning: extra link "
1256                                                 "pointer in partition table"
1257                                                 " %u\n", g_partitions + 1);
1258                                 else
1259                                         pe->ext_pointer = p;
1260                         } else if (p->sys_ind) {
1261                                 if (pe->part_table)
1262                                         printf("Warning: ignoring extra "
1263                                                   "data in partition table"
1264                                                   " %u\n", g_partitions + 1);
1265                                 else
1266                                         pe->part_table = p;
1267                         }
1268                 }
1269
1270                 /* very strange code here... */
1271                 if (!pe->part_table) {
1272                         if (q != pe->ext_pointer)
1273                                 pe->part_table = q;
1274                         else
1275                                 pe->part_table = q + 1;
1276                 }
1277                 if (!pe->ext_pointer) {
1278                         if (q != pe->part_table)
1279                                 pe->ext_pointer = q;
1280                         else
1281                                 pe->ext_pointer = q + 1;
1282                 }
1283
1284                 p = pe->ext_pointer;
1285                 g_partitions++;
1286         }
1287
1288 #if ENABLE_FEATURE_FDISK_WRITABLE
1289         /* remove empty links */
1290  remove:
1291         for (i = 4; i < g_partitions; i++) {
1292                 struct pte *pe = &ptes[i];
1293
1294                 if (!get_nr_sects(pe->part_table)
1295                  && (g_partitions > 5 || ptes[4].part_table->sys_ind)
1296                 ) {
1297                         printf("Omitting empty partition (%u)\n", i+1);
1298                         delete_partition(i);
1299                         goto remove;    /* numbering changed */
1300                 }
1301         }
1302 #endif
1303 }
1304
1305 #if ENABLE_FEATURE_FDISK_WRITABLE
1306 static void
1307 create_doslabel(void)
1308 {
1309         printf(msg_building_new_label, "DOS disklabel");
1310
1311         current_label_type = LABEL_DOS;
1312 #if ENABLE_FEATURE_OSF_LABEL
1313         possibly_osf_label = 0;
1314 #endif
1315         g_partitions = 4;
1316
1317         memset(&MBRbuffer[510 - 4*16], 0, 4*16);
1318         write_part_table_flag(MBRbuffer);
1319         extended_offset = 0;
1320         set_all_unchanged();
1321         set_changed(0);
1322         get_boot(CREATE_EMPTY_DOS);
1323 }
1324 #endif
1325
1326 static void
1327 get_sectorsize(void)
1328 {
1329         if (!user_set_sector_size) {
1330                 int arg;
1331                 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
1332                         sector_size = arg;
1333                 if (sector_size != DEFAULT_SECTOR_SIZE)
1334                         printf("Note: sector size is %u "
1335                                 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1336                                 sector_size);
1337         }
1338 }
1339
1340 static void
1341 get_kernel_geometry(void)
1342 {
1343         struct hd_geometry geometry;
1344
1345         if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
1346                 kern_heads = geometry.heads;
1347                 kern_sectors = geometry.sectors;
1348                 /* never use geometry.cylinders - it is truncated */
1349         }
1350 }
1351
1352 static void
1353 get_partition_table_geometry(void)
1354 {
1355         const unsigned char *bufp = (const unsigned char *)MBRbuffer;
1356         struct partition *p;
1357         int i, h, s, hh, ss;
1358         int first = 1;
1359         int bad = 0;
1360
1361         if (!(valid_part_table_flag((char*)bufp)))
1362                 return;
1363
1364         hh = ss = 0;
1365         for (i = 0; i < 4; i++) {
1366                 p = pt_offset(bufp, i);
1367                 if (p->sys_ind != 0) {
1368                         h = p->end_head + 1;
1369                         s = (p->end_sector & 077);
1370                         if (first) {
1371                                 hh = h;
1372                                 ss = s;
1373                                 first = 0;
1374                         } else if (hh != h || ss != s)
1375                                 bad = 1;
1376                 }
1377         }
1378
1379         if (!first && !bad) {
1380                 pt_heads = hh;
1381                 pt_sectors = ss;
1382         }
1383 }
1384
1385 static void
1386 get_geometry(void)
1387 {
1388         int sec_fac;
1389
1390         get_sectorsize();
1391         sec_fac = sector_size / 512;
1392 #if ENABLE_FEATURE_SUN_LABEL
1393         guess_device_type();
1394 #endif
1395         g_heads = g_cylinders = g_sectors = 0;
1396         kern_heads = kern_sectors = 0;
1397         pt_heads = pt_sectors = 0;
1398
1399         get_kernel_geometry();
1400         get_partition_table_geometry();
1401
1402         g_heads = user_heads ? user_heads :
1403                 pt_heads ? pt_heads :
1404                 kern_heads ? kern_heads : 255;
1405         g_sectors = user_sectors ? user_sectors :
1406                 pt_sectors ? pt_sectors :
1407                 kern_sectors ? kern_sectors : 63;
1408         total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
1409
1410         sector_offset = 1;
1411         if (dos_compatible_flag)
1412                 sector_offset = g_sectors;
1413
1414         g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1415         if (!g_cylinders)
1416                 g_cylinders = user_cylinders;
1417 }
1418
1419 /*
1420  * Opens disk_device and optionally reads MBR.
1421  *    If what == OPEN_MAIN:
1422  *      Open device, read MBR.  Abort program on short read.  Create empty
1423  *      disklabel if the on-disk structure is invalid (WRITABLE mode).
1424  *    If what == TRY_ONLY:
1425  *      Open device, read MBR.  Return an error if anything is out of place.
1426  *      Do not create an empty disklabel.  This is used for the "list"
1427  *      operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1428  *    If what == CREATE_EMPTY_*:
1429  *      This means that get_boot() was called recursively from create_*label().
1430  *      Do not re-open the device; just set up the ptes array and print
1431  *      geometry warnings.
1432  *
1433  * Returns:
1434  *   -1: no 0xaa55 flag present (possibly entire disk BSD)
1435  *    0: found or created label
1436  *    1: I/O error
1437  */
1438 #if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1439 static int get_boot(enum action what)
1440 #else
1441 static int get_boot(void)
1442 #define get_boot(what) get_boot()
1443 #endif
1444 {
1445         int i, fd;
1446
1447         g_partitions = 4;
1448         for (i = 0; i < 4; i++) {
1449                 struct pte *pe = &ptes[i];
1450                 pe->part_table = pt_offset(MBRbuffer, i);
1451                 pe->ext_pointer = NULL;
1452                 pe->offset_from_dev_start = 0;
1453                 pe->sectorbuffer = MBRbuffer;
1454 #if ENABLE_FEATURE_FDISK_WRITABLE
1455                 pe->changed = (what == CREATE_EMPTY_DOS);
1456 #endif
1457         }
1458
1459 #if ENABLE_FEATURE_FDISK_WRITABLE
1460 // ALERT! highly idiotic design!
1461 // We end up here when we call get_boot() recursively
1462 // via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1463 // or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1464 // (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1465 // So skip opening device _again_...
1466         if (what == CREATE_EMPTY_DOS  IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
1467                 goto created_table;
1468
1469         fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1470
1471         if (fd < 0) {
1472                 fd = open(disk_device, O_RDONLY);
1473                 if (fd < 0) {
1474                         if (what == TRY_ONLY)
1475                                 return 1;
1476                         fdisk_fatal(unable_to_open);
1477                 }
1478                 printf("'%s' is opened for read only\n", disk_device);
1479         }
1480         xmove_fd(fd, dev_fd);
1481         if (512 != full_read(dev_fd, MBRbuffer, 512)) {
1482                 if (what == TRY_ONLY) {
1483                         close_dev_fd();
1484                         return 1;
1485                 }
1486                 fdisk_fatal(unable_to_read);
1487         }
1488 #else
1489         fd = open(disk_device, O_RDONLY);
1490         if (fd < 0)
1491                 return 1;
1492         if (512 != full_read(fd, MBRbuffer, 512)) {
1493                 close(fd);
1494                 return 1;
1495         }
1496         xmove_fd(fd, dev_fd);
1497 #endif
1498
1499         get_geometry();
1500         update_units();
1501
1502 #if ENABLE_FEATURE_SUN_LABEL
1503         if (check_sun_label())
1504                 return 0;
1505 #endif
1506 #if ENABLE_FEATURE_SGI_LABEL
1507         if (check_sgi_label())
1508                 return 0;
1509 #endif
1510 #if ENABLE_FEATURE_AIX_LABEL
1511         if (check_aix_label())
1512                 return 0;
1513 #endif
1514 #if ENABLE_FEATURE_GPT_LABEL
1515         if (check_gpt_label())
1516                 return 0;
1517 #endif
1518 #if ENABLE_FEATURE_OSF_LABEL
1519         if (check_osf_label()) {
1520                 possibly_osf_label = 1;
1521                 if (!valid_part_table_flag(MBRbuffer)) {
1522                         current_label_type = LABEL_OSF;
1523                         return 0;
1524                 }
1525                 puts("This disk has both DOS and BSD magic.\n"
1526                      "Give the 'b' command to go to BSD mode.");
1527         }
1528 #endif
1529
1530 #if !ENABLE_FEATURE_FDISK_WRITABLE
1531         if (!valid_part_table_flag(MBRbuffer))
1532                 return -1;
1533 #else
1534         if (!valid_part_table_flag(MBRbuffer)) {
1535                 if (what == OPEN_MAIN) {
1536                         puts("Device contains neither a valid DOS "
1537                              "partition table, nor Sun, SGI, OSF or GPT "
1538                              "disklabel");
1539 #ifdef __sparc__
1540                         IF_FEATURE_SUN_LABEL(create_sunlabel();)
1541 #else
1542                         create_doslabel();
1543 #endif
1544                         return 0;
1545                 }
1546                 /* TRY_ONLY: */
1547                 return -1;
1548         }
1549  created_table:
1550 #endif /* FEATURE_FDISK_WRITABLE */
1551
1552
1553         IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
1554         warn_geometry();
1555
1556         for (i = 0; i < 4; i++) {
1557                 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
1558                         if (g_partitions != 4)
1559                                 printf("Ignoring extra extended "
1560                                         "partition %u\n", i + 1);
1561                         else
1562                                 read_extended(i);
1563                 }
1564         }
1565
1566         for (i = 3; i < g_partitions; i++) {
1567                 struct pte *pe = &ptes[i];
1568                 if (!valid_part_table_flag(pe->sectorbuffer)) {
1569                         printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1570                                 "table %u will be corrected by w(rite)\n",
1571                                 pe->sectorbuffer[510],
1572                                 pe->sectorbuffer[511],
1573                                 i + 1);
1574                         IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
1575                 }
1576         }
1577
1578         return 0;
1579 }
1580
1581 #if ENABLE_FEATURE_FDISK_WRITABLE
1582 /*
1583  * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1584  * If the user hits Enter, DFLT is returned.
1585  * Answers like +10 are interpreted as offsets from BASE.
1586  *
1587  * There is no default if DFLT is not between LOW and HIGH.
1588  */
1589 static sector_t
1590 read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
1591 {
1592         sector_t value;
1593         int default_ok = 1;
1594         const char *fmt = "%s (%u-%u, default %u): ";
1595
1596         if (dflt < low || dflt > high) {
1597                 fmt = "%s (%u-%u): ";
1598                 default_ok = 0;
1599         }
1600
1601         while (1) {
1602                 int use_default = default_ok;
1603
1604                 /* ask question and read answer */
1605                 do {
1606                         printf(fmt, mesg, low, high, dflt);
1607                         read_maybe_empty("");
1608                 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1609                  && *line_ptr != '-' && *line_ptr != '+');
1610
1611                 if (*line_ptr == '+' || *line_ptr == '-') {
1612                         int minus = (*line_ptr == '-');
1613                         int absolute = 0;
1614
1615                         value = atoi(line_ptr + 1);
1616
1617                         /* (1) if 2nd char is digit, use_default = 0.
1618                          * (2) move line_ptr to first non-digit. */
1619                         while (isdigit(*++line_ptr))
1620                                 use_default = 0;
1621
1622                         switch (*line_ptr) {
1623                         case 'c':
1624                         case 'C':
1625                                 if (!display_in_cyl_units)
1626                                         value *= g_heads * g_sectors;
1627                                 break;
1628                         case 'K':
1629                                 absolute = 1024;
1630                                 break;
1631                         case 'k':
1632                                 absolute = 1000;
1633                                 break;
1634                         case 'm':
1635                         case 'M':
1636                                 absolute = 1000000;
1637                                 break;
1638                         case 'g':
1639                         case 'G':
1640                                 absolute = 1000000000;
1641                                 break;
1642                         default:
1643                                 break;
1644                         }
1645                         if (absolute) {
1646                                 ullong bytes;
1647                                 unsigned long unit;
1648
1649                                 bytes = (ullong) value * absolute;
1650                                 unit = sector_size * units_per_sector;
1651                                 bytes += unit/2; /* round */
1652                                 bytes /= unit;
1653                                 value = bytes;
1654                         }
1655                         if (minus)
1656                                 value = -value;
1657                         value += base;
1658                 } else {
1659                         value = atoi(line_ptr);
1660                         while (isdigit(*line_ptr)) {
1661                                 line_ptr++;
1662                                 use_default = 0;
1663                         }
1664                 }
1665                 if (use_default) {
1666                         value = dflt;
1667                         printf("Using default value %u\n", value);
1668                 }
1669                 if (value >= low && value <= high)
1670                         break;
1671                 puts("Value is out of range");
1672         }
1673         return value;
1674 }
1675
1676 static unsigned
1677 get_partition(int warn, unsigned max)
1678 {
1679         struct pte *pe;
1680         unsigned i;
1681
1682         i = read_int(1, 0, max, 0, "Partition number") - 1;
1683         pe = &ptes[i];
1684
1685         if (warn) {
1686                 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1687                  || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1688                  || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1689                 ) {
1690                         printf("Warning: partition %u has empty type\n", i+1);
1691                 }
1692         }
1693         return i;
1694 }
1695
1696 static int
1697 get_existing_partition(int warn, unsigned max)
1698 {
1699         int pno = -1;
1700         unsigned i;
1701
1702         for (i = 0; i < max; i++) {
1703                 struct pte *pe = &ptes[i];
1704                 struct partition *p = pe->part_table;
1705
1706                 if (p && !is_cleared_partition(p)) {
1707                         if (pno >= 0)
1708                                 goto not_unique;
1709                         pno = i;
1710                 }
1711         }
1712         if (pno >= 0) {
1713                 printf("Selected partition %u\n", pno+1);
1714                 return pno;
1715         }
1716         puts("No partition is defined yet!");
1717         return -1;
1718
1719  not_unique:
1720         return get_partition(warn, max);
1721 }
1722
1723 static int
1724 get_nonexisting_partition(int warn, unsigned max)
1725 {
1726         int pno = -1;
1727         unsigned i;
1728
1729         for (i = 0; i < max; i++) {
1730                 struct pte *pe = &ptes[i];
1731                 struct partition *p = pe->part_table;
1732
1733                 if (p && is_cleared_partition(p)) {
1734                         if (pno >= 0)
1735                                 goto not_unique;
1736                         pno = i;
1737                 }
1738         }
1739         if (pno >= 0) {
1740                 printf("Selected partition %u\n", pno+1);
1741                 return pno;
1742         }
1743         puts("All primary partitions have been defined already!");
1744         return -1;
1745
1746  not_unique:
1747         return get_partition(warn, max);
1748 }
1749
1750
1751 static void
1752 change_units(void)
1753 {
1754         display_in_cyl_units = !display_in_cyl_units;
1755         update_units();
1756         printf("Changing display/entry units to %s\n",
1757                 str_units(PLURAL));
1758 }
1759
1760 static void
1761 toggle_active(int i)
1762 {
1763         struct pte *pe = &ptes[i];
1764         struct partition *p = pe->part_table;
1765
1766         if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
1767                 printf("WARNING: Partition %u is an extended partition\n", i + 1);
1768         p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1769         pe->changed = 1;
1770 }
1771
1772 static void
1773 toggle_dos_compatibility_flag(void)
1774 {
1775         dos_compatible_flag = 1 - dos_compatible_flag;
1776         if (dos_compatible_flag) {
1777                 sector_offset = g_sectors;
1778                 printf("DOS Compatibility flag is %sset\n", "");
1779         } else {
1780                 sector_offset = 1;
1781                 printf("DOS Compatibility flag is %sset\n", "not ");
1782         }
1783 }
1784
1785 static void
1786 delete_partition(int i)
1787 {
1788         struct pte *pe = &ptes[i];
1789         struct partition *p = pe->part_table;
1790         struct partition *q = pe->ext_pointer;
1791
1792 /* Note that for the fifth partition (i == 4) we don't actually
1793  * decrement partitions.
1794  */
1795
1796         if (warn_geometry())
1797                 return;         /* C/H/S not set */
1798         pe->changed = 1;
1799
1800         if (LABEL_IS_SUN) {
1801                 sun_delete_partition(i);
1802                 return;
1803         }
1804         if (LABEL_IS_SGI) {
1805                 sgi_delete_partition(i);
1806                 return;
1807         }
1808
1809         if (i < 4) {
1810                 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
1811                         g_partitions = 4;
1812                         ptes[ext_index].ext_pointer = NULL;
1813                         extended_offset = 0;
1814                 }
1815                 clear_partition(p);
1816                 return;
1817         }
1818
1819         if (!q->sys_ind && i > 4) {
1820                 /* the last one in the chain - just delete */
1821                 --g_partitions;
1822                 --i;
1823                 clear_partition(ptes[i].ext_pointer);
1824                 ptes[i].changed = 1;
1825         } else {
1826                 /* not the last one - further ones will be moved down */
1827                 if (i > 4) {
1828                         /* delete this link in the chain */
1829                         p = ptes[i-1].ext_pointer;
1830                         *p = *q;
1831                         set_start_sect(p, get_start_sect(q));
1832                         set_nr_sects(p, get_nr_sects(q));
1833                         ptes[i-1].changed = 1;
1834                 } else if (g_partitions > 5) {    /* 5 will be moved to 4 */
1835                         /* the first logical in a longer chain */
1836                         pe = &ptes[5];
1837
1838                         if (pe->part_table) /* prevent SEGFAULT */
1839                                 set_start_sect(pe->part_table,
1840                                                 get_partition_start_from_dev_start(pe) -
1841                                                 extended_offset);
1842                         pe->offset_from_dev_start = extended_offset;
1843                         pe->changed = 1;
1844                 }
1845
1846                 if (g_partitions > 5) {
1847                         g_partitions--;
1848                         while (i < g_partitions) {
1849                                 ptes[i] = ptes[i+1];
1850                                 i++;
1851                         }
1852                 } else {
1853                         /* the only logical: clear only */
1854                         clear_partition(ptes[i].part_table);
1855                 }
1856         }
1857 }
1858
1859 static void
1860 change_sysid(void)
1861 {
1862         int i, sys, origsys;
1863         struct partition *p;
1864
1865         /* If sgi_label then don't use get_existing_partition,
1866            let the user select a partition, since get_existing_partition()
1867            only works for Linux like partition tables. */
1868         if (!LABEL_IS_SGI) {
1869                 i = get_existing_partition(0, g_partitions);
1870         } else {
1871                 i = get_partition(0, g_partitions);
1872         }
1873         if (i == -1)
1874                 return;
1875         p = ptes[i].part_table;
1876         origsys = sys = get_sysid(i);
1877
1878         /* if changing types T to 0 is allowed, then
1879            the reverse change must be allowed, too */
1880         if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
1881                 printf("Partition %u does not exist yet!\n", i + 1);
1882                 return;
1883         }
1884         while (1) {
1885                 sys = read_hex(get_sys_types());
1886
1887                 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
1888                         puts("Type 0 means free space to many systems\n"
1889                                 "(but not to Linux). Having partitions of\n"
1890                                 "type 0 is probably unwise.");
1891                         /* break; */
1892                 }
1893
1894                 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
1895                         if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
1896                                 puts("You cannot change a partition into"
1897                                         " an extended one or vice versa");
1898                                 break;
1899                         }
1900                 }
1901
1902                 if (sys < 256) {
1903 #if ENABLE_FEATURE_SUN_LABEL
1904                         if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
1905                                 puts("Consider leaving partition 3 "
1906                                         "as Whole disk (5),\n"
1907                                         "as SunOS/Solaris expects it and "
1908                                         "even Linux likes it\n");
1909 #endif
1910 #if ENABLE_FEATURE_SGI_LABEL
1911                         if (LABEL_IS_SGI &&
1912                                 (
1913                                         (i == 10 && sys != SGI_ENTIRE_DISK) ||
1914                                         (i == 8 && sys != 0)
1915                                 )
1916                         ) {
1917                                 puts("Consider leaving partition 9 "
1918                                         "as volume header (0),\nand "
1919                                         "partition 11 as entire volume (6)"
1920                                         "as IRIX expects it\n");
1921                         }
1922 #endif
1923                         if (sys == origsys)
1924                                 break;
1925                         if (LABEL_IS_SUN) {
1926                                 sun_change_sysid(i, sys);
1927                         } else if (LABEL_IS_SGI) {
1928                                 sgi_change_sysid(i, sys);
1929                         } else
1930                                 p->sys_ind = sys;
1931
1932                         printf("Changed system type of partition %u "
1933                                 "to %x (%s)\n", i + 1, sys,
1934                                 partition_type(sys));
1935                         ptes[i].changed = 1;
1936                         //if (is_dos_partition(origsys) || is_dos_partition(sys))
1937                         //      dos_changed = 1;
1938                         break;
1939                 }
1940         }
1941 }
1942 #endif /* FEATURE_FDISK_WRITABLE */
1943
1944
1945 /* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
1946  * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1947  * Jan.  1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1948  * Lubkin Oct.  1991). */
1949
1950 static void
1951 linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
1952 {
1953         int spc = g_heads * g_sectors;
1954
1955         *c = ls / spc;
1956         ls = ls % spc;
1957         *h = ls / g_sectors;
1958         *s = ls % g_sectors + 1;  /* sectors count from 1 */
1959 }
1960
1961 static void
1962 check_consistency(const struct partition *p, int partition)
1963 {
1964         unsigned pbc, pbh, pbs;          /* physical beginning c, h, s */
1965         unsigned pec, peh, pes;          /* physical ending c, h, s */
1966         unsigned lbc, lbh, lbs;          /* logical beginning c, h, s */
1967         unsigned lec, leh, les;          /* logical ending c, h, s */
1968
1969         if (!g_heads || !g_sectors || (partition >= 4))
1970                 return;         /* do not check extended partitions */
1971
1972 /* physical beginning c, h, s */
1973         pbc = cylinder(p->sector, p->cyl);
1974         pbh = p->head;
1975         pbs = sector(p->sector);
1976
1977 /* physical ending c, h, s */
1978         pec = cylinder(p->end_sector, p->end_cyl);
1979         peh = p->end_head;
1980         pes = sector(p->end_sector);
1981
1982 /* compute logical beginning (c, h, s) */
1983         linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1984
1985 /* compute logical ending (c, h, s) */
1986         linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1987
1988 /* Same physical / logical beginning? */
1989         if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1990                 printf("Partition %u has different physical/logical "
1991                         "start (non-Linux?):\n", partition + 1);
1992                 printf("     phys=(%u,%u,%u) ", pbc, pbh, pbs);
1993                 printf("logical=(%u,%u,%u)\n", lbc, lbh, lbs);
1994         }
1995
1996 /* Same physical / logical ending? */
1997         if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1998                 printf("Partition %u has different physical/logical "
1999                         "end:\n", partition + 1);
2000                 printf("     phys=(%u,%u,%u) ", pec, peh, pes);
2001                 printf("logical=(%u,%u,%u)\n", lec, leh, les);
2002         }
2003
2004 /* Ending on cylinder boundary? */
2005         if (peh != (g_heads - 1) || pes != g_sectors) {
2006                 printf("Partition %u does not end on cylinder boundary\n",
2007                         partition + 1);
2008         }
2009 }
2010
2011 static void
2012 list_disk_geometry(void)
2013 {
2014         ullong bytes = ((ullong)total_number_of_sectors << 9);
2015         ullong xbytes = bytes / (1024*1024);
2016         char x = 'M';
2017
2018         if (xbytes >= 10000) {
2019                 xbytes += 512; /* fdisk util-linux 2.28 does this */
2020                 xbytes /= 1024;
2021                 x = 'G';
2022         }
2023         printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
2024                 "%u cylinders, %u heads, %u sectors/track\n"
2025                 "Units: %s of %u * %u = %u bytes\n\n",
2026                 disk_device, xbytes, x,
2027                 bytes, total_number_of_sectors,
2028                 g_cylinders, g_heads, g_sectors,
2029                 str_units(PLURAL),
2030                 units_per_sector, sector_size, units_per_sector * sector_size
2031         );
2032 }
2033
2034 /*
2035  * Check whether partition entries are ordered by their starting positions.
2036  * Return 0 if OK. Return i if partition i should have been earlier.
2037  * Two separate checks: primary and logical partitions.
2038  */
2039 static int
2040 wrong_p_order(int *prev)
2041 {
2042         const struct pte *pe;
2043         const struct partition *p;
2044         sector_t last_p_start_pos = 0, p_start_pos;
2045         unsigned i, last_i = 0;
2046
2047         for (i = 0; i < g_partitions; i++) {
2048                 if (i == 4) {
2049                         last_i = 4;
2050                         last_p_start_pos = 0;
2051                 }
2052                 pe = &ptes[i];
2053                 p = pe->part_table;
2054                 if (p->sys_ind) {
2055                         p_start_pos = get_partition_start_from_dev_start(pe);
2056
2057                         if (last_p_start_pos > p_start_pos) {
2058                                 if (prev)
2059                                         *prev = last_i;
2060                                 return i;
2061                         }
2062
2063                         last_p_start_pos = p_start_pos;
2064                         last_i = i;
2065                 }
2066         }
2067         return 0;
2068 }
2069
2070 #if ENABLE_FEATURE_FDISK_ADVANCED
2071 /*
2072  * Fix the chain of logicals.
2073  * extended_offset is unchanged, the set of sectors used is unchanged
2074  * The chain is sorted so that sectors increase, and so that
2075  * starting sectors increase.
2076  *
2077  * After this it may still be that cfdisk doesnt like the table.
2078  * (This is because cfdisk considers expanded parts, from link to
2079  * end of partition, and these may still overlap.)
2080  * Now
2081  *   sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
2082  * may help.
2083  */
2084 static void
2085 fix_chain_of_logicals(void)
2086 {
2087         int j, oj, ojj, sj, sjj;
2088         struct partition *pj,*pjj,tmp;
2089
2090         /* Stage 1: sort sectors but leave sector of part 4 */
2091         /* (Its sector is the global extended_offset.) */
2092  stage1:
2093         for (j = 5; j < g_partitions - 1; j++) {
2094                 oj = ptes[j].offset_from_dev_start;
2095                 ojj = ptes[j+1].offset_from_dev_start;
2096                 if (oj > ojj) {
2097                         ptes[j].offset_from_dev_start = ojj;
2098                         ptes[j+1].offset_from_dev_start = oj;
2099                         pj = ptes[j].part_table;
2100                         set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2101                         pjj = ptes[j+1].part_table;
2102                         set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2103                         set_start_sect(ptes[j-1].ext_pointer,
2104                                            ojj-extended_offset);
2105                         set_start_sect(ptes[j].ext_pointer,
2106                                            oj-extended_offset);
2107                         goto stage1;
2108                 }
2109         }
2110
2111         /* Stage 2: sort starting sectors */
2112  stage2:
2113         for (j = 4; j < g_partitions - 1; j++) {
2114                 pj = ptes[j].part_table;
2115                 pjj = ptes[j+1].part_table;
2116                 sj = get_start_sect(pj);
2117                 sjj = get_start_sect(pjj);
2118                 oj = ptes[j].offset_from_dev_start;
2119                 ojj = ptes[j+1].offset_from_dev_start;
2120                 if (oj+sj > ojj+sjj) {
2121                         tmp = *pj;
2122                         *pj = *pjj;
2123                         *pjj = tmp;
2124                         set_start_sect(pj, ojj+sjj-oj);
2125                         set_start_sect(pjj, oj+sj-ojj);
2126                         goto stage2;
2127                 }
2128         }
2129
2130         /* Probably something was changed */
2131         for (j = 4; j < g_partitions; j++)
2132                 ptes[j].changed = 1;
2133 }
2134
2135
2136 static void
2137 fix_partition_table_order(void)
2138 {
2139         struct pte *pei, *pek;
2140         int i,k;
2141
2142         if (!wrong_p_order(NULL)) {
2143                 puts("Ordering is already correct\n");
2144                 return;
2145         }
2146
2147         while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2148                 /* partition i should have come earlier, move it */
2149                 /* We have to move data in the MBR */
2150                 struct partition *pi, *pk, *pe, pbuf;
2151                 pei = &ptes[i];
2152                 pek = &ptes[k];
2153
2154                 pe = pei->ext_pointer;
2155                 pei->ext_pointer = pek->ext_pointer;
2156                 pek->ext_pointer = pe;
2157
2158                 pi = pei->part_table;
2159                 pk = pek->part_table;
2160
2161                 memmove(&pbuf, pi, sizeof(struct partition));
2162                 memmove(pi, pk, sizeof(struct partition));
2163                 memmove(pk, &pbuf, sizeof(struct partition));
2164
2165                 pei->changed = pek->changed = 1;
2166         }
2167
2168         if (i)
2169                 fix_chain_of_logicals();
2170
2171         puts("Done");
2172 }
2173 #endif
2174
2175 static const char *
2176 chs_string11(unsigned cyl, unsigned head, unsigned sect)
2177 {
2178         char *buf = auto_string(xzalloc(sizeof(int)*3 * 3));
2179         sprintf(buf, "%u,%u,%u", cylinder(sect,cyl), head, sector(sect));
2180         return buf;
2181 }
2182
2183 static void
2184 list_table(int xtra)
2185 {
2186         int i, w;
2187
2188         if (LABEL_IS_SUN) {
2189                 sun_list_table(xtra);
2190                 return;
2191         }
2192         if (LABEL_IS_SGI) {
2193                 sgi_list_table(xtra);
2194                 return;
2195         }
2196         if (LABEL_IS_GPT) {
2197                 gpt_list_table(xtra);
2198                 return;
2199         }
2200
2201         list_disk_geometry();
2202
2203         if (LABEL_IS_OSF) {
2204                 xbsd_print_disklabel(xtra);
2205                 return;
2206         }
2207
2208         /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2209          * but if the device name ends in a digit, say /dev/foo1,
2210          * then the partition is called /dev/foo1p3.
2211          */
2212         w = strlen(disk_device);
2213         if (w && isdigit(disk_device[w-1]))
2214                 w++;
2215         if (w < 7)
2216                 w = 7;
2217
2218         printf("%-*s Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type\n",
2219                    w-1, "Device");
2220
2221         for (i = 0; i < g_partitions; i++) {
2222                 const struct partition *p;
2223                 const struct pte *pe = &ptes[i];
2224                 char boot4[4];
2225                 char numstr6[6];
2226                 sector_t start_sect;
2227                 sector_t end_sect;
2228                 sector_t nr_sects;
2229
2230                 p = pe->part_table;
2231                 if (!p || is_cleared_partition(p))
2232                         continue;
2233
2234                 sprintf(boot4, "%02x", p->boot_ind);
2235                 if ((p->boot_ind & 0x7f) == 0) {
2236                         /* 0x80 shown as '*', 0x00 is ' ' */
2237                         boot4[0] = p->boot_ind ? '*' : ' ';
2238                         boot4[1] = ' ';
2239                 }
2240
2241                 start_sect = get_partition_start_from_dev_start(pe);
2242                 end_sect = start_sect;
2243                 nr_sects = get_nr_sects(p);
2244                 if (nr_sects != 0)
2245                         end_sect += nr_sects - 1;
2246
2247                 smart_ulltoa5((ullong)nr_sects * sector_size,
2248                         numstr6, " KMGTPEZY")[0] = '\0';
2249
2250 #define SFMT SECT_FMT
2251                 //      Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
2252                 printf("%s%s %-11s"/**/" %-11s"/**/" %10"SFMT"u %10"SFMT"u %10"SFMT"u %s %2x %s\n",
2253                         partname(disk_device, i+1, w+2),
2254                         boot4,
2255                         chs_string11(p->cyl, p->head, p->sector),
2256                         chs_string11(p->end_cyl, p->end_head, p->end_sector),
2257                         start_sect,
2258                         end_sect,
2259                         nr_sects,
2260                         numstr6,
2261                         p->sys_ind,
2262                         partition_type(p->sys_ind)
2263                 );
2264 #undef SFMT
2265                 check_consistency(p, i);
2266         }
2267
2268         /* Is partition table in disk order? It need not be, but... */
2269         /* partition table entries are not checked for correct order
2270          * if this is a sgi, sun or aix labeled disk... */
2271         if (LABEL_IS_DOS && wrong_p_order(NULL)) {
2272                 /* FIXME */
2273                 puts("\nPartition table entries are not in disk order");
2274         }
2275 }
2276
2277 #if ENABLE_FEATURE_FDISK_ADVANCED
2278 static void
2279 x_list_table(int extend)
2280 {
2281         const struct pte *pe;
2282         const struct partition *p;
2283         int i;
2284
2285         printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
2286                 disk_device, g_heads, g_sectors, g_cylinders);
2287         puts("Nr AF  Hd Sec  Cyl  Hd Sec  Cyl      Start       Size ID");
2288         for (i = 0; i < g_partitions; i++) {
2289                 pe = &ptes[i];
2290                 p = (extend ? pe->ext_pointer : pe->part_table);
2291                 if (p != NULL) {
2292                         printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n",
2293                                 i + 1, p->boot_ind,
2294                                 p->head,
2295                                 sector(p->sector),
2296                                 cylinder(p->sector, p->cyl),
2297                                 p->end_head,
2298                                 sector(p->end_sector),
2299                                 cylinder(p->end_sector, p->end_cyl),
2300                                 get_start_sect(p),
2301                                 get_nr_sects(p),
2302                                 p->sys_ind
2303                         );
2304                         if (p->sys_ind)
2305                                 check_consistency(p, i);
2306                 }
2307         }
2308 }
2309 #endif
2310
2311 #if ENABLE_FEATURE_FDISK_WRITABLE
2312 static void
2313 fill_bounds(sector_t *first, sector_t *last)
2314 {
2315         unsigned i;
2316         const struct pte *pe = &ptes[0];
2317         const struct partition *p;
2318
2319         for (i = 0; i < g_partitions; pe++,i++) {
2320                 p = pe->part_table;
2321                 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
2322                         first[i] = 0xffffffff;
2323                         last[i] = 0;
2324                 } else {
2325                         first[i] = get_partition_start_from_dev_start(pe);
2326                         last[i] = first[i] + get_nr_sects(p) - 1;
2327                 }
2328         }
2329 }
2330
2331 static void
2332 check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
2333 {
2334         sector_t total, real_s, real_c;
2335
2336         real_s = sector(s) - 1;
2337         real_c = cylinder(s, c);
2338         total = (real_c * g_sectors + real_s) * g_heads + h;
2339         if (!total)
2340                 printf("Partition %u contains sector 0\n", n);
2341         if (h >= g_heads)
2342                 printf("Partition %u: head %u greater than maximum %u\n",
2343                         n, h + 1, g_heads);
2344         if (real_s >= g_sectors)
2345                 printf("Partition %u: sector %u greater than "
2346                         "maximum %u\n", n, s, g_sectors);
2347         if (real_c >= g_cylinders)
2348                 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2349                         "maximum %u\n", n, real_c + 1, g_cylinders);
2350         if (g_cylinders <= 1024 && start != total)
2351                 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2352                         "total %"SECT_FMT"u\n", n, start, total);
2353 }
2354
2355 static void
2356 verify(void)
2357 {
2358         int i, j;
2359         sector_t total = 1;
2360         sector_t chs_size;
2361         sector_t first[g_partitions], last[g_partitions];
2362         struct partition *p;
2363
2364         if (warn_geometry())
2365                 return;
2366
2367         if (LABEL_IS_SUN) {
2368                 verify_sun();
2369                 return;
2370         }
2371         if (LABEL_IS_SGI) {
2372                 verify_sgi(1);
2373                 return;
2374         }
2375
2376         fill_bounds(first, last);
2377         for (i = 0; i < g_partitions; i++) {
2378                 struct pte *pe = &ptes[i];
2379
2380                 p = pe->part_table;
2381                 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
2382                         check_consistency(p, i);
2383                         if (get_partition_start_from_dev_start(pe) < first[i])
2384                                 printf("Warning: bad start-of-data in "
2385                                         "partition %u\n", i + 1);
2386                         check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2387                                 last[i]);
2388                         total += last[i] + 1 - first[i];
2389                         for (j = 0; j < i; j++) {
2390                                 if ((first[i] >= first[j] && first[i] <= last[j])
2391                                  || ((last[i] <= last[j] && last[i] >= first[j]))) {
2392                                         printf("Warning: partition %u overlaps "
2393                                                 "partition %u\n", j + 1, i + 1);
2394                                         total += first[i] >= first[j] ?
2395                                                 first[i] : first[j];
2396                                         total -= last[i] <= last[j] ?
2397                                                 last[i] : last[j];
2398                                 }
2399                         }
2400                 }
2401         }
2402
2403         if (extended_offset) {
2404                 struct pte *pex = &ptes[ext_index];
2405                 sector_t e_last = get_start_sect(pex->part_table) +
2406                         get_nr_sects(pex->part_table) - 1;
2407
2408                 for (i = 4; i < g_partitions; i++) {
2409                         total++;
2410                         p = ptes[i].part_table;
2411                         if (!p->sys_ind) {
2412                                 if (i != 4 || i + 1 < g_partitions)
2413                                         printf("Warning: partition %u "
2414                                                 "is empty\n", i + 1);
2415                         } else if (first[i] < extended_offset || last[i] > e_last) {
2416                                 printf("Logical partition %u not entirely in "
2417                                         "partition %u\n", i + 1, ext_index + 1);
2418                         }
2419                 }
2420         }
2421
2422         chs_size = (sector_t)g_heads * g_sectors * g_cylinders;
2423         if (total > chs_size)
2424                 printf("Total allocated sectors %u"
2425                         " greater than CHS size %"SECT_FMT"u\n",
2426                         total, chs_size
2427                 );
2428         else {
2429                 total = chs_size - total;
2430                 if (total != 0)
2431                         printf("%"SECT_FMT"u unallocated sectors\n", total);
2432         }
2433 }
2434
2435 static void
2436 add_partition(int n, int sys)
2437 {
2438         char mesg[256];         /* 48 does not suffice in Japanese */
2439         int i, num_read = 0;
2440         struct partition *p = ptes[n].part_table;
2441         struct partition *q = ptes[ext_index].part_table;
2442         sector_t limit, temp;
2443         sector_t start, stop = 0;
2444         sector_t first[g_partitions], last[g_partitions];
2445
2446         if (p && p->sys_ind) {
2447                 printf(msg_part_already_defined, n + 1);
2448                 return;
2449         }
2450         fill_bounds(first, last);
2451         if (n < 4) {
2452                 start = sector_offset;
2453                 if (display_in_cyl_units || !total_number_of_sectors)
2454                         limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
2455                 else
2456                         limit = total_number_of_sectors - 1;
2457                 if (extended_offset) {
2458                         first[ext_index] = extended_offset;
2459                         last[ext_index] = get_start_sect(q) +
2460                                 get_nr_sects(q) - 1;
2461                 }
2462         } else {
2463                 start = extended_offset + sector_offset;
2464                 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2465         }
2466         if (display_in_cyl_units)
2467                 for (i = 0; i < g_partitions; i++)
2468                         first[i] = (cround(first[i]) - 1) * units_per_sector;
2469
2470         snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
2471         do {
2472                 temp = start;
2473                 for (i = 0; i < g_partitions; i++) {
2474                         int lastplusoff;
2475
2476                         if (start == ptes[i].offset_from_dev_start)
2477                                 start += sector_offset;
2478                         lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
2479                         if (start >= first[i] && start <= lastplusoff)
2480                                 start = lastplusoff + 1;
2481                 }
2482                 if (start > limit)
2483                         break;
2484                 if (start >= temp+units_per_sector && num_read) {
2485                         printf("Sector %"SECT_FMT"u is already allocated\n", temp);
2486                         temp = start;
2487                         num_read = 0;
2488                 }
2489                 if (!num_read && start == temp) {
2490                         sector_t saved_start;
2491
2492                         saved_start = start;
2493                         start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
2494                         if (display_in_cyl_units) {
2495                                 start = (start - 1) * units_per_sector;
2496                                 if (start < saved_start)
2497                                         start = saved_start;
2498                         }
2499                         num_read = 1;
2500                 }
2501         } while (start != temp || !num_read);
2502         if (n > 4) {                    /* NOT for fifth partition */
2503                 struct pte *pe = &ptes[n];
2504
2505                 pe->offset_from_dev_start = start - sector_offset;
2506                 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2507                         pe->offset_from_dev_start++;
2508                         if (sector_offset == 1)
2509                                 start++;
2510                 }
2511         }
2512
2513         for (i = 0; i < g_partitions; i++) {
2514                 struct pte *pe = &ptes[i];
2515
2516                 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2517                         limit = pe->offset_from_dev_start - 1;
2518                 if (start < first[i] && limit >= first[i])
2519                         limit = first[i] - 1;
2520         }
2521         if (start > limit) {
2522                 puts("No free sectors available");
2523                 if (n > 4)
2524                         g_partitions--;
2525                 return;
2526         }
2527         if (cround(start) == cround(limit)) {
2528                 stop = limit;
2529         } else {
2530                 snprintf(mesg, sizeof(mesg),
2531                          "Last %s or +size or +sizeM or +sizeK",
2532                          str_units(SINGULAR));
2533                 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
2534                 if (display_in_cyl_units) {
2535                         stop = stop * units_per_sector - 1;
2536                         if (stop >limit)
2537                                 stop = limit;
2538                 }
2539         }
2540
2541         set_partition(n, 0, start, stop, sys);
2542         if (n > 4)
2543                 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
2544
2545         if (IS_EXTENDED(sys)) {
2546                 struct pte *pe4 = &ptes[4];
2547                 struct pte *pen = &ptes[n];
2548
2549                 ext_index = n;
2550                 pen->ext_pointer = p;
2551                 pe4->offset_from_dev_start = extended_offset = start;
2552                 pe4->sectorbuffer = xzalloc(sector_size);
2553                 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2554                 pe4->ext_pointer = pe4->part_table + 1;
2555                 pe4->changed = 1;
2556                 g_partitions = 5;
2557         }
2558 }
2559
2560 static void
2561 add_logical(void)
2562 {
2563         if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2564                 struct pte *pe = &ptes[g_partitions];
2565
2566                 pe->sectorbuffer = xzalloc(sector_size);
2567                 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2568                 pe->ext_pointer = pe->part_table + 1;
2569                 pe->offset_from_dev_start = 0;
2570                 pe->changed = 1;
2571                 g_partitions++;
2572         }
2573         add_partition(g_partitions - 1, LINUX_NATIVE);
2574 }
2575
2576 static void
2577 new_partition(void)
2578 {
2579         int i, free_primary = 0;
2580
2581         if (warn_geometry())
2582                 return;
2583
2584         if (LABEL_IS_SUN) {
2585                 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
2586                 return;
2587         }
2588         if (LABEL_IS_SGI) {
2589                 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
2590                 return;
2591         }
2592         if (LABEL_IS_AIX) {
2593                 puts("Sorry - this fdisk cannot handle AIX disk labels.\n"
2594 "If you want to add DOS-type partitions, create a new empty DOS partition\n"
2595 "table first (use 'o'). This will destroy the present disk contents.");
2596                 return;
2597         }
2598
2599         for (i = 0; i < 4; i++)
2600                 free_primary += !ptes[i].part_table->sys_ind;
2601
2602         if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
2603                 puts("The maximum number of partitions has been created");
2604                 return;
2605         }
2606
2607         if (!free_primary) {
2608                 if (extended_offset)
2609                         add_logical();
2610                 else
2611                         puts("You must delete some partition and add "
2612                                  "an extended partition first");
2613         } else {
2614                 char c, line[80];
2615                 snprintf(line, sizeof(line),
2616                         "Command action\n"
2617                         "   %s\n"
2618                         "   p   primary partition (1-4)\n",
2619                         (extended_offset ?
2620                         "l   logical (5 or over)" : "e   extended"));
2621                 while (1) {
2622                         c = read_nonempty(line);
2623                         if ((c | 0x20) == 'p') {
2624                                 i = get_nonexisting_partition(0, 4);
2625                                 if (i >= 0)
2626                                         add_partition(i, LINUX_NATIVE);
2627                                 return;
2628                         }
2629                         if (c == 'l' && extended_offset) {
2630                                 add_logical();
2631                                 return;
2632                         }
2633                         if (c == 'e' && !extended_offset) {
2634                                 i = get_nonexisting_partition(0, 4);
2635                                 if (i >= 0)
2636                                         add_partition(i, EXTENDED);
2637                                 return;
2638                         }
2639                         printf("Invalid partition number "
2640                                          "for type '%c'\n", c);
2641                 }
2642         }
2643 }
2644
2645 static void
2646 reread_partition_table(int leave)
2647 {
2648         int i;
2649
2650         puts("Calling ioctl() to re-read partition table");
2651         sync();
2652         /* Users with slow external USB disks on a 320MHz ARM system (year 2011)
2653          * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
2654          */
2655         sleep(1);
2656         i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
2657                         "WARNING: rereading partition table "
2658                         "failed, kernel still uses old table");
2659 #if 0
2660         if (dos_changed)
2661                 puts(
2662                 "\nWARNING: If you have created or modified any DOS 6.x\n"
2663                 "partitions, please see the fdisk manual page for additional\n"
2664                 "information");
2665 #endif
2666
2667         if (leave) {
2668                 if (ENABLE_FEATURE_CLEAN_UP)
2669                         close_dev_fd();
2670                 exit(i != 0);
2671         }
2672 }
2673
2674 static void
2675 write_table(void)
2676 {
2677         int i;
2678
2679         if (LABEL_IS_DOS) {
2680                 for (i = 0; i < 3; i++)
2681                         if (ptes[i].changed)
2682                                 ptes[3].changed = 1;
2683                 for (i = 3; i < g_partitions; i++) {
2684                         struct pte *pe = &ptes[i];
2685                         if (pe->changed) {
2686                                 write_part_table_flag(pe->sectorbuffer);
2687                                 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
2688                         }
2689                 }
2690         }
2691         else if (LABEL_IS_SGI) {
2692                 /* no test on change? the "altered" msg below might be mistaken */
2693                 sgi_write_table();
2694         }
2695         else if (LABEL_IS_SUN) {
2696                 for (i = 0; i < 8; i++) {
2697                         if (ptes[i].changed) {
2698                                 sun_write_table();
2699                                 break;
2700                         }
2701                 }
2702         }
2703
2704         puts("The partition table has been altered.");
2705         reread_partition_table(1);
2706 }
2707 #endif /* FEATURE_FDISK_WRITABLE */
2708
2709 #if ENABLE_FEATURE_FDISK_ADVANCED
2710 #define MAX_PER_LINE    16
2711 static void
2712 print_buffer(char *pbuffer)
2713 {
2714         int i,l;
2715
2716         for (i = 0, l = 0; i < sector_size; i++, l++) {
2717                 if (l == 0)
2718                         printf("0x%03X:", i);
2719                 printf(" %02X", (unsigned char) pbuffer[i]);
2720                 if (l == MAX_PER_LINE - 1) {
2721                         bb_putchar('\n');
2722                         l = -1;
2723                 }
2724         }
2725         if (l > 0)
2726                 bb_putchar('\n');
2727         bb_putchar('\n');
2728 }
2729
2730 static void
2731 print_raw(void)
2732 {
2733         int i;
2734
2735         printf("Device: %s\n", disk_device);
2736         if (LABEL_IS_SGI || LABEL_IS_SUN)
2737                 print_buffer(MBRbuffer);
2738         else {
2739                 for (i = 3; i < g_partitions; i++)
2740                         print_buffer(ptes[i].sectorbuffer);
2741         }
2742 }
2743
2744 static void
2745 move_begin(unsigned i)
2746 {
2747         struct pte *pe = &ptes[i];
2748         struct partition *p = pe->part_table;
2749         sector_t new, first, nr_sects;
2750
2751         if (warn_geometry())
2752                 return;
2753         nr_sects = get_nr_sects(p);
2754         if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
2755                 printf("Partition %u has no data area\n", i + 1);
2756                 return;
2757         }
2758         first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
2759         new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2760         if (new != first) {
2761                 sector_t new_relative = new - pe->offset_from_dev_start;
2762                 nr_sects += (get_start_sect(p) - new_relative);
2763                 set_start_sect(p, new_relative);
2764                 set_nr_sects(p, nr_sects);
2765                 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2766                 if ((line_ptr[0] | 0x20) == 'y')
2767                         set_hsc_start_end(p, new, new + nr_sects - 1);
2768                 pe->changed = 1;
2769         }
2770 }
2771
2772 static void
2773 xselect(void)
2774 {
2775         char c;
2776
2777         while (1) {
2778                 bb_putchar('\n');
2779                 c = 0x20 | read_nonempty("Expert command (m for help): ");
2780                 switch (c) {
2781                 case 'a':
2782                         if (LABEL_IS_SUN)
2783                                 sun_set_alt_cyl();
2784                         break;
2785                 case 'b':
2786                         if (LABEL_IS_DOS)
2787                                 move_begin(get_partition(0, g_partitions));
2788                         break;
2789                 case 'c':
2790                         user_cylinders = g_cylinders =
2791                                 read_int(1, g_cylinders, 1048576, 0,
2792                                         "Number of cylinders");
2793                         if (LABEL_IS_SUN)
2794                                 sun_set_ncyl(g_cylinders);
2795                         if (LABEL_IS_DOS)
2796                                 warn_cylinders();
2797                         break;
2798                 case 'd':
2799                         print_raw();
2800                         break;
2801                 case 'e':
2802                         if (LABEL_IS_SGI)
2803                                 sgi_set_xcyl();
2804                         else if (LABEL_IS_SUN)
2805                                 sun_set_xcyl();
2806                         else if (LABEL_IS_DOS)
2807                                 x_list_table(1);
2808                         break;
2809                 case 'f':
2810                         if (LABEL_IS_DOS)
2811                                 fix_partition_table_order();
2812                         break;
2813                 case 'g':
2814 #if ENABLE_FEATURE_SGI_LABEL
2815                         create_sgilabel();
2816 #endif
2817                         break;
2818                 case 'h':
2819                         user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
2820                         update_units();
2821                         break;
2822                 case 'i':
2823                         if (LABEL_IS_SUN)
2824                                 sun_set_ilfact();
2825                         break;
2826                 case 'o':
2827                         if (LABEL_IS_SUN)
2828                                 sun_set_rspeed();
2829                         break;
2830                 case 'p':
2831                         if (LABEL_IS_SUN)
2832                                 list_table(1);
2833                         else
2834                                 x_list_table(0);
2835                         break;
2836                 case 'q':
2837                         if (ENABLE_FEATURE_CLEAN_UP)
2838                                 close_dev_fd();
2839                         bb_putchar('\n');
2840                         exit(EXIT_SUCCESS);
2841                 case 'r':
2842                         return;
2843                 case 's':
2844                         user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
2845                         if (dos_compatible_flag) {
2846                                 sector_offset = g_sectors;
2847                                 puts("Warning: setting sector offset for DOS "
2848                                         "compatiblity");
2849                         }
2850                         update_units();
2851                         break;
2852                 case 'v':
2853                         verify();
2854                         break;
2855                 case 'w':
2856                         write_table();  /* does not return */
2857                         break;
2858                 case 'y':
2859                         if (LABEL_IS_SUN)
2860                                 sun_set_pcylcount();
2861                         break;
2862                 default:
2863                         xmenu();
2864                 }
2865         }
2866 }
2867 #endif /* ADVANCED mode */
2868
2869 static int
2870 is_ide_cdrom_or_tape(const char *device)
2871 {
2872         FILE *procf;
2873         char buf[100];
2874         struct stat statbuf;
2875         int is_ide = 0;
2876
2877         /* No device was given explicitly, and we are trying some
2878            likely things.  But opening /dev/hdc may produce errors like
2879            "hdc: tray open or drive not ready"
2880            if it happens to be a CD-ROM drive. It even happens that
2881            the process hangs on the attempt to read a music CD.
2882            So try to be careful. This only works since 2.1.73. */
2883
2884         if (!is_prefixed_with(device, "/dev/hd"))
2885                 return 0;
2886
2887         snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2888         procf = fopen_for_read(buf);
2889         if (procf != NULL && fgets(buf, sizeof(buf), procf))
2890                 is_ide = (is_prefixed_with(buf, "cdrom") ||
2891                           is_prefixed_with(buf, "tape"));
2892         else
2893                 /* Now when this proc file does not exist, skip the
2894                    device when it is read-only. */
2895                 if (stat(device, &statbuf) == 0)
2896                         is_ide = ((statbuf.st_mode & 0222) == 0);
2897
2898         if (procf)
2899                 fclose(procf);
2900         return is_ide;
2901 }
2902
2903
2904 static void
2905 open_list_and_close(const char *device, int user_specified)
2906 {
2907         int gb;
2908
2909         disk_device = device;
2910         if (setjmp(listingbuf))
2911                 return;
2912         if (!user_specified)
2913                 if (is_ide_cdrom_or_tape(device))
2914                         return;
2915
2916         /* Open disk_device, save file descriptor to dev_fd */
2917         errno = 0;
2918         gb = get_boot(TRY_ONLY);
2919         if (gb > 0) {   /* I/O error */
2920                 /* Ignore other errors, since we try IDE
2921                    and SCSI hard disks which may not be
2922                    installed on the system. */
2923                 if (user_specified || errno == EACCES)
2924                         bb_perror_msg("can't open '%s'", device);
2925                 return;
2926         }
2927
2928         if (gb < 0) { /* no DOS signature */
2929                 list_disk_geometry();
2930                 if (LABEL_IS_AIX)
2931                         goto ret;
2932 #if ENABLE_FEATURE_OSF_LABEL
2933                 if (bsd_trydev(device) < 0)
2934 #endif
2935                         printf("Disk %s doesn't contain a valid "
2936                                 "partition table\n", device);
2937         } else {
2938                 list_table(0);
2939 #if ENABLE_FEATURE_FDISK_WRITABLE
2940                 if (!LABEL_IS_SUN && g_partitions > 4) {
2941                         delete_partition(ext_index);
2942                 }
2943 #endif
2944         }
2945  ret:
2946         close_dev_fd();
2947 }
2948
2949 /* Is it a whole disk? The digit check is still useful
2950    for Xen devices for example. */
2951 static int is_whole_disk(const char *disk)
2952 {
2953         unsigned len;
2954         int fd = open(disk, O_RDONLY);
2955
2956         if (fd != -1) {
2957                 struct hd_geometry geometry;
2958                 int err = ioctl(fd, HDIO_GETGEO, &geometry);
2959                 close(fd);
2960                 if (!err)
2961                         return (geometry.start == 0);
2962         }
2963
2964         /* Treat "nameN" as a partition name, not whole disk */
2965         /* note: mmcblk0 should work from the geometry check above */
2966         len = strlen(disk);
2967         if (len != 0 && isdigit(disk[len - 1]))
2968                 return 0;
2969
2970         return 1;
2971 }
2972
2973 /* for fdisk -l: try all things in /proc/partitions
2974    that look like a partition name (do not end in a digit) */
2975 static void
2976 list_devs_in_proc_partititons(void)
2977 {
2978         FILE *procpt;
2979         char line[100], ptname[100], devname[120];
2980         int ma, mi, sz;
2981
2982         procpt = fopen_or_warn("/proc/partitions", "r");
2983
2984         while (fgets(line, sizeof(line), procpt)) {
2985                 if (sscanf(line, " %u %u %u %[^\n ]",
2986                                 &ma, &mi, &sz, ptname) != 4)
2987                         continue;
2988
2989                 sprintf(devname, "/dev/%s", ptname);
2990                 if (is_whole_disk(devname))
2991                         open_list_and_close(devname, 0);
2992         }
2993 #if ENABLE_FEATURE_CLEAN_UP
2994         fclose(procpt);
2995 #endif
2996 }
2997
2998 #if ENABLE_FEATURE_FDISK_WRITABLE
2999 static void
3000 unknown_command(int c)
3001 {
3002         printf("%c: unknown command\n", c);
3003 }
3004 #endif
3005
3006 int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3007 int fdisk_main(int argc UNUSED_PARAM, char **argv)
3008 {
3009         unsigned opt;
3010         /*
3011          *  fdisk -v
3012          *  fdisk -l [-b sectorsize] [-u] device ...
3013          *  fdisk -s [partition] ...
3014          *  fdisk [-b sectorsize] [-u] device
3015          *
3016          * Options -C, -H, -S set the geometry.
3017          */
3018         INIT_G();
3019
3020         close_dev_fd(); /* needed: fd 3 must not stay closed */
3021
3022         opt = getopt32(argv, "b:+C:+H:+lS:+u" IF_FEATURE_FDISK_BLKSIZE("s"),
3023                                 &sector_size, &user_cylinders, &user_heads, &user_sectors);
3024         argv += optind;
3025         if (opt & OPT_b) {
3026                 /* Ugly: this sector size is really per device,
3027                  * so cannot be combined with multiple disks,
3028                  * and the same goes for the C/H/S options.
3029                  */
3030                 if (sector_size < 512
3031                  || sector_size > 0x10000
3032                  || (sector_size & (sector_size-1)) /* not power of 2 */
3033                 ) {
3034                         bb_show_usage();
3035                 }
3036                 sector_offset = 2;
3037                 user_set_sector_size = 1;
3038         }
3039         if (user_heads <= 0 || user_heads >= 256)
3040                 user_heads = 0;
3041         if (user_sectors <= 0 || user_sectors >= 64)
3042                 user_sectors = 0;
3043         if (opt & OPT_u)
3044                 display_in_cyl_units = 0; // -u
3045
3046 #if ENABLE_FEATURE_FDISK_WRITABLE
3047         if (opt & OPT_l) {
3048                 nowarn = 1;
3049 #endif
3050                 if (*argv) {
3051                         listing = 1;
3052                         do {
3053                                 open_list_and_close(*argv, 1);
3054                         } while (*++argv);
3055                 } else {
3056                         /* we don't have device names, */
3057                         /* use /proc/partitions instead */
3058                         list_devs_in_proc_partititons();
3059                 }
3060                 return 0;
3061 #if ENABLE_FEATURE_FDISK_WRITABLE
3062         }
3063 #endif
3064
3065 #if ENABLE_FEATURE_FDISK_BLKSIZE
3066         if (opt & OPT_s) {
3067                 int j;
3068
3069                 nowarn = 1;
3070                 if (!argv[0])
3071                         bb_show_usage();
3072                 for (j = 0; argv[j]; j++) {
3073                         unsigned long long size;
3074                         fd = xopen(argv[j], O_RDONLY);
3075                         size = bb_BLKGETSIZE_sectors(fd) / 2;
3076                         close(fd);
3077                         if (argv[1])
3078                                 printf("%llu\n", size);
3079                         else
3080                                 printf("%s: %llu\n", argv[j], size);
3081                 }
3082                 return 0;
3083         }
3084 #endif
3085
3086 #if ENABLE_FEATURE_FDISK_WRITABLE
3087         if (!argv[0] || argv[1])
3088                 bb_show_usage();
3089
3090         disk_device = argv[0];
3091         get_boot(OPEN_MAIN);
3092
3093         if (LABEL_IS_OSF) {
3094                 /* OSF label, and no DOS label */
3095                 printf("Detected an OSF/1 disklabel on %s, entering "
3096                         "disklabel mode\n", disk_device);
3097                 bsd_select();
3098                 /*Why do we do this?  It seems to be counter-intuitive*/
3099                 current_label_type = LABEL_DOS;
3100                 /* If we return we may want to make an empty DOS label? */
3101         }
3102
3103         while (1) {
3104                 int c;
3105                 bb_putchar('\n');
3106                 c = 0x20 | read_nonempty("Command (m for help): ");
3107                 switch (c) {
3108                 case 'a':
3109                         if (LABEL_IS_DOS)
3110                                 toggle_active(get_partition(1, g_partitions));
3111                         else if (LABEL_IS_SUN)
3112                                 toggle_sunflags(get_partition(1, g_partitions),
3113                                                 0x01);
3114                         else if (LABEL_IS_SGI)
3115                                 sgi_set_bootpartition(
3116                                         get_partition(1, g_partitions));
3117                         else
3118                                 unknown_command(c);
3119                         break;
3120                 case 'b':
3121                         if (LABEL_IS_SGI) {
3122                                 printf("\nThe current boot file is: %s\n",
3123                                         sgi_get_bootfile());
3124                                 if (read_maybe_empty("Please enter the name of the "
3125                                                 "new boot file: ") == '\n')
3126                                         puts("Boot file unchanged");
3127                                 else
3128                                         sgi_set_bootfile(line_ptr);
3129                         }
3130 #if ENABLE_FEATURE_OSF_LABEL
3131                         else
3132                                 bsd_select();
3133 #endif
3134                         break;
3135                 case 'c':
3136                         if (LABEL_IS_DOS)
3137                                 toggle_dos_compatibility_flag();
3138                         else if (LABEL_IS_SUN)
3139                                 toggle_sunflags(get_partition(1, g_partitions),
3140                                                 0x10);
3141                         else if (LABEL_IS_SGI)
3142                                 sgi_set_swappartition(
3143                                                 get_partition(1, g_partitions));
3144                         else
3145                                 unknown_command(c);
3146                         break;
3147                 case 'd':
3148                         {
3149                                 int j;
3150                         /* If sgi_label then don't use get_existing_partition,
3151                            let the user select a partition, since
3152                            get_existing_partition() only works for Linux-like
3153                            partition tables */
3154                                 if (!LABEL_IS_SGI) {
3155                                         j = get_existing_partition(1, g_partitions);
3156                                 } else {
3157                                         j = get_partition(1, g_partitions);
3158                                 }
3159                                 if (j >= 0)
3160                                         delete_partition(j);
3161                         }
3162                         break;
3163                 case 'i':
3164                         if (LABEL_IS_SGI)
3165                                 create_sgiinfo();
3166                         else
3167                                 unknown_command(c);
3168                 case 'l':
3169                         list_types(get_sys_types());
3170                         break;
3171                 case 'm':
3172                         menu();
3173                         break;
3174                 case 'n':
3175                         new_partition();
3176                         break;
3177                 case 'o':
3178                         create_doslabel();
3179                         break;
3180                 case 'p':
3181                         list_table(0);
3182                         break;
3183                 case 'q':
3184                         if (ENABLE_FEATURE_CLEAN_UP)
3185                                 close_dev_fd();
3186                         bb_putchar('\n');
3187                         return 0;
3188                 case 's':
3189 #if ENABLE_FEATURE_SUN_LABEL
3190                         create_sunlabel();
3191 #endif
3192                         break;
3193                 case 't':
3194                         change_sysid();
3195                         break;
3196                 case 'u':
3197                         change_units();
3198                         break;
3199                 case 'v':
3200                         verify();
3201                         break;
3202                 case 'w':
3203                         write_table();  /* does not return */
3204                         break;
3205 #if ENABLE_FEATURE_FDISK_ADVANCED
3206                 case 'x':
3207                         if (LABEL_IS_SGI) {
3208                                 puts("\n\tSorry, no experts menu for SGI "
3209                                         "partition tables available\n");
3210                         } else
3211                                 xselect();
3212                         break;
3213 #endif
3214                 default:
3215                         unknown_command(c);
3216                         menu();
3217                 }
3218         }
3219         return 0;
3220 #endif /* FEATURE_FDISK_WRITABLE */
3221 }