blob: 980568bc8c6dab0ecee201636a5f2ccf5d836201 [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
Denis Vlasenko834410a2006-11-29 12:00:28 +000024/* Looks like someone forgot to add this to config system */
25#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
26# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000027# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +000028#endif
29
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000030#define DEFAULT_SECTOR_SIZE 512
31#define DEFAULT_SECTOR_SIZE_STR "512"
32#define MAX_SECTOR_SIZE 2048
33#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
34#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000035
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000036#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000037
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000038#define EXTENDED 0x05
39#define WIN98_EXTENDED 0x0f
40#define LINUX_PARTITION 0x81
41#define LINUX_SWAP 0x82
42#define LINUX_NATIVE 0x83
43#define LINUX_EXTENDED 0x85
44#define LINUX_LVM 0x8e
45#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000046
Denis Vlasenkoc033d512008-04-17 01:52:28 +000047
48enum {
49 OPT_b = 1 << 0,
50 OPT_C = 1 << 1,
51 OPT_H = 1 << 2,
52 OPT_l = 1 << 3,
53 OPT_S = 1 << 4,
54 OPT_u = 1 << 5,
55 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
56};
57
58
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000059typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +020060/* Used for sector numbers. Partition formats we know
61 * do not support more than 2^32 sectors
62 */
63typedef uint32_t sector_t;
64#if UINT_MAX == 4294967295
65# define SECT_FMT ""
66#elif ULONG_MAX == 4294967295
67# define SECT_FMT "l"
68#else
69# error Cant detect sizeof(uint32_t)
70#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000071
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000073 unsigned char heads;
74 unsigned char sectors;
75 unsigned short cylinders;
76 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000077};
78
Denis Vlasenko98ae2162006-10-12 19:30:44 +000079#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000080
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000081static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000082"Building a new %s. Changes will remain in memory only,\n"
83"until you decide to write them. After that the previous content\n"
84"won't be recoverable.\n\n";
85
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000086static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +020087"Partition %u is already defined, delete it before re-adding\n";
Denis Vlasenkobd852072007-03-19 14:43:38 +000088
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000089
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000090struct partition {
91 unsigned char boot_ind; /* 0x80 - active */
92 unsigned char head; /* starting head */
93 unsigned char sector; /* starting sector */
94 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +000095 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000096 unsigned char end_head; /* end head */
97 unsigned char end_sector; /* end sector */
98 unsigned char end_cyl; /* end cylinder */
99 unsigned char start4[4]; /* starting sector counting from 0 */
100 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000101} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000102
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000103static const char unable_to_open[] ALIGN1 = "can't open '%s'";
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000104static const char unable_to_read[] ALIGN1 = "can't read from %s";
105static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000106
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000107enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +0000108 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +0000109};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000110
Denis Vlasenko4437d192008-04-17 00:12:10 +0000111#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112
Denis Vlasenko834410a2006-11-29 12:00:28 +0000113#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000114#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000115#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#else
117#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000118#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000119#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000120
Denis Vlasenko834410a2006-11-29 12:00:28 +0000121#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000122#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000123#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000124#else
125#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000126#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000127#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000128
Denis Vlasenko834410a2006-11-29 12:00:28 +0000129#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000130#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000131#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000132#else
133#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000134#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000135#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000136
Denis Vlasenko834410a2006-11-29 12:00:28 +0000137#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000138#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000139#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000140#else
141#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000142#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000143#endif
Rob Landley5527b912006-02-25 03:46:10 +0000144
Denis Vlasenko4437d192008-04-17 00:12:10 +0000145enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000146
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000147static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000148#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000149static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000150static void reread_partition_table(int leave);
151static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200152static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000153static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200154static 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 +0000155#endif
156static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000157static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000158#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000159static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000160#else
161static int get_boot(void);
162#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000163
164#define PLURAL 0
165#define SINGULAR 1
166
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200167static sector_t get_start_sect(const struct partition *p);
168static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000169
170/*
171 * per partition table entry data
172 *
173 * The four primary partitions have the same sectorbuffer (MBRbuffer)
174 * and have NULL ext_pointer.
175 * Each logical partition table entry has two pointers, one for the
176 * partition and one link to the next one.
177 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000178struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000179 struct partition *part_table; /* points into sectorbuffer */
180 struct partition *ext_pointer; /* points into sectorbuffer */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200181 sector_t offset; /* disk sector number */
182 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000183#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000184 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000185#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000186};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000187
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000188/* DOS partition types */
189
190static const char *const i386_sys_types[] = {
191 "\x00" "Empty",
192 "\x01" "FAT12",
193 "\x04" "FAT16 <32M",
194 "\x05" "Extended", /* DOS 3.3+ extended partition */
195 "\x06" "FAT16", /* DOS 16-bit >=32M */
196 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
197 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
198 "\x0b" "Win95 FAT32",
199 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
200 "\x0e" "Win95 FAT16 (LBA)",
201 "\x0f" "Win95 Ext'd (LBA)",
202 "\x11" "Hidden FAT12",
203 "\x12" "Compaq diagnostics",
204 "\x14" "Hidden FAT16 <32M",
205 "\x16" "Hidden FAT16",
206 "\x17" "Hidden HPFS/NTFS",
207 "\x1b" "Hidden Win95 FAT32",
208 "\x1c" "Hidden W95 FAT32 (LBA)",
209 "\x1e" "Hidden W95 FAT16 (LBA)",
210 "\x3c" "Part.Magic recovery",
211 "\x41" "PPC PReP Boot",
212 "\x42" "SFS",
213 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
214 "\x80" "Old Minix", /* Minix 1.4a and earlier */
215 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
216 "\x82" "Linux swap", /* also Solaris */
217 "\x83" "Linux",
218 "\x84" "OS/2 hidden C: drive",
219 "\x85" "Linux extended",
220 "\x86" "NTFS volume set",
221 "\x87" "NTFS volume set",
222 "\x8e" "Linux LVM",
223 "\x9f" "BSD/OS", /* BSDI */
224 "\xa0" "Thinkpad hibernation",
225 "\xa5" "FreeBSD", /* various BSD flavours */
226 "\xa6" "OpenBSD",
227 "\xa8" "Darwin UFS",
228 "\xa9" "NetBSD",
229 "\xab" "Darwin boot",
230 "\xb7" "BSDI fs",
231 "\xb8" "BSDI swap",
232 "\xbe" "Solaris boot",
233 "\xeb" "BeOS fs",
234 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
235 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
236 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
237 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
238 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
239 autodetect using persistent
240 superblock */
241#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
242 "\x02" "XENIX root",
243 "\x03" "XENIX usr",
244 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
245 "\x09" "AIX bootable", /* AIX data or Coherent */
246 "\x10" "OPUS",
247 "\x18" "AST SmartSleep",
248 "\x24" "NEC DOS",
249 "\x39" "Plan 9",
250 "\x40" "Venix 80286",
251 "\x4d" "QNX4.x",
252 "\x4e" "QNX4.x 2nd part",
253 "\x4f" "QNX4.x 3rd part",
254 "\x50" "OnTrack DM",
255 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
256 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
257 "\x53" "OnTrack DM6 Aux3",
258 "\x54" "OnTrackDM6",
259 "\x55" "EZ-Drive",
260 "\x56" "Golden Bow",
261 "\x5c" "Priam Edisk",
262 "\x61" "SpeedStor",
263 "\x64" "Novell Netware 286",
264 "\x65" "Novell Netware 386",
265 "\x70" "DiskSecure Multi-Boot",
266 "\x75" "PC/IX",
267 "\x93" "Amoeba",
268 "\x94" "Amoeba BBT", /* (bad block table) */
269 "\xa7" "NeXTSTEP",
270 "\xbb" "Boot Wizard hidden",
271 "\xc1" "DRDOS/sec (FAT-12)",
272 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
273 "\xc6" "DRDOS/sec (FAT-16)",
274 "\xc7" "Syrinx",
275 "\xda" "Non-FS data",
276 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
277 Concurrent DOS or CTOS */
278 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
279 "\xdf" "BootIt", /* BootIt EMBRM */
280 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
281 extended partition */
282 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
283 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
284 partition < 1024 cyl. */
285 "\xf1" "SpeedStor",
286 "\xf4" "SpeedStor", /* SpeedStor large partition */
287 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
288 "\xff" "BBT", /* Xenix Bad Block Table */
289#endif
290 NULL
291};
292
Denis Vlasenko4437d192008-04-17 00:12:10 +0000293enum {
294 dev_fd = 3 /* the disk */
295};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000296
297/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000298struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000299 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000300
301 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000302 int g_partitions; // = 4; /* maximum partition + 1 */
303 unsigned units_per_sector; // = 1;
304 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
305 unsigned user_set_sector_size;
306 unsigned sector_offset; // = 1;
307 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000308 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000309 smallint display_in_cyl_units; // = 1;
310#if ENABLE_FEATURE_OSF_LABEL
311 smallint possibly_osf_label;
312#endif
313
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000314 smallint listing; /* no aborts for fdisk -l */
315 smallint dos_compatible_flag; // = 1;
316#if ENABLE_FEATURE_FDISK_WRITABLE
317 //int dos_changed;
318 smallint nowarn; /* no warnings for fdisk -l/-s */
319#endif
320 int ext_index; /* the prime extended partition */
321 unsigned user_cylinders, user_heads, user_sectors;
322 unsigned pt_heads, pt_sectors;
323 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200324 sector_t extended_offset; /* offset of link pointers */
325 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000326
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000327 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000328 char line_buffer[80];
329 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000330 /* Raw disk label. For DOS-type partition tables the MBR,
331 * with descriptions of the primary partitions. */
332 char MBRbuffer[MAX_SECTOR_SIZE];
333 /* Partition tables */
334 struct pte ptes[MAXIMUM_PARTS];
335};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000336#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000337#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000338#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000339#define g_partitions (G.g_partitions )
340#define units_per_sector (G.units_per_sector )
341#define sector_size (G.sector_size )
342#define user_set_sector_size (G.user_set_sector_size)
343#define sector_offset (G.sector_offset )
344#define g_heads (G.g_heads )
345#define g_sectors (G.g_sectors )
346#define g_cylinders (G.g_cylinders )
347#define current_label_type (G.current_label_type )
348#define display_in_cyl_units (G.display_in_cyl_units)
349#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000350#define listing (G.listing )
351#define dos_compatible_flag (G.dos_compatible_flag )
352#define nowarn (G.nowarn )
353#define ext_index (G.ext_index )
354#define user_cylinders (G.user_cylinders )
355#define user_heads (G.user_heads )
356#define user_sectors (G.user_sectors )
357#define pt_heads (G.pt_heads )
358#define pt_sectors (G.pt_sectors )
359#define kern_heads (G.kern_heads )
360#define kern_sectors (G.kern_sectors )
361#define extended_offset (G.extended_offset )
362#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000363#define listingbuf (G.listingbuf )
364#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000365#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000366#define MBRbuffer (G.MBRbuffer )
367#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000368#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000369 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000370 sector_size = DEFAULT_SECTOR_SIZE; \
371 sector_offset = 1; \
372 g_partitions = 4; \
373 display_in_cyl_units = 1; \
374 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000375 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000376} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000377
Denis Vlasenkobd852072007-03-19 14:43:38 +0000378
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000379/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200380/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
381 * disks > 2^32 sectors
382 */
383static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000384{
385 uint64_t v64;
386 unsigned long longsectors;
387
388 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000389 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200390 v64 >>= 9;
391 if (v64 != (sector_t)v64) {
392 ret_trunc:
393 /* Not only DOS, but all other partition tables
394 * we support can't record more than 32 bit
395 * sector counts or offsets
396 */
397 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
398 v64 = (uint32_t)-1L;
399 }
400 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000401 }
402 /* Needs temp of type long */
403 if (ioctl(fd, BLKGETSIZE, &longsectors))
404 longsectors = 0;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200405 if (sizeof(long) > sizeof(sector_t)
406 && longsectors != (sector_t)longsectors
407 ) {
408 goto ret_trunc;
409 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000410 return longsectors;
411}
412
413
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000414#define IS_EXTENDED(i) \
415 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
416
417#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
418
419#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
420
421#define pt_offset(b, n) \
422 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
423
424#define sector(s) ((s) & 0x3f)
425
426#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
427
428#define hsc2sector(h,s,c) \
429 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
430
431#define set_hsc(h,s,c,sector) \
432 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000433 s = sector % g_sectors + 1; \
434 sector /= g_sectors; \
435 h = sector % g_heads; \
436 sector /= g_heads; \
437 c = sector & 0xff; \
438 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000439 } while (0)
440
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000441static void
442close_dev_fd(void)
443{
444 /* Not really closing, but making sure it is open, and to harmless place */
445 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
446}
447
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000448#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000449/* Read line; return 0 or first printable char */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000450static int
451read_line(const char *prompt)
452{
453 int sz;
454
455 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
456 if (sz <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000457 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000458
459 if (line_buffer[sz-1] == '\n')
460 line_buffer[--sz] = '\0';
461
462 line_ptr = line_buffer;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200463 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000464 line_ptr++;
465 return *line_ptr;
466}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000467#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000468
Denis Vlasenkobd852072007-03-19 14:43:38 +0000469/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000470 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000471 */
472static const char *
473partname(const char *dev, int pno, int lth)
474{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000475 const char *p;
476 int w, wp;
477 int bufsiz;
478 char *bufp;
479
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000480 bufp = partname_buffer;
481 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000482
483 w = strlen(dev);
484 p = "";
485
486 if (isdigit(dev[w-1]))
487 p = "p";
488
489 /* devfs kludge - note: fdisk partition names are not supposed
490 to equal kernel names, so there is no reason to do this */
491 if (strcmp(dev + w - 4, "disc") == 0) {
492 w -= 4;
493 p = "part";
494 }
495
496 wp = strlen(p);
497
498 if (lth) {
499 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
500 lth-wp-2, w, dev, p, pno);
501 } else {
502 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
503 }
504 return bufp;
505}
506
Denis Vlasenko834410a2006-11-29 12:00:28 +0000507#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000508static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000509set_all_unchanged(void)
510{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000511 int i;
512
513 for (i = 0; i < MAXIMUM_PARTS; i++)
514 ptes[i].changed = 0;
515}
516
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000517static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000518set_changed(int i)
519{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000520 ptes[i].changed = 1;
521}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000522#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000523
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000524static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000525get_part_table(int i)
526{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000527 return ptes[i].part_table;
528}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000529
530static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000531str_units(int n)
532{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000533 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000534 return display_in_cyl_units ? "cylinder" : "sector";
535 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000536}
537
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000538static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000539valid_part_table_flag(const char *mbuffer)
540{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000541 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000542}
543
Denis Vlasenko834410a2006-11-29 12:00:28 +0000544#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000545static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000546write_part_table_flag(char *b)
547{
548 b[510] = 0x55;
549 b[511] = 0xaa;
550}
551
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000552static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000553read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000555 while (!read_line(mesg))
556 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000557 return *line_ptr;
558}
559
560static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000561read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000562{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000563 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000564 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000565 line_ptr[0] = '\n';
566 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000567 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000568 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000569}
570
571static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000572read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000573{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000574 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000575 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000576 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000577 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000578 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000579 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000580 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000581 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000582 if (v > 0xff)
583 /* Bad input also triggers this */
584 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000585 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000586 }
587}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000588#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589
Denis Vlasenko9764d692008-07-09 21:20:50 +0000590static void fdisk_fatal(const char *why)
591{
592 if (listing) {
593 close_dev_fd();
594 longjmp(listingbuf, 1);
595 }
596 bb_error_msg_and_die(why, disk_device);
597}
598
599static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200600seek_sector(sector_t secno)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000601{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000602#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200603 off64_t off = (off64_t)secno * sector_size;
604 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000605 fdisk_fatal(unable_to_seek);
606#else
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200607 uint64_t off = (uint64_t)secno * sector_size;
608 if (off > MAXINT(off_t)
609 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
Denis Vlasenko9764d692008-07-09 21:20:50 +0000610 ) {
611 fdisk_fatal(unable_to_seek);
612 }
613#endif
614}
615
616#if ENABLE_FEATURE_FDISK_WRITABLE
617static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200618write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000619{
620 seek_sector(secno);
621 xwrite(dev_fd, buf, sector_size);
622}
623#endif
624
625
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000626#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100628struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000629 unsigned char info[128]; /* Informative text string */
630 unsigned char spare0[14];
631 struct sun_info {
632 unsigned char spare1;
633 unsigned char id;
634 unsigned char spare2;
635 unsigned char flags;
636 } infos[8];
637 unsigned char spare1[246]; /* Boot information etc. */
638 unsigned short rspeed; /* Disk rotational speed */
639 unsigned short pcylcount; /* Physical cylinder count */
640 unsigned short sparecyl; /* extra sects per cylinder */
641 unsigned char spare2[4]; /* More magic... */
642 unsigned short ilfact; /* Interleave factor */
643 unsigned short ncyl; /* Data cylinder count */
644 unsigned short nacyl; /* Alt. cylinder count */
645 unsigned short ntrks; /* Tracks per cylinder */
646 unsigned short nsect; /* Sectors per track */
647 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000648 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000649 uint32_t start_cylinder;
650 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000651 } partitions[8];
652 unsigned short magic; /* Magic number */
653 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100654} FIX_ALIASING;
655typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000656#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000657STATIC_OSF void bsd_select(void);
658STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000659#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000660
Denis Vlasenko28703012006-12-19 20:32:02 +0000661#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000662static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000663fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000664{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000665 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000666}
667
Rob Landley88621d72006-08-29 19:41:06 +0000668static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000669fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000670{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000671 return (x << 24) |
672 ((x & 0xFF00) << 8) |
673 ((x & 0xFF0000) >> 8) |
674 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000675}
676#endif
677
Denis Vlasenkobd852072007-03-19 14:43:38 +0000678STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000679STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000680STATIC_SGI int sgi_get_sysid(int i);
681STATIC_SGI void sgi_delete_partition(int i);
682STATIC_SGI void sgi_change_sysid(int i, int sys);
683STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000684#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000685STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000686#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000687STATIC_SGI int verify_sgi(int verbose);
688STATIC_SGI void sgi_add_partition(int n, int sys);
689STATIC_SGI void sgi_set_swappartition(int i);
690STATIC_SGI const char *sgi_get_bootfile(void);
691STATIC_SGI void sgi_set_bootfile(const char* aFile);
692STATIC_SGI void create_sgiinfo(void);
693STATIC_SGI void sgi_write_table(void);
694STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000695#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000696
Denis Vlasenkobd852072007-03-19 14:43:38 +0000697STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000698STATIC_SUN void sun_delete_partition(int i);
699STATIC_SUN void sun_change_sysid(int i, int sys);
700STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000701STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000702#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000703STATIC_SUN void sun_set_alt_cyl(void);
704STATIC_SUN void sun_set_ncyl(int cyl);
705STATIC_SUN void sun_set_xcyl(void);
706STATIC_SUN void sun_set_ilfact(void);
707STATIC_SUN void sun_set_rspeed(void);
708STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000709#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000710STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
711STATIC_SUN void verify_sun(void);
712STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000713#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000714
Denis Vlasenko9764d692008-07-09 21:20:50 +0000715
Denis Vlasenko834410a2006-11-29 12:00:28 +0000716#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000717/* start_sect and nr_sects are stored little endian on all machines */
718/* moreover, they are not aligned correctly */
719static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000720store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000721{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000722 cp[0] = val;
723 cp[1] = val >> 8;
724 cp[2] = val >> 16;
725 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000726}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000727#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000728
Denis Vlasenko834410a2006-11-29 12:00:28 +0000729static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000730read4_little_endian(const unsigned char *cp)
731{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000732 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000733}
734
Denis Vlasenko834410a2006-11-29 12:00:28 +0000735#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000736static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000737set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000738{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000739 store4_little_endian(p->start4, start_sect);
740}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000741#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000742
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200743static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000744get_start_sect(const struct partition *p)
745{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000746 return read4_little_endian(p->start4);
747}
748
Denis Vlasenko834410a2006-11-29 12:00:28 +0000749#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000750static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000751set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000752{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000753 store4_little_endian(p->size4, nr_sects);
754}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000755#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000756
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200757static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000758get_nr_sects(const struct partition *p)
759{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000760 return read4_little_endian(p->size4);
761}
762
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000763/* Allocate a buffer and read a partition table sector */
764static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200765read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000766{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000767 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000768 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000769 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000770 /* xread would make us abort - bad for fdisk -l */
771 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000772 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000773#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000774 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000775#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000776 pe->part_table = pe->ext_pointer = NULL;
777}
778
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200779static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000780get_partition_start(const struct pte *pe)
781{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000782 return pe->offset + get_start_sect(pe->part_table);
783}
784
Denis Vlasenko834410a2006-11-29 12:00:28 +0000785#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000786/*
787 * Avoid warning about DOS partitions when no DOS partition was changed.
788 * Here a heuristic "is probably dos partition".
789 * We might also do the opposite and warn in all cases except
790 * for "is probably nondos partition".
791 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000792#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000793static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000794is_dos_partition(int t)
795{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000796 return (t == 1 || t == 4 || t == 6 ||
797 t == 0x0b || t == 0x0c || t == 0x0e ||
798 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
799 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
800 t == 0xc1 || t == 0xc4 || t == 0xc6);
801}
Denis Vlasenko89398812008-01-25 20:18:46 +0000802#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000803
804static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000805menu(void)
806{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000807 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000808 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000809 puts("a\ttoggle a read only flag"); /* sun */
810 puts("b\tedit bsd disklabel");
811 puts("c\ttoggle the mountable flag"); /* sun */
812 puts("d\tdelete a partition");
813 puts("l\tlist known partition types");
814 puts("n\tadd a new partition");
815 puts("o\tcreate a new empty DOS partition table");
816 puts("p\tprint the partition table");
817 puts("q\tquit without saving changes");
818 puts("s\tcreate a new empty Sun disklabel"); /* sun */
819 puts("t\tchange a partition's system id");
820 puts("u\tchange display/entry units");
821 puts("v\tverify the partition table");
822 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000823#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000824 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000825#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000826 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000827 puts("a\tselect bootable partition"); /* sgi flavour */
828 puts("b\tedit bootfile entry"); /* sgi */
829 puts("c\tselect sgi swap partition"); /* sgi flavour */
830 puts("d\tdelete a partition");
831 puts("l\tlist known partition types");
832 puts("n\tadd a new partition");
833 puts("o\tcreate a new empty DOS partition table");
834 puts("p\tprint the partition table");
835 puts("q\tquit without saving changes");
836 puts("s\tcreate a new empty Sun disklabel"); /* sun */
837 puts("t\tchange a partition's system id");
838 puts("u\tchange display/entry units");
839 puts("v\tverify the partition table");
840 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000841 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000842 puts("o\tcreate a new empty DOS partition table");
843 puts("q\tquit without saving changes");
844 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000845 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000846 puts("a\ttoggle a bootable flag");
847 puts("b\tedit bsd disklabel");
848 puts("c\ttoggle the dos compatibility flag");
849 puts("d\tdelete a partition");
850 puts("l\tlist known partition types");
851 puts("n\tadd a new partition");
852 puts("o\tcreate a new empty DOS partition table");
853 puts("p\tprint the partition table");
854 puts("q\tquit without saving changes");
855 puts("s\tcreate a new empty Sun disklabel"); /* sun */
856 puts("t\tchange a partition's system id");
857 puts("u\tchange display/entry units");
858 puts("v\tverify the partition table");
859 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000860#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000861 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000862#endif
863 }
864}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000865#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000866
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000867
Denis Vlasenko834410a2006-11-29 12:00:28 +0000868#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000869static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000870xmenu(void)
871{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000872 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000873 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000874 puts("a\tchange number of alternate cylinders"); /*sun*/
875 puts("c\tchange number of cylinders");
876 puts("d\tprint the raw data in the partition table");
877 puts("e\tchange number of extra sectors per cylinder");/*sun*/
878 puts("h\tchange number of heads");
879 puts("i\tchange interleave factor"); /*sun*/
880 puts("o\tchange rotation speed (rpm)"); /*sun*/
881 puts("p\tprint the partition table");
882 puts("q\tquit without saving changes");
883 puts("r\treturn to main menu");
884 puts("s\tchange number of sectors/track");
885 puts("v\tverify the partition table");
886 puts("w\twrite table to disk and exit");
887 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000888 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000889 puts("b\tmove beginning of data in a partition"); /* !sun */
890 puts("c\tchange number of cylinders");
891 puts("d\tprint the raw data in the partition table");
892 puts("e\tlist extended partitions"); /* !sun */
893 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
894 puts("h\tchange number of heads");
895 puts("p\tprint the partition table");
896 puts("q\tquit without saving changes");
897 puts("r\treturn to main menu");
898 puts("s\tchange number of sectors/track");
899 puts("v\tverify the partition table");
900 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000901 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000902 puts("b\tmove beginning of data in a partition"); /* !sun */
903 puts("c\tchange number of cylinders");
904 puts("d\tprint the raw data in the partition table");
905 puts("e\tlist extended partitions"); /* !sun */
906 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
907 puts("h\tchange number of heads");
908 puts("p\tprint the partition table");
909 puts("q\tquit without saving changes");
910 puts("r\treturn to main menu");
911 puts("s\tchange number of sectors/track");
912 puts("v\tverify the partition table");
913 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000914 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000915 puts("b\tmove beginning of data in a partition"); /* !sun */
916 puts("c\tchange number of cylinders");
917 puts("d\tprint the raw data in the partition table");
918 puts("e\tlist extended partitions"); /* !sun */
919 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000920#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000921 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000922#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000923 puts("h\tchange number of heads");
924 puts("p\tprint the partition table");
925 puts("q\tquit without saving changes");
926 puts("r\treturn to main menu");
927 puts("s\tchange number of sectors/track");
928 puts("v\tverify the partition table");
929 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000930 }
931}
932#endif /* ADVANCED mode */
933
Denis Vlasenko834410a2006-11-29 12:00:28 +0000934#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000935static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000936get_sys_types(void)
937{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000939 LABEL_IS_SUN ? sun_sys_types :
940 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000941 i386_sys_types);
942}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000943#else
944#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000945#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000946
Denis Vlasenkobd852072007-03-19 14:43:38 +0000947static const char *
948partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949{
950 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000951 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000952
Denis Vlasenkobd852072007-03-19 14:43:38 +0000953 for (i = 0; types[i]; i++)
954 if ((unsigned char)types[i][0] == type)
955 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956
Denis Vlasenkobd852072007-03-19 14:43:38 +0000957 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000958}
959
960
Denis Vlasenko834410a2006-11-29 12:00:28 +0000961#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000962static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000963get_sysid(int i)
964{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000965 return LABEL_IS_SUN ? sunlabel->infos[i].id :
966 (LABEL_IS_SGI ? sgi_get_sysid(i) :
967 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968}
969
Denis Vlasenkobd852072007-03-19 14:43:38 +0000970static void
971list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000972{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000973 enum { COLS = 3 };
974
975 unsigned last[COLS];
976 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000977 int i;
978
Denis Vlasenko9764d692008-07-09 21:20:50 +0000979 for (size = 0; sys[size]; size++)
980 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000981
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000982 done = 0;
983 for (i = COLS-1; i >= 0; i--) {
984 done += (size + i - done) / (i + 1);
985 last[COLS-1 - i] = done;
986 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000987
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000988 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000989 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000990 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000991 (unsigned char)sys[next][0],
992 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000993 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000994 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000995 i = 0;
996 next = ++done;
997 }
998 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000999 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001000}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001001#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002
1003static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001004is_cleared_partition(const struct partition *p)
1005{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
1007 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
1008 get_start_sect(p) || get_nr_sects(p));
1009}
1010
1011static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001012clear_partition(struct partition *p)
1013{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001014 if (!p)
1015 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001016 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001017}
1018
Denis Vlasenko834410a2006-11-29 12:00:28 +00001019#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001020static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001021set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001022{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001023 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001024 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001025
1026 if (doext) {
1027 p = ptes[i].ext_pointer;
1028 offset = extended_offset;
1029 } else {
1030 p = ptes[i].part_table;
1031 offset = ptes[i].offset;
1032 }
1033 p->boot_ind = 0;
1034 p->sys_ind = sysid;
1035 set_start_sect(p, start - offset);
1036 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001037 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1038 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001039 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001040 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1041 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1043 ptes[i].changed = 1;
1044}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001045#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001046
1047static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001048warn_geometry(void)
1049{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001050 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001052
Denis Vlasenkobd852072007-03-19 14:43:38 +00001053 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001054 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001056 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001058 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001059 printf(" cylinders");
1060 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001061#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001062 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001063#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001064 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001065 return 1;
1066}
1067
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001068static void
1069update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001070{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001071 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001072
1073 if (display_in_cyl_units && cyl_units)
1074 units_per_sector = cyl_units;
1075 else
1076 units_per_sector = 1; /* in sectors */
1077}
1078
Denis Vlasenko834410a2006-11-29 12:00:28 +00001079#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001080static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001081warn_cylinders(void)
1082{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001083 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001084 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001085"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001086"There is nothing wrong with that, but this is larger than 1024,\n"
1087"and could in certain setups cause problems with:\n"
1088"1) software that runs at boot time (e.g., old versions of LILO)\n"
1089"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001090" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001091 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001092}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001093#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094
1095static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001096read_extended(int ext)
1097{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001098 int i;
1099 struct pte *pex;
1100 struct partition *p, *q;
1101
1102 ext_index = ext;
1103 pex = &ptes[ext];
1104 pex->ext_pointer = pex->part_table;
1105
1106 p = pex->part_table;
1107 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001108 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109 return;
1110 }
1111
Rob Landleyb73451d2006-02-24 16:29:00 +00001112 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001113 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001114
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001115 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116 /* This is not a Linux restriction, but
1117 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001118 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001119 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001120#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001121 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001122 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001123 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001124#endif
1125 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001126 return;
1127 }
1128
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001129 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001130
1131 if (!extended_offset)
1132 extended_offset = get_start_sect(p);
1133
1134 q = p = pt_offset(pe->sectorbuffer, 0);
1135 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001136 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001137 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001138 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001139 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001140 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001141 else
1142 pe->ext_pointer = p;
1143 } else if (p->sys_ind) {
1144 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001145 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001146 "data 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->part_table = p;
1150 }
1151 }
1152
1153 /* very strange code here... */
1154 if (!pe->part_table) {
1155 if (q != pe->ext_pointer)
1156 pe->part_table = q;
1157 else
1158 pe->part_table = q + 1;
1159 }
1160 if (!pe->ext_pointer) {
1161 if (q != pe->part_table)
1162 pe->ext_pointer = q;
1163 else
1164 pe->ext_pointer = q + 1;
1165 }
1166
1167 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001168 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001169 }
1170
Denis Vlasenko834410a2006-11-29 12:00:28 +00001171#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 /* remove empty links */
1173 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001174 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001175 struct pte *pe = &ptes[i];
1176
Denis Vlasenkobd852072007-03-19 14:43:38 +00001177 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001178 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001179 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001180 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181 delete_partition(i);
1182 goto remove; /* numbering changed */
1183 }
1184 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001185#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001186}
1187
Denis Vlasenko834410a2006-11-29 12:00:28 +00001188#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001189static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001190create_doslabel(void)
1191{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001192 int i;
1193
Denis Vlasenkobd852072007-03-19 14:43:38 +00001194 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001195
Denis Vlasenko4437d192008-04-17 00:12:10 +00001196 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001197
Denis Vlasenko834410a2006-11-29 12:00:28 +00001198#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001199 possibly_osf_label = 0;
1200#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001201 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001202
1203 for (i = 510-64; i < 510; i++)
1204 MBRbuffer[i] = 0;
1205 write_part_table_flag(MBRbuffer);
1206 extended_offset = 0;
1207 set_all_unchanged();
1208 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001209 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001211#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212
1213static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001214get_sectorsize(void)
1215{
Rob Landley736e5252006-02-25 03:36:00 +00001216 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001217 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001218 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219 sector_size = arg;
1220 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001221 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001222 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1223 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001224 }
1225}
1226
Rob Landley88621d72006-08-29 19:41:06 +00001227static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001228get_kernel_geometry(void)
1229{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001230 struct hd_geometry geometry;
1231
Denis Vlasenko4437d192008-04-17 00:12:10 +00001232 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001233 kern_heads = geometry.heads;
1234 kern_sectors = geometry.sectors;
1235 /* never use geometry.cylinders - it is truncated */
1236 }
1237}
1238
1239static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001240get_partition_table_geometry(void)
1241{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001242 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 struct partition *p;
1244 int i, h, s, hh, ss;
1245 int first = 1;
1246 int bad = 0;
1247
Eric Andersen3496fdc2006-01-30 23:09:20 +00001248 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001249 return;
1250
1251 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001252 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253 p = pt_offset(bufp, i);
1254 if (p->sys_ind != 0) {
1255 h = p->end_head + 1;
1256 s = (p->end_sector & 077);
1257 if (first) {
1258 hh = h;
1259 ss = s;
1260 first = 0;
1261 } else if (hh != h || ss != s)
1262 bad = 1;
1263 }
1264 }
1265
1266 if (!first && !bad) {
1267 pt_heads = hh;
1268 pt_sectors = ss;
1269 }
1270}
1271
Rob Landleyb73451d2006-02-24 16:29:00 +00001272static void
1273get_geometry(void)
1274{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001275 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001276
1277 get_sectorsize();
1278 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001279#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001280 guess_device_type();
1281#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001282 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001283 kern_heads = kern_sectors = 0;
1284 pt_heads = pt_sectors = 0;
1285
1286 get_kernel_geometry();
1287 get_partition_table_geometry();
1288
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001289 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 pt_heads ? pt_heads :
1291 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001292 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 pt_sectors ? pt_sectors :
1294 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001295 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001296
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001297 sector_offset = 1;
1298 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001299 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001301 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1302 if (!g_cylinders)
1303 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001304}
1305
1306/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001307 * Opens disk_device and optionally reads MBR.
1308 * FIXME: document what each 'what' value will do!
1309 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001310 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1311 * 0: found or created label
1312 * 1: I/O error
1313 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001314#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1315static int get_boot(enum action what)
1316#else
1317static int get_boot(void)
1318#define get_boot(what) get_boot()
1319#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001320{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001321 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001323 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324 for (i = 0; i < 4; i++) {
1325 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001326 pe->part_table = pt_offset(MBRbuffer, i);
1327 pe->ext_pointer = NULL;
1328 pe->offset = 0;
1329 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001330#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001331 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001332#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001333 }
1334
Denis Vlasenko834410a2006-11-29 12:00:28 +00001335#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001336// ALERT! highly idiotic design!
1337// We end up here when we call get_boot() recursively
1338// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1339// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1340// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1341// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001342 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001343 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001344
Denis Vlasenko4437d192008-04-17 00:12:10 +00001345 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1346
Denis Vlasenkobd852072007-03-19 14:43:38 +00001347 if (fd < 0) {
1348 fd = open(disk_device, O_RDONLY);
1349 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001350 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001351 return 1;
1352 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001353 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001354 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001356 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001357 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001358 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001359 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001360 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001361 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 fdisk_fatal(unable_to_read);
1363 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001364#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001365 fd = open(disk_device, O_RDONLY);
1366 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001367 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001368 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001369 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001370 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001371 }
1372 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001373#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001375 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001376 update_units();
1377
Denis Vlasenko834410a2006-11-29 12:00:28 +00001378#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 if (check_sun_label())
1380 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001381#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001382#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001383 if (check_sgi_label())
1384 return 0;
1385#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001386#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 if (check_aix_label())
1388 return 0;
1389#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001390#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 if (check_osf_label()) {
1392 possibly_osf_label = 1;
1393 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001394 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395 return 0;
1396 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001397 printf("This disk has both DOS and BSD magic.\n"
1398 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399 }
1400#endif
1401
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001402#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001403 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001404 return -1;
1405#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001406 if (!valid_part_table_flag(MBRbuffer)) {
1407 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001408 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001410 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001411#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001412 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413#else
1414 create_doslabel();
1415#endif
1416 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001417 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001418 /* TRY_ONLY: */
1419 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001421 created_table:
1422#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001423
Denis Vlasenko4437d192008-04-17 00:12:10 +00001424
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001425 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001426 warn_geometry();
1427
1428 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001429 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001430 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001431 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001432 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001433 else
1434 read_extended(i);
1435 }
1436 }
1437
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001438 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001439 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001440 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001441 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001442 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001443 pe->sectorbuffer[510],
1444 pe->sectorbuffer[511],
1445 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001446 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001447 }
1448 }
1449
1450 return 0;
1451}
1452
Denis Vlasenko834410a2006-11-29 12:00:28 +00001453#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001454/*
1455 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1456 * If the user hits Enter, DFLT is returned.
1457 * Answers like +10 are interpreted as offsets from BASE.
1458 *
1459 * There is no default if DFLT is not between LOW and HIGH.
1460 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001461static sector_t
1462read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001463{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001464 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001465 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001466 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001467
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001468 if (dflt < low || dflt > high) {
1469 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001470 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001471 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001472
1473 while (1) {
1474 int use_default = default_ok;
1475
1476 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001477 do {
1478 printf(fmt, mesg, low, high, dflt);
1479 read_maybe_empty("");
1480 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1481 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001482
Eric Andersen84bdea82004-05-19 10:49:17 +00001483 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001484 int minus = (*line_ptr == '-');
1485 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001486
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001487 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001488
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001489 /* (1) if 2nd char is digit, use_default = 0.
1490 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001491 while (isdigit(*++line_ptr))
1492 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001493
Rob Landleyb73451d2006-02-24 16:29:00 +00001494 switch (*line_ptr) {
1495 case 'c':
1496 case 'C':
1497 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001498 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001499 break;
1500 case 'K':
1501 absolute = 1024;
1502 break;
1503 case 'k':
1504 absolute = 1000;
1505 break;
1506 case 'm':
1507 case 'M':
1508 absolute = 1000000;
1509 break;
1510 case 'g':
1511 case 'G':
1512 absolute = 1000000000;
1513 break;
1514 default:
1515 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001516 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001517 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001518 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001519 unsigned long unit;
1520
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001521 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001522 unit = sector_size * units_per_sector;
1523 bytes += unit/2; /* round */
1524 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001525 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001526 }
1527 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001528 value = -value;
1529 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001530 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001531 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001532 while (isdigit(*line_ptr)) {
1533 line_ptr++;
1534 use_default = 0;
1535 }
1536 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001537 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001538 value = dflt;
1539 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001540 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001541 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001542 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001543 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001545 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001546}
1547
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001548static unsigned
1549get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001552 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001553
Denis Vlasenkobd852072007-03-19 14:43:38 +00001554 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555 pe = &ptes[i];
1556
1557 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001558 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1559 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1560 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1561 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001562 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001563 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001564 }
1565 return i;
1566}
1567
1568static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001569get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001570{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001572 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001573
1574 for (i = 0; i < max; i++) {
1575 struct pte *pe = &ptes[i];
1576 struct partition *p = pe->part_table;
1577
1578 if (p && !is_cleared_partition(p)) {
1579 if (pno >= 0)
1580 goto not_unique;
1581 pno = i;
1582 }
1583 }
1584 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001585 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586 return pno;
1587 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001588 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 return -1;
1590
1591 not_unique:
1592 return get_partition(warn, max);
1593}
1594
1595static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001596get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001597{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001599 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001600
1601 for (i = 0; i < max; i++) {
1602 struct pte *pe = &ptes[i];
1603 struct partition *p = pe->part_table;
1604
1605 if (p && is_cleared_partition(p)) {
1606 if (pno >= 0)
1607 goto not_unique;
1608 pno = i;
1609 }
1610 }
1611 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001612 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613 return pno;
1614 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001615 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001616 return -1;
1617
1618 not_unique:
1619 return get_partition(warn, max);
1620}
1621
1622
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001623static void
1624change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001625{
1626 display_in_cyl_units = !display_in_cyl_units;
1627 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001628 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001629 str_units(PLURAL));
1630}
1631
1632static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001633toggle_active(int i)
1634{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635 struct pte *pe = &ptes[i];
1636 struct partition *p = pe->part_table;
1637
Rob Landleyb73451d2006-02-24 16:29:00 +00001638 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001639 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001640 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1641 pe->changed = 1;
1642}
1643
1644static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001645toggle_dos_compatibility_flag(void)
1646{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001647 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001648 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001649 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001650 printf("DOS Compatibility flag is set\n");
1651 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001652 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001653 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001654 }
1655}
1656
1657static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001658delete_partition(int i)
1659{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001660 struct pte *pe = &ptes[i];
1661 struct partition *p = pe->part_table;
1662 struct partition *q = pe->ext_pointer;
1663
1664/* Note that for the fifth partition (i == 4) we don't actually
1665 * decrement partitions.
1666 */
1667
1668 if (warn_geometry())
1669 return; /* C/H/S not set */
1670 pe->changed = 1;
1671
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001672 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001673 sun_delete_partition(i);
1674 return;
1675 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001676 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001677 sgi_delete_partition(i);
1678 return;
1679 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001680
1681 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001682 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001683 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001684 ptes[ext_index].ext_pointer = NULL;
1685 extended_offset = 0;
1686 }
1687 clear_partition(p);
1688 return;
1689 }
1690
1691 if (!q->sys_ind && i > 4) {
1692 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001693 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001694 --i;
1695 clear_partition(ptes[i].ext_pointer);
1696 ptes[i].changed = 1;
1697 } else {
1698 /* not the last one - further ones will be moved down */
1699 if (i > 4) {
1700 /* delete this link in the chain */
1701 p = ptes[i-1].ext_pointer;
1702 *p = *q;
1703 set_start_sect(p, get_start_sect(q));
1704 set_nr_sects(p, get_nr_sects(q));
1705 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001706 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001707 /* the first logical in a longer chain */
1708 pe = &ptes[5];
1709
1710 if (pe->part_table) /* prevent SEGFAULT */
1711 set_start_sect(pe->part_table,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001712 get_partition_start(pe) -
1713 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001714 pe->offset = extended_offset;
1715 pe->changed = 1;
1716 }
1717
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001718 if (g_partitions > 5) {
1719 g_partitions--;
1720 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001721 ptes[i] = ptes[i+1];
1722 i++;
1723 }
1724 } else
1725 /* the only logical: clear only */
1726 clear_partition(ptes[i].part_table);
1727 }
1728}
1729
1730static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001731change_sysid(void)
1732{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001733 int i, sys, origsys;
1734 struct partition *p;
1735
Eric Andersen040f4402003-07-30 08:40:37 +00001736 /* If sgi_label then don't use get_existing_partition,
1737 let the user select a partition, since get_existing_partition()
1738 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001739 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001740 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001741 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001742 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001743 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001744 if (i == -1)
1745 return;
1746 p = ptes[i].part_table;
1747 origsys = sys = get_sysid(i);
1748
1749 /* if changing types T to 0 is allowed, then
1750 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001751 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001752 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001753 return;
1754 }
1755 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001756 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001758 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001759 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001760 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001761 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001762 /* break; */
1763 }
1764
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001765 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001766 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001767 printf("You cannot change a partition into"
1768 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001769 break;
1770 }
1771 }
1772
1773 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001774#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001775 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001776 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001777 "as Whole disk (5),\n"
1778 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001779 "even Linux likes it\n\n");
1780#endif
1781#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001782 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001783 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001784 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001785 (i == 8 && sys != 0)
1786 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001787 ) {
1788 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001789 "as volume header (0),\nand "
1790 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001791 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001792 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001793#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001794 if (sys == origsys)
1795 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001796 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001797 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001798 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001799 sgi_change_sysid(i, sys);
1800 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001802
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001803 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001804 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001805 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001807 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1808 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001809 break;
1810 }
1811 }
1812}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001813#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001814
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001815
Denis Vlasenko28703012006-12-19 20:32:02 +00001816/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001817 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1818 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1819 * Lubkin Oct. 1991). */
1820
Rob Landleyb73451d2006-02-24 16:29:00 +00001821static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001822linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001823{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001824 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001825
1826 *c = ls / spc;
1827 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001828 *h = ls / g_sectors;
1829 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001830}
1831
Rob Landleyb73451d2006-02-24 16:29:00 +00001832static void
1833check_consistency(const struct partition *p, int partition)
1834{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001835 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1836 unsigned pec, peh, pes; /* physical ending c, h, s */
1837 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1838 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001839
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001840 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001841 return; /* do not check extended partitions */
1842
1843/* physical beginning c, h, s */
1844 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1845 pbh = p->head;
1846 pbs = p->sector & 0x3f;
1847
1848/* physical ending c, h, s */
1849 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1850 peh = p->end_head;
1851 pes = p->end_sector & 0x3f;
1852
1853/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001854 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001855
1856/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001857 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001858
1859/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001860 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001861 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001862 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001863 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1864 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001865 }
1866
1867/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001868 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001869 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001870 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001871 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1872 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873 }
1874
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001875/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001876 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001877 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001878 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001879 }
1880}
1881
1882static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001883list_disk_geometry(void)
1884{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001885 ullong bytes = ((ullong)total_number_of_sectors << 9);
1886 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001887
1888 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001889 printf("\nDisk %s: %lu MB, %llu bytes\n",
1890 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001891 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001892 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1893 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1894 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001895 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001896 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001897 printf(", total %"SECT_FMT"u sectors",
1898 total_number_of_sectors / (sector_size/512));
1899 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1900 str_units(PLURAL),
1901 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001902}
1903
1904/*
1905 * Check whether partition entries are ordered by their starting positions.
1906 * Return 0 if OK. Return i if partition i should have been earlier.
1907 * Two separate checks: primary and logical partitions.
1908 */
1909static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001910wrong_p_order(int *prev)
1911{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001912 const struct pte *pe;
1913 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001914 sector_t last_p_start_pos = 0, p_start_pos;
1915 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001916
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001917 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001918 if (i == 4) {
1919 last_i = 4;
1920 last_p_start_pos = 0;
1921 }
1922 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001923 p = pe->part_table;
1924 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001925 p_start_pos = get_partition_start(pe);
1926
1927 if (last_p_start_pos > p_start_pos) {
1928 if (prev)
1929 *prev = last_i;
1930 return i;
1931 }
1932
1933 last_p_start_pos = p_start_pos;
1934 last_i = i;
1935 }
1936 }
1937 return 0;
1938}
1939
Denis Vlasenko834410a2006-11-29 12:00:28 +00001940#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001941/*
1942 * Fix the chain of logicals.
1943 * extended_offset is unchanged, the set of sectors used is unchanged
1944 * The chain is sorted so that sectors increase, and so that
1945 * starting sectors increase.
1946 *
1947 * After this it may still be that cfdisk doesnt like the table.
1948 * (This is because cfdisk considers expanded parts, from link to
1949 * end of partition, and these may still overlap.)
1950 * Now
1951 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1952 * may help.
1953 */
1954static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001955fix_chain_of_logicals(void)
1956{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001957 int j, oj, ojj, sj, sjj;
1958 struct partition *pj,*pjj,tmp;
1959
1960 /* Stage 1: sort sectors but leave sector of part 4 */
1961 /* (Its sector is the global extended_offset.) */
1962 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001963 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001964 oj = ptes[j].offset;
1965 ojj = ptes[j+1].offset;
1966 if (oj > ojj) {
1967 ptes[j].offset = ojj;
1968 ptes[j+1].offset = oj;
1969 pj = ptes[j].part_table;
1970 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1971 pjj = ptes[j+1].part_table;
1972 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1973 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001974 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001975 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001976 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001977 goto stage1;
1978 }
1979 }
1980
1981 /* Stage 2: sort starting sectors */
1982 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001983 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001984 pj = ptes[j].part_table;
1985 pjj = ptes[j+1].part_table;
1986 sj = get_start_sect(pj);
1987 sjj = get_start_sect(pjj);
1988 oj = ptes[j].offset;
1989 ojj = ptes[j+1].offset;
1990 if (oj+sj > ojj+sjj) {
1991 tmp = *pj;
1992 *pj = *pjj;
1993 *pjj = tmp;
1994 set_start_sect(pj, ojj+sjj-oj);
1995 set_start_sect(pjj, oj+sj-ojj);
1996 goto stage2;
1997 }
1998 }
1999
2000 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002001 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002002 ptes[j].changed = 1;
2003}
2004
2005
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002007fix_partition_table_order(void)
2008{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002009 struct pte *pei, *pek;
2010 int i,k;
2011
2012 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002013 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002014 return;
2015 }
2016
2017 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2018 /* partition i should have come earlier, move it */
2019 /* We have to move data in the MBR */
2020 struct partition *pi, *pk, *pe, pbuf;
2021 pei = &ptes[i];
2022 pek = &ptes[k];
2023
2024 pe = pei->ext_pointer;
2025 pei->ext_pointer = pek->ext_pointer;
2026 pek->ext_pointer = pe;
2027
2028 pi = pei->part_table;
2029 pk = pek->part_table;
2030
2031 memmove(&pbuf, pi, sizeof(struct partition));
2032 memmove(pi, pk, sizeof(struct partition));
2033 memmove(pk, &pbuf, sizeof(struct partition));
2034
2035 pei->changed = pek->changed = 1;
2036 }
2037
2038 if (i)
2039 fix_chain_of_logicals();
2040
2041 printf("Done.\n");
2042
2043}
2044#endif
2045
2046static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002047list_table(int xtra)
2048{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002050 int i, w;
2051
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002052 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 sun_list_table(xtra);
2054 return;
2055 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002056 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002057 sgi_list_table(xtra);
2058 return;
2059 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002060
2061 list_disk_geometry();
2062
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002063 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002064 xbsd_print_disklabel(xtra);
2065 return;
2066 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002067
2068 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2069 but if the device name ends in a digit, say /dev/foo1,
2070 then the partition is called /dev/foo1p3. */
2071 w = strlen(disk_device);
2072 if (w && isdigit(disk_device[w-1]))
2073 w++;
2074 if (w < 5)
2075 w = 5;
2076
Denis Vlasenkobd852072007-03-19 14:43:38 +00002077 // 1 12345678901 12345678901 12345678901 12
2078 printf("%*s Boot Start End Blocks Id System\n",
2079 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002080
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002081 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002082 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002083 sector_t psects;
2084 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002085 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002086
2087 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002088 if (!p || is_cleared_partition(p))
2089 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002091 psects = get_nr_sects(p);
2092 pblocks = psects;
2093 podd = 0;
2094
2095 if (sector_size < 1024) {
2096 pblocks /= (1024 / sector_size);
2097 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002098 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002099 if (sector_size > 1024)
2100 pblocks *= (sector_size / 1024);
2101
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002102 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 +00002103 partname(disk_device, i+1, w+2),
2104 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2105 ? '*' : '?',
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002106 cround(get_partition_start(pe)), /* start */
2107 cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002108 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002109 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002110 p->sys_ind, /* type id */
2111 partition_type(p->sys_ind)); /* type name */
2112
2113 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002114 }
2115
2116 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002117 /* partition table entries are not checked for correct order
2118 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002119 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002120 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002121 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002122 }
2123}
2124
Denis Vlasenko834410a2006-11-29 12:00:28 +00002125#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002127x_list_table(int extend)
2128{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002129 const struct pte *pe;
2130 const struct partition *p;
2131 int i;
2132
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002133 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002134 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002135 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002136 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002137 pe = &ptes[i];
2138 p = (extend ? pe->ext_pointer : pe->part_table);
2139 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002140 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 +00002141 i + 1, p->boot_ind, p->head,
2142 sector(p->sector),
2143 cylinder(p->sector, p->cyl), p->end_head,
2144 sector(p->end_sector),
2145 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002146 get_start_sect(p), get_nr_sects(p),
2147 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148 if (p->sys_ind)
2149 check_consistency(p, i);
2150 }
2151 }
2152}
2153#endif
2154
Denis Vlasenko834410a2006-11-29 12:00:28 +00002155#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002157fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002158{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002159 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 const struct pte *pe = &ptes[0];
2161 const struct partition *p;
2162
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002163 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002165 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002166 first[i] = 0xffffffff;
2167 last[i] = 0;
2168 } else {
2169 first[i] = get_partition_start(pe);
2170 last[i] = first[i] + get_nr_sects(p) - 1;
2171 }
2172 }
2173}
2174
2175static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002176check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002177{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002178 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002179
2180 real_s = sector(s) - 1;
2181 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002182 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002183 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002184 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002185 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002186 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002187 n, h + 1, g_heads);
2188 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002189 printf("Partition %u: sector %u greater than "
2190 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002191 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002192 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2193 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002194 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002195 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2196 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002197}
2198
2199static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002200verify(void)
2201{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002203 sector_t total = 1;
2204 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002205 struct partition *p;
2206
2207 if (warn_geometry())
2208 return;
2209
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002210 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002211 verify_sun();
2212 return;
2213 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002214 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002215 verify_sgi(1);
2216 return;
2217 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002218
2219 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002220 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221 struct pte *pe = &ptes[i];
2222
2223 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002224 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 check_consistency(p, i);
2226 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002227 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002228 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002229 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2230 last[i]);
2231 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002232 for (j = 0; j < i; j++) {
2233 if ((first[i] >= first[j] && first[i] <= last[j])
2234 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002235 printf("Warning: partition %u overlaps "
2236 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002237 total += first[i] >= first[j] ?
2238 first[i] : first[j];
2239 total -= last[i] <= last[j] ?
2240 last[i] : last[j];
2241 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 }
2243 }
2244 }
2245
2246 if (extended_offset) {
2247 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002248 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002249 get_nr_sects(pex->part_table) - 1;
2250
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002251 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252 total++;
2253 p = ptes[i].part_table;
2254 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002255 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002256 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002257 "is empty\n", i + 1);
2258 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002259 printf("Logical partition %u not entirely in "
2260 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002261 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002262 }
2263 }
2264
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002265 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002266 printf("Total allocated sectors %u greater than the maximum "
2267 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002268 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002269 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002270 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002271 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002272 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002273}
2274
2275static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002276add_partition(int n, int sys)
2277{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002278 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002279 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002280 struct partition *p = ptes[n].part_table;
2281 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002282 sector_t limit, temp;
2283 sector_t start, stop = 0;
2284 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002285
2286 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002287 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002288 return;
2289 }
2290 fill_bounds(first, last);
2291 if (n < 4) {
2292 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002293 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002294 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002296 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002297 if (extended_offset) {
2298 first[ext_index] = extended_offset;
2299 last[ext_index] = get_start_sect(q) +
2300 get_nr_sects(q) - 1;
2301 }
2302 } else {
2303 start = extended_offset + sector_offset;
2304 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2305 }
2306 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002307 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002308 first[i] = (cround(first[i]) - 1) * units_per_sector;
2309
Denis Vlasenkobd852072007-03-19 14:43:38 +00002310 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002311 do {
2312 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002313 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002314 int lastplusoff;
2315
2316 if (start == ptes[i].offset)
2317 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002318 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 if (start >= first[i] && start <= lastplusoff)
2320 start = lastplusoff + 1;
2321 }
2322 if (start > limit)
2323 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002324 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002325 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002326 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002327 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002328 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002329 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002330 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002331
2332 saved_start = start;
2333 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2334 0, mesg);
2335 if (display_in_cyl_units) {
2336 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002337 if (start < saved_start)
2338 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002339 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002340 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002341 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002342 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002343 if (n > 4) { /* NOT for fifth partition */
2344 struct pte *pe = &ptes[n];
2345
2346 pe->offset = start - sector_offset;
2347 if (pe->offset == extended_offset) { /* must be corrected */
2348 pe->offset++;
2349 if (sector_offset == 1)
2350 start++;
2351 }
2352 }
2353
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002354 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002355 struct pte *pe = &ptes[i];
2356
2357 if (start < pe->offset && limit >= pe->offset)
2358 limit = pe->offset - 1;
2359 if (start < first[i] && limit >= first[i])
2360 limit = first[i] - 1;
2361 }
2362 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002363 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002365 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002366 return;
2367 }
2368 if (cround(start) == cround(limit)) {
2369 stop = limit;
2370 } else {
2371 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002372 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002373 str_units(SINGULAR));
2374 stop = read_int(cround(start), cround(limit), cround(limit),
2375 cround(start), mesg);
2376 if (display_in_cyl_units) {
2377 stop = stop * units_per_sector - 1;
2378 if (stop >limit)
2379 stop = limit;
2380 }
2381 }
2382
2383 set_partition(n, 0, start, stop, sys);
2384 if (n > 4)
2385 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2386
Rob Landleyb73451d2006-02-24 16:29:00 +00002387 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002388 struct pte *pe4 = &ptes[4];
2389 struct pte *pen = &ptes[n];
2390
2391 ext_index = n;
2392 pen->ext_pointer = p;
2393 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002394 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2396 pe4->ext_pointer = pe4->part_table + 1;
2397 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002398 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002399 }
2400}
2401
2402static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002403add_logical(void)
2404{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002405 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2406 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002407
Rob Landley081e3842006-08-03 20:07:35 +00002408 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2410 pe->ext_pointer = pe->part_table + 1;
2411 pe->offset = 0;
2412 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002413 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002414 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002415 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002416}
2417
2418static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002419new_partition(void)
2420{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002421 int i, free_primary = 0;
2422
2423 if (warn_geometry())
2424 return;
2425
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002426 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002427 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002428 return;
2429 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002430 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002431 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 return;
2433 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002434 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002435 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2436"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2437"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002438 return;
2439 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002440
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002441 for (i = 0; i < 4; i++)
2442 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002443
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002444 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002445 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002446 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002447 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002448
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449 if (!free_primary) {
2450 if (extended_offset)
2451 add_logical();
2452 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002453 printf("You must delete some partition and add "
2454 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002456 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002457 snprintf(line, sizeof(line),
2458 "Command action\n"
2459 " %s\n"
2460 " p primary partition (1-4)\n",
2461 (extended_offset ?
2462 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002463 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002464 c = read_nonempty(line);
2465 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 i = get_nonexisting_partition(0, 4);
2467 if (i >= 0)
2468 add_partition(i, LINUX_NATIVE);
2469 return;
2470 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002471 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002472 add_logical();
2473 return;
2474 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002475 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476 i = get_nonexisting_partition(0, 4);
2477 if (i >= 0)
2478 add_partition(i, EXTENDED);
2479 return;
2480 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002481 printf("Invalid partition number "
2482 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483 }
2484 }
2485}
2486
2487static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002488write_table(void)
2489{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002490 int i;
2491
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002492 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002493 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494 if (ptes[i].changed)
2495 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002496 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002497 struct pte *pe = &ptes[i];
2498
2499 if (pe->changed) {
2500 write_part_table_flag(pe->sectorbuffer);
2501 write_sector(pe->offset, pe->sectorbuffer);
2502 }
2503 }
2504 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002505 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002506 /* no test on change? the printf below might be mistaken */
2507 sgi_write_table();
2508 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002509 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510 int needw = 0;
2511
Rob Landleyb73451d2006-02-24 16:29:00 +00002512 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002513 if (ptes[i].changed)
2514 needw = 1;
2515 if (needw)
2516 sun_write_table();
2517 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518
Denis Vlasenkobd852072007-03-19 14:43:38 +00002519 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520 reread_partition_table(1);
2521}
2522
Rob Landleyb73451d2006-02-24 16:29:00 +00002523static void
2524reread_partition_table(int leave)
2525{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 int i;
2527
Denis Vlasenkobd852072007-03-19 14:43:38 +00002528 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002529 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002530 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002531 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002532 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002533 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002534#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002535 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002536 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002537 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002539 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002540#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541
2542 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002543 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002544 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002545 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546 }
2547}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002548#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002549
Denis Vlasenko834410a2006-11-29 12:00:28 +00002550#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551#define MAX_PER_LINE 16
2552static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002553print_buffer(char *pbuffer)
2554{
2555 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556
2557 for (i = 0, l = 0; i < sector_size; i++, l++) {
2558 if (l == 0)
2559 printf("0x%03X:", i);
2560 printf(" %02X", (unsigned char) pbuffer[i]);
2561 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002562 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 l = -1;
2564 }
2565 }
2566 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002567 bb_putchar('\n');
2568 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569}
2570
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002571static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002572print_raw(void)
2573{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 int i;
2575
Denis Vlasenkobd852072007-03-19 14:43:38 +00002576 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002577 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002579 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002580 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002582 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583}
2584
2585static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002586move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002587{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 struct pte *pe = &ptes[i];
2589 struct partition *p = pe->part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002590 sector_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002591
2592 if (warn_geometry())
2593 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002594 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002595 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002596 return;
2597 }
2598 first = get_partition_start(pe);
2599 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002600 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002601
2602 if (new != get_nr_sects(p)) {
2603 first = get_nr_sects(p) + get_start_sect(p) - new;
2604 set_nr_sects(p, first);
2605 set_start_sect(p, new);
2606 pe->changed = 1;
2607 }
2608}
2609
2610static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002611xselect(void)
2612{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 char c;
2614
Rob Landleyb73451d2006-02-24 16:29:00 +00002615 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002616 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002617 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 switch (c) {
2619 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002620 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002621 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 break;
2623 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002624 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002625 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 break;
2627 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002628 user_cylinders = g_cylinders =
2629 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002630 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002631 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002632 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002633 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 warn_cylinders();
2635 break;
2636 case 'd':
2637 print_raw();
2638 break;
2639 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002640 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002642 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002643 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002644 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 x_list_table(1);
2646 break;
2647 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002648 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002649 fix_partition_table_order();
2650 break;
2651 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002652#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002653 create_sgilabel();
2654#endif
2655 break;
2656 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002657 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002658 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002659 update_units();
2660 break;
2661 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002662 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002664 break;
2665 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002666 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002667 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002668 break;
2669 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002670 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002671 list_table(1);
2672 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002673 x_list_table(0);
2674 break;
2675 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002676 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002677 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002678 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002679 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002680 case 'r':
2681 return;
2682 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002683 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002684 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002686 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002687 printf("Warning: setting sector offset for DOS "
2688 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002689 }
2690 update_units();
2691 break;
2692 case 'v':
2693 verify();
2694 break;
2695 case 'w':
2696 write_table(); /* does not return */
2697 break;
2698 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002699 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002700 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002701 break;
2702 default:
2703 xmenu();
2704 }
2705 }
2706}
2707#endif /* ADVANCED mode */
2708
2709static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002710is_ide_cdrom_or_tape(const char *device)
2711{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002712 FILE *procf;
2713 char buf[100];
2714 struct stat statbuf;
2715 int is_ide = 0;
2716
2717 /* No device was given explicitly, and we are trying some
2718 likely things. But opening /dev/hdc may produce errors like
2719 "hdc: tray open or drive not ready"
2720 if it happens to be a CD-ROM drive. It even happens that
2721 the process hangs on the attempt to read a music CD.
2722 So try to be careful. This only works since 2.1.73. */
2723
2724 if (strncmp("/dev/hd", device, 7))
2725 return 0;
2726
2727 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002728 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002729 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2730 is_ide = (!strncmp(buf, "cdrom", 5) ||
2731 !strncmp(buf, "tape", 4));
2732 else
2733 /* Now when this proc file does not exist, skip the
2734 device when it is read-only. */
2735 if (stat(device, &statbuf) == 0)
2736 is_ide = ((statbuf.st_mode & 0222) == 0);
2737
2738 if (procf)
2739 fclose(procf);
2740 return is_ide;
2741}
2742
Rob Landley5527b912006-02-25 03:46:10 +00002743
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002744static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002745open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002746{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002747 int gb;
2748
2749 disk_device = device;
2750 if (setjmp(listingbuf))
2751 return;
2752 if (!user_specified)
2753 if (is_ide_cdrom_or_tape(device))
2754 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002755
2756 /* Open disk_device, save file descriptor to dev_fd */
2757 errno = 0;
2758 gb = get_boot(TRY_ONLY);
2759 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002760 /* Ignore other errors, since we try IDE
2761 and SCSI hard disks which may not be
2762 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002763 if (user_specified || errno == EACCES)
2764 bb_perror_msg("can't open '%s'", device);
2765 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002766 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002767
2768 if (gb < 0) { /* no DOS signature */
2769 list_disk_geometry();
2770 if (LABEL_IS_AIX)
2771 goto ret;
2772#if ENABLE_FEATURE_OSF_LABEL
2773 if (bsd_trydev(device) < 0)
2774#endif
2775 printf("Disk %s doesn't contain a valid "
2776 "partition table\n", device);
2777 } else {
2778 list_table(0);
2779#if ENABLE_FEATURE_FDISK_WRITABLE
2780 if (!LABEL_IS_SUN && g_partitions > 4) {
2781 delete_partition(ext_index);
2782 }
2783#endif
2784 }
2785 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002786 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002787}
2788
2789/* for fdisk -l: try all things in /proc/partitions
2790 that look like a partition name (do not end in a digit) */
2791static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002792list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002793{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002794 FILE *procpt;
2795 char line[100], ptname[100], devname[120], *s;
2796 int ma, mi, sz;
2797
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002798 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002799
2800 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002801 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002802 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002803 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002804 for (s = ptname; *s; s++)
Denys Vlasenkobf1d3472010-02-24 08:13:30 +01002805 continue;
2806 /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
2807 if (s[-1] >= '1' && s[-1] <= '9')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002808 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002809 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002810 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002812#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002813 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002814#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002815}
2816
Denis Vlasenko834410a2006-11-29 12:00:28 +00002817#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002818static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002819unknown_command(int c)
2820{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002821 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002823#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002825int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002826int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002827{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002828 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002829 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002830 * fdisk -v
2831 * fdisk -l [-b sectorsize] [-u] device ...
2832 * fdisk -s [partition] ...
2833 * fdisk [-b sectorsize] [-u] device
2834 *
2835 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002836 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002837 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002838
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002839 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002840
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002841 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002842 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002843 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002844 argv += optind;
2845 if (opt & OPT_b) { // -b
2846 /* Ugly: this sector size is really per device,
2847 so cannot be combined with multiple disks,
2848 and the same goes for the C/H/S options.
2849 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002850 if (sector_size != 512 && sector_size != 1024
2851 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002852 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002853 sector_offset = 2;
2854 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002856 if (user_heads <= 0 || user_heads >= 256)
2857 user_heads = 0;
2858 if (user_sectors <= 0 || user_sectors >= 64)
2859 user_sectors = 0;
2860 if (opt & OPT_u)
2861 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862
Denis Vlasenko834410a2006-11-29 12:00:28 +00002863#if ENABLE_FEATURE_FDISK_WRITABLE
2864 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002866#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002867 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002869 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002870 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002871 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002873 /* we don't have device names, */
2874 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002875 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 }
2877 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002878#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002879 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002880#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881
Denis Vlasenko834410a2006-11-29 12:00:28 +00002882#if ENABLE_FEATURE_FDISK_BLKSIZE
2883 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002884 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885
2886 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002887 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002888 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002889 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002890 unsigned long long size;
2891 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002892 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002894 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002895 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002897 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898 }
2899 return 0;
2900 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002901#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902
Denis Vlasenko834410a2006-11-29 12:00:28 +00002903#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002904 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002905 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906
Denis Vlasenko834410a2006-11-29 12:00:28 +00002907 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002908 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002910 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002911 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002912 printf("Detected an OSF/1 disklabel on %s, entering "
2913 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002914 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002915 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002916 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 /* If we return we may want to make an empty DOS label? */
2918 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002919
2920 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002921 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002922 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002923 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 switch (c) {
2925 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002926 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002927 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002928 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002929 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002931 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002933 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002934 else
2935 unknown_command(c);
2936 break;
2937 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002938 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002939 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002940 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002941 if (read_maybe_empty("Please enter the name of the "
2942 "new boot file: ") == '\n')
2943 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002944 else
2945 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002946 }
2947#if ENABLE_FEATURE_OSF_LABEL
2948 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002949 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950#endif
2951 break;
2952 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002953 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002954 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002955 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002956 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002957 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002958 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002960 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002961 else
2962 unknown_command(c);
2963 break;
2964 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002965 {
Eric Andersen040f4402003-07-30 08:40:37 +00002966 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002967 /* If sgi_label then don't use get_existing_partition,
2968 let the user select a partition, since
2969 get_existing_partition() only works for Linux-like
2970 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002971 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002972 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002973 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002974 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002975 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002976 if (j >= 0)
2977 delete_partition(j);
2978 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002979 break;
2980 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002981 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 create_sgiinfo();
2983 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 unknown_command(c);
2985 case 'l':
2986 list_types(get_sys_types());
2987 break;
2988 case 'm':
2989 menu();
2990 break;
2991 case 'n':
2992 new_partition();
2993 break;
2994 case 'o':
2995 create_doslabel();
2996 break;
2997 case 'p':
2998 list_table(0);
2999 break;
3000 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003001 if (ENABLE_FEATURE_CLEAN_UP)
3002 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003003 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003004 return 0;
3005 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003006#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003007 create_sunlabel();
3008#endif
3009 break;
3010 case 't':
3011 change_sysid();
3012 break;
3013 case 'u':
3014 change_units();
3015 break;
3016 case 'v':
3017 verify();
3018 break;
3019 case 'w':
3020 write_table(); /* does not return */
3021 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003022#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003023 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003024 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003025 printf("\n\tSorry, no experts menu for SGI "
3026 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003027 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003028 xselect();
3029 break;
3030#endif
3031 default:
3032 unknown_command(c);
3033 menu();
3034 }
3035 }
3036 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003037#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003038}