blob: cb15bd26738d16e8220d018c0faf637e59faff6e [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 */
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +020012# define _LARGEFILE64_SOURCE
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000013#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
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +020019#if !defined(BLKGETSIZE64)
20# define BLKGETSIZE64 _IOR(0x12,114,size_t)
21#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000023
Denys Vlasenko5ea1de22010-04-06 02:31:43 +020024#if BB_LITTLE_ENDIAN
25# define inline_if_little_endian ALWAYS_INLINE
26#else
27# define inline_if_little_endian /* nothing */
28#endif
29
30
Denis Vlasenko834410a2006-11-29 12:00:28 +000031/* Looks like someone forgot to add this to config system */
32#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
33# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000034# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +000035#endif
36
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000037#define DEFAULT_SECTOR_SIZE 512
38#define DEFAULT_SECTOR_SIZE_STR "512"
39#define MAX_SECTOR_SIZE 2048
40#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
41#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000043#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000044
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000045#define EXTENDED 0x05
46#define WIN98_EXTENDED 0x0f
47#define LINUX_PARTITION 0x81
48#define LINUX_SWAP 0x82
49#define LINUX_NATIVE 0x83
50#define LINUX_EXTENDED 0x85
51#define LINUX_LVM 0x8e
52#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000053
Denis Vlasenkoc033d512008-04-17 01:52:28 +000054
55enum {
56 OPT_b = 1 << 0,
57 OPT_C = 1 << 1,
58 OPT_H = 1 << 2,
59 OPT_l = 1 << 3,
60 OPT_S = 1 << 4,
61 OPT_u = 1 << 5,
62 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
63};
64
65
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000066typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +020067/* Used for sector numbers. Partition formats we know
68 * do not support more than 2^32 sectors
69 */
70typedef uint32_t sector_t;
71#if UINT_MAX == 4294967295
72# define SECT_FMT ""
73#elif ULONG_MAX == 4294967295
74# define SECT_FMT "l"
75#else
76# error Cant detect sizeof(uint32_t)
77#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000078
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000080 unsigned char heads;
81 unsigned char sectors;
82 unsigned short cylinders;
83 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000084};
85
Denis Vlasenko98ae2162006-10-12 19:30:44 +000086#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000087
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000088static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000089"Building a new %s. Changes will remain in memory only,\n"
90"until you decide to write them. After that the previous content\n"
91"won't be recoverable.\n\n";
92
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000093static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +020094"Partition %u is already defined, delete it before re-adding\n";
Denis Vlasenkobd852072007-03-19 14:43:38 +000095
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000096
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000097struct partition {
98 unsigned char boot_ind; /* 0x80 - active */
99 unsigned char head; /* starting head */
100 unsigned char sector; /* starting sector */
101 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000102 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000103 unsigned char end_head; /* end head */
104 unsigned char end_sector; /* end sector */
105 unsigned char end_cyl; /* end cylinder */
106 unsigned char start4[4]; /* starting sector counting from 0 */
107 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000108} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000109
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000110static const char unable_to_open[] ALIGN1 = "can't open '%s'";
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000111static const char unable_to_read[] ALIGN1 = "can't read from %s";
112static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000113
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000114enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +0000115 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +0000116};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000117
Denis Vlasenko4437d192008-04-17 00:12:10 +0000118#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000119
Denis Vlasenko834410a2006-11-29 12:00:28 +0000120#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000121#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000122#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000123#else
124#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000125#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000126#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000127
Denis Vlasenko834410a2006-11-29 12:00:28 +0000128#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000129#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000130#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000131#else
132#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000133#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000134#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000135
Denis Vlasenko834410a2006-11-29 12:00:28 +0000136#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000137#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000138#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000139#else
140#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000141#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000142#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000143
Denis Vlasenko834410a2006-11-29 12:00:28 +0000144#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000145#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000146#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000147#else
148#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000149#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000150#endif
Rob Landley5527b912006-02-25 03:46:10 +0000151
Denis Vlasenko4437d192008-04-17 00:12:10 +0000152enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000153
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000154static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000155#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000156static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000157static void reread_partition_table(int leave);
158static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200159static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000160static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200161static 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 +0000162#endif
163static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000164static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000165#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000166static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000167#else
168static int get_boot(void);
169#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000170
171#define PLURAL 0
172#define SINGULAR 1
173
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200174static sector_t get_start_sect(const struct partition *p);
175static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000176
177/*
178 * per partition table entry data
179 *
180 * The four primary partitions have the same sectorbuffer (MBRbuffer)
181 * and have NULL ext_pointer.
182 * Each logical partition table entry has two pointers, one for the
183 * partition and one link to the next one.
184 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000185struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000186 struct partition *part_table; /* points into sectorbuffer */
187 struct partition *ext_pointer; /* points into sectorbuffer */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200188 sector_t offset; /* disk sector number */
189 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000190#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200191 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000192#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000193};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000194
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000195/* DOS partition types */
196
197static const char *const i386_sys_types[] = {
198 "\x00" "Empty",
199 "\x01" "FAT12",
200 "\x04" "FAT16 <32M",
201 "\x05" "Extended", /* DOS 3.3+ extended partition */
202 "\x06" "FAT16", /* DOS 16-bit >=32M */
203 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
204 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
205 "\x0b" "Win95 FAT32",
206 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
207 "\x0e" "Win95 FAT16 (LBA)",
208 "\x0f" "Win95 Ext'd (LBA)",
209 "\x11" "Hidden FAT12",
210 "\x12" "Compaq diagnostics",
211 "\x14" "Hidden FAT16 <32M",
212 "\x16" "Hidden FAT16",
213 "\x17" "Hidden HPFS/NTFS",
214 "\x1b" "Hidden Win95 FAT32",
215 "\x1c" "Hidden W95 FAT32 (LBA)",
216 "\x1e" "Hidden W95 FAT16 (LBA)",
217 "\x3c" "Part.Magic recovery",
218 "\x41" "PPC PReP Boot",
219 "\x42" "SFS",
220 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
221 "\x80" "Old Minix", /* Minix 1.4a and earlier */
222 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
223 "\x82" "Linux swap", /* also Solaris */
224 "\x83" "Linux",
225 "\x84" "OS/2 hidden C: drive",
226 "\x85" "Linux extended",
227 "\x86" "NTFS volume set",
228 "\x87" "NTFS volume set",
229 "\x8e" "Linux LVM",
230 "\x9f" "BSD/OS", /* BSDI */
231 "\xa0" "Thinkpad hibernation",
232 "\xa5" "FreeBSD", /* various BSD flavours */
233 "\xa6" "OpenBSD",
234 "\xa8" "Darwin UFS",
235 "\xa9" "NetBSD",
236 "\xab" "Darwin boot",
237 "\xb7" "BSDI fs",
238 "\xb8" "BSDI swap",
239 "\xbe" "Solaris boot",
240 "\xeb" "BeOS fs",
241 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
242 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
243 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
244 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
245 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
246 autodetect using persistent
247 superblock */
248#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
249 "\x02" "XENIX root",
250 "\x03" "XENIX usr",
251 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
252 "\x09" "AIX bootable", /* AIX data or Coherent */
253 "\x10" "OPUS",
254 "\x18" "AST SmartSleep",
255 "\x24" "NEC DOS",
256 "\x39" "Plan 9",
257 "\x40" "Venix 80286",
258 "\x4d" "QNX4.x",
259 "\x4e" "QNX4.x 2nd part",
260 "\x4f" "QNX4.x 3rd part",
261 "\x50" "OnTrack DM",
262 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
263 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
264 "\x53" "OnTrack DM6 Aux3",
265 "\x54" "OnTrackDM6",
266 "\x55" "EZ-Drive",
267 "\x56" "Golden Bow",
268 "\x5c" "Priam Edisk",
269 "\x61" "SpeedStor",
270 "\x64" "Novell Netware 286",
271 "\x65" "Novell Netware 386",
272 "\x70" "DiskSecure Multi-Boot",
273 "\x75" "PC/IX",
274 "\x93" "Amoeba",
275 "\x94" "Amoeba BBT", /* (bad block table) */
276 "\xa7" "NeXTSTEP",
277 "\xbb" "Boot Wizard hidden",
278 "\xc1" "DRDOS/sec (FAT-12)",
279 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
280 "\xc6" "DRDOS/sec (FAT-16)",
281 "\xc7" "Syrinx",
282 "\xda" "Non-FS data",
283 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
284 Concurrent DOS or CTOS */
285 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
286 "\xdf" "BootIt", /* BootIt EMBRM */
287 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
288 extended partition */
289 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
290 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
291 partition < 1024 cyl. */
292 "\xf1" "SpeedStor",
293 "\xf4" "SpeedStor", /* SpeedStor large partition */
294 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
295 "\xff" "BBT", /* Xenix Bad Block Table */
296#endif
297 NULL
298};
299
Denis Vlasenko4437d192008-04-17 00:12:10 +0000300enum {
301 dev_fd = 3 /* the disk */
302};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000303
304/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000305struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000306 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000307
308 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000309 int g_partitions; // = 4; /* maximum partition + 1 */
310 unsigned units_per_sector; // = 1;
311 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
312 unsigned user_set_sector_size;
313 unsigned sector_offset; // = 1;
314 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000315 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000316 smallint display_in_cyl_units; // = 1;
317#if ENABLE_FEATURE_OSF_LABEL
318 smallint possibly_osf_label;
319#endif
320
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000321 smallint listing; /* no aborts for fdisk -l */
322 smallint dos_compatible_flag; // = 1;
323#if ENABLE_FEATURE_FDISK_WRITABLE
324 //int dos_changed;
325 smallint nowarn; /* no warnings for fdisk -l/-s */
326#endif
327 int ext_index; /* the prime extended partition */
328 unsigned user_cylinders, user_heads, user_sectors;
329 unsigned pt_heads, pt_sectors;
330 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200331 sector_t extended_offset; /* offset of link pointers */
332 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000333
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000334 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000335 char line_buffer[80];
336 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000337 /* Raw disk label. For DOS-type partition tables the MBR,
338 * with descriptions of the primary partitions. */
339 char MBRbuffer[MAX_SECTOR_SIZE];
340 /* Partition tables */
341 struct pte ptes[MAXIMUM_PARTS];
342};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000343#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000344#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000345#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000346#define g_partitions (G.g_partitions )
347#define units_per_sector (G.units_per_sector )
348#define sector_size (G.sector_size )
349#define user_set_sector_size (G.user_set_sector_size)
350#define sector_offset (G.sector_offset )
351#define g_heads (G.g_heads )
352#define g_sectors (G.g_sectors )
353#define g_cylinders (G.g_cylinders )
354#define current_label_type (G.current_label_type )
355#define display_in_cyl_units (G.display_in_cyl_units)
356#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000357#define listing (G.listing )
358#define dos_compatible_flag (G.dos_compatible_flag )
359#define nowarn (G.nowarn )
360#define ext_index (G.ext_index )
361#define user_cylinders (G.user_cylinders )
362#define user_heads (G.user_heads )
363#define user_sectors (G.user_sectors )
364#define pt_heads (G.pt_heads )
365#define pt_sectors (G.pt_sectors )
366#define kern_heads (G.kern_heads )
367#define kern_sectors (G.kern_sectors )
368#define extended_offset (G.extended_offset )
369#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000370#define listingbuf (G.listingbuf )
371#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000372#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000373#define MBRbuffer (G.MBRbuffer )
374#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000375#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000376 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000377 sector_size = DEFAULT_SECTOR_SIZE; \
378 sector_offset = 1; \
379 g_partitions = 4; \
380 display_in_cyl_units = 1; \
381 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000382 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000383} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000384
Denis Vlasenkobd852072007-03-19 14:43:38 +0000385
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000386/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200387/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
388 * disks > 2^32 sectors
389 */
390static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000391{
392 uint64_t v64;
393 unsigned long longsectors;
394
395 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000396 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200397 v64 >>= 9;
398 if (v64 != (sector_t)v64) {
399 ret_trunc:
400 /* Not only DOS, but all other partition tables
401 * we support can't record more than 32 bit
402 * sector counts or offsets
403 */
404 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
405 v64 = (uint32_t)-1L;
406 }
407 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000408 }
409 /* Needs temp of type long */
410 if (ioctl(fd, BLKGETSIZE, &longsectors))
411 longsectors = 0;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200412 if (sizeof(long) > sizeof(sector_t)
413 && longsectors != (sector_t)longsectors
414 ) {
415 goto ret_trunc;
416 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000417 return longsectors;
418}
419
420
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000421#define IS_EXTENDED(i) \
422 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
423
424#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
425
426#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
427
428#define pt_offset(b, n) \
429 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
430
431#define sector(s) ((s) & 0x3f)
432
433#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
434
435#define hsc2sector(h,s,c) \
436 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
437
438#define set_hsc(h,s,c,sector) \
439 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000440 s = sector % g_sectors + 1; \
441 sector /= g_sectors; \
442 h = sector % g_heads; \
443 sector /= g_heads; \
444 c = sector & 0xff; \
445 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000446 } while (0)
447
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000448static void
449close_dev_fd(void)
450{
451 /* Not really closing, but making sure it is open, and to harmless place */
452 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
453}
454
Denis Vlasenkobd852072007-03-19 14:43:38 +0000455/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000456 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000457 */
458static const char *
459partname(const char *dev, int pno, int lth)
460{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000461 const char *p;
462 int w, wp;
463 int bufsiz;
464 char *bufp;
465
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000466 bufp = partname_buffer;
467 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000468
469 w = strlen(dev);
470 p = "";
471
472 if (isdigit(dev[w-1]))
473 p = "p";
474
475 /* devfs kludge - note: fdisk partition names are not supposed
476 to equal kernel names, so there is no reason to do this */
477 if (strcmp(dev + w - 4, "disc") == 0) {
478 w -= 4;
479 p = "part";
480 }
481
482 wp = strlen(p);
483
484 if (lth) {
485 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200486 lth-wp-2, w, dev, p, pno);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000487 } else {
488 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
489 }
490 return bufp;
491}
492
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000493static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000494get_part_table(int i)
495{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000496 return ptes[i].part_table;
497}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000498
499static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000500str_units(int n)
501{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000502 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000503 return display_in_cyl_units ? "cylinder" : "sector";
504 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000505}
506
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000507static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000508valid_part_table_flag(const char *mbuffer)
509{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000510 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000511}
512
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200513static void fdisk_fatal(const char *why)
514{
515 if (listing) {
516 close_dev_fd();
517 longjmp(listingbuf, 1);
518 }
519 bb_error_msg_and_die(why, disk_device);
520}
521
522static void
523seek_sector(sector_t secno)
524{
525#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
526 off64_t off = (off64_t)secno * sector_size;
527 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
528 fdisk_fatal(unable_to_seek);
529#else
530 uint64_t off = (uint64_t)secno * sector_size;
531 if (off > MAXINT(off_t)
532 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
533 ) {
534 fdisk_fatal(unable_to_seek);
535 }
536#endif
537}
538
Denis Vlasenko834410a2006-11-29 12:00:28 +0000539#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200540/* Read line; return 0 or first printable char */
541static int
542read_line(const char *prompt)
543{
544 int sz;
545
546 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
547 if (sz <= 0)
548 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
549
550 if (line_buffer[sz-1] == '\n')
551 line_buffer[--sz] = '\0';
552
553 line_ptr = line_buffer;
554 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
555 line_ptr++;
556 return *line_ptr;
557}
558
559static void
560set_all_unchanged(void)
561{
562 int i;
563
564 for (i = 0; i < MAXIMUM_PARTS; i++)
565 ptes[i].changed = 0;
566}
567
568static ALWAYS_INLINE void
569set_changed(int i)
570{
571 ptes[i].changed = 1;
572}
573
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000574static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000575write_part_table_flag(char *b)
576{
577 b[510] = 0x55;
578 b[511] = 0xaa;
579}
580
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000581static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000582read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000583{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000584 while (!read_line(mesg))
585 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000586 return *line_ptr;
587}
588
589static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000590read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000591{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000592 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000593 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000594 line_ptr[0] = '\n';
595 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000596 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000597 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598}
599
600static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000601read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000602{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000603 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000604 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000605 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000606 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000607 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000608 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000609 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000610 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200611 if (v <= 0xff)
612 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000613 }
614}
615
Denis Vlasenko9764d692008-07-09 21:20:50 +0000616static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200617write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000618{
619 seek_sector(secno);
620 xwrite(dev_fd, buf, sector_size);
621}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200622#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000623
624
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000625#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000626
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100627struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000628 unsigned char info[128]; /* Informative text string */
629 unsigned char spare0[14];
630 struct sun_info {
631 unsigned char spare1;
632 unsigned char id;
633 unsigned char spare2;
634 unsigned char flags;
635 } infos[8];
636 unsigned char spare1[246]; /* Boot information etc. */
637 unsigned short rspeed; /* Disk rotational speed */
638 unsigned short pcylcount; /* Physical cylinder count */
639 unsigned short sparecyl; /* extra sects per cylinder */
640 unsigned char spare2[4]; /* More magic... */
641 unsigned short ilfact; /* Interleave factor */
642 unsigned short ncyl; /* Data cylinder count */
643 unsigned short nacyl; /* Alt. cylinder count */
644 unsigned short ntrks; /* Tracks per cylinder */
645 unsigned short nsect; /* Sectors per track */
646 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000647 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000648 uint32_t start_cylinder;
649 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000650 } partitions[8];
651 unsigned short magic; /* Magic number */
652 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100653} FIX_ALIASING;
654typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000656STATIC_OSF void bsd_select(void);
657STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000658#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000659
Denis Vlasenko28703012006-12-19 20:32:02 +0000660#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000661static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000662fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000663{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000664 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000665}
666
Rob Landley88621d72006-08-29 19:41:06 +0000667static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000668fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000669{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000670 return (x << 24) |
671 ((x & 0xFF00) << 8) |
672 ((x & 0xFF0000) >> 8) |
673 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674}
675#endif
676
Denis Vlasenkobd852072007-03-19 14:43:38 +0000677STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000678STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000679STATIC_SGI int sgi_get_sysid(int i);
680STATIC_SGI void sgi_delete_partition(int i);
681STATIC_SGI void sgi_change_sysid(int i, int sys);
682STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000683#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000684STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000685#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000686STATIC_SGI int verify_sgi(int verbose);
687STATIC_SGI void sgi_add_partition(int n, int sys);
688STATIC_SGI void sgi_set_swappartition(int i);
689STATIC_SGI const char *sgi_get_bootfile(void);
690STATIC_SGI void sgi_set_bootfile(const char* aFile);
691STATIC_SGI void create_sgiinfo(void);
692STATIC_SGI void sgi_write_table(void);
693STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000694#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695
Denis Vlasenkobd852072007-03-19 14:43:38 +0000696STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000697STATIC_SUN void sun_delete_partition(int i);
698STATIC_SUN void sun_change_sysid(int i, int sys);
699STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000700STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000701#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000702STATIC_SUN void sun_set_alt_cyl(void);
703STATIC_SUN void sun_set_ncyl(int cyl);
704STATIC_SUN void sun_set_xcyl(void);
705STATIC_SUN void sun_set_ilfact(void);
706STATIC_SUN void sun_set_rspeed(void);
707STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000708#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000709STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
710STATIC_SUN void verify_sun(void);
711STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000712#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713
Denis Vlasenko9764d692008-07-09 21:20:50 +0000714
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200715static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000716read4_little_endian(const unsigned char *cp)
717{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200718 uint32_t v;
719 move_from_unaligned32(v, cp);
720 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000721}
722
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200723static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000724get_start_sect(const struct partition *p)
725{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000726 return read4_little_endian(p->start4);
727}
728
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200729static sector_t
730get_nr_sects(const struct partition *p)
731{
732 return read4_little_endian(p->size4);
733}
734
Denis Vlasenko834410a2006-11-29 12:00:28 +0000735#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200736/* start_sect and nr_sects are stored little endian on all machines */
737/* moreover, they are not aligned correctly */
738static inline_if_little_endian void
739store4_little_endian(unsigned char *cp, unsigned val)
740{
741 uint32_t v = SWAP_LE32(val);
742 move_to_unaligned32(cp, v);
743}
744
745static void
746set_start_sect(struct partition *p, unsigned start_sect)
747{
748 store4_little_endian(p->start4, start_sect);
749}
750
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000751static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000752set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000753{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000754 store4_little_endian(p->size4, nr_sects);
755}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000756#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000757
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000758/* Allocate a buffer and read a partition table sector */
759static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200760read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000761{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000762 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000763 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000764 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000765 /* xread would make us abort - bad for fdisk -l */
766 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000767 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000768#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000769 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000770#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000771 pe->part_table = pe->ext_pointer = NULL;
772}
773
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200774static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000775get_partition_start(const struct pte *pe)
776{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000777 return pe->offset + get_start_sect(pe->part_table);
778}
779
Denis Vlasenko834410a2006-11-29 12:00:28 +0000780#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000781/*
782 * Avoid warning about DOS partitions when no DOS partition was changed.
783 * Here a heuristic "is probably dos partition".
784 * We might also do the opposite and warn in all cases except
785 * for "is probably nondos partition".
786 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000787#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000788static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000789is_dos_partition(int t)
790{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000791 return (t == 1 || t == 4 || t == 6 ||
792 t == 0x0b || t == 0x0c || t == 0x0e ||
793 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
794 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
795 t == 0xc1 || t == 0xc4 || t == 0xc6);
796}
Denis Vlasenko89398812008-01-25 20:18:46 +0000797#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000798
799static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000800menu(void)
801{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000802 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000803 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000804 puts("a\ttoggle a read only flag"); /* sun */
805 puts("b\tedit bsd disklabel");
806 puts("c\ttoggle the mountable flag"); /* sun */
807 puts("d\tdelete a partition");
808 puts("l\tlist known partition types");
809 puts("n\tadd a new partition");
810 puts("o\tcreate a new empty DOS partition table");
811 puts("p\tprint the partition table");
812 puts("q\tquit without saving changes");
813 puts("s\tcreate a new empty Sun disklabel"); /* sun */
814 puts("t\tchange a partition's system id");
815 puts("u\tchange display/entry units");
816 puts("v\tverify the partition table");
817 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000818#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000819 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000820#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000821 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000822 puts("a\tselect bootable partition"); /* sgi flavour */
823 puts("b\tedit bootfile entry"); /* sgi */
824 puts("c\tselect sgi swap partition"); /* sgi flavour */
825 puts("d\tdelete a partition");
826 puts("l\tlist known partition types");
827 puts("n\tadd a new partition");
828 puts("o\tcreate a new empty DOS partition table");
829 puts("p\tprint the partition table");
830 puts("q\tquit without saving changes");
831 puts("s\tcreate a new empty Sun disklabel"); /* sun */
832 puts("t\tchange a partition's system id");
833 puts("u\tchange display/entry units");
834 puts("v\tverify the partition table");
835 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000836 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000837 puts("o\tcreate a new empty DOS partition table");
838 puts("q\tquit without saving changes");
839 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000840 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000841 puts("a\ttoggle a bootable flag");
842 puts("b\tedit bsd disklabel");
843 puts("c\ttoggle the dos compatibility flag");
844 puts("d\tdelete a partition");
845 puts("l\tlist known partition types");
846 puts("n\tadd a new partition");
847 puts("o\tcreate a new empty DOS partition table");
848 puts("p\tprint the partition table");
849 puts("q\tquit without saving changes");
850 puts("s\tcreate a new empty Sun disklabel"); /* sun */
851 puts("t\tchange a partition's system id");
852 puts("u\tchange display/entry units");
853 puts("v\tverify the partition table");
854 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000855#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000856 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000857#endif
858 }
859}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000860#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000861
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000862
Denis Vlasenko834410a2006-11-29 12:00:28 +0000863#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000864static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000865xmenu(void)
866{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000867 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000868 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000869 puts("a\tchange number of alternate cylinders"); /*sun*/
870 puts("c\tchange number of cylinders");
871 puts("d\tprint the raw data in the partition table");
872 puts("e\tchange number of extra sectors per cylinder");/*sun*/
873 puts("h\tchange number of heads");
874 puts("i\tchange interleave factor"); /*sun*/
875 puts("o\tchange rotation speed (rpm)"); /*sun*/
876 puts("p\tprint the partition table");
877 puts("q\tquit without saving changes");
878 puts("r\treturn to main menu");
879 puts("s\tchange number of sectors/track");
880 puts("v\tverify the partition table");
881 puts("w\twrite table to disk and exit");
882 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000883 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000884 puts("b\tmove beginning of data in a partition"); /* !sun */
885 puts("c\tchange number of cylinders");
886 puts("d\tprint the raw data in the partition table");
887 puts("e\tlist extended partitions"); /* !sun */
888 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
889 puts("h\tchange number of heads");
890 puts("p\tprint the partition table");
891 puts("q\tquit without saving changes");
892 puts("r\treturn to main menu");
893 puts("s\tchange number of sectors/track");
894 puts("v\tverify the partition table");
895 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000896 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000897 puts("b\tmove beginning of data in a partition"); /* !sun */
898 puts("c\tchange number of cylinders");
899 puts("d\tprint the raw data in the partition table");
900 puts("e\tlist extended partitions"); /* !sun */
901 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
902 puts("h\tchange number of heads");
903 puts("p\tprint the partition table");
904 puts("q\tquit without saving changes");
905 puts("r\treturn to main menu");
906 puts("s\tchange number of sectors/track");
907 puts("v\tverify the partition table");
908 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000909 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000910 puts("b\tmove beginning of data in a partition"); /* !sun */
911 puts("c\tchange number of cylinders");
912 puts("d\tprint the raw data in the partition table");
913 puts("e\tlist extended partitions"); /* !sun */
914 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000915#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000916 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000917#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000918 puts("h\tchange number of heads");
919 puts("p\tprint the partition table");
920 puts("q\tquit without saving changes");
921 puts("r\treturn to main menu");
922 puts("s\tchange number of sectors/track");
923 puts("v\tverify the partition table");
924 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000925 }
926}
927#endif /* ADVANCED mode */
928
Denis Vlasenko834410a2006-11-29 12:00:28 +0000929#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000930static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000931get_sys_types(void)
932{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000934 LABEL_IS_SUN ? sun_sys_types :
935 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000936 i386_sys_types);
937}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000938#else
939#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200940#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000941
Denis Vlasenkobd852072007-03-19 14:43:38 +0000942static const char *
943partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000944{
945 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000946 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947
Denis Vlasenkobd852072007-03-19 14:43:38 +0000948 for (i = 0; types[i]; i++)
949 if ((unsigned char)types[i][0] == type)
950 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000951
Denis Vlasenkobd852072007-03-19 14:43:38 +0000952 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000953}
954
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200955static int
956is_cleared_partition(const struct partition *p)
957{
958 /* We consider partition "cleared" only if it has only zeros */
959 const char *cp = (const char *)p;
960 int cnt = sizeof(*p);
961 char bits = 0;
962 while (--cnt >= 0)
963 bits |= *cp++;
964 return (bits == 0);
965}
966
967static void
968clear_partition(struct partition *p)
969{
970 if (p)
971 memset(p, 0, sizeof(*p));
972}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000973
Denis Vlasenko834410a2006-11-29 12:00:28 +0000974#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000975static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000976get_sysid(int i)
977{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000978 return LABEL_IS_SUN ? sunlabel->infos[i].id :
979 (LABEL_IS_SGI ? sgi_get_sysid(i) :
980 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000981}
982
Denis Vlasenkobd852072007-03-19 14:43:38 +0000983static void
984list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000985{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000986 enum { COLS = 3 };
987
988 unsigned last[COLS];
989 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990 int i;
991
Denis Vlasenko9764d692008-07-09 21:20:50 +0000992 for (size = 0; sys[size]; size++)
993 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000995 done = 0;
996 for (i = COLS-1; i >= 0; i--) {
997 done += (size + i - done) / (i + 1);
998 last[COLS-1 - i] = done;
999 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001000
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001001 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001003 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001004 (unsigned char)sys[next][0],
1005 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001007 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001008 i = 0;
1009 next = ++done;
1010 }
1011 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001012 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013}
1014
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001015static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001016set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001017{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001019 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001020
1021 if (doext) {
1022 p = ptes[i].ext_pointer;
1023 offset = extended_offset;
1024 } else {
1025 p = ptes[i].part_table;
1026 offset = ptes[i].offset;
1027 }
1028 p->boot_ind = 0;
1029 p->sys_ind = sysid;
1030 set_start_sect(p, start - offset);
1031 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001032 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1033 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001034 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001035 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1036 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001037 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1038 ptes[i].changed = 1;
1039}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001040#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001041
1042static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001043warn_geometry(void)
1044{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001045 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001046 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001047
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001049 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001050 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001051 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001052 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001053 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001054 printf(" cylinders");
1055 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001056#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001058#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001059 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 return 1;
1061}
1062
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001063static void
1064update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001065{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001066 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001067
1068 if (display_in_cyl_units && cyl_units)
1069 units_per_sector = cyl_units;
1070 else
1071 units_per_sector = 1; /* in sectors */
1072}
1073
Denis Vlasenko834410a2006-11-29 12:00:28 +00001074#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001076warn_cylinders(void)
1077{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001078 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001079 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001080"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001081"There is nothing wrong with that, but this is larger than 1024,\n"
1082"and could in certain setups cause problems with:\n"
1083"1) software that runs at boot time (e.g., old versions of LILO)\n"
1084"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001085" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001086 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001087}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001088#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001089
1090static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001091read_extended(int ext)
1092{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001093 int i;
1094 struct pte *pex;
1095 struct partition *p, *q;
1096
1097 ext_index = ext;
1098 pex = &ptes[ext];
1099 pex->ext_pointer = pex->part_table;
1100
1101 p = pex->part_table;
1102 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001103 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001104 return;
1105 }
1106
Rob Landleyb73451d2006-02-24 16:29:00 +00001107 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001108 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001110 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001111 /* This is not a Linux restriction, but
1112 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001113 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001114 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001115#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001116 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001117 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001118 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001119#endif
1120 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001121 return;
1122 }
1123
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001124 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125
1126 if (!extended_offset)
1127 extended_offset = get_start_sect(p);
1128
1129 q = p = pt_offset(pe->sectorbuffer, 0);
1130 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001131 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001133 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001134 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001135 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001136 else
1137 pe->ext_pointer = p;
1138 } else if (p->sys_ind) {
1139 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001140 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001141 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001142 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001143 else
1144 pe->part_table = p;
1145 }
1146 }
1147
1148 /* very strange code here... */
1149 if (!pe->part_table) {
1150 if (q != pe->ext_pointer)
1151 pe->part_table = q;
1152 else
1153 pe->part_table = q + 1;
1154 }
1155 if (!pe->ext_pointer) {
1156 if (q != pe->part_table)
1157 pe->ext_pointer = q;
1158 else
1159 pe->ext_pointer = q + 1;
1160 }
1161
1162 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001163 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001164 }
1165
Denis Vlasenko834410a2006-11-29 12:00:28 +00001166#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001167 /* remove empty links */
1168 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001169 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001170 struct pte *pe = &ptes[i];
1171
Denis Vlasenkobd852072007-03-19 14:43:38 +00001172 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001173 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001174 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001175 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001176 delete_partition(i);
1177 goto remove; /* numbering changed */
1178 }
1179 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001180#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181}
1182
Denis Vlasenko834410a2006-11-29 12:00:28 +00001183#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001185create_doslabel(void)
1186{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001187 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001188
Denis Vlasenko4437d192008-04-17 00:12:10 +00001189 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001190#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 possibly_osf_label = 0;
1192#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001193 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001194
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001195 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001196 write_part_table_flag(MBRbuffer);
1197 extended_offset = 0;
1198 set_all_unchanged();
1199 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001200 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001201}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001202#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001203
1204static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001205get_sectorsize(void)
1206{
Rob Landley736e5252006-02-25 03:36:00 +00001207 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001209 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210 sector_size = arg;
1211 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001212 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001213 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1214 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215 }
1216}
1217
Rob Landley88621d72006-08-29 19:41:06 +00001218static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001219get_kernel_geometry(void)
1220{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001221 struct hd_geometry geometry;
1222
Denis Vlasenko4437d192008-04-17 00:12:10 +00001223 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001224 kern_heads = geometry.heads;
1225 kern_sectors = geometry.sectors;
1226 /* never use geometry.cylinders - it is truncated */
1227 }
1228}
1229
1230static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001231get_partition_table_geometry(void)
1232{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001233 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001234 struct partition *p;
1235 int i, h, s, hh, ss;
1236 int first = 1;
1237 int bad = 0;
1238
Eric Andersen3496fdc2006-01-30 23:09:20 +00001239 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001240 return;
1241
1242 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001243 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001244 p = pt_offset(bufp, i);
1245 if (p->sys_ind != 0) {
1246 h = p->end_head + 1;
1247 s = (p->end_sector & 077);
1248 if (first) {
1249 hh = h;
1250 ss = s;
1251 first = 0;
1252 } else if (hh != h || ss != s)
1253 bad = 1;
1254 }
1255 }
1256
1257 if (!first && !bad) {
1258 pt_heads = hh;
1259 pt_sectors = ss;
1260 }
1261}
1262
Rob Landleyb73451d2006-02-24 16:29:00 +00001263static void
1264get_geometry(void)
1265{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001266 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001267
1268 get_sectorsize();
1269 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001270#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001271 guess_device_type();
1272#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001273 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001274 kern_heads = kern_sectors = 0;
1275 pt_heads = pt_sectors = 0;
1276
1277 get_kernel_geometry();
1278 get_partition_table_geometry();
1279
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001280 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281 pt_heads ? pt_heads :
1282 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001283 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001284 pt_sectors ? pt_sectors :
1285 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001286 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001287
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288 sector_offset = 1;
1289 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001290 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001291
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001292 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1293 if (!g_cylinders)
1294 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295}
1296
1297/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001298 * Opens disk_device and optionally reads MBR.
1299 * FIXME: document what each 'what' value will do!
1300 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001301 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1302 * 0: found or created label
1303 * 1: I/O error
1304 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001305#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1306static int get_boot(enum action what)
1307#else
1308static int get_boot(void)
1309#define get_boot(what) get_boot()
1310#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001311{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001312 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001314 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315 for (i = 0; i < 4; i++) {
1316 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001317 pe->part_table = pt_offset(MBRbuffer, i);
1318 pe->ext_pointer = NULL;
1319 pe->offset = 0;
1320 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001321#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001322 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001323#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324 }
1325
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001327// ALERT! highly idiotic design!
1328// We end up here when we call get_boot() recursively
1329// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1330// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1331// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1332// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001333 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001334 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001335
Denis Vlasenko4437d192008-04-17 00:12:10 +00001336 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1337
Denis Vlasenkobd852072007-03-19 14:43:38 +00001338 if (fd < 0) {
1339 fd = open(disk_device, O_RDONLY);
1340 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001341 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001342 return 1;
1343 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001344 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001345 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001347 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001348 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001349 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001350 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001352 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 fdisk_fatal(unable_to_read);
1354 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001355#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001356 fd = open(disk_device, O_RDONLY);
1357 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001358 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001359 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001360 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001361 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001362 }
1363 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001364#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001366 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001367 update_units();
1368
Denis Vlasenko834410a2006-11-29 12:00:28 +00001369#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001370 if (check_sun_label())
1371 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001372#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001373#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374 if (check_sgi_label())
1375 return 0;
1376#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001377#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 if (check_aix_label())
1379 return 0;
1380#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001381#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001382 if (check_osf_label()) {
1383 possibly_osf_label = 1;
1384 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001385 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 return 0;
1387 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001388 printf("This disk has both DOS and BSD magic.\n"
1389 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390 }
1391#endif
1392
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001393#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001394 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001395 return -1;
1396#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001397 if (!valid_part_table_flag(MBRbuffer)) {
1398 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001399 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001401 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001403 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404#else
1405 create_doslabel();
1406#endif
1407 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001408 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001409 /* TRY_ONLY: */
1410 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001411 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001412 created_table:
1413#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414
Denis Vlasenko4437d192008-04-17 00:12:10 +00001415
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001416 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001417 warn_geometry();
1418
1419 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001420 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001421 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001422 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001423 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001424 else
1425 read_extended(i);
1426 }
1427 }
1428
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001429 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001431 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001432 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001433 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001434 pe->sectorbuffer[510],
1435 pe->sectorbuffer[511],
1436 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001437 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001438 }
1439 }
1440
1441 return 0;
1442}
1443
Denis Vlasenko834410a2006-11-29 12:00:28 +00001444#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001445/*
1446 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1447 * If the user hits Enter, DFLT is returned.
1448 * Answers like +10 are interpreted as offsets from BASE.
1449 *
1450 * There is no default if DFLT is not between LOW and HIGH.
1451 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001452static sector_t
1453read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001454{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001455 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001456 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001457 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001458
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001459 if (dflt < low || dflt > high) {
1460 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001462 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001463
1464 while (1) {
1465 int use_default = default_ok;
1466
1467 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001468 do {
1469 printf(fmt, mesg, low, high, dflt);
1470 read_maybe_empty("");
1471 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1472 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001473
Eric Andersen84bdea82004-05-19 10:49:17 +00001474 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001475 int minus = (*line_ptr == '-');
1476 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001477
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001478 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001479
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001480 /* (1) if 2nd char is digit, use_default = 0.
1481 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001482 while (isdigit(*++line_ptr))
1483 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001484
Rob Landleyb73451d2006-02-24 16:29:00 +00001485 switch (*line_ptr) {
1486 case 'c':
1487 case 'C':
1488 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001489 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001490 break;
1491 case 'K':
1492 absolute = 1024;
1493 break;
1494 case 'k':
1495 absolute = 1000;
1496 break;
1497 case 'm':
1498 case 'M':
1499 absolute = 1000000;
1500 break;
1501 case 'g':
1502 case 'G':
1503 absolute = 1000000000;
1504 break;
1505 default:
1506 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001507 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001508 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001509 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001510 unsigned long unit;
1511
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001512 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001513 unit = sector_size * units_per_sector;
1514 bytes += unit/2; /* round */
1515 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001516 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001517 }
1518 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001519 value = -value;
1520 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001521 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001522 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001523 while (isdigit(*line_ptr)) {
1524 line_ptr++;
1525 use_default = 0;
1526 }
1527 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001528 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001529 value = dflt;
1530 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001531 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001532 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001533 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001534 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001535 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001536 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001537}
1538
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001539static unsigned
1540get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001541{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001542 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001543 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544
Denis Vlasenkobd852072007-03-19 14:43:38 +00001545 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001546 pe = &ptes[i];
1547
1548 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001549 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1550 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1551 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1552 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001553 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001554 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555 }
1556 return i;
1557}
1558
1559static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001560get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001561{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001562 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001563 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001564
1565 for (i = 0; i < max; i++) {
1566 struct pte *pe = &ptes[i];
1567 struct partition *p = pe->part_table;
1568
1569 if (p && !is_cleared_partition(p)) {
1570 if (pno >= 0)
1571 goto not_unique;
1572 pno = i;
1573 }
1574 }
1575 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001576 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 return pno;
1578 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001579 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001580 return -1;
1581
1582 not_unique:
1583 return get_partition(warn, max);
1584}
1585
1586static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001587get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001588{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001590 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001591
1592 for (i = 0; i < max; i++) {
1593 struct pte *pe = &ptes[i];
1594 struct partition *p = pe->part_table;
1595
1596 if (p && is_cleared_partition(p)) {
1597 if (pno >= 0)
1598 goto not_unique;
1599 pno = i;
1600 }
1601 }
1602 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001603 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001604 return pno;
1605 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001606 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001607 return -1;
1608
1609 not_unique:
1610 return get_partition(warn, max);
1611}
1612
1613
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001614static void
1615change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001616{
1617 display_in_cyl_units = !display_in_cyl_units;
1618 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001619 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001620 str_units(PLURAL));
1621}
1622
1623static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001624toggle_active(int i)
1625{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001626 struct pte *pe = &ptes[i];
1627 struct partition *p = pe->part_table;
1628
Rob Landleyb73451d2006-02-24 16:29:00 +00001629 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001630 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001631 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1632 pe->changed = 1;
1633}
1634
1635static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001636toggle_dos_compatibility_flag(void)
1637{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001638 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001639 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001640 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001641 printf("DOS Compatibility flag is set\n");
1642 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001643 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001644 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001645 }
1646}
1647
1648static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001649delete_partition(int i)
1650{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001651 struct pte *pe = &ptes[i];
1652 struct partition *p = pe->part_table;
1653 struct partition *q = pe->ext_pointer;
1654
1655/* Note that for the fifth partition (i == 4) we don't actually
1656 * decrement partitions.
1657 */
1658
1659 if (warn_geometry())
1660 return; /* C/H/S not set */
1661 pe->changed = 1;
1662
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001663 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001664 sun_delete_partition(i);
1665 return;
1666 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001667 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001668 sgi_delete_partition(i);
1669 return;
1670 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001671
1672 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001673 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001674 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001675 ptes[ext_index].ext_pointer = NULL;
1676 extended_offset = 0;
1677 }
1678 clear_partition(p);
1679 return;
1680 }
1681
1682 if (!q->sys_ind && i > 4) {
1683 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001684 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001685 --i;
1686 clear_partition(ptes[i].ext_pointer);
1687 ptes[i].changed = 1;
1688 } else {
1689 /* not the last one - further ones will be moved down */
1690 if (i > 4) {
1691 /* delete this link in the chain */
1692 p = ptes[i-1].ext_pointer;
1693 *p = *q;
1694 set_start_sect(p, get_start_sect(q));
1695 set_nr_sects(p, get_nr_sects(q));
1696 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001697 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001698 /* the first logical in a longer chain */
1699 pe = &ptes[5];
1700
1701 if (pe->part_table) /* prevent SEGFAULT */
1702 set_start_sect(pe->part_table,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001703 get_partition_start(pe) -
1704 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001705 pe->offset = extended_offset;
1706 pe->changed = 1;
1707 }
1708
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001709 if (g_partitions > 5) {
1710 g_partitions--;
1711 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001712 ptes[i] = ptes[i+1];
1713 i++;
1714 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001715 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001716 /* the only logical: clear only */
1717 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001718 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001719 }
1720}
1721
1722static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001723change_sysid(void)
1724{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001725 int i, sys, origsys;
1726 struct partition *p;
1727
Eric Andersen040f4402003-07-30 08:40:37 +00001728 /* If sgi_label then don't use get_existing_partition,
1729 let the user select a partition, since get_existing_partition()
1730 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001731 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001732 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001733 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001734 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001735 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001736 if (i == -1)
1737 return;
1738 p = ptes[i].part_table;
1739 origsys = sys = get_sysid(i);
1740
1741 /* if changing types T to 0 is allowed, then
1742 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001743 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001744 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001745 return;
1746 }
1747 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001748 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001750 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001751 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001752 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001753 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001754 /* break; */
1755 }
1756
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001757 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001758 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001759 printf("You cannot change a partition into"
1760 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761 break;
1762 }
1763 }
1764
1765 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001766#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001767 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001768 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001769 "as Whole disk (5),\n"
1770 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001771 "even Linux likes it\n\n");
1772#endif
1773#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001774 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001775 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001776 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001777 (i == 8 && sys != 0)
1778 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001779 ) {
1780 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001781 "as volume header (0),\nand "
1782 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001783 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001784 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001785#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001786 if (sys == origsys)
1787 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001788 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001789 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001790 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001791 sgi_change_sysid(i, sys);
1792 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001794
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001795 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001796 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001797 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001799 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1800 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801 break;
1802 }
1803 }
1804}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001805#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001806
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001807
Denis Vlasenko28703012006-12-19 20:32:02 +00001808/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001809 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1810 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1811 * Lubkin Oct. 1991). */
1812
Rob Landleyb73451d2006-02-24 16:29:00 +00001813static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001814linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001815{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001816 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001817
1818 *c = ls / spc;
1819 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001820 *h = ls / g_sectors;
1821 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001822}
1823
Rob Landleyb73451d2006-02-24 16:29:00 +00001824static void
1825check_consistency(const struct partition *p, int partition)
1826{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001827 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1828 unsigned pec, peh, pes; /* physical ending c, h, s */
1829 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1830 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001831
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001832 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833 return; /* do not check extended partitions */
1834
1835/* physical beginning c, h, s */
1836 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1837 pbh = p->head;
1838 pbs = p->sector & 0x3f;
1839
1840/* physical ending c, h, s */
1841 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1842 peh = p->end_head;
1843 pes = p->end_sector & 0x3f;
1844
1845/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001846 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001847
1848/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001849 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001850
1851/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001852 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001853 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001854 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001855 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1856 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001857 }
1858
1859/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001860 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001861 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001862 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001863 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1864 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001865 }
1866
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001867/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001868 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001869 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001870 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001871 }
1872}
1873
1874static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001875list_disk_geometry(void)
1876{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001877 ullong bytes = ((ullong)total_number_of_sectors << 9);
1878 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001879
1880 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001881 printf("\nDisk %s: %lu MB, %llu bytes\n",
1882 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001883 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001884 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1885 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1886 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001887 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001888 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001889 printf(", total %"SECT_FMT"u sectors",
1890 total_number_of_sectors / (sector_size/512));
1891 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1892 str_units(PLURAL),
1893 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001894}
1895
1896/*
1897 * Check whether partition entries are ordered by their starting positions.
1898 * Return 0 if OK. Return i if partition i should have been earlier.
1899 * Two separate checks: primary and logical partitions.
1900 */
1901static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001902wrong_p_order(int *prev)
1903{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001904 const struct pte *pe;
1905 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001906 sector_t last_p_start_pos = 0, p_start_pos;
1907 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001908
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001909 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001910 if (i == 4) {
1911 last_i = 4;
1912 last_p_start_pos = 0;
1913 }
1914 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001915 p = pe->part_table;
1916 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001917 p_start_pos = get_partition_start(pe);
1918
1919 if (last_p_start_pos > p_start_pos) {
1920 if (prev)
1921 *prev = last_i;
1922 return i;
1923 }
1924
1925 last_p_start_pos = p_start_pos;
1926 last_i = i;
1927 }
1928 }
1929 return 0;
1930}
1931
Denis Vlasenko834410a2006-11-29 12:00:28 +00001932#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001933/*
1934 * Fix the chain of logicals.
1935 * extended_offset is unchanged, the set of sectors used is unchanged
1936 * The chain is sorted so that sectors increase, and so that
1937 * starting sectors increase.
1938 *
1939 * After this it may still be that cfdisk doesnt like the table.
1940 * (This is because cfdisk considers expanded parts, from link to
1941 * end of partition, and these may still overlap.)
1942 * Now
1943 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1944 * may help.
1945 */
1946static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001947fix_chain_of_logicals(void)
1948{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001949 int j, oj, ojj, sj, sjj;
1950 struct partition *pj,*pjj,tmp;
1951
1952 /* Stage 1: sort sectors but leave sector of part 4 */
1953 /* (Its sector is the global extended_offset.) */
1954 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001955 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001956 oj = ptes[j].offset;
1957 ojj = ptes[j+1].offset;
1958 if (oj > ojj) {
1959 ptes[j].offset = ojj;
1960 ptes[j+1].offset = oj;
1961 pj = ptes[j].part_table;
1962 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1963 pjj = ptes[j+1].part_table;
1964 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1965 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001966 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001967 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001968 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001969 goto stage1;
1970 }
1971 }
1972
1973 /* Stage 2: sort starting sectors */
1974 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001975 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001976 pj = ptes[j].part_table;
1977 pjj = ptes[j+1].part_table;
1978 sj = get_start_sect(pj);
1979 sjj = get_start_sect(pjj);
1980 oj = ptes[j].offset;
1981 ojj = ptes[j+1].offset;
1982 if (oj+sj > ojj+sjj) {
1983 tmp = *pj;
1984 *pj = *pjj;
1985 *pjj = tmp;
1986 set_start_sect(pj, ojj+sjj-oj);
1987 set_start_sect(pjj, oj+sj-ojj);
1988 goto stage2;
1989 }
1990 }
1991
1992 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001993 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001994 ptes[j].changed = 1;
1995}
1996
1997
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001998static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001999fix_partition_table_order(void)
2000{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002001 struct pte *pei, *pek;
2002 int i,k;
2003
2004 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002005 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006 return;
2007 }
2008
2009 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2010 /* partition i should have come earlier, move it */
2011 /* We have to move data in the MBR */
2012 struct partition *pi, *pk, *pe, pbuf;
2013 pei = &ptes[i];
2014 pek = &ptes[k];
2015
2016 pe = pei->ext_pointer;
2017 pei->ext_pointer = pek->ext_pointer;
2018 pek->ext_pointer = pe;
2019
2020 pi = pei->part_table;
2021 pk = pek->part_table;
2022
2023 memmove(&pbuf, pi, sizeof(struct partition));
2024 memmove(pi, pk, sizeof(struct partition));
2025 memmove(pk, &pbuf, sizeof(struct partition));
2026
2027 pei->changed = pek->changed = 1;
2028 }
2029
2030 if (i)
2031 fix_chain_of_logicals();
2032
2033 printf("Done.\n");
2034
2035}
2036#endif
2037
2038static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002039list_table(int xtra)
2040{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002041 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002042 int i, w;
2043
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002044 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 sun_list_table(xtra);
2046 return;
2047 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002048 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049 sgi_list_table(xtra);
2050 return;
2051 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002052
2053 list_disk_geometry();
2054
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002055 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002056 xbsd_print_disklabel(xtra);
2057 return;
2058 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002059
2060 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2061 but if the device name ends in a digit, say /dev/foo1,
2062 then the partition is called /dev/foo1p3. */
2063 w = strlen(disk_device);
2064 if (w && isdigit(disk_device[w-1]))
2065 w++;
2066 if (w < 5)
2067 w = 5;
2068
Denis Vlasenkobd852072007-03-19 14:43:38 +00002069 // 1 12345678901 12345678901 12345678901 12
2070 printf("%*s Boot Start End Blocks Id System\n",
2071 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002072
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002073 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002075 sector_t psects;
2076 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002077 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002078
2079 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002080 if (!p || is_cleared_partition(p))
2081 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002082
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002083 psects = get_nr_sects(p);
2084 pblocks = psects;
2085 podd = 0;
2086
2087 if (sector_size < 1024) {
2088 pblocks /= (1024 / sector_size);
2089 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002091 if (sector_size > 1024)
2092 pblocks *= (sector_size / 1024);
2093
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002094 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 +00002095 partname(disk_device, i+1, w+2),
2096 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2097 ? '*' : '?',
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002098 cround(get_partition_start(pe)), /* start */
2099 cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002100 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002101 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002102 p->sys_ind, /* type id */
2103 partition_type(p->sys_ind)); /* type name */
2104
2105 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106 }
2107
2108 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002109 /* partition table entries are not checked for correct order
2110 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002111 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002112 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002113 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002114 }
2115}
2116
Denis Vlasenko834410a2006-11-29 12:00:28 +00002117#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002119x_list_table(int extend)
2120{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121 const struct pte *pe;
2122 const struct partition *p;
2123 int i;
2124
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002125 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002126 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002127 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002128 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002129 pe = &ptes[i];
2130 p = (extend ? pe->ext_pointer : pe->part_table);
2131 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002132 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 +00002133 i + 1, p->boot_ind, p->head,
2134 sector(p->sector),
2135 cylinder(p->sector, p->cyl), p->end_head,
2136 sector(p->end_sector),
2137 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002138 get_start_sect(p), get_nr_sects(p),
2139 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 if (p->sys_ind)
2141 check_consistency(p, i);
2142 }
2143 }
2144}
2145#endif
2146
Denis Vlasenko834410a2006-11-29 12:00:28 +00002147#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002149fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002150{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002151 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152 const struct pte *pe = &ptes[0];
2153 const struct partition *p;
2154
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002155 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002157 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002158 first[i] = 0xffffffff;
2159 last[i] = 0;
2160 } else {
2161 first[i] = get_partition_start(pe);
2162 last[i] = first[i] + get_nr_sects(p) - 1;
2163 }
2164 }
2165}
2166
2167static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002168check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002169{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002170 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002171
2172 real_s = sector(s) - 1;
2173 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002174 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002176 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002177 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002178 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002179 n, h + 1, g_heads);
2180 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002181 printf("Partition %u: sector %u greater than "
2182 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002183 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002184 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2185 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002186 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002187 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2188 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002189}
2190
2191static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002192verify(void)
2193{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002194 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002195 sector_t total = 1;
2196 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002197 struct partition *p;
2198
2199 if (warn_geometry())
2200 return;
2201
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002202 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203 verify_sun();
2204 return;
2205 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002206 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 verify_sgi(1);
2208 return;
2209 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002210
2211 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002212 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002213 struct pte *pe = &ptes[i];
2214
2215 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002216 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217 check_consistency(p, i);
2218 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002219 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002220 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2222 last[i]);
2223 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002224 for (j = 0; j < i; j++) {
2225 if ((first[i] >= first[j] && first[i] <= last[j])
2226 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002227 printf("Warning: partition %u overlaps "
2228 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002229 total += first[i] >= first[j] ?
2230 first[i] : first[j];
2231 total -= last[i] <= last[j] ?
2232 last[i] : last[j];
2233 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002234 }
2235 }
2236 }
2237
2238 if (extended_offset) {
2239 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002240 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002241 get_nr_sects(pex->part_table) - 1;
2242
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002243 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002244 total++;
2245 p = ptes[i].part_table;
2246 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002247 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002248 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002249 "is empty\n", i + 1);
2250 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002251 printf("Logical partition %u not entirely in "
2252 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002254 }
2255 }
2256
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002257 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002258 printf("Total allocated sectors %u greater than the maximum "
2259 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002260 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002261 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002262 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002263 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002264 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002265}
2266
2267static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002268add_partition(int n, int sys)
2269{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002270 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002271 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272 struct partition *p = ptes[n].part_table;
2273 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002274 sector_t limit, temp;
2275 sector_t start, stop = 0;
2276 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002277
2278 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002279 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002280 return;
2281 }
2282 fill_bounds(first, last);
2283 if (n < 4) {
2284 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002285 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002286 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002288 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002289 if (extended_offset) {
2290 first[ext_index] = extended_offset;
2291 last[ext_index] = get_start_sect(q) +
2292 get_nr_sects(q) - 1;
2293 }
2294 } else {
2295 start = extended_offset + sector_offset;
2296 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2297 }
2298 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002299 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002300 first[i] = (cround(first[i]) - 1) * units_per_sector;
2301
Denis Vlasenkobd852072007-03-19 14:43:38 +00002302 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002303 do {
2304 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002305 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002306 int lastplusoff;
2307
2308 if (start == ptes[i].offset)
2309 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002310 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002311 if (start >= first[i] && start <= lastplusoff)
2312 start = lastplusoff + 1;
2313 }
2314 if (start > limit)
2315 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002316 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002317 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002318 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002319 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002320 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002321 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002322 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323
2324 saved_start = start;
2325 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2326 0, mesg);
2327 if (display_in_cyl_units) {
2328 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002329 if (start < saved_start)
2330 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002331 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002332 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002333 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002334 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 if (n > 4) { /* NOT for fifth partition */
2336 struct pte *pe = &ptes[n];
2337
2338 pe->offset = start - sector_offset;
2339 if (pe->offset == extended_offset) { /* must be corrected */
2340 pe->offset++;
2341 if (sector_offset == 1)
2342 start++;
2343 }
2344 }
2345
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002346 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 struct pte *pe = &ptes[i];
2348
2349 if (start < pe->offset && limit >= pe->offset)
2350 limit = pe->offset - 1;
2351 if (start < first[i] && limit >= first[i])
2352 limit = first[i] - 1;
2353 }
2354 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002355 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002356 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002357 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 return;
2359 }
2360 if (cround(start) == cround(limit)) {
2361 stop = limit;
2362 } else {
2363 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002364 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 str_units(SINGULAR));
2366 stop = read_int(cround(start), cround(limit), cround(limit),
2367 cround(start), mesg);
2368 if (display_in_cyl_units) {
2369 stop = stop * units_per_sector - 1;
2370 if (stop >limit)
2371 stop = limit;
2372 }
2373 }
2374
2375 set_partition(n, 0, start, stop, sys);
2376 if (n > 4)
2377 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2378
Rob Landleyb73451d2006-02-24 16:29:00 +00002379 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002380 struct pte *pe4 = &ptes[4];
2381 struct pte *pen = &ptes[n];
2382
2383 ext_index = n;
2384 pen->ext_pointer = p;
2385 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002386 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2388 pe4->ext_pointer = pe4->part_table + 1;
2389 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002390 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002391 }
2392}
2393
2394static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002395add_logical(void)
2396{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002397 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2398 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002399
Rob Landley081e3842006-08-03 20:07:35 +00002400 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2402 pe->ext_pointer = pe->part_table + 1;
2403 pe->offset = 0;
2404 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002405 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002406 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002407 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002408}
2409
2410static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002411new_partition(void)
2412{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002413 int i, free_primary = 0;
2414
2415 if (warn_geometry())
2416 return;
2417
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002418 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002419 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 return;
2421 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002422 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002423 sgi_add_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_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002427 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2428"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2429"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002430 return;
2431 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002433 for (i = 0; i < 4; i++)
2434 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002435
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002436 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002437 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002438 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002439 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002440
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002441 if (!free_primary) {
2442 if (extended_offset)
2443 add_logical();
2444 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002445 printf("You must delete some partition and add "
2446 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002447 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002448 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002449 snprintf(line, sizeof(line),
2450 "Command action\n"
2451 " %s\n"
2452 " p primary partition (1-4)\n",
2453 (extended_offset ?
2454 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002456 c = read_nonempty(line);
2457 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002458 i = get_nonexisting_partition(0, 4);
2459 if (i >= 0)
2460 add_partition(i, LINUX_NATIVE);
2461 return;
2462 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002463 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464 add_logical();
2465 return;
2466 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002467 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468 i = get_nonexisting_partition(0, 4);
2469 if (i >= 0)
2470 add_partition(i, EXTENDED);
2471 return;
2472 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002473 printf("Invalid partition number "
2474 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 }
2476 }
2477}
2478
2479static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002480write_table(void)
2481{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 int i;
2483
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002484 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002485 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486 if (ptes[i].changed)
2487 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002488 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 struct pte *pe = &ptes[i];
2490
2491 if (pe->changed) {
2492 write_part_table_flag(pe->sectorbuffer);
2493 write_sector(pe->offset, pe->sectorbuffer);
2494 }
2495 }
2496 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002497 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498 /* no test on change? the printf below might be mistaken */
2499 sgi_write_table();
2500 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002501 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 int needw = 0;
2503
Rob Landleyb73451d2006-02-24 16:29:00 +00002504 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505 if (ptes[i].changed)
2506 needw = 1;
2507 if (needw)
2508 sun_write_table();
2509 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510
Denis Vlasenkobd852072007-03-19 14:43:38 +00002511 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 reread_partition_table(1);
2513}
2514
Rob Landleyb73451d2006-02-24 16:29:00 +00002515static void
2516reread_partition_table(int leave)
2517{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518 int i;
2519
Denis Vlasenkobd852072007-03-19 14:43:38 +00002520 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002521 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002522 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002523 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002524 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002525 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002526#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002528 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002529 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002531 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002532#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002533
2534 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002535 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002536 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002537 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538 }
2539}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002540#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541
Denis Vlasenko834410a2006-11-29 12:00:28 +00002542#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543#define MAX_PER_LINE 16
2544static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002545print_buffer(char *pbuffer)
2546{
2547 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548
2549 for (i = 0, l = 0; i < sector_size; i++, l++) {
2550 if (l == 0)
2551 printf("0x%03X:", i);
2552 printf(" %02X", (unsigned char) pbuffer[i]);
2553 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002554 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555 l = -1;
2556 }
2557 }
2558 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002559 bb_putchar('\n');
2560 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002561}
2562
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002564print_raw(void)
2565{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002566 int i;
2567
Denis Vlasenkobd852072007-03-19 14:43:38 +00002568 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002569 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002571 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002572 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002574 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002575}
2576
2577static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002578move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002579{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002580 struct pte *pe = &ptes[i];
2581 struct partition *p = pe->part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002582 sector_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583
2584 if (warn_geometry())
2585 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002586 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002587 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 return;
2589 }
2590 first = get_partition_start(pe);
2591 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002592 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593
2594 if (new != get_nr_sects(p)) {
2595 first = get_nr_sects(p) + get_start_sect(p) - new;
2596 set_nr_sects(p, first);
2597 set_start_sect(p, new);
2598 pe->changed = 1;
2599 }
2600}
2601
2602static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002603xselect(void)
2604{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002605 char c;
2606
Rob Landleyb73451d2006-02-24 16:29:00 +00002607 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002608 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002609 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 switch (c) {
2611 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002612 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 break;
2615 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002617 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 break;
2619 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002620 user_cylinders = g_cylinders =
2621 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002622 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002623 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002624 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 warn_cylinders();
2627 break;
2628 case 'd':
2629 print_raw();
2630 break;
2631 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002632 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002634 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002635 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 x_list_table(1);
2638 break;
2639 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002640 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 fix_partition_table_order();
2642 break;
2643 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002644#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 create_sgilabel();
2646#endif
2647 break;
2648 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002649 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002650 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002651 update_units();
2652 break;
2653 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002654 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002655 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002656 break;
2657 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002658 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002659 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002660 break;
2661 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002662 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 list_table(1);
2664 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002665 x_list_table(0);
2666 break;
2667 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002668 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002669 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002670 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002671 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002672 case 'r':
2673 return;
2674 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002675 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002676 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002677 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002678 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002679 printf("Warning: setting sector offset for DOS "
2680 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002681 }
2682 update_units();
2683 break;
2684 case 'v':
2685 verify();
2686 break;
2687 case 'w':
2688 write_table(); /* does not return */
2689 break;
2690 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002691 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002693 break;
2694 default:
2695 xmenu();
2696 }
2697 }
2698}
2699#endif /* ADVANCED mode */
2700
2701static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002702is_ide_cdrom_or_tape(const char *device)
2703{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002704 FILE *procf;
2705 char buf[100];
2706 struct stat statbuf;
2707 int is_ide = 0;
2708
2709 /* No device was given explicitly, and we are trying some
2710 likely things. But opening /dev/hdc may produce errors like
2711 "hdc: tray open or drive not ready"
2712 if it happens to be a CD-ROM drive. It even happens that
2713 the process hangs on the attempt to read a music CD.
2714 So try to be careful. This only works since 2.1.73. */
2715
2716 if (strncmp("/dev/hd", device, 7))
2717 return 0;
2718
2719 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002720 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002721 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2722 is_ide = (!strncmp(buf, "cdrom", 5) ||
2723 !strncmp(buf, "tape", 4));
2724 else
2725 /* Now when this proc file does not exist, skip the
2726 device when it is read-only. */
2727 if (stat(device, &statbuf) == 0)
2728 is_ide = ((statbuf.st_mode & 0222) == 0);
2729
2730 if (procf)
2731 fclose(procf);
2732 return is_ide;
2733}
2734
Rob Landley5527b912006-02-25 03:46:10 +00002735
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002736static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002737open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002738{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002739 int gb;
2740
2741 disk_device = device;
2742 if (setjmp(listingbuf))
2743 return;
2744 if (!user_specified)
2745 if (is_ide_cdrom_or_tape(device))
2746 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002747
2748 /* Open disk_device, save file descriptor to dev_fd */
2749 errno = 0;
2750 gb = get_boot(TRY_ONLY);
2751 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002752 /* Ignore other errors, since we try IDE
2753 and SCSI hard disks which may not be
2754 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002755 if (user_specified || errno == EACCES)
2756 bb_perror_msg("can't open '%s'", device);
2757 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002759
2760 if (gb < 0) { /* no DOS signature */
2761 list_disk_geometry();
2762 if (LABEL_IS_AIX)
2763 goto ret;
2764#if ENABLE_FEATURE_OSF_LABEL
2765 if (bsd_trydev(device) < 0)
2766#endif
2767 printf("Disk %s doesn't contain a valid "
2768 "partition table\n", device);
2769 } else {
2770 list_table(0);
2771#if ENABLE_FEATURE_FDISK_WRITABLE
2772 if (!LABEL_IS_SUN && g_partitions > 4) {
2773 delete_partition(ext_index);
2774 }
2775#endif
2776 }
2777 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002778 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002779}
2780
2781/* for fdisk -l: try all things in /proc/partitions
2782 that look like a partition name (do not end in a digit) */
2783static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002784list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002785{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786 FILE *procpt;
2787 char line[100], ptname[100], devname[120], *s;
2788 int ma, mi, sz;
2789
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002790 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791
2792 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002793 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002794 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002795 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002796 for (s = ptname; *s; s++)
Denys Vlasenkobf1d3472010-02-24 08:13:30 +01002797 continue;
2798 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2799 if (s[-1] >= '1' && s[-1] <= '9')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002800 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002801 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002802 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002803 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002804#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002805 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002806#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002807}
2808
Denis Vlasenko834410a2006-11-29 12:00:28 +00002809#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002810static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002811unknown_command(int c)
2812{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002813 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002814}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002815#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002816
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002817int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002818int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002819{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002820 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002821 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822 * fdisk -v
2823 * fdisk -l [-b sectorsize] [-u] device ...
2824 * fdisk -s [partition] ...
2825 * fdisk [-b sectorsize] [-u] device
2826 *
2827 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002829 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002830
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002831 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002832
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002833 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002834 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002835 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002836 argv += optind;
2837 if (opt & OPT_b) { // -b
2838 /* Ugly: this sector size is really per device,
2839 so cannot be combined with multiple disks,
2840 and the same goes for the C/H/S options.
2841 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002842 if (sector_size != 512 && sector_size != 1024
2843 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002844 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002845 sector_offset = 2;
2846 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002847 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002848 if (user_heads <= 0 || user_heads >= 256)
2849 user_heads = 0;
2850 if (user_sectors <= 0 || user_sectors >= 64)
2851 user_sectors = 0;
2852 if (opt & OPT_u)
2853 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854
Denis Vlasenko834410a2006-11-29 12:00:28 +00002855#if ENABLE_FEATURE_FDISK_WRITABLE
2856 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002857 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002858#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002859 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002861 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002862 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002863 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002865 /* we don't have device names, */
2866 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002867 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 }
2869 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002870#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002871 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002872#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002873
Denis Vlasenko834410a2006-11-29 12:00:28 +00002874#if ENABLE_FEATURE_FDISK_BLKSIZE
2875 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002876 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002877
2878 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002879 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002880 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002881 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002882 unsigned long long size;
2883 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002884 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002886 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002887 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002889 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002890 }
2891 return 0;
2892 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002893#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894
Denis Vlasenko834410a2006-11-29 12:00:28 +00002895#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002896 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002897 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898
Denis Vlasenko834410a2006-11-29 12:00:28 +00002899 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002900 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002901
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002902 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002903 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002904 printf("Detected an OSF/1 disklabel on %s, entering "
2905 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002906 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002907 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002908 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 /* If we return we may want to make an empty DOS label? */
2910 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002911
2912 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002913 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002914 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002915 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002916 switch (c) {
2917 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002918 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002919 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002920 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002921 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002923 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002925 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926 else
2927 unknown_command(c);
2928 break;
2929 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002930 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002931 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002932 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002933 if (read_maybe_empty("Please enter the name of the "
2934 "new boot file: ") == '\n')
2935 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 else
2937 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002938 }
2939#if ENABLE_FEATURE_OSF_LABEL
2940 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002941 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942#endif
2943 break;
2944 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002945 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002946 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002947 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002948 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002949 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002950 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002952 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002953 else
2954 unknown_command(c);
2955 break;
2956 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002957 {
Eric Andersen040f4402003-07-30 08:40:37 +00002958 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002959 /* If sgi_label then don't use get_existing_partition,
2960 let the user select a partition, since
2961 get_existing_partition() only works for Linux-like
2962 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002963 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002964 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002965 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002966 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002967 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002968 if (j >= 0)
2969 delete_partition(j);
2970 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002971 break;
2972 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002973 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002974 create_sgiinfo();
2975 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002976 unknown_command(c);
2977 case 'l':
2978 list_types(get_sys_types());
2979 break;
2980 case 'm':
2981 menu();
2982 break;
2983 case 'n':
2984 new_partition();
2985 break;
2986 case 'o':
2987 create_doslabel();
2988 break;
2989 case 'p':
2990 list_table(0);
2991 break;
2992 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002993 if (ENABLE_FEATURE_CLEAN_UP)
2994 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002995 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002996 return 0;
2997 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002998#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999 create_sunlabel();
3000#endif
3001 break;
3002 case 't':
3003 change_sysid();
3004 break;
3005 case 'u':
3006 change_units();
3007 break;
3008 case 'v':
3009 verify();
3010 break;
3011 case 'w':
3012 write_table(); /* does not return */
3013 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003014#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003015 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003016 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003017 printf("\n\tSorry, no experts menu for SGI "
3018 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003019 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003020 xselect();
3021 break;
3022#endif
3023 default:
3024 unknown_command(c);
3025 menu();
3026 }
3027 }
3028 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003029#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003030}