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