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