blob: 8e133178de40e3c87749d04eca140bfaf29c980e [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000010#ifndef _LARGEFILE64_SOURCE
11/* For lseek64 */
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +020012# define _LARGEFILE64_SOURCE
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000013#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000014#include <assert.h> /* assert */
Denys Vlasenkoda49f582009-07-08 02:58:38 +020015#include <sys/mount.h>
16#if !defined(BLKSSZGET)
17# define BLKSSZGET _IO(0x12, 104)
18#endif
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +020019#if !defined(BLKGETSIZE64)
20# define BLKGETSIZE64 _IOR(0x12,114,size_t)
21#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000023
Denys Vlasenko5ea1de22010-04-06 02:31:43 +020024#if BB_LITTLE_ENDIAN
25# define inline_if_little_endian ALWAYS_INLINE
26#else
27# define inline_if_little_endian /* nothing */
28#endif
29
30
Denis Vlasenko834410a2006-11-29 12:00:28 +000031/* Looks like someone forgot to add this to config system */
32#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
33# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000034# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +000035#endif
36
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000037#define DEFAULT_SECTOR_SIZE 512
38#define DEFAULT_SECTOR_SIZE_STR "512"
39#define MAX_SECTOR_SIZE 2048
40#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
41#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000043#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000044
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000045#define EXTENDED 0x05
46#define WIN98_EXTENDED 0x0f
47#define LINUX_PARTITION 0x81
48#define LINUX_SWAP 0x82
49#define LINUX_NATIVE 0x83
50#define LINUX_EXTENDED 0x85
51#define LINUX_LVM 0x8e
52#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000053
Denis Vlasenkoc033d512008-04-17 01:52:28 +000054
55enum {
56 OPT_b = 1 << 0,
57 OPT_C = 1 << 1,
58 OPT_H = 1 << 2,
59 OPT_l = 1 << 3,
60 OPT_S = 1 << 4,
61 OPT_u = 1 << 5,
62 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
63};
64
65
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000066typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +020067/* Used for sector numbers. Partition formats we know
68 * do not support more than 2^32 sectors
69 */
70typedef uint32_t sector_t;
71#if UINT_MAX == 4294967295
72# define SECT_FMT ""
73#elif ULONG_MAX == 4294967295
74# define SECT_FMT "l"
75#else
76# error Cant detect sizeof(uint32_t)
77#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000078
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000080 unsigned char heads;
81 unsigned char sectors;
82 unsigned short cylinders;
83 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000084};
85
Denis Vlasenko98ae2162006-10-12 19:30:44 +000086#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000087
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000088static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000089"Building a new %s. Changes will remain in memory only,\n"
90"until you decide to write them. After that the previous content\n"
91"won't be recoverable.\n\n";
92
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000093static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +020094"Partition %u is already defined, delete it before re-adding\n";
Denis Vlasenkobd852072007-03-19 14:43:38 +000095
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000096
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000097struct partition {
98 unsigned char boot_ind; /* 0x80 - active */
99 unsigned char head; /* starting head */
100 unsigned char sector; /* starting sector */
101 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000102 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000103 unsigned char end_head; /* end head */
104 unsigned char end_sector; /* end sector */
105 unsigned char end_cyl; /* end cylinder */
106 unsigned char start4[4]; /* starting sector counting from 0 */
107 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000108} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000109
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000110static const char unable_to_open[] ALIGN1 = "can't open '%s'";
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000111static const char unable_to_read[] ALIGN1 = "can't read from %s";
112static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000113
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000114enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +0000115 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +0000116};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000117
Denis Vlasenko4437d192008-04-17 00:12:10 +0000118#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000119
Denis Vlasenko834410a2006-11-29 12:00:28 +0000120#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000121#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000122#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000123#else
124#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000125#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000126#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000127
Denis Vlasenko834410a2006-11-29 12:00:28 +0000128#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000129#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000130#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000131#else
132#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000133#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000134#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000135
Denis Vlasenko834410a2006-11-29 12:00:28 +0000136#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000137#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000138#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000139#else
140#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000141#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000142#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000143
Denis Vlasenko834410a2006-11-29 12:00:28 +0000144#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000145#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000146#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000147#else
148#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000149#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000150#endif
Rob Landley5527b912006-02-25 03:46:10 +0000151
Denis Vlasenko4437d192008-04-17 00:12:10 +0000152enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000153
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000154static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000155#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000156static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000157static void reread_partition_table(int leave);
158static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200159static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000160static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200161static sector_t read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000162#endif
163static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000164static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000165#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000166static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000167#else
168static int get_boot(void);
169#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000170
171#define PLURAL 0
172#define SINGULAR 1
173
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200174static sector_t get_start_sect(const struct partition *p);
175static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000176
177/*
178 * per partition table entry data
179 *
180 * The four primary partitions have the same sectorbuffer (MBRbuffer)
181 * and have NULL ext_pointer.
182 * Each logical partition table entry has two pointers, one for the
183 * partition and one link to the next one.
184 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000185struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000186 struct partition *part_table; /* points into sectorbuffer */
187 struct partition *ext_pointer; /* points into sectorbuffer */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200188 sector_t offset_from_dev_start; /* disk sector number */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200189 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000190#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200191 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000192#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000193};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000194
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000195/* DOS partition types */
196
197static const char *const i386_sys_types[] = {
198 "\x00" "Empty",
199 "\x01" "FAT12",
200 "\x04" "FAT16 <32M",
201 "\x05" "Extended", /* DOS 3.3+ extended partition */
202 "\x06" "FAT16", /* DOS 16-bit >=32M */
203 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
204 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
205 "\x0b" "Win95 FAT32",
206 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
207 "\x0e" "Win95 FAT16 (LBA)",
208 "\x0f" "Win95 Ext'd (LBA)",
209 "\x11" "Hidden FAT12",
210 "\x12" "Compaq diagnostics",
211 "\x14" "Hidden FAT16 <32M",
212 "\x16" "Hidden FAT16",
213 "\x17" "Hidden HPFS/NTFS",
214 "\x1b" "Hidden Win95 FAT32",
215 "\x1c" "Hidden W95 FAT32 (LBA)",
216 "\x1e" "Hidden W95 FAT16 (LBA)",
217 "\x3c" "Part.Magic recovery",
218 "\x41" "PPC PReP Boot",
219 "\x42" "SFS",
220 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
221 "\x80" "Old Minix", /* Minix 1.4a and earlier */
222 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
223 "\x82" "Linux swap", /* also Solaris */
224 "\x83" "Linux",
225 "\x84" "OS/2 hidden C: drive",
226 "\x85" "Linux extended",
227 "\x86" "NTFS volume set",
228 "\x87" "NTFS volume set",
229 "\x8e" "Linux LVM",
230 "\x9f" "BSD/OS", /* BSDI */
231 "\xa0" "Thinkpad hibernation",
232 "\xa5" "FreeBSD", /* various BSD flavours */
233 "\xa6" "OpenBSD",
234 "\xa8" "Darwin UFS",
235 "\xa9" "NetBSD",
236 "\xab" "Darwin boot",
237 "\xb7" "BSDI fs",
238 "\xb8" "BSDI swap",
239 "\xbe" "Solaris boot",
240 "\xeb" "BeOS fs",
241 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
242 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
243 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
244 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
245 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
246 autodetect using persistent
247 superblock */
248#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
249 "\x02" "XENIX root",
250 "\x03" "XENIX usr",
251 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
252 "\x09" "AIX bootable", /* AIX data or Coherent */
253 "\x10" "OPUS",
254 "\x18" "AST SmartSleep",
255 "\x24" "NEC DOS",
256 "\x39" "Plan 9",
257 "\x40" "Venix 80286",
258 "\x4d" "QNX4.x",
259 "\x4e" "QNX4.x 2nd part",
260 "\x4f" "QNX4.x 3rd part",
261 "\x50" "OnTrack DM",
262 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
263 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
264 "\x53" "OnTrack DM6 Aux3",
265 "\x54" "OnTrackDM6",
266 "\x55" "EZ-Drive",
267 "\x56" "Golden Bow",
268 "\x5c" "Priam Edisk",
269 "\x61" "SpeedStor",
270 "\x64" "Novell Netware 286",
271 "\x65" "Novell Netware 386",
272 "\x70" "DiskSecure Multi-Boot",
273 "\x75" "PC/IX",
274 "\x93" "Amoeba",
275 "\x94" "Amoeba BBT", /* (bad block table) */
276 "\xa7" "NeXTSTEP",
277 "\xbb" "Boot Wizard hidden",
278 "\xc1" "DRDOS/sec (FAT-12)",
279 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
280 "\xc6" "DRDOS/sec (FAT-16)",
281 "\xc7" "Syrinx",
282 "\xda" "Non-FS data",
283 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
284 Concurrent DOS or CTOS */
285 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
286 "\xdf" "BootIt", /* BootIt EMBRM */
287 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
288 extended partition */
289 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
290 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
291 partition < 1024 cyl. */
292 "\xf1" "SpeedStor",
293 "\xf4" "SpeedStor", /* SpeedStor large partition */
294 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
295 "\xff" "BBT", /* Xenix Bad Block Table */
296#endif
297 NULL
298};
299
Denis Vlasenko4437d192008-04-17 00:12:10 +0000300enum {
301 dev_fd = 3 /* the disk */
302};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000303
304/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000305struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000306 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000307
308 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000309 int g_partitions; // = 4; /* maximum partition + 1 */
310 unsigned units_per_sector; // = 1;
311 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
312 unsigned user_set_sector_size;
313 unsigned sector_offset; // = 1;
314 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000315 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000316 smallint display_in_cyl_units; // = 1;
317#if ENABLE_FEATURE_OSF_LABEL
318 smallint possibly_osf_label;
319#endif
320
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000321 smallint listing; /* no aborts for fdisk -l */
322 smallint dos_compatible_flag; // = 1;
323#if ENABLE_FEATURE_FDISK_WRITABLE
324 //int dos_changed;
325 smallint nowarn; /* no warnings for fdisk -l/-s */
326#endif
327 int ext_index; /* the prime extended partition */
328 unsigned user_cylinders, user_heads, user_sectors;
329 unsigned pt_heads, pt_sectors;
330 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200331 sector_t extended_offset; /* offset of link pointers */
332 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000333
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000334 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000335 char line_buffer[80];
336 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000337 /* Raw disk label. For DOS-type partition tables the MBR,
338 * with descriptions of the primary partitions. */
339 char MBRbuffer[MAX_SECTOR_SIZE];
340 /* Partition tables */
341 struct pte ptes[MAXIMUM_PARTS];
342};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000343#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000344#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000345#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000346#define g_partitions (G.g_partitions )
347#define units_per_sector (G.units_per_sector )
348#define sector_size (G.sector_size )
349#define user_set_sector_size (G.user_set_sector_size)
350#define sector_offset (G.sector_offset )
351#define g_heads (G.g_heads )
352#define g_sectors (G.g_sectors )
353#define g_cylinders (G.g_cylinders )
354#define current_label_type (G.current_label_type )
355#define display_in_cyl_units (G.display_in_cyl_units)
356#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000357#define listing (G.listing )
358#define dos_compatible_flag (G.dos_compatible_flag )
359#define nowarn (G.nowarn )
360#define ext_index (G.ext_index )
361#define user_cylinders (G.user_cylinders )
362#define user_heads (G.user_heads )
363#define user_sectors (G.user_sectors )
364#define pt_heads (G.pt_heads )
365#define pt_sectors (G.pt_sectors )
366#define kern_heads (G.kern_heads )
367#define kern_sectors (G.kern_sectors )
368#define extended_offset (G.extended_offset )
369#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000370#define listingbuf (G.listingbuf )
371#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000372#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000373#define MBRbuffer (G.MBRbuffer )
374#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000375#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000376 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000377 sector_size = DEFAULT_SECTOR_SIZE; \
378 sector_offset = 1; \
379 g_partitions = 4; \
380 display_in_cyl_units = 1; \
381 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000382 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000383} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000384
Denis Vlasenkobd852072007-03-19 14:43:38 +0000385
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000386/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200387/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
388 * disks > 2^32 sectors
389 */
390static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000391{
392 uint64_t v64;
393 unsigned long longsectors;
394
395 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000396 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200397 v64 >>= 9;
398 if (v64 != (sector_t)v64) {
399 ret_trunc:
400 /* Not only DOS, but all other partition tables
401 * we support can't record more than 32 bit
402 * sector counts or offsets
403 */
404 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
405 v64 = (uint32_t)-1L;
406 }
407 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000408 }
409 /* Needs temp of type long */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200410 if (ioctl(fd, BLKGETSIZE, &longsectors)) {
411 /* Perhaps this is a disk image */
412 off_t sz = lseek(fd, 0, SEEK_END);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000413 longsectors = 0;
Denys Vlasenkod958e902010-04-06 02:32:26 +0200414 if (sz > 0)
415 longsectors = (uoff_t)sz / sector_size;
416 lseek(fd, 0, SEEK_SET);
417 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200418 if (sizeof(long) > sizeof(sector_t)
419 && longsectors != (sector_t)longsectors
420 ) {
421 goto ret_trunc;
422 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000423 return longsectors;
424}
425
426
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000427#define IS_EXTENDED(i) \
428 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
429
430#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
431
432#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
433
434#define pt_offset(b, n) \
435 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
436
437#define sector(s) ((s) & 0x3f)
438
439#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
440
441#define hsc2sector(h,s,c) \
442 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
443
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000444static void
445close_dev_fd(void)
446{
447 /* Not really closing, but making sure it is open, and to harmless place */
448 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
449}
450
Denis Vlasenkobd852072007-03-19 14:43:38 +0000451/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000452 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000453 */
454static const char *
455partname(const char *dev, int pno, int lth)
456{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000457 const char *p;
458 int w, wp;
459 int bufsiz;
460 char *bufp;
461
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000462 bufp = partname_buffer;
463 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000464
465 w = strlen(dev);
466 p = "";
467
468 if (isdigit(dev[w-1]))
469 p = "p";
470
471 /* devfs kludge - note: fdisk partition names are not supposed
472 to equal kernel names, so there is no reason to do this */
473 if (strcmp(dev + w - 4, "disc") == 0) {
474 w -= 4;
475 p = "part";
476 }
477
478 wp = strlen(p);
479
480 if (lth) {
481 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200482 lth-wp-2, w, dev, p, pno);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000483 } else {
484 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
485 }
486 return bufp;
487}
488
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000489static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000490get_part_table(int i)
491{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000492 return ptes[i].part_table;
493}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000494
495static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000496str_units(int n)
497{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000498 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000499 return display_in_cyl_units ? "cylinder" : "sector";
500 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000501}
502
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000503static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000504valid_part_table_flag(const char *mbuffer)
505{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000506 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000507}
508
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200509static void fdisk_fatal(const char *why)
510{
511 if (listing) {
512 close_dev_fd();
513 longjmp(listingbuf, 1);
514 }
515 bb_error_msg_and_die(why, disk_device);
516}
517
518static void
519seek_sector(sector_t secno)
520{
521#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
522 off64_t off = (off64_t)secno * sector_size;
523 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
524 fdisk_fatal(unable_to_seek);
525#else
526 uint64_t off = (uint64_t)secno * sector_size;
527 if (off > MAXINT(off_t)
528 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
529 ) {
530 fdisk_fatal(unable_to_seek);
531 }
532#endif
533}
534
Denis Vlasenko834410a2006-11-29 12:00:28 +0000535#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200536/* Read line; return 0 or first printable char */
537static int
538read_line(const char *prompt)
539{
540 int sz;
541
542 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
543 if (sz <= 0)
544 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
545
546 if (line_buffer[sz-1] == '\n')
547 line_buffer[--sz] = '\0';
548
549 line_ptr = line_buffer;
550 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
551 line_ptr++;
552 return *line_ptr;
553}
554
555static void
556set_all_unchanged(void)
557{
558 int i;
559
560 for (i = 0; i < MAXIMUM_PARTS; i++)
561 ptes[i].changed = 0;
562}
563
564static ALWAYS_INLINE void
565set_changed(int i)
566{
567 ptes[i].changed = 1;
568}
569
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000570static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000571write_part_table_flag(char *b)
572{
573 b[510] = 0x55;
574 b[511] = 0xaa;
575}
576
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000577static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000578read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000579{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000580 while (!read_line(mesg))
581 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000582 return *line_ptr;
583}
584
585static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000586read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000587{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000588 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000589 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000590 line_ptr[0] = '\n';
591 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000592 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000593 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000594}
595
596static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000597read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000599 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000600 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000601 read_nonempty("Hex code (type L to list codes): ");
Denys Vlasenkod958e902010-04-06 02:32:26 +0200602 if ((line_ptr[0] | 0x20) == 'l') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000603 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000604 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000605 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000606 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200607 if (v <= 0xff)
608 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000609 }
610}
611
Denis Vlasenko9764d692008-07-09 21:20:50 +0000612static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200613write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000614{
615 seek_sector(secno);
616 xwrite(dev_fd, buf, sector_size);
617}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200618#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000619
620
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000621#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000622
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100623struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624 unsigned char info[128]; /* Informative text string */
625 unsigned char spare0[14];
626 struct sun_info {
627 unsigned char spare1;
628 unsigned char id;
629 unsigned char spare2;
630 unsigned char flags;
631 } infos[8];
632 unsigned char spare1[246]; /* Boot information etc. */
633 unsigned short rspeed; /* Disk rotational speed */
634 unsigned short pcylcount; /* Physical cylinder count */
635 unsigned short sparecyl; /* extra sects per cylinder */
636 unsigned char spare2[4]; /* More magic... */
637 unsigned short ilfact; /* Interleave factor */
638 unsigned short ncyl; /* Data cylinder count */
639 unsigned short nacyl; /* Alt. cylinder count */
640 unsigned short ntrks; /* Tracks per cylinder */
641 unsigned short nsect; /* Sectors per track */
642 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000643 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000644 uint32_t start_cylinder;
645 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000646 } partitions[8];
647 unsigned short magic; /* Magic number */
648 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100649} FIX_ALIASING;
650typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000651#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000652STATIC_OSF void bsd_select(void);
653STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000654#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655
Denis Vlasenko28703012006-12-19 20:32:02 +0000656#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000657static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000658fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000659{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000660 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000661}
662
Rob Landley88621d72006-08-29 19:41:06 +0000663static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000664fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000665{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000666 return (x << 24) |
667 ((x & 0xFF00) << 8) |
668 ((x & 0xFF0000) >> 8) |
669 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000670}
671#endif
672
Denis Vlasenkobd852072007-03-19 14:43:38 +0000673STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000674STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000675STATIC_SGI int sgi_get_sysid(int i);
676STATIC_SGI void sgi_delete_partition(int i);
677STATIC_SGI void sgi_change_sysid(int i, int sys);
678STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000679#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000680STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000681#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000682STATIC_SGI int verify_sgi(int verbose);
683STATIC_SGI void sgi_add_partition(int n, int sys);
684STATIC_SGI void sgi_set_swappartition(int i);
685STATIC_SGI const char *sgi_get_bootfile(void);
686STATIC_SGI void sgi_set_bootfile(const char* aFile);
687STATIC_SGI void create_sgiinfo(void);
688STATIC_SGI void sgi_write_table(void);
689STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000690#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000691
Denis Vlasenkobd852072007-03-19 14:43:38 +0000692STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000693STATIC_SUN void sun_delete_partition(int i);
694STATIC_SUN void sun_change_sysid(int i, int sys);
695STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000696STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000697#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000698STATIC_SUN void sun_set_alt_cyl(void);
699STATIC_SUN void sun_set_ncyl(int cyl);
700STATIC_SUN void sun_set_xcyl(void);
701STATIC_SUN void sun_set_ilfact(void);
702STATIC_SUN void sun_set_rspeed(void);
703STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000704#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000705STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
706STATIC_SUN void verify_sun(void);
707STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000708#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000709
Denis Vlasenko9764d692008-07-09 21:20:50 +0000710
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200711static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000712read4_little_endian(const unsigned char *cp)
713{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200714 uint32_t v;
715 move_from_unaligned32(v, cp);
716 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000717}
718
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200719static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000720get_start_sect(const struct partition *p)
721{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000722 return read4_little_endian(p->start4);
723}
724
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200725static sector_t
726get_nr_sects(const struct partition *p)
727{
728 return read4_little_endian(p->size4);
729}
730
Denis Vlasenko834410a2006-11-29 12:00:28 +0000731#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200732/* start_sect and nr_sects are stored little endian on all machines */
733/* moreover, they are not aligned correctly */
734static inline_if_little_endian void
735store4_little_endian(unsigned char *cp, unsigned val)
736{
737 uint32_t v = SWAP_LE32(val);
738 move_to_unaligned32(cp, v);
739}
740
741static void
742set_start_sect(struct partition *p, unsigned start_sect)
743{
744 store4_little_endian(p->start4, start_sect);
745}
746
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000747static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000748set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000749{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000750 store4_little_endian(p->size4, nr_sects);
751}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000752#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000753
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000754/* Allocate a buffer and read a partition table sector */
755static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200756read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000757{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200758 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000759 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000760 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000761 /* xread would make us abort - bad for fdisk -l */
762 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000763 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000764#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000765 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000766#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000767 pe->part_table = pe->ext_pointer = NULL;
768}
769
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200770static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200771get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000772{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200773 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000774}
775
Denis Vlasenko834410a2006-11-29 12:00:28 +0000776#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000777/*
778 * Avoid warning about DOS partitions when no DOS partition was changed.
779 * Here a heuristic "is probably dos partition".
780 * We might also do the opposite and warn in all cases except
781 * for "is probably nondos partition".
782 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000783#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000784static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000785is_dos_partition(int t)
786{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000787 return (t == 1 || t == 4 || t == 6 ||
788 t == 0x0b || t == 0x0c || t == 0x0e ||
789 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
790 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
791 t == 0xc1 || t == 0xc4 || t == 0xc6);
792}
Denis Vlasenko89398812008-01-25 20:18:46 +0000793#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000794
795static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000796menu(void)
797{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000798 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000799 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000800 puts("a\ttoggle a read only flag"); /* sun */
801 puts("b\tedit bsd disklabel");
802 puts("c\ttoggle the mountable flag"); /* sun */
803 puts("d\tdelete a partition");
804 puts("l\tlist known partition types");
805 puts("n\tadd a new partition");
806 puts("o\tcreate a new empty DOS partition table");
807 puts("p\tprint the partition table");
808 puts("q\tquit without saving changes");
809 puts("s\tcreate a new empty Sun disklabel"); /* sun */
810 puts("t\tchange a partition's system id");
811 puts("u\tchange display/entry units");
812 puts("v\tverify the partition table");
813 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000814#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000815 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000816#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000817 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000818 puts("a\tselect bootable partition"); /* sgi flavour */
819 puts("b\tedit bootfile entry"); /* sgi */
820 puts("c\tselect sgi swap partition"); /* sgi flavour */
821 puts("d\tdelete a partition");
822 puts("l\tlist known partition types");
823 puts("n\tadd a new partition");
824 puts("o\tcreate a new empty DOS partition table");
825 puts("p\tprint the partition table");
826 puts("q\tquit without saving changes");
827 puts("s\tcreate a new empty Sun disklabel"); /* sun */
828 puts("t\tchange a partition's system id");
829 puts("u\tchange display/entry units");
830 puts("v\tverify the partition table");
831 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000832 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000833 puts("o\tcreate a new empty DOS partition table");
834 puts("q\tquit without saving changes");
835 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000836 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000837 puts("a\ttoggle a bootable flag");
838 puts("b\tedit bsd disklabel");
839 puts("c\ttoggle the dos compatibility flag");
840 puts("d\tdelete a partition");
841 puts("l\tlist known partition types");
842 puts("n\tadd a new partition");
843 puts("o\tcreate a new empty DOS partition table");
844 puts("p\tprint the partition table");
845 puts("q\tquit without saving changes");
846 puts("s\tcreate a new empty Sun disklabel"); /* sun */
847 puts("t\tchange a partition's system id");
848 puts("u\tchange display/entry units");
849 puts("v\tverify the partition table");
850 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000851#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000852 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000853#endif
854 }
855}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000856#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000857
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000858
Denis Vlasenko834410a2006-11-29 12:00:28 +0000859#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000861xmenu(void)
862{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000863 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000864 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000865 puts("a\tchange number of alternate cylinders"); /*sun*/
866 puts("c\tchange number of cylinders");
867 puts("d\tprint the raw data in the partition table");
868 puts("e\tchange number of extra sectors per cylinder");/*sun*/
869 puts("h\tchange number of heads");
870 puts("i\tchange interleave factor"); /*sun*/
871 puts("o\tchange rotation speed (rpm)"); /*sun*/
872 puts("p\tprint the partition table");
873 puts("q\tquit without saving changes");
874 puts("r\treturn to main menu");
875 puts("s\tchange number of sectors/track");
876 puts("v\tverify the partition table");
877 puts("w\twrite table to disk and exit");
878 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000879 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000880 puts("b\tmove beginning of data in a partition"); /* !sun */
881 puts("c\tchange number of cylinders");
882 puts("d\tprint the raw data in the partition table");
883 puts("e\tlist extended partitions"); /* !sun */
884 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
885 puts("h\tchange number of heads");
886 puts("p\tprint the partition table");
887 puts("q\tquit without saving changes");
888 puts("r\treturn to main menu");
889 puts("s\tchange number of sectors/track");
890 puts("v\tverify the partition table");
891 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000892 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000893 puts("b\tmove beginning of data in a partition"); /* !sun */
894 puts("c\tchange number of cylinders");
895 puts("d\tprint the raw data in the partition table");
896 puts("e\tlist extended partitions"); /* !sun */
897 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
898 puts("h\tchange number of heads");
899 puts("p\tprint the partition table");
900 puts("q\tquit without saving changes");
901 puts("r\treturn to main menu");
902 puts("s\tchange number of sectors/track");
903 puts("v\tverify the partition table");
904 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000905 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000906 puts("b\tmove beginning of data in a partition"); /* !sun */
907 puts("c\tchange number of cylinders");
908 puts("d\tprint the raw data in the partition table");
909 puts("e\tlist extended partitions"); /* !sun */
910 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000911#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000912 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000913#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000914 puts("h\tchange number of heads");
915 puts("p\tprint the partition table");
916 puts("q\tquit without saving changes");
917 puts("r\treturn to main menu");
918 puts("s\tchange number of sectors/track");
919 puts("v\tverify the partition table");
920 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000921 }
922}
923#endif /* ADVANCED mode */
924
Denis Vlasenko834410a2006-11-29 12:00:28 +0000925#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000926static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000927get_sys_types(void)
928{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000930 LABEL_IS_SUN ? sun_sys_types :
931 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000932 i386_sys_types);
933}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000934#else
935#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200936#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000937
Denis Vlasenkobd852072007-03-19 14:43:38 +0000938static const char *
939partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000940{
941 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000942 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000943
Denis Vlasenkobd852072007-03-19 14:43:38 +0000944 for (i = 0; types[i]; i++)
945 if ((unsigned char)types[i][0] == type)
946 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947
Denis Vlasenkobd852072007-03-19 14:43:38 +0000948 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000949}
950
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200951static int
952is_cleared_partition(const struct partition *p)
953{
954 /* We consider partition "cleared" only if it has only zeros */
955 const char *cp = (const char *)p;
956 int cnt = sizeof(*p);
957 char bits = 0;
958 while (--cnt >= 0)
959 bits |= *cp++;
960 return (bits == 0);
961}
962
963static void
964clear_partition(struct partition *p)
965{
966 if (p)
967 memset(p, 0, sizeof(*p));
968}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000969
Denis Vlasenko834410a2006-11-29 12:00:28 +0000970#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000971static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000972get_sysid(int i)
973{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000974 return LABEL_IS_SUN ? sunlabel->infos[i].id :
975 (LABEL_IS_SGI ? sgi_get_sysid(i) :
976 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000977}
978
Denis Vlasenkobd852072007-03-19 14:43:38 +0000979static void
980list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000981{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000982 enum { COLS = 3 };
983
984 unsigned last[COLS];
985 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000986 int i;
987
Denis Vlasenko9764d692008-07-09 21:20:50 +0000988 for (size = 0; sys[size]; size++)
989 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000991 done = 0;
992 for (i = COLS-1; i >= 0; i--) {
993 done += (size + i - done) / (i + 1);
994 last[COLS-1 - i] = done;
995 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000996
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000997 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000998 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000999 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001000 (unsigned char)sys[next][0],
1001 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001003 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001004 i = 0;
1005 next = ++done;
1006 }
1007 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001008 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001009}
1010
Denys Vlasenkod958e902010-04-06 02:32:26 +02001011#define set_hsc(h, s, c, sector) do \
1012{ \
1013 s = sector % g_sectors + 1; \
1014 sector /= g_sectors; \
1015 h = sector % g_heads; \
1016 sector /= g_heads; \
1017 c = sector & 0xff; \
1018 s |= (sector >> 2) & 0xc0; \
1019} while (0)
1020
1021static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1022{
1023 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1024 start = g_heads * g_sectors * 1024 - 1;
1025 set_hsc(p->head, p->sector, p->cyl, start);
1026
1027 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1028 stop = g_heads * g_sectors * 1024 - 1;
1029 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1030}
1031
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001033set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001034{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001035 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001036 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001037
1038 if (doext) {
1039 p = ptes[i].ext_pointer;
1040 offset = extended_offset;
1041 } else {
1042 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001043 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001044 }
1045 p->boot_ind = 0;
1046 p->sys_ind = sysid;
1047 set_start_sect(p, start - offset);
1048 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001049 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050 ptes[i].changed = 1;
1051}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001052#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001053
1054static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001055warn_geometry(void)
1056{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001057 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001059
Denis Vlasenkobd852072007-03-19 14:43:38 +00001060 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001061 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001062 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001063 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001064 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001065 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001066 printf(" cylinders");
1067 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001068#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001069 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001070#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001071 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001072 return 1;
1073}
1074
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001075static void
1076update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001077{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001078 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079
1080 if (display_in_cyl_units && cyl_units)
1081 units_per_sector = cyl_units;
1082 else
1083 units_per_sector = 1; /* in sectors */
1084}
1085
Denis Vlasenko834410a2006-11-29 12:00:28 +00001086#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001087static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001088warn_cylinders(void)
1089{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001090 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001091 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001092"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001093"There is nothing wrong with that, but this is larger than 1024,\n"
1094"and could in certain setups cause problems with:\n"
1095"1) software that runs at boot time (e.g., old versions of LILO)\n"
1096"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001097" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001098 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001099}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001100#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001101
1102static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001103read_extended(int ext)
1104{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001105 int i;
1106 struct pte *pex;
1107 struct partition *p, *q;
1108
1109 ext_index = ext;
1110 pex = &ptes[ext];
1111 pex->ext_pointer = pex->part_table;
1112
1113 p = pex->part_table;
1114 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001115 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116 return;
1117 }
1118
Rob Landleyb73451d2006-02-24 16:29:00 +00001119 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001120 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001121
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001122 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001123 /* This is not a Linux restriction, but
1124 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001125 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001126 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001127#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001128 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001129 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001130 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001131#endif
1132 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001133 return;
1134 }
1135
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001136 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001137
1138 if (!extended_offset)
1139 extended_offset = get_start_sect(p);
1140
1141 q = p = pt_offset(pe->sectorbuffer, 0);
1142 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001143 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001144 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001145 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001146 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001147 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 else
1149 pe->ext_pointer = p;
1150 } else if (p->sys_ind) {
1151 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001152 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001153 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001154 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001155 else
1156 pe->part_table = p;
1157 }
1158 }
1159
1160 /* very strange code here... */
1161 if (!pe->part_table) {
1162 if (q != pe->ext_pointer)
1163 pe->part_table = q;
1164 else
1165 pe->part_table = q + 1;
1166 }
1167 if (!pe->ext_pointer) {
1168 if (q != pe->part_table)
1169 pe->ext_pointer = q;
1170 else
1171 pe->ext_pointer = q + 1;
1172 }
1173
1174 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001175 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001176 }
1177
Denis Vlasenko834410a2006-11-29 12:00:28 +00001178#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001179 /* remove empty links */
1180 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001181 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001182 struct pte *pe = &ptes[i];
1183
Denis Vlasenkobd852072007-03-19 14:43:38 +00001184 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001185 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001186 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001187 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188 delete_partition(i);
1189 goto remove; /* numbering changed */
1190 }
1191 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001192#endif
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 +00001196static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001197create_doslabel(void)
1198{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001199 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001200
Denis Vlasenko4437d192008-04-17 00:12:10 +00001201 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001202#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001203 possibly_osf_label = 0;
1204#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001205 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001207 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208 write_part_table_flag(MBRbuffer);
1209 extended_offset = 0;
1210 set_all_unchanged();
1211 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001212 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001214#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215
1216static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001217get_sectorsize(void)
1218{
Rob Landley736e5252006-02-25 03:36:00 +00001219 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001220 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001221 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222 sector_size = arg;
1223 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001224 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001225 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1226 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001227 }
1228}
1229
Rob Landley88621d72006-08-29 19:41:06 +00001230static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001231get_kernel_geometry(void)
1232{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001233 struct hd_geometry geometry;
1234
Denis Vlasenko4437d192008-04-17 00:12:10 +00001235 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236 kern_heads = geometry.heads;
1237 kern_sectors = geometry.sectors;
1238 /* never use geometry.cylinders - it is truncated */
1239 }
1240}
1241
1242static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001243get_partition_table_geometry(void)
1244{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001245 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001246 struct partition *p;
1247 int i, h, s, hh, ss;
1248 int first = 1;
1249 int bad = 0;
1250
Eric Andersen3496fdc2006-01-30 23:09:20 +00001251 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 return;
1253
1254 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001255 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001256 p = pt_offset(bufp, i);
1257 if (p->sys_ind != 0) {
1258 h = p->end_head + 1;
1259 s = (p->end_sector & 077);
1260 if (first) {
1261 hh = h;
1262 ss = s;
1263 first = 0;
1264 } else if (hh != h || ss != s)
1265 bad = 1;
1266 }
1267 }
1268
1269 if (!first && !bad) {
1270 pt_heads = hh;
1271 pt_sectors = ss;
1272 }
1273}
1274
Rob Landleyb73451d2006-02-24 16:29:00 +00001275static void
1276get_geometry(void)
1277{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001278 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279
1280 get_sectorsize();
1281 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001282#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001283 guess_device_type();
1284#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001285 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286 kern_heads = kern_sectors = 0;
1287 pt_heads = pt_sectors = 0;
1288
1289 get_kernel_geometry();
1290 get_partition_table_geometry();
1291
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001292 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 pt_heads ? pt_heads :
1294 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001295 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296 pt_sectors ? pt_sectors :
1297 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001298 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001299
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 sector_offset = 1;
1301 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001302 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001304 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1305 if (!g_cylinders)
1306 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001307}
1308
1309/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001310 * Opens disk_device and optionally reads MBR.
1311 * FIXME: document what each 'what' value will do!
1312 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1314 * 0: found or created label
1315 * 1: I/O error
1316 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001317#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1318static int get_boot(enum action what)
1319#else
1320static int get_boot(void)
1321#define get_boot(what) get_boot()
1322#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001323{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001324 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001326 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 for (i = 0; i < 4; i++) {
1328 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329 pe->part_table = pt_offset(MBRbuffer, i);
1330 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001331 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001332 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001333#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001334 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001335#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001336 }
1337
Denis Vlasenko834410a2006-11-29 12:00:28 +00001338#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001339// ALERT! highly idiotic design!
1340// We end up here when we call get_boot() recursively
1341// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1342// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1343// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1344// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001345 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001346 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347
Denis Vlasenko4437d192008-04-17 00:12:10 +00001348 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1349
Denis Vlasenkobd852072007-03-19 14:43:38 +00001350 if (fd < 0) {
1351 fd = open(disk_device, O_RDONLY);
1352 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001353 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001354 return 1;
1355 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001356 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001357 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001358 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001359 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001360 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001361 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001362 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001363 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001364 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 fdisk_fatal(unable_to_read);
1366 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001367#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001368 fd = open(disk_device, O_RDONLY);
1369 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001370 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001371 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001372 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001373 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001374 }
1375 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001376#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001377
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001378 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 update_units();
1380
Denis Vlasenko834410a2006-11-29 12:00:28 +00001381#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001382 if (check_sun_label())
1383 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001384#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001385#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 if (check_sgi_label())
1387 return 0;
1388#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001389#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390 if (check_aix_label())
1391 return 0;
1392#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001393#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001394 if (check_osf_label()) {
1395 possibly_osf_label = 1;
1396 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001397 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001398 return 0;
1399 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001400 printf("This disk has both DOS and BSD magic.\n"
1401 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402 }
1403#endif
1404
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001405#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001406 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001407 return -1;
1408#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001409 if (!valid_part_table_flag(MBRbuffer)) {
1410 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001411 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001412 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001413 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001415 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416#else
1417 create_doslabel();
1418#endif
1419 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001421 /* TRY_ONLY: */
1422 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001423 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001424 created_table:
1425#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001426
Denis Vlasenko4437d192008-04-17 00:12:10 +00001427
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001428 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429 warn_geometry();
1430
1431 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001432 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001433 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001434 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001435 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001436 else
1437 read_extended(i);
1438 }
1439 }
1440
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001441 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001442 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001443 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001444 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001445 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001446 pe->sectorbuffer[510],
1447 pe->sectorbuffer[511],
1448 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001449 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001450 }
1451 }
1452
1453 return 0;
1454}
1455
Denis Vlasenko834410a2006-11-29 12:00:28 +00001456#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001457/*
1458 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1459 * If the user hits Enter, DFLT is returned.
1460 * Answers like +10 are interpreted as offsets from BASE.
1461 *
1462 * There is no default if DFLT is not between LOW and HIGH.
1463 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001464static sector_t
1465read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001466{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001467 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001468 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001469 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001470
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001471 if (dflt < low || dflt > high) {
1472 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001473 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001474 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001475
1476 while (1) {
1477 int use_default = default_ok;
1478
1479 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001480 do {
1481 printf(fmt, mesg, low, high, dflt);
1482 read_maybe_empty("");
1483 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1484 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001485
Eric Andersen84bdea82004-05-19 10:49:17 +00001486 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001487 int minus = (*line_ptr == '-');
1488 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001489
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001490 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001491
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001492 /* (1) if 2nd char is digit, use_default = 0.
1493 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001494 while (isdigit(*++line_ptr))
1495 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001496
Rob Landleyb73451d2006-02-24 16:29:00 +00001497 switch (*line_ptr) {
1498 case 'c':
1499 case 'C':
1500 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001501 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001502 break;
1503 case 'K':
1504 absolute = 1024;
1505 break;
1506 case 'k':
1507 absolute = 1000;
1508 break;
1509 case 'm':
1510 case 'M':
1511 absolute = 1000000;
1512 break;
1513 case 'g':
1514 case 'G':
1515 absolute = 1000000000;
1516 break;
1517 default:
1518 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001519 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001520 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001521 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001522 unsigned long unit;
1523
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001524 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001525 unit = sector_size * units_per_sector;
1526 bytes += unit/2; /* round */
1527 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001528 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001529 }
1530 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001531 value = -value;
1532 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001533 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001534 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001535 while (isdigit(*line_ptr)) {
1536 line_ptr++;
1537 use_default = 0;
1538 }
1539 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001540 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001541 value = dflt;
1542 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001543 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001544 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001545 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001546 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001547 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001548 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001549}
1550
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001551static unsigned
1552get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001553{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001555 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001556
Denis Vlasenkobd852072007-03-19 14:43:38 +00001557 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001558 pe = &ptes[i];
1559
1560 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001561 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1562 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1563 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1564 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001565 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001566 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001567 }
1568 return i;
1569}
1570
1571static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001572get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001573{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001574 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001575 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001576
1577 for (i = 0; i < max; i++) {
1578 struct pte *pe = &ptes[i];
1579 struct partition *p = pe->part_table;
1580
1581 if (p && !is_cleared_partition(p)) {
1582 if (pno >= 0)
1583 goto not_unique;
1584 pno = i;
1585 }
1586 }
1587 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001588 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 return pno;
1590 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001591 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001592 return -1;
1593
1594 not_unique:
1595 return get_partition(warn, max);
1596}
1597
1598static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001599get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001600{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001601 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001602 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603
1604 for (i = 0; i < max; i++) {
1605 struct pte *pe = &ptes[i];
1606 struct partition *p = pe->part_table;
1607
1608 if (p && is_cleared_partition(p)) {
1609 if (pno >= 0)
1610 goto not_unique;
1611 pno = i;
1612 }
1613 }
1614 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001615 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001616 return pno;
1617 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001618 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001619 return -1;
1620
1621 not_unique:
1622 return get_partition(warn, max);
1623}
1624
1625
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001626static void
1627change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001628{
1629 display_in_cyl_units = !display_in_cyl_units;
1630 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001631 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001632 str_units(PLURAL));
1633}
1634
1635static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001636toggle_active(int i)
1637{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001638 struct pte *pe = &ptes[i];
1639 struct partition *p = pe->part_table;
1640
Rob Landleyb73451d2006-02-24 16:29:00 +00001641 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001642 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001643 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1644 pe->changed = 1;
1645}
1646
1647static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001648toggle_dos_compatibility_flag(void)
1649{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001650 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001651 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001652 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001653 printf("DOS Compatibility flag is set\n");
1654 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001655 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001656 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001657 }
1658}
1659
1660static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001661delete_partition(int i)
1662{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 struct pte *pe = &ptes[i];
1664 struct partition *p = pe->part_table;
1665 struct partition *q = pe->ext_pointer;
1666
1667/* Note that for the fifth partition (i == 4) we don't actually
1668 * decrement partitions.
1669 */
1670
1671 if (warn_geometry())
1672 return; /* C/H/S not set */
1673 pe->changed = 1;
1674
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001675 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676 sun_delete_partition(i);
1677 return;
1678 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001679 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001680 sgi_delete_partition(i);
1681 return;
1682 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001683
1684 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001685 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001686 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687 ptes[ext_index].ext_pointer = NULL;
1688 extended_offset = 0;
1689 }
1690 clear_partition(p);
1691 return;
1692 }
1693
1694 if (!q->sys_ind && i > 4) {
1695 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001696 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001697 --i;
1698 clear_partition(ptes[i].ext_pointer);
1699 ptes[i].changed = 1;
1700 } else {
1701 /* not the last one - further ones will be moved down */
1702 if (i > 4) {
1703 /* delete this link in the chain */
1704 p = ptes[i-1].ext_pointer;
1705 *p = *q;
1706 set_start_sect(p, get_start_sect(q));
1707 set_nr_sects(p, get_nr_sects(q));
1708 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001709 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001710 /* the first logical in a longer chain */
1711 pe = &ptes[5];
1712
1713 if (pe->part_table) /* prevent SEGFAULT */
1714 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001715 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001716 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001717 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001718 pe->changed = 1;
1719 }
1720
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001721 if (g_partitions > 5) {
1722 g_partitions--;
1723 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001724 ptes[i] = ptes[i+1];
1725 i++;
1726 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001727 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001728 /* the only logical: clear only */
1729 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001730 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731 }
1732}
1733
1734static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001735change_sysid(void)
1736{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001737 int i, sys, origsys;
1738 struct partition *p;
1739
Eric Andersen040f4402003-07-30 08:40:37 +00001740 /* If sgi_label then don't use get_existing_partition,
1741 let the user select a partition, since get_existing_partition()
1742 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001743 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001744 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001745 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001746 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001747 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748 if (i == -1)
1749 return;
1750 p = ptes[i].part_table;
1751 origsys = sys = get_sysid(i);
1752
1753 /* if changing types T to 0 is allowed, then
1754 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001755 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001756 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001757 return;
1758 }
1759 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001760 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001762 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001763 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001764 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001765 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001766 /* break; */
1767 }
1768
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001769 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001770 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001771 printf("You cannot change a partition into"
1772 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001773 break;
1774 }
1775 }
1776
1777 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001778#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001779 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001780 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001781 "as Whole disk (5),\n"
1782 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001783 "even Linux likes it\n\n");
1784#endif
1785#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001786 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001787 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001788 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001789 (i == 8 && sys != 0)
1790 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001791 ) {
1792 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001793 "as volume header (0),\nand "
1794 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001795 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001796 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001797#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798 if (sys == origsys)
1799 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001800 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001802 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001803 sgi_change_sysid(i, sys);
1804 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001806
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001807 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001808 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001809 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001810 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001811 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1812 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 break;
1814 }
1815 }
1816}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001817#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001818
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819
Denis Vlasenko28703012006-12-19 20:32:02 +00001820/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001821 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1822 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1823 * Lubkin Oct. 1991). */
1824
Rob Landleyb73451d2006-02-24 16:29:00 +00001825static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001826linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001827{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001828 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829
1830 *c = ls / spc;
1831 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001832 *h = ls / g_sectors;
1833 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001834}
1835
Rob Landleyb73451d2006-02-24 16:29:00 +00001836static void
1837check_consistency(const struct partition *p, int partition)
1838{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001839 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1840 unsigned pec, peh, pes; /* physical ending c, h, s */
1841 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1842 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001843
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001844 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001845 return; /* do not check extended partitions */
1846
1847/* physical beginning c, h, s */
1848 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1849 pbh = p->head;
1850 pbs = p->sector & 0x3f;
1851
1852/* physical ending c, h, s */
1853 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1854 peh = p->end_head;
1855 pes = p->end_sector & 0x3f;
1856
1857/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001858 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001859
1860/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001861 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001862
1863/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001864 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001865 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001866 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001867 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1868 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001869 }
1870
1871/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001872 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001873 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001874 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001875 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1876 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001877 }
1878
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001879/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001880 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001881 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001882 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001883 }
1884}
1885
1886static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001887list_disk_geometry(void)
1888{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001889 ullong bytes = ((ullong)total_number_of_sectors << 9);
1890 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001891
1892 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001893 printf("\nDisk %s: %lu MB, %llu bytes\n",
1894 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001895 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001896 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1897 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1898 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001899 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001900 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001901 printf(", total %"SECT_FMT"u sectors",
1902 total_number_of_sectors / (sector_size/512));
1903 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1904 str_units(PLURAL),
1905 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001906}
1907
1908/*
1909 * Check whether partition entries are ordered by their starting positions.
1910 * Return 0 if OK. Return i if partition i should have been earlier.
1911 * Two separate checks: primary and logical partitions.
1912 */
1913static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001914wrong_p_order(int *prev)
1915{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001916 const struct pte *pe;
1917 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001918 sector_t last_p_start_pos = 0, p_start_pos;
1919 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001920
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001921 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922 if (i == 4) {
1923 last_i = 4;
1924 last_p_start_pos = 0;
1925 }
1926 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001927 p = pe->part_table;
1928 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001929 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930
1931 if (last_p_start_pos > p_start_pos) {
1932 if (prev)
1933 *prev = last_i;
1934 return i;
1935 }
1936
1937 last_p_start_pos = p_start_pos;
1938 last_i = i;
1939 }
1940 }
1941 return 0;
1942}
1943
Denis Vlasenko834410a2006-11-29 12:00:28 +00001944#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001945/*
1946 * Fix the chain of logicals.
1947 * extended_offset is unchanged, the set of sectors used is unchanged
1948 * The chain is sorted so that sectors increase, and so that
1949 * starting sectors increase.
1950 *
1951 * After this it may still be that cfdisk doesnt like the table.
1952 * (This is because cfdisk considers expanded parts, from link to
1953 * end of partition, and these may still overlap.)
1954 * Now
1955 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1956 * may help.
1957 */
1958static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001959fix_chain_of_logicals(void)
1960{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001961 int j, oj, ojj, sj, sjj;
1962 struct partition *pj,*pjj,tmp;
1963
1964 /* Stage 1: sort sectors but leave sector of part 4 */
1965 /* (Its sector is the global extended_offset.) */
1966 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001967 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001968 oj = ptes[j].offset_from_dev_start;
1969 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001970 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02001971 ptes[j].offset_from_dev_start = ojj;
1972 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001973 pj = ptes[j].part_table;
1974 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1975 pjj = ptes[j+1].part_table;
1976 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1977 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001978 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001979 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001980 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001981 goto stage1;
1982 }
1983 }
1984
1985 /* Stage 2: sort starting sectors */
1986 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001987 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001988 pj = ptes[j].part_table;
1989 pjj = ptes[j+1].part_table;
1990 sj = get_start_sect(pj);
1991 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001992 oj = ptes[j].offset_from_dev_start;
1993 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001994 if (oj+sj > ojj+sjj) {
1995 tmp = *pj;
1996 *pj = *pjj;
1997 *pjj = tmp;
1998 set_start_sect(pj, ojj+sjj-oj);
1999 set_start_sect(pjj, oj+sj-ojj);
2000 goto stage2;
2001 }
2002 }
2003
2004 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002005 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006 ptes[j].changed = 1;
2007}
2008
2009
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002011fix_partition_table_order(void)
2012{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 struct pte *pei, *pek;
2014 int i,k;
2015
2016 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002017 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002018 return;
2019 }
2020
2021 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2022 /* partition i should have come earlier, move it */
2023 /* We have to move data in the MBR */
2024 struct partition *pi, *pk, *pe, pbuf;
2025 pei = &ptes[i];
2026 pek = &ptes[k];
2027
2028 pe = pei->ext_pointer;
2029 pei->ext_pointer = pek->ext_pointer;
2030 pek->ext_pointer = pe;
2031
2032 pi = pei->part_table;
2033 pk = pek->part_table;
2034
2035 memmove(&pbuf, pi, sizeof(struct partition));
2036 memmove(pi, pk, sizeof(struct partition));
2037 memmove(pk, &pbuf, sizeof(struct partition));
2038
2039 pei->changed = pek->changed = 1;
2040 }
2041
2042 if (i)
2043 fix_chain_of_logicals();
2044
2045 printf("Done.\n");
2046
2047}
2048#endif
2049
2050static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002051list_table(int xtra)
2052{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002054 int i, w;
2055
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002056 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002057 sun_list_table(xtra);
2058 return;
2059 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002060 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002061 sgi_list_table(xtra);
2062 return;
2063 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002064
2065 list_disk_geometry();
2066
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002067 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002068 xbsd_print_disklabel(xtra);
2069 return;
2070 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002071
2072 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2073 but if the device name ends in a digit, say /dev/foo1,
2074 then the partition is called /dev/foo1p3. */
2075 w = strlen(disk_device);
2076 if (w && isdigit(disk_device[w-1]))
2077 w++;
2078 if (w < 5)
2079 w = 5;
2080
Denis Vlasenkobd852072007-03-19 14:43:38 +00002081 // 1 12345678901 12345678901 12345678901 12
2082 printf("%*s Boot Start End Blocks Id System\n",
2083 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002084
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002085 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002086 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002087 sector_t psects;
2088 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002089 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090
2091 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002092 if (!p || is_cleared_partition(p))
2093 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002095 psects = get_nr_sects(p);
2096 pblocks = psects;
2097 podd = 0;
2098
2099 if (sector_size < 1024) {
2100 pblocks /= (1024 / sector_size);
2101 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002103 if (sector_size > 1024)
2104 pblocks *= (sector_size / 1024);
2105
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002106 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 +00002107 partname(disk_device, i+1, w+2),
2108 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2109 ? '*' : '?',
Denys Vlasenkod958e902010-04-06 02:32:26 +02002110 cround(get_partition_start_from_dev_start(pe)), /* start */
2111 cround(get_partition_start_from_dev_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002112 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002113 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002114 p->sys_ind, /* type id */
2115 partition_type(p->sys_ind)); /* type name */
2116
2117 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 }
2119
2120 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002121 /* partition table entries are not checked for correct order
2122 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002123 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002124 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002125 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126 }
2127}
2128
Denis Vlasenko834410a2006-11-29 12:00:28 +00002129#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002130static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002131x_list_table(int extend)
2132{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133 const struct pte *pe;
2134 const struct partition *p;
2135 int i;
2136
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002137 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002138 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002139 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002140 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002141 pe = &ptes[i];
2142 p = (extend ? pe->ext_pointer : pe->part_table);
2143 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002144 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 +00002145 i + 1, p->boot_ind, p->head,
2146 sector(p->sector),
2147 cylinder(p->sector, p->cyl), p->end_head,
2148 sector(p->end_sector),
2149 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002150 get_start_sect(p), get_nr_sects(p),
2151 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152 if (p->sys_ind)
2153 check_consistency(p, i);
2154 }
2155 }
2156}
2157#endif
2158
Denis Vlasenko834410a2006-11-29 12:00:28 +00002159#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002161fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002162{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002163 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164 const struct pte *pe = &ptes[0];
2165 const struct partition *p;
2166
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002167 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002168 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002169 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002170 first[i] = 0xffffffff;
2171 last[i] = 0;
2172 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002173 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002174 last[i] = first[i] + get_nr_sects(p) - 1;
2175 }
2176 }
2177}
2178
2179static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002180check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002181{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002182 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002183
2184 real_s = sector(s) - 1;
2185 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002186 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002187 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002188 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002189 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002190 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002191 n, h + 1, g_heads);
2192 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002193 printf("Partition %u: sector %u greater than "
2194 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002195 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002196 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2197 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002198 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002199 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2200 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002201}
2202
2203static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002204verify(void)
2205{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002206 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002207 sector_t total = 1;
2208 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 struct partition *p;
2210
2211 if (warn_geometry())
2212 return;
2213
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002214 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002215 verify_sun();
2216 return;
2217 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002218 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219 verify_sgi(1);
2220 return;
2221 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002222
2223 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002224 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 struct pte *pe = &ptes[i];
2226
2227 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002228 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002229 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002230 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002231 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002232 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002233 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2234 last[i]);
2235 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002236 for (j = 0; j < i; j++) {
2237 if ((first[i] >= first[j] && first[i] <= last[j])
2238 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002239 printf("Warning: partition %u overlaps "
2240 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002241 total += first[i] >= first[j] ?
2242 first[i] : first[j];
2243 total -= last[i] <= last[j] ?
2244 last[i] : last[j];
2245 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002246 }
2247 }
2248 }
2249
2250 if (extended_offset) {
2251 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002252 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 get_nr_sects(pex->part_table) - 1;
2254
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002255 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002256 total++;
2257 p = ptes[i].part_table;
2258 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002259 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002260 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002261 "is empty\n", i + 1);
2262 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002263 printf("Logical partition %u not entirely in "
2264 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002265 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002266 }
2267 }
2268
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002269 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002270 printf("Total allocated sectors %u greater than the maximum "
2271 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002272 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002273 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002274 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002275 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002276 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002277}
2278
2279static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002280add_partition(int n, int sys)
2281{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002282 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002283 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284 struct partition *p = ptes[n].part_table;
2285 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002286 sector_t limit, temp;
2287 sector_t start, stop = 0;
2288 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002289
2290 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002291 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002292 return;
2293 }
2294 fill_bounds(first, last);
2295 if (n < 4) {
2296 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002297 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002298 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002300 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002301 if (extended_offset) {
2302 first[ext_index] = extended_offset;
2303 last[ext_index] = get_start_sect(q) +
2304 get_nr_sects(q) - 1;
2305 }
2306 } else {
2307 start = extended_offset + sector_offset;
2308 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2309 }
2310 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002311 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002312 first[i] = (cround(first[i]) - 1) * units_per_sector;
2313
Denis Vlasenkobd852072007-03-19 14:43:38 +00002314 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002315 do {
2316 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002317 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002318 int lastplusoff;
2319
Denys Vlasenkod958e902010-04-06 02:32:26 +02002320 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002321 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002322 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323 if (start >= first[i] && start <= lastplusoff)
2324 start = lastplusoff + 1;
2325 }
2326 if (start > limit)
2327 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002328 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002329 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002330 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002331 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002332 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002333 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002334 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335
2336 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002337 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002338 if (display_in_cyl_units) {
2339 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002340 if (start < saved_start)
2341 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002342 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002343 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002344 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002345 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002346 if (n > 4) { /* NOT for fifth partition */
2347 struct pte *pe = &ptes[n];
2348
Denys Vlasenkod958e902010-04-06 02:32:26 +02002349 pe->offset_from_dev_start = start - sector_offset;
2350 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2351 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002352 if (sector_offset == 1)
2353 start++;
2354 }
2355 }
2356
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002357 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 struct pte *pe = &ptes[i];
2359
Denys Vlasenkod958e902010-04-06 02:32:26 +02002360 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2361 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362 if (start < first[i] && limit >= first[i])
2363 limit = first[i] - 1;
2364 }
2365 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002366 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002367 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002368 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369 return;
2370 }
2371 if (cround(start) == cround(limit)) {
2372 stop = limit;
2373 } else {
2374 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002375 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 str_units(SINGULAR));
Denys Vlasenkod958e902010-04-06 02:32:26 +02002377 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378 if (display_in_cyl_units) {
2379 stop = stop * units_per_sector - 1;
2380 if (stop >limit)
2381 stop = limit;
2382 }
2383 }
2384
2385 set_partition(n, 0, start, stop, sys);
2386 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002387 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002388
Rob Landleyb73451d2006-02-24 16:29:00 +00002389 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002390 struct pte *pe4 = &ptes[4];
2391 struct pte *pen = &ptes[n];
2392
2393 ext_index = n;
2394 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002395 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002396 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2398 pe4->ext_pointer = pe4->part_table + 1;
2399 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002400 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 }
2402}
2403
2404static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002405add_logical(void)
2406{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002407 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2408 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409
Rob Landley081e3842006-08-03 20:07:35 +00002410 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002411 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2412 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002413 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002414 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002415 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002416 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002417 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002418}
2419
2420static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002421new_partition(void)
2422{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002423 int i, free_primary = 0;
2424
2425 if (warn_geometry())
2426 return;
2427
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002428 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002429 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002430 return;
2431 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002432 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002433 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002434 return;
2435 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002436 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002437 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2438"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2439"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002440 return;
2441 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002442
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 for (i = 0; i < 4; i++)
2444 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002445
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002446 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002447 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002448 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002449 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002450
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 if (!free_primary) {
2452 if (extended_offset)
2453 add_logical();
2454 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002455 printf("You must delete some partition and add "
2456 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002458 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002459 snprintf(line, sizeof(line),
2460 "Command action\n"
2461 " %s\n"
2462 " p primary partition (1-4)\n",
2463 (extended_offset ?
2464 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002466 c = read_nonempty(line);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002467 if ((c | 0x20) == 'p') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468 i = get_nonexisting_partition(0, 4);
2469 if (i >= 0)
2470 add_partition(i, LINUX_NATIVE);
2471 return;
2472 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002473 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474 add_logical();
2475 return;
2476 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002477 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478 i = get_nonexisting_partition(0, 4);
2479 if (i >= 0)
2480 add_partition(i, EXTENDED);
2481 return;
2482 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002483 printf("Invalid partition number "
2484 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485 }
2486 }
2487}
2488
2489static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002490write_table(void)
2491{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 int i;
2493
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002494 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002495 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496 if (ptes[i].changed)
2497 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002498 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002499 struct pte *pe = &ptes[i];
2500
2501 if (pe->changed) {
2502 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002503 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002504 }
2505 }
2506 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002507 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002508 /* no test on change? the printf below might be mistaken */
2509 sgi_write_table();
2510 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002511 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 int needw = 0;
2513
Rob Landleyb73451d2006-02-24 16:29:00 +00002514 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002515 if (ptes[i].changed)
2516 needw = 1;
2517 if (needw)
2518 sun_write_table();
2519 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520
Denis Vlasenkobd852072007-03-19 14:43:38 +00002521 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002522 reread_partition_table(1);
2523}
2524
Rob Landleyb73451d2006-02-24 16:29:00 +00002525static void
2526reread_partition_table(int leave)
2527{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002528 int i;
2529
Denis Vlasenkobd852072007-03-19 14:43:38 +00002530 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002531 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002532 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002533 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002534 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002535 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002536#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002538 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002539 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002540 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002541 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002542#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543
2544 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002545 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002546 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002547 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548 }
2549}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002550#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551
Denis Vlasenko834410a2006-11-29 12:00:28 +00002552#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002553#define MAX_PER_LINE 16
2554static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002555print_buffer(char *pbuffer)
2556{
2557 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002558
2559 for (i = 0, l = 0; i < sector_size; i++, l++) {
2560 if (l == 0)
2561 printf("0x%03X:", i);
2562 printf(" %02X", (unsigned char) pbuffer[i]);
2563 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002564 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 l = -1;
2566 }
2567 }
2568 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002569 bb_putchar('\n');
2570 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002571}
2572
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002574print_raw(void)
2575{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002576 int i;
2577
Denis Vlasenkobd852072007-03-19 14:43:38 +00002578 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002579 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002580 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002581 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002582 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002584 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585}
2586
2587static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002588move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002589{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 struct pte *pe = &ptes[i];
2591 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002592 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593
2594 if (warn_geometry())
2595 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002596 nr_sects = get_nr_sects(p);
2597 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002598 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 return;
2600 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002601 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002602 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2603 if (new != first) {
2604 sector_t new_relative = new - pe->offset_from_dev_start;
2605 nr_sects += (get_start_sect(p) - new_relative);
2606 set_start_sect(p, new_relative);
2607 set_nr_sects(p, nr_sects);
2608 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2609 if ((line_ptr[0] | 0x20) == 'y')
2610 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 pe->changed = 1;
2612 }
2613}
2614
2615static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002616xselect(void)
2617{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 char c;
2619
Rob Landleyb73451d2006-02-24 16:29:00 +00002620 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002621 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002622 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 switch (c) {
2624 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 break;
2628 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002629 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002630 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002631 break;
2632 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002633 user_cylinders = g_cylinders =
2634 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002635 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002637 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002638 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002639 warn_cylinders();
2640 break;
2641 case 'd':
2642 print_raw();
2643 break;
2644 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002645 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002646 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002647 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002648 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002649 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002650 x_list_table(1);
2651 break;
2652 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002653 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002654 fix_partition_table_order();
2655 break;
2656 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002657#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002658 create_sgilabel();
2659#endif
2660 break;
2661 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002662 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 update_units();
2664 break;
2665 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002666 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002667 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002668 break;
2669 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002670 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002671 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002672 break;
2673 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002674 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002675 list_table(1);
2676 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002677 x_list_table(0);
2678 break;
2679 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002680 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002681 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002682 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002683 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002684 case 'r':
2685 return;
2686 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002687 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002689 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002690 printf("Warning: setting sector offset for DOS "
2691 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 }
2693 update_units();
2694 break;
2695 case 'v':
2696 verify();
2697 break;
2698 case 'w':
2699 write_table(); /* does not return */
2700 break;
2701 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002702 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002703 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002704 break;
2705 default:
2706 xmenu();
2707 }
2708 }
2709}
2710#endif /* ADVANCED mode */
2711
2712static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002713is_ide_cdrom_or_tape(const char *device)
2714{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002715 FILE *procf;
2716 char buf[100];
2717 struct stat statbuf;
2718 int is_ide = 0;
2719
2720 /* No device was given explicitly, and we are trying some
2721 likely things. But opening /dev/hdc may produce errors like
2722 "hdc: tray open or drive not ready"
2723 if it happens to be a CD-ROM drive. It even happens that
2724 the process hangs on the attempt to read a music CD.
2725 So try to be careful. This only works since 2.1.73. */
2726
2727 if (strncmp("/dev/hd", device, 7))
2728 return 0;
2729
2730 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002731 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002732 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2733 is_ide = (!strncmp(buf, "cdrom", 5) ||
2734 !strncmp(buf, "tape", 4));
2735 else
2736 /* Now when this proc file does not exist, skip the
2737 device when it is read-only. */
2738 if (stat(device, &statbuf) == 0)
2739 is_ide = ((statbuf.st_mode & 0222) == 0);
2740
2741 if (procf)
2742 fclose(procf);
2743 return is_ide;
2744}
2745
Rob Landley5527b912006-02-25 03:46:10 +00002746
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002747static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002748open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002749{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 int gb;
2751
2752 disk_device = device;
2753 if (setjmp(listingbuf))
2754 return;
2755 if (!user_specified)
2756 if (is_ide_cdrom_or_tape(device))
2757 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002758
2759 /* Open disk_device, save file descriptor to dev_fd */
2760 errno = 0;
2761 gb = get_boot(TRY_ONLY);
2762 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002763 /* Ignore other errors, since we try IDE
2764 and SCSI hard disks which may not be
2765 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002766 if (user_specified || errno == EACCES)
2767 bb_perror_msg("can't open '%s'", device);
2768 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002769 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002770
2771 if (gb < 0) { /* no DOS signature */
2772 list_disk_geometry();
2773 if (LABEL_IS_AIX)
2774 goto ret;
2775#if ENABLE_FEATURE_OSF_LABEL
2776 if (bsd_trydev(device) < 0)
2777#endif
2778 printf("Disk %s doesn't contain a valid "
2779 "partition table\n", device);
2780 } else {
2781 list_table(0);
2782#if ENABLE_FEATURE_FDISK_WRITABLE
2783 if (!LABEL_IS_SUN && g_partitions > 4) {
2784 delete_partition(ext_index);
2785 }
2786#endif
2787 }
2788 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002789 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002790}
2791
2792/* for fdisk -l: try all things in /proc/partitions
2793 that look like a partition name (do not end in a digit) */
2794static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002795list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002796{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002797 FILE *procpt;
2798 char line[100], ptname[100], devname[120], *s;
2799 int ma, mi, sz;
2800
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002801 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802
2803 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002804 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002805 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002807 for (s = ptname; *s; s++)
Denys Vlasenkobf1d3472010-02-24 08:13:30 +01002808 continue;
2809 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2810 if (s[-1] >= '1' && s[-1] <= '9')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002812 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002813 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002814 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002815#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002816 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002817#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002818}
2819
Denis Vlasenko834410a2006-11-29 12:00:28 +00002820#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002821static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002822unknown_command(int c)
2823{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002824 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002826#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002827
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002828int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002829int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002830{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002831 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 * fdisk -v
2834 * fdisk -l [-b sectorsize] [-u] device ...
2835 * fdisk -s [partition] ...
2836 * fdisk [-b sectorsize] [-u] device
2837 *
2838 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002839 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002840 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002841
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002842 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002843
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002844 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002845 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002846 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002847 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002848 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00002849 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02002850 * so cannot be combined with multiple disks,
2851 * and the same goes for the C/H/S options.
2852 */
2853 if (sector_size < 512
2854 || sector_size > 0x10000
2855 || (sector_size & (sector_size-1)) /* not power of 2 */
2856 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002857 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02002858 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859 sector_offset = 2;
2860 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002862 if (user_heads <= 0 || user_heads >= 256)
2863 user_heads = 0;
2864 if (user_sectors <= 0 || user_sectors >= 64)
2865 user_sectors = 0;
2866 if (opt & OPT_u)
2867 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868
Denis Vlasenko834410a2006-11-29 12:00:28 +00002869#if ENABLE_FEATURE_FDISK_WRITABLE
2870 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002871 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002872#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002873 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002875 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002876 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002877 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002879 /* we don't have device names, */
2880 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002881 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882 }
2883 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002884#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002886#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002887
Denis Vlasenko834410a2006-11-29 12:00:28 +00002888#if ENABLE_FEATURE_FDISK_BLKSIZE
2889 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002890 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002891
2892 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002893 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002894 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002895 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002896 unsigned long long size;
2897 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002898 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002899 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002900 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002901 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002903 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002904 }
2905 return 0;
2906 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002907#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002908
Denis Vlasenko834410a2006-11-29 12:00:28 +00002909#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002910 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002911 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002912
Denis Vlasenko834410a2006-11-29 12:00:28 +00002913 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002914 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002916 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002918 printf("Detected an OSF/1 disklabel on %s, entering "
2919 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002920 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002921 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002922 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923 /* If we return we may want to make an empty DOS label? */
2924 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925
2926 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002927 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002928 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002929 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 switch (c) {
2931 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002933 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002935 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002937 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002939 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 else
2941 unknown_command(c);
2942 break;
2943 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002945 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002946 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002947 if (read_maybe_empty("Please enter the name of the "
2948 "new boot file: ") == '\n')
2949 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950 else
2951 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002952 }
2953#if ENABLE_FEATURE_OSF_LABEL
2954 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002955 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002956#endif
2957 break;
2958 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002959 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002961 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002962 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002963 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002964 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002965 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002966 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002967 else
2968 unknown_command(c);
2969 break;
2970 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002971 {
Eric Andersen040f4402003-07-30 08:40:37 +00002972 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002973 /* If sgi_label then don't use get_existing_partition,
2974 let the user select a partition, since
2975 get_existing_partition() only works for Linux-like
2976 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002977 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002978 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002979 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002980 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002981 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002982 if (j >= 0)
2983 delete_partition(j);
2984 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 break;
2986 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002987 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002988 create_sgiinfo();
2989 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990 unknown_command(c);
2991 case 'l':
2992 list_types(get_sys_types());
2993 break;
2994 case 'm':
2995 menu();
2996 break;
2997 case 'n':
2998 new_partition();
2999 break;
3000 case 'o':
3001 create_doslabel();
3002 break;
3003 case 'p':
3004 list_table(0);
3005 break;
3006 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003007 if (ENABLE_FEATURE_CLEAN_UP)
3008 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003009 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003010 return 0;
3011 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003012#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003013 create_sunlabel();
3014#endif
3015 break;
3016 case 't':
3017 change_sysid();
3018 break;
3019 case 'u':
3020 change_units();
3021 break;
3022 case 'v':
3023 verify();
3024 break;
3025 case 'w':
3026 write_table(); /* does not return */
3027 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003028#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003029 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003030 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003031 printf("\n\tSorry, no experts menu for SGI "
3032 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003033 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003034 xselect();
3035 break;
3036#endif
3037 default:
3038 unknown_command(c);
3039 menu();
3040 }
3041 }
3042 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003043#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003044}