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