blob: 3f2e0d3ae0ddf185e2a47b856bf9adaa623af754 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200110/*
111 * per partition table entry data
112 *
113 * The four primary partitions have the same sectorbuffer (MBRbuffer)
114 * and have NULL ext_pointer.
115 * Each logical partition table entry has two pointers, one for the
116 * partition and one link to the next one.
117 */
118struct pte {
119 struct partition *part_table; /* points into sectorbuffer */
120 struct partition *ext_pointer; /* points into sectorbuffer */
121 sector_t offset_from_dev_start; /* disk sector number */
122 char *sectorbuffer; /* disk sector contents */
123#if ENABLE_FEATURE_FDISK_WRITABLE
124 char changed; /* boolean */
125#endif
126};
127
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200128#define unable_to_open "can't open '%s'"
129#define unable_to_read "can't read from %s"
130#define unable_to_seek "can't seek on %s"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000131
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000132enum label_type {
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200133 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF, LABEL_GPT
Rob Landley5527b912006-02-25 03:46:10 +0000134};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000135
Denis Vlasenko4437d192008-04-17 00:12:10 +0000136#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000137
Denis Vlasenko834410a2006-11-29 12:00:28 +0000138#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000139#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000140#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000141#else
142#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000143#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000144#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000145
Denis Vlasenko834410a2006-11-29 12:00:28 +0000146#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000147#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000148#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000149#else
150#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000151#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000152#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000153
Denis Vlasenko834410a2006-11-29 12:00:28 +0000154#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000155#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000156#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000157#else
158#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000159#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000160#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000161
Denis Vlasenko834410a2006-11-29 12:00:28 +0000162#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000163#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000164#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000165#else
166#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000167#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000168#endif
Rob Landley5527b912006-02-25 03:46:10 +0000169
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200170#if ENABLE_FEATURE_GPT_LABEL
171#define LABEL_IS_GPT (LABEL_GPT == current_label_type)
172#define STATIC_GPT static
173#else
174#define LABEL_IS_GPT 0
175#define STATIC_GPT extern
176#endif
177
Denis Vlasenko4437d192008-04-17 00:12:10 +0000178enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000179
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000180static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000181#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000182static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000183static void reread_partition_table(int leave);
184static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200185static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000186static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200187static 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 +0000188#endif
189static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000190static void get_geometry(void);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200191static void read_pte(struct pte *pe, sector_t offset);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000192#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000193static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000194#else
195static int get_boot(void);
196#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000197
198#define PLURAL 0
199#define SINGULAR 1
200
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200201static sector_t get_start_sect(const struct partition *p);
202static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000203
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000204/* DOS partition types */
205
206static const char *const i386_sys_types[] = {
207 "\x00" "Empty",
208 "\x01" "FAT12",
209 "\x04" "FAT16 <32M",
210 "\x05" "Extended", /* DOS 3.3+ extended partition */
211 "\x06" "FAT16", /* DOS 16-bit >=32M */
212 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
213 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
214 "\x0b" "Win95 FAT32",
215 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
216 "\x0e" "Win95 FAT16 (LBA)",
217 "\x0f" "Win95 Ext'd (LBA)",
218 "\x11" "Hidden FAT12",
219 "\x12" "Compaq diagnostics",
220 "\x14" "Hidden FAT16 <32M",
221 "\x16" "Hidden FAT16",
222 "\x17" "Hidden HPFS/NTFS",
223 "\x1b" "Hidden Win95 FAT32",
224 "\x1c" "Hidden W95 FAT32 (LBA)",
225 "\x1e" "Hidden W95 FAT16 (LBA)",
226 "\x3c" "Part.Magic recovery",
227 "\x41" "PPC PReP Boot",
228 "\x42" "SFS",
229 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
230 "\x80" "Old Minix", /* Minix 1.4a and earlier */
231 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
232 "\x82" "Linux swap", /* also Solaris */
233 "\x83" "Linux",
234 "\x84" "OS/2 hidden C: drive",
235 "\x85" "Linux extended",
236 "\x86" "NTFS volume set",
237 "\x87" "NTFS volume set",
238 "\x8e" "Linux LVM",
239 "\x9f" "BSD/OS", /* BSDI */
240 "\xa0" "Thinkpad hibernation",
241 "\xa5" "FreeBSD", /* various BSD flavours */
242 "\xa6" "OpenBSD",
243 "\xa8" "Darwin UFS",
244 "\xa9" "NetBSD",
245 "\xab" "Darwin boot",
246 "\xb7" "BSDI fs",
247 "\xb8" "BSDI swap",
248 "\xbe" "Solaris boot",
249 "\xeb" "BeOS fs",
250 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
251 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
252 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
253 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
254 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
255 autodetect using persistent
256 superblock */
257#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
258 "\x02" "XENIX root",
259 "\x03" "XENIX usr",
260 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
261 "\x09" "AIX bootable", /* AIX data or Coherent */
262 "\x10" "OPUS",
263 "\x18" "AST SmartSleep",
264 "\x24" "NEC DOS",
265 "\x39" "Plan 9",
266 "\x40" "Venix 80286",
267 "\x4d" "QNX4.x",
268 "\x4e" "QNX4.x 2nd part",
269 "\x4f" "QNX4.x 3rd part",
270 "\x50" "OnTrack DM",
271 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
272 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
273 "\x53" "OnTrack DM6 Aux3",
274 "\x54" "OnTrackDM6",
275 "\x55" "EZ-Drive",
276 "\x56" "Golden Bow",
277 "\x5c" "Priam Edisk",
278 "\x61" "SpeedStor",
279 "\x64" "Novell Netware 286",
280 "\x65" "Novell Netware 386",
281 "\x70" "DiskSecure Multi-Boot",
282 "\x75" "PC/IX",
283 "\x93" "Amoeba",
284 "\x94" "Amoeba BBT", /* (bad block table) */
285 "\xa7" "NeXTSTEP",
286 "\xbb" "Boot Wizard hidden",
287 "\xc1" "DRDOS/sec (FAT-12)",
288 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
289 "\xc6" "DRDOS/sec (FAT-16)",
290 "\xc7" "Syrinx",
291 "\xda" "Non-FS data",
292 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
293 Concurrent DOS or CTOS */
294 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
295 "\xdf" "BootIt", /* BootIt EMBRM */
296 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
297 extended partition */
298 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
299 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
300 partition < 1024 cyl. */
301 "\xf1" "SpeedStor",
302 "\xf4" "SpeedStor", /* SpeedStor large partition */
303 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
304 "\xff" "BBT", /* Xenix Bad Block Table */
305#endif
306 NULL
307};
308
Denis Vlasenko4437d192008-04-17 00:12:10 +0000309enum {
310 dev_fd = 3 /* the disk */
311};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000312
313/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000314struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000315 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000316
317 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000318 int g_partitions; // = 4; /* maximum partition + 1 */
319 unsigned units_per_sector; // = 1;
320 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
321 unsigned user_set_sector_size;
322 unsigned sector_offset; // = 1;
323 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000324 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000325 smallint display_in_cyl_units; // = 1;
326#if ENABLE_FEATURE_OSF_LABEL
327 smallint possibly_osf_label;
328#endif
329
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000330 smallint listing; /* no aborts for fdisk -l */
331 smallint dos_compatible_flag; // = 1;
332#if ENABLE_FEATURE_FDISK_WRITABLE
333 //int dos_changed;
334 smallint nowarn; /* no warnings for fdisk -l/-s */
335#endif
336 int ext_index; /* the prime extended partition */
337 unsigned user_cylinders, user_heads, user_sectors;
338 unsigned pt_heads, pt_sectors;
339 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200340 sector_t extended_offset; /* offset of link pointers */
341 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000342
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000343 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000344 char line_buffer[80];
345 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000346 /* Raw disk label. For DOS-type partition tables the MBR,
347 * with descriptions of the primary partitions. */
348 char MBRbuffer[MAX_SECTOR_SIZE];
349 /* Partition tables */
350 struct pte ptes[MAXIMUM_PARTS];
351};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000352#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000353#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000354#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000355#define g_partitions (G.g_partitions )
356#define units_per_sector (G.units_per_sector )
357#define sector_size (G.sector_size )
358#define user_set_sector_size (G.user_set_sector_size)
359#define sector_offset (G.sector_offset )
360#define g_heads (G.g_heads )
361#define g_sectors (G.g_sectors )
362#define g_cylinders (G.g_cylinders )
363#define current_label_type (G.current_label_type )
364#define display_in_cyl_units (G.display_in_cyl_units)
365#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000366#define listing (G.listing )
367#define dos_compatible_flag (G.dos_compatible_flag )
368#define nowarn (G.nowarn )
369#define ext_index (G.ext_index )
370#define user_cylinders (G.user_cylinders )
371#define user_heads (G.user_heads )
372#define user_sectors (G.user_sectors )
373#define pt_heads (G.pt_heads )
374#define pt_sectors (G.pt_sectors )
375#define kern_heads (G.kern_heads )
376#define kern_sectors (G.kern_sectors )
377#define extended_offset (G.extended_offset )
378#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000379#define listingbuf (G.listingbuf )
380#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000381#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000382#define MBRbuffer (G.MBRbuffer )
383#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000384#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000385 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000386 sector_size = DEFAULT_SECTOR_SIZE; \
387 sector_offset = 1; \
388 g_partitions = 4; \
389 display_in_cyl_units = 1; \
390 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000391 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000392} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000393
Denis Vlasenkobd852072007-03-19 14:43:38 +0000394
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000395/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200396/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
397 * disks > 2^32 sectors
398 */
399static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000400{
401 uint64_t v64;
402 unsigned long longsectors;
403
404 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000405 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200406 v64 >>= 9;
407 if (v64 != (sector_t)v64) {
408 ret_trunc:
409 /* Not only DOS, but all other partition tables
410 * we support can't record more than 32 bit
411 * sector counts or offsets
412 */
413 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
414 v64 = (uint32_t)-1L;
415 }
416 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000417 }
418 /* Needs temp of type long */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200419 if (ioctl(fd, BLKGETSIZE, &longsectors)) {
420 /* Perhaps this is a disk image */
421 off_t sz = lseek(fd, 0, SEEK_END);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000422 longsectors = 0;
Denys Vlasenkod958e902010-04-06 02:32:26 +0200423 if (sz > 0)
424 longsectors = (uoff_t)sz / sector_size;
425 lseek(fd, 0, SEEK_SET);
426 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200427 if (sizeof(long) > sizeof(sector_t)
428 && longsectors != (sector_t)longsectors
429 ) {
430 goto ret_trunc;
431 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000432 return longsectors;
433}
434
435
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000436#define IS_EXTENDED(i) \
437 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
438
439#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
440
441#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
442
443#define pt_offset(b, n) \
444 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
445
446#define sector(s) ((s) & 0x3f)
447
448#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
449
450#define hsc2sector(h,s,c) \
451 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
452
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000453static void
454close_dev_fd(void)
455{
456 /* Not really closing, but making sure it is open, and to harmless place */
457 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
458}
459
Denis Vlasenkobd852072007-03-19 14:43:38 +0000460/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000461 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000462 */
463static const char *
464partname(const char *dev, int pno, int lth)
465{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000466 const char *p;
467 int w, wp;
468 int bufsiz;
469 char *bufp;
470
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000471 bufp = partname_buffer;
472 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000473
474 w = strlen(dev);
475 p = "";
476
477 if (isdigit(dev[w-1]))
478 p = "p";
479
480 /* devfs kludge - note: fdisk partition names are not supposed
481 to equal kernel names, so there is no reason to do this */
482 if (strcmp(dev + w - 4, "disc") == 0) {
483 w -= 4;
484 p = "part";
485 }
486
487 wp = strlen(p);
488
489 if (lth) {
490 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200491 lth-wp-2, w, dev, p, pno);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000492 } else {
493 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
494 }
495 return bufp;
496}
497
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000498static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000499get_part_table(int i)
500{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000501 return ptes[i].part_table;
502}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000503
504static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000505str_units(int n)
506{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000507 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000508 return display_in_cyl_units ? "cylinder" : "sector";
509 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000510}
511
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000512static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000513valid_part_table_flag(const char *mbuffer)
514{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000515 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000516}
517
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200518static void fdisk_fatal(const char *why)
519{
520 if (listing) {
521 close_dev_fd();
522 longjmp(listingbuf, 1);
523 }
524 bb_error_msg_and_die(why, disk_device);
525}
526
527static void
528seek_sector(sector_t secno)
529{
530#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
531 off64_t off = (off64_t)secno * sector_size;
532 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
533 fdisk_fatal(unable_to_seek);
534#else
535 uint64_t off = (uint64_t)secno * sector_size;
536 if (off > MAXINT(off_t)
537 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
538 ) {
539 fdisk_fatal(unable_to_seek);
540 }
541#endif
542}
543
Denis Vlasenko834410a2006-11-29 12:00:28 +0000544#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200545/* Read line; return 0 or first printable char */
546static int
547read_line(const char *prompt)
548{
549 int sz;
550
551 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
552 if (sz <= 0)
553 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
554
555 if (line_buffer[sz-1] == '\n')
556 line_buffer[--sz] = '\0';
557
558 line_ptr = line_buffer;
559 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
560 line_ptr++;
561 return *line_ptr;
562}
563
564static void
565set_all_unchanged(void)
566{
567 int i;
568
569 for (i = 0; i < MAXIMUM_PARTS; i++)
570 ptes[i].changed = 0;
571}
572
573static ALWAYS_INLINE void
574set_changed(int i)
575{
576 ptes[i].changed = 1;
577}
578
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000579static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000580write_part_table_flag(char *b)
581{
582 b[510] = 0x55;
583 b[511] = 0xaa;
584}
585
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000586static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000587read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000589 while (!read_line(mesg))
590 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000591 return *line_ptr;
592}
593
594static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000595read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000596{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000597 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000598 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000599 line_ptr[0] = '\n';
600 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000601 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000602 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603}
604
605static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000606read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000607{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000608 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000609 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000610 read_nonempty("Hex code (type L to list codes): ");
Denys Vlasenkod958e902010-04-06 02:32:26 +0200611 if ((line_ptr[0] | 0x20) == 'l') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000612 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000613 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000614 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000615 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200616 if (v <= 0xff)
617 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000618 }
619}
620
Denis Vlasenko9764d692008-07-09 21:20:50 +0000621static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200622write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000623{
624 seek_sector(secno);
625 xwrite(dev_fd, buf, sector_size);
626}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200627#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000628
629
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000630#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000631
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100632struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000633 unsigned char info[128]; /* Informative text string */
634 unsigned char spare0[14];
635 struct sun_info {
636 unsigned char spare1;
637 unsigned char id;
638 unsigned char spare2;
639 unsigned char flags;
640 } infos[8];
641 unsigned char spare1[246]; /* Boot information etc. */
642 unsigned short rspeed; /* Disk rotational speed */
643 unsigned short pcylcount; /* Physical cylinder count */
644 unsigned short sparecyl; /* extra sects per cylinder */
645 unsigned char spare2[4]; /* More magic... */
646 unsigned short ilfact; /* Interleave factor */
647 unsigned short ncyl; /* Data cylinder count */
648 unsigned short nacyl; /* Alt. cylinder count */
649 unsigned short ntrks; /* Tracks per cylinder */
650 unsigned short nsect; /* Sectors per track */
651 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000652 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000653 uint32_t start_cylinder;
654 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655 } partitions[8];
656 unsigned short magic; /* Magic number */
657 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100658} FIX_ALIASING;
659typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000660#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000661STATIC_OSF void bsd_select(void);
662STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000663#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000664
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200665#include "fdisk_gpt.c"
666
Denis Vlasenko28703012006-12-19 20:32:02 +0000667#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000668static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000669fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000670{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000671 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000672}
673
Rob Landley88621d72006-08-29 19:41:06 +0000674static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000675fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000676{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000677 return (x << 24) |
678 ((x & 0xFF00) << 8) |
679 ((x & 0xFF0000) >> 8) |
680 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681}
682#endif
683
Denis Vlasenkobd852072007-03-19 14:43:38 +0000684STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000685STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000686STATIC_SGI int sgi_get_sysid(int i);
687STATIC_SGI void sgi_delete_partition(int i);
688STATIC_SGI void sgi_change_sysid(int i, int sys);
689STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000690#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000691STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000692#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000693STATIC_SGI int verify_sgi(int verbose);
694STATIC_SGI void sgi_add_partition(int n, int sys);
695STATIC_SGI void sgi_set_swappartition(int i);
696STATIC_SGI const char *sgi_get_bootfile(void);
697STATIC_SGI void sgi_set_bootfile(const char* aFile);
698STATIC_SGI void create_sgiinfo(void);
699STATIC_SGI void sgi_write_table(void);
700STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000701#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000702
Denis Vlasenkobd852072007-03-19 14:43:38 +0000703STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000704STATIC_SUN void sun_delete_partition(int i);
705STATIC_SUN void sun_change_sysid(int i, int sys);
706STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000707STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000708#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000709STATIC_SUN void sun_set_alt_cyl(void);
710STATIC_SUN void sun_set_ncyl(int cyl);
711STATIC_SUN void sun_set_xcyl(void);
712STATIC_SUN void sun_set_ilfact(void);
713STATIC_SUN void sun_set_rspeed(void);
714STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000715#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000716STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
717STATIC_SUN void verify_sun(void);
718STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000719#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000720
Denis Vlasenko9764d692008-07-09 21:20:50 +0000721
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200722static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000723read4_little_endian(const unsigned char *cp)
724{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200725 uint32_t v;
726 move_from_unaligned32(v, cp);
727 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000728}
729
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200730static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000731get_start_sect(const struct partition *p)
732{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000733 return read4_little_endian(p->start4);
734}
735
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200736static sector_t
737get_nr_sects(const struct partition *p)
738{
739 return read4_little_endian(p->size4);
740}
741
Denis Vlasenko834410a2006-11-29 12:00:28 +0000742#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200743/* start_sect and nr_sects are stored little endian on all machines */
744/* moreover, they are not aligned correctly */
745static inline_if_little_endian void
746store4_little_endian(unsigned char *cp, unsigned val)
747{
748 uint32_t v = SWAP_LE32(val);
749 move_to_unaligned32(cp, v);
750}
751
752static void
753set_start_sect(struct partition *p, unsigned start_sect)
754{
755 store4_little_endian(p->start4, start_sect);
756}
757
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000758static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000759set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000760{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000761 store4_little_endian(p->size4, nr_sects);
762}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000763#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000764
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000765/* Allocate a buffer and read a partition table sector */
766static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200767read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000768{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200769 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000770 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000771 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000772 /* xread would make us abort - bad for fdisk -l */
773 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000774 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000775#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000776 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000777#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778 pe->part_table = pe->ext_pointer = NULL;
779}
780
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200781static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200782get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000783{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200784 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000785}
786
Denis Vlasenko834410a2006-11-29 12:00:28 +0000787#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000788/*
789 * Avoid warning about DOS partitions when no DOS partition was changed.
790 * Here a heuristic "is probably dos partition".
791 * We might also do the opposite and warn in all cases except
792 * for "is probably nondos partition".
793 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000794#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000795static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000796is_dos_partition(int t)
797{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000798 return (t == 1 || t == 4 || t == 6 ||
799 t == 0x0b || t == 0x0c || t == 0x0e ||
800 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
801 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
802 t == 0xc1 || t == 0xc4 || t == 0xc6);
803}
Denis Vlasenko89398812008-01-25 20:18:46 +0000804#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000805
806static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000807menu(void)
808{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000809 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000810 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000811 puts("a\ttoggle a read only flag"); /* sun */
812 puts("b\tedit bsd disklabel");
813 puts("c\ttoggle the mountable flag"); /* sun */
814 puts("d\tdelete a partition");
815 puts("l\tlist known partition types");
816 puts("n\tadd a new partition");
817 puts("o\tcreate a new empty DOS partition table");
818 puts("p\tprint the partition table");
819 puts("q\tquit without saving changes");
820 puts("s\tcreate a new empty Sun disklabel"); /* sun */
821 puts("t\tchange a partition's system id");
822 puts("u\tchange display/entry units");
823 puts("v\tverify the partition table");
824 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000825#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000826 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000827#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000828 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000829 puts("a\tselect bootable partition"); /* sgi flavour */
830 puts("b\tedit bootfile entry"); /* sgi */
831 puts("c\tselect sgi swap partition"); /* sgi flavour */
832 puts("d\tdelete a partition");
833 puts("l\tlist known partition types");
834 puts("n\tadd a new partition");
835 puts("o\tcreate a new empty DOS partition table");
836 puts("p\tprint the partition table");
837 puts("q\tquit without saving changes");
838 puts("s\tcreate a new empty Sun disklabel"); /* sun */
839 puts("t\tchange a partition's system id");
840 puts("u\tchange display/entry units");
841 puts("v\tverify the partition table");
842 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000843 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000844 puts("o\tcreate a new empty DOS partition table");
845 puts("q\tquit without saving changes");
846 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200847 } else if (LABEL_IS_GPT) {
848 puts("o\tcreate a new empty DOS partition table");
849 puts("p\tprint the partition table");
850 puts("q\tquit without saving changes");
851 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000852 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000853 puts("a\ttoggle a bootable flag");
854 puts("b\tedit bsd disklabel");
855 puts("c\ttoggle the dos compatibility flag");
856 puts("d\tdelete a partition");
857 puts("l\tlist known partition types");
858 puts("n\tadd a new partition");
859 puts("o\tcreate a new empty DOS partition table");
860 puts("p\tprint the partition table");
861 puts("q\tquit without saving changes");
862 puts("s\tcreate a new empty Sun disklabel"); /* sun */
863 puts("t\tchange a partition's system id");
864 puts("u\tchange display/entry units");
865 puts("v\tverify the partition table");
866 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000867#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000868 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000869#endif
870 }
871}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000872#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000873
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000874
Denis Vlasenko834410a2006-11-29 12:00:28 +0000875#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000876static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000877xmenu(void)
878{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000879 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000880 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000881 puts("a\tchange number of alternate cylinders"); /*sun*/
882 puts("c\tchange number of cylinders");
883 puts("d\tprint the raw data in the partition table");
884 puts("e\tchange number of extra sectors per cylinder");/*sun*/
885 puts("h\tchange number of heads");
886 puts("i\tchange interleave factor"); /*sun*/
887 puts("o\tchange rotation speed (rpm)"); /*sun*/
888 puts("p\tprint the partition table");
889 puts("q\tquit without saving changes");
890 puts("r\treturn to main menu");
891 puts("s\tchange number of sectors/track");
892 puts("v\tverify the partition table");
893 puts("w\twrite table to disk and exit");
894 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000895 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000896 puts("b\tmove beginning of data in a partition"); /* !sun */
897 puts("c\tchange number of cylinders");
898 puts("d\tprint the raw data in the partition table");
899 puts("e\tlist extended partitions"); /* !sun */
900 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
901 puts("h\tchange number of heads");
902 puts("p\tprint the partition table");
903 puts("q\tquit without saving changes");
904 puts("r\treturn to main menu");
905 puts("s\tchange number of sectors/track");
906 puts("v\tverify the partition table");
907 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000908 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000909 puts("b\tmove beginning of data in a partition"); /* !sun */
910 puts("c\tchange number of cylinders");
911 puts("d\tprint the raw data in the partition table");
912 puts("e\tlist extended partitions"); /* !sun */
913 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
914 puts("h\tchange number of heads");
915 puts("p\tprint the partition table");
916 puts("q\tquit without saving changes");
917 puts("r\treturn to main menu");
918 puts("s\tchange number of sectors/track");
919 puts("v\tverify the partition table");
920 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000921 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000922 puts("b\tmove beginning of data in a partition"); /* !sun */
923 puts("c\tchange number of cylinders");
924 puts("d\tprint the raw data in the partition table");
925 puts("e\tlist extended partitions"); /* !sun */
926 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000927#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000928 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000930 puts("h\tchange number of heads");
931 puts("p\tprint the partition table");
932 puts("q\tquit without saving changes");
933 puts("r\treturn to main menu");
934 puts("s\tchange number of sectors/track");
935 puts("v\tverify the partition table");
936 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000937 }
938}
939#endif /* ADVANCED mode */
940
Denis Vlasenko834410a2006-11-29 12:00:28 +0000941#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000942static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000943get_sys_types(void)
944{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000945 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000946 LABEL_IS_SUN ? sun_sys_types :
947 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000948 i386_sys_types);
949}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000950#else
951#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200952#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000953
Denis Vlasenkobd852072007-03-19 14:43:38 +0000954static const char *
955partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956{
957 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000958 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000959
Denis Vlasenkobd852072007-03-19 14:43:38 +0000960 for (i = 0; types[i]; i++)
961 if ((unsigned char)types[i][0] == type)
962 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000963
Denis Vlasenkobd852072007-03-19 14:43:38 +0000964 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000965}
966
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200967static int
968is_cleared_partition(const struct partition *p)
969{
970 /* We consider partition "cleared" only if it has only zeros */
971 const char *cp = (const char *)p;
972 int cnt = sizeof(*p);
973 char bits = 0;
974 while (--cnt >= 0)
975 bits |= *cp++;
976 return (bits == 0);
977}
978
979static void
980clear_partition(struct partition *p)
981{
982 if (p)
983 memset(p, 0, sizeof(*p));
984}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000985
Denis Vlasenko834410a2006-11-29 12:00:28 +0000986#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000987static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000988get_sysid(int i)
989{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000990 return LABEL_IS_SUN ? sunlabel->infos[i].id :
991 (LABEL_IS_SGI ? sgi_get_sysid(i) :
992 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000993}
994
Denis Vlasenkobd852072007-03-19 14:43:38 +0000995static void
996list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000997{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000998 enum { COLS = 3 };
999
1000 unsigned last[COLS];
1001 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002 int i;
1003
Denis Vlasenko9764d692008-07-09 21:20:50 +00001004 for (size = 0; sys[size]; size++)
1005 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001007 done = 0;
1008 for (i = COLS-1; i >= 0; i--) {
1009 done += (size + i - done) / (i + 1);
1010 last[COLS-1 - i] = done;
1011 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001012
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001013 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001014 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001015 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001016 (unsigned char)sys[next][0],
1017 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001019 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001020 i = 0;
1021 next = ++done;
1022 }
1023 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001024 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001025}
1026
Denys Vlasenkod958e902010-04-06 02:32:26 +02001027#define set_hsc(h, s, c, sector) do \
1028{ \
1029 s = sector % g_sectors + 1; \
1030 sector /= g_sectors; \
1031 h = sector % g_heads; \
1032 sector /= g_heads; \
1033 c = sector & 0xff; \
1034 s |= (sector >> 2) & 0xc0; \
1035} while (0)
1036
1037static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1038{
1039 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1040 start = g_heads * g_sectors * 1024 - 1;
1041 set_hsc(p->head, p->sector, p->cyl, start);
1042
1043 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1044 stop = g_heads * g_sectors * 1024 - 1;
1045 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1046}
1047
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001048static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001049set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001050{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001052 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001053
1054 if (doext) {
1055 p = ptes[i].ext_pointer;
1056 offset = extended_offset;
1057 } else {
1058 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001059 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 }
1061 p->boot_ind = 0;
1062 p->sys_ind = sysid;
1063 set_start_sect(p, start - offset);
1064 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001065 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001066 ptes[i].changed = 1;
1067}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001068#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001069
1070static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001071warn_geometry(void)
1072{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001073 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001074 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001075
Denis Vlasenkobd852072007-03-19 14:43:38 +00001076 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001077 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001078 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001079 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001080 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001081 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001082 printf(" cylinders");
1083 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001084#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001085 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001086#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001087 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001088 return 1;
1089}
1090
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001091static void
1092update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001093{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001094 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001095
1096 if (display_in_cyl_units && cyl_units)
1097 units_per_sector = cyl_units;
1098 else
1099 units_per_sector = 1; /* in sectors */
1100}
1101
Denis Vlasenko834410a2006-11-29 12:00:28 +00001102#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001103static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001104warn_cylinders(void)
1105{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001106 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001107 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001108"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109"There is nothing wrong with that, but this is larger than 1024,\n"
1110"and could in certain setups cause problems with:\n"
1111"1) software that runs at boot time (e.g., old versions of LILO)\n"
1112"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001113" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001114 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001116#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001117
1118static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001119read_extended(int ext)
1120{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001121 int i;
1122 struct pte *pex;
1123 struct partition *p, *q;
1124
1125 ext_index = ext;
1126 pex = &ptes[ext];
1127 pex->ext_pointer = pex->part_table;
1128
1129 p = pex->part_table;
1130 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001131 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 return;
1133 }
1134
Rob Landleyb73451d2006-02-24 16:29:00 +00001135 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001136 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001137
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001138 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001139 /* This is not a Linux restriction, but
1140 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001141 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001142 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001143#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001144 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001145 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001146 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001147#endif
1148 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001149 return;
1150 }
1151
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001152 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001153
1154 if (!extended_offset)
1155 extended_offset = get_start_sect(p);
1156
1157 q = p = pt_offset(pe->sectorbuffer, 0);
1158 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001159 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001160 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001161 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001162 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001163 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001164 else
1165 pe->ext_pointer = p;
1166 } else if (p->sys_ind) {
1167 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001168 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001169 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001170 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 else
1172 pe->part_table = p;
1173 }
1174 }
1175
1176 /* very strange code here... */
1177 if (!pe->part_table) {
1178 if (q != pe->ext_pointer)
1179 pe->part_table = q;
1180 else
1181 pe->part_table = q + 1;
1182 }
1183 if (!pe->ext_pointer) {
1184 if (q != pe->part_table)
1185 pe->ext_pointer = q;
1186 else
1187 pe->ext_pointer = q + 1;
1188 }
1189
1190 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001191 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001192 }
1193
Denis Vlasenko834410a2006-11-29 12:00:28 +00001194#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001195 /* remove empty links */
1196 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001197 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001198 struct pte *pe = &ptes[i];
1199
Denis Vlasenkobd852072007-03-19 14:43:38 +00001200 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001201 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001202 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001203 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001204 delete_partition(i);
1205 goto remove; /* numbering changed */
1206 }
1207 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001208#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001209}
1210
Denis Vlasenko834410a2006-11-29 12:00:28 +00001211#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001213create_doslabel(void)
1214{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001215 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001216
Denis Vlasenko4437d192008-04-17 00:12:10 +00001217 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001218#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219 possibly_osf_label = 0;
1220#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001221 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001223 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001224 write_part_table_flag(MBRbuffer);
1225 extended_offset = 0;
1226 set_all_unchanged();
1227 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001228 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001230#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001231
1232static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001233get_sectorsize(void)
1234{
Rob Landley736e5252006-02-25 03:36:00 +00001235 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001237 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001238 sector_size = arg;
1239 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001240 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001241 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1242 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 }
1244}
1245
Rob Landley88621d72006-08-29 19:41:06 +00001246static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001247get_kernel_geometry(void)
1248{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001249 struct hd_geometry geometry;
1250
Denis Vlasenko4437d192008-04-17 00:12:10 +00001251 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 kern_heads = geometry.heads;
1253 kern_sectors = geometry.sectors;
1254 /* never use geometry.cylinders - it is truncated */
1255 }
1256}
1257
1258static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001259get_partition_table_geometry(void)
1260{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001261 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001262 struct partition *p;
1263 int i, h, s, hh, ss;
1264 int first = 1;
1265 int bad = 0;
1266
Eric Andersen3496fdc2006-01-30 23:09:20 +00001267 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001268 return;
1269
1270 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001271 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272 p = pt_offset(bufp, i);
1273 if (p->sys_ind != 0) {
1274 h = p->end_head + 1;
1275 s = (p->end_sector & 077);
1276 if (first) {
1277 hh = h;
1278 ss = s;
1279 first = 0;
1280 } else if (hh != h || ss != s)
1281 bad = 1;
1282 }
1283 }
1284
1285 if (!first && !bad) {
1286 pt_heads = hh;
1287 pt_sectors = ss;
1288 }
1289}
1290
Rob Landleyb73451d2006-02-24 16:29:00 +00001291static void
1292get_geometry(void)
1293{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001294 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295
1296 get_sectorsize();
1297 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001298#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001299 guess_device_type();
1300#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001301 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001302 kern_heads = kern_sectors = 0;
1303 pt_heads = pt_sectors = 0;
1304
1305 get_kernel_geometry();
1306 get_partition_table_geometry();
1307
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001308 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309 pt_heads ? pt_heads :
1310 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001311 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001312 pt_sectors ? pt_sectors :
1313 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001314 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001315
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001316 sector_offset = 1;
1317 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001318 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001320 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1321 if (!g_cylinders)
1322 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323}
1324
1325/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001326 * Opens disk_device and optionally reads MBR.
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001327 * If what == OPEN_MAIN:
1328 * Open device, read MBR. Abort program on short read. Create empty
1329 * disklabel if the on-disk structure is invalid (WRITABLE mode).
1330 * If what == TRY_ONLY:
1331 * Open device, read MBR. Return an error if anything is out of place.
1332 * Do not create an empty disklabel. This is used for the "list"
1333 * operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1334 * If what == CREATE_EMPTY_*:
1335 * This means that get_boot() was called recursively from create_*label().
1336 * Do not re-open the device; just set up the ptes array and print
1337 * geometry warnings.
1338 *
Denis Vlasenko4437d192008-04-17 00:12:10 +00001339 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001340 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1341 * 0: found or created label
1342 * 1: I/O error
1343 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001344#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1345static int get_boot(enum action what)
1346#else
1347static int get_boot(void)
1348#define get_boot(what) get_boot()
1349#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001350{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001351 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001352
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001353 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001354 for (i = 0; i < 4; i++) {
1355 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 pe->part_table = pt_offset(MBRbuffer, i);
1357 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001358 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001360#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001361 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001362#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001363 }
1364
Denis Vlasenko834410a2006-11-29 12:00:28 +00001365#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001366// ALERT! highly idiotic design!
1367// We end up here when we call get_boot() recursively
1368// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1369// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1370// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1371// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001372 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001373 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374
Denis Vlasenko4437d192008-04-17 00:12:10 +00001375 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1376
Denis Vlasenkobd852072007-03-19 14:43:38 +00001377 if (fd < 0) {
1378 fd = open(disk_device, O_RDONLY);
1379 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001380 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001381 return 1;
1382 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001383 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001384 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001385 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001386 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001387 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001388 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001389 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001391 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001392 fdisk_fatal(unable_to_read);
1393 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001394#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001395 fd = open(disk_device, O_RDONLY);
1396 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001397 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001398 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001399 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001400 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001401 }
1402 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001403#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001405 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406 update_units();
1407
Denis Vlasenko834410a2006-11-29 12:00:28 +00001408#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409 if (check_sun_label())
1410 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001411#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001412#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413 if (check_sgi_label())
1414 return 0;
1415#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001416#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001417 if (check_aix_label())
1418 return 0;
1419#endif
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001420#if ENABLE_FEATURE_GPT_LABEL
1421 if (check_gpt_label())
1422 return 0;
1423#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001424#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001425 if (check_osf_label()) {
1426 possibly_osf_label = 1;
1427 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001428 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429 return 0;
1430 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001431 printf("This disk has both DOS and BSD magic.\n"
1432 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001433 }
1434#endif
1435
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001436#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001437 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001438 return -1;
1439#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001440 if (!valid_part_table_flag(MBRbuffer)) {
1441 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001442 printf("Device contains neither a valid DOS "
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001443 "partition table, nor Sun, SGI, OSF or GPT "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001444 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001445#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001446 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001447#else
1448 create_doslabel();
1449#endif
1450 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001451 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001452 /* TRY_ONLY: */
1453 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001454 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001455 created_table:
1456#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001457
Denis Vlasenko4437d192008-04-17 00:12:10 +00001458
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001459 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001460 warn_geometry();
1461
1462 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001463 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001464 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001465 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001466 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001467 else
1468 read_extended(i);
1469 }
1470 }
1471
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001472 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001473 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001475 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001476 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001477 pe->sectorbuffer[510],
1478 pe->sectorbuffer[511],
1479 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001480 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481 }
1482 }
1483
1484 return 0;
1485}
1486
Denis Vlasenko834410a2006-11-29 12:00:28 +00001487#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001488/*
1489 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1490 * If the user hits Enter, DFLT is returned.
1491 * Answers like +10 are interpreted as offsets from BASE.
1492 *
1493 * There is no default if DFLT is not between LOW and HIGH.
1494 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001495static sector_t
1496read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001497{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001498 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001499 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001500 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001502 if (dflt < low || dflt > high) {
1503 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001504 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001505 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001506
1507 while (1) {
1508 int use_default = default_ok;
1509
1510 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001511 do {
1512 printf(fmt, mesg, low, high, dflt);
1513 read_maybe_empty("");
1514 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1515 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001516
Eric Andersen84bdea82004-05-19 10:49:17 +00001517 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001518 int minus = (*line_ptr == '-');
1519 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001520
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001521 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001522
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001523 /* (1) if 2nd char is digit, use_default = 0.
1524 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001525 while (isdigit(*++line_ptr))
1526 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001527
Rob Landleyb73451d2006-02-24 16:29:00 +00001528 switch (*line_ptr) {
1529 case 'c':
1530 case 'C':
1531 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001532 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001533 break;
1534 case 'K':
1535 absolute = 1024;
1536 break;
1537 case 'k':
1538 absolute = 1000;
1539 break;
1540 case 'm':
1541 case 'M':
1542 absolute = 1000000;
1543 break;
1544 case 'g':
1545 case 'G':
1546 absolute = 1000000000;
1547 break;
1548 default:
1549 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001550 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001551 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001552 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001553 unsigned long unit;
1554
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001555 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001556 unit = sector_size * units_per_sector;
1557 bytes += unit/2; /* round */
1558 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001559 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001560 }
1561 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001562 value = -value;
1563 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001564 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001565 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001566 while (isdigit(*line_ptr)) {
1567 line_ptr++;
1568 use_default = 0;
1569 }
1570 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001571 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001572 value = dflt;
1573 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001574 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001575 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001576 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001577 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001578 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001579 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001580}
1581
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001582static unsigned
1583get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001584{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001586 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001587
Denis Vlasenkobd852072007-03-19 14:43:38 +00001588 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 pe = &ptes[i];
1590
1591 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001592 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1593 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1594 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1595 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001596 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001597 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598 }
1599 return i;
1600}
1601
1602static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001603get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001604{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001605 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001606 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001607
1608 for (i = 0; i < max; i++) {
1609 struct pte *pe = &ptes[i];
1610 struct partition *p = pe->part_table;
1611
1612 if (p && !is_cleared_partition(p)) {
1613 if (pno >= 0)
1614 goto not_unique;
1615 pno = i;
1616 }
1617 }
1618 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001619 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001620 return pno;
1621 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001622 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001623 return -1;
1624
1625 not_unique:
1626 return get_partition(warn, max);
1627}
1628
1629static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001630get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001631{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001632 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001633 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001634
1635 for (i = 0; i < max; i++) {
1636 struct pte *pe = &ptes[i];
1637 struct partition *p = pe->part_table;
1638
1639 if (p && is_cleared_partition(p)) {
1640 if (pno >= 0)
1641 goto not_unique;
1642 pno = i;
1643 }
1644 }
1645 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001646 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001647 return pno;
1648 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001649 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001650 return -1;
1651
1652 not_unique:
1653 return get_partition(warn, max);
1654}
1655
1656
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001657static void
1658change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001659{
1660 display_in_cyl_units = !display_in_cyl_units;
1661 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001662 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 str_units(PLURAL));
1664}
1665
1666static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001667toggle_active(int i)
1668{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001669 struct pte *pe = &ptes[i];
1670 struct partition *p = pe->part_table;
1671
Rob Landleyb73451d2006-02-24 16:29:00 +00001672 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001673 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001674 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1675 pe->changed = 1;
1676}
1677
1678static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001679toggle_dos_compatibility_flag(void)
1680{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001681 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001682 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001683 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001684 printf("DOS Compatibility flag is set\n");
1685 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001686 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001687 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001688 }
1689}
1690
1691static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001692delete_partition(int i)
1693{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001694 struct pte *pe = &ptes[i];
1695 struct partition *p = pe->part_table;
1696 struct partition *q = pe->ext_pointer;
1697
1698/* Note that for the fifth partition (i == 4) we don't actually
1699 * decrement partitions.
1700 */
1701
1702 if (warn_geometry())
1703 return; /* C/H/S not set */
1704 pe->changed = 1;
1705
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001706 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001707 sun_delete_partition(i);
1708 return;
1709 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001710 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001711 sgi_delete_partition(i);
1712 return;
1713 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001714
1715 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001716 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001717 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001718 ptes[ext_index].ext_pointer = NULL;
1719 extended_offset = 0;
1720 }
1721 clear_partition(p);
1722 return;
1723 }
1724
1725 if (!q->sys_ind && i > 4) {
1726 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001727 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001728 --i;
1729 clear_partition(ptes[i].ext_pointer);
1730 ptes[i].changed = 1;
1731 } else {
1732 /* not the last one - further ones will be moved down */
1733 if (i > 4) {
1734 /* delete this link in the chain */
1735 p = ptes[i-1].ext_pointer;
1736 *p = *q;
1737 set_start_sect(p, get_start_sect(q));
1738 set_nr_sects(p, get_nr_sects(q));
1739 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001740 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741 /* the first logical in a longer chain */
1742 pe = &ptes[5];
1743
1744 if (pe->part_table) /* prevent SEGFAULT */
1745 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001746 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001747 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001748 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749 pe->changed = 1;
1750 }
1751
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001752 if (g_partitions > 5) {
1753 g_partitions--;
1754 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001755 ptes[i] = ptes[i+1];
1756 i++;
1757 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001758 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 /* the only logical: clear only */
1760 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001761 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001762 }
1763}
1764
1765static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001766change_sysid(void)
1767{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001768 int i, sys, origsys;
1769 struct partition *p;
1770
Eric Andersen040f4402003-07-30 08:40:37 +00001771 /* If sgi_label then don't use get_existing_partition,
1772 let the user select a partition, since get_existing_partition()
1773 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001774 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001775 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001776 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001777 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001778 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001779 if (i == -1)
1780 return;
1781 p = ptes[i].part_table;
1782 origsys = sys = get_sysid(i);
1783
1784 /* if changing types T to 0 is allowed, then
1785 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001786 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001787 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001788 return;
1789 }
1790 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001791 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001792
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001793 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001794 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001795 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001796 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001797 /* break; */
1798 }
1799
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001800 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001801 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001802 printf("You cannot change a partition into"
1803 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804 break;
1805 }
1806 }
1807
1808 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001809#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001810 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001811 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001812 "as Whole disk (5),\n"
1813 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001814 "even Linux likes it\n\n");
1815#endif
1816#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001817 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001818 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001819 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001820 (i == 8 && sys != 0)
1821 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001822 ) {
1823 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001824 "as volume header (0),\nand "
1825 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001826 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001827 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001828#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829 if (sys == origsys)
1830 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001831 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001832 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001833 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001834 sgi_change_sysid(i, sys);
1835 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001836 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001837
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001838 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001839 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001840 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001841 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001842 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1843 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001844 break;
1845 }
1846 }
1847}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001848#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001849
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001850
Denis Vlasenko28703012006-12-19 20:32:02 +00001851/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001852 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1853 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1854 * Lubkin Oct. 1991). */
1855
Rob Landleyb73451d2006-02-24 16:29:00 +00001856static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001857linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001858{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001859 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001860
1861 *c = ls / spc;
1862 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001863 *h = ls / g_sectors;
1864 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001865}
1866
Rob Landleyb73451d2006-02-24 16:29:00 +00001867static void
1868check_consistency(const struct partition *p, int partition)
1869{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001870 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1871 unsigned pec, peh, pes; /* physical ending c, h, s */
1872 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1873 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001874
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001875 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001876 return; /* do not check extended partitions */
1877
1878/* physical beginning c, h, s */
1879 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1880 pbh = p->head;
1881 pbs = p->sector & 0x3f;
1882
1883/* physical ending c, h, s */
1884 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1885 peh = p->end_head;
1886 pes = p->end_sector & 0x3f;
1887
1888/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001889 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001890
1891/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001892 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001893
1894/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001895 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001896 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001897 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001898 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1899 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001900 }
1901
1902/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001903 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001904 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001905 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001906 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1907 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001908 }
1909
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001910/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001911 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001912 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001913 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001914 }
1915}
1916
1917static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001918list_disk_geometry(void)
1919{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001920 ullong bytes = ((ullong)total_number_of_sectors << 9);
1921 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922
1923 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001924 printf("\nDisk %s: %lu MB, %llu bytes\n",
1925 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001926 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001927 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1928 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1929 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001930 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001931 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001932 printf(", total %"SECT_FMT"u sectors",
1933 total_number_of_sectors / (sector_size/512));
1934 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1935 str_units(PLURAL),
1936 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001937}
1938
1939/*
1940 * Check whether partition entries are ordered by their starting positions.
1941 * Return 0 if OK. Return i if partition i should have been earlier.
1942 * Two separate checks: primary and logical partitions.
1943 */
1944static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001945wrong_p_order(int *prev)
1946{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001947 const struct pte *pe;
1948 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001949 sector_t last_p_start_pos = 0, p_start_pos;
1950 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001951
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001952 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001953 if (i == 4) {
1954 last_i = 4;
1955 last_p_start_pos = 0;
1956 }
1957 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001958 p = pe->part_table;
1959 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001960 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001961
1962 if (last_p_start_pos > p_start_pos) {
1963 if (prev)
1964 *prev = last_i;
1965 return i;
1966 }
1967
1968 last_p_start_pos = p_start_pos;
1969 last_i = i;
1970 }
1971 }
1972 return 0;
1973}
1974
Denis Vlasenko834410a2006-11-29 12:00:28 +00001975#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001976/*
1977 * Fix the chain of logicals.
1978 * extended_offset is unchanged, the set of sectors used is unchanged
1979 * The chain is sorted so that sectors increase, and so that
1980 * starting sectors increase.
1981 *
1982 * After this it may still be that cfdisk doesnt like the table.
1983 * (This is because cfdisk considers expanded parts, from link to
1984 * end of partition, and these may still overlap.)
1985 * Now
1986 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1987 * may help.
1988 */
1989static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001990fix_chain_of_logicals(void)
1991{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001992 int j, oj, ojj, sj, sjj;
1993 struct partition *pj,*pjj,tmp;
1994
1995 /* Stage 1: sort sectors but leave sector of part 4 */
1996 /* (Its sector is the global extended_offset.) */
1997 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001998 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001999 oj = ptes[j].offset_from_dev_start;
2000 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002001 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002002 ptes[j].offset_from_dev_start = ojj;
2003 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002004 pj = ptes[j].part_table;
2005 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2006 pjj = ptes[j+1].part_table;
2007 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2008 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002009 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002011 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012 goto stage1;
2013 }
2014 }
2015
2016 /* Stage 2: sort starting sectors */
2017 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002018 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002019 pj = ptes[j].part_table;
2020 pjj = ptes[j+1].part_table;
2021 sj = get_start_sect(pj);
2022 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002023 oj = ptes[j].offset_from_dev_start;
2024 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002025 if (oj+sj > ojj+sjj) {
2026 tmp = *pj;
2027 *pj = *pjj;
2028 *pjj = tmp;
2029 set_start_sect(pj, ojj+sjj-oj);
2030 set_start_sect(pjj, oj+sj-ojj);
2031 goto stage2;
2032 }
2033 }
2034
2035 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002036 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002037 ptes[j].changed = 1;
2038}
2039
2040
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002041static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002042fix_partition_table_order(void)
2043{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002044 struct pte *pei, *pek;
2045 int i,k;
2046
2047 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002048 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049 return;
2050 }
2051
2052 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2053 /* partition i should have come earlier, move it */
2054 /* We have to move data in the MBR */
2055 struct partition *pi, *pk, *pe, pbuf;
2056 pei = &ptes[i];
2057 pek = &ptes[k];
2058
2059 pe = pei->ext_pointer;
2060 pei->ext_pointer = pek->ext_pointer;
2061 pek->ext_pointer = pe;
2062
2063 pi = pei->part_table;
2064 pk = pek->part_table;
2065
2066 memmove(&pbuf, pi, sizeof(struct partition));
2067 memmove(pi, pk, sizeof(struct partition));
2068 memmove(pk, &pbuf, sizeof(struct partition));
2069
2070 pei->changed = pek->changed = 1;
2071 }
2072
2073 if (i)
2074 fix_chain_of_logicals();
2075
2076 printf("Done.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002077}
2078#endif
2079
2080static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002081list_table(int xtra)
2082{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002083 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002084 int i, w;
2085
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002086 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002087 sun_list_table(xtra);
2088 return;
2089 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002090 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002091 sgi_list_table(xtra);
2092 return;
2093 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002094 if (LABEL_IS_GPT) {
2095 gpt_list_table(xtra);
2096 return;
2097 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002098
2099 list_disk_geometry();
2100
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002101 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102 xbsd_print_disklabel(xtra);
2103 return;
2104 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002105
2106 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2107 but if the device name ends in a digit, say /dev/foo1,
2108 then the partition is called /dev/foo1p3. */
2109 w = strlen(disk_device);
2110 if (w && isdigit(disk_device[w-1]))
2111 w++;
2112 if (w < 5)
2113 w = 5;
2114
Denis Vlasenkobd852072007-03-19 14:43:38 +00002115 // 1 12345678901 12345678901 12345678901 12
2116 printf("%*s Boot Start End Blocks Id System\n",
2117 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002119 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002120 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002121 sector_t psects;
2122 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002123 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002124
2125 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002126 if (!p || is_cleared_partition(p))
2127 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002128
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002129 psects = get_nr_sects(p);
2130 pblocks = psects;
2131 podd = 0;
2132
2133 if (sector_size < 1024) {
2134 pblocks /= (1024 / sector_size);
2135 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002137 if (sector_size > 1024)
2138 pblocks *= (sector_size / 1024);
2139
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002140 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 +00002141 partname(disk_device, i+1, w+2),
2142 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2143 ? '*' : '?',
Denys Vlasenkod958e902010-04-06 02:32:26 +02002144 cround(get_partition_start_from_dev_start(pe)), /* start */
2145 cround(get_partition_start_from_dev_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002146 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002147 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002148 p->sys_ind, /* type id */
2149 partition_type(p->sys_ind)); /* type name */
2150
2151 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152 }
2153
2154 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002155 /* partition table entries are not checked for correct order
2156 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002157 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002158 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002159 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 }
2161}
2162
Denis Vlasenko834410a2006-11-29 12:00:28 +00002163#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002165x_list_table(int extend)
2166{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002167 const struct pte *pe;
2168 const struct partition *p;
2169 int i;
2170
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002171 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002172 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002173 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002174 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175 pe = &ptes[i];
2176 p = (extend ? pe->ext_pointer : pe->part_table);
2177 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002178 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 +00002179 i + 1, p->boot_ind, p->head,
2180 sector(p->sector),
2181 cylinder(p->sector, p->cyl), p->end_head,
2182 sector(p->end_sector),
2183 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002184 get_start_sect(p), get_nr_sects(p),
2185 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002186 if (p->sys_ind)
2187 check_consistency(p, i);
2188 }
2189 }
2190}
2191#endif
2192
Denis Vlasenko834410a2006-11-29 12:00:28 +00002193#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002194static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002195fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002196{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002197 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002198 const struct pte *pe = &ptes[0];
2199 const struct partition *p;
2200
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002201 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002203 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002204 first[i] = 0xffffffff;
2205 last[i] = 0;
2206 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002207 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 last[i] = first[i] + get_nr_sects(p) - 1;
2209 }
2210 }
2211}
2212
2213static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002214check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002215{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002216 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217
2218 real_s = sector(s) - 1;
2219 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002220 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002222 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002223 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002224 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002225 n, h + 1, g_heads);
2226 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002227 printf("Partition %u: sector %u greater than "
2228 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002229 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002230 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2231 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002232 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002233 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2234 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002235}
2236
2237static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002238verify(void)
2239{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002240 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002241 sector_t total = 1;
2242 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002243 struct partition *p;
2244
2245 if (warn_geometry())
2246 return;
2247
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002248 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002249 verify_sun();
2250 return;
2251 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002252 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 verify_sgi(1);
2254 return;
2255 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002256
2257 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002258 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002259 struct pte *pe = &ptes[i];
2260
2261 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002262 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002263 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002264 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002265 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002266 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002267 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2268 last[i]);
2269 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002270 for (j = 0; j < i; j++) {
2271 if ((first[i] >= first[j] && first[i] <= last[j])
2272 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002273 printf("Warning: partition %u overlaps "
2274 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002275 total += first[i] >= first[j] ?
2276 first[i] : first[j];
2277 total -= last[i] <= last[j] ?
2278 last[i] : last[j];
2279 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002280 }
2281 }
2282 }
2283
2284 if (extended_offset) {
2285 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002286 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 get_nr_sects(pex->part_table) - 1;
2288
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002289 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002290 total++;
2291 p = ptes[i].part_table;
2292 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002293 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002294 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002295 "is empty\n", i + 1);
2296 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002297 printf("Logical partition %u not entirely in "
2298 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002300 }
2301 }
2302
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002303 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002304 printf("Total allocated sectors %u greater than the maximum "
2305 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002306 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002307 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002308 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002309 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002310 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002311}
2312
2313static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002314add_partition(int n, int sys)
2315{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002316 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002317 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002318 struct partition *p = ptes[n].part_table;
2319 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002320 sector_t limit, temp;
2321 sector_t start, stop = 0;
2322 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323
2324 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002325 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002326 return;
2327 }
2328 fill_bounds(first, last);
2329 if (n < 4) {
2330 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002331 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002332 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002333 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002334 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 if (extended_offset) {
2336 first[ext_index] = extended_offset;
2337 last[ext_index] = get_start_sect(q) +
2338 get_nr_sects(q) - 1;
2339 }
2340 } else {
2341 start = extended_offset + sector_offset;
2342 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2343 }
2344 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002345 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002346 first[i] = (cround(first[i]) - 1) * units_per_sector;
2347
Denis Vlasenkobd852072007-03-19 14:43:38 +00002348 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002349 do {
2350 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002351 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002352 int lastplusoff;
2353
Denys Vlasenkod958e902010-04-06 02:32:26 +02002354 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002355 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002356 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002357 if (start >= first[i] && start <= lastplusoff)
2358 start = lastplusoff + 1;
2359 }
2360 if (start > limit)
2361 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002362 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002363 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002365 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002366 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002367 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002368 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369
2370 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002371 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 if (display_in_cyl_units) {
2373 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002374 if (start < saved_start)
2375 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002377 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002379 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002380 if (n > 4) { /* NOT for fifth partition */
2381 struct pte *pe = &ptes[n];
2382
Denys Vlasenkod958e902010-04-06 02:32:26 +02002383 pe->offset_from_dev_start = start - sector_offset;
2384 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2385 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 if (sector_offset == 1)
2387 start++;
2388 }
2389 }
2390
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002391 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002392 struct pte *pe = &ptes[i];
2393
Denys Vlasenkod958e902010-04-06 02:32:26 +02002394 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2395 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002396 if (start < first[i] && limit >= first[i])
2397 limit = first[i] - 1;
2398 }
2399 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002400 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002402 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403 return;
2404 }
2405 if (cround(start) == cround(limit)) {
2406 stop = limit;
2407 } else {
2408 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002409 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 str_units(SINGULAR));
Denys Vlasenkod958e902010-04-06 02:32:26 +02002411 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412 if (display_in_cyl_units) {
2413 stop = stop * units_per_sector - 1;
2414 if (stop >limit)
2415 stop = limit;
2416 }
2417 }
2418
2419 set_partition(n, 0, start, stop, sys);
2420 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002421 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002422
Rob Landleyb73451d2006-02-24 16:29:00 +00002423 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002424 struct pte *pe4 = &ptes[4];
2425 struct pte *pen = &ptes[n];
2426
2427 ext_index = n;
2428 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002429 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002430 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002431 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2432 pe4->ext_pointer = pe4->part_table + 1;
2433 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002434 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002435 }
2436}
2437
2438static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002439add_logical(void)
2440{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002441 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2442 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443
Rob Landley081e3842006-08-03 20:07:35 +00002444 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002445 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2446 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002447 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002448 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002449 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002450 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002451 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452}
2453
2454static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002455new_partition(void)
2456{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 int i, free_primary = 0;
2458
2459 if (warn_geometry())
2460 return;
2461
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002462 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002463 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464 return;
2465 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002466 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002467 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468 return;
2469 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002470 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002471 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2472"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2473"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474 return;
2475 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002477 for (i = 0; i < 4; i++)
2478 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002479
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002480 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002481 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002482 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002483 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002484
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485 if (!free_primary) {
2486 if (extended_offset)
2487 add_logical();
2488 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002489 printf("You must delete some partition and add "
2490 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002492 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002493 snprintf(line, sizeof(line),
2494 "Command action\n"
2495 " %s\n"
2496 " p primary partition (1-4)\n",
2497 (extended_offset ?
2498 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002499 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002500 c = read_nonempty(line);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002501 if ((c | 0x20) == 'p') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 i = get_nonexisting_partition(0, 4);
2503 if (i >= 0)
2504 add_partition(i, LINUX_NATIVE);
2505 return;
2506 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002507 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002508 add_logical();
2509 return;
2510 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002511 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 i = get_nonexisting_partition(0, 4);
2513 if (i >= 0)
2514 add_partition(i, EXTENDED);
2515 return;
2516 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002517 printf("Invalid partition number "
2518 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 }
2520 }
2521}
2522
2523static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002524write_table(void)
2525{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 int i;
2527
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002528 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002529 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530 if (ptes[i].changed)
2531 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002532 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002533 struct pte *pe = &ptes[i];
2534
2535 if (pe->changed) {
2536 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002537 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538 }
2539 }
2540 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002541 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542 /* no test on change? the printf below might be mistaken */
2543 sgi_write_table();
2544 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002545 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546 int needw = 0;
2547
Rob Landleyb73451d2006-02-24 16:29:00 +00002548 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002549 if (ptes[i].changed)
2550 needw = 1;
2551 if (needw)
2552 sun_write_table();
2553 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554
Denis Vlasenkobd852072007-03-19 14:43:38 +00002555 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556 reread_partition_table(1);
2557}
2558
Rob Landleyb73451d2006-02-24 16:29:00 +00002559static void
2560reread_partition_table(int leave)
2561{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 int i;
2563
Denis Vlasenkobd852072007-03-19 14:43:38 +00002564 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002566 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002567 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002568 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002569 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002570#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002571 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002572 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002573 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002575 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002576#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577
2578 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002579 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002580 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002581 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582 }
2583}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002584#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585
Denis Vlasenko834410a2006-11-29 12:00:28 +00002586#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587#define MAX_PER_LINE 16
2588static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002589print_buffer(char *pbuffer)
2590{
2591 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592
2593 for (i = 0, l = 0; i < sector_size; i++, l++) {
2594 if (l == 0)
2595 printf("0x%03X:", i);
2596 printf(" %02X", (unsigned char) pbuffer[i]);
2597 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002598 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 l = -1;
2600 }
2601 }
2602 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002603 bb_putchar('\n');
2604 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002605}
2606
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002607static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002608print_raw(void)
2609{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 int i;
2611
Denis Vlasenkobd852072007-03-19 14:43:38 +00002612 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002613 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002615 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002616 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002618 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619}
2620
2621static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002622move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002623{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002624 struct pte *pe = &ptes[i];
2625 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002626 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627
2628 if (warn_geometry())
2629 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002630 nr_sects = get_nr_sects(p);
2631 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002632 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 return;
2634 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002635 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002636 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2637 if (new != first) {
2638 sector_t new_relative = new - pe->offset_from_dev_start;
2639 nr_sects += (get_start_sect(p) - new_relative);
2640 set_start_sect(p, new_relative);
2641 set_nr_sects(p, nr_sects);
2642 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2643 if ((line_ptr[0] | 0x20) == 'y')
2644 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 pe->changed = 1;
2646 }
2647}
2648
2649static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002650xselect(void)
2651{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002652 char c;
2653
Rob Landleyb73451d2006-02-24 16:29:00 +00002654 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002655 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002656 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002657 switch (c) {
2658 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002659 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002660 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002661 break;
2662 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002663 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002664 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002665 break;
2666 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002667 user_cylinders = g_cylinders =
2668 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002669 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002670 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002671 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002672 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002673 warn_cylinders();
2674 break;
2675 case 'd':
2676 print_raw();
2677 break;
2678 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002679 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002680 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002681 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002682 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002683 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002684 x_list_table(1);
2685 break;
2686 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002687 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 fix_partition_table_order();
2689 break;
2690 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002691#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 create_sgilabel();
2693#endif
2694 break;
2695 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002696 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002697 update_units();
2698 break;
2699 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002700 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002701 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002702 break;
2703 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002704 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002705 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706 break;
2707 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002708 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 list_table(1);
2710 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002711 x_list_table(0);
2712 break;
2713 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002714 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002715 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002716 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002717 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002718 case 'r':
2719 return;
2720 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002721 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002722 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002723 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002724 printf("Warning: setting sector offset for DOS "
2725 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002726 }
2727 update_units();
2728 break;
2729 case 'v':
2730 verify();
2731 break;
2732 case 'w':
2733 write_table(); /* does not return */
2734 break;
2735 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002736 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002737 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 break;
2739 default:
2740 xmenu();
2741 }
2742 }
2743}
2744#endif /* ADVANCED mode */
2745
2746static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002747is_ide_cdrom_or_tape(const char *device)
2748{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002749 FILE *procf;
2750 char buf[100];
2751 struct stat statbuf;
2752 int is_ide = 0;
2753
2754 /* No device was given explicitly, and we are trying some
2755 likely things. But opening /dev/hdc may produce errors like
2756 "hdc: tray open or drive not ready"
2757 if it happens to be a CD-ROM drive. It even happens that
2758 the process hangs on the attempt to read a music CD.
2759 So try to be careful. This only works since 2.1.73. */
2760
2761 if (strncmp("/dev/hd", device, 7))
2762 return 0;
2763
2764 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002765 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002766 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2767 is_ide = (!strncmp(buf, "cdrom", 5) ||
2768 !strncmp(buf, "tape", 4));
2769 else
2770 /* Now when this proc file does not exist, skip the
2771 device when it is read-only. */
2772 if (stat(device, &statbuf) == 0)
2773 is_ide = ((statbuf.st_mode & 0222) == 0);
2774
2775 if (procf)
2776 fclose(procf);
2777 return is_ide;
2778}
2779
Rob Landley5527b912006-02-25 03:46:10 +00002780
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002781static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002782open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002783{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784 int gb;
2785
2786 disk_device = device;
2787 if (setjmp(listingbuf))
2788 return;
2789 if (!user_specified)
2790 if (is_ide_cdrom_or_tape(device))
2791 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002792
2793 /* Open disk_device, save file descriptor to dev_fd */
2794 errno = 0;
2795 gb = get_boot(TRY_ONLY);
2796 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002797 /* Ignore other errors, since we try IDE
2798 and SCSI hard disks which may not be
2799 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002800 if (user_specified || errno == EACCES)
2801 bb_perror_msg("can't open '%s'", device);
2802 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002803 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002804
2805 if (gb < 0) { /* no DOS signature */
2806 list_disk_geometry();
2807 if (LABEL_IS_AIX)
2808 goto ret;
2809#if ENABLE_FEATURE_OSF_LABEL
2810 if (bsd_trydev(device) < 0)
2811#endif
2812 printf("Disk %s doesn't contain a valid "
2813 "partition table\n", device);
2814 } else {
2815 list_table(0);
2816#if ENABLE_FEATURE_FDISK_WRITABLE
2817 if (!LABEL_IS_SUN && g_partitions > 4) {
2818 delete_partition(ext_index);
2819 }
2820#endif
2821 }
2822 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002823 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824}
2825
2826/* for fdisk -l: try all things in /proc/partitions
2827 that look like a partition name (do not end in a digit) */
2828static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002829list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002830{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 FILE *procpt;
2832 char line[100], ptname[100], devname[120], *s;
2833 int ma, mi, sz;
2834
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002835 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002836
2837 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002838 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002839 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002840 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002841 for (s = ptname; *s; s++)
Denys Vlasenko8f65b0c2010-07-06 18:46:02 +02002842 continue;
Denys Vlasenkobf1d3472010-02-24 08:13:30 +01002843 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2844 if (s[-1] >= '1' && s[-1] <= '9')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002845 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002846 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002847 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002848 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002849#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002851#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002852}
2853
Denis Vlasenko834410a2006-11-29 12:00:28 +00002854#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002856unknown_command(int c)
2857{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002858 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002860#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002862int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002863int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002864{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002865 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002866 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 * fdisk -v
2868 * fdisk -l [-b sectorsize] [-u] device ...
2869 * fdisk -s [partition] ...
2870 * fdisk [-b sectorsize] [-u] device
2871 *
2872 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002873 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002874 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002875
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002876 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002877
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002878 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002879 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002880 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002881 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002882 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00002883 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02002884 * so cannot be combined with multiple disks,
2885 * and the same goes for the C/H/S options.
2886 */
2887 if (sector_size < 512
2888 || sector_size > 0x10000
2889 || (sector_size & (sector_size-1)) /* not power of 2 */
2890 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002891 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02002892 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002893 sector_offset = 2;
2894 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002896 if (user_heads <= 0 || user_heads >= 256)
2897 user_heads = 0;
2898 if (user_sectors <= 0 || user_sectors >= 64)
2899 user_sectors = 0;
2900 if (opt & OPT_u)
2901 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902
Denis Vlasenko834410a2006-11-29 12:00:28 +00002903#if ENABLE_FEATURE_FDISK_WRITABLE
2904 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002905 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002906#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002907 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002908 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002909 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002910 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002911 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002912 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002913 /* we don't have device names, */
2914 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002915 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002916 }
2917 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002918#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002919 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002920#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002921
Denis Vlasenko834410a2006-11-29 12:00:28 +00002922#if ENABLE_FEATURE_FDISK_BLKSIZE
2923 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002924 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925
2926 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002927 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002928 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002929 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002930 unsigned long long size;
2931 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002932 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002934 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002935 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002937 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 }
2939 return 0;
2940 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002941#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942
Denis Vlasenko834410a2006-11-29 12:00:28 +00002943#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002944 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002945 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002946
Denis Vlasenko834410a2006-11-29 12:00:28 +00002947 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002948 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002949
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002950 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002952 printf("Detected an OSF/1 disklabel on %s, entering "
2953 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002954 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002955 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002956 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002957 /* If we return we may want to make an empty DOS label? */
2958 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959
2960 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002961 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002962 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002963 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002964 switch (c) {
2965 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002966 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002967 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002968 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002969 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002970 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002971 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002972 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002973 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002974 else
2975 unknown_command(c);
2976 break;
2977 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002978 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002979 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002980 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002981 if (read_maybe_empty("Please enter the name of the "
2982 "new boot file: ") == '\n')
2983 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 else
2985 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002986 }
2987#if ENABLE_FEATURE_OSF_LABEL
2988 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002989 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990#endif
2991 break;
2992 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002993 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002994 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002995 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002996 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002997 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002998 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003000 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003001 else
3002 unknown_command(c);
3003 break;
3004 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003005 {
Eric Andersen040f4402003-07-30 08:40:37 +00003006 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00003007 /* If sgi_label then don't use get_existing_partition,
3008 let the user select a partition, since
3009 get_existing_partition() only works for Linux-like
3010 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003011 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003012 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003013 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003014 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003015 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003016 if (j >= 0)
3017 delete_partition(j);
3018 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003019 break;
3020 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003021 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003022 create_sgiinfo();
3023 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003024 unknown_command(c);
3025 case 'l':
3026 list_types(get_sys_types());
3027 break;
3028 case 'm':
3029 menu();
3030 break;
3031 case 'n':
3032 new_partition();
3033 break;
3034 case 'o':
3035 create_doslabel();
3036 break;
3037 case 'p':
3038 list_table(0);
3039 break;
3040 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003041 if (ENABLE_FEATURE_CLEAN_UP)
3042 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003043 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003044 return 0;
3045 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003046#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003047 create_sunlabel();
3048#endif
3049 break;
3050 case 't':
3051 change_sysid();
3052 break;
3053 case 'u':
3054 change_units();
3055 break;
3056 case 'v':
3057 verify();
3058 break;
3059 case 'w':
3060 write_table(); /* does not return */
3061 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003062#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003063 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003064 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003065 printf("\n\tSorry, no experts menu for SGI "
3066 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003067 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003068 xselect();
3069 break;
3070#endif
3071 default:
3072 unknown_command(c);
3073 menu();
3074 }
3075 }
3076 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003077#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003078}