blob: c9f57c6d67c546ab6effaaf840568c54033f880e [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
628typedef struct {
629 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 */
654} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000656STATIC_OSF void bsd_select(void);
657STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000658#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000659
Denis Vlasenko28703012006-12-19 20:32:02 +0000660#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000661static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000662fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000663{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000664 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000665}
666
Rob Landley88621d72006-08-29 19:41:06 +0000667static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000668fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000669{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000670 return (x << 24) |
671 ((x & 0xFF00) << 8) |
672 ((x & 0xFF0000) >> 8) |
673 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674}
675#endif
676
Denis Vlasenkobd852072007-03-19 14:43:38 +0000677STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000678STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000679STATIC_SGI int sgi_get_sysid(int i);
680STATIC_SGI void sgi_delete_partition(int i);
681STATIC_SGI void sgi_change_sysid(int i, int sys);
682STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000683#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000684STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000685#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000686STATIC_SGI int verify_sgi(int verbose);
687STATIC_SGI void sgi_add_partition(int n, int sys);
688STATIC_SGI void sgi_set_swappartition(int i);
689STATIC_SGI const char *sgi_get_bootfile(void);
690STATIC_SGI void sgi_set_bootfile(const char* aFile);
691STATIC_SGI void create_sgiinfo(void);
692STATIC_SGI void sgi_write_table(void);
693STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000694#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695
Denis Vlasenkobd852072007-03-19 14:43:38 +0000696STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000697STATIC_SUN void sun_delete_partition(int i);
698STATIC_SUN void sun_change_sysid(int i, int sys);
699STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000700STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000701#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000702STATIC_SUN void sun_set_alt_cyl(void);
703STATIC_SUN void sun_set_ncyl(int cyl);
704STATIC_SUN void sun_set_xcyl(void);
705STATIC_SUN void sun_set_ilfact(void);
706STATIC_SUN void sun_set_rspeed(void);
707STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000708#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000709STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
710STATIC_SUN void verify_sun(void);
711STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000712#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713
Denis Vlasenko9764d692008-07-09 21:20:50 +0000714
Denis Vlasenko834410a2006-11-29 12:00:28 +0000715#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000716/* start_sect and nr_sects are stored little endian on all machines */
717/* moreover, they are not aligned correctly */
718static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000719store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000720{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000721 cp[0] = val;
722 cp[1] = val >> 8;
723 cp[2] = val >> 16;
724 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000725}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000726#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000727
Denis Vlasenko834410a2006-11-29 12:00:28 +0000728static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000729read4_little_endian(const unsigned char *cp)
730{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000731 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000732}
733
Denis Vlasenko834410a2006-11-29 12:00:28 +0000734#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000735static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000736set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000737{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000738 store4_little_endian(p->start4, start_sect);
739}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000740#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000741
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200742static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000743get_start_sect(const struct partition *p)
744{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000745 return read4_little_endian(p->start4);
746}
747
Denis Vlasenko834410a2006-11-29 12:00:28 +0000748#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000749static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000750set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000751{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000752 store4_little_endian(p->size4, nr_sects);
753}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000754#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000755
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200756static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000757get_nr_sects(const struct partition *p)
758{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759 return read4_little_endian(p->size4);
760}
761
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000762/* Allocate a buffer and read a partition table sector */
763static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200764read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000765{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000766 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000767 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000768 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000769 /* xread would make us abort - bad for fdisk -l */
770 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000771 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000772#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000773 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000774#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000775 pe->part_table = pe->ext_pointer = NULL;
776}
777
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200778static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000779get_partition_start(const struct pte *pe)
780{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000781 return pe->offset + get_start_sect(pe->part_table);
782}
783
Denis Vlasenko834410a2006-11-29 12:00:28 +0000784#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000785/*
786 * Avoid warning about DOS partitions when no DOS partition was changed.
787 * Here a heuristic "is probably dos partition".
788 * We might also do the opposite and warn in all cases except
789 * for "is probably nondos partition".
790 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000791#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000792static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000793is_dos_partition(int t)
794{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000795 return (t == 1 || t == 4 || t == 6 ||
796 t == 0x0b || t == 0x0c || t == 0x0e ||
797 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
798 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
799 t == 0xc1 || t == 0xc4 || t == 0xc6);
800}
Denis Vlasenko89398812008-01-25 20:18:46 +0000801#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000802
803static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000804menu(void)
805{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000806 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000807 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000808 puts("a\ttoggle a read only flag"); /* sun */
809 puts("b\tedit bsd disklabel");
810 puts("c\ttoggle the mountable flag"); /* sun */
811 puts("d\tdelete a partition");
812 puts("l\tlist known partition types");
813 puts("n\tadd a new partition");
814 puts("o\tcreate a new empty DOS partition table");
815 puts("p\tprint the partition table");
816 puts("q\tquit without saving changes");
817 puts("s\tcreate a new empty Sun disklabel"); /* sun */
818 puts("t\tchange a partition's system id");
819 puts("u\tchange display/entry units");
820 puts("v\tverify the partition table");
821 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000822#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000823 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000824#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000825 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000826 puts("a\tselect bootable partition"); /* sgi flavour */
827 puts("b\tedit bootfile entry"); /* sgi */
828 puts("c\tselect sgi swap partition"); /* sgi flavour */
829 puts("d\tdelete a partition");
830 puts("l\tlist known partition types");
831 puts("n\tadd a new partition");
832 puts("o\tcreate a new empty DOS partition table");
833 puts("p\tprint the partition table");
834 puts("q\tquit without saving changes");
835 puts("s\tcreate a new empty Sun disklabel"); /* sun */
836 puts("t\tchange a partition's system id");
837 puts("u\tchange display/entry units");
838 puts("v\tverify the partition table");
839 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000840 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000841 puts("o\tcreate a new empty DOS partition table");
842 puts("q\tquit without saving changes");
843 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000844 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000845 puts("a\ttoggle a bootable flag");
846 puts("b\tedit bsd disklabel");
847 puts("c\ttoggle the dos compatibility flag");
848 puts("d\tdelete a partition");
849 puts("l\tlist known partition types");
850 puts("n\tadd a new partition");
851 puts("o\tcreate a new empty DOS partition table");
852 puts("p\tprint the partition table");
853 puts("q\tquit without saving changes");
854 puts("s\tcreate a new empty Sun disklabel"); /* sun */
855 puts("t\tchange a partition's system id");
856 puts("u\tchange display/entry units");
857 puts("v\tverify the partition table");
858 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000859#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000860 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861#endif
862 }
863}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000864#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000865
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000866
Denis Vlasenko834410a2006-11-29 12:00:28 +0000867#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000868static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000869xmenu(void)
870{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000871 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000872 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000873 puts("a\tchange number of alternate cylinders"); /*sun*/
874 puts("c\tchange number of cylinders");
875 puts("d\tprint the raw data in the partition table");
876 puts("e\tchange number of extra sectors per cylinder");/*sun*/
877 puts("h\tchange number of heads");
878 puts("i\tchange interleave factor"); /*sun*/
879 puts("o\tchange rotation speed (rpm)"); /*sun*/
880 puts("p\tprint the partition table");
881 puts("q\tquit without saving changes");
882 puts("r\treturn to main menu");
883 puts("s\tchange number of sectors/track");
884 puts("v\tverify the partition table");
885 puts("w\twrite table to disk and exit");
886 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000887 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000888 puts("b\tmove beginning of data in a partition"); /* !sun */
889 puts("c\tchange number of cylinders");
890 puts("d\tprint the raw data in the partition table");
891 puts("e\tlist extended partitions"); /* !sun */
892 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
893 puts("h\tchange number of heads");
894 puts("p\tprint the partition table");
895 puts("q\tquit without saving changes");
896 puts("r\treturn to main menu");
897 puts("s\tchange number of sectors/track");
898 puts("v\tverify the partition table");
899 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000900 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000901 puts("b\tmove beginning of data in a partition"); /* !sun */
902 puts("c\tchange number of cylinders");
903 puts("d\tprint the raw data in the partition table");
904 puts("e\tlist extended partitions"); /* !sun */
905 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
906 puts("h\tchange number of heads");
907 puts("p\tprint the partition table");
908 puts("q\tquit without saving changes");
909 puts("r\treturn to main menu");
910 puts("s\tchange number of sectors/track");
911 puts("v\tverify the partition table");
912 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000913 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000914 puts("b\tmove beginning of data in a partition"); /* !sun */
915 puts("c\tchange number of cylinders");
916 puts("d\tprint the raw data in the partition table");
917 puts("e\tlist extended partitions"); /* !sun */
918 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000919#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000920 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000921#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000922 puts("h\tchange number of heads");
923 puts("p\tprint the partition table");
924 puts("q\tquit without saving changes");
925 puts("r\treturn to main menu");
926 puts("s\tchange number of sectors/track");
927 puts("v\tverify the partition table");
928 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929 }
930}
931#endif /* ADVANCED mode */
932
Denis Vlasenko834410a2006-11-29 12:00:28 +0000933#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000934static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000935get_sys_types(void)
936{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000937 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000938 LABEL_IS_SUN ? sun_sys_types :
939 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000940 i386_sys_types);
941}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000942#else
943#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000944#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000945
Denis Vlasenkobd852072007-03-19 14:43:38 +0000946static const char *
947partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000948{
949 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000950 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000951
Denis Vlasenkobd852072007-03-19 14:43:38 +0000952 for (i = 0; types[i]; i++)
953 if ((unsigned char)types[i][0] == type)
954 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000955
Denis Vlasenkobd852072007-03-19 14:43:38 +0000956 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000957}
958
959
Denis Vlasenko834410a2006-11-29 12:00:28 +0000960#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000961static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000962get_sysid(int i)
963{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000964 return LABEL_IS_SUN ? sunlabel->infos[i].id :
965 (LABEL_IS_SGI ? sgi_get_sysid(i) :
966 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000967}
968
Denis Vlasenkobd852072007-03-19 14:43:38 +0000969static void
970list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000972 enum { COLS = 3 };
973
974 unsigned last[COLS];
975 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000976 int i;
977
Denis Vlasenko9764d692008-07-09 21:20:50 +0000978 for (size = 0; sys[size]; size++)
979 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000981 done = 0;
982 for (i = COLS-1; i >= 0; i--) {
983 done += (size + i - done) / (i + 1);
984 last[COLS-1 - i] = done;
985 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000986
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000987 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000988 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000989 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000990 (unsigned char)sys[next][0],
991 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000992 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994 i = 0;
995 next = ++done;
996 }
997 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000998 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000999}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001000#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001001
1002static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001003is_cleared_partition(const struct partition *p)
1004{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001005 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
1006 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
1007 get_start_sect(p) || get_nr_sects(p));
1008}
1009
1010static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001011clear_partition(struct partition *p)
1012{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013 if (!p)
1014 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001015 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001016}
1017
Denis Vlasenko834410a2006-11-29 12:00:28 +00001018#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001020set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001021{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001022 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001023 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001024
1025 if (doext) {
1026 p = ptes[i].ext_pointer;
1027 offset = extended_offset;
1028 } else {
1029 p = ptes[i].part_table;
1030 offset = ptes[i].offset;
1031 }
1032 p->boot_ind = 0;
1033 p->sys_ind = sysid;
1034 set_start_sect(p, start - offset);
1035 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001036 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1037 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001038 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001039 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1040 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001041 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1042 ptes[i].changed = 1;
1043}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001044#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045
1046static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001047warn_geometry(void)
1048{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001049 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001051
Denis Vlasenkobd852072007-03-19 14:43:38 +00001052 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001053 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001054 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001055 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001056 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001057 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001058 printf(" cylinders");
1059 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001060#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001061 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001062#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001063 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001064 return 1;
1065}
1066
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001067static void
1068update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001069{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001070 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001071
1072 if (display_in_cyl_units && cyl_units)
1073 units_per_sector = cyl_units;
1074 else
1075 units_per_sector = 1; /* in sectors */
1076}
1077
Denis Vlasenko834410a2006-11-29 12:00:28 +00001078#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001080warn_cylinders(void)
1081{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001082 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001083 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001084"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001085"There is nothing wrong with that, but this is larger than 1024,\n"
1086"and could in certain setups cause problems with:\n"
1087"1) software that runs at boot time (e.g., old versions of LILO)\n"
1088"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001089" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001090 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001091}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001092#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001093
1094static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001095read_extended(int ext)
1096{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001097 int i;
1098 struct pte *pex;
1099 struct partition *p, *q;
1100
1101 ext_index = ext;
1102 pex = &ptes[ext];
1103 pex->ext_pointer = pex->part_table;
1104
1105 p = pex->part_table;
1106 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001107 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001108 return;
1109 }
1110
Rob Landleyb73451d2006-02-24 16:29:00 +00001111 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001112 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001114 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115 /* This is not a Linux restriction, but
1116 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001117 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001118 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001119#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001120 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001121 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001123#endif
1124 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125 return;
1126 }
1127
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001128 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001129
1130 if (!extended_offset)
1131 extended_offset = get_start_sect(p);
1132
1133 q = p = pt_offset(pe->sectorbuffer, 0);
1134 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001135 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001136 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001137 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001138 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001139 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001140 else
1141 pe->ext_pointer = p;
1142 } else if (p->sys_ind) {
1143 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001144 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001145 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001146 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001147 else
1148 pe->part_table = p;
1149 }
1150 }
1151
1152 /* very strange code here... */
1153 if (!pe->part_table) {
1154 if (q != pe->ext_pointer)
1155 pe->part_table = q;
1156 else
1157 pe->part_table = q + 1;
1158 }
1159 if (!pe->ext_pointer) {
1160 if (q != pe->part_table)
1161 pe->ext_pointer = q;
1162 else
1163 pe->ext_pointer = q + 1;
1164 }
1165
1166 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001167 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001168 }
1169
Denis Vlasenko834410a2006-11-29 12:00:28 +00001170#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 /* remove empty links */
1172 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001173 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001174 struct pte *pe = &ptes[i];
1175
Denis Vlasenkobd852072007-03-19 14:43:38 +00001176 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001177 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001178 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001179 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001180 delete_partition(i);
1181 goto remove; /* numbering changed */
1182 }
1183 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001184#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185}
1186
Denis Vlasenko834410a2006-11-29 12:00:28 +00001187#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001189create_doslabel(void)
1190{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 int i;
1192
Denis Vlasenkobd852072007-03-19 14:43:38 +00001193 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001194
Denis Vlasenko4437d192008-04-17 00:12:10 +00001195 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001196
Denis Vlasenko834410a2006-11-29 12:00:28 +00001197#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001198 possibly_osf_label = 0;
1199#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001200 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001201
1202 for (i = 510-64; i < 510; i++)
1203 MBRbuffer[i] = 0;
1204 write_part_table_flag(MBRbuffer);
1205 extended_offset = 0;
1206 set_all_unchanged();
1207 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001208 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001209}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001210#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001211
1212static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001213get_sectorsize(void)
1214{
Rob Landley736e5252006-02-25 03:36:00 +00001215 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001216 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001217 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001218 sector_size = arg;
1219 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001220 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001221 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1222 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001223 }
1224}
1225
Rob Landley88621d72006-08-29 19:41:06 +00001226static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001227get_kernel_geometry(void)
1228{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229 struct hd_geometry geometry;
1230
Denis Vlasenko4437d192008-04-17 00:12:10 +00001231 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001232 kern_heads = geometry.heads;
1233 kern_sectors = geometry.sectors;
1234 /* never use geometry.cylinders - it is truncated */
1235 }
1236}
1237
1238static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001239get_partition_table_geometry(void)
1240{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001241 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242 struct partition *p;
1243 int i, h, s, hh, ss;
1244 int first = 1;
1245 int bad = 0;
1246
Eric Andersen3496fdc2006-01-30 23:09:20 +00001247 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001248 return;
1249
1250 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001251 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 p = pt_offset(bufp, i);
1253 if (p->sys_ind != 0) {
1254 h = p->end_head + 1;
1255 s = (p->end_sector & 077);
1256 if (first) {
1257 hh = h;
1258 ss = s;
1259 first = 0;
1260 } else if (hh != h || ss != s)
1261 bad = 1;
1262 }
1263 }
1264
1265 if (!first && !bad) {
1266 pt_heads = hh;
1267 pt_sectors = ss;
1268 }
1269}
1270
Rob Landleyb73451d2006-02-24 16:29:00 +00001271static void
1272get_geometry(void)
1273{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001274 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001275
1276 get_sectorsize();
1277 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001278#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279 guess_device_type();
1280#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001281 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001282 kern_heads = kern_sectors = 0;
1283 pt_heads = pt_sectors = 0;
1284
1285 get_kernel_geometry();
1286 get_partition_table_geometry();
1287
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001288 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289 pt_heads ? pt_heads :
1290 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001291 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292 pt_sectors ? pt_sectors :
1293 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001294 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001295
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296 sector_offset = 1;
1297 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001298 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001299
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001300 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1301 if (!g_cylinders)
1302 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303}
1304
1305/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001306 * Opens disk_device and optionally reads MBR.
1307 * FIXME: document what each 'what' value will do!
1308 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1310 * 0: found or created label
1311 * 1: I/O error
1312 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001313#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1314static int get_boot(enum action what)
1315#else
1316static int get_boot(void)
1317#define get_boot(what) get_boot()
1318#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001319{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001320 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001321
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001322 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323 for (i = 0; i < 4; i++) {
1324 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325 pe->part_table = pt_offset(MBRbuffer, i);
1326 pe->ext_pointer = NULL;
1327 pe->offset = 0;
1328 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001329#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001330 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001331#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001332 }
1333
Denis Vlasenko834410a2006-11-29 12:00:28 +00001334#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001335// ALERT! highly idiotic design!
1336// We end up here when we call get_boot() recursively
1337// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1338// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1339// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1340// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001341 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001342 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343
Denis Vlasenko4437d192008-04-17 00:12:10 +00001344 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1345
Denis Vlasenkobd852072007-03-19 14:43:38 +00001346 if (fd < 0) {
1347 fd = open(disk_device, O_RDONLY);
1348 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001349 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001350 return 1;
1351 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001352 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001353 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001354 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001355 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001356 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001357 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001358 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001360 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001361 fdisk_fatal(unable_to_read);
1362 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001363#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001364 fd = open(disk_device, O_RDONLY);
1365 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001366 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001367 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001368 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001369 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001370 }
1371 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001372#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001373
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001374 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001375 update_units();
1376
Denis Vlasenko834410a2006-11-29 12:00:28 +00001377#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 if (check_sun_label())
1379 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001380#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001381#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001382 if (check_sgi_label())
1383 return 0;
1384#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001385#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 if (check_aix_label())
1387 return 0;
1388#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001389#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390 if (check_osf_label()) {
1391 possibly_osf_label = 1;
1392 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001393 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001394 return 0;
1395 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001396 printf("This disk has both DOS and BSD magic.\n"
1397 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001398 }
1399#endif
1400
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001401#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001402 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001403 return -1;
1404#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001405 if (!valid_part_table_flag(MBRbuffer)) {
1406 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001407 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001408 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001409 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001410#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001411 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001412#else
1413 create_doslabel();
1414#endif
1415 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001417 /* TRY_ONLY: */
1418 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001419 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001420 created_table:
1421#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001422
Denis Vlasenko4437d192008-04-17 00:12:10 +00001423
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001424 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001425 warn_geometry();
1426
1427 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001428 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001429 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001430 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001431 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432 else
1433 read_extended(i);
1434 }
1435 }
1436
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001437 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001438 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001439 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001440 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001441 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001442 pe->sectorbuffer[510],
1443 pe->sectorbuffer[511],
1444 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001445 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001446 }
1447 }
1448
1449 return 0;
1450}
1451
Denis Vlasenko834410a2006-11-29 12:00:28 +00001452#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001453/*
1454 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1455 * If the user hits Enter, DFLT is returned.
1456 * Answers like +10 are interpreted as offsets from BASE.
1457 *
1458 * There is no default if DFLT is not between LOW and HIGH.
1459 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001460static sector_t
1461read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001462{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001463 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001464 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001465 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001466
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001467 if (dflt < low || dflt > high) {
1468 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001469 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001470 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471
1472 while (1) {
1473 int use_default = default_ok;
1474
1475 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001476 do {
1477 printf(fmt, mesg, low, high, dflt);
1478 read_maybe_empty("");
1479 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1480 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481
Eric Andersen84bdea82004-05-19 10:49:17 +00001482 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001483 int minus = (*line_ptr == '-');
1484 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001485
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001486 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001487
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001488 /* (1) if 2nd char is digit, use_default = 0.
1489 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001490 while (isdigit(*++line_ptr))
1491 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001492
Rob Landleyb73451d2006-02-24 16:29:00 +00001493 switch (*line_ptr) {
1494 case 'c':
1495 case 'C':
1496 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001497 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001498 break;
1499 case 'K':
1500 absolute = 1024;
1501 break;
1502 case 'k':
1503 absolute = 1000;
1504 break;
1505 case 'm':
1506 case 'M':
1507 absolute = 1000000;
1508 break;
1509 case 'g':
1510 case 'G':
1511 absolute = 1000000000;
1512 break;
1513 default:
1514 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001515 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001516 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001517 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001518 unsigned long unit;
1519
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001520 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001521 unit = sector_size * units_per_sector;
1522 bytes += unit/2; /* round */
1523 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001524 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001525 }
1526 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001527 value = -value;
1528 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001529 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001530 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001531 while (isdigit(*line_ptr)) {
1532 line_ptr++;
1533 use_default = 0;
1534 }
1535 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001536 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001537 value = dflt;
1538 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001539 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001540 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001541 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001542 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001543 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001544 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001545}
1546
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001547static unsigned
1548get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001549{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001550 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001551 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001552
Denis Vlasenkobd852072007-03-19 14:43:38 +00001553 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554 pe = &ptes[i];
1555
1556 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001557 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1558 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1559 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1560 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001561 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001562 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001563 }
1564 return i;
1565}
1566
1567static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001568get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001569{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001571 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001572
1573 for (i = 0; i < max; i++) {
1574 struct pte *pe = &ptes[i];
1575 struct partition *p = pe->part_table;
1576
1577 if (p && !is_cleared_partition(p)) {
1578 if (pno >= 0)
1579 goto not_unique;
1580 pno = i;
1581 }
1582 }
1583 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001584 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585 return pno;
1586 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001587 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001588 return -1;
1589
1590 not_unique:
1591 return get_partition(warn, max);
1592}
1593
1594static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001595get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001596{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001597 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001598 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001599
1600 for (i = 0; i < max; i++) {
1601 struct pte *pe = &ptes[i];
1602 struct partition *p = pe->part_table;
1603
1604 if (p && is_cleared_partition(p)) {
1605 if (pno >= 0)
1606 goto not_unique;
1607 pno = i;
1608 }
1609 }
1610 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001611 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001612 return pno;
1613 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001614 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001615 return -1;
1616
1617 not_unique:
1618 return get_partition(warn, max);
1619}
1620
1621
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001622static void
1623change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001624{
1625 display_in_cyl_units = !display_in_cyl_units;
1626 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001627 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001628 str_units(PLURAL));
1629}
1630
1631static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001632toggle_active(int i)
1633{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001634 struct pte *pe = &ptes[i];
1635 struct partition *p = pe->part_table;
1636
Rob Landleyb73451d2006-02-24 16:29:00 +00001637 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001638 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001639 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1640 pe->changed = 1;
1641}
1642
1643static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001644toggle_dos_compatibility_flag(void)
1645{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001646 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001647 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001648 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001649 printf("DOS Compatibility flag is set\n");
1650 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001651 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001652 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001653 }
1654}
1655
1656static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001657delete_partition(int i)
1658{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001659 struct pte *pe = &ptes[i];
1660 struct partition *p = pe->part_table;
1661 struct partition *q = pe->ext_pointer;
1662
1663/* Note that for the fifth partition (i == 4) we don't actually
1664 * decrement partitions.
1665 */
1666
1667 if (warn_geometry())
1668 return; /* C/H/S not set */
1669 pe->changed = 1;
1670
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001671 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001672 sun_delete_partition(i);
1673 return;
1674 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001675 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676 sgi_delete_partition(i);
1677 return;
1678 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001679
1680 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001681 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001682 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001683 ptes[ext_index].ext_pointer = NULL;
1684 extended_offset = 0;
1685 }
1686 clear_partition(p);
1687 return;
1688 }
1689
1690 if (!q->sys_ind && i > 4) {
1691 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001692 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001693 --i;
1694 clear_partition(ptes[i].ext_pointer);
1695 ptes[i].changed = 1;
1696 } else {
1697 /* not the last one - further ones will be moved down */
1698 if (i > 4) {
1699 /* delete this link in the chain */
1700 p = ptes[i-1].ext_pointer;
1701 *p = *q;
1702 set_start_sect(p, get_start_sect(q));
1703 set_nr_sects(p, get_nr_sects(q));
1704 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001705 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001706 /* the first logical in a longer chain */
1707 pe = &ptes[5];
1708
1709 if (pe->part_table) /* prevent SEGFAULT */
1710 set_start_sect(pe->part_table,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001711 get_partition_start(pe) -
1712 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001713 pe->offset = extended_offset;
1714 pe->changed = 1;
1715 }
1716
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001717 if (g_partitions > 5) {
1718 g_partitions--;
1719 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001720 ptes[i] = ptes[i+1];
1721 i++;
1722 }
1723 } else
1724 /* the only logical: clear only */
1725 clear_partition(ptes[i].part_table);
1726 }
1727}
1728
1729static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001730change_sysid(void)
1731{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001732 int i, sys, origsys;
1733 struct partition *p;
1734
Eric Andersen040f4402003-07-30 08:40:37 +00001735 /* If sgi_label then don't use get_existing_partition,
1736 let the user select a partition, since get_existing_partition()
1737 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001738 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001739 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001740 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001741 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001742 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001743 if (i == -1)
1744 return;
1745 p = ptes[i].part_table;
1746 origsys = sys = get_sysid(i);
1747
1748 /* if changing types T to 0 is allowed, then
1749 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001750 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001751 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001752 return;
1753 }
1754 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001755 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001757 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001758 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001759 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001760 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761 /* break; */
1762 }
1763
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001764 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001765 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001766 printf("You cannot change a partition into"
1767 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001768 break;
1769 }
1770 }
1771
1772 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001773#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001774 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001775 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001776 "as Whole disk (5),\n"
1777 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001778 "even Linux likes it\n\n");
1779#endif
1780#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001781 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001782 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001783 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001784 (i == 8 && sys != 0)
1785 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001786 ) {
1787 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001788 "as volume header (0),\nand "
1789 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001790 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001791 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001792#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793 if (sys == origsys)
1794 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001795 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001796 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001797 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798 sgi_change_sysid(i, sys);
1799 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001800 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001801
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001802 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001803 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001804 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001806 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1807 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001808 break;
1809 }
1810 }
1811}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001812#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001813
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001814
Denis Vlasenko28703012006-12-19 20:32:02 +00001815/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001816 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1817 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1818 * Lubkin Oct. 1991). */
1819
Rob Landleyb73451d2006-02-24 16:29:00 +00001820static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001821linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001822{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001823 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001824
1825 *c = ls / spc;
1826 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001827 *h = ls / g_sectors;
1828 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829}
1830
Rob Landleyb73451d2006-02-24 16:29:00 +00001831static void
1832check_consistency(const struct partition *p, int partition)
1833{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001834 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1835 unsigned pec, peh, pes; /* physical ending c, h, s */
1836 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1837 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001838
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001839 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001840 return; /* do not check extended partitions */
1841
1842/* physical beginning c, h, s */
1843 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1844 pbh = p->head;
1845 pbs = p->sector & 0x3f;
1846
1847/* physical ending c, h, s */
1848 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1849 peh = p->end_head;
1850 pes = p->end_sector & 0x3f;
1851
1852/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001853 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001854
1855/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001856 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001857
1858/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001859 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001860 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001861 "beginnings (non-Linux?):\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001862 printf(" phys=(%u, %u, %u) ", pbc, pbh, pbs);
1863 printf("logical=(%u, %u, %u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001864 }
1865
1866/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001867 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001868 printf("Partition %u has different physical/logical "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001869 "endings:\n", partition + 1);
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001870 printf(" phys=(%u, %u, %u) ", pec, peh, pes);
1871 printf("logical=(%u, %u, %u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001872 }
1873
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001874/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001875 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001876 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001877 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001878 }
1879}
1880
1881static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001882list_disk_geometry(void)
1883{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001884 ullong bytes = ((ullong)total_number_of_sectors << 9);
1885 long megabytes = bytes / 1000000;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001886
1887 if (megabytes < 10000)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001888 printf("\nDisk %s: %lu MB, %llu bytes\n",
1889 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001890 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001891 printf("\nDisk %s: %lu.%lu GB, %llu bytes\n",
1892 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
1893 printf("%u heads, %u sectors/track, %u cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001894 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001895 if (units_per_sector == 1)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001896 printf(", total %"SECT_FMT"u sectors",
1897 total_number_of_sectors / (sector_size/512));
1898 printf("\nUnits = %s of %u * %u = %u bytes\n\n",
1899 str_units(PLURAL),
1900 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001901}
1902
1903/*
1904 * Check whether partition entries are ordered by their starting positions.
1905 * Return 0 if OK. Return i if partition i should have been earlier.
1906 * Two separate checks: primary and logical partitions.
1907 */
1908static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001909wrong_p_order(int *prev)
1910{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001911 const struct pte *pe;
1912 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001913 sector_t last_p_start_pos = 0, p_start_pos;
1914 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001915
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001916 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001917 if (i == 4) {
1918 last_i = 4;
1919 last_p_start_pos = 0;
1920 }
1921 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001922 p = pe->part_table;
1923 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001924 p_start_pos = get_partition_start(pe);
1925
1926 if (last_p_start_pos > p_start_pos) {
1927 if (prev)
1928 *prev = last_i;
1929 return i;
1930 }
1931
1932 last_p_start_pos = p_start_pos;
1933 last_i = i;
1934 }
1935 }
1936 return 0;
1937}
1938
Denis Vlasenko834410a2006-11-29 12:00:28 +00001939#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001940/*
1941 * Fix the chain of logicals.
1942 * extended_offset is unchanged, the set of sectors used is unchanged
1943 * The chain is sorted so that sectors increase, and so that
1944 * starting sectors increase.
1945 *
1946 * After this it may still be that cfdisk doesnt like the table.
1947 * (This is because cfdisk considers expanded parts, from link to
1948 * end of partition, and these may still overlap.)
1949 * Now
1950 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1951 * may help.
1952 */
1953static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001954fix_chain_of_logicals(void)
1955{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001956 int j, oj, ojj, sj, sjj;
1957 struct partition *pj,*pjj,tmp;
1958
1959 /* Stage 1: sort sectors but leave sector of part 4 */
1960 /* (Its sector is the global extended_offset.) */
1961 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001962 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001963 oj = ptes[j].offset;
1964 ojj = ptes[j+1].offset;
1965 if (oj > ojj) {
1966 ptes[j].offset = ojj;
1967 ptes[j+1].offset = oj;
1968 pj = ptes[j].part_table;
1969 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1970 pjj = ptes[j+1].part_table;
1971 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1972 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001973 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001974 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001975 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001976 goto stage1;
1977 }
1978 }
1979
1980 /* Stage 2: sort starting sectors */
1981 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001982 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001983 pj = ptes[j].part_table;
1984 pjj = ptes[j+1].part_table;
1985 sj = get_start_sect(pj);
1986 sjj = get_start_sect(pjj);
1987 oj = ptes[j].offset;
1988 ojj = ptes[j+1].offset;
1989 if (oj+sj > ojj+sjj) {
1990 tmp = *pj;
1991 *pj = *pjj;
1992 *pjj = tmp;
1993 set_start_sect(pj, ojj+sjj-oj);
1994 set_start_sect(pjj, oj+sj-ojj);
1995 goto stage2;
1996 }
1997 }
1998
1999 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002000 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002001 ptes[j].changed = 1;
2002}
2003
2004
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002006fix_partition_table_order(void)
2007{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002008 struct pte *pei, *pek;
2009 int i,k;
2010
2011 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002012 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 return;
2014 }
2015
2016 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2017 /* partition i should have come earlier, move it */
2018 /* We have to move data in the MBR */
2019 struct partition *pi, *pk, *pe, pbuf;
2020 pei = &ptes[i];
2021 pek = &ptes[k];
2022
2023 pe = pei->ext_pointer;
2024 pei->ext_pointer = pek->ext_pointer;
2025 pek->ext_pointer = pe;
2026
2027 pi = pei->part_table;
2028 pk = pek->part_table;
2029
2030 memmove(&pbuf, pi, sizeof(struct partition));
2031 memmove(pi, pk, sizeof(struct partition));
2032 memmove(pk, &pbuf, sizeof(struct partition));
2033
2034 pei->changed = pek->changed = 1;
2035 }
2036
2037 if (i)
2038 fix_chain_of_logicals();
2039
2040 printf("Done.\n");
2041
2042}
2043#endif
2044
2045static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002046list_table(int xtra)
2047{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002048 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049 int i, w;
2050
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002051 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002052 sun_list_table(xtra);
2053 return;
2054 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002055 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002056 sgi_list_table(xtra);
2057 return;
2058 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002059
2060 list_disk_geometry();
2061
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002062 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002063 xbsd_print_disklabel(xtra);
2064 return;
2065 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002066
2067 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2068 but if the device name ends in a digit, say /dev/foo1,
2069 then the partition is called /dev/foo1p3. */
2070 w = strlen(disk_device);
2071 if (w && isdigit(disk_device[w-1]))
2072 w++;
2073 if (w < 5)
2074 w = 5;
2075
Denis Vlasenkobd852072007-03-19 14:43:38 +00002076 // 1 12345678901 12345678901 12345678901 12
2077 printf("%*s Boot Start End Blocks Id System\n",
2078 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002079
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002080 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002081 const struct pte *pe = &ptes[i];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002082 sector_t psects;
2083 sector_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002084 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002085
2086 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002087 if (!p || is_cleared_partition(p))
2088 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002089
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002090 psects = get_nr_sects(p);
2091 pblocks = psects;
2092 podd = 0;
2093
2094 if (sector_size < 1024) {
2095 pblocks /= (1024 / sector_size);
2096 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002097 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002098 if (sector_size > 1024)
2099 pblocks *= (sector_size / 1024);
2100
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002101 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 +00002102 partname(disk_device, i+1, w+2),
2103 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2104 ? '*' : '?',
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002105 cround(get_partition_start(pe)), /* start */
2106 cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002107 - (psects ? 1 : 0)),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002108 pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002109 p->sys_ind, /* type id */
2110 partition_type(p->sys_ind)); /* type name */
2111
2112 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002113 }
2114
2115 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002116 /* partition table entries are not checked for correct order
2117 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002118 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002119 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002120 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121 }
2122}
2123
Denis Vlasenko834410a2006-11-29 12:00:28 +00002124#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002126x_list_table(int extend)
2127{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002128 const struct pte *pe;
2129 const struct partition *p;
2130 int i;
2131
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002132 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002133 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002134 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002135 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136 pe = &ptes[i];
2137 p = (extend ? pe->ext_pointer : pe->part_table);
2138 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002139 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 +00002140 i + 1, p->boot_ind, p->head,
2141 sector(p->sector),
2142 cylinder(p->sector, p->cyl), p->end_head,
2143 sector(p->end_sector),
2144 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002145 get_start_sect(p), get_nr_sects(p),
2146 p->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002147 if (p->sys_ind)
2148 check_consistency(p, i);
2149 }
2150 }
2151}
2152#endif
2153
Denis Vlasenko834410a2006-11-29 12:00:28 +00002154#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002155static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002156fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002157{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002158 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002159 const struct pte *pe = &ptes[0];
2160 const struct partition *p;
2161
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002162 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002163 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002164 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002165 first[i] = 0xffffffff;
2166 last[i] = 0;
2167 } else {
2168 first[i] = get_partition_start(pe);
2169 last[i] = first[i] + get_nr_sects(p) - 1;
2170 }
2171 }
2172}
2173
2174static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002175check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002176{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002177 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002178
2179 real_s = sector(s) - 1;
2180 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002181 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002182 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002183 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002184 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002185 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002186 n, h + 1, g_heads);
2187 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002188 printf("Partition %u: sector %u greater than "
2189 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002190 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002191 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2192 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002193 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002194 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2195 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002196}
2197
2198static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002199verify(void)
2200{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002201 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002202 sector_t total = 1;
2203 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002204 struct partition *p;
2205
2206 if (warn_geometry())
2207 return;
2208
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002209 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002210 verify_sun();
2211 return;
2212 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002213 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002214 verify_sgi(1);
2215 return;
2216 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217
2218 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002219 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002220 struct pte *pe = &ptes[i];
2221
2222 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002223 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002224 check_consistency(p, i);
2225 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002226 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002227 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002228 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2229 last[i]);
2230 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002231 for (j = 0; j < i; j++) {
2232 if ((first[i] >= first[j] && first[i] <= last[j])
2233 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002234 printf("Warning: partition %u overlaps "
2235 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002236 total += first[i] >= first[j] ?
2237 first[i] : first[j];
2238 total -= last[i] <= last[j] ?
2239 last[i] : last[j];
2240 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002241 }
2242 }
2243 }
2244
2245 if (extended_offset) {
2246 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002247 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002248 get_nr_sects(pex->part_table) - 1;
2249
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002250 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002251 total++;
2252 p = ptes[i].part_table;
2253 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002254 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002255 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002256 "is empty\n", i + 1);
2257 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002258 printf("Logical partition %u not entirely in "
2259 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002260 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002261 }
2262 }
2263
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002264 if (total > g_heads * g_sectors * g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002265 printf("Total allocated sectors %u greater than the maximum "
2266 "%u\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002267 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002268 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002269 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002270 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002271 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272}
2273
2274static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002275add_partition(int n, int sys)
2276{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002277 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002278 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002279 struct partition *p = ptes[n].part_table;
2280 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002281 sector_t limit, temp;
2282 sector_t start, stop = 0;
2283 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284
2285 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002286 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 return;
2288 }
2289 fill_bounds(first, last);
2290 if (n < 4) {
2291 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002292 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002293 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002294 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002295 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002296 if (extended_offset) {
2297 first[ext_index] = extended_offset;
2298 last[ext_index] = get_start_sect(q) +
2299 get_nr_sects(q) - 1;
2300 }
2301 } else {
2302 start = extended_offset + sector_offset;
2303 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2304 }
2305 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002306 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002307 first[i] = (cround(first[i]) - 1) * units_per_sector;
2308
Denis Vlasenkobd852072007-03-19 14:43:38 +00002309 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002310 do {
2311 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002312 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002313 int lastplusoff;
2314
2315 if (start == ptes[i].offset)
2316 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002317 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002318 if (start >= first[i] && start <= lastplusoff)
2319 start = lastplusoff + 1;
2320 }
2321 if (start > limit)
2322 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002323 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002324 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002325 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002326 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002327 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002328 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002329 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002330
2331 saved_start = start;
2332 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2333 0, mesg);
2334 if (display_in_cyl_units) {
2335 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002336 if (start < saved_start)
2337 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002338 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002339 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002340 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002341 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002342 if (n > 4) { /* NOT for fifth partition */
2343 struct pte *pe = &ptes[n];
2344
2345 pe->offset = start - sector_offset;
2346 if (pe->offset == extended_offset) { /* must be corrected */
2347 pe->offset++;
2348 if (sector_offset == 1)
2349 start++;
2350 }
2351 }
2352
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002353 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354 struct pte *pe = &ptes[i];
2355
2356 if (start < pe->offset && limit >= pe->offset)
2357 limit = pe->offset - 1;
2358 if (start < first[i] && limit >= first[i])
2359 limit = first[i] - 1;
2360 }
2361 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002362 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002363 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002364 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 return;
2366 }
2367 if (cround(start) == cround(limit)) {
2368 stop = limit;
2369 } else {
2370 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002371 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 str_units(SINGULAR));
2373 stop = read_int(cround(start), cround(limit), cround(limit),
2374 cround(start), mesg);
2375 if (display_in_cyl_units) {
2376 stop = stop * units_per_sector - 1;
2377 if (stop >limit)
2378 stop = limit;
2379 }
2380 }
2381
2382 set_partition(n, 0, start, stop, sys);
2383 if (n > 4)
2384 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2385
Rob Landleyb73451d2006-02-24 16:29:00 +00002386 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 struct pte *pe4 = &ptes[4];
2388 struct pte *pen = &ptes[n];
2389
2390 ext_index = n;
2391 pen->ext_pointer = p;
2392 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002393 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2395 pe4->ext_pointer = pe4->part_table + 1;
2396 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002397 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002398 }
2399}
2400
2401static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002402add_logical(void)
2403{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002404 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2405 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002406
Rob Landley081e3842006-08-03 20:07:35 +00002407 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002408 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2409 pe->ext_pointer = pe->part_table + 1;
2410 pe->offset = 0;
2411 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002412 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002413 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002414 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002415}
2416
2417static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002418new_partition(void)
2419{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 int i, free_primary = 0;
2421
2422 if (warn_geometry())
2423 return;
2424
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002425 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002426 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002427 return;
2428 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002429 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002430 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002431 return;
2432 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002433 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002434 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2435"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2436"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002437 return;
2438 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002440 for (i = 0; i < 4; i++)
2441 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002442
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002443 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002444 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002445 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002446 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002447
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002448 if (!free_primary) {
2449 if (extended_offset)
2450 add_logical();
2451 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002452 printf("You must delete some partition and add "
2453 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002454 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002455 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002456 snprintf(line, sizeof(line),
2457 "Command action\n"
2458 " %s\n"
2459 " p primary partition (1-4)\n",
2460 (extended_offset ?
2461 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002463 c = read_nonempty(line);
2464 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465 i = get_nonexisting_partition(0, 4);
2466 if (i >= 0)
2467 add_partition(i, LINUX_NATIVE);
2468 return;
2469 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002470 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002471 add_logical();
2472 return;
2473 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002474 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 i = get_nonexisting_partition(0, 4);
2476 if (i >= 0)
2477 add_partition(i, EXTENDED);
2478 return;
2479 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002480 printf("Invalid partition number "
2481 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 }
2483 }
2484}
2485
2486static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002487write_table(void)
2488{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 int i;
2490
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002491 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002492 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002493 if (ptes[i].changed)
2494 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002495 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496 struct pte *pe = &ptes[i];
2497
2498 if (pe->changed) {
2499 write_part_table_flag(pe->sectorbuffer);
2500 write_sector(pe->offset, pe->sectorbuffer);
2501 }
2502 }
2503 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002504 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505 /* no test on change? the printf below might be mistaken */
2506 sgi_write_table();
2507 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002508 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 int needw = 0;
2510
Rob Landleyb73451d2006-02-24 16:29:00 +00002511 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 if (ptes[i].changed)
2513 needw = 1;
2514 if (needw)
2515 sun_write_table();
2516 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002517
Denis Vlasenkobd852072007-03-19 14:43:38 +00002518 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 reread_partition_table(1);
2520}
2521
Rob Landleyb73451d2006-02-24 16:29:00 +00002522static void
2523reread_partition_table(int leave)
2524{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 int i;
2526
Denis Vlasenkobd852072007-03-19 14:43:38 +00002527 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002528 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002529 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002530 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002531 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002532 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002533#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002535 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002536 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002538 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002539#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002540
2541 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002542 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002543 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002544 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002545 }
2546}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002547#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548
Denis Vlasenko834410a2006-11-29 12:00:28 +00002549#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002550#define MAX_PER_LINE 16
2551static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002552print_buffer(char *pbuffer)
2553{
2554 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555
2556 for (i = 0, l = 0; i < sector_size; i++, l++) {
2557 if (l == 0)
2558 printf("0x%03X:", i);
2559 printf(" %02X", (unsigned char) pbuffer[i]);
2560 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002561 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 l = -1;
2563 }
2564 }
2565 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002566 bb_putchar('\n');
2567 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002568}
2569
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002571print_raw(void)
2572{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573 int i;
2574
Denis Vlasenkobd852072007-03-19 14:43:38 +00002575 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002576 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002578 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002579 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002580 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002581 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582}
2583
2584static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002585move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002586{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 struct pte *pe = &ptes[i];
2588 struct partition *p = pe->part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002589 sector_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590
2591 if (warn_geometry())
2592 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002593 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002594 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 return;
2596 }
2597 first = get_partition_start(pe);
2598 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002599 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002600
2601 if (new != get_nr_sects(p)) {
2602 first = get_nr_sects(p) + get_start_sect(p) - new;
2603 set_nr_sects(p, first);
2604 set_start_sect(p, new);
2605 pe->changed = 1;
2606 }
2607}
2608
2609static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002610xselect(void)
2611{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002612 char c;
2613
Rob Landleyb73451d2006-02-24 16:29:00 +00002614 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002615 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002616 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 switch (c) {
2618 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002619 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002620 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002621 break;
2622 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002623 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002624 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 break;
2626 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002627 user_cylinders = g_cylinders =
2628 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002629 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002630 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002631 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002632 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 warn_cylinders();
2634 break;
2635 case 'd':
2636 print_raw();
2637 break;
2638 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002639 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002640 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002641 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002642 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002643 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 x_list_table(1);
2645 break;
2646 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002647 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002648 fix_partition_table_order();
2649 break;
2650 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002651#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002652 create_sgilabel();
2653#endif
2654 break;
2655 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002656 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002657 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002658 update_units();
2659 break;
2660 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002661 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002662 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 break;
2664 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002665 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002666 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002667 break;
2668 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002669 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002670 list_table(1);
2671 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002672 x_list_table(0);
2673 break;
2674 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002675 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002676 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002677 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002678 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002679 case 'r':
2680 return;
2681 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002682 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002683 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002684 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002685 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002686 printf("Warning: setting sector offset for DOS "
2687 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 }
2689 update_units();
2690 break;
2691 case 'v':
2692 verify();
2693 break;
2694 case 'w':
2695 write_table(); /* does not return */
2696 break;
2697 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002698 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002699 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002700 break;
2701 default:
2702 xmenu();
2703 }
2704 }
2705}
2706#endif /* ADVANCED mode */
2707
2708static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002709is_ide_cdrom_or_tape(const char *device)
2710{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002711 FILE *procf;
2712 char buf[100];
2713 struct stat statbuf;
2714 int is_ide = 0;
2715
2716 /* No device was given explicitly, and we are trying some
2717 likely things. But opening /dev/hdc may produce errors like
2718 "hdc: tray open or drive not ready"
2719 if it happens to be a CD-ROM drive. It even happens that
2720 the process hangs on the attempt to read a music CD.
2721 So try to be careful. This only works since 2.1.73. */
2722
2723 if (strncmp("/dev/hd", device, 7))
2724 return 0;
2725
2726 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002727 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002728 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2729 is_ide = (!strncmp(buf, "cdrom", 5) ||
2730 !strncmp(buf, "tape", 4));
2731 else
2732 /* Now when this proc file does not exist, skip the
2733 device when it is read-only. */
2734 if (stat(device, &statbuf) == 0)
2735 is_ide = ((statbuf.st_mode & 0222) == 0);
2736
2737 if (procf)
2738 fclose(procf);
2739 return is_ide;
2740}
2741
Rob Landley5527b912006-02-25 03:46:10 +00002742
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002744open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002745{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002746 int gb;
2747
2748 disk_device = device;
2749 if (setjmp(listingbuf))
2750 return;
2751 if (!user_specified)
2752 if (is_ide_cdrom_or_tape(device))
2753 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002754
2755 /* Open disk_device, save file descriptor to dev_fd */
2756 errno = 0;
2757 gb = get_boot(TRY_ONLY);
2758 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 /* Ignore other errors, since we try IDE
2760 and SCSI hard disks which may not be
2761 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002762 if (user_specified || errno == EACCES)
2763 bb_perror_msg("can't open '%s'", device);
2764 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002765 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002766
2767 if (gb < 0) { /* no DOS signature */
2768 list_disk_geometry();
2769 if (LABEL_IS_AIX)
2770 goto ret;
2771#if ENABLE_FEATURE_OSF_LABEL
2772 if (bsd_trydev(device) < 0)
2773#endif
2774 printf("Disk %s doesn't contain a valid "
2775 "partition table\n", device);
2776 } else {
2777 list_table(0);
2778#if ENABLE_FEATURE_FDISK_WRITABLE
2779 if (!LABEL_IS_SUN && g_partitions > 4) {
2780 delete_partition(ext_index);
2781 }
2782#endif
2783 }
2784 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002785 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786}
2787
2788/* for fdisk -l: try all things in /proc/partitions
2789 that look like a partition name (do not end in a digit) */
2790static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002791list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002792{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002793 FILE *procpt;
2794 char line[100], ptname[100], devname[120], *s;
2795 int ma, mi, sz;
2796
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002797 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002798
2799 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002800 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002801 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002803 for (s = ptname; *s; s++)
2804 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002805 if (isdigit(s[-1]))
2806 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002807 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002808 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002810#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002812#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002813}
2814
Denis Vlasenko834410a2006-11-29 12:00:28 +00002815#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002816static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002817unknown_command(int c)
2818{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002819 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002820}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002821#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002823int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002824int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00002825{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002826 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002827 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 * fdisk -v
2829 * fdisk -l [-b sectorsize] [-u] device ...
2830 * fdisk -s [partition] ...
2831 * fdisk [-b sectorsize] [-u] device
2832 *
2833 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002834 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002835 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002836
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002837 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002838
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002839 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002840 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002841 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002842 argv += optind;
2843 if (opt & OPT_b) { // -b
2844 /* Ugly: this sector size is really per device,
2845 so cannot be combined with multiple disks,
2846 and the same goes for the C/H/S options.
2847 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002848 if (sector_size != 512 && sector_size != 1024
2849 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002850 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002851 sector_offset = 2;
2852 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002854 if (user_heads <= 0 || user_heads >= 256)
2855 user_heads = 0;
2856 if (user_sectors <= 0 || user_sectors >= 64)
2857 user_sectors = 0;
2858 if (opt & OPT_u)
2859 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860
Denis Vlasenko834410a2006-11-29 12:00:28 +00002861#if ENABLE_FEATURE_FDISK_WRITABLE
2862 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002863 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002864#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002865 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002866 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002867 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002868 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002869 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002870 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002871 /* we don't have device names, */
2872 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002873 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 }
2875 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002876#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002877 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002878#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002879
Denis Vlasenko834410a2006-11-29 12:00:28 +00002880#if ENABLE_FEATURE_FDISK_BLKSIZE
2881 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002882 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883
2884 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002885 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002886 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002887 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002888 unsigned long long size;
2889 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002890 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002891 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002892 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002893 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002895 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896 }
2897 return 0;
2898 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002899#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002900
Denis Vlasenko834410a2006-11-29 12:00:28 +00002901#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01002902 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00002903 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002904
Denis Vlasenko834410a2006-11-29 12:00:28 +00002905 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002906 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002908 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002910 printf("Detected an OSF/1 disklabel on %s, entering "
2911 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002912 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002913 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002914 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 /* If we return we may want to make an empty DOS label? */
2916 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917
2918 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002919 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002920 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002921 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922 switch (c) {
2923 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002924 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002925 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002926 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002927 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002928 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002929 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002931 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 else
2933 unknown_command(c);
2934 break;
2935 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002936 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002937 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002938 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002939 if (read_maybe_empty("Please enter the name of the "
2940 "new boot file: ") == '\n')
2941 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942 else
2943 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002944 }
2945#if ENABLE_FEATURE_OSF_LABEL
2946 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002947 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002948#endif
2949 break;
2950 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002951 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002952 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002953 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002954 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002955 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002956 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002957 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002958 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959 else
2960 unknown_command(c);
2961 break;
2962 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002963 {
Eric Andersen040f4402003-07-30 08:40:37 +00002964 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002965 /* If sgi_label then don't use get_existing_partition,
2966 let the user select a partition, since
2967 get_existing_partition() only works for Linux-like
2968 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002969 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002970 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002971 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002972 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002973 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002974 if (j >= 0)
2975 delete_partition(j);
2976 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002977 break;
2978 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002979 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002980 create_sgiinfo();
2981 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 unknown_command(c);
2983 case 'l':
2984 list_types(get_sys_types());
2985 break;
2986 case 'm':
2987 menu();
2988 break;
2989 case 'n':
2990 new_partition();
2991 break;
2992 case 'o':
2993 create_doslabel();
2994 break;
2995 case 'p':
2996 list_table(0);
2997 break;
2998 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002999 if (ENABLE_FEATURE_CLEAN_UP)
3000 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003001 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003002 return 0;
3003 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003004#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003005 create_sunlabel();
3006#endif
3007 break;
3008 case 't':
3009 change_sysid();
3010 break;
3011 case 'u':
3012 change_units();
3013 break;
3014 case 'v':
3015 verify();
3016 break;
3017 case 'w':
3018 write_table(); /* does not return */
3019 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003020#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003021 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003022 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003023 printf("\n\tSorry, no experts menu for SGI "
3024 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003025 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003026 xselect();
3027 break;
3028#endif
3029 default:
3030 unknown_command(c);
3031 menu();
3032 }
3033 }
3034 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003035#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003036}