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