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