blob: 4416408314ca77d221c0661b09d7274a26515960 [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 */
12#define _LARGEFILE64_SOURCE
13#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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000020
Denis Vlasenko834410a2006-11-29 12:00:28 +000021/* Looks like someone forgot to add this to config system */
22#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
23# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000024# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +000025#endif
26
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000027#define DEFAULT_SECTOR_SIZE 512
28#define DEFAULT_SECTOR_SIZE_STR "512"
29#define MAX_SECTOR_SIZE 2048
30#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
31#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000032
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000033#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000034
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000035#define EXTENDED 0x05
36#define WIN98_EXTENDED 0x0f
37#define LINUX_PARTITION 0x81
38#define LINUX_SWAP 0x82
39#define LINUX_NATIVE 0x83
40#define LINUX_EXTENDED 0x85
41#define LINUX_LVM 0x8e
42#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000043
Denis Vlasenkoc033d512008-04-17 01:52:28 +000044
45enum {
46 OPT_b = 1 << 0,
47 OPT_C = 1 << 1,
48 OPT_H = 1 << 2,
49 OPT_l = 1 << 3,
50 OPT_S = 1 << 4,
51 OPT_u = 1 << 5,
52 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
53};
54
55
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000056/* Used for sector numbers. Today's disk sizes make it necessary */
57typedef unsigned long long ullong;
58
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000059struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000060 unsigned char heads;
61 unsigned char sectors;
62 unsigned short cylinders;
63 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000064};
65
Denis Vlasenko98ae2162006-10-12 19:30:44 +000066#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000067
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000068static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000069"Building a new %s. Changes will remain in memory only,\n"
70"until you decide to write them. After that the previous content\n"
71"won't be recoverable.\n\n";
72
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000073static const char msg_part_already_defined[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000074"Partition %d is already defined, delete it before re-adding\n";
75
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000076
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000077struct partition {
78 unsigned char boot_ind; /* 0x80 - active */
79 unsigned char head; /* starting head */
80 unsigned char sector; /* starting sector */
81 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +000082 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000083 unsigned char end_head; /* end head */
84 unsigned char end_sector; /* end sector */
85 unsigned char end_cyl; /* end cylinder */
86 unsigned char start4[4]; /* starting sector counting from 0 */
87 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000088} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000089
Denis Vlasenko9604e1b2009-03-03 18:47:56 +000090static const char unable_to_open[] ALIGN1 = "can't open '%s'";
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +000091static const char unable_to_read[] ALIGN1 = "can't read from %s";
92static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000093
Denis Vlasenko98ae2162006-10-12 19:30:44 +000094enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +000095 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +000096};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000097
Denis Vlasenko4437d192008-04-17 00:12:10 +000098#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000099
Denis Vlasenko834410a2006-11-29 12:00:28 +0000100#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000101#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000102#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000103#else
104#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000105#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000106#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000107
Denis Vlasenko834410a2006-11-29 12:00:28 +0000108#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000109#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000110#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000111#else
112#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000113#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000114#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000115
Denis Vlasenko834410a2006-11-29 12:00:28 +0000116#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000117#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000118#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000119#else
120#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000121#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000122#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000123
Denis Vlasenko834410a2006-11-29 12:00:28 +0000124#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000125#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000126#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000127#else
128#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000129#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000130#endif
Rob Landley5527b912006-02-25 03:46:10 +0000131
Denis Vlasenko4437d192008-04-17 00:12:10 +0000132enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000133
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000134static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000135#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000136static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000137static void reread_partition_table(int leave);
138static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000139static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000140static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000141static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000142#endif
143static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000144static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000145#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000146static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000147#else
148static int get_boot(void);
149#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000150
151#define PLURAL 0
152#define SINGULAR 1
153
Denis Vlasenko28703012006-12-19 20:32:02 +0000154static unsigned get_start_sect(const struct partition *p);
155static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000156
157/*
158 * per partition table entry data
159 *
160 * The four primary partitions have the same sectorbuffer (MBRbuffer)
161 * and have NULL ext_pointer.
162 * Each logical partition table entry has two pointers, one for the
163 * partition and one link to the next one.
164 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000165struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000166 struct partition *part_table; /* points into sectorbuffer */
167 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000168 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000169 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000170#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000171 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000172#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000173};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000174
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000175/* DOS partition types */
176
177static const char *const i386_sys_types[] = {
178 "\x00" "Empty",
179 "\x01" "FAT12",
180 "\x04" "FAT16 <32M",
181 "\x05" "Extended", /* DOS 3.3+ extended partition */
182 "\x06" "FAT16", /* DOS 16-bit >=32M */
183 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
184 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
185 "\x0b" "Win95 FAT32",
186 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
187 "\x0e" "Win95 FAT16 (LBA)",
188 "\x0f" "Win95 Ext'd (LBA)",
189 "\x11" "Hidden FAT12",
190 "\x12" "Compaq diagnostics",
191 "\x14" "Hidden FAT16 <32M",
192 "\x16" "Hidden FAT16",
193 "\x17" "Hidden HPFS/NTFS",
194 "\x1b" "Hidden Win95 FAT32",
195 "\x1c" "Hidden W95 FAT32 (LBA)",
196 "\x1e" "Hidden W95 FAT16 (LBA)",
197 "\x3c" "Part.Magic recovery",
198 "\x41" "PPC PReP Boot",
199 "\x42" "SFS",
200 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
201 "\x80" "Old Minix", /* Minix 1.4a and earlier */
202 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
203 "\x82" "Linux swap", /* also Solaris */
204 "\x83" "Linux",
205 "\x84" "OS/2 hidden C: drive",
206 "\x85" "Linux extended",
207 "\x86" "NTFS volume set",
208 "\x87" "NTFS volume set",
209 "\x8e" "Linux LVM",
210 "\x9f" "BSD/OS", /* BSDI */
211 "\xa0" "Thinkpad hibernation",
212 "\xa5" "FreeBSD", /* various BSD flavours */
213 "\xa6" "OpenBSD",
214 "\xa8" "Darwin UFS",
215 "\xa9" "NetBSD",
216 "\xab" "Darwin boot",
217 "\xb7" "BSDI fs",
218 "\xb8" "BSDI swap",
219 "\xbe" "Solaris boot",
220 "\xeb" "BeOS fs",
221 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
222 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
223 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
224 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
225 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
226 autodetect using persistent
227 superblock */
228#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
229 "\x02" "XENIX root",
230 "\x03" "XENIX usr",
231 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
232 "\x09" "AIX bootable", /* AIX data or Coherent */
233 "\x10" "OPUS",
234 "\x18" "AST SmartSleep",
235 "\x24" "NEC DOS",
236 "\x39" "Plan 9",
237 "\x40" "Venix 80286",
238 "\x4d" "QNX4.x",
239 "\x4e" "QNX4.x 2nd part",
240 "\x4f" "QNX4.x 3rd part",
241 "\x50" "OnTrack DM",
242 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
243 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
244 "\x53" "OnTrack DM6 Aux3",
245 "\x54" "OnTrackDM6",
246 "\x55" "EZ-Drive",
247 "\x56" "Golden Bow",
248 "\x5c" "Priam Edisk",
249 "\x61" "SpeedStor",
250 "\x64" "Novell Netware 286",
251 "\x65" "Novell Netware 386",
252 "\x70" "DiskSecure Multi-Boot",
253 "\x75" "PC/IX",
254 "\x93" "Amoeba",
255 "\x94" "Amoeba BBT", /* (bad block table) */
256 "\xa7" "NeXTSTEP",
257 "\xbb" "Boot Wizard hidden",
258 "\xc1" "DRDOS/sec (FAT-12)",
259 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
260 "\xc6" "DRDOS/sec (FAT-16)",
261 "\xc7" "Syrinx",
262 "\xda" "Non-FS data",
263 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
264 Concurrent DOS or CTOS */
265 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
266 "\xdf" "BootIt", /* BootIt EMBRM */
267 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
268 extended partition */
269 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
270 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
271 partition < 1024 cyl. */
272 "\xf1" "SpeedStor",
273 "\xf4" "SpeedStor", /* SpeedStor large partition */
274 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
275 "\xff" "BBT", /* Xenix Bad Block Table */
276#endif
277 NULL
278};
279
Denis Vlasenko4437d192008-04-17 00:12:10 +0000280enum {
281 dev_fd = 3 /* the disk */
282};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000283
284/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000285struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000286 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000287
288 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000289 int g_partitions; // = 4; /* maximum partition + 1 */
290 unsigned units_per_sector; // = 1;
291 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
292 unsigned user_set_sector_size;
293 unsigned sector_offset; // = 1;
294 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000295 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000296 smallint display_in_cyl_units; // = 1;
297#if ENABLE_FEATURE_OSF_LABEL
298 smallint possibly_osf_label;
299#endif
300
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000301 smallint listing; /* no aborts for fdisk -l */
302 smallint dos_compatible_flag; // = 1;
303#if ENABLE_FEATURE_FDISK_WRITABLE
304 //int dos_changed;
305 smallint nowarn; /* no warnings for fdisk -l/-s */
306#endif
307 int ext_index; /* the prime extended partition */
308 unsigned user_cylinders, user_heads, user_sectors;
309 unsigned pt_heads, pt_sectors;
310 unsigned kern_heads, kern_sectors;
311 ullong extended_offset; /* offset of link pointers */
312 ullong total_number_of_sectors;
313
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000314 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000315 char line_buffer[80];
316 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000317 /* Raw disk label. For DOS-type partition tables the MBR,
318 * with descriptions of the primary partitions. */
319 char MBRbuffer[MAX_SECTOR_SIZE];
320 /* Partition tables */
321 struct pte ptes[MAXIMUM_PARTS];
322};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000323#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000324#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000325#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000326#define g_partitions (G.g_partitions )
327#define units_per_sector (G.units_per_sector )
328#define sector_size (G.sector_size )
329#define user_set_sector_size (G.user_set_sector_size)
330#define sector_offset (G.sector_offset )
331#define g_heads (G.g_heads )
332#define g_sectors (G.g_sectors )
333#define g_cylinders (G.g_cylinders )
334#define current_label_type (G.current_label_type )
335#define display_in_cyl_units (G.display_in_cyl_units)
336#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000337#define listing (G.listing )
338#define dos_compatible_flag (G.dos_compatible_flag )
339#define nowarn (G.nowarn )
340#define ext_index (G.ext_index )
341#define user_cylinders (G.user_cylinders )
342#define user_heads (G.user_heads )
343#define user_sectors (G.user_sectors )
344#define pt_heads (G.pt_heads )
345#define pt_sectors (G.pt_sectors )
346#define kern_heads (G.kern_heads )
347#define kern_sectors (G.kern_sectors )
348#define extended_offset (G.extended_offset )
349#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000350#define listingbuf (G.listingbuf )
351#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000352#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000353#define MBRbuffer (G.MBRbuffer )
354#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000355#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000356 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000357 sector_size = DEFAULT_SECTOR_SIZE; \
358 sector_offset = 1; \
359 g_partitions = 4; \
360 display_in_cyl_units = 1; \
361 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000362 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000363} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000364
Denis Vlasenkobd852072007-03-19 14:43:38 +0000365
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000366/* TODO: move to libbb? */
Denis Vlasenko4437d192008-04-17 00:12:10 +0000367static ullong bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000368{
369 uint64_t v64;
370 unsigned long longsectors;
371
372 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000373 /* Got bytes, convert to 512 byte sectors */
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000374 return (v64 >> 9);
375 }
376 /* Needs temp of type long */
377 if (ioctl(fd, BLKGETSIZE, &longsectors))
378 longsectors = 0;
379 return longsectors;
380}
381
382
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000383#define IS_EXTENDED(i) \
384 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
385
386#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
387
388#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
389
390#define pt_offset(b, n) \
391 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
392
393#define sector(s) ((s) & 0x3f)
394
395#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
396
397#define hsc2sector(h,s,c) \
398 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
399
400#define set_hsc(h,s,c,sector) \
401 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000402 s = sector % g_sectors + 1; \
403 sector /= g_sectors; \
404 h = sector % g_heads; \
405 sector /= g_heads; \
406 c = sector & 0xff; \
407 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000408 } while (0)
409
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000410static void
411close_dev_fd(void)
412{
413 /* Not really closing, but making sure it is open, and to harmless place */
414 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
415}
416
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000417#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000418/* Read line; return 0 or first printable char */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000419static int
420read_line(const char *prompt)
421{
422 int sz;
423
424 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
425 if (sz <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000426 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000427
428 if (line_buffer[sz-1] == '\n')
429 line_buffer[--sz] = '\0';
430
431 line_ptr = line_buffer;
432 while (*line_ptr && !isgraph(*line_ptr))
433 line_ptr++;
434 return *line_ptr;
435}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000436#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000437
Denis Vlasenkobd852072007-03-19 14:43:38 +0000438/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000439 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000440 */
441static const char *
442partname(const char *dev, int pno, int lth)
443{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000444 const char *p;
445 int w, wp;
446 int bufsiz;
447 char *bufp;
448
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000449 bufp = partname_buffer;
450 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000451
452 w = strlen(dev);
453 p = "";
454
455 if (isdigit(dev[w-1]))
456 p = "p";
457
458 /* devfs kludge - note: fdisk partition names are not supposed
459 to equal kernel names, so there is no reason to do this */
460 if (strcmp(dev + w - 4, "disc") == 0) {
461 w -= 4;
462 p = "part";
463 }
464
465 wp = strlen(p);
466
467 if (lth) {
468 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
469 lth-wp-2, w, dev, p, pno);
470 } else {
471 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
472 }
473 return bufp;
474}
475
Denis Vlasenko834410a2006-11-29 12:00:28 +0000476#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000477static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000478set_all_unchanged(void)
479{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000480 int i;
481
482 for (i = 0; i < MAXIMUM_PARTS; i++)
483 ptes[i].changed = 0;
484}
485
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000486static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000487set_changed(int i)
488{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000489 ptes[i].changed = 1;
490}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000491#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000492
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000493static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000494get_part_table(int i)
495{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000496 return ptes[i].part_table;
497}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000498
499static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000500str_units(int n)
501{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000502 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000503 return display_in_cyl_units ? "cylinder" : "sector";
504 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000505}
506
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000507static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000508valid_part_table_flag(const char *mbuffer)
509{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000510 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000511}
512
Denis Vlasenko834410a2006-11-29 12:00:28 +0000513#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000514static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000515write_part_table_flag(char *b)
516{
517 b[510] = 0x55;
518 b[511] = 0xaa;
519}
520
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000521static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000522read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000523{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000524 while (!read_line(mesg))
525 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000526 return *line_ptr;
527}
528
529static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000530read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000531{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000532 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000533 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000534 line_ptr[0] = '\n';
535 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000536 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000537 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000538}
539
540static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000541read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000542{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000543 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000544 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000545 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000546 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000547 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000548 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000549 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000550 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000551 if (v > 0xff)
552 /* Bad input also triggers this */
553 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000554 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000555 }
556}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000557#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558
Denis Vlasenko9764d692008-07-09 21:20:50 +0000559static void fdisk_fatal(const char *why)
560{
561 if (listing) {
562 close_dev_fd();
563 longjmp(listingbuf, 1);
564 }
565 bb_error_msg_and_die(why, disk_device);
566}
567
568static void
569seek_sector(ullong secno)
570{
571 secno *= sector_size;
572#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
573 if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
574 fdisk_fatal(unable_to_seek);
575#else
576 if (secno > MAXINT(off_t)
577 || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
578 ) {
579 fdisk_fatal(unable_to_seek);
580 }
581#endif
582}
583
584#if ENABLE_FEATURE_FDISK_WRITABLE
585static void
586write_sector(ullong secno, const void *buf)
587{
588 seek_sector(secno);
589 xwrite(dev_fd, buf, sector_size);
590}
591#endif
592
593
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000594#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595
596typedef struct {
597 unsigned char info[128]; /* Informative text string */
598 unsigned char spare0[14];
599 struct sun_info {
600 unsigned char spare1;
601 unsigned char id;
602 unsigned char spare2;
603 unsigned char flags;
604 } infos[8];
605 unsigned char spare1[246]; /* Boot information etc. */
606 unsigned short rspeed; /* Disk rotational speed */
607 unsigned short pcylcount; /* Physical cylinder count */
608 unsigned short sparecyl; /* extra sects per cylinder */
609 unsigned char spare2[4]; /* More magic... */
610 unsigned short ilfact; /* Interleave factor */
611 unsigned short ncyl; /* Data cylinder count */
612 unsigned short nacyl; /* Alt. cylinder count */
613 unsigned short ntrks; /* Tracks per cylinder */
614 unsigned short nsect; /* Sectors per track */
615 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000616 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000617 uint32_t start_cylinder;
618 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000619 } partitions[8];
620 unsigned short magic; /* Magic number */
621 unsigned short csum; /* Label xor'd checksum */
622} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000623#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000624STATIC_OSF void bsd_select(void);
625STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000626#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627
Denis Vlasenko28703012006-12-19 20:32:02 +0000628#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000629static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000630fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000631{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000632 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000633}
634
Rob Landley88621d72006-08-29 19:41:06 +0000635static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000636fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000637{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000638 return (x << 24) |
639 ((x & 0xFF00) << 8) |
640 ((x & 0xFF0000) >> 8) |
641 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000642}
643#endif
644
Denis Vlasenkobd852072007-03-19 14:43:38 +0000645STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000646STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000647STATIC_SGI int sgi_get_sysid(int i);
648STATIC_SGI void sgi_delete_partition(int i);
649STATIC_SGI void sgi_change_sysid(int i, int sys);
650STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000651#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000652STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000653#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000654STATIC_SGI int verify_sgi(int verbose);
655STATIC_SGI void sgi_add_partition(int n, int sys);
656STATIC_SGI void sgi_set_swappartition(int i);
657STATIC_SGI const char *sgi_get_bootfile(void);
658STATIC_SGI void sgi_set_bootfile(const char* aFile);
659STATIC_SGI void create_sgiinfo(void);
660STATIC_SGI void sgi_write_table(void);
661STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000662#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000663
Denis Vlasenkobd852072007-03-19 14:43:38 +0000664STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000665STATIC_SUN void sun_delete_partition(int i);
666STATIC_SUN void sun_change_sysid(int i, int sys);
667STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000668STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000669#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000670STATIC_SUN void sun_set_alt_cyl(void);
671STATIC_SUN void sun_set_ncyl(int cyl);
672STATIC_SUN void sun_set_xcyl(void);
673STATIC_SUN void sun_set_ilfact(void);
674STATIC_SUN void sun_set_rspeed(void);
675STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000676#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000677STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
678STATIC_SUN void verify_sun(void);
679STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000680#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681
Denis Vlasenko9764d692008-07-09 21:20:50 +0000682
Denis Vlasenko834410a2006-11-29 12:00:28 +0000683#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000684/* start_sect and nr_sects are stored little endian on all machines */
685/* moreover, they are not aligned correctly */
686static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000687store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000688{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000689 cp[0] = val;
690 cp[1] = val >> 8;
691 cp[2] = val >> 16;
692 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000693}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000694#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695
Denis Vlasenko834410a2006-11-29 12:00:28 +0000696static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000697read4_little_endian(const unsigned char *cp)
698{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000699 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000700}
701
Denis Vlasenko834410a2006-11-29 12:00:28 +0000702#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000703static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000704set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000705{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000706 store4_little_endian(p->start4, start_sect);
707}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000708#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000709
Denis Vlasenko28703012006-12-19 20:32:02 +0000710static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000711get_start_sect(const struct partition *p)
712{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713 return read4_little_endian(p->start4);
714}
715
Denis Vlasenko834410a2006-11-29 12:00:28 +0000716#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000717static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000718set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000719{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000720 store4_little_endian(p->size4, nr_sects);
721}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000722#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000723
Denis Vlasenko28703012006-12-19 20:32:02 +0000724static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000725get_nr_sects(const struct partition *p)
726{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000727 return read4_little_endian(p->size4);
728}
729
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000730/* Allocate a buffer and read a partition table sector */
731static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000732read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000733{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000734 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000735 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000736 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000737 /* xread would make us abort - bad for fdisk -l */
738 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000739 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000740#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000741 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000742#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000743 pe->part_table = pe->ext_pointer = NULL;
744}
745
Denis Vlasenko834410a2006-11-29 12:00:28 +0000746static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000747get_partition_start(const struct pte *pe)
748{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000749 return pe->offset + get_start_sect(pe->part_table);
750}
751
Denis Vlasenko834410a2006-11-29 12:00:28 +0000752#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000753/*
754 * Avoid warning about DOS partitions when no DOS partition was changed.
755 * Here a heuristic "is probably dos partition".
756 * We might also do the opposite and warn in all cases except
757 * for "is probably nondos partition".
758 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000759#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000760static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000761is_dos_partition(int t)
762{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000763 return (t == 1 || t == 4 || t == 6 ||
764 t == 0x0b || t == 0x0c || t == 0x0e ||
765 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
766 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
767 t == 0xc1 || t == 0xc4 || t == 0xc6);
768}
Denis Vlasenko89398812008-01-25 20:18:46 +0000769#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000770
771static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000772menu(void)
773{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000774 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000775 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000776 puts("a\ttoggle a read only flag"); /* sun */
777 puts("b\tedit bsd disklabel");
778 puts("c\ttoggle the mountable flag"); /* sun */
779 puts("d\tdelete a partition");
780 puts("l\tlist known partition types");
781 puts("n\tadd a new partition");
782 puts("o\tcreate a new empty DOS partition table");
783 puts("p\tprint the partition table");
784 puts("q\tquit without saving changes");
785 puts("s\tcreate a new empty Sun disklabel"); /* sun */
786 puts("t\tchange a partition's system id");
787 puts("u\tchange display/entry units");
788 puts("v\tverify the partition table");
789 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000790#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000791 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000792#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000793 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000794 puts("a\tselect bootable partition"); /* sgi flavour */
795 puts("b\tedit bootfile entry"); /* sgi */
796 puts("c\tselect sgi swap partition"); /* sgi flavour */
797 puts("d\tdelete a partition");
798 puts("l\tlist known partition types");
799 puts("n\tadd a new partition");
800 puts("o\tcreate a new empty DOS partition table");
801 puts("p\tprint the partition table");
802 puts("q\tquit without saving changes");
803 puts("s\tcreate a new empty Sun disklabel"); /* sun */
804 puts("t\tchange a partition's system id");
805 puts("u\tchange display/entry units");
806 puts("v\tverify the partition table");
807 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000808 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000809 puts("o\tcreate a new empty DOS partition table");
810 puts("q\tquit without saving changes");
811 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000812 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000813 puts("a\ttoggle a bootable flag");
814 puts("b\tedit bsd disklabel");
815 puts("c\ttoggle the dos compatibility flag");
816 puts("d\tdelete a partition");
817 puts("l\tlist known partition types");
818 puts("n\tadd a new partition");
819 puts("o\tcreate a new empty DOS partition table");
820 puts("p\tprint the partition table");
821 puts("q\tquit without saving changes");
822 puts("s\tcreate a new empty Sun disklabel"); /* sun */
823 puts("t\tchange a partition's system id");
824 puts("u\tchange display/entry units");
825 puts("v\tverify the partition table");
826 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000827#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000828 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000829#endif
830 }
831}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000832#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000833
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000834
Denis Vlasenko834410a2006-11-29 12:00:28 +0000835#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000836static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000837xmenu(void)
838{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000839 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000840 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000841 puts("a\tchange number of alternate cylinders"); /*sun*/
842 puts("c\tchange number of cylinders");
843 puts("d\tprint the raw data in the partition table");
844 puts("e\tchange number of extra sectors per cylinder");/*sun*/
845 puts("h\tchange number of heads");
846 puts("i\tchange interleave factor"); /*sun*/
847 puts("o\tchange rotation speed (rpm)"); /*sun*/
848 puts("p\tprint the partition table");
849 puts("q\tquit without saving changes");
850 puts("r\treturn to main menu");
851 puts("s\tchange number of sectors/track");
852 puts("v\tverify the partition table");
853 puts("w\twrite table to disk and exit");
854 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000855 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000856 puts("b\tmove beginning of data in a partition"); /* !sun */
857 puts("c\tchange number of cylinders");
858 puts("d\tprint the raw data in the partition table");
859 puts("e\tlist extended partitions"); /* !sun */
860 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
861 puts("h\tchange number of heads");
862 puts("p\tprint the partition table");
863 puts("q\tquit without saving changes");
864 puts("r\treturn to main menu");
865 puts("s\tchange number of sectors/track");
866 puts("v\tverify the partition table");
867 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000868 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000869 puts("b\tmove beginning of data in a partition"); /* !sun */
870 puts("c\tchange number of cylinders");
871 puts("d\tprint the raw data in the partition table");
872 puts("e\tlist extended partitions"); /* !sun */
873 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
874 puts("h\tchange number of heads");
875 puts("p\tprint the partition table");
876 puts("q\tquit without saving changes");
877 puts("r\treturn to main menu");
878 puts("s\tchange number of sectors/track");
879 puts("v\tverify the partition table");
880 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000881 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000882 puts("b\tmove beginning of data in a partition"); /* !sun */
883 puts("c\tchange number of cylinders");
884 puts("d\tprint the raw data in the partition table");
885 puts("e\tlist extended partitions"); /* !sun */
886 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000887#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000888 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000889#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000890 puts("h\tchange number of heads");
891 puts("p\tprint the partition table");
892 puts("q\tquit without saving changes");
893 puts("r\treturn to main menu");
894 puts("s\tchange number of sectors/track");
895 puts("v\tverify the partition table");
896 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000897 }
898}
899#endif /* ADVANCED mode */
900
Denis Vlasenko834410a2006-11-29 12:00:28 +0000901#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000902static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000903get_sys_types(void)
904{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000906 LABEL_IS_SUN ? sun_sys_types :
907 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000908 i386_sys_types);
909}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000910#else
911#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000912#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000913
Denis Vlasenkobd852072007-03-19 14:43:38 +0000914static const char *
915partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000916{
917 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000918 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000919
Denis Vlasenkobd852072007-03-19 14:43:38 +0000920 for (i = 0; types[i]; i++)
921 if ((unsigned char)types[i][0] == type)
922 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000923
Denis Vlasenkobd852072007-03-19 14:43:38 +0000924 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000925}
926
927
Denis Vlasenko834410a2006-11-29 12:00:28 +0000928#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000929static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000930get_sysid(int i)
931{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000932 return LABEL_IS_SUN ? sunlabel->infos[i].id :
933 (LABEL_IS_SGI ? sgi_get_sysid(i) :
934 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935}
936
Denis Vlasenkobd852072007-03-19 14:43:38 +0000937static void
938list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000939{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000940 enum { COLS = 3 };
941
942 unsigned last[COLS];
943 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000944 int i;
945
Denis Vlasenko9764d692008-07-09 21:20:50 +0000946 for (size = 0; sys[size]; size++)
947 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000948
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949 done = 0;
950 for (i = COLS-1; i >= 0; i--) {
951 done += (size + i - done) / (i + 1);
952 last[COLS-1 - i] = done;
953 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000954
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000955 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000957 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000958 (unsigned char)sys[next][0],
959 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000960 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000961 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000962 i = 0;
963 next = ++done;
964 }
965 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000966 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000967}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000968#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000969
970static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000971is_cleared_partition(const struct partition *p)
972{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
974 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
975 get_start_sect(p) || get_nr_sects(p));
976}
977
978static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000979clear_partition(struct partition *p)
980{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000981 if (!p)
982 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000983 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000984}
985
Denis Vlasenko834410a2006-11-29 12:00:28 +0000986#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000987static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000988set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000989{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000991 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000992
993 if (doext) {
994 p = ptes[i].ext_pointer;
995 offset = extended_offset;
996 } else {
997 p = ptes[i].part_table;
998 offset = ptes[i].offset;
999 }
1000 p->boot_ind = 0;
1001 p->sys_ind = sysid;
1002 set_start_sect(p, start - offset);
1003 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001004 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1005 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001007 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1008 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001009 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1010 ptes[i].changed = 1;
1011}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001012#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013
1014static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001015warn_geometry(void)
1016{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001017 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001019
Denis Vlasenkobd852072007-03-19 14:43:38 +00001020 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001021 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001022 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001023 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001024 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001025 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001026 printf(" cylinders");
1027 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001028#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001029 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001030#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001031 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032 return 1;
1033}
1034
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001035static void
1036update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001037{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001038 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001039
1040 if (display_in_cyl_units && cyl_units)
1041 units_per_sector = cyl_units;
1042 else
1043 units_per_sector = 1; /* in sectors */
1044}
1045
Denis Vlasenko834410a2006-11-29 12:00:28 +00001046#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001048warn_cylinders(void)
1049{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001050 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001051 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001052"The number of cylinders for this disk is set to %d.\n"
1053"There is nothing wrong with that, but this is larger than 1024,\n"
1054"and could in certain setups cause problems with:\n"
1055"1) software that runs at boot time (e.g., old versions of LILO)\n"
1056"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001058 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001059}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001060#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001061
1062static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001063read_extended(int ext)
1064{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001065 int i;
1066 struct pte *pex;
1067 struct partition *p, *q;
1068
1069 ext_index = ext;
1070 pex = &ptes[ext];
1071 pex->ext_pointer = pex->part_table;
1072
1073 p = pex->part_table;
1074 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001075 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001076 return;
1077 }
1078
Rob Landleyb73451d2006-02-24 16:29:00 +00001079 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001080 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001081
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001082 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001083 /* This is not a Linux restriction, but
1084 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001085 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001086 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001087#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001088 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001089 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001091#endif
1092 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001093 return;
1094 }
1095
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001096 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001097
1098 if (!extended_offset)
1099 extended_offset = get_start_sect(p);
1100
1101 q = p = pt_offset(pe->sectorbuffer, 0);
1102 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001103 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001104 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001105 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001106 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001107 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001108 else
1109 pe->ext_pointer = p;
1110 } else if (p->sys_ind) {
1111 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001112 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001114 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115 else
1116 pe->part_table = p;
1117 }
1118 }
1119
1120 /* very strange code here... */
1121 if (!pe->part_table) {
1122 if (q != pe->ext_pointer)
1123 pe->part_table = q;
1124 else
1125 pe->part_table = q + 1;
1126 }
1127 if (!pe->ext_pointer) {
1128 if (q != pe->part_table)
1129 pe->ext_pointer = q;
1130 else
1131 pe->ext_pointer = q + 1;
1132 }
1133
1134 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001135 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001136 }
1137
Denis Vlasenko834410a2006-11-29 12:00:28 +00001138#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001139 /* remove empty links */
1140 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001141 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001142 struct pte *pe = &ptes[i];
1143
Denis Vlasenkobd852072007-03-19 14:43:38 +00001144 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001145 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001146 ) {
1147 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 delete_partition(i);
1149 goto remove; /* numbering changed */
1150 }
1151 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001152#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001153}
1154
Denis Vlasenko834410a2006-11-29 12:00:28 +00001155#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001156static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001157create_doslabel(void)
1158{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001159 int i;
1160
Denis Vlasenkobd852072007-03-19 14:43:38 +00001161 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001162
Denis Vlasenko4437d192008-04-17 00:12:10 +00001163 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001164
Denis Vlasenko834410a2006-11-29 12:00:28 +00001165#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001166 possibly_osf_label = 0;
1167#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001168 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001169
1170 for (i = 510-64; i < 510; i++)
1171 MBRbuffer[i] = 0;
1172 write_part_table_flag(MBRbuffer);
1173 extended_offset = 0;
1174 set_all_unchanged();
1175 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001176 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001177}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001178#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001179
1180static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001181get_sectorsize(void)
1182{
Rob Landley736e5252006-02-25 03:36:00 +00001183 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001185 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001186 sector_size = arg;
1187 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001188 printf("Note: sector size is %d "
1189 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1190 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 }
1192}
1193
Rob Landley88621d72006-08-29 19:41:06 +00001194static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001195get_kernel_geometry(void)
1196{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001197 struct hd_geometry geometry;
1198
Denis Vlasenko4437d192008-04-17 00:12:10 +00001199 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001200 kern_heads = geometry.heads;
1201 kern_sectors = geometry.sectors;
1202 /* never use geometry.cylinders - it is truncated */
1203 }
1204}
1205
1206static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001207get_partition_table_geometry(void)
1208{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001209 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210 struct partition *p;
1211 int i, h, s, hh, ss;
1212 int first = 1;
1213 int bad = 0;
1214
Eric Andersen3496fdc2006-01-30 23:09:20 +00001215 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001216 return;
1217
1218 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001219 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001220 p = pt_offset(bufp, i);
1221 if (p->sys_ind != 0) {
1222 h = p->end_head + 1;
1223 s = (p->end_sector & 077);
1224 if (first) {
1225 hh = h;
1226 ss = s;
1227 first = 0;
1228 } else if (hh != h || ss != s)
1229 bad = 1;
1230 }
1231 }
1232
1233 if (!first && !bad) {
1234 pt_heads = hh;
1235 pt_sectors = ss;
1236 }
1237}
1238
Rob Landleyb73451d2006-02-24 16:29:00 +00001239static void
1240get_geometry(void)
1241{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243
1244 get_sectorsize();
1245 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001246#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001247 guess_device_type();
1248#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001249 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250 kern_heads = kern_sectors = 0;
1251 pt_heads = pt_sectors = 0;
1252
1253 get_kernel_geometry();
1254 get_partition_table_geometry();
1255
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001256 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 pt_heads ? pt_heads :
1258 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001259 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001260 pt_sectors ? pt_sectors :
1261 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001262 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001263
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001264 sector_offset = 1;
1265 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001266 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001267
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001268 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1269 if (!g_cylinders)
1270 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001271}
1272
1273/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001274 * Opens disk_device and optionally reads MBR.
1275 * FIXME: document what each 'what' value will do!
1276 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001277 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1278 * 0: found or created label
1279 * 1: I/O error
1280 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001281#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1282static int get_boot(enum action what)
1283#else
1284static int get_boot(void)
1285#define get_boot(what) get_boot()
1286#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001287{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001288 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001290 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001291 for (i = 0; i < 4; i++) {
1292 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 pe->part_table = pt_offset(MBRbuffer, i);
1294 pe->ext_pointer = NULL;
1295 pe->offset = 0;
1296 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001297#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001298 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001299#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 }
1301
Denis Vlasenko834410a2006-11-29 12:00:28 +00001302#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001303// ALERT! highly idiotic design!
1304// We end up here when we call get_boot() recursively
1305// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1306// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1307// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1308// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001309 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001310 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001311
Denis Vlasenko4437d192008-04-17 00:12:10 +00001312 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1313
Denis Vlasenkobd852072007-03-19 14:43:38 +00001314 if (fd < 0) {
1315 fd = open(disk_device, O_RDONLY);
1316 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001317 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001318 return 1;
1319 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001320 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001321 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001323 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001324 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001325 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001326 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001328 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329 fdisk_fatal(unable_to_read);
1330 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001331#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001332 fd = open(disk_device, O_RDONLY);
1333 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001334 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001335 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001336 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001337 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001338 }
1339 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001340#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001342 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 update_units();
1344
Denis Vlasenko834410a2006-11-29 12:00:28 +00001345#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 if (check_sun_label())
1347 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001348#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001349#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001350 if (check_sgi_label())
1351 return 0;
1352#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001353#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001354 if (check_aix_label())
1355 return 0;
1356#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001357#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001358 if (check_osf_label()) {
1359 possibly_osf_label = 1;
1360 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001361 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 return 0;
1363 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001364 printf("This disk has both DOS and BSD magic.\n"
1365 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001366 }
1367#endif
1368
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001369#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001370 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001371 return -1;
1372#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001373 if (!valid_part_table_flag(MBRbuffer)) {
1374 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001375 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001376 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001377 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001379 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380#else
1381 create_doslabel();
1382#endif
1383 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001384 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001385 /* TRY_ONLY: */
1386 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001388 created_table:
1389#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390
Denis Vlasenko4437d192008-04-17 00:12:10 +00001391
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001392 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001393 warn_geometry();
1394
1395 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001396 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001397 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001398 printf("Ignoring extra extended "
1399 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400 else
1401 read_extended(i);
1402 }
1403 }
1404
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001405 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001408 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1409 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001410 pe->sectorbuffer[510],
1411 pe->sectorbuffer[511],
1412 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001413 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414 }
1415 }
1416
1417 return 0;
1418}
1419
Denis Vlasenko834410a2006-11-29 12:00:28 +00001420#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001421/*
1422 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1423 * If the user hits Enter, DFLT is returned.
1424 * Answers like +10 are interpreted as offsets from BASE.
1425 *
1426 * There is no default if DFLT is not between LOW and HIGH.
1427 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001428static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001429read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001431 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001433 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001434
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001435 if (dflt < low || dflt > high) {
1436 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001437 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001438 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001439
1440 while (1) {
1441 int use_default = default_ok;
1442
1443 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001444 do {
1445 printf(fmt, mesg, low, high, dflt);
1446 read_maybe_empty("");
1447 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1448 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001449
Eric Andersen84bdea82004-05-19 10:49:17 +00001450 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001451 int minus = (*line_ptr == '-');
1452 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001453
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001454 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001455
Rob Landleyb73451d2006-02-24 16:29:00 +00001456 while (isdigit(*++line_ptr))
1457 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001458
Rob Landleyb73451d2006-02-24 16:29:00 +00001459 switch (*line_ptr) {
1460 case 'c':
1461 case 'C':
1462 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001463 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001464 break;
1465 case 'K':
1466 absolute = 1024;
1467 break;
1468 case 'k':
1469 absolute = 1000;
1470 break;
1471 case 'm':
1472 case 'M':
1473 absolute = 1000000;
1474 break;
1475 case 'g':
1476 case 'G':
1477 absolute = 1000000000;
1478 break;
1479 default:
1480 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001482 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001483 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001484 unsigned long unit;
1485
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001486 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001487 unit = sector_size * units_per_sector;
1488 bytes += unit/2; /* round */
1489 bytes /= unit;
1490 i = bytes;
1491 }
1492 if (minus)
1493 i = -i;
1494 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001495 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001496 i = atoi(line_ptr);
1497 while (isdigit(*line_ptr)) {
1498 line_ptr++;
1499 use_default = 0;
1500 }
1501 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001502 if (use_default) {
1503 i = dflt;
1504 printf("Using default value %u\n", i);
1505 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001506 if (i >= low && i <= high)
1507 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001508 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001509 }
1510 return i;
1511}
1512
Rob Landleyb73451d2006-02-24 16:29:00 +00001513static int
1514get_partition(int warn, int max)
1515{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001516 struct pte *pe;
1517 int i;
1518
Denis Vlasenkobd852072007-03-19 14:43:38 +00001519 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001520 pe = &ptes[i];
1521
1522 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001523 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1524 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1525 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1526 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001527 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001528 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001529 }
1530 return i;
1531}
1532
1533static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001534get_existing_partition(int warn, int max)
1535{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001536 int pno = -1;
1537 int i;
1538
1539 for (i = 0; i < max; i++) {
1540 struct pte *pe = &ptes[i];
1541 struct partition *p = pe->part_table;
1542
1543 if (p && !is_cleared_partition(p)) {
1544 if (pno >= 0)
1545 goto not_unique;
1546 pno = i;
1547 }
1548 }
1549 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001550 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 return pno;
1552 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001553 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554 return -1;
1555
1556 not_unique:
1557 return get_partition(warn, max);
1558}
1559
1560static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001561get_nonexisting_partition(int warn, int max)
1562{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001563 int pno = -1;
1564 int i;
1565
1566 for (i = 0; i < max; i++) {
1567 struct pte *pe = &ptes[i];
1568 struct partition *p = pe->part_table;
1569
1570 if (p && is_cleared_partition(p)) {
1571 if (pno >= 0)
1572 goto not_unique;
1573 pno = i;
1574 }
1575 }
1576 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001577 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001578 return pno;
1579 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001580 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001581 return -1;
1582
1583 not_unique:
1584 return get_partition(warn, max);
1585}
1586
1587
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001588static void
1589change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001590{
1591 display_in_cyl_units = !display_in_cyl_units;
1592 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001593 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594 str_units(PLURAL));
1595}
1596
1597static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001598toggle_active(int i)
1599{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001600 struct pte *pe = &ptes[i];
1601 struct partition *p = pe->part_table;
1602
Rob Landleyb73451d2006-02-24 16:29:00 +00001603 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001604 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001605 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1606 pe->changed = 1;
1607}
1608
1609static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001610toggle_dos_compatibility_flag(void)
1611{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001612 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001614 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001615 printf("DOS Compatibility flag is set\n");
1616 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001617 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001618 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001619 }
1620}
1621
1622static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001623delete_partition(int i)
1624{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001625 struct pte *pe = &ptes[i];
1626 struct partition *p = pe->part_table;
1627 struct partition *q = pe->ext_pointer;
1628
1629/* Note that for the fifth partition (i == 4) we don't actually
1630 * decrement partitions.
1631 */
1632
1633 if (warn_geometry())
1634 return; /* C/H/S not set */
1635 pe->changed = 1;
1636
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001637 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001638 sun_delete_partition(i);
1639 return;
1640 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001641 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001642 sgi_delete_partition(i);
1643 return;
1644 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001645
1646 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001647 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001648 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001649 ptes[ext_index].ext_pointer = NULL;
1650 extended_offset = 0;
1651 }
1652 clear_partition(p);
1653 return;
1654 }
1655
1656 if (!q->sys_ind && i > 4) {
1657 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001658 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001659 --i;
1660 clear_partition(ptes[i].ext_pointer);
1661 ptes[i].changed = 1;
1662 } else {
1663 /* not the last one - further ones will be moved down */
1664 if (i > 4) {
1665 /* delete this link in the chain */
1666 p = ptes[i-1].ext_pointer;
1667 *p = *q;
1668 set_start_sect(p, get_start_sect(q));
1669 set_nr_sects(p, get_nr_sects(q));
1670 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001671 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001672 /* the first logical in a longer chain */
1673 pe = &ptes[5];
1674
1675 if (pe->part_table) /* prevent SEGFAULT */
1676 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001677 get_partition_start(pe) -
1678 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001679 pe->offset = extended_offset;
1680 pe->changed = 1;
1681 }
1682
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001683 if (g_partitions > 5) {
1684 g_partitions--;
1685 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001686 ptes[i] = ptes[i+1];
1687 i++;
1688 }
1689 } else
1690 /* the only logical: clear only */
1691 clear_partition(ptes[i].part_table);
1692 }
1693}
1694
1695static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001696change_sysid(void)
1697{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001698 int i, sys, origsys;
1699 struct partition *p;
1700
Eric Andersen040f4402003-07-30 08:40:37 +00001701 /* If sgi_label then don't use get_existing_partition,
1702 let the user select a partition, since get_existing_partition()
1703 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001704 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001705 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001706 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001707 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001708 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001709 if (i == -1)
1710 return;
1711 p = ptes[i].part_table;
1712 origsys = sys = get_sysid(i);
1713
1714 /* if changing types T to 0 is allowed, then
1715 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001716 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001717 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001718 return;
1719 }
1720 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001721 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001722
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001723 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001724 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001725 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001726 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001727 /* break; */
1728 }
1729
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001730 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001731 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001732 printf("You cannot change a partition into"
1733 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001734 break;
1735 }
1736 }
1737
1738 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001739#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001740 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001741 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001742 "as Whole disk (5),\n"
1743 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001744 "even Linux likes it\n\n");
1745#endif
1746#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001747 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001748 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001749 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001750 (i == 8 && sys != 0)
1751 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001752 ) {
1753 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001754 "as volume header (0),\nand "
1755 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001756 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001757 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001758#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 if (sys == origsys)
1760 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001761 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001762 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001763 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001764 sgi_change_sysid(i, sys);
1765 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001766 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001767
Denis Vlasenkobd852072007-03-19 14:43:38 +00001768 printf("Changed system type of partition %d "
1769 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001770 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001771 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001772 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1773 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001774 break;
1775 }
1776 }
1777}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001778#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001779
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001780
Denis Vlasenko28703012006-12-19 20:32:02 +00001781/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001782 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1783 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1784 * Lubkin Oct. 1991). */
1785
Rob Landleyb73451d2006-02-24 16:29:00 +00001786static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001787linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001788{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001789 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001790
1791 *c = ls / spc;
1792 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001793 *h = ls / g_sectors;
1794 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001795}
1796
Rob Landleyb73451d2006-02-24 16:29:00 +00001797static void
1798check_consistency(const struct partition *p, int partition)
1799{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001800 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1801 unsigned pec, peh, pes; /* physical ending c, h, s */
1802 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1803 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001805 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806 return; /* do not check extended partitions */
1807
1808/* physical beginning c, h, s */
1809 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1810 pbh = p->head;
1811 pbs = p->sector & 0x3f;
1812
1813/* physical ending c, h, s */
1814 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1815 peh = p->end_head;
1816 pes = p->end_sector & 0x3f;
1817
1818/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001819 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001820
1821/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001822 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001823
1824/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001825 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001826 printf("Partition %d has different physical/logical "
1827 "beginnings (non-Linux?):\n", partition + 1);
1828 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001829 printf("logical=(%d, %d, %d)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001830 }
1831
1832/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001833 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001834 printf("Partition %d has different physical/logical "
1835 "endings:\n", partition + 1);
1836 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1837 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001838 }
1839
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001840/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001841 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001842 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001843 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001844 }
1845}
1846
1847static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001848list_disk_geometry(void)
1849{
Eric Andersen040f4402003-07-30 08:40:37 +00001850 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001851 long megabytes = bytes/1000000;
1852
1853 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001854 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001855 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001856 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001857 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001858 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001859 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001860 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001861 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001862 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001863 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001864 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001865 str_units(PLURAL),
1866 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001867}
1868
1869/*
1870 * Check whether partition entries are ordered by their starting positions.
1871 * Return 0 if OK. Return i if partition i should have been earlier.
1872 * Two separate checks: primary and logical partitions.
1873 */
1874static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001875wrong_p_order(int *prev)
1876{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001877 const struct pte *pe;
1878 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001879 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001880 int i, last_i = 0;
1881
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001882 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001883 if (i == 4) {
1884 last_i = 4;
1885 last_p_start_pos = 0;
1886 }
1887 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001888 p = pe->part_table;
1889 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001890 p_start_pos = get_partition_start(pe);
1891
1892 if (last_p_start_pos > p_start_pos) {
1893 if (prev)
1894 *prev = last_i;
1895 return i;
1896 }
1897
1898 last_p_start_pos = p_start_pos;
1899 last_i = i;
1900 }
1901 }
1902 return 0;
1903}
1904
Denis Vlasenko834410a2006-11-29 12:00:28 +00001905#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001906/*
1907 * Fix the chain of logicals.
1908 * extended_offset is unchanged, the set of sectors used is unchanged
1909 * The chain is sorted so that sectors increase, and so that
1910 * starting sectors increase.
1911 *
1912 * After this it may still be that cfdisk doesnt like the table.
1913 * (This is because cfdisk considers expanded parts, from link to
1914 * end of partition, and these may still overlap.)
1915 * Now
1916 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1917 * may help.
1918 */
1919static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001920fix_chain_of_logicals(void)
1921{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922 int j, oj, ojj, sj, sjj;
1923 struct partition *pj,*pjj,tmp;
1924
1925 /* Stage 1: sort sectors but leave sector of part 4 */
1926 /* (Its sector is the global extended_offset.) */
1927 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001928 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001929 oj = ptes[j].offset;
1930 ojj = ptes[j+1].offset;
1931 if (oj > ojj) {
1932 ptes[j].offset = ojj;
1933 ptes[j+1].offset = oj;
1934 pj = ptes[j].part_table;
1935 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1936 pjj = ptes[j+1].part_table;
1937 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1938 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001939 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001940 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001941 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001942 goto stage1;
1943 }
1944 }
1945
1946 /* Stage 2: sort starting sectors */
1947 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001948 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001949 pj = ptes[j].part_table;
1950 pjj = ptes[j+1].part_table;
1951 sj = get_start_sect(pj);
1952 sjj = get_start_sect(pjj);
1953 oj = ptes[j].offset;
1954 ojj = ptes[j+1].offset;
1955 if (oj+sj > ojj+sjj) {
1956 tmp = *pj;
1957 *pj = *pjj;
1958 *pjj = tmp;
1959 set_start_sect(pj, ojj+sjj-oj);
1960 set_start_sect(pjj, oj+sj-ojj);
1961 goto stage2;
1962 }
1963 }
1964
1965 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001966 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001967 ptes[j].changed = 1;
1968}
1969
1970
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001971static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001972fix_partition_table_order(void)
1973{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001974 struct pte *pei, *pek;
1975 int i,k;
1976
1977 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001978 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001979 return;
1980 }
1981
1982 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1983 /* partition i should have come earlier, move it */
1984 /* We have to move data in the MBR */
1985 struct partition *pi, *pk, *pe, pbuf;
1986 pei = &ptes[i];
1987 pek = &ptes[k];
1988
1989 pe = pei->ext_pointer;
1990 pei->ext_pointer = pek->ext_pointer;
1991 pek->ext_pointer = pe;
1992
1993 pi = pei->part_table;
1994 pk = pek->part_table;
1995
1996 memmove(&pbuf, pi, sizeof(struct partition));
1997 memmove(pi, pk, sizeof(struct partition));
1998 memmove(pk, &pbuf, sizeof(struct partition));
1999
2000 pei->changed = pek->changed = 1;
2001 }
2002
2003 if (i)
2004 fix_chain_of_logicals();
2005
2006 printf("Done.\n");
2007
2008}
2009#endif
2010
2011static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002012list_table(int xtra)
2013{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002014 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002015 int i, w;
2016
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002017 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002018 sun_list_table(xtra);
2019 return;
2020 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002021 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002022 sgi_list_table(xtra);
2023 return;
2024 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002025
2026 list_disk_geometry();
2027
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002028 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002029 xbsd_print_disklabel(xtra);
2030 return;
2031 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002032
2033 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2034 but if the device name ends in a digit, say /dev/foo1,
2035 then the partition is called /dev/foo1p3. */
2036 w = strlen(disk_device);
2037 if (w && isdigit(disk_device[w-1]))
2038 w++;
2039 if (w < 5)
2040 w = 5;
2041
Denis Vlasenkobd852072007-03-19 14:43:38 +00002042 // 1 12345678901 12345678901 12345678901 12
2043 printf("%*s Boot Start End Blocks Id System\n",
2044 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002046 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002047 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002048 ullong psects;
2049 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002050 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002051
2052 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002053 if (!p || is_cleared_partition(p))
2054 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002055
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002056 psects = get_nr_sects(p);
2057 pblocks = psects;
2058 podd = 0;
2059
2060 if (sector_size < 1024) {
2061 pblocks /= (1024 / sector_size);
2062 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002063 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002064 if (sector_size > 1024)
2065 pblocks *= (sector_size / 1024);
2066
2067 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2068 partname(disk_device, i+1, w+2),
2069 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2070 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002071 (ullong) cround(get_partition_start(pe)), /* start */
2072 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002073 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002074 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002075 p->sys_ind, /* type id */
2076 partition_type(p->sys_ind)); /* type name */
2077
2078 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002079 }
2080
2081 /* Is partition table in disk order? It need not be, but... */
2082 /* partition table entries are not checked for correct order if this
2083 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002084 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002085 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002086 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002087 }
2088}
2089
Denis Vlasenko834410a2006-11-29 12:00:28 +00002090#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002091static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002092x_list_table(int extend)
2093{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094 const struct pte *pe;
2095 const struct partition *p;
2096 int i;
2097
Denis Vlasenkobd852072007-03-19 14:43:38 +00002098 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002099 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002100 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002101 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102 pe = &ptes[i];
2103 p = (extend ? pe->ext_pointer : pe->part_table);
2104 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002105 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106 i + 1, p->boot_ind, p->head,
2107 sector(p->sector),
2108 cylinder(p->sector, p->cyl), p->end_head,
2109 sector(p->end_sector),
2110 cylinder(p->end_sector, p->end_cyl),
2111 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2112 if (p->sys_ind)
2113 check_consistency(p, i);
2114 }
2115 }
2116}
2117#endif
2118
Denis Vlasenko834410a2006-11-29 12:00:28 +00002119#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002120static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002121fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002122{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002123 int i;
2124 const struct pte *pe = &ptes[0];
2125 const struct partition *p;
2126
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002127 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002128 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002129 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002130 first[i] = 0xffffffff;
2131 last[i] = 0;
2132 } else {
2133 first[i] = get_partition_start(pe);
2134 last[i] = first[i] + get_nr_sects(p) - 1;
2135 }
2136 }
2137}
2138
2139static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002140check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002141{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002142 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002143
2144 real_s = sector(s) - 1;
2145 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002146 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002147 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002148 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002149 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002150 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002151 n, h + 1, g_heads);
2152 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002153 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002154 "maximum %d\n", n, s, g_sectors);
2155 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002156 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002157 "maximum %d\n", n, real_c + 1, g_cylinders);
2158 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002159 printf("Partition %d: previous sectors %llu disagrees with "
2160 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002161}
2162
2163static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002164verify(void)
2165{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002166 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002167 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002168 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002169 struct partition *p;
2170
2171 if (warn_geometry())
2172 return;
2173
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002174 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175 verify_sun();
2176 return;
2177 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002178 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002179 verify_sgi(1);
2180 return;
2181 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002182
2183 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002184 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002185 struct pte *pe = &ptes[i];
2186
2187 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002188 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002189 check_consistency(p, i);
2190 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002191 printf("Warning: bad start-of-data in "
2192 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002193 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2194 last[i]);
2195 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002196 for (j = 0; j < i; j++) {
2197 if ((first[i] >= first[j] && first[i] <= last[j])
2198 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2199 printf("Warning: partition %d overlaps "
2200 "partition %d\n", j + 1, i + 1);
2201 total += first[i] >= first[j] ?
2202 first[i] : first[j];
2203 total -= last[i] <= last[j] ?
2204 last[i] : last[j];
2205 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002206 }
2207 }
2208 }
2209
2210 if (extended_offset) {
2211 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002212 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002213 get_nr_sects(pex->part_table) - 1;
2214
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002215 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002216 total++;
2217 p = ptes[i].part_table;
2218 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002219 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002220 printf("Warning: partition %d "
2221 "is empty\n", i + 1);
2222 } else if (first[i] < extended_offset || last[i] > e_last) {
2223 printf("Logical partition %d not entirely in "
2224 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002226 }
2227 }
2228
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002229 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002230 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002231 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002232 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002233 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002234 if (total != 0)
2235 printf("%d unallocated sectors\n", total);
2236 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002237}
2238
2239static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002240add_partition(int n, int sys)
2241{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002243 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002244 struct partition *p = ptes[n].part_table;
2245 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002246 ullong limit, temp;
2247 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002248 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002249
2250 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002251 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252 return;
2253 }
2254 fill_bounds(first, last);
2255 if (n < 4) {
2256 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002257 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002258 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002259 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002260 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002261 if (extended_offset) {
2262 first[ext_index] = extended_offset;
2263 last[ext_index] = get_start_sect(q) +
2264 get_nr_sects(q) - 1;
2265 }
2266 } else {
2267 start = extended_offset + sector_offset;
2268 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2269 }
2270 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002271 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272 first[i] = (cround(first[i]) - 1) * units_per_sector;
2273
Denis Vlasenkobd852072007-03-19 14:43:38 +00002274 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002275 do {
2276 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002277 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002278 int lastplusoff;
2279
2280 if (start == ptes[i].offset)
2281 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002282 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002283 if (start >= first[i] && start <= lastplusoff)
2284 start = lastplusoff + 1;
2285 }
2286 if (start > limit)
2287 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002288 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002289 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002290 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002291 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002292 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002293 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002294 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295
2296 saved_start = start;
2297 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2298 0, mesg);
2299 if (display_in_cyl_units) {
2300 start = (start - 1) * units_per_sector;
2301 if (start < saved_start) start = saved_start;
2302 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002303 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002304 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002305 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002306 if (n > 4) { /* NOT for fifth partition */
2307 struct pte *pe = &ptes[n];
2308
2309 pe->offset = start - sector_offset;
2310 if (pe->offset == extended_offset) { /* must be corrected */
2311 pe->offset++;
2312 if (sector_offset == 1)
2313 start++;
2314 }
2315 }
2316
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002317 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002318 struct pte *pe = &ptes[i];
2319
2320 if (start < pe->offset && limit >= pe->offset)
2321 limit = pe->offset - 1;
2322 if (start < first[i] && limit >= first[i])
2323 limit = first[i] - 1;
2324 }
2325 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002326 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002327 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002328 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002329 return;
2330 }
2331 if (cround(start) == cround(limit)) {
2332 stop = limit;
2333 } else {
2334 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002335 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002336 str_units(SINGULAR));
2337 stop = read_int(cround(start), cround(limit), cround(limit),
2338 cround(start), mesg);
2339 if (display_in_cyl_units) {
2340 stop = stop * units_per_sector - 1;
2341 if (stop >limit)
2342 stop = limit;
2343 }
2344 }
2345
2346 set_partition(n, 0, start, stop, sys);
2347 if (n > 4)
2348 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2349
Rob Landleyb73451d2006-02-24 16:29:00 +00002350 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002351 struct pte *pe4 = &ptes[4];
2352 struct pte *pen = &ptes[n];
2353
2354 ext_index = n;
2355 pen->ext_pointer = p;
2356 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002357 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2359 pe4->ext_pointer = pe4->part_table + 1;
2360 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002361 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362 }
2363}
2364
2365static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002366add_logical(void)
2367{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002368 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2369 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002370
Rob Landley081e3842006-08-03 20:07:35 +00002371 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2373 pe->ext_pointer = pe->part_table + 1;
2374 pe->offset = 0;
2375 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002376 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002378 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002379}
2380
2381static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002382new_partition(void)
2383{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384 int i, free_primary = 0;
2385
2386 if (warn_geometry())
2387 return;
2388
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002389 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002390 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002391 return;
2392 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002393 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002394 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 return;
2396 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002397 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002398 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2399"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2400"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 return;
2402 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002404 for (i = 0; i < 4; i++)
2405 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002406
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002407 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002408 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002409 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002410 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002411
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412 if (!free_primary) {
2413 if (extended_offset)
2414 add_logical();
2415 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002416 printf("You must delete some partition and add "
2417 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002418 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002419 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002420 snprintf(line, sizeof(line),
2421 "Command action\n"
2422 " %s\n"
2423 " p primary partition (1-4)\n",
2424 (extended_offset ?
2425 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002426 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002427 c = read_nonempty(line);
2428 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002429 i = get_nonexisting_partition(0, 4);
2430 if (i >= 0)
2431 add_partition(i, LINUX_NATIVE);
2432 return;
2433 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002434 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002435 add_logical();
2436 return;
2437 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002438 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 i = get_nonexisting_partition(0, 4);
2440 if (i >= 0)
2441 add_partition(i, EXTENDED);
2442 return;
2443 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002444 printf("Invalid partition number "
2445 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 }
2447 }
2448}
2449
2450static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002451write_table(void)
2452{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002453 int i;
2454
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002455 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002456 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 if (ptes[i].changed)
2458 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002459 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002460 struct pte *pe = &ptes[i];
2461
2462 if (pe->changed) {
2463 write_part_table_flag(pe->sectorbuffer);
2464 write_sector(pe->offset, pe->sectorbuffer);
2465 }
2466 }
2467 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002468 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002469 /* no test on change? the printf below might be mistaken */
2470 sgi_write_table();
2471 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002472 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473 int needw = 0;
2474
Rob Landleyb73451d2006-02-24 16:29:00 +00002475 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476 if (ptes[i].changed)
2477 needw = 1;
2478 if (needw)
2479 sun_write_table();
2480 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002481
Denis Vlasenkobd852072007-03-19 14:43:38 +00002482 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483 reread_partition_table(1);
2484}
2485
Rob Landleyb73451d2006-02-24 16:29:00 +00002486static void
2487reread_partition_table(int leave)
2488{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 int i;
2490
Denis Vlasenkobd852072007-03-19 14:43:38 +00002491 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002493 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002494 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002495 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002496 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002497#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002499 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002500 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002501 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002502 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002503#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002504
2505 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002506 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002507 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002508 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 }
2510}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002511#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512
Denis Vlasenko834410a2006-11-29 12:00:28 +00002513#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002514#define MAX_PER_LINE 16
2515static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002516print_buffer(char *pbuffer)
2517{
2518 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519
2520 for (i = 0, l = 0; i < sector_size; i++, l++) {
2521 if (l == 0)
2522 printf("0x%03X:", i);
2523 printf(" %02X", (unsigned char) pbuffer[i]);
2524 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002525 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 l = -1;
2527 }
2528 }
2529 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002530 bb_putchar('\n');
2531 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002532}
2533
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002535print_raw(void)
2536{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 int i;
2538
Denis Vlasenkobd852072007-03-19 14:43:38 +00002539 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002540 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002542 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002543 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002544 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002545 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546}
2547
2548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002549move_begin(int i)
2550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551 struct pte *pe = &ptes[i];
2552 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002553 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554
2555 if (warn_geometry())
2556 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002557 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002558 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559 return;
2560 }
2561 first = get_partition_start(pe);
2562 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002563 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564
2565 if (new != get_nr_sects(p)) {
2566 first = get_nr_sects(p) + get_start_sect(p) - new;
2567 set_nr_sects(p, first);
2568 set_start_sect(p, new);
2569 pe->changed = 1;
2570 }
2571}
2572
2573static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002574xselect(void)
2575{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002576 char c;
2577
Rob Landleyb73451d2006-02-24 16:29:00 +00002578 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002579 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002580 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 switch (c) {
2582 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002583 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002584 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585 break;
2586 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002588 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 break;
2590 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002591 user_cylinders = g_cylinders =
2592 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002593 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002595 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002596 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 warn_cylinders();
2598 break;
2599 case 'd':
2600 print_raw();
2601 break;
2602 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002603 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002604 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002605 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 x_list_table(1);
2609 break;
2610 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002611 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002612 fix_partition_table_order();
2613 break;
2614 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002615#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 create_sgilabel();
2617#endif
2618 break;
2619 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002620 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002621 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 update_units();
2623 break;
2624 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 break;
2628 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002629 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002630 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002631 break;
2632 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002633 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 list_table(1);
2635 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002636 x_list_table(0);
2637 break;
2638 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002639 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002640 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002641 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002642 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002643 case 'r':
2644 return;
2645 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002646 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002647 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002648 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002649 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002650 printf("Warning: setting sector offset for DOS "
2651 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002652 }
2653 update_units();
2654 break;
2655 case 'v':
2656 verify();
2657 break;
2658 case 'w':
2659 write_table(); /* does not return */
2660 break;
2661 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002662 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002664 break;
2665 default:
2666 xmenu();
2667 }
2668 }
2669}
2670#endif /* ADVANCED mode */
2671
2672static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002673is_ide_cdrom_or_tape(const char *device)
2674{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002675 FILE *procf;
2676 char buf[100];
2677 struct stat statbuf;
2678 int is_ide = 0;
2679
2680 /* No device was given explicitly, and we are trying some
2681 likely things. But opening /dev/hdc may produce errors like
2682 "hdc: tray open or drive not ready"
2683 if it happens to be a CD-ROM drive. It even happens that
2684 the process hangs on the attempt to read a music CD.
2685 So try to be careful. This only works since 2.1.73. */
2686
2687 if (strncmp("/dev/hd", device, 7))
2688 return 0;
2689
2690 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002691 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2693 is_ide = (!strncmp(buf, "cdrom", 5) ||
2694 !strncmp(buf, "tape", 4));
2695 else
2696 /* Now when this proc file does not exist, skip the
2697 device when it is read-only. */
2698 if (stat(device, &statbuf) == 0)
2699 is_ide = ((statbuf.st_mode & 0222) == 0);
2700
2701 if (procf)
2702 fclose(procf);
2703 return is_ide;
2704}
2705
Rob Landley5527b912006-02-25 03:46:10 +00002706
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002708open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002709{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002710 int gb;
2711
2712 disk_device = device;
2713 if (setjmp(listingbuf))
2714 return;
2715 if (!user_specified)
2716 if (is_ide_cdrom_or_tape(device))
2717 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002718
2719 /* Open disk_device, save file descriptor to dev_fd */
2720 errno = 0;
2721 gb = get_boot(TRY_ONLY);
2722 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 /* Ignore other errors, since we try IDE
2724 and SCSI hard disks which may not be
2725 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002726 if (user_specified || errno == EACCES)
2727 bb_perror_msg("can't open '%s'", device);
2728 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002729 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002730
2731 if (gb < 0) { /* no DOS signature */
2732 list_disk_geometry();
2733 if (LABEL_IS_AIX)
2734 goto ret;
2735#if ENABLE_FEATURE_OSF_LABEL
2736 if (bsd_trydev(device) < 0)
2737#endif
2738 printf("Disk %s doesn't contain a valid "
2739 "partition table\n", device);
2740 } else {
2741 list_table(0);
2742#if ENABLE_FEATURE_FDISK_WRITABLE
2743 if (!LABEL_IS_SUN && g_partitions > 4) {
2744 delete_partition(ext_index);
2745 }
2746#endif
2747 }
2748 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002749 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750}
2751
2752/* for fdisk -l: try all things in /proc/partitions
2753 that look like a partition name (do not end in a digit) */
2754static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002755list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002756{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002757 FILE *procpt;
2758 char line[100], ptname[100], devname[120], *s;
2759 int ma, mi, sz;
2760
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002761 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002762
2763 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002764 if (sscanf(line, " %d %d %d %[^\n ]",
2765 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002766 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002767 for (s = ptname; *s; s++)
2768 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002769 if (isdigit(s[-1]))
2770 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002771 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002772 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002773 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002774#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002775 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002776#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777}
2778
Denis Vlasenko834410a2006-11-29 12:00:28 +00002779#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002780static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002781unknown_command(int c)
2782{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002783 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002785#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002787int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002788int fdisk_main(int argc, char **argv)
2789{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002790 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002792 * fdisk -v
2793 * fdisk -l [-b sectorsize] [-u] device ...
2794 * fdisk -s [partition] ...
2795 * fdisk [-b sectorsize] [-u] device
2796 *
2797 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002798 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002799 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002800
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002801 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002802
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002803 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00002804 opt = getopt32(argv, "b:C:H:lS:u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002805 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002806 argc -= optind;
2807 argv += optind;
2808 if (opt & OPT_b) { // -b
2809 /* Ugly: this sector size is really per device,
2810 so cannot be combined with multiple disks,
2811 and the same goes for the C/H/S options.
2812 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002813 if (sector_size != 512 && sector_size != 1024
2814 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002815 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002816 sector_offset = 2;
2817 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002818 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002819 if (user_heads <= 0 || user_heads >= 256)
2820 user_heads = 0;
2821 if (user_sectors <= 0 || user_sectors >= 64)
2822 user_sectors = 0;
2823 if (opt & OPT_u)
2824 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825
Denis Vlasenko834410a2006-11-29 12:00:28 +00002826#if ENABLE_FEATURE_FDISK_WRITABLE
2827 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002829#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002830 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002832 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002833 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002834 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002836 /* we don't have device names, */
2837 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002838 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002839 }
2840 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002841#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002842 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002843#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002844
Denis Vlasenko834410a2006-11-29 12:00:28 +00002845#if ENABLE_FEATURE_FDISK_BLKSIZE
2846 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002847 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002848
2849 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002851 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002852 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002853 unsigned long long size;
2854 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002855 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002857 if (argc == 1)
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002858 printf("%lld\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859 else
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002860 printf("%s: %lld\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861 }
2862 return 0;
2863 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002864#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865
Denis Vlasenko834410a2006-11-29 12:00:28 +00002866#if ENABLE_FEATURE_FDISK_WRITABLE
2867 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002868 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002869
Denis Vlasenko834410a2006-11-29 12:00:28 +00002870 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002871 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002873 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002875 printf("Detected an OSF/1 disklabel on %s, entering "
2876 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002877 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002878 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002879 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 /* If we return we may want to make an empty DOS label? */
2881 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882
2883 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002884 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002885 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002886 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002887 switch (c) {
2888 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002889 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002890 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002891 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002892 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002894 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002896 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002897 else
2898 unknown_command(c);
2899 break;
2900 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002901 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002902 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002903 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002904 if (read_maybe_empty("Please enter the name of the "
2905 "new boot file: ") == '\n')
2906 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907 else
2908 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002909 }
2910#if ENABLE_FEATURE_OSF_LABEL
2911 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002912 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913#endif
2914 break;
2915 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002916 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002918 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002919 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002920 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002921 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002923 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 else
2925 unknown_command(c);
2926 break;
2927 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002928 {
Eric Andersen040f4402003-07-30 08:40:37 +00002929 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002930 /* If sgi_label then don't use get_existing_partition,
2931 let the user select a partition, since
2932 get_existing_partition() only works for Linux-like
2933 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002935 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002936 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002937 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002938 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002939 if (j >= 0)
2940 delete_partition(j);
2941 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942 break;
2943 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002945 create_sgiinfo();
2946 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947 unknown_command(c);
2948 case 'l':
2949 list_types(get_sys_types());
2950 break;
2951 case 'm':
2952 menu();
2953 break;
2954 case 'n':
2955 new_partition();
2956 break;
2957 case 'o':
2958 create_doslabel();
2959 break;
2960 case 'p':
2961 list_table(0);
2962 break;
2963 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002964 if (ENABLE_FEATURE_CLEAN_UP)
2965 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002966 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002967 return 0;
2968 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002969#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002970 create_sunlabel();
2971#endif
2972 break;
2973 case 't':
2974 change_sysid();
2975 break;
2976 case 'u':
2977 change_units();
2978 break;
2979 case 'v':
2980 verify();
2981 break;
2982 case 'w':
2983 write_table(); /* does not return */
2984 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002985#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002986 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002987 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002988 printf("\n\tSorry, no experts menu for SGI "
2989 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002991 xselect();
2992 break;
2993#endif
2994 default:
2995 unknown_command(c);
2996 menu();
2997 }
2998 }
2999 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003000#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003001}