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