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