blob: 02785ab85858ea596e4078509d7ec13f7137c95d [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
Denys Vlasenkod7559c22010-10-30 02:40:08 +0200665STATIC_GPT void gpt_list_table(int xtra);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200666#include "fdisk_gpt.c"
667
Denis Vlasenko28703012006-12-19 20:32:02 +0000668#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000669static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000670fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000671{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000672 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000673}
674
Rob Landley88621d72006-08-29 19:41:06 +0000675static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000676fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000677{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000678 return (x << 24) |
679 ((x & 0xFF00) << 8) |
680 ((x & 0xFF0000) >> 8) |
681 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000682}
683#endif
684
Denis Vlasenkobd852072007-03-19 14:43:38 +0000685STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000686STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000687STATIC_SGI int sgi_get_sysid(int i);
688STATIC_SGI void sgi_delete_partition(int i);
689STATIC_SGI void sgi_change_sysid(int i, int sys);
690STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000691#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000692STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000693#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000694STATIC_SGI int verify_sgi(int verbose);
695STATIC_SGI void sgi_add_partition(int n, int sys);
696STATIC_SGI void sgi_set_swappartition(int i);
697STATIC_SGI const char *sgi_get_bootfile(void);
698STATIC_SGI void sgi_set_bootfile(const char* aFile);
699STATIC_SGI void create_sgiinfo(void);
700STATIC_SGI void sgi_write_table(void);
701STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000702#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000703
Denis Vlasenkobd852072007-03-19 14:43:38 +0000704STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000705STATIC_SUN void sun_delete_partition(int i);
706STATIC_SUN void sun_change_sysid(int i, int sys);
707STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000708STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000709#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000710STATIC_SUN void sun_set_alt_cyl(void);
711STATIC_SUN void sun_set_ncyl(int cyl);
712STATIC_SUN void sun_set_xcyl(void);
713STATIC_SUN void sun_set_ilfact(void);
714STATIC_SUN void sun_set_rspeed(void);
715STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000716#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000717STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
718STATIC_SUN void verify_sun(void);
719STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000720#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000721
Denis Vlasenko9764d692008-07-09 21:20:50 +0000722
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200723static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000724read4_little_endian(const unsigned char *cp)
725{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200726 uint32_t v;
727 move_from_unaligned32(v, cp);
728 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000729}
730
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200731static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000732get_start_sect(const struct partition *p)
733{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000734 return read4_little_endian(p->start4);
735}
736
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200737static sector_t
738get_nr_sects(const struct partition *p)
739{
740 return read4_little_endian(p->size4);
741}
742
Denis Vlasenko834410a2006-11-29 12:00:28 +0000743#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200744/* start_sect and nr_sects are stored little endian on all machines */
745/* moreover, they are not aligned correctly */
746static inline_if_little_endian void
747store4_little_endian(unsigned char *cp, unsigned val)
748{
749 uint32_t v = SWAP_LE32(val);
750 move_to_unaligned32(cp, v);
751}
752
753static void
754set_start_sect(struct partition *p, unsigned start_sect)
755{
756 store4_little_endian(p->start4, start_sect);
757}
758
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000760set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000761{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000762 store4_little_endian(p->size4, nr_sects);
763}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000764#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000765
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000766/* Allocate a buffer and read a partition table sector */
767static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200768read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000769{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200770 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000771 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000772 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000773 /* xread would make us abort - bad for fdisk -l */
774 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000775 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000776#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000777 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000778#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000779 pe->part_table = pe->ext_pointer = NULL;
780}
781
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200782static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200783get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000784{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200785 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000786}
787
Denis Vlasenko834410a2006-11-29 12:00:28 +0000788#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000789/*
790 * Avoid warning about DOS partitions when no DOS partition was changed.
791 * Here a heuristic "is probably dos partition".
792 * We might also do the opposite and warn in all cases except
793 * for "is probably nondos partition".
794 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000795#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000796static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000797is_dos_partition(int t)
798{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000799 return (t == 1 || t == 4 || t == 6 ||
800 t == 0x0b || t == 0x0c || t == 0x0e ||
801 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
802 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
803 t == 0xc1 || t == 0xc4 || t == 0xc6);
804}
Denis Vlasenko89398812008-01-25 20:18:46 +0000805#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000806
807static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000808menu(void)
809{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000810 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000811 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000812 puts("a\ttoggle a read only flag"); /* sun */
813 puts("b\tedit bsd disklabel");
814 puts("c\ttoggle the mountable flag"); /* sun */
815 puts("d\tdelete a partition");
816 puts("l\tlist known partition types");
817 puts("n\tadd a new partition");
818 puts("o\tcreate a new empty DOS partition table");
819 puts("p\tprint the partition table");
820 puts("q\tquit without saving changes");
821 puts("s\tcreate a new empty Sun disklabel"); /* sun */
822 puts("t\tchange a partition's system id");
823 puts("u\tchange display/entry units");
824 puts("v\tverify the partition table");
825 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000826#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000827 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000828#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000829 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000830 puts("a\tselect bootable partition"); /* sgi flavour */
831 puts("b\tedit bootfile entry"); /* sgi */
832 puts("c\tselect sgi swap partition"); /* sgi flavour */
833 puts("d\tdelete a partition");
834 puts("l\tlist known partition types");
835 puts("n\tadd a new partition");
836 puts("o\tcreate a new empty DOS partition table");
837 puts("p\tprint the partition table");
838 puts("q\tquit without saving changes");
839 puts("s\tcreate a new empty Sun disklabel"); /* sun */
840 puts("t\tchange a partition's system id");
841 puts("u\tchange display/entry units");
842 puts("v\tverify the partition table");
843 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000844 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000845 puts("o\tcreate a new empty DOS partition table");
846 puts("q\tquit without saving changes");
847 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200848 } else if (LABEL_IS_GPT) {
849 puts("o\tcreate a new empty DOS partition table");
850 puts("p\tprint the partition table");
851 puts("q\tquit without saving changes");
852 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000853 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000854 puts("a\ttoggle a bootable flag");
855 puts("b\tedit bsd disklabel");
856 puts("c\ttoggle the dos compatibility flag");
857 puts("d\tdelete a partition");
858 puts("l\tlist known partition types");
859 puts("n\tadd a new partition");
860 puts("o\tcreate a new empty DOS partition table");
861 puts("p\tprint the partition table");
862 puts("q\tquit without saving changes");
863 puts("s\tcreate a new empty Sun disklabel"); /* sun */
864 puts("t\tchange a partition's system id");
865 puts("u\tchange display/entry units");
866 puts("v\tverify the partition table");
867 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000868#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000869 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000870#endif
871 }
872}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000873#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000874
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000875
Denis Vlasenko834410a2006-11-29 12:00:28 +0000876#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000877static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000878xmenu(void)
879{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000880 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000881 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000882 puts("a\tchange number of alternate cylinders"); /*sun*/
883 puts("c\tchange number of cylinders");
884 puts("d\tprint the raw data in the partition table");
885 puts("e\tchange number of extra sectors per cylinder");/*sun*/
886 puts("h\tchange number of heads");
887 puts("i\tchange interleave factor"); /*sun*/
888 puts("o\tchange rotation speed (rpm)"); /*sun*/
889 puts("p\tprint the partition table");
890 puts("q\tquit without saving changes");
891 puts("r\treturn to main menu");
892 puts("s\tchange number of sectors/track");
893 puts("v\tverify the partition table");
894 puts("w\twrite table to disk and exit");
895 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000896 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000897 puts("b\tmove beginning of data in a partition"); /* !sun */
898 puts("c\tchange number of cylinders");
899 puts("d\tprint the raw data in the partition table");
900 puts("e\tlist extended partitions"); /* !sun */
901 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
902 puts("h\tchange number of heads");
903 puts("p\tprint the partition table");
904 puts("q\tquit without saving changes");
905 puts("r\treturn to main menu");
906 puts("s\tchange number of sectors/track");
907 puts("v\tverify the partition table");
908 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000909 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000910 puts("b\tmove beginning of data in a partition"); /* !sun */
911 puts("c\tchange number of cylinders");
912 puts("d\tprint the raw data in the partition table");
913 puts("e\tlist extended partitions"); /* !sun */
914 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
915 puts("h\tchange number of heads");
916 puts("p\tprint the partition table");
917 puts("q\tquit without saving changes");
918 puts("r\treturn to main menu");
919 puts("s\tchange number of sectors/track");
920 puts("v\tverify the partition table");
921 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000922 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000923 puts("b\tmove beginning of data in a partition"); /* !sun */
924 puts("c\tchange number of cylinders");
925 puts("d\tprint the raw data in the partition table");
926 puts("e\tlist extended partitions"); /* !sun */
927 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000928#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000929 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000930#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000931 puts("h\tchange number of heads");
932 puts("p\tprint the partition table");
933 puts("q\tquit without saving changes");
934 puts("r\treturn to main menu");
935 puts("s\tchange number of sectors/track");
936 puts("v\tverify the partition table");
937 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938 }
939}
940#endif /* ADVANCED mode */
941
Denis Vlasenko834410a2006-11-29 12:00:28 +0000942#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000943static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000944get_sys_types(void)
945{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000946 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000947 LABEL_IS_SUN ? sun_sys_types :
948 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949 i386_sys_types);
950}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000951#else
952#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200953#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000954
Denis Vlasenkobd852072007-03-19 14:43:38 +0000955static const char *
956partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000957{
958 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000959 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000960
Denis Vlasenkobd852072007-03-19 14:43:38 +0000961 for (i = 0; types[i]; i++)
962 if ((unsigned char)types[i][0] == type)
963 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000964
Denis Vlasenkobd852072007-03-19 14:43:38 +0000965 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000966}
967
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200968static int
969is_cleared_partition(const struct partition *p)
970{
971 /* We consider partition "cleared" only if it has only zeros */
972 const char *cp = (const char *)p;
973 int cnt = sizeof(*p);
974 char bits = 0;
975 while (--cnt >= 0)
976 bits |= *cp++;
977 return (bits == 0);
978}
979
980static void
981clear_partition(struct partition *p)
982{
983 if (p)
984 memset(p, 0, sizeof(*p));
985}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000986
Denis Vlasenko834410a2006-11-29 12:00:28 +0000987#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000988static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000989get_sysid(int i)
990{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000991 return LABEL_IS_SUN ? sunlabel->infos[i].id :
992 (LABEL_IS_SGI ? sgi_get_sysid(i) :
993 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994}
995
Denis Vlasenkobd852072007-03-19 14:43:38 +0000996static void
997list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000998{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000999 enum { COLS = 3 };
1000
1001 unsigned last[COLS];
1002 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001003 int i;
1004
Denis Vlasenko9764d692008-07-09 21:20:50 +00001005 for (size = 0; sys[size]; size++)
1006 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001007
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001008 done = 0;
1009 for (i = COLS-1; i >= 0; i--) {
1010 done += (size + i - done) / (i + 1);
1011 last[COLS-1 - i] = done;
1012 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001014 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001015 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001016 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001017 (unsigned char)sys[next][0],
1018 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001020 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021 i = 0;
1022 next = ++done;
1023 }
1024 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001025 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026}
1027
Denys Vlasenkod958e902010-04-06 02:32:26 +02001028#define set_hsc(h, s, c, sector) do \
1029{ \
1030 s = sector % g_sectors + 1; \
1031 sector /= g_sectors; \
1032 h = sector % g_heads; \
1033 sector /= g_heads; \
1034 c = sector & 0xff; \
1035 s |= (sector >> 2) & 0xc0; \
1036} while (0)
1037
1038static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1039{
1040 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1041 start = g_heads * g_sectors * 1024 - 1;
1042 set_hsc(p->head, p->sector, p->cyl, start);
1043
1044 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1045 stop = g_heads * g_sectors * 1024 - 1;
1046 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1047}
1048
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001049static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001050set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001051{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001052 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001053 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054
1055 if (doext) {
1056 p = ptes[i].ext_pointer;
1057 offset = extended_offset;
1058 } else {
1059 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001060 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001061 }
1062 p->boot_ind = 0;
1063 p->sys_ind = sysid;
1064 set_start_sect(p, start - offset);
1065 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001066 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001067 ptes[i].changed = 1;
1068}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001069#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001070
1071static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001072warn_geometry(void)
1073{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001074 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001076
Denis Vlasenkobd852072007-03-19 14:43:38 +00001077 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001078 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001079 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001080 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001081 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001082 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001083 printf(" cylinders");
1084 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001085#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001086 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001087#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001088 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001089 return 1;
1090}
1091
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001092static void
1093update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001095 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001096
1097 if (display_in_cyl_units && cyl_units)
1098 units_per_sector = cyl_units;
1099 else
1100 units_per_sector = 1; /* in sectors */
1101}
1102
Denis Vlasenko834410a2006-11-29 12:00:28 +00001103#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001104static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001105warn_cylinders(void)
1106{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001107 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001108 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001109"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001110"There is nothing wrong with that, but this is larger than 1024,\n"
1111"and could in certain setups cause problems with:\n"
1112"1) software that runs at boot time (e.g., old versions of LILO)\n"
1113"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001114" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001115 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001117#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001118
1119static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001120read_extended(int ext)
1121{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122 int i;
1123 struct pte *pex;
1124 struct partition *p, *q;
1125
1126 ext_index = ext;
1127 pex = &ptes[ext];
1128 pex->ext_pointer = pex->part_table;
1129
1130 p = pex->part_table;
1131 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001132 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001133 return;
1134 }
1135
Rob Landleyb73451d2006-02-24 16:29:00 +00001136 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001137 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001138
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001139 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001140 /* This is not a Linux restriction, but
1141 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001142 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001143 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001144#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001145 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001146 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001147 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001148#endif
1149 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001150 return;
1151 }
1152
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001153 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001154
1155 if (!extended_offset)
1156 extended_offset = get_start_sect(p);
1157
1158 q = p = pt_offset(pe->sectorbuffer, 0);
1159 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001160 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001161 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001162 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001163 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001164 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001165 else
1166 pe->ext_pointer = p;
1167 } else if (p->sys_ind) {
1168 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001169 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001170 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001171 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 else
1173 pe->part_table = p;
1174 }
1175 }
1176
1177 /* very strange code here... */
1178 if (!pe->part_table) {
1179 if (q != pe->ext_pointer)
1180 pe->part_table = q;
1181 else
1182 pe->part_table = q + 1;
1183 }
1184 if (!pe->ext_pointer) {
1185 if (q != pe->part_table)
1186 pe->ext_pointer = q;
1187 else
1188 pe->ext_pointer = q + 1;
1189 }
1190
1191 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001192 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001193 }
1194
Denis Vlasenko834410a2006-11-29 12:00:28 +00001195#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001196 /* remove empty links */
1197 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001198 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001199 struct pte *pe = &ptes[i];
1200
Denis Vlasenkobd852072007-03-19 14:43:38 +00001201 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001202 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001203 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001204 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001205 delete_partition(i);
1206 goto remove; /* numbering changed */
1207 }
1208 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001209#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210}
1211
Denis Vlasenko834410a2006-11-29 12:00:28 +00001212#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001214create_doslabel(void)
1215{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001216 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001217
Denis Vlasenko4437d192008-04-17 00:12:10 +00001218 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001219#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001220 possibly_osf_label = 0;
1221#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001222 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001223
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001224 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001225 write_part_table_flag(MBRbuffer);
1226 extended_offset = 0;
1227 set_all_unchanged();
1228 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001229 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001230}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001231#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001232
1233static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001234get_sectorsize(void)
1235{
Rob Landley736e5252006-02-25 03:36:00 +00001236 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001237 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001238 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001239 sector_size = arg;
1240 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001241 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001242 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1243 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001244 }
1245}
1246
Rob Landley88621d72006-08-29 19:41:06 +00001247static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001248get_kernel_geometry(void)
1249{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250 struct hd_geometry geometry;
1251
Denis Vlasenko4437d192008-04-17 00:12:10 +00001252 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253 kern_heads = geometry.heads;
1254 kern_sectors = geometry.sectors;
1255 /* never use geometry.cylinders - it is truncated */
1256 }
1257}
1258
1259static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001260get_partition_table_geometry(void)
1261{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001262 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001263 struct partition *p;
1264 int i, h, s, hh, ss;
1265 int first = 1;
1266 int bad = 0;
1267
Eric Andersen3496fdc2006-01-30 23:09:20 +00001268 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001269 return;
1270
1271 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001272 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001273 p = pt_offset(bufp, i);
1274 if (p->sys_ind != 0) {
1275 h = p->end_head + 1;
1276 s = (p->end_sector & 077);
1277 if (first) {
1278 hh = h;
1279 ss = s;
1280 first = 0;
1281 } else if (hh != h || ss != s)
1282 bad = 1;
1283 }
1284 }
1285
1286 if (!first && !bad) {
1287 pt_heads = hh;
1288 pt_sectors = ss;
1289 }
1290}
1291
Rob Landleyb73451d2006-02-24 16:29:00 +00001292static void
1293get_geometry(void)
1294{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296
1297 get_sectorsize();
1298 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001299#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 guess_device_type();
1301#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001302 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303 kern_heads = kern_sectors = 0;
1304 pt_heads = pt_sectors = 0;
1305
1306 get_kernel_geometry();
1307 get_partition_table_geometry();
1308
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001309 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001310 pt_heads ? pt_heads :
1311 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001312 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 pt_sectors ? pt_sectors :
1314 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001315 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001316
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001317 sector_offset = 1;
1318 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001319 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001321 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1322 if (!g_cylinders)
1323 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324}
1325
1326/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001327 * Opens disk_device and optionally reads MBR.
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001328 * If what == OPEN_MAIN:
1329 * Open device, read MBR. Abort program on short read. Create empty
1330 * disklabel if the on-disk structure is invalid (WRITABLE mode).
1331 * If what == TRY_ONLY:
1332 * Open device, read MBR. Return an error if anything is out of place.
1333 * Do not create an empty disklabel. This is used for the "list"
1334 * operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1335 * If what == CREATE_EMPTY_*:
1336 * This means that get_boot() was called recursively from create_*label().
1337 * Do not re-open the device; just set up the ptes array and print
1338 * geometry warnings.
1339 *
Denis Vlasenko4437d192008-04-17 00:12:10 +00001340 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1342 * 0: found or created label
1343 * 1: I/O error
1344 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001345#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1346static int get_boot(enum action what)
1347#else
1348static int get_boot(void)
1349#define get_boot(what) get_boot()
1350#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001351{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001352 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001354 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 for (i = 0; i < 4; i++) {
1356 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001357 pe->part_table = pt_offset(MBRbuffer, i);
1358 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001359 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001360 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001361#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001362 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001363#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001364 }
1365
Denis Vlasenko834410a2006-11-29 12:00:28 +00001366#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001367// ALERT! highly idiotic design!
1368// We end up here when we call get_boot() recursively
1369// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1370// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1371// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1372// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001373 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001374 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001375
Denis Vlasenko4437d192008-04-17 00:12:10 +00001376 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1377
Denis Vlasenkobd852072007-03-19 14:43:38 +00001378 if (fd < 0) {
1379 fd = open(disk_device, O_RDONLY);
1380 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001381 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001382 return 1;
1383 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001384 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001385 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001387 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001388 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001389 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001390 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001392 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001393 fdisk_fatal(unable_to_read);
1394 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001395#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001396 fd = open(disk_device, O_RDONLY);
1397 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001398 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001399 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001400 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001401 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001402 }
1403 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001404#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001405
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001406 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 update_units();
1408
Denis Vlasenko834410a2006-11-29 12:00:28 +00001409#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001410 if (check_sun_label())
1411 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001412#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001413#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414 if (check_sgi_label())
1415 return 0;
1416#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001417#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001418 if (check_aix_label())
1419 return 0;
1420#endif
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001421#if ENABLE_FEATURE_GPT_LABEL
1422 if (check_gpt_label())
1423 return 0;
1424#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001425#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001426 if (check_osf_label()) {
1427 possibly_osf_label = 1;
1428 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001429 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430 return 0;
1431 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001432 printf("This disk has both DOS and BSD magic.\n"
1433 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001434 }
1435#endif
1436
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001437#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001438 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001439 return -1;
1440#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001441 if (!valid_part_table_flag(MBRbuffer)) {
1442 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001443 printf("Device contains neither a valid DOS "
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001444 "partition table, nor Sun, SGI, OSF or GPT "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001445 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001446#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001447 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001448#else
1449 create_doslabel();
1450#endif
1451 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001452 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001453 /* TRY_ONLY: */
1454 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001455 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001456 created_table:
1457#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001458
Denis Vlasenko4437d192008-04-17 00:12:10 +00001459
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001460 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 warn_geometry();
1462
1463 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001464 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001465 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001466 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001467 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001468 else
1469 read_extended(i);
1470 }
1471 }
1472
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001473 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001475 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001476 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001477 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001478 pe->sectorbuffer[510],
1479 pe->sectorbuffer[511],
1480 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001481 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001482 }
1483 }
1484
1485 return 0;
1486}
1487
Denis Vlasenko834410a2006-11-29 12:00:28 +00001488#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489/*
1490 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1491 * If the user hits Enter, DFLT is returned.
1492 * Answers like +10 are interpreted as offsets from BASE.
1493 *
1494 * There is no default if DFLT is not between LOW and HIGH.
1495 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001496static sector_t
1497read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001498{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001499 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001500 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001501 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001502
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001503 if (dflt < low || dflt > high) {
1504 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001505 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001506 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001507
1508 while (1) {
1509 int use_default = default_ok;
1510
1511 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001512 do {
1513 printf(fmt, mesg, low, high, dflt);
1514 read_maybe_empty("");
1515 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1516 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001517
Eric Andersen84bdea82004-05-19 10:49:17 +00001518 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001519 int minus = (*line_ptr == '-');
1520 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001521
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001522 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001523
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001524 /* (1) if 2nd char is digit, use_default = 0.
1525 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001526 while (isdigit(*++line_ptr))
1527 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001528
Rob Landleyb73451d2006-02-24 16:29:00 +00001529 switch (*line_ptr) {
1530 case 'c':
1531 case 'C':
1532 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001533 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001534 break;
1535 case 'K':
1536 absolute = 1024;
1537 break;
1538 case 'k':
1539 absolute = 1000;
1540 break;
1541 case 'm':
1542 case 'M':
1543 absolute = 1000000;
1544 break;
1545 case 'g':
1546 case 'G':
1547 absolute = 1000000000;
1548 break;
1549 default:
1550 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001552 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001553 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001554 unsigned long unit;
1555
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001556 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001557 unit = sector_size * units_per_sector;
1558 bytes += unit/2; /* round */
1559 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001560 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001561 }
1562 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001563 value = -value;
1564 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001565 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001566 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001567 while (isdigit(*line_ptr)) {
1568 line_ptr++;
1569 use_default = 0;
1570 }
1571 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001572 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001573 value = dflt;
1574 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001575 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001576 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001578 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001579 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001580 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001581}
1582
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001583static unsigned
1584get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001585{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001587 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001588
Denis Vlasenkobd852072007-03-19 14:43:38 +00001589 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001590 pe = &ptes[i];
1591
1592 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001593 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1594 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1595 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1596 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001597 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001598 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001599 }
1600 return i;
1601}
1602
1603static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001604get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001605{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001606 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001607 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001608
1609 for (i = 0; i < max; i++) {
1610 struct pte *pe = &ptes[i];
1611 struct partition *p = pe->part_table;
1612
1613 if (p && !is_cleared_partition(p)) {
1614 if (pno >= 0)
1615 goto not_unique;
1616 pno = i;
1617 }
1618 }
1619 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001620 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001621 return pno;
1622 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001623 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001624 return -1;
1625
1626 not_unique:
1627 return get_partition(warn, max);
1628}
1629
1630static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001631get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001632{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001633 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001634 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635
1636 for (i = 0; i < max; i++) {
1637 struct pte *pe = &ptes[i];
1638 struct partition *p = pe->part_table;
1639
1640 if (p && is_cleared_partition(p)) {
1641 if (pno >= 0)
1642 goto not_unique;
1643 pno = i;
1644 }
1645 }
1646 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001647 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001648 return pno;
1649 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001650 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001651 return -1;
1652
1653 not_unique:
1654 return get_partition(warn, max);
1655}
1656
1657
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001658static void
1659change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001660{
1661 display_in_cyl_units = !display_in_cyl_units;
1662 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001663 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001664 str_units(PLURAL));
1665}
1666
1667static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001668toggle_active(int i)
1669{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001670 struct pte *pe = &ptes[i];
1671 struct partition *p = pe->part_table;
1672
Rob Landleyb73451d2006-02-24 16:29:00 +00001673 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001674 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001675 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1676 pe->changed = 1;
1677}
1678
1679static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001680toggle_dos_compatibility_flag(void)
1681{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001682 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001683 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001684 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001685 printf("DOS Compatibility flag is set\n");
1686 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001688 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001689 }
1690}
1691
1692static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001693delete_partition(int i)
1694{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001695 struct pte *pe = &ptes[i];
1696 struct partition *p = pe->part_table;
1697 struct partition *q = pe->ext_pointer;
1698
1699/* Note that for the fifth partition (i == 4) we don't actually
1700 * decrement partitions.
1701 */
1702
1703 if (warn_geometry())
1704 return; /* C/H/S not set */
1705 pe->changed = 1;
1706
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001707 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001708 sun_delete_partition(i);
1709 return;
1710 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001711 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001712 sgi_delete_partition(i);
1713 return;
1714 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001715
1716 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001717 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001718 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001719 ptes[ext_index].ext_pointer = NULL;
1720 extended_offset = 0;
1721 }
1722 clear_partition(p);
1723 return;
1724 }
1725
1726 if (!q->sys_ind && i > 4) {
1727 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001728 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 --i;
1730 clear_partition(ptes[i].ext_pointer);
1731 ptes[i].changed = 1;
1732 } else {
1733 /* not the last one - further ones will be moved down */
1734 if (i > 4) {
1735 /* delete this link in the chain */
1736 p = ptes[i-1].ext_pointer;
1737 *p = *q;
1738 set_start_sect(p, get_start_sect(q));
1739 set_nr_sects(p, get_nr_sects(q));
1740 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001741 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001742 /* the first logical in a longer chain */
1743 pe = &ptes[5];
1744
1745 if (pe->part_table) /* prevent SEGFAULT */
1746 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001747 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001748 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001749 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001750 pe->changed = 1;
1751 }
1752
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001753 if (g_partitions > 5) {
1754 g_partitions--;
1755 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756 ptes[i] = ptes[i+1];
1757 i++;
1758 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001759 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001760 /* the only logical: clear only */
1761 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001762 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001763 }
1764}
1765
1766static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001767change_sysid(void)
1768{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001769 int i, sys, origsys;
1770 struct partition *p;
1771
Eric Andersen040f4402003-07-30 08:40:37 +00001772 /* If sgi_label then don't use get_existing_partition,
1773 let the user select a partition, since get_existing_partition()
1774 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001775 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001776 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001777 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001778 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001779 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001780 if (i == -1)
1781 return;
1782 p = ptes[i].part_table;
1783 origsys = sys = get_sysid(i);
1784
1785 /* if changing types T to 0 is allowed, then
1786 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001787 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001788 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001789 return;
1790 }
1791 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001792 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001794 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001795 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001796 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001797 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798 /* break; */
1799 }
1800
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001801 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001802 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001803 printf("You cannot change a partition into"
1804 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 break;
1806 }
1807 }
1808
1809 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001810#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001811 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001812 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001813 "as Whole disk (5),\n"
1814 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001815 "even Linux likes it\n\n");
1816#endif
1817#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001818 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001819 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001820 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001821 (i == 8 && sys != 0)
1822 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001823 ) {
1824 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001825 "as volume header (0),\nand "
1826 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001827 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001828 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001829#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001830 if (sys == origsys)
1831 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001832 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001834 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835 sgi_change_sysid(i, sys);
1836 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001837 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001838
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001839 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001840 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001841 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001842 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001843 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1844 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001845 break;
1846 }
1847 }
1848}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001849#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001850
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001851
Denis Vlasenko28703012006-12-19 20:32:02 +00001852/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001853 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1854 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1855 * Lubkin Oct. 1991). */
1856
Rob Landleyb73451d2006-02-24 16:29:00 +00001857static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001858linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001859{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001860 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001861
1862 *c = ls / spc;
1863 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001864 *h = ls / g_sectors;
1865 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001866}
1867
Rob Landleyb73451d2006-02-24 16:29:00 +00001868static void
1869check_consistency(const struct partition *p, int partition)
1870{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001871 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1872 unsigned pec, peh, pes; /* physical ending c, h, s */
1873 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1874 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001875
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001876 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001877 return; /* do not check extended partitions */
1878
1879/* physical beginning c, h, s */
1880 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1881 pbh = p->head;
1882 pbs = p->sector & 0x3f;
1883
1884/* physical ending c, h, s */
1885 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1886 peh = p->end_head;
1887 pes = p->end_sector & 0x3f;
1888
1889/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001890 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001891
1892/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001893 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001894
1895/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001896 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001897 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001898 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001899 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1900 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001901 }
1902
1903/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001904 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001905 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001906 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001907 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1908 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001909 }
1910
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001911/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001912 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001913 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001914 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001915 }
1916}
1917
1918static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001919list_disk_geometry(void)
1920{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001921 ullong bytes = ((ullong)total_number_of_sectors << 9);
1922 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001923
1924 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001925 printf("\nDisk %s: %lu MB, %llu bytes\n",
1926 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001927 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001928 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1929 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1930 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001931 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001932 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001933 printf(", total %"SECT_FMT"u sectors",
1934 total_number_of_sectors / (sector_size/512));
1935 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1936 str_units(PLURAL),
1937 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001938}
1939
1940/*
1941 * Check whether partition entries are ordered by their starting positions.
1942 * Return 0 if OK. Return i if partition i should have been earlier.
1943 * Two separate checks: primary and logical partitions.
1944 */
1945static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001946wrong_p_order(int *prev)
1947{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001948 const struct pte *pe;
1949 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001950 sector_t last_p_start_pos = 0, p_start_pos;
1951 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001952
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001953 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001954 if (i == 4) {
1955 last_i = 4;
1956 last_p_start_pos = 0;
1957 }
1958 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001959 p = pe->part_table;
1960 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001961 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001962
1963 if (last_p_start_pos > p_start_pos) {
1964 if (prev)
1965 *prev = last_i;
1966 return i;
1967 }
1968
1969 last_p_start_pos = p_start_pos;
1970 last_i = i;
1971 }
1972 }
1973 return 0;
1974}
1975
Denis Vlasenko834410a2006-11-29 12:00:28 +00001976#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001977/*
1978 * Fix the chain of logicals.
1979 * extended_offset is unchanged, the set of sectors used is unchanged
1980 * The chain is sorted so that sectors increase, and so that
1981 * starting sectors increase.
1982 *
1983 * After this it may still be that cfdisk doesnt like the table.
1984 * (This is because cfdisk considers expanded parts, from link to
1985 * end of partition, and these may still overlap.)
1986 * Now
1987 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1988 * may help.
1989 */
1990static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001991fix_chain_of_logicals(void)
1992{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001993 int j, oj, ojj, sj, sjj;
1994 struct partition *pj,*pjj,tmp;
1995
1996 /* Stage 1: sort sectors but leave sector of part 4 */
1997 /* (Its sector is the global extended_offset.) */
1998 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001999 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002000 oj = ptes[j].offset_from_dev_start;
2001 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002002 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002003 ptes[j].offset_from_dev_start = ojj;
2004 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 pj = ptes[j].part_table;
2006 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2007 pjj = ptes[j+1].part_table;
2008 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2009 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002010 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002012 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 goto stage1;
2014 }
2015 }
2016
2017 /* Stage 2: sort starting sectors */
2018 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002019 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020 pj = ptes[j].part_table;
2021 pjj = ptes[j+1].part_table;
2022 sj = get_start_sect(pj);
2023 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002024 oj = ptes[j].offset_from_dev_start;
2025 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002026 if (oj+sj > ojj+sjj) {
2027 tmp = *pj;
2028 *pj = *pjj;
2029 *pjj = tmp;
2030 set_start_sect(pj, ojj+sjj-oj);
2031 set_start_sect(pjj, oj+sj-ojj);
2032 goto stage2;
2033 }
2034 }
2035
2036 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002037 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002038 ptes[j].changed = 1;
2039}
2040
2041
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002042static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002043fix_partition_table_order(void)
2044{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 struct pte *pei, *pek;
2046 int i,k;
2047
2048 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002049 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002050 return;
2051 }
2052
2053 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2054 /* partition i should have come earlier, move it */
2055 /* We have to move data in the MBR */
2056 struct partition *pi, *pk, *pe, pbuf;
2057 pei = &ptes[i];
2058 pek = &ptes[k];
2059
2060 pe = pei->ext_pointer;
2061 pei->ext_pointer = pek->ext_pointer;
2062 pek->ext_pointer = pe;
2063
2064 pi = pei->part_table;
2065 pk = pek->part_table;
2066
2067 memmove(&pbuf, pi, sizeof(struct partition));
2068 memmove(pi, pk, sizeof(struct partition));
2069 memmove(pk, &pbuf, sizeof(struct partition));
2070
2071 pei->changed = pek->changed = 1;
2072 }
2073
2074 if (i)
2075 fix_chain_of_logicals();
2076
2077 printf("Done.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002078}
2079#endif
2080
2081static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002082list_table(int xtra)
2083{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002084 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002085 int i, w;
2086
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002087 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002088 sun_list_table(xtra);
2089 return;
2090 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002091 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002092 sgi_list_table(xtra);
2093 return;
2094 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002095 if (LABEL_IS_GPT) {
2096 gpt_list_table(xtra);
2097 return;
2098 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002099
2100 list_disk_geometry();
2101
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002102 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002103 xbsd_print_disklabel(xtra);
2104 return;
2105 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106
2107 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2108 but if the device name ends in a digit, say /dev/foo1,
2109 then the partition is called /dev/foo1p3. */
2110 w = strlen(disk_device);
2111 if (w && isdigit(disk_device[w-1]))
2112 w++;
2113 if (w < 5)
2114 w = 5;
2115
Denis Vlasenkobd852072007-03-19 14:43:38 +00002116 // 1 12345678901 12345678901 12345678901 12
2117 printf("%*s Boot Start End Blocks Id System\n",
2118 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002119
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002120 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002122 sector_t psects;
2123 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002124 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125
2126 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002127 if (!p || is_cleared_partition(p))
2128 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002129
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002130 psects = get_nr_sects(p);
2131 pblocks = psects;
2132 podd = 0;
2133
2134 if (sector_size < 1024) {
2135 pblocks /= (1024 / sector_size);
2136 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002137 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002138 if (sector_size > 1024)
2139 pblocks *= (sector_size / 1024);
2140
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002141 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 +00002142 partname(disk_device, i+1, w+2),
2143 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2144 ? '*' : '?',
Denys Vlasenkod958e902010-04-06 02:32:26 +02002145 cround(get_partition_start_from_dev_start(pe)), /* start */
2146 cround(get_partition_start_from_dev_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002147 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002148 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002149 p->sys_ind, /* type id */
2150 partition_type(p->sys_ind)); /* type name */
2151
2152 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002153 }
2154
2155 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002156 /* partition table entries are not checked for correct order
2157 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002158 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002159 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002160 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002161 }
2162}
2163
Denis Vlasenko834410a2006-11-29 12:00:28 +00002164#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002165static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002166x_list_table(int extend)
2167{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002168 const struct pte *pe;
2169 const struct partition *p;
2170 int i;
2171
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002172 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002173 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002174 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002175 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002176 pe = &ptes[i];
2177 p = (extend ? pe->ext_pointer : pe->part_table);
2178 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002179 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 +00002180 i + 1, p->boot_ind, p->head,
2181 sector(p->sector),
2182 cylinder(p->sector, p->cyl), p->end_head,
2183 sector(p->end_sector),
2184 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002185 get_start_sect(p), get_nr_sects(p),
2186 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002187 if (p->sys_ind)
2188 check_consistency(p, i);
2189 }
2190 }
2191}
2192#endif
2193
Denis Vlasenko834410a2006-11-29 12:00:28 +00002194#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002195static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002196fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002197{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002198 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002199 const struct pte *pe = &ptes[0];
2200 const struct partition *p;
2201
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002202 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002204 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002205 first[i] = 0xffffffff;
2206 last[i] = 0;
2207 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002208 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 last[i] = first[i] + get_nr_sects(p) - 1;
2210 }
2211 }
2212}
2213
2214static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002215check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002216{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002217 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002218
2219 real_s = sector(s) - 1;
2220 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002221 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002222 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002223 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002224 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002225 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002226 n, h + 1, g_heads);
2227 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002228 printf("Partition %u: sector %u greater than "
2229 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002230 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002231 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2232 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002233 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002234 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2235 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236}
2237
2238static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002239verify(void)
2240{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002241 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002242 sector_t total = 1;
2243 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002244 struct partition *p;
2245
2246 if (warn_geometry())
2247 return;
2248
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002249 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002250 verify_sun();
2251 return;
2252 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002253 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002254 verify_sgi(1);
2255 return;
2256 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002257
2258 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002259 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002260 struct pte *pe = &ptes[i];
2261
2262 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002263 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002264 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002265 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002266 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002267 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002268 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2269 last[i]);
2270 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002271 for (j = 0; j < i; j++) {
2272 if ((first[i] >= first[j] && first[i] <= last[j])
2273 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002274 printf("Warning: partition %u overlaps "
2275 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002276 total += first[i] >= first[j] ?
2277 first[i] : first[j];
2278 total -= last[i] <= last[j] ?
2279 last[i] : last[j];
2280 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281 }
2282 }
2283 }
2284
2285 if (extended_offset) {
2286 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002287 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002288 get_nr_sects(pex->part_table) - 1;
2289
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002290 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002291 total++;
2292 p = ptes[i].part_table;
2293 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002294 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002295 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002296 "is empty\n", i + 1);
2297 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002298 printf("Logical partition %u not entirely in "
2299 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002300 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002301 }
2302 }
2303
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002304 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002305 printf("Total allocated sectors %u greater than the maximum "
2306 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002307 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002308 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002309 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002310 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002311 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002312}
2313
2314static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002315add_partition(int n, int sys)
2316{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002317 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002318 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 struct partition *p = ptes[n].part_table;
2320 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002321 sector_t limit, temp;
2322 sector_t start, stop = 0;
2323 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002324
2325 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002326 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002327 return;
2328 }
2329 fill_bounds(first, last);
2330 if (n < 4) {
2331 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002332 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002333 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002334 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002335 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002336 if (extended_offset) {
2337 first[ext_index] = extended_offset;
2338 last[ext_index] = get_start_sect(q) +
2339 get_nr_sects(q) - 1;
2340 }
2341 } else {
2342 start = extended_offset + sector_offset;
2343 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2344 }
2345 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002346 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 first[i] = (cround(first[i]) - 1) * units_per_sector;
2348
Denis Vlasenkobd852072007-03-19 14:43:38 +00002349 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002350 do {
2351 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002352 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002353 int lastplusoff;
2354
Denys Vlasenkod958e902010-04-06 02:32:26 +02002355 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002356 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002357 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 if (start >= first[i] && start <= lastplusoff)
2359 start = lastplusoff + 1;
2360 }
2361 if (start > limit)
2362 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002363 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002364 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002366 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002367 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002368 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002369 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002370
2371 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002372 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002373 if (display_in_cyl_units) {
2374 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002375 if (start < saved_start)
2376 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002378 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002379 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002380 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 if (n > 4) { /* NOT for fifth partition */
2382 struct pte *pe = &ptes[n];
2383
Denys Vlasenkod958e902010-04-06 02:32:26 +02002384 pe->offset_from_dev_start = start - sector_offset;
2385 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2386 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 if (sector_offset == 1)
2388 start++;
2389 }
2390 }
2391
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002392 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002393 struct pte *pe = &ptes[i];
2394
Denys Vlasenkod958e902010-04-06 02:32:26 +02002395 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2396 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 if (start < first[i] && limit >= first[i])
2398 limit = first[i] - 1;
2399 }
2400 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002401 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002402 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002403 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002404 return;
2405 }
2406 if (cround(start) == cround(limit)) {
2407 stop = limit;
2408 } else {
2409 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002410 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002411 str_units(SINGULAR));
Denys Vlasenkod958e902010-04-06 02:32:26 +02002412 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002413 if (display_in_cyl_units) {
2414 stop = stop * units_per_sector - 1;
2415 if (stop >limit)
2416 stop = limit;
2417 }
2418 }
2419
2420 set_partition(n, 0, start, stop, sys);
2421 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002422 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002423
Rob Landleyb73451d2006-02-24 16:29:00 +00002424 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002425 struct pte *pe4 = &ptes[4];
2426 struct pte *pen = &ptes[n];
2427
2428 ext_index = n;
2429 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002430 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002431 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2433 pe4->ext_pointer = pe4->part_table + 1;
2434 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002435 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436 }
2437}
2438
2439static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002440add_logical(void)
2441{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002442 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2443 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002444
Rob Landley081e3842006-08-03 20:07:35 +00002445 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2447 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002448 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002450 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002452 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002453}
2454
2455static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002456new_partition(void)
2457{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002458 int i, free_primary = 0;
2459
2460 if (warn_geometry())
2461 return;
2462
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002463 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002464 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465 return;
2466 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002467 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002468 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002469 return;
2470 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002471 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002472 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2473"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2474"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 return;
2476 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002477
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478 for (i = 0; i < 4; i++)
2479 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002480
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002481 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002482 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002483 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002484 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002485
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486 if (!free_primary) {
2487 if (extended_offset)
2488 add_logical();
2489 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002490 printf("You must delete some partition and add "
2491 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002493 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002494 snprintf(line, sizeof(line),
2495 "Command action\n"
2496 " %s\n"
2497 " p primary partition (1-4)\n",
2498 (extended_offset ?
2499 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002500 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002501 c = read_nonempty(line);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002502 if ((c | 0x20) == 'p') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002503 i = get_nonexisting_partition(0, 4);
2504 if (i >= 0)
2505 add_partition(i, LINUX_NATIVE);
2506 return;
2507 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002508 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 add_logical();
2510 return;
2511 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002512 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002513 i = get_nonexisting_partition(0, 4);
2514 if (i >= 0)
2515 add_partition(i, EXTENDED);
2516 return;
2517 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002518 printf("Invalid partition number "
2519 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520 }
2521 }
2522}
2523
2524static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002525write_table(void)
2526{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 int i;
2528
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002529 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002530 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002531 if (ptes[i].changed)
2532 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002533 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 struct pte *pe = &ptes[i];
2535
2536 if (pe->changed) {
2537 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002538 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002539 }
2540 }
2541 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002542 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543 /* no test on change? the printf below might be mistaken */
2544 sgi_write_table();
2545 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002546 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547 int needw = 0;
2548
Rob Landleyb73451d2006-02-24 16:29:00 +00002549 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002550 if (ptes[i].changed)
2551 needw = 1;
2552 if (needw)
2553 sun_write_table();
2554 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555
Denis Vlasenkobd852072007-03-19 14:43:38 +00002556 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557 reread_partition_table(1);
2558}
2559
Rob Landleyb73451d2006-02-24 16:29:00 +00002560static void
2561reread_partition_table(int leave)
2562{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 int i;
2564
Denis Vlasenkobd852072007-03-19 14:43:38 +00002565 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002566 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002567 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002568 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002569 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002570 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002571#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002573 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002574 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002575 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002576 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002577#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578
2579 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002580 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002581 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002582 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583 }
2584}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002585#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002586
Denis Vlasenko834410a2006-11-29 12:00:28 +00002587#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588#define MAX_PER_LINE 16
2589static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002590print_buffer(char *pbuffer)
2591{
2592 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593
2594 for (i = 0, l = 0; i < sector_size; i++, l++) {
2595 if (l == 0)
2596 printf("0x%03X:", i);
2597 printf(" %02X", (unsigned char) pbuffer[i]);
2598 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002599 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002600 l = -1;
2601 }
2602 }
2603 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002604 bb_putchar('\n');
2605 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606}
2607
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002609print_raw(void)
2610{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 int i;
2612
Denis Vlasenkobd852072007-03-19 14:43:38 +00002613 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002614 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002617 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002619 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002620}
2621
2622static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002623move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002624{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 struct pte *pe = &ptes[i];
2626 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002627 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002628
2629 if (warn_geometry())
2630 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002631 nr_sects = get_nr_sects(p);
2632 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002633 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 return;
2635 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002636 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002637 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2638 if (new != first) {
2639 sector_t new_relative = new - pe->offset_from_dev_start;
2640 nr_sects += (get_start_sect(p) - new_relative);
2641 set_start_sect(p, new_relative);
2642 set_nr_sects(p, nr_sects);
2643 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2644 if ((line_ptr[0] | 0x20) == 'y')
2645 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002646 pe->changed = 1;
2647 }
2648}
2649
2650static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002651xselect(void)
2652{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002653 char c;
2654
Rob Landleyb73451d2006-02-24 16:29:00 +00002655 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002656 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002657 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002658 switch (c) {
2659 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002660 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002661 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002662 break;
2663 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002664 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002665 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002666 break;
2667 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002668 user_cylinders = g_cylinders =
2669 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002670 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002671 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002672 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002673 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002674 warn_cylinders();
2675 break;
2676 case 'd':
2677 print_raw();
2678 break;
2679 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002680 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002681 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002682 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002683 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002684 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 x_list_table(1);
2686 break;
2687 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002688 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002689 fix_partition_table_order();
2690 break;
2691 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002692#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002693 create_sgilabel();
2694#endif
2695 break;
2696 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002697 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002698 update_units();
2699 break;
2700 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002701 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002702 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002703 break;
2704 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002705 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707 break;
2708 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002709 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002710 list_table(1);
2711 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002712 x_list_table(0);
2713 break;
2714 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002715 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002716 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002717 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002718 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002719 case 'r':
2720 return;
2721 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002722 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002724 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002725 printf("Warning: setting sector offset for DOS "
2726 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002727 }
2728 update_units();
2729 break;
2730 case 'v':
2731 verify();
2732 break;
2733 case 'w':
2734 write_table(); /* does not return */
2735 break;
2736 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002737 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002739 break;
2740 default:
2741 xmenu();
2742 }
2743 }
2744}
2745#endif /* ADVANCED mode */
2746
2747static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002748is_ide_cdrom_or_tape(const char *device)
2749{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 FILE *procf;
2751 char buf[100];
2752 struct stat statbuf;
2753 int is_ide = 0;
2754
2755 /* No device was given explicitly, and we are trying some
2756 likely things. But opening /dev/hdc may produce errors like
2757 "hdc: tray open or drive not ready"
2758 if it happens to be a CD-ROM drive. It even happens that
2759 the process hangs on the attempt to read a music CD.
2760 So try to be careful. This only works since 2.1.73. */
2761
2762 if (strncmp("/dev/hd", device, 7))
2763 return 0;
2764
2765 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002766 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002767 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2768 is_ide = (!strncmp(buf, "cdrom", 5) ||
2769 !strncmp(buf, "tape", 4));
2770 else
2771 /* Now when this proc file does not exist, skip the
2772 device when it is read-only. */
2773 if (stat(device, &statbuf) == 0)
2774 is_ide = ((statbuf.st_mode & 0222) == 0);
2775
2776 if (procf)
2777 fclose(procf);
2778 return is_ide;
2779}
2780
Rob Landley5527b912006-02-25 03:46:10 +00002781
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002782static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002783open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002784{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002785 int gb;
2786
2787 disk_device = device;
2788 if (setjmp(listingbuf))
2789 return;
2790 if (!user_specified)
2791 if (is_ide_cdrom_or_tape(device))
2792 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002793
2794 /* Open disk_device, save file descriptor to dev_fd */
2795 errno = 0;
2796 gb = get_boot(TRY_ONLY);
2797 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002798 /* Ignore other errors, since we try IDE
2799 and SCSI hard disks which may not be
2800 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002801 if (user_specified || errno == EACCES)
2802 bb_perror_msg("can't open '%s'", device);
2803 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002804 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002805
2806 if (gb < 0) { /* no DOS signature */
2807 list_disk_geometry();
2808 if (LABEL_IS_AIX)
2809 goto ret;
2810#if ENABLE_FEATURE_OSF_LABEL
2811 if (bsd_trydev(device) < 0)
2812#endif
2813 printf("Disk %s doesn't contain a valid "
2814 "partition table\n", device);
2815 } else {
2816 list_table(0);
2817#if ENABLE_FEATURE_FDISK_WRITABLE
2818 if (!LABEL_IS_SUN && g_partitions > 4) {
2819 delete_partition(ext_index);
2820 }
2821#endif
2822 }
2823 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002824 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825}
2826
2827/* for fdisk -l: try all things in /proc/partitions
2828 that look like a partition name (do not end in a digit) */
2829static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002830list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002831{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 FILE *procpt;
2833 char line[100], ptname[100], devname[120], *s;
2834 int ma, mi, sz;
2835
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002836 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837
2838 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002839 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002840 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002842 for (s = ptname; *s; s++)
Denys Vlasenko8f65b0c2010-07-06 18:46:02 +02002843 continue;
Denys Vlasenkobf1d3472010-02-24 08:13:30 +01002844 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2845 if (s[-1] >= '1' && s[-1] <= '9')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002847 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002848 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002852#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853}
2854
Denis Vlasenko834410a2006-11-29 12:00:28 +00002855#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002857unknown_command(int c)
2858{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002859 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002861#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002863int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002864int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002865{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002866 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 * fdisk -v
2869 * fdisk -l [-b sectorsize] [-u] device ...
2870 * fdisk -s [partition] ...
2871 * fdisk [-b sectorsize] [-u] device
2872 *
2873 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002875 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002876
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002877 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002878
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002879 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002880 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002881 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002882 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002883 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00002884 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02002885 * so cannot be combined with multiple disks,
2886 * and the same goes for the C/H/S options.
2887 */
2888 if (sector_size < 512
2889 || sector_size > 0x10000
2890 || (sector_size & (sector_size-1)) /* not power of 2 */
2891 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002892 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02002893 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002894 sector_offset = 2;
2895 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002897 if (user_heads <= 0 || user_heads >= 256)
2898 user_heads = 0;
2899 if (user_sectors <= 0 || user_sectors >= 64)
2900 user_sectors = 0;
2901 if (opt & OPT_u)
2902 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002903
Denis Vlasenko834410a2006-11-29 12:00:28 +00002904#if ENABLE_FEATURE_FDISK_WRITABLE
2905 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002907#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002908 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002910 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002911 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002912 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002914 /* we don't have device names, */
2915 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002916 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 }
2918 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002919#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002920 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002921#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922
Denis Vlasenko834410a2006-11-29 12:00:28 +00002923#if ENABLE_FEATURE_FDISK_BLKSIZE
2924 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002925 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926
2927 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002928 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002929 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002930 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002931 unsigned long long size;
2932 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002933 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002934 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002935 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002936 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002937 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002938 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002939 }
2940 return 0;
2941 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002942#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002943
Denis Vlasenko834410a2006-11-29 12:00:28 +00002944#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002945 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002946 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947
Denis Vlasenko834410a2006-11-29 12:00:28 +00002948 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002949 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002951 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002952 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002953 printf("Detected an OSF/1 disklabel on %s, entering "
2954 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002955 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002956 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002957 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002958 /* If we return we may want to make an empty DOS label? */
2959 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960
2961 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002962 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002963 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002964 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002965 switch (c) {
2966 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002967 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002968 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002969 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002970 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002971 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002972 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002973 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002974 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002975 else
2976 unknown_command(c);
2977 break;
2978 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002979 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002980 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002981 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002982 if (read_maybe_empty("Please enter the name of the "
2983 "new boot file: ") == '\n')
2984 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 else
2986 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002987 }
2988#if ENABLE_FEATURE_OSF_LABEL
2989 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002990 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002991#endif
2992 break;
2993 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002994 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002995 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002996 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002997 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002998 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002999 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003000 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003001 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003002 else
3003 unknown_command(c);
3004 break;
3005 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003006 {
Eric Andersen040f4402003-07-30 08:40:37 +00003007 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00003008 /* If sgi_label then don't use get_existing_partition,
3009 let the user select a partition, since
3010 get_existing_partition() only works for Linux-like
3011 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003012 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003013 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003014 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003015 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003016 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003017 if (j >= 0)
3018 delete_partition(j);
3019 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003020 break;
3021 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003022 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003023 create_sgiinfo();
3024 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003025 unknown_command(c);
3026 case 'l':
3027 list_types(get_sys_types());
3028 break;
3029 case 'm':
3030 menu();
3031 break;
3032 case 'n':
3033 new_partition();
3034 break;
3035 case 'o':
3036 create_doslabel();
3037 break;
3038 case 'p':
3039 list_table(0);
3040 break;
3041 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003042 if (ENABLE_FEATURE_CLEAN_UP)
3043 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003044 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003045 return 0;
3046 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003047#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003048 create_sunlabel();
3049#endif
3050 break;
3051 case 't':
3052 change_sysid();
3053 break;
3054 case 'u':
3055 change_units();
3056 break;
3057 case 'v':
3058 verify();
3059 break;
3060 case 'w':
3061 write_table(); /* does not return */
3062 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003063#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003064 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003065 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003066 printf("\n\tSorry, no experts menu for SGI "
3067 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003068 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003069 xselect();
3070 break;
3071#endif
3072 default:
3073 unknown_command(c);
3074 menu();
3075 }
3076 }
3077 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003078#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003079}