blob: e50ee77d15bcc8acb2fa4b0fbbe85028f98cff14 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000010#ifndef _LARGEFILE64_SOURCE
11/* For lseek64 */
12#define _LARGEFILE64_SOURCE
13#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000014#include <assert.h> /* assert */
Denys Vlasenkoda49f582009-07-08 02:58:38 +020015#include <sys/mount.h>
16#if !defined(BLKSSZGET)
17# define BLKSSZGET _IO(0x12, 104)
18#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000020
Denis Vlasenko834410a2006-11-29 12:00:28 +000021/* Looks like someone forgot to add this to config system */
22#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
23# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000024# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +000025#endif
26
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000027#define DEFAULT_SECTOR_SIZE 512
28#define DEFAULT_SECTOR_SIZE_STR "512"
29#define MAX_SECTOR_SIZE 2048
30#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
31#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000032
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000033#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000034
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000035#define EXTENDED 0x05
36#define WIN98_EXTENDED 0x0f
37#define LINUX_PARTITION 0x81
38#define LINUX_SWAP 0x82
39#define LINUX_NATIVE 0x83
40#define LINUX_EXTENDED 0x85
41#define LINUX_LVM 0x8e
42#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000043
Denis Vlasenkoc033d512008-04-17 01:52:28 +000044
45enum {
46 OPT_b = 1 << 0,
47 OPT_C = 1 << 1,
48 OPT_H = 1 << 2,
49 OPT_l = 1 << 3,
50 OPT_S = 1 << 4,
51 OPT_u = 1 << 5,
52 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
53};
54
55
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000056typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +020057/* Used for sector numbers. Partition formats we know
58 * do not support more than 2^32 sectors
59 */
60typedef uint32_t sector_t;
61#if UINT_MAX == 4294967295
62# define SECT_FMT ""
63#elif ULONG_MAX == 4294967295
64# define SECT_FMT "l"
65#else
66# error Cant detect sizeof(uint32_t)
67#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000068
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000069struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000070 unsigned char heads;
71 unsigned char sectors;
72 unsigned short cylinders;
73 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000074};
75
Denis Vlasenko98ae2162006-10-12 19:30:44 +000076#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000077
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000078static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000079"Building a new %s. Changes will remain in memory only,\n"
80"until you decide to write them. After that the previous content\n"
81"won't be recoverable.\n\n";
82
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000083static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +020084"Partition %u is already defined, delete it before re-adding\n";
Denis Vlasenkobd852072007-03-19 14:43:38 +000085
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000086
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000087struct partition {
88 unsigned char boot_ind; /* 0x80 - active */
89 unsigned char head; /* starting head */
90 unsigned char sector; /* starting sector */
91 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +000092 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000093 unsigned char end_head; /* end head */
94 unsigned char end_sector; /* end sector */
95 unsigned char end_cyl; /* end cylinder */
96 unsigned char start4[4]; /* starting sector counting from 0 */
97 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000098} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000099
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000100static const char unable_to_open[] ALIGN1 = "can't open '%s'";
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000101static const char unable_to_read[] ALIGN1 = "can't read from %s";
102static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000103
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000104enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +0000105 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +0000106};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000107
Denis Vlasenko4437d192008-04-17 00:12:10 +0000108#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000109
Denis Vlasenko834410a2006-11-29 12:00:28 +0000110#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000111#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000113#else
114#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000115#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000117
Denis Vlasenko834410a2006-11-29 12:00:28 +0000118#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000119#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000120#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000121#else
122#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000123#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000124#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000125
Denis Vlasenko834410a2006-11-29 12:00:28 +0000126#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000127#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000128#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000129#else
130#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000131#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000132#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000133
Denis Vlasenko834410a2006-11-29 12:00:28 +0000134#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000135#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000136#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000137#else
138#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000139#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000140#endif
Rob Landley5527b912006-02-25 03:46:10 +0000141
Denis Vlasenko4437d192008-04-17 00:12:10 +0000142enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000143
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000144static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000145#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000146static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000147static void reread_partition_table(int leave);
148static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200149static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000150static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200151static sector_t read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000152#endif
153static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000154static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000155#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000156static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000157#else
158static int get_boot(void);
159#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000160
161#define PLURAL 0
162#define SINGULAR 1
163
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200164static sector_t get_start_sect(const struct partition *p);
165static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000166
167/*
168 * per partition table entry data
169 *
170 * The four primary partitions have the same sectorbuffer (MBRbuffer)
171 * and have NULL ext_pointer.
172 * Each logical partition table entry has two pointers, one for the
173 * partition and one link to the next one.
174 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000175struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000176 struct partition *part_table; /* points into sectorbuffer */
177 struct partition *ext_pointer; /* points into sectorbuffer */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200178 sector_t offset; /* disk sector number */
179 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000180#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000181 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000182#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000183};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000184
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000185/* DOS partition types */
186
187static const char *const i386_sys_types[] = {
188 "\x00" "Empty",
189 "\x01" "FAT12",
190 "\x04" "FAT16 <32M",
191 "\x05" "Extended", /* DOS 3.3+ extended partition */
192 "\x06" "FAT16", /* DOS 16-bit >=32M */
193 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
194 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
195 "\x0b" "Win95 FAT32",
196 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
197 "\x0e" "Win95 FAT16 (LBA)",
198 "\x0f" "Win95 Ext'd (LBA)",
199 "\x11" "Hidden FAT12",
200 "\x12" "Compaq diagnostics",
201 "\x14" "Hidden FAT16 <32M",
202 "\x16" "Hidden FAT16",
203 "\x17" "Hidden HPFS/NTFS",
204 "\x1b" "Hidden Win95 FAT32",
205 "\x1c" "Hidden W95 FAT32 (LBA)",
206 "\x1e" "Hidden W95 FAT16 (LBA)",
207 "\x3c" "Part.Magic recovery",
208 "\x41" "PPC PReP Boot",
209 "\x42" "SFS",
210 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
211 "\x80" "Old Minix", /* Minix 1.4a and earlier */
212 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
213 "\x82" "Linux swap", /* also Solaris */
214 "\x83" "Linux",
215 "\x84" "OS/2 hidden C: drive",
216 "\x85" "Linux extended",
217 "\x86" "NTFS volume set",
218 "\x87" "NTFS volume set",
219 "\x8e" "Linux LVM",
220 "\x9f" "BSD/OS", /* BSDI */
221 "\xa0" "Thinkpad hibernation",
222 "\xa5" "FreeBSD", /* various BSD flavours */
223 "\xa6" "OpenBSD",
224 "\xa8" "Darwin UFS",
225 "\xa9" "NetBSD",
226 "\xab" "Darwin boot",
227 "\xb7" "BSDI fs",
228 "\xb8" "BSDI swap",
229 "\xbe" "Solaris boot",
230 "\xeb" "BeOS fs",
231 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
232 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
233 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
234 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
235 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
236 autodetect using persistent
237 superblock */
238#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
239 "\x02" "XENIX root",
240 "\x03" "XENIX usr",
241 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
242 "\x09" "AIX bootable", /* AIX data or Coherent */
243 "\x10" "OPUS",
244 "\x18" "AST SmartSleep",
245 "\x24" "NEC DOS",
246 "\x39" "Plan 9",
247 "\x40" "Venix 80286",
248 "\x4d" "QNX4.x",
249 "\x4e" "QNX4.x 2nd part",
250 "\x4f" "QNX4.x 3rd part",
251 "\x50" "OnTrack DM",
252 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
253 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
254 "\x53" "OnTrack DM6 Aux3",
255 "\x54" "OnTrackDM6",
256 "\x55" "EZ-Drive",
257 "\x56" "Golden Bow",
258 "\x5c" "Priam Edisk",
259 "\x61" "SpeedStor",
260 "\x64" "Novell Netware 286",
261 "\x65" "Novell Netware 386",
262 "\x70" "DiskSecure Multi-Boot",
263 "\x75" "PC/IX",
264 "\x93" "Amoeba",
265 "\x94" "Amoeba BBT", /* (bad block table) */
266 "\xa7" "NeXTSTEP",
267 "\xbb" "Boot Wizard hidden",
268 "\xc1" "DRDOS/sec (FAT-12)",
269 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
270 "\xc6" "DRDOS/sec (FAT-16)",
271 "\xc7" "Syrinx",
272 "\xda" "Non-FS data",
273 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
274 Concurrent DOS or CTOS */
275 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
276 "\xdf" "BootIt", /* BootIt EMBRM */
277 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
278 extended partition */
279 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
280 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
281 partition < 1024 cyl. */
282 "\xf1" "SpeedStor",
283 "\xf4" "SpeedStor", /* SpeedStor large partition */
284 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
285 "\xff" "BBT", /* Xenix Bad Block Table */
286#endif
287 NULL
288};
289
Denis Vlasenko4437d192008-04-17 00:12:10 +0000290enum {
291 dev_fd = 3 /* the disk */
292};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000293
294/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000295struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000296 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000297
298 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000299 int g_partitions; // = 4; /* maximum partition + 1 */
300 unsigned units_per_sector; // = 1;
301 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
302 unsigned user_set_sector_size;
303 unsigned sector_offset; // = 1;
304 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000305 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000306 smallint display_in_cyl_units; // = 1;
307#if ENABLE_FEATURE_OSF_LABEL
308 smallint possibly_osf_label;
309#endif
310
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000311 smallint listing; /* no aborts for fdisk -l */
312 smallint dos_compatible_flag; // = 1;
313#if ENABLE_FEATURE_FDISK_WRITABLE
314 //int dos_changed;
315 smallint nowarn; /* no warnings for fdisk -l/-s */
316#endif
317 int ext_index; /* the prime extended partition */
318 unsigned user_cylinders, user_heads, user_sectors;
319 unsigned pt_heads, pt_sectors;
320 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200321 sector_t extended_offset; /* offset of link pointers */
322 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000323
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000324 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000325 char line_buffer[80];
326 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000327 /* Raw disk label. For DOS-type partition tables the MBR,
328 * with descriptions of the primary partitions. */
329 char MBRbuffer[MAX_SECTOR_SIZE];
330 /* Partition tables */
331 struct pte ptes[MAXIMUM_PARTS];
332};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000333#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000334#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000335#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000336#define g_partitions (G.g_partitions )
337#define units_per_sector (G.units_per_sector )
338#define sector_size (G.sector_size )
339#define user_set_sector_size (G.user_set_sector_size)
340#define sector_offset (G.sector_offset )
341#define g_heads (G.g_heads )
342#define g_sectors (G.g_sectors )
343#define g_cylinders (G.g_cylinders )
344#define current_label_type (G.current_label_type )
345#define display_in_cyl_units (G.display_in_cyl_units)
346#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000347#define listing (G.listing )
348#define dos_compatible_flag (G.dos_compatible_flag )
349#define nowarn (G.nowarn )
350#define ext_index (G.ext_index )
351#define user_cylinders (G.user_cylinders )
352#define user_heads (G.user_heads )
353#define user_sectors (G.user_sectors )
354#define pt_heads (G.pt_heads )
355#define pt_sectors (G.pt_sectors )
356#define kern_heads (G.kern_heads )
357#define kern_sectors (G.kern_sectors )
358#define extended_offset (G.extended_offset )
359#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000360#define listingbuf (G.listingbuf )
361#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000362#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000363#define MBRbuffer (G.MBRbuffer )
364#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000365#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000366 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000367 sector_size = DEFAULT_SECTOR_SIZE; \
368 sector_offset = 1; \
369 g_partitions = 4; \
370 display_in_cyl_units = 1; \
371 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000372 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000373} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000374
Denis Vlasenkobd852072007-03-19 14:43:38 +0000375
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000376/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200377/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
378 * disks > 2^32 sectors
379 */
380static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000381{
382 uint64_t v64;
383 unsigned long longsectors;
384
385 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000386 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200387 v64 >>= 9;
388 if (v64 != (sector_t)v64) {
389 ret_trunc:
390 /* Not only DOS, but all other partition tables
391 * we support can't record more than 32 bit
392 * sector counts or offsets
393 */
394 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
395 v64 = (uint32_t)-1L;
396 }
397 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000398 }
399 /* Needs temp of type long */
400 if (ioctl(fd, BLKGETSIZE, &longsectors))
401 longsectors = 0;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200402 if (sizeof(long) > sizeof(sector_t)
403 && longsectors != (sector_t)longsectors
404 ) {
405 goto ret_trunc;
406 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000407 return longsectors;
408}
409
410
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000411#define IS_EXTENDED(i) \
412 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
413
414#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
415
416#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
417
418#define pt_offset(b, n) \
419 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
420
421#define sector(s) ((s) & 0x3f)
422
423#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
424
425#define hsc2sector(h,s,c) \
426 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
427
428#define set_hsc(h,s,c,sector) \
429 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000430 s = sector % g_sectors + 1; \
431 sector /= g_sectors; \
432 h = sector % g_heads; \
433 sector /= g_heads; \
434 c = sector & 0xff; \
435 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000436 } while (0)
437
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000438static void
439close_dev_fd(void)
440{
441 /* Not really closing, but making sure it is open, and to harmless place */
442 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
443}
444
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000445#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000446/* Read line; return 0 or first printable char */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000447static int
448read_line(const char *prompt)
449{
450 int sz;
451
452 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
453 if (sz <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000454 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000455
456 if (line_buffer[sz-1] == '\n')
457 line_buffer[--sz] = '\0';
458
459 line_ptr = line_buffer;
460 while (*line_ptr && !isgraph(*line_ptr))
461 line_ptr++;
462 return *line_ptr;
463}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000464#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000465
Denis Vlasenkobd852072007-03-19 14:43:38 +0000466/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000467 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000468 */
469static const char *
470partname(const char *dev, int pno, int lth)
471{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000472 const char *p;
473 int w, wp;
474 int bufsiz;
475 char *bufp;
476
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000477 bufp = partname_buffer;
478 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000479
480 w = strlen(dev);
481 p = "";
482
483 if (isdigit(dev[w-1]))
484 p = "p";
485
486 /* devfs kludge - note: fdisk partition names are not supposed
487 to equal kernel names, so there is no reason to do this */
488 if (strcmp(dev + w - 4, "disc") == 0) {
489 w -= 4;
490 p = "part";
491 }
492
493 wp = strlen(p);
494
495 if (lth) {
496 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
497 lth-wp-2, w, dev, p, pno);
498 } else {
499 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
500 }
501 return bufp;
502}
503
Denis Vlasenko834410a2006-11-29 12:00:28 +0000504#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000505static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000506set_all_unchanged(void)
507{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000508 int i;
509
510 for (i = 0; i < MAXIMUM_PARTS; i++)
511 ptes[i].changed = 0;
512}
513
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000514static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000515set_changed(int i)
516{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000517 ptes[i].changed = 1;
518}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000519#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000520
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000521static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000522get_part_table(int i)
523{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000524 return ptes[i].part_table;
525}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000526
527static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000528str_units(int n)
529{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000530 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000531 return display_in_cyl_units ? "cylinder" : "sector";
532 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000533}
534
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000535static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000536valid_part_table_flag(const char *mbuffer)
537{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000538 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000539}
540
Denis Vlasenko834410a2006-11-29 12:00:28 +0000541#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000542static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000543write_part_table_flag(char *b)
544{
545 b[510] = 0x55;
546 b[511] = 0xaa;
547}
548
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000549static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000550read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000551{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000552 while (!read_line(mesg))
553 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554 return *line_ptr;
555}
556
557static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000558read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000559{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000560 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000561 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000562 line_ptr[0] = '\n';
563 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000564 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000565 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000566}
567
568static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000569read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000570{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000571 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000572 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000573 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000574 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000575 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000576 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000577 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000578 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000579 if (v > 0xff)
580 /* Bad input also triggers this */
581 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000582 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000583 }
584}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000585#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000586
Denis Vlasenko9764d692008-07-09 21:20:50 +0000587static void fdisk_fatal(const char *why)
588{
589 if (listing) {
590 close_dev_fd();
591 longjmp(listingbuf, 1);
592 }
593 bb_error_msg_and_die(why, disk_device);
594}
595
596static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200597seek_sector(sector_t secno)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000598{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000599#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200600 off64_t off = (off64_t)secno * sector_size;
601 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000602 fdisk_fatal(unable_to_seek);
603#else
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200604 uint64_t off = (uint64_t)secno * sector_size;
605 if (off > MAXINT(off_t)
606 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
Denis Vlasenko9764d692008-07-09 21:20:50 +0000607 ) {
608 fdisk_fatal(unable_to_seek);
609 }
610#endif
611}
612
613#if ENABLE_FEATURE_FDISK_WRITABLE
614static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200615write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000616{
617 seek_sector(secno);
618 xwrite(dev_fd, buf, sector_size);
619}
620#endif
621
622
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000623#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624
625typedef struct {
626 unsigned char info[128]; /* Informative text string */
627 unsigned char spare0[14];
628 struct sun_info {
629 unsigned char spare1;
630 unsigned char id;
631 unsigned char spare2;
632 unsigned char flags;
633 } infos[8];
634 unsigned char spare1[246]; /* Boot information etc. */
635 unsigned short rspeed; /* Disk rotational speed */
636 unsigned short pcylcount; /* Physical cylinder count */
637 unsigned short sparecyl; /* extra sects per cylinder */
638 unsigned char spare2[4]; /* More magic... */
639 unsigned short ilfact; /* Interleave factor */
640 unsigned short ncyl; /* Data cylinder count */
641 unsigned short nacyl; /* Alt. cylinder count */
642 unsigned short ntrks; /* Tracks per cylinder */
643 unsigned short nsect; /* Sectors per track */
644 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000645 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000646 uint32_t start_cylinder;
647 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000648 } partitions[8];
649 unsigned short magic; /* Magic number */
650 unsigned short csum; /* Label xor'd checksum */
651} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000652#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000653STATIC_OSF void bsd_select(void);
654STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000655#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000656
Denis Vlasenko28703012006-12-19 20:32:02 +0000657#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000658static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000659fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000660{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000661 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000662}
663
Rob Landley88621d72006-08-29 19:41:06 +0000664static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000665fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000666{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000667 return (x << 24) |
668 ((x & 0xFF00) << 8) |
669 ((x & 0xFF0000) >> 8) |
670 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000671}
672#endif
673
Denis Vlasenkobd852072007-03-19 14:43:38 +0000674STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000675STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000676STATIC_SGI int sgi_get_sysid(int i);
677STATIC_SGI void sgi_delete_partition(int i);
678STATIC_SGI void sgi_change_sysid(int i, int sys);
679STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000680#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000681STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000682#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000683STATIC_SGI int verify_sgi(int verbose);
684STATIC_SGI void sgi_add_partition(int n, int sys);
685STATIC_SGI void sgi_set_swappartition(int i);
686STATIC_SGI const char *sgi_get_bootfile(void);
687STATIC_SGI void sgi_set_bootfile(const char* aFile);
688STATIC_SGI void create_sgiinfo(void);
689STATIC_SGI void sgi_write_table(void);
690STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000691#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692
Denis Vlasenkobd852072007-03-19 14:43:38 +0000693STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000694STATIC_SUN void sun_delete_partition(int i);
695STATIC_SUN void sun_change_sysid(int i, int sys);
696STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000697STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000698#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000699STATIC_SUN void sun_set_alt_cyl(void);
700STATIC_SUN void sun_set_ncyl(int cyl);
701STATIC_SUN void sun_set_xcyl(void);
702STATIC_SUN void sun_set_ilfact(void);
703STATIC_SUN void sun_set_rspeed(void);
704STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000705#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000706STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
707STATIC_SUN void verify_sun(void);
708STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000709#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000710
Denis Vlasenko9764d692008-07-09 21:20:50 +0000711
Denis Vlasenko834410a2006-11-29 12:00:28 +0000712#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713/* start_sect and nr_sects are stored little endian on all machines */
714/* moreover, they are not aligned correctly */
715static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000716store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000717{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000718 cp[0] = val;
719 cp[1] = val >> 8;
720 cp[2] = val >> 16;
721 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000722}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000723#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000724
Denis Vlasenko834410a2006-11-29 12:00:28 +0000725static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000726read4_little_endian(const unsigned char *cp)
727{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000728 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000729}
730
Denis Vlasenko834410a2006-11-29 12:00:28 +0000731#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000732static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000733set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000734{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000735 store4_little_endian(p->start4, start_sect);
736}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000737#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000738
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200739static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000740get_start_sect(const struct partition *p)
741{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000742 return read4_little_endian(p->start4);
743}
744
Denis Vlasenko834410a2006-11-29 12:00:28 +0000745#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000746static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000747set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000748{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000749 store4_little_endian(p->size4, nr_sects);
750}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000751#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000752
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200753static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000754get_nr_sects(const struct partition *p)
755{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000756 return read4_little_endian(p->size4);
757}
758
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759/* Allocate a buffer and read a partition table sector */
760static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200761read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000762{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000763 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000764 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000765 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000766 /* xread would make us abort - bad for fdisk -l */
767 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000768 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000769#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000770 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000771#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000772 pe->part_table = pe->ext_pointer = NULL;
773}
774
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200775static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000776get_partition_start(const struct pte *pe)
777{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778 return pe->offset + get_start_sect(pe->part_table);
779}
780
Denis Vlasenko834410a2006-11-29 12:00:28 +0000781#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000782/*
783 * Avoid warning about DOS partitions when no DOS partition was changed.
784 * Here a heuristic "is probably dos partition".
785 * We might also do the opposite and warn in all cases except
786 * for "is probably nondos partition".
787 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000788#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000789static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000790is_dos_partition(int t)
791{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000792 return (t == 1 || t == 4 || t == 6 ||
793 t == 0x0b || t == 0x0c || t == 0x0e ||
794 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
795 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
796 t == 0xc1 || t == 0xc4 || t == 0xc6);
797}
Denis Vlasenko89398812008-01-25 20:18:46 +0000798#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000799
800static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000801menu(void)
802{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000803 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000804 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000805 puts("a\ttoggle a read only flag"); /* sun */
806 puts("b\tedit bsd disklabel");
807 puts("c\ttoggle the mountable flag"); /* sun */
808 puts("d\tdelete a partition");
809 puts("l\tlist known partition types");
810 puts("n\tadd a new partition");
811 puts("o\tcreate a new empty DOS partition table");
812 puts("p\tprint the partition table");
813 puts("q\tquit without saving changes");
814 puts("s\tcreate a new empty Sun disklabel"); /* sun */
815 puts("t\tchange a partition's system id");
816 puts("u\tchange display/entry units");
817 puts("v\tverify the partition table");
818 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000819#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000820 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000821#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000822 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000823 puts("a\tselect bootable partition"); /* sgi flavour */
824 puts("b\tedit bootfile entry"); /* sgi */
825 puts("c\tselect sgi swap partition"); /* sgi flavour */
826 puts("d\tdelete a partition");
827 puts("l\tlist known partition types");
828 puts("n\tadd a new partition");
829 puts("o\tcreate a new empty DOS partition table");
830 puts("p\tprint the partition table");
831 puts("q\tquit without saving changes");
832 puts("s\tcreate a new empty Sun disklabel"); /* sun */
833 puts("t\tchange a partition's system id");
834 puts("u\tchange display/entry units");
835 puts("v\tverify the partition table");
836 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000837 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000838 puts("o\tcreate a new empty DOS partition table");
839 puts("q\tquit without saving changes");
840 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000841 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000842 puts("a\ttoggle a bootable flag");
843 puts("b\tedit bsd disklabel");
844 puts("c\ttoggle the dos compatibility flag");
845 puts("d\tdelete a partition");
846 puts("l\tlist known partition types");
847 puts("n\tadd a new partition");
848 puts("o\tcreate a new empty DOS partition table");
849 puts("p\tprint the partition table");
850 puts("q\tquit without saving changes");
851 puts("s\tcreate a new empty Sun disklabel"); /* sun */
852 puts("t\tchange a partition's system id");
853 puts("u\tchange display/entry units");
854 puts("v\tverify the partition table");
855 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000856#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000857 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000858#endif
859 }
860}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000861#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000862
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000863
Denis Vlasenko834410a2006-11-29 12:00:28 +0000864#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000865static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000866xmenu(void)
867{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000868 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000869 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000870 puts("a\tchange number of alternate cylinders"); /*sun*/
871 puts("c\tchange number of cylinders");
872 puts("d\tprint the raw data in the partition table");
873 puts("e\tchange number of extra sectors per cylinder");/*sun*/
874 puts("h\tchange number of heads");
875 puts("i\tchange interleave factor"); /*sun*/
876 puts("o\tchange rotation speed (rpm)"); /*sun*/
877 puts("p\tprint the partition table");
878 puts("q\tquit without saving changes");
879 puts("r\treturn to main menu");
880 puts("s\tchange number of sectors/track");
881 puts("v\tverify the partition table");
882 puts("w\twrite table to disk and exit");
883 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000885 puts("b\tmove beginning of data in a partition"); /* !sun */
886 puts("c\tchange number of cylinders");
887 puts("d\tprint the raw data in the partition table");
888 puts("e\tlist extended partitions"); /* !sun */
889 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
890 puts("h\tchange number of heads");
891 puts("p\tprint the partition table");
892 puts("q\tquit without saving changes");
893 puts("r\treturn to main menu");
894 puts("s\tchange number of sectors/track");
895 puts("v\tverify the partition table");
896 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000897 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000898 puts("b\tmove beginning of data in a partition"); /* !sun */
899 puts("c\tchange number of cylinders");
900 puts("d\tprint the raw data in the partition table");
901 puts("e\tlist extended partitions"); /* !sun */
902 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
903 puts("h\tchange number of heads");
904 puts("p\tprint the partition table");
905 puts("q\tquit without saving changes");
906 puts("r\treturn to main menu");
907 puts("s\tchange number of sectors/track");
908 puts("v\tverify the partition table");
909 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000910 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000911 puts("b\tmove beginning of data in a partition"); /* !sun */
912 puts("c\tchange number of cylinders");
913 puts("d\tprint the raw data in the partition table");
914 puts("e\tlist extended partitions"); /* !sun */
915 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000916#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000917 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000918#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000919 puts("h\tchange number of heads");
920 puts("p\tprint the partition table");
921 puts("q\tquit without saving changes");
922 puts("r\treturn to main menu");
923 puts("s\tchange number of sectors/track");
924 puts("v\tverify the partition table");
925 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000926 }
927}
928#endif /* ADVANCED mode */
929
Denis Vlasenko834410a2006-11-29 12:00:28 +0000930#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000931static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000932get_sys_types(void)
933{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000934 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000935 LABEL_IS_SUN ? sun_sys_types :
936 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000937 i386_sys_types);
938}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000939#else
940#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000941#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000942
Denis Vlasenkobd852072007-03-19 14:43:38 +0000943static const char *
944partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000945{
946 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000947 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000948
Denis Vlasenkobd852072007-03-19 14:43:38 +0000949 for (i = 0; types[i]; i++)
950 if ((unsigned char)types[i][0] == type)
951 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000952
Denis Vlasenkobd852072007-03-19 14:43:38 +0000953 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000954}
955
956
Denis Vlasenko834410a2006-11-29 12:00:28 +0000957#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000958static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000959get_sysid(int i)
960{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000961 return LABEL_IS_SUN ? sunlabel->infos[i].id :
962 (LABEL_IS_SGI ? sgi_get_sysid(i) :
963 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000964}
965
Denis Vlasenkobd852072007-03-19 14:43:38 +0000966static void
967list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000969 enum { COLS = 3 };
970
971 unsigned last[COLS];
972 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973 int i;
974
Denis Vlasenko9764d692008-07-09 21:20:50 +0000975 for (size = 0; sys[size]; size++)
976 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000977
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000978 done = 0;
979 for (i = COLS-1; i >= 0; i--) {
980 done += (size + i - done) / (i + 1);
981 last[COLS-1 - i] = done;
982 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000983
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000984 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000985 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000987 (unsigned char)sys[next][0],
988 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000989 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000990 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000991 i = 0;
992 next = ++done;
993 }
994 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000995 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000996}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000997#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000998
999static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001000is_cleared_partition(const struct partition *p)
1001{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
1003 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
1004 get_start_sect(p) || get_nr_sects(p));
1005}
1006
1007static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001008clear_partition(struct partition *p)
1009{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001010 if (!p)
1011 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001012 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013}
1014
Denis Vlasenko834410a2006-11-29 12:00:28 +00001015#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001016static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001017set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001018{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001020 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021
1022 if (doext) {
1023 p = ptes[i].ext_pointer;
1024 offset = extended_offset;
1025 } else {
1026 p = ptes[i].part_table;
1027 offset = ptes[i].offset;
1028 }
1029 p->boot_ind = 0;
1030 p->sys_ind = sysid;
1031 set_start_sect(p, start - offset);
1032 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001033 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1034 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001035 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001036 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1037 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001038 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1039 ptes[i].changed = 1;
1040}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001041#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042
1043static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001044warn_geometry(void)
1045{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001046 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001048
Denis Vlasenkobd852072007-03-19 14:43:38 +00001049 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001050 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001051 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001052 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001053 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001054 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 printf(" cylinders");
1056 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001057#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001058 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001059#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001060 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001061 return 1;
1062}
1063
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001064static void
1065update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001066{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001067 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001068
1069 if (display_in_cyl_units && cyl_units)
1070 units_per_sector = cyl_units;
1071 else
1072 units_per_sector = 1; /* in sectors */
1073}
1074
Denis Vlasenko834410a2006-11-29 12:00:28 +00001075#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001076static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001077warn_cylinders(void)
1078{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001079 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001080 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001081"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001082"There is nothing wrong with that, but this is larger than 1024,\n"
1083"and could in certain setups cause problems with:\n"
1084"1) software that runs at boot time (e.g., old versions of LILO)\n"
1085"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001086" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001087 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001088}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001089#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090
1091static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001092read_extended(int ext)
1093{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094 int i;
1095 struct pte *pex;
1096 struct partition *p, *q;
1097
1098 ext_index = ext;
1099 pex = &ptes[ext];
1100 pex->ext_pointer = pex->part_table;
1101
1102 p = pex->part_table;
1103 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001104 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001105 return;
1106 }
1107
Rob Landleyb73451d2006-02-24 16:29:00 +00001108 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001109 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001110
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001111 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001112 /* This is not a Linux restriction, but
1113 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001114 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001115 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001116#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001117 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001118 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001119 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001120#endif
1121 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122 return;
1123 }
1124
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001125 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001126
1127 if (!extended_offset)
1128 extended_offset = get_start_sect(p);
1129
1130 q = p = pt_offset(pe->sectorbuffer, 0);
1131 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001132 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001133 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001134 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001135 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001136 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001137 else
1138 pe->ext_pointer = p;
1139 } else if (p->sys_ind) {
1140 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001141 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001142 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001143 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001144 else
1145 pe->part_table = p;
1146 }
1147 }
1148
1149 /* very strange code here... */
1150 if (!pe->part_table) {
1151 if (q != pe->ext_pointer)
1152 pe->part_table = q;
1153 else
1154 pe->part_table = q + 1;
1155 }
1156 if (!pe->ext_pointer) {
1157 if (q != pe->part_table)
1158 pe->ext_pointer = q;
1159 else
1160 pe->ext_pointer = q + 1;
1161 }
1162
1163 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001164 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001165 }
1166
Denis Vlasenko834410a2006-11-29 12:00:28 +00001167#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001168 /* remove empty links */
1169 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001170 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 struct pte *pe = &ptes[i];
1172
Denis Vlasenkobd852072007-03-19 14:43:38 +00001173 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001174 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001175 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001176 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001177 delete_partition(i);
1178 goto remove; /* numbering changed */
1179 }
1180 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001181#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001182}
1183
Denis Vlasenko834410a2006-11-29 12:00:28 +00001184#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001186create_doslabel(void)
1187{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188 int i;
1189
Denis Vlasenkobd852072007-03-19 14:43:38 +00001190 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001191
Denis Vlasenko4437d192008-04-17 00:12:10 +00001192 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001193
Denis Vlasenko834410a2006-11-29 12:00:28 +00001194#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001195 possibly_osf_label = 0;
1196#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001197 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001198
1199 for (i = 510-64; i < 510; i++)
1200 MBRbuffer[i] = 0;
1201 write_part_table_flag(MBRbuffer);
1202 extended_offset = 0;
1203 set_all_unchanged();
1204 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001205 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001207#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208
1209static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001210get_sectorsize(void)
1211{
Rob Landley736e5252006-02-25 03:36:00 +00001212 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001214 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215 sector_size = arg;
1216 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001217 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001218 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1219 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001220 }
1221}
1222
Rob Landley88621d72006-08-29 19:41:06 +00001223static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001224get_kernel_geometry(void)
1225{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001226 struct hd_geometry geometry;
1227
Denis Vlasenko4437d192008-04-17 00:12:10 +00001228 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229 kern_heads = geometry.heads;
1230 kern_sectors = geometry.sectors;
1231 /* never use geometry.cylinders - it is truncated */
1232 }
1233}
1234
1235static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001236get_partition_table_geometry(void)
1237{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001238 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001239 struct partition *p;
1240 int i, h, s, hh, ss;
1241 int first = 1;
1242 int bad = 0;
1243
Eric Andersen3496fdc2006-01-30 23:09:20 +00001244 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245 return;
1246
1247 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001248 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001249 p = pt_offset(bufp, i);
1250 if (p->sys_ind != 0) {
1251 h = p->end_head + 1;
1252 s = (p->end_sector & 077);
1253 if (first) {
1254 hh = h;
1255 ss = s;
1256 first = 0;
1257 } else if (hh != h || ss != s)
1258 bad = 1;
1259 }
1260 }
1261
1262 if (!first && !bad) {
1263 pt_heads = hh;
1264 pt_sectors = ss;
1265 }
1266}
1267
Rob Landleyb73451d2006-02-24 16:29:00 +00001268static void
1269get_geometry(void)
1270{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001271 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272
1273 get_sectorsize();
1274 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001275#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001276 guess_device_type();
1277#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001278 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279 kern_heads = kern_sectors = 0;
1280 pt_heads = pt_sectors = 0;
1281
1282 get_kernel_geometry();
1283 get_partition_table_geometry();
1284
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001285 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286 pt_heads ? pt_heads :
1287 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001288 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289 pt_sectors ? pt_sectors :
1290 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001291 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001292
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 sector_offset = 1;
1294 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001295 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001297 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1298 if (!g_cylinders)
1299 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300}
1301
1302/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001303 * Opens disk_device and optionally reads MBR.
1304 * FIXME: document what each 'what' value will do!
1305 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001306 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1307 * 0: found or created label
1308 * 1: I/O error
1309 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001310#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1311static int get_boot(enum action what)
1312#else
1313static int get_boot(void)
1314#define get_boot(what) get_boot()
1315#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001316{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001317 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001318
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001319 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320 for (i = 0; i < 4; i++) {
1321 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 pe->part_table = pt_offset(MBRbuffer, i);
1323 pe->ext_pointer = NULL;
1324 pe->offset = 0;
1325 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001327 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001328#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329 }
1330
Denis Vlasenko834410a2006-11-29 12:00:28 +00001331#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001332// ALERT! highly idiotic design!
1333// We end up here when we call get_boot() recursively
1334// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1335// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1336// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1337// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001338 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001339 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001340
Denis Vlasenko4437d192008-04-17 00:12:10 +00001341 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1342
Denis Vlasenkobd852072007-03-19 14:43:38 +00001343 if (fd < 0) {
1344 fd = open(disk_device, O_RDONLY);
1345 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001346 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001347 return 1;
1348 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001349 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001350 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001352 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001353 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001354 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001355 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001357 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001358 fdisk_fatal(unable_to_read);
1359 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001360#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001361 fd = open(disk_device, O_RDONLY);
1362 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001363 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001364 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001365 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001366 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001367 }
1368 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001369#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001370
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001371 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372 update_units();
1373
Denis Vlasenko834410a2006-11-29 12:00:28 +00001374#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001375 if (check_sun_label())
1376 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001377#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001378#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 if (check_sgi_label())
1380 return 0;
1381#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001382#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001383 if (check_aix_label())
1384 return 0;
1385#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001386#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 if (check_osf_label()) {
1388 possibly_osf_label = 1;
1389 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001390 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 return 0;
1392 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001393 printf("This disk has both DOS and BSD magic.\n"
1394 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395 }
1396#endif
1397
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001398#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001399 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001400 return -1;
1401#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001402 if (!valid_part_table_flag(MBRbuffer)) {
1403 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001404 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001405 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001406 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001408 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409#else
1410 create_doslabel();
1411#endif
1412 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001414 /* TRY_ONLY: */
1415 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001417 created_table:
1418#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001419
Denis Vlasenko4437d192008-04-17 00:12:10 +00001420
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001421 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001422 warn_geometry();
1423
1424 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001425 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001426 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001427 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001428 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429 else
1430 read_extended(i);
1431 }
1432 }
1433
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001434 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001435 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001436 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001437 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001438 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001439 pe->sectorbuffer[510],
1440 pe->sectorbuffer[511],
1441 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001442 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001443 }
1444 }
1445
1446 return 0;
1447}
1448
Denis Vlasenko834410a2006-11-29 12:00:28 +00001449#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001450/*
1451 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1452 * If the user hits Enter, DFLT is returned.
1453 * Answers like +10 are interpreted as offsets from BASE.
1454 *
1455 * There is no default if DFLT is not between LOW and HIGH.
1456 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001457static sector_t
1458read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001459{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001460 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001462 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001463
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001464 if (dflt < low || dflt > high) {
1465 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001466 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001467 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001468
1469 while (1) {
1470 int use_default = default_ok;
1471
1472 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001473 do {
1474 printf(fmt, mesg, low, high, dflt);
1475 read_maybe_empty("");
1476 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1477 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001478
Eric Andersen84bdea82004-05-19 10:49:17 +00001479 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001480 int minus = (*line_ptr == '-');
1481 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001482
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001483 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001484
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001485 /* (1) if 2nd char is digit, use_default = 0.
1486 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001487 while (isdigit(*++line_ptr))
1488 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001489
Rob Landleyb73451d2006-02-24 16:29:00 +00001490 switch (*line_ptr) {
1491 case 'c':
1492 case 'C':
1493 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001494 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001495 break;
1496 case 'K':
1497 absolute = 1024;
1498 break;
1499 case 'k':
1500 absolute = 1000;
1501 break;
1502 case 'm':
1503 case 'M':
1504 absolute = 1000000;
1505 break;
1506 case 'g':
1507 case 'G':
1508 absolute = 1000000000;
1509 break;
1510 default:
1511 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001512 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001513 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001514 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001515 unsigned long unit;
1516
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001517 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001518 unit = sector_size * units_per_sector;
1519 bytes += unit/2; /* round */
1520 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001521 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001522 }
1523 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001524 value = -value;
1525 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001526 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001527 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001528 while (isdigit(*line_ptr)) {
1529 line_ptr++;
1530 use_default = 0;
1531 }
1532 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001533 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001534 value = dflt;
1535 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001536 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001537 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001538 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001539 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001540 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001541 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001542}
1543
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001544static unsigned
1545get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001546{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001547 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001548 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001549
Denis Vlasenkobd852072007-03-19 14:43:38 +00001550 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 pe = &ptes[i];
1552
1553 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001554 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1555 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1556 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1557 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001558 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001559 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001560 }
1561 return i;
1562}
1563
1564static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001565get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001566{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001567 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001568 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001569
1570 for (i = 0; i < max; i++) {
1571 struct pte *pe = &ptes[i];
1572 struct partition *p = pe->part_table;
1573
1574 if (p && !is_cleared_partition(p)) {
1575 if (pno >= 0)
1576 goto not_unique;
1577 pno = i;
1578 }
1579 }
1580 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001581 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001582 return pno;
1583 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001584 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585 return -1;
1586
1587 not_unique:
1588 return get_partition(warn, max);
1589}
1590
1591static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001592get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001593{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001595 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596
1597 for (i = 0; i < max; i++) {
1598 struct pte *pe = &ptes[i];
1599 struct partition *p = pe->part_table;
1600
1601 if (p && is_cleared_partition(p)) {
1602 if (pno >= 0)
1603 goto not_unique;
1604 pno = i;
1605 }
1606 }
1607 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001608 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001609 return pno;
1610 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001611 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001612 return -1;
1613
1614 not_unique:
1615 return get_partition(warn, max);
1616}
1617
1618
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001619static void
1620change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001621{
1622 display_in_cyl_units = !display_in_cyl_units;
1623 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001624 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001625 str_units(PLURAL));
1626}
1627
1628static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001629toggle_active(int i)
1630{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001631 struct pte *pe = &ptes[i];
1632 struct partition *p = pe->part_table;
1633
Rob Landleyb73451d2006-02-24 16:29:00 +00001634 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001635 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001636 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1637 pe->changed = 1;
1638}
1639
1640static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001641toggle_dos_compatibility_flag(void)
1642{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001643 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001644 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001645 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001646 printf("DOS Compatibility flag is set\n");
1647 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001648 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001649 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001650 }
1651}
1652
1653static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001654delete_partition(int i)
1655{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001656 struct pte *pe = &ptes[i];
1657 struct partition *p = pe->part_table;
1658 struct partition *q = pe->ext_pointer;
1659
1660/* Note that for the fifth partition (i == 4) we don't actually
1661 * decrement partitions.
1662 */
1663
1664 if (warn_geometry())
1665 return; /* C/H/S not set */
1666 pe->changed = 1;
1667
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001668 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001669 sun_delete_partition(i);
1670 return;
1671 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001672 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001673 sgi_delete_partition(i);
1674 return;
1675 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676
1677 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001678 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001679 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001680 ptes[ext_index].ext_pointer = NULL;
1681 extended_offset = 0;
1682 }
1683 clear_partition(p);
1684 return;
1685 }
1686
1687 if (!q->sys_ind && i > 4) {
1688 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001689 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001690 --i;
1691 clear_partition(ptes[i].ext_pointer);
1692 ptes[i].changed = 1;
1693 } else {
1694 /* not the last one - further ones will be moved down */
1695 if (i > 4) {
1696 /* delete this link in the chain */
1697 p = ptes[i-1].ext_pointer;
1698 *p = *q;
1699 set_start_sect(p, get_start_sect(q));
1700 set_nr_sects(p, get_nr_sects(q));
1701 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001702 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001703 /* the first logical in a longer chain */
1704 pe = &ptes[5];
1705
1706 if (pe->part_table) /* prevent SEGFAULT */
1707 set_start_sect(pe->part_table,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001708 get_partition_start(pe) -
1709 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001710 pe->offset = extended_offset;
1711 pe->changed = 1;
1712 }
1713
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001714 if (g_partitions > 5) {
1715 g_partitions--;
1716 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 ptes[i] = ptes[i+1];
1718 i++;
1719 }
1720 } else
1721 /* the only logical: clear only */
1722 clear_partition(ptes[i].part_table);
1723 }
1724}
1725
1726static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001727change_sysid(void)
1728{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 int i, sys, origsys;
1730 struct partition *p;
1731
Eric Andersen040f4402003-07-30 08:40:37 +00001732 /* If sgi_label then don't use get_existing_partition,
1733 let the user select a partition, since get_existing_partition()
1734 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001735 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001736 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001737 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001738 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001739 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001740 if (i == -1)
1741 return;
1742 p = ptes[i].part_table;
1743 origsys = sys = get_sysid(i);
1744
1745 /* if changing types T to 0 is allowed, then
1746 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001747 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001748 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001749 return;
1750 }
1751 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001752 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001753
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001754 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001755 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001756 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001757 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001758 /* break; */
1759 }
1760
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001761 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001762 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001763 printf("You cannot change a partition into"
1764 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001765 break;
1766 }
1767 }
1768
1769 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001770#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001771 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001772 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001773 "as Whole disk (5),\n"
1774 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001775 "even Linux likes it\n\n");
1776#endif
1777#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001778 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001779 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001780 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001781 (i == 8 && sys != 0)
1782 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001783 ) {
1784 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001785 "as volume header (0),\nand "
1786 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001787 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001788 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001789#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001790 if (sys == origsys)
1791 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001792 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001794 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001795 sgi_change_sysid(i, sys);
1796 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001797 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001798
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001799 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001800 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001801 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001802 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001803 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1804 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 break;
1806 }
1807 }
1808}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001809#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001810
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001811
Denis Vlasenko28703012006-12-19 20:32:02 +00001812/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1814 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1815 * Lubkin Oct. 1991). */
1816
Rob Landleyb73451d2006-02-24 16:29:00 +00001817static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001818linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001819{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001820 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001821
1822 *c = ls / spc;
1823 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001824 *h = ls / g_sectors;
1825 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001826}
1827
Rob Landleyb73451d2006-02-24 16:29:00 +00001828static void
1829check_consistency(const struct partition *p, int partition)
1830{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001831 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1832 unsigned pec, peh, pes; /* physical ending c, h, s */
1833 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1834 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001836 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001837 return; /* do not check extended partitions */
1838
1839/* physical beginning c, h, s */
1840 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1841 pbh = p->head;
1842 pbs = p->sector & 0x3f;
1843
1844/* physical ending c, h, s */
1845 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1846 peh = p->end_head;
1847 pes = p->end_sector & 0x3f;
1848
1849/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001850 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001851
1852/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001853 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001854
1855/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001856 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001857 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001858 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001859 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1860 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001861 }
1862
1863/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001864 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001865 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001866 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001867 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1868 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001869 }
1870
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001871/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001872 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001873 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001874 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001875 }
1876}
1877
1878static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001879list_disk_geometry(void)
1880{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001881 ullong bytes = ((ullong)total_number_of_sectors << 9);
1882 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001883
1884 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001885 printf("\nDisk %s: %lu MB, %llu bytes\n",
1886 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001887 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001888 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1889 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1890 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001891 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001892 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001893 printf(", total %"SECT_FMT"u sectors",
1894 total_number_of_sectors / (sector_size/512));
1895 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1896 str_units(PLURAL),
1897 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001898}
1899
1900/*
1901 * Check whether partition entries are ordered by their starting positions.
1902 * Return 0 if OK. Return i if partition i should have been earlier.
1903 * Two separate checks: primary and logical partitions.
1904 */
1905static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001906wrong_p_order(int *prev)
1907{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001908 const struct pte *pe;
1909 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001910 sector_t last_p_start_pos = 0, p_start_pos;
1911 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001912
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001913 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001914 if (i == 4) {
1915 last_i = 4;
1916 last_p_start_pos = 0;
1917 }
1918 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001919 p = pe->part_table;
1920 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001921 p_start_pos = get_partition_start(pe);
1922
1923 if (last_p_start_pos > p_start_pos) {
1924 if (prev)
1925 *prev = last_i;
1926 return i;
1927 }
1928
1929 last_p_start_pos = p_start_pos;
1930 last_i = i;
1931 }
1932 }
1933 return 0;
1934}
1935
Denis Vlasenko834410a2006-11-29 12:00:28 +00001936#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001937/*
1938 * Fix the chain of logicals.
1939 * extended_offset is unchanged, the set of sectors used is unchanged
1940 * The chain is sorted so that sectors increase, and so that
1941 * starting sectors increase.
1942 *
1943 * After this it may still be that cfdisk doesnt like the table.
1944 * (This is because cfdisk considers expanded parts, from link to
1945 * end of partition, and these may still overlap.)
1946 * Now
1947 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1948 * may help.
1949 */
1950static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001951fix_chain_of_logicals(void)
1952{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001953 int j, oj, ojj, sj, sjj;
1954 struct partition *pj,*pjj,tmp;
1955
1956 /* Stage 1: sort sectors but leave sector of part 4 */
1957 /* (Its sector is the global extended_offset.) */
1958 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001959 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001960 oj = ptes[j].offset;
1961 ojj = ptes[j+1].offset;
1962 if (oj > ojj) {
1963 ptes[j].offset = ojj;
1964 ptes[j+1].offset = oj;
1965 pj = ptes[j].part_table;
1966 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1967 pjj = ptes[j+1].part_table;
1968 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1969 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001970 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001971 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001972 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001973 goto stage1;
1974 }
1975 }
1976
1977 /* Stage 2: sort starting sectors */
1978 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001979 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001980 pj = ptes[j].part_table;
1981 pjj = ptes[j+1].part_table;
1982 sj = get_start_sect(pj);
1983 sjj = get_start_sect(pjj);
1984 oj = ptes[j].offset;
1985 ojj = ptes[j+1].offset;
1986 if (oj+sj > ojj+sjj) {
1987 tmp = *pj;
1988 *pj = *pjj;
1989 *pjj = tmp;
1990 set_start_sect(pj, ojj+sjj-oj);
1991 set_start_sect(pjj, oj+sj-ojj);
1992 goto stage2;
1993 }
1994 }
1995
1996 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001997 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001998 ptes[j].changed = 1;
1999}
2000
2001
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002002static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002003fix_partition_table_order(void)
2004{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 struct pte *pei, *pek;
2006 int i,k;
2007
2008 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002009 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 return;
2011 }
2012
2013 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2014 /* partition i should have come earlier, move it */
2015 /* We have to move data in the MBR */
2016 struct partition *pi, *pk, *pe, pbuf;
2017 pei = &ptes[i];
2018 pek = &ptes[k];
2019
2020 pe = pei->ext_pointer;
2021 pei->ext_pointer = pek->ext_pointer;
2022 pek->ext_pointer = pe;
2023
2024 pi = pei->part_table;
2025 pk = pek->part_table;
2026
2027 memmove(&pbuf, pi, sizeof(struct partition));
2028 memmove(pi, pk, sizeof(struct partition));
2029 memmove(pk, &pbuf, sizeof(struct partition));
2030
2031 pei->changed = pek->changed = 1;
2032 }
2033
2034 if (i)
2035 fix_chain_of_logicals();
2036
2037 printf("Done.\n");
2038
2039}
2040#endif
2041
2042static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002043list_table(int xtra)
2044{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046 int i, w;
2047
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002048 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049 sun_list_table(xtra);
2050 return;
2051 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002052 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 sgi_list_table(xtra);
2054 return;
2055 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002056
2057 list_disk_geometry();
2058
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002059 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002060 xbsd_print_disklabel(xtra);
2061 return;
2062 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002063
2064 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2065 but if the device name ends in a digit, say /dev/foo1,
2066 then the partition is called /dev/foo1p3. */
2067 w = strlen(disk_device);
2068 if (w && isdigit(disk_device[w-1]))
2069 w++;
2070 if (w < 5)
2071 w = 5;
2072
Denis Vlasenkobd852072007-03-19 14:43:38 +00002073 // 1 12345678901 12345678901 12345678901 12
2074 printf("%*s Boot Start End Blocks Id System\n",
2075 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002076
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002077 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002078 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002079 sector_t psects;
2080 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002081 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002082
2083 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002084 if (!p || is_cleared_partition(p))
2085 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002086
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002087 psects = get_nr_sects(p);
2088 pblocks = psects;
2089 podd = 0;
2090
2091 if (sector_size < 1024) {
2092 pblocks /= (1024 / sector_size);
2093 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002095 if (sector_size > 1024)
2096 pblocks *= (sector_size / 1024);
2097
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002098 printf("%s %c %11"SECT_FMT"u %11"SECT_FMT"u %11"SECT_FMT"u%c %2x %s\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002099 partname(disk_device, i+1, w+2),
2100 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2101 ? '*' : '?',
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002102 cround(get_partition_start(pe)), /* start */
2103 cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002104 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002105 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002106 p->sys_ind, /* type id */
2107 partition_type(p->sys_ind)); /* type name */
2108
2109 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002110 }
2111
2112 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002113 /* partition table entries are not checked for correct order
2114 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002115 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002116 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002117 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 }
2119}
2120
Denis Vlasenko834410a2006-11-29 12:00:28 +00002121#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002122static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002123x_list_table(int extend)
2124{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125 const struct pte *pe;
2126 const struct partition *p;
2127 int i;
2128
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002129 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002130 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002131 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002132 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133 pe = &ptes[i];
2134 p = (extend ? pe->ext_pointer : pe->part_table);
2135 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002136 printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002137 i + 1, p->boot_ind, p->head,
2138 sector(p->sector),
2139 cylinder(p->sector, p->cyl), p->end_head,
2140 sector(p->end_sector),
2141 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002142 get_start_sect(p), get_nr_sects(p),
2143 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 if (p->sys_ind)
2145 check_consistency(p, i);
2146 }
2147 }
2148}
2149#endif
2150
Denis Vlasenko834410a2006-11-29 12:00:28 +00002151#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002153fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002154{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002155 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156 const struct pte *pe = &ptes[0];
2157 const struct partition *p;
2158
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002159 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002161 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002162 first[i] = 0xffffffff;
2163 last[i] = 0;
2164 } else {
2165 first[i] = get_partition_start(pe);
2166 last[i] = first[i] + get_nr_sects(p) - 1;
2167 }
2168 }
2169}
2170
2171static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002172check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002173{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002174 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175
2176 real_s = sector(s) - 1;
2177 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002178 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002179 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002180 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002181 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002182 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002183 n, h + 1, g_heads);
2184 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002185 printf("Partition %u: sector %u greater than "
2186 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002187 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002188 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2189 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002190 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002191 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2192 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002193}
2194
2195static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002196verify(void)
2197{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002198 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002199 sector_t total = 1;
2200 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002201 struct partition *p;
2202
2203 if (warn_geometry())
2204 return;
2205
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002206 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 verify_sun();
2208 return;
2209 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002210 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002211 verify_sgi(1);
2212 return;
2213 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002214
2215 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002216 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217 struct pte *pe = &ptes[i];
2218
2219 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002220 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221 check_consistency(p, i);
2222 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002223 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002224 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2226 last[i]);
2227 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002228 for (j = 0; j < i; j++) {
2229 if ((first[i] >= first[j] && first[i] <= last[j])
2230 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002231 printf("Warning: partition %u overlaps "
2232 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002233 total += first[i] >= first[j] ?
2234 first[i] : first[j];
2235 total -= last[i] <= last[j] ?
2236 last[i] : last[j];
2237 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002238 }
2239 }
2240 }
2241
2242 if (extended_offset) {
2243 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002244 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002245 get_nr_sects(pex->part_table) - 1;
2246
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002247 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002248 total++;
2249 p = ptes[i].part_table;
2250 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002251 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002252 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002253 "is empty\n", i + 1);
2254 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002255 printf("Logical partition %u not entirely in "
2256 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002257 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 }
2259 }
2260
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002261 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002262 printf("Total allocated sectors %u greater than the maximum "
2263 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002264 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002265 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002266 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002267 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002268 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002269}
2270
2271static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002272add_partition(int n, int sys)
2273{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002274 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002275 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002276 struct partition *p = ptes[n].part_table;
2277 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002278 sector_t limit, temp;
2279 sector_t start, stop = 0;
2280 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281
2282 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002283 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284 return;
2285 }
2286 fill_bounds(first, last);
2287 if (n < 4) {
2288 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002289 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002290 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002291 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002292 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 if (extended_offset) {
2294 first[ext_index] = extended_offset;
2295 last[ext_index] = get_start_sect(q) +
2296 get_nr_sects(q) - 1;
2297 }
2298 } else {
2299 start = extended_offset + sector_offset;
2300 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2301 }
2302 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002303 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002304 first[i] = (cround(first[i]) - 1) * units_per_sector;
2305
Denis Vlasenkobd852072007-03-19 14:43:38 +00002306 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002307 do {
2308 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002309 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002310 int lastplusoff;
2311
2312 if (start == ptes[i].offset)
2313 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002314 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002315 if (start >= first[i] && start <= lastplusoff)
2316 start = lastplusoff + 1;
2317 }
2318 if (start > limit)
2319 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002320 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002321 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002322 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002323 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002324 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002325 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002326 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002327
2328 saved_start = start;
2329 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2330 0, mesg);
2331 if (display_in_cyl_units) {
2332 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002333 if (start < saved_start)
2334 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002336 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002337 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002338 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002339 if (n > 4) { /* NOT for fifth partition */
2340 struct pte *pe = &ptes[n];
2341
2342 pe->offset = start - sector_offset;
2343 if (pe->offset == extended_offset) { /* must be corrected */
2344 pe->offset++;
2345 if (sector_offset == 1)
2346 start++;
2347 }
2348 }
2349
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002350 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002351 struct pte *pe = &ptes[i];
2352
2353 if (start < pe->offset && limit >= pe->offset)
2354 limit = pe->offset - 1;
2355 if (start < first[i] && limit >= first[i])
2356 limit = first[i] - 1;
2357 }
2358 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002359 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002360 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002361 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362 return;
2363 }
2364 if (cround(start) == cround(limit)) {
2365 stop = limit;
2366 } else {
2367 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002368 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369 str_units(SINGULAR));
2370 stop = read_int(cround(start), cround(limit), cround(limit),
2371 cround(start), mesg);
2372 if (display_in_cyl_units) {
2373 stop = stop * units_per_sector - 1;
2374 if (stop >limit)
2375 stop = limit;
2376 }
2377 }
2378
2379 set_partition(n, 0, start, stop, sys);
2380 if (n > 4)
2381 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2382
Rob Landleyb73451d2006-02-24 16:29:00 +00002383 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384 struct pte *pe4 = &ptes[4];
2385 struct pte *pen = &ptes[n];
2386
2387 ext_index = n;
2388 pen->ext_pointer = p;
2389 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002390 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002391 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2392 pe4->ext_pointer = pe4->part_table + 1;
2393 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002394 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 }
2396}
2397
2398static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002399add_logical(void)
2400{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002401 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2402 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403
Rob Landley081e3842006-08-03 20:07:35 +00002404 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2406 pe->ext_pointer = pe->part_table + 1;
2407 pe->offset = 0;
2408 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002409 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002411 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412}
2413
2414static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002415new_partition(void)
2416{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002417 int i, free_primary = 0;
2418
2419 if (warn_geometry())
2420 return;
2421
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002422 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002423 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002424 return;
2425 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002426 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002427 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002428 return;
2429 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002430 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002431 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2432"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2433"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002434 return;
2435 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002437 for (i = 0; i < 4; i++)
2438 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002439
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002440 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002441 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002442 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002443 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002444
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002445 if (!free_primary) {
2446 if (extended_offset)
2447 add_logical();
2448 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002449 printf("You must delete some partition and add "
2450 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002452 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002453 snprintf(line, sizeof(line),
2454 "Command action\n"
2455 " %s\n"
2456 " p primary partition (1-4)\n",
2457 (extended_offset ?
2458 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002460 c = read_nonempty(line);
2461 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 i = get_nonexisting_partition(0, 4);
2463 if (i >= 0)
2464 add_partition(i, LINUX_NATIVE);
2465 return;
2466 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002467 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468 add_logical();
2469 return;
2470 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002471 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002472 i = get_nonexisting_partition(0, 4);
2473 if (i >= 0)
2474 add_partition(i, EXTENDED);
2475 return;
2476 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002477 printf("Invalid partition number "
2478 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002479 }
2480 }
2481}
2482
2483static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002484write_table(void)
2485{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486 int i;
2487
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002488 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002489 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002490 if (ptes[i].changed)
2491 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002492 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002493 struct pte *pe = &ptes[i];
2494
2495 if (pe->changed) {
2496 write_part_table_flag(pe->sectorbuffer);
2497 write_sector(pe->offset, pe->sectorbuffer);
2498 }
2499 }
2500 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002501 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 /* no test on change? the printf below might be mistaken */
2503 sgi_write_table();
2504 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002505 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002506 int needw = 0;
2507
Rob Landleyb73451d2006-02-24 16:29:00 +00002508 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 if (ptes[i].changed)
2510 needw = 1;
2511 if (needw)
2512 sun_write_table();
2513 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002514
Denis Vlasenkobd852072007-03-19 14:43:38 +00002515 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002516 reread_partition_table(1);
2517}
2518
Rob Landleyb73451d2006-02-24 16:29:00 +00002519static void
2520reread_partition_table(int leave)
2521{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002522 int i;
2523
Denis Vlasenkobd852072007-03-19 14:43:38 +00002524 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002526 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002527 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002528 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002529 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002530#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002531 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002532 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002533 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002535 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002536#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537
2538 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002539 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002540 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002541 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542 }
2543}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002544#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002545
Denis Vlasenko834410a2006-11-29 12:00:28 +00002546#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547#define MAX_PER_LINE 16
2548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002549print_buffer(char *pbuffer)
2550{
2551 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002552
2553 for (i = 0, l = 0; i < sector_size; i++, l++) {
2554 if (l == 0)
2555 printf("0x%03X:", i);
2556 printf(" %02X", (unsigned char) pbuffer[i]);
2557 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002558 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559 l = -1;
2560 }
2561 }
2562 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002563 bb_putchar('\n');
2564 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565}
2566
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002567static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002568print_raw(void)
2569{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 int i;
2571
Denis Vlasenkobd852072007-03-19 14:43:38 +00002572 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002573 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002575 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002576 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002578 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002579}
2580
2581static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002582move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002583{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002584 struct pte *pe = &ptes[i];
2585 struct partition *p = pe->part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002586 sector_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587
2588 if (warn_geometry())
2589 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002590 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002591 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 return;
2593 }
2594 first = get_partition_start(pe);
2595 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002596 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597
2598 if (new != get_nr_sects(p)) {
2599 first = get_nr_sects(p) + get_start_sect(p) - new;
2600 set_nr_sects(p, first);
2601 set_start_sect(p, new);
2602 pe->changed = 1;
2603 }
2604}
2605
2606static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002607xselect(void)
2608{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 char c;
2610
Rob Landleyb73451d2006-02-24 16:29:00 +00002611 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002612 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002613 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 switch (c) {
2615 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 break;
2619 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002620 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002621 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 break;
2623 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002624 user_cylinders = g_cylinders =
2625 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002626 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002627 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002628 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002629 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002630 warn_cylinders();
2631 break;
2632 case 'd':
2633 print_raw();
2634 break;
2635 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002638 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002639 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002640 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 x_list_table(1);
2642 break;
2643 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002644 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 fix_partition_table_order();
2646 break;
2647 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002648#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002649 create_sgilabel();
2650#endif
2651 break;
2652 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002653 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002654 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002655 update_units();
2656 break;
2657 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002658 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002659 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002660 break;
2661 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002662 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002664 break;
2665 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002666 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002667 list_table(1);
2668 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002669 x_list_table(0);
2670 break;
2671 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002672 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002673 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002674 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002675 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002676 case 'r':
2677 return;
2678 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002679 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002680 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002681 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002682 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002683 printf("Warning: setting sector offset for DOS "
2684 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 }
2686 update_units();
2687 break;
2688 case 'v':
2689 verify();
2690 break;
2691 case 'w':
2692 write_table(); /* does not return */
2693 break;
2694 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002695 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002696 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002697 break;
2698 default:
2699 xmenu();
2700 }
2701 }
2702}
2703#endif /* ADVANCED mode */
2704
2705static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002706is_ide_cdrom_or_tape(const char *device)
2707{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002708 FILE *procf;
2709 char buf[100];
2710 struct stat statbuf;
2711 int is_ide = 0;
2712
2713 /* No device was given explicitly, and we are trying some
2714 likely things. But opening /dev/hdc may produce errors like
2715 "hdc: tray open or drive not ready"
2716 if it happens to be a CD-ROM drive. It even happens that
2717 the process hangs on the attempt to read a music CD.
2718 So try to be careful. This only works since 2.1.73. */
2719
2720 if (strncmp("/dev/hd", device, 7))
2721 return 0;
2722
2723 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002724 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002725 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2726 is_ide = (!strncmp(buf, "cdrom", 5) ||
2727 !strncmp(buf, "tape", 4));
2728 else
2729 /* Now when this proc file does not exist, skip the
2730 device when it is read-only. */
2731 if (stat(device, &statbuf) == 0)
2732 is_ide = ((statbuf.st_mode & 0222) == 0);
2733
2734 if (procf)
2735 fclose(procf);
2736 return is_ide;
2737}
2738
Rob Landley5527b912006-02-25 03:46:10 +00002739
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002740static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002741open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002742{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743 int gb;
2744
2745 disk_device = device;
2746 if (setjmp(listingbuf))
2747 return;
2748 if (!user_specified)
2749 if (is_ide_cdrom_or_tape(device))
2750 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002751
2752 /* Open disk_device, save file descriptor to dev_fd */
2753 errno = 0;
2754 gb = get_boot(TRY_ONLY);
2755 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002756 /* Ignore other errors, since we try IDE
2757 and SCSI hard disks which may not be
2758 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002759 if (user_specified || errno == EACCES)
2760 bb_perror_msg("can't open '%s'", device);
2761 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002762 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002763
2764 if (gb < 0) { /* no DOS signature */
2765 list_disk_geometry();
2766 if (LABEL_IS_AIX)
2767 goto ret;
2768#if ENABLE_FEATURE_OSF_LABEL
2769 if (bsd_trydev(device) < 0)
2770#endif
2771 printf("Disk %s doesn't contain a valid "
2772 "partition table\n", device);
2773 } else {
2774 list_table(0);
2775#if ENABLE_FEATURE_FDISK_WRITABLE
2776 if (!LABEL_IS_SUN && g_partitions > 4) {
2777 delete_partition(ext_index);
2778 }
2779#endif
2780 }
2781 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002782 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002783}
2784
2785/* for fdisk -l: try all things in /proc/partitions
2786 that look like a partition name (do not end in a digit) */
2787static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002788list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002789{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002790 FILE *procpt;
2791 char line[100], ptname[100], devname[120], *s;
2792 int ma, mi, sz;
2793
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002794 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002795
2796 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002797 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002798 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002799 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002800 for (s = ptname; *s; s++)
2801 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 if (isdigit(s[-1]))
2803 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002804 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002805 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002807#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002808 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002809#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002810}
2811
Denis Vlasenko834410a2006-11-29 12:00:28 +00002812#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002813static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002814unknown_command(int c)
2815{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002816 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002817}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002818#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002819
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002820int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002821int fdisk_main(int argc, char **argv)
2822{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002823 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825 * fdisk -v
2826 * fdisk -l [-b sectorsize] [-u] device ...
2827 * fdisk -s [partition] ...
2828 * fdisk [-b sectorsize] [-u] device
2829 *
2830 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002832 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002833
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002834 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002835
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002836 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002837 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002838 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002839 argc -= optind;
2840 argv += optind;
2841 if (opt & OPT_b) { // -b
2842 /* Ugly: this sector size is really per device,
2843 so cannot be combined with multiple disks,
2844 and the same goes for the C/H/S options.
2845 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002846 if (sector_size != 512 && sector_size != 1024
2847 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002848 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002849 sector_offset = 2;
2850 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002852 if (user_heads <= 0 || user_heads >= 256)
2853 user_heads = 0;
2854 if (user_sectors <= 0 || user_sectors >= 64)
2855 user_sectors = 0;
2856 if (opt & OPT_u)
2857 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859#if ENABLE_FEATURE_FDISK_WRITABLE
2860 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002862#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002863 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002865 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002866 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002867 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002869 /* we don't have device names, */
2870 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002871 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 }
2873 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002874#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002876#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002877
Denis Vlasenko834410a2006-11-29 12:00:28 +00002878#if ENABLE_FEATURE_FDISK_BLKSIZE
2879 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002880 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881
2882 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002883 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002884 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002885 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002886 unsigned long long size;
2887 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002888 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002889 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002890 if (argc == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002891 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002893 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 }
2895 return 0;
2896 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002897#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898
Denis Vlasenko834410a2006-11-29 12:00:28 +00002899#if ENABLE_FEATURE_FDISK_WRITABLE
2900 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002901 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902
Denis Vlasenko834410a2006-11-29 12:00:28 +00002903 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002904 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002905
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002906 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002908 printf("Detected an OSF/1 disklabel on %s, entering "
2909 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002910 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002911 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002912 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 /* If we return we may want to make an empty DOS label? */
2914 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915
2916 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002917 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002918 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002919 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002920 switch (c) {
2921 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002922 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002923 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002924 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002925 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002927 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002928 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002929 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 else
2931 unknown_command(c);
2932 break;
2933 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002935 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002936 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002937 if (read_maybe_empty("Please enter the name of the "
2938 "new boot file: ") == '\n')
2939 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 else
2941 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002942 }
2943#if ENABLE_FEATURE_OSF_LABEL
2944 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002945 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002946#endif
2947 break;
2948 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002949 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002951 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002952 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002953 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002954 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002955 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002956 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002957 else
2958 unknown_command(c);
2959 break;
2960 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002961 {
Eric Andersen040f4402003-07-30 08:40:37 +00002962 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002963 /* If sgi_label then don't use get_existing_partition,
2964 let the user select a partition, since
2965 get_existing_partition() only works for Linux-like
2966 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002967 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002968 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002969 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002970 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002971 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002972 if (j >= 0)
2973 delete_partition(j);
2974 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002975 break;
2976 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002977 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002978 create_sgiinfo();
2979 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002980 unknown_command(c);
2981 case 'l':
2982 list_types(get_sys_types());
2983 break;
2984 case 'm':
2985 menu();
2986 break;
2987 case 'n':
2988 new_partition();
2989 break;
2990 case 'o':
2991 create_doslabel();
2992 break;
2993 case 'p':
2994 list_table(0);
2995 break;
2996 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002997 if (ENABLE_FEATURE_CLEAN_UP)
2998 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002999 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003000 return 0;
3001 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003002#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003003 create_sunlabel();
3004#endif
3005 break;
3006 case 't':
3007 change_sysid();
3008 break;
3009 case 'u':
3010 change_units();
3011 break;
3012 case 'v':
3013 verify();
3014 break;
3015 case 'w':
3016 write_table(); /* does not return */
3017 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003018#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003019 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003020 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003021 printf("\n\tSorry, no experts menu for SGI "
3022 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003023 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003024 xselect();
3025 break;
3026#endif
3027 default:
3028 unknown_command(c);
3029 menu();
3030 }
3031 }
3032 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003033#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003034}