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