blob: 50ab48dab7c1484216768af0e89dc6b6c6cb2a35 [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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000016
Denis Vlasenko834410a2006-11-29 12:00:28 +000017/* Looks like someone forgot to add this to config system */
18#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
19# define ENABLE_FEATURE_FDISK_BLKSIZE 0
20# define USE_FEATURE_FDISK_BLKSIZE(a)
21#endif
22
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000023#define DEFAULT_SECTOR_SIZE 512
24#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000025#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000026#define MAXIMUM_PARTS 60
27
28#define ACTIVE_FLAG 0x80
29
30#define EXTENDED 0x05
31#define WIN98_EXTENDED 0x0f
32#define LINUX_PARTITION 0x81
33#define LINUX_SWAP 0x82
34#define LINUX_NATIVE 0x83
35#define LINUX_EXTENDED 0x85
36#define LINUX_LVM 0x8e
37#define LINUX_RAID 0xfd
38
Denis Vlasenkoc033d512008-04-17 01:52:28 +000039
40enum {
41 OPT_b = 1 << 0,
42 OPT_C = 1 << 1,
43 OPT_H = 1 << 2,
44 OPT_l = 1 << 3,
45 OPT_S = 1 << 4,
46 OPT_u = 1 << 5,
47 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
48};
49
50
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000051/* Used for sector numbers. Today's disk sizes make it necessary */
52typedef unsigned long long ullong;
53
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000054struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000055 unsigned char heads;
56 unsigned char sectors;
57 unsigned short cylinders;
58 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000059};
60
Denis Vlasenko98ae2162006-10-12 19:30:44 +000061#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000062
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000063static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000064"Building a new %s. Changes will remain in memory only,\n"
65"until you decide to write them. After that the previous content\n"
66"won't be recoverable.\n\n";
67
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000068static const char msg_part_already_defined[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000069"Partition %d is already defined, delete it before re-adding\n";
70
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000071
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072struct partition {
73 unsigned char boot_ind; /* 0x80 - active */
74 unsigned char head; /* starting head */
75 unsigned char sector; /* starting sector */
76 unsigned char cyl; /* starting cylinder */
77 unsigned char sys_ind; /* What partition type */
78 unsigned char end_head; /* end head */
79 unsigned char end_sector; /* end sector */
80 unsigned char end_cyl; /* end cylinder */
81 unsigned char start4[4]; /* starting sector counting from 0 */
82 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000083} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000084
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000085static const char unable_to_open[] ALIGN1 = "cannot open %s";
86static const char unable_to_read[] ALIGN1 = "cannot read from %s";
87static const char unable_to_seek[] ALIGN1 = "cannot seek on %s";
88static const char unable_to_write[] ALIGN1 = "cannot write to %s";
89static const char ioctl_error[] ALIGN1 = "BLKGETSIZE ioctl failed on %s";
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000090static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000091
Denis Vlasenko98ae2162006-10-12 19:30:44 +000092enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +000093 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +000094};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000095
Denis Vlasenko4437d192008-04-17 00:12:10 +000096#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000097
Denis Vlasenko834410a2006-11-29 12:00:28 +000098#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +000099#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000100#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000101#else
102#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000103#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000104#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000105
Denis Vlasenko834410a2006-11-29 12:00:28 +0000106#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000107#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000108#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000109#else
110#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000111#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000112#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000113
Denis Vlasenko834410a2006-11-29 12:00:28 +0000114#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000115#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000116#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000117#else
118#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000119#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000120#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000121
Denis Vlasenko834410a2006-11-29 12:00:28 +0000122#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000123#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000124#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000125#else
126#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000127#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000128#endif
Rob Landley5527b912006-02-25 03:46:10 +0000129
Denis Vlasenko4437d192008-04-17 00:12:10 +0000130enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000131
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000132static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000133#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000134static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000135static void reread_partition_table(int leave);
136static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000137static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000138static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000139static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000140#endif
141static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000142static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000143#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000144static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000145#else
146static int get_boot(void);
147#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000148
149#define PLURAL 0
150#define SINGULAR 1
151
Denis Vlasenko28703012006-12-19 20:32:02 +0000152static unsigned get_start_sect(const struct partition *p);
153static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000154
155/*
156 * per partition table entry data
157 *
158 * The four primary partitions have the same sectorbuffer (MBRbuffer)
159 * and have NULL ext_pointer.
160 * Each logical partition table entry has two pointers, one for the
161 * partition and one link to the next one.
162 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000163struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000164 struct partition *part_table; /* points into sectorbuffer */
165 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000166 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000167 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000168#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000169 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000170#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000171};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000172
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000173/* DOS partition types */
174
175static const char *const i386_sys_types[] = {
176 "\x00" "Empty",
177 "\x01" "FAT12",
178 "\x04" "FAT16 <32M",
179 "\x05" "Extended", /* DOS 3.3+ extended partition */
180 "\x06" "FAT16", /* DOS 16-bit >=32M */
181 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
182 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
183 "\x0b" "Win95 FAT32",
184 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
185 "\x0e" "Win95 FAT16 (LBA)",
186 "\x0f" "Win95 Ext'd (LBA)",
187 "\x11" "Hidden FAT12",
188 "\x12" "Compaq diagnostics",
189 "\x14" "Hidden FAT16 <32M",
190 "\x16" "Hidden FAT16",
191 "\x17" "Hidden HPFS/NTFS",
192 "\x1b" "Hidden Win95 FAT32",
193 "\x1c" "Hidden W95 FAT32 (LBA)",
194 "\x1e" "Hidden W95 FAT16 (LBA)",
195 "\x3c" "Part.Magic recovery",
196 "\x41" "PPC PReP Boot",
197 "\x42" "SFS",
198 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
199 "\x80" "Old Minix", /* Minix 1.4a and earlier */
200 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
201 "\x82" "Linux swap", /* also Solaris */
202 "\x83" "Linux",
203 "\x84" "OS/2 hidden C: drive",
204 "\x85" "Linux extended",
205 "\x86" "NTFS volume set",
206 "\x87" "NTFS volume set",
207 "\x8e" "Linux LVM",
208 "\x9f" "BSD/OS", /* BSDI */
209 "\xa0" "Thinkpad hibernation",
210 "\xa5" "FreeBSD", /* various BSD flavours */
211 "\xa6" "OpenBSD",
212 "\xa8" "Darwin UFS",
213 "\xa9" "NetBSD",
214 "\xab" "Darwin boot",
215 "\xb7" "BSDI fs",
216 "\xb8" "BSDI swap",
217 "\xbe" "Solaris boot",
218 "\xeb" "BeOS fs",
219 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
220 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
221 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
222 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
223 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
224 autodetect using persistent
225 superblock */
226#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
227 "\x02" "XENIX root",
228 "\x03" "XENIX usr",
229 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
230 "\x09" "AIX bootable", /* AIX data or Coherent */
231 "\x10" "OPUS",
232 "\x18" "AST SmartSleep",
233 "\x24" "NEC DOS",
234 "\x39" "Plan 9",
235 "\x40" "Venix 80286",
236 "\x4d" "QNX4.x",
237 "\x4e" "QNX4.x 2nd part",
238 "\x4f" "QNX4.x 3rd part",
239 "\x50" "OnTrack DM",
240 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
241 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
242 "\x53" "OnTrack DM6 Aux3",
243 "\x54" "OnTrackDM6",
244 "\x55" "EZ-Drive",
245 "\x56" "Golden Bow",
246 "\x5c" "Priam Edisk",
247 "\x61" "SpeedStor",
248 "\x64" "Novell Netware 286",
249 "\x65" "Novell Netware 386",
250 "\x70" "DiskSecure Multi-Boot",
251 "\x75" "PC/IX",
252 "\x93" "Amoeba",
253 "\x94" "Amoeba BBT", /* (bad block table) */
254 "\xa7" "NeXTSTEP",
255 "\xbb" "Boot Wizard hidden",
256 "\xc1" "DRDOS/sec (FAT-12)",
257 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
258 "\xc6" "DRDOS/sec (FAT-16)",
259 "\xc7" "Syrinx",
260 "\xda" "Non-FS data",
261 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
262 Concurrent DOS or CTOS */
263 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
264 "\xdf" "BootIt", /* BootIt EMBRM */
265 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
266 extended partition */
267 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
268 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
269 partition < 1024 cyl. */
270 "\xf1" "SpeedStor",
271 "\xf4" "SpeedStor", /* SpeedStor large partition */
272 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
273 "\xff" "BBT", /* Xenix Bad Block Table */
274#endif
275 NULL
276};
277
Denis Vlasenko4437d192008-04-17 00:12:10 +0000278enum {
279 dev_fd = 3 /* the disk */
280};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000281
282/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000283struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000284 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000285
286 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000287 int g_partitions; // = 4; /* maximum partition + 1 */
288 unsigned units_per_sector; // = 1;
289 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
290 unsigned user_set_sector_size;
291 unsigned sector_offset; // = 1;
292 unsigned g_heads, g_sectors, g_cylinders;
293 enum label_type current_label_type;
294 smallint display_in_cyl_units; // = 1;
295#if ENABLE_FEATURE_OSF_LABEL
296 smallint possibly_osf_label;
297#endif
298
299 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000300 char line_buffer[80];
301 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000302 /* Raw disk label. For DOS-type partition tables the MBR,
303 * with descriptions of the primary partitions. */
304 char MBRbuffer[MAX_SECTOR_SIZE];
305 /* Partition tables */
306 struct pte ptes[MAXIMUM_PARTS];
307};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000308#define G (*ptr_to_globals)
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000309#define line_ptr (G.line_ptr)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000310#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000311#define g_partitions (G.g_partitions )
312#define units_per_sector (G.units_per_sector )
313#define sector_size (G.sector_size )
314#define user_set_sector_size (G.user_set_sector_size)
315#define sector_offset (G.sector_offset )
316#define g_heads (G.g_heads )
317#define g_sectors (G.g_sectors )
318#define g_cylinders (G.g_cylinders )
319#define current_label_type (G.current_label_type )
320#define display_in_cyl_units (G.display_in_cyl_units)
321#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000322#define listingbuf (G.listingbuf)
323#define line_buffer (G.line_buffer)
324#define partname_buffer (G.partname_buffer)
325#define MBRbuffer (G.MBRbuffer)
326#define ptes (G.ptes)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000327#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000328 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000329 sector_size = DEFAULT_SECTOR_SIZE; \
330 sector_offset = 1; \
331 g_partitions = 4; \
332 display_in_cyl_units = 1; \
333 units_per_sector = 1; \
334} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000335
Denis Vlasenkobd852072007-03-19 14:43:38 +0000336
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000337/* TODO: move to libbb? */
Denis Vlasenko4437d192008-04-17 00:12:10 +0000338static ullong bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000339{
340 uint64_t v64;
341 unsigned long longsectors;
342
343 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
344 /* got bytes, convert to 512 byte sectors */
345 return (v64 >> 9);
346 }
347 /* Needs temp of type long */
348 if (ioctl(fd, BLKGETSIZE, &longsectors))
349 longsectors = 0;
350 return longsectors;
351}
352
353
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000354#define IS_EXTENDED(i) \
355 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
356
357#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
358
359#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
360
361#define pt_offset(b, n) \
362 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
363
364#define sector(s) ((s) & 0x3f)
365
366#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
367
368#define hsc2sector(h,s,c) \
369 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
370
371#define set_hsc(h,s,c,sector) \
372 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000373 s = sector % g_sectors + 1; \
374 sector /= g_sectors; \
375 h = sector % g_heads; \
376 sector /= g_heads; \
377 c = sector & 0xff; \
378 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000379 } while (0)
380
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000381static void
382close_dev_fd(void)
383{
384 /* Not really closing, but making sure it is open, and to harmless place */
385 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
386}
387
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000388#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000389/* read line; return 0 or first printable char */
390static int
391read_line(const char *prompt)
392{
393 int sz;
394
395 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
396 if (sz <= 0)
397 exit(0); /* Ctrl-D or Ctrl-C */
398
399 if (line_buffer[sz-1] == '\n')
400 line_buffer[--sz] = '\0';
401
402 line_ptr = line_buffer;
403 while (*line_ptr && !isgraph(*line_ptr))
404 line_ptr++;
405 return *line_ptr;
406}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000407#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000408
Denis Vlasenkobd852072007-03-19 14:43:38 +0000409/*
410 * return partition name - uses static storage
411 */
412static const char *
413partname(const char *dev, int pno, int lth)
414{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000415 const char *p;
416 int w, wp;
417 int bufsiz;
418 char *bufp;
419
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000420 bufp = partname_buffer;
421 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000422
423 w = strlen(dev);
424 p = "";
425
426 if (isdigit(dev[w-1]))
427 p = "p";
428
429 /* devfs kludge - note: fdisk partition names are not supposed
430 to equal kernel names, so there is no reason to do this */
431 if (strcmp(dev + w - 4, "disc") == 0) {
432 w -= 4;
433 p = "part";
434 }
435
436 wp = strlen(p);
437
438 if (lth) {
439 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
440 lth-wp-2, w, dev, p, pno);
441 } else {
442 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
443 }
444 return bufp;
445}
446
Denis Vlasenko834410a2006-11-29 12:00:28 +0000447#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000448static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000449set_all_unchanged(void)
450{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000451 int i;
452
453 for (i = 0; i < MAXIMUM_PARTS; i++)
454 ptes[i].changed = 0;
455}
456
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000457static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000458set_changed(int i)
459{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000460 ptes[i].changed = 1;
461}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000462#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000463
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000464static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000465get_part_table(int i)
466{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000467 return ptes[i].part_table;
468}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000469
470static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000471str_units(int n)
472{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000473 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000474 return display_in_cyl_units ? "cylinder" : "sector";
475 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000476}
477
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000478static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000479valid_part_table_flag(const char *mbuffer)
480{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000481 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000482}
483
Denis Vlasenko834410a2006-11-29 12:00:28 +0000484#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000485static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000486write_part_table_flag(char *b)
487{
488 b[510] = 0x55;
489 b[511] = 0xaa;
490}
491
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000492static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000493read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000494{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000495 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000496 return *line_ptr;
497}
498
499static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000500read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000501{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000502 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000503 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000504 line_ptr[0] = '\n';
505 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000506 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000507 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000508}
509
510static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000511read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000512{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000513 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000514 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000515 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000516 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000517 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000518 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000519 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000520 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000521 if (v > 0xff)
522 /* Bad input also triggers this */
523 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000524 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000525 }
526}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000527#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000528
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000529#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000530
531typedef struct {
532 unsigned char info[128]; /* Informative text string */
533 unsigned char spare0[14];
534 struct sun_info {
535 unsigned char spare1;
536 unsigned char id;
537 unsigned char spare2;
538 unsigned char flags;
539 } infos[8];
540 unsigned char spare1[246]; /* Boot information etc. */
541 unsigned short rspeed; /* Disk rotational speed */
542 unsigned short pcylcount; /* Physical cylinder count */
543 unsigned short sparecyl; /* extra sects per cylinder */
544 unsigned char spare2[4]; /* More magic... */
545 unsigned short ilfact; /* Interleave factor */
546 unsigned short ncyl; /* Data cylinder count */
547 unsigned short nacyl; /* Alt. cylinder count */
548 unsigned short ntrks; /* Tracks per cylinder */
549 unsigned short nsect; /* Sectors per track */
550 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000551 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000552 uint32_t start_cylinder;
553 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554 } partitions[8];
555 unsigned short magic; /* Magic number */
556 unsigned short csum; /* Label xor'd checksum */
557} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000559STATIC_OSF void bsd_select(void);
560STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000561#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000562
Denis Vlasenko28703012006-12-19 20:32:02 +0000563#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000564static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000565fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000566{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000567 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000568}
569
Rob Landley88621d72006-08-29 19:41:06 +0000570static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000571fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000572{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000573 return (x << 24) |
574 ((x & 0xFF00) << 8) |
575 ((x & 0xFF0000) >> 8) |
576 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000577}
578#endif
579
Denis Vlasenkobd852072007-03-19 14:43:38 +0000580STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000581STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000582STATIC_SGI int sgi_get_sysid(int i);
583STATIC_SGI void sgi_delete_partition(int i);
584STATIC_SGI void sgi_change_sysid(int i, int sys);
585STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000586#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000587STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000588#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000589STATIC_SGI int verify_sgi(int verbose);
590STATIC_SGI void sgi_add_partition(int n, int sys);
591STATIC_SGI void sgi_set_swappartition(int i);
592STATIC_SGI const char *sgi_get_bootfile(void);
593STATIC_SGI void sgi_set_bootfile(const char* aFile);
594STATIC_SGI void create_sgiinfo(void);
595STATIC_SGI void sgi_write_table(void);
596STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000597#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598
Denis Vlasenkobd852072007-03-19 14:43:38 +0000599STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000600STATIC_SUN void sun_delete_partition(int i);
601STATIC_SUN void sun_change_sysid(int i, int sys);
602STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000603STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000604#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000605STATIC_SUN void sun_set_alt_cyl(void);
606STATIC_SUN void sun_set_ncyl(int cyl);
607STATIC_SUN void sun_set_xcyl(void);
608STATIC_SUN void sun_set_ilfact(void);
609STATIC_SUN void sun_set_rspeed(void);
610STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000611#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000612STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
613STATIC_SUN void verify_sun(void);
614STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000615#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000616
Denis Vlasenko834410a2006-11-29 12:00:28 +0000617#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000618/* start_sect and nr_sects are stored little endian on all machines */
619/* moreover, they are not aligned correctly */
620static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000621store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000622{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000623 cp[0] = val;
624 cp[1] = val >> 8;
625 cp[2] = val >> 16;
626 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000628#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000629
Denis Vlasenko834410a2006-11-29 12:00:28 +0000630static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000631read4_little_endian(const unsigned char *cp)
632{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000633 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000634}
635
Denis Vlasenko834410a2006-11-29 12:00:28 +0000636#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000637static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000638set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000639{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000640 store4_little_endian(p->start4, start_sect);
641}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000642#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000643
Denis Vlasenko28703012006-12-19 20:32:02 +0000644static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000645get_start_sect(const struct partition *p)
646{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000647 return read4_little_endian(p->start4);
648}
649
Denis Vlasenko834410a2006-11-29 12:00:28 +0000650#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000651static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000652set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000653{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000654 store4_little_endian(p->size4, nr_sects);
655}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000656#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657
Denis Vlasenko28703012006-12-19 20:32:02 +0000658static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000659get_nr_sects(const struct partition *p)
660{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000661 return read4_little_endian(p->size4);
662}
663
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000664static int ext_index; /* the prime extended partition */
665static smallint listing; /* no aborts for fdisk -l */
666static smallint dos_compatible_flag = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000667#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000668//static int dos_changed;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000669static smallint nowarn; /* no warnings for fdisk -l/-s */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000670#endif
671
Denis Vlasenko834410a2006-11-29 12:00:28 +0000672static unsigned user_cylinders, user_heads, user_sectors;
673static unsigned pt_heads, pt_sectors;
674static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000675
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000676static ullong extended_offset; /* offset of link pointers */
677static ullong total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000678
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000679static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681 if (listing) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000682 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000683 longjmp(listingbuf, 1);
684 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000685 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000686}
687
688static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000689seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000690{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000691 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000692#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko4437d192008-04-17 00:12:10 +0000693 if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000694 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000695#else
696 if (secno > MAXINT(off_t)
Denis Vlasenko4437d192008-04-17 00:12:10 +0000697 || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000698 ) {
699 fdisk_fatal(unable_to_seek);
700 }
701#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000702}
703
Denis Vlasenko834410a2006-11-29 12:00:28 +0000704#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000705static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000706write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000707{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000708 seek_sector(secno);
Denis Vlasenko4437d192008-04-17 00:12:10 +0000709 if (write(dev_fd, buf, sector_size) != sector_size)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000710 fdisk_fatal(unable_to_write);
711}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000712#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713
714/* Allocate a buffer and read a partition table sector */
715static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000716read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000717{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000718 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000719 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000720 seek_sector(offset);
Denis Vlasenko4437d192008-04-17 00:12:10 +0000721 if (read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000722 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000723#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000724 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000725#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000726 pe->part_table = pe->ext_pointer = NULL;
727}
728
Denis Vlasenko834410a2006-11-29 12:00:28 +0000729static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000730get_partition_start(const struct pte *pe)
731{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000732 return pe->offset + get_start_sect(pe->part_table);
733}
734
Denis Vlasenko834410a2006-11-29 12:00:28 +0000735#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000736/*
737 * Avoid warning about DOS partitions when no DOS partition was changed.
738 * Here a heuristic "is probably dos partition".
739 * We might also do the opposite and warn in all cases except
740 * for "is probably nondos partition".
741 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000742#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000743static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000744is_dos_partition(int t)
745{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000746 return (t == 1 || t == 4 || t == 6 ||
747 t == 0x0b || t == 0x0c || t == 0x0e ||
748 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
749 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
750 t == 0xc1 || t == 0xc4 || t == 0xc6);
751}
Denis Vlasenko89398812008-01-25 20:18:46 +0000752#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000753
754static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000755menu(void)
756{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000757 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000758 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000759 puts("a\ttoggle a read only flag"); /* sun */
760 puts("b\tedit bsd disklabel");
761 puts("c\ttoggle the mountable flag"); /* sun */
762 puts("d\tdelete a partition");
763 puts("l\tlist known partition types");
764 puts("n\tadd a new partition");
765 puts("o\tcreate a new empty DOS partition table");
766 puts("p\tprint the partition table");
767 puts("q\tquit without saving changes");
768 puts("s\tcreate a new empty Sun disklabel"); /* sun */
769 puts("t\tchange a partition's system id");
770 puts("u\tchange display/entry units");
771 puts("v\tverify the partition table");
772 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000773#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000774 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000775#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000776 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000777 puts("a\tselect bootable partition"); /* sgi flavour */
778 puts("b\tedit bootfile entry"); /* sgi */
779 puts("c\tselect sgi swap partition"); /* sgi flavour */
780 puts("d\tdelete a partition");
781 puts("l\tlist known partition types");
782 puts("n\tadd a new partition");
783 puts("o\tcreate a new empty DOS partition table");
784 puts("p\tprint the partition table");
785 puts("q\tquit without saving changes");
786 puts("s\tcreate a new empty Sun disklabel"); /* sun */
787 puts("t\tchange a partition's system id");
788 puts("u\tchange display/entry units");
789 puts("v\tverify the partition table");
790 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000791 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000792 puts("o\tcreate a new empty DOS partition table");
793 puts("q\tquit without saving changes");
794 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000795 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000796 puts("a\ttoggle a bootable flag");
797 puts("b\tedit bsd disklabel");
798 puts("c\ttoggle the dos compatibility flag");
799 puts("d\tdelete a partition");
800 puts("l\tlist known partition types");
801 puts("n\tadd a new partition");
802 puts("o\tcreate a new empty DOS partition table");
803 puts("p\tprint the partition table");
804 puts("q\tquit without saving changes");
805 puts("s\tcreate a new empty Sun disklabel"); /* sun */
806 puts("t\tchange a partition's system id");
807 puts("u\tchange display/entry units");
808 puts("v\tverify the partition table");
809 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000810#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000811 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000812#endif
813 }
814}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000815#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000816
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000817
Denis Vlasenko834410a2006-11-29 12:00:28 +0000818#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000819static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000820xmenu(void)
821{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000822 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000823 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000824 puts("a\tchange number of alternate cylinders"); /*sun*/
825 puts("c\tchange number of cylinders");
826 puts("d\tprint the raw data in the partition table");
827 puts("e\tchange number of extra sectors per cylinder");/*sun*/
828 puts("h\tchange number of heads");
829 puts("i\tchange interleave factor"); /*sun*/
830 puts("o\tchange rotation speed (rpm)"); /*sun*/
831 puts("p\tprint the partition table");
832 puts("q\tquit without saving changes");
833 puts("r\treturn to main menu");
834 puts("s\tchange number of sectors/track");
835 puts("v\tverify the partition table");
836 puts("w\twrite table to disk and exit");
837 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000838 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000839 puts("b\tmove beginning of data in a partition"); /* !sun */
840 puts("c\tchange number of cylinders");
841 puts("d\tprint the raw data in the partition table");
842 puts("e\tlist extended partitions"); /* !sun */
843 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
844 puts("h\tchange number of heads");
845 puts("p\tprint the partition table");
846 puts("q\tquit without saving changes");
847 puts("r\treturn to main menu");
848 puts("s\tchange number of sectors/track");
849 puts("v\tverify the partition table");
850 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000851 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000852 puts("b\tmove beginning of data in a partition"); /* !sun */
853 puts("c\tchange number of cylinders");
854 puts("d\tprint the raw data in the partition table");
855 puts("e\tlist extended partitions"); /* !sun */
856 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
857 puts("h\tchange number of heads");
858 puts("p\tprint the partition table");
859 puts("q\tquit without saving changes");
860 puts("r\treturn to main menu");
861 puts("s\tchange number of sectors/track");
862 puts("v\tverify the partition table");
863 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000864 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000865 puts("b\tmove beginning of data in a partition"); /* !sun */
866 puts("c\tchange number of cylinders");
867 puts("d\tprint the raw data in the partition table");
868 puts("e\tlist extended partitions"); /* !sun */
869 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000870#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000871 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000872#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000873 puts("h\tchange number of heads");
874 puts("p\tprint the partition table");
875 puts("q\tquit without saving changes");
876 puts("r\treturn to main menu");
877 puts("s\tchange number of sectors/track");
878 puts("v\tverify the partition table");
879 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000880 }
881}
882#endif /* ADVANCED mode */
883
Denis Vlasenko834410a2006-11-29 12:00:28 +0000884#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000885static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000886get_sys_types(void)
887{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000888 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000889 LABEL_IS_SUN ? sun_sys_types :
890 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000891 i386_sys_types);
892}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000893#else
894#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000895#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000896
Denis Vlasenkobd852072007-03-19 14:43:38 +0000897static const char *
898partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000899{
900 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000901 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902
Denis Vlasenkobd852072007-03-19 14:43:38 +0000903 for (i = 0; types[i]; i++)
904 if ((unsigned char)types[i][0] == type)
905 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000906
Denis Vlasenkobd852072007-03-19 14:43:38 +0000907 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000908}
909
910
Denis Vlasenko834410a2006-11-29 12:00:28 +0000911#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000912static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000913get_sysid(int i)
914{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000915 return LABEL_IS_SUN ? sunlabel->infos[i].id :
916 (LABEL_IS_SGI ? sgi_get_sysid(i) :
917 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000918}
919
Denis Vlasenkobd852072007-03-19 14:43:38 +0000920static void
921list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000922{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000923 enum { COLS = 3 };
924
925 unsigned last[COLS];
926 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000927 int i;
928
Denis Vlasenkobd852072007-03-19 14:43:38 +0000929 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000930
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000931 done = 0;
932 for (i = COLS-1; i >= 0; i--) {
933 done += (size + i - done) / (i + 1);
934 last[COLS-1 - i] = done;
935 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000936
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000937 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000939 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000940 (unsigned char)sys[next][0],
941 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000942 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000943 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000944 i = 0;
945 next = ++done;
946 }
947 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000948 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000950#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000951
952static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000953is_cleared_partition(const struct partition *p)
954{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000955 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
956 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
957 get_start_sect(p) || get_nr_sects(p));
958}
959
960static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000961clear_partition(struct partition *p)
962{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000963 if (!p)
964 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000965 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000966}
967
Denis Vlasenko834410a2006-11-29 12:00:28 +0000968#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000969static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000970set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000971{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000972 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000973 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000974
975 if (doext) {
976 p = ptes[i].ext_pointer;
977 offset = extended_offset;
978 } else {
979 p = ptes[i].part_table;
980 offset = ptes[i].offset;
981 }
982 p->boot_ind = 0;
983 p->sys_ind = sysid;
984 set_start_sect(p, start - offset);
985 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000986 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
987 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000988 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000989 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
990 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000991 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
992 ptes[i].changed = 1;
993}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000994#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000995
996static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000997warn_geometry(void)
998{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000999 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001000 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001001
Denis Vlasenkobd852072007-03-19 14:43:38 +00001002 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001003 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001004 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001005 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001006 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001007 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001008 printf(" cylinders");
1009 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001010#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001011 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001012#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001013 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001014 return 1;
1015}
1016
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001017static void
1018update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001020 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021
1022 if (display_in_cyl_units && cyl_units)
1023 units_per_sector = cyl_units;
1024 else
1025 units_per_sector = 1; /* in sectors */
1026}
1027
Denis Vlasenko834410a2006-11-29 12:00:28 +00001028#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001029static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001030warn_cylinders(void)
1031{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001032 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001033 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001034"The number of cylinders for this disk is set to %d.\n"
1035"There is nothing wrong with that, but this is larger than 1024,\n"
1036"and could in certain setups cause problems with:\n"
1037"1) software that runs at boot time (e.g., old versions of LILO)\n"
1038"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001039" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001040 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001041}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001042#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001043
1044static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001045read_extended(int ext)
1046{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 int i;
1048 struct pte *pex;
1049 struct partition *p, *q;
1050
1051 ext_index = ext;
1052 pex = &ptes[ext];
1053 pex->ext_pointer = pex->part_table;
1054
1055 p = pex->part_table;
1056 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058 return;
1059 }
1060
Rob Landleyb73451d2006-02-24 16:29:00 +00001061 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001062 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001063
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001064 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001065 /* This is not a Linux restriction, but
1066 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001067 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001068 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001069#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001070 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001071 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001072 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001073#endif
1074 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 return;
1076 }
1077
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001078 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079
1080 if (!extended_offset)
1081 extended_offset = get_start_sect(p);
1082
1083 q = p = pt_offset(pe->sectorbuffer, 0);
1084 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001085 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001086 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001087 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001088 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001089 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090 else
1091 pe->ext_pointer = p;
1092 } else if (p->sys_ind) {
1093 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001094 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001095 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001096 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001097 else
1098 pe->part_table = p;
1099 }
1100 }
1101
1102 /* very strange code here... */
1103 if (!pe->part_table) {
1104 if (q != pe->ext_pointer)
1105 pe->part_table = q;
1106 else
1107 pe->part_table = q + 1;
1108 }
1109 if (!pe->ext_pointer) {
1110 if (q != pe->part_table)
1111 pe->ext_pointer = q;
1112 else
1113 pe->ext_pointer = q + 1;
1114 }
1115
1116 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001117 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001118 }
1119
Denis Vlasenko834410a2006-11-29 12:00:28 +00001120#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001121 /* remove empty links */
1122 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001123 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001124 struct pte *pe = &ptes[i];
1125
Denis Vlasenkobd852072007-03-19 14:43:38 +00001126 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001127 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001128 ) {
1129 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001130 delete_partition(i);
1131 goto remove; /* numbering changed */
1132 }
1133 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001134#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001135}
1136
Denis Vlasenko834410a2006-11-29 12:00:28 +00001137#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001138static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001139create_doslabel(void)
1140{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001141 int i;
1142
Denis Vlasenkobd852072007-03-19 14:43:38 +00001143 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001144
Denis Vlasenko4437d192008-04-17 00:12:10 +00001145 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001146
Denis Vlasenko834410a2006-11-29 12:00:28 +00001147#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 possibly_osf_label = 0;
1149#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001150 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001151
1152 for (i = 510-64; i < 510; i++)
1153 MBRbuffer[i] = 0;
1154 write_part_table_flag(MBRbuffer);
1155 extended_offset = 0;
1156 set_all_unchanged();
1157 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001158 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001159}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001160#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001161
1162static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001163get_sectorsize(void)
1164{
Rob Landley736e5252006-02-25 03:36:00 +00001165 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001166 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001167 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001168 sector_size = arg;
1169 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001170 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001171 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 }
1173}
1174
Rob Landley88621d72006-08-29 19:41:06 +00001175static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001176get_kernel_geometry(void)
1177{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001178 struct hd_geometry geometry;
1179
Denis Vlasenko4437d192008-04-17 00:12:10 +00001180 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181 kern_heads = geometry.heads;
1182 kern_sectors = geometry.sectors;
1183 /* never use geometry.cylinders - it is truncated */
1184 }
1185}
1186
1187static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001188get_partition_table_geometry(void)
1189{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001190 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 struct partition *p;
1192 int i, h, s, hh, ss;
1193 int first = 1;
1194 int bad = 0;
1195
Eric Andersen3496fdc2006-01-30 23:09:20 +00001196 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001197 return;
1198
1199 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001200 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001201 p = pt_offset(bufp, i);
1202 if (p->sys_ind != 0) {
1203 h = p->end_head + 1;
1204 s = (p->end_sector & 077);
1205 if (first) {
1206 hh = h;
1207 ss = s;
1208 first = 0;
1209 } else if (hh != h || ss != s)
1210 bad = 1;
1211 }
1212 }
1213
1214 if (!first && !bad) {
1215 pt_heads = hh;
1216 pt_sectors = ss;
1217 }
1218}
1219
Rob Landleyb73451d2006-02-24 16:29:00 +00001220static void
1221get_geometry(void)
1222{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001223 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001224
1225 get_sectorsize();
1226 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001227#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001228 guess_device_type();
1229#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001230 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001231 kern_heads = kern_sectors = 0;
1232 pt_heads = pt_sectors = 0;
1233
1234 get_kernel_geometry();
1235 get_partition_table_geometry();
1236
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001237 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001238 pt_heads ? pt_heads :
1239 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001240 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001241 pt_sectors ? pt_sectors :
1242 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001243 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001244
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245 sector_offset = 1;
1246 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001247 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001248
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001249 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1250 if (!g_cylinders)
1251 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252}
1253
1254/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001255 * Opens disk_device and optionally reads MBR.
1256 * FIXME: document what each 'what' value will do!
1257 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001258 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1259 * 0: found or created label
1260 * 1: I/O error
1261 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001262#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1263static int get_boot(enum action what)
1264#else
1265static int get_boot(void)
1266#define get_boot(what) get_boot()
1267#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001268{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001269 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001270
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001271 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272 for (i = 0; i < 4; i++) {
1273 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001274 pe->part_table = pt_offset(MBRbuffer, i);
1275 pe->ext_pointer = NULL;
1276 pe->offset = 0;
1277 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001278#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001279 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001280#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281 }
1282
Denis Vlasenko834410a2006-11-29 12:00:28 +00001283#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001284// ALERT! highly idiotic design!
1285// We end up here when we call get_boot() recursively
1286// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1287// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1288// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1289// So skip opening device _again_...
1290 if (what == CREATE_EMPTY_DOS USE_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
1291 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292
Denis Vlasenko4437d192008-04-17 00:12:10 +00001293 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1294
Denis Vlasenkobd852072007-03-19 14:43:38 +00001295 if (fd < 0) {
1296 fd = open(disk_device, O_RDONLY);
1297 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001298 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001299 return 1;
1300 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001301 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001302 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001304 xmove_fd(fd, dev_fd);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001305 if (512 != read(dev_fd, MBRbuffer, 512)) {
1306 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001307 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001308 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001309 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001310 fdisk_fatal(unable_to_read);
1311 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001312#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001313 fd = open(disk_device, O_RDONLY);
1314 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001315 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001316 if (512 != read(fd, MBRbuffer, 512)) {
1317 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001318 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001319 }
1320 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001321#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001323 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324 update_units();
1325
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 if (check_sun_label())
1328 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001329#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001330#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001331 if (check_sgi_label())
1332 return 0;
1333#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001334#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001335 if (check_aix_label())
1336 return 0;
1337#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001338#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001339 if (check_osf_label()) {
1340 possibly_osf_label = 1;
1341 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001342 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 return 0;
1344 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001345 printf("This disk has both DOS and BSD magic.\n"
1346 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347 }
1348#endif
1349
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001350#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001351 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001352 return -1;
1353#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001354 if (!valid_part_table_flag(MBRbuffer)) {
1355 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001356 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001357 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001358 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359#ifdef __sparc__
Denis Vlasenko4437d192008-04-17 00:12:10 +00001360 USE_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001361#else
1362 create_doslabel();
1363#endif
1364 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001366 /* TRY_ONLY: */
1367 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001368 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001369 created_table:
1370#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001371
Denis Vlasenko4437d192008-04-17 00:12:10 +00001372
1373 USE_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374 warn_geometry();
1375
1376 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001377 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001378 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001379 printf("Ignoring extra extended "
1380 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001381 else
1382 read_extended(i);
1383 }
1384 }
1385
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001386 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001388 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001389 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1390 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001391 pe->sectorbuffer[510],
1392 pe->sectorbuffer[511],
1393 i + 1);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001394 USE_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395 }
1396 }
1397
1398 return 0;
1399}
1400
Denis Vlasenko834410a2006-11-29 12:00:28 +00001401#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402/*
1403 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1404 * If the user hits Enter, DFLT is returned.
1405 * Answers like +10 are interpreted as offsets from BASE.
1406 *
1407 * There is no default if DFLT is not between LOW and HIGH.
1408 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001409static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001410read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001411{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001412 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001414 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001415
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001416 if (dflt < low || dflt > high) {
1417 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001418 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001419 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420
1421 while (1) {
1422 int use_default = default_ok;
1423
1424 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001425 do {
1426 printf(fmt, mesg, low, high, dflt);
1427 read_maybe_empty("");
1428 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1429 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430
Eric Andersen84bdea82004-05-19 10:49:17 +00001431 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001432 int minus = (*line_ptr == '-');
1433 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001434
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001435 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001436
Rob Landleyb73451d2006-02-24 16:29:00 +00001437 while (isdigit(*++line_ptr))
1438 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001439
Rob Landleyb73451d2006-02-24 16:29:00 +00001440 switch (*line_ptr) {
1441 case 'c':
1442 case 'C':
1443 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001444 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001445 break;
1446 case 'K':
1447 absolute = 1024;
1448 break;
1449 case 'k':
1450 absolute = 1000;
1451 break;
1452 case 'm':
1453 case 'M':
1454 absolute = 1000000;
1455 break;
1456 case 'g':
1457 case 'G':
1458 absolute = 1000000000;
1459 break;
1460 default:
1461 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001462 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001463 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001464 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001465 unsigned long unit;
1466
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001467 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001468 unit = sector_size * units_per_sector;
1469 bytes += unit/2; /* round */
1470 bytes /= unit;
1471 i = bytes;
1472 }
1473 if (minus)
1474 i = -i;
1475 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001476 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001477 i = atoi(line_ptr);
1478 while (isdigit(*line_ptr)) {
1479 line_ptr++;
1480 use_default = 0;
1481 }
1482 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001483 if (use_default) {
1484 i = dflt;
1485 printf("Using default value %u\n", i);
1486 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001487 if (i >= low && i <= high)
1488 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001489 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001490 }
1491 return i;
1492}
1493
Rob Landleyb73451d2006-02-24 16:29:00 +00001494static int
1495get_partition(int warn, int max)
1496{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001497 struct pte *pe;
1498 int i;
1499
Denis Vlasenkobd852072007-03-19 14:43:38 +00001500 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501 pe = &ptes[i];
1502
1503 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001504 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1505 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1506 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1507 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001508 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001509 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001510 }
1511 return i;
1512}
1513
1514static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001515get_existing_partition(int warn, int max)
1516{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001517 int pno = -1;
1518 int i;
1519
1520 for (i = 0; i < max; i++) {
1521 struct pte *pe = &ptes[i];
1522 struct partition *p = pe->part_table;
1523
1524 if (p && !is_cleared_partition(p)) {
1525 if (pno >= 0)
1526 goto not_unique;
1527 pno = i;
1528 }
1529 }
1530 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001531 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001532 return pno;
1533 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001534 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001535 return -1;
1536
1537 not_unique:
1538 return get_partition(warn, max);
1539}
1540
1541static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001542get_nonexisting_partition(int warn, int max)
1543{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544 int pno = -1;
1545 int i;
1546
1547 for (i = 0; i < max; i++) {
1548 struct pte *pe = &ptes[i];
1549 struct partition *p = pe->part_table;
1550
1551 if (p && is_cleared_partition(p)) {
1552 if (pno >= 0)
1553 goto not_unique;
1554 pno = i;
1555 }
1556 }
1557 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001558 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001559 return pno;
1560 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001561 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001562 return -1;
1563
1564 not_unique:
1565 return get_partition(warn, max);
1566}
1567
1568
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001569static void
1570change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571{
1572 display_in_cyl_units = !display_in_cyl_units;
1573 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001574 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001575 str_units(PLURAL));
1576}
1577
1578static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001579toggle_active(int i)
1580{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001581 struct pte *pe = &ptes[i];
1582 struct partition *p = pe->part_table;
1583
Rob Landleyb73451d2006-02-24 16:29:00 +00001584 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001585 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1587 pe->changed = 1;
1588}
1589
1590static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001591toggle_dos_compatibility_flag(void)
1592{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001593 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001595 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001596 printf("DOS Compatibility flag is set\n");
1597 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001599 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001600 }
1601}
1602
1603static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001604delete_partition(int i)
1605{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001606 struct pte *pe = &ptes[i];
1607 struct partition *p = pe->part_table;
1608 struct partition *q = pe->ext_pointer;
1609
1610/* Note that for the fifth partition (i == 4) we don't actually
1611 * decrement partitions.
1612 */
1613
1614 if (warn_geometry())
1615 return; /* C/H/S not set */
1616 pe->changed = 1;
1617
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001618 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001619 sun_delete_partition(i);
1620 return;
1621 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001622 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001623 sgi_delete_partition(i);
1624 return;
1625 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001626
1627 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001628 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001629 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001630 ptes[ext_index].ext_pointer = NULL;
1631 extended_offset = 0;
1632 }
1633 clear_partition(p);
1634 return;
1635 }
1636
1637 if (!q->sys_ind && i > 4) {
1638 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001639 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001640 --i;
1641 clear_partition(ptes[i].ext_pointer);
1642 ptes[i].changed = 1;
1643 } else {
1644 /* not the last one - further ones will be moved down */
1645 if (i > 4) {
1646 /* delete this link in the chain */
1647 p = ptes[i-1].ext_pointer;
1648 *p = *q;
1649 set_start_sect(p, get_start_sect(q));
1650 set_nr_sects(p, get_nr_sects(q));
1651 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001652 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001653 /* the first logical in a longer chain */
1654 pe = &ptes[5];
1655
1656 if (pe->part_table) /* prevent SEGFAULT */
1657 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001658 get_partition_start(pe) -
1659 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001660 pe->offset = extended_offset;
1661 pe->changed = 1;
1662 }
1663
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001664 if (g_partitions > 5) {
1665 g_partitions--;
1666 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001667 ptes[i] = ptes[i+1];
1668 i++;
1669 }
1670 } else
1671 /* the only logical: clear only */
1672 clear_partition(ptes[i].part_table);
1673 }
1674}
1675
1676static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001677change_sysid(void)
1678{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001679 int i, sys, origsys;
1680 struct partition *p;
1681
Eric Andersen040f4402003-07-30 08:40:37 +00001682 /* If sgi_label then don't use get_existing_partition,
1683 let the user select a partition, since get_existing_partition()
1684 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001685 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001686 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001687 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001688 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001689 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001690 if (i == -1)
1691 return;
1692 p = ptes[i].part_table;
1693 origsys = sys = get_sysid(i);
1694
1695 /* if changing types T to 0 is allowed, then
1696 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001697 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001698 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001699 return;
1700 }
1701 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001702 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001703
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001704 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001705 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001706 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001707 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001708 /* break; */
1709 }
1710
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001711 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001712 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001713 printf("You cannot change a partition into"
1714 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001715 break;
1716 }
1717 }
1718
1719 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001720#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001721 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001722 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001723 "as Whole disk (5),\n"
1724 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001725 "even Linux likes it\n\n");
1726#endif
1727#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001728 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001729 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001730 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001731 (i == 8 && sys != 0)
1732 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001733 ) {
1734 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001735 "as volume header (0),\nand "
1736 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001737 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001738 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001739#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001740 if (sys == origsys)
1741 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001742 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001743 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001744 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001745 sgi_change_sysid(i, sys);
1746 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001747 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001748
Denis Vlasenkobd852072007-03-19 14:43:38 +00001749 printf("Changed system type of partition %d "
1750 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001751 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001752 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001753 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1754 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001755 break;
1756 }
1757 }
1758}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001759#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001760
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761
Denis Vlasenko28703012006-12-19 20:32:02 +00001762/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001763 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1764 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1765 * Lubkin Oct. 1991). */
1766
Rob Landleyb73451d2006-02-24 16:29:00 +00001767static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001768linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001769{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001770 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001771
1772 *c = ls / spc;
1773 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001774 *h = ls / g_sectors;
1775 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001776}
1777
Rob Landleyb73451d2006-02-24 16:29:00 +00001778static void
1779check_consistency(const struct partition *p, int partition)
1780{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001781 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1782 unsigned pec, peh, pes; /* physical ending c, h, s */
1783 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1784 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001785
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001786 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001787 return; /* do not check extended partitions */
1788
1789/* physical beginning c, h, s */
1790 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1791 pbh = p->head;
1792 pbs = p->sector & 0x3f;
1793
1794/* physical ending c, h, s */
1795 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1796 peh = p->end_head;
1797 pes = p->end_sector & 0x3f;
1798
1799/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001800 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801
1802/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001803 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804
1805/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001806 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001807 printf("Partition %d has different physical/logical "
1808 "beginnings (non-Linux?):\n", partition + 1);
1809 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1810 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001811 }
1812
1813/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001814 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001815 printf("Partition %d has different physical/logical "
1816 "endings:\n", partition + 1);
1817 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1818 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819 }
1820
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001821/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001822 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001823 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001824 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001825 }
1826}
1827
1828static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001829list_disk_geometry(void)
1830{
Eric Andersen040f4402003-07-30 08:40:37 +00001831 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001832 long megabytes = bytes/1000000;
1833
1834 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001835 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001836 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001837 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001838 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001839 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001840 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001841 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001842 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001843 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001844 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001845 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001846 str_units(PLURAL),
1847 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001848}
1849
1850/*
1851 * Check whether partition entries are ordered by their starting positions.
1852 * Return 0 if OK. Return i if partition i should have been earlier.
1853 * Two separate checks: primary and logical partitions.
1854 */
1855static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001856wrong_p_order(int *prev)
1857{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001858 const struct pte *pe;
1859 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001860 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001861 int i, last_i = 0;
1862
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001863 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001864 if (i == 4) {
1865 last_i = 4;
1866 last_p_start_pos = 0;
1867 }
1868 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001869 p = pe->part_table;
1870 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001871 p_start_pos = get_partition_start(pe);
1872
1873 if (last_p_start_pos > p_start_pos) {
1874 if (prev)
1875 *prev = last_i;
1876 return i;
1877 }
1878
1879 last_p_start_pos = p_start_pos;
1880 last_i = i;
1881 }
1882 }
1883 return 0;
1884}
1885
Denis Vlasenko834410a2006-11-29 12:00:28 +00001886#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001887/*
1888 * Fix the chain of logicals.
1889 * extended_offset is unchanged, the set of sectors used is unchanged
1890 * The chain is sorted so that sectors increase, and so that
1891 * starting sectors increase.
1892 *
1893 * After this it may still be that cfdisk doesnt like the table.
1894 * (This is because cfdisk considers expanded parts, from link to
1895 * end of partition, and these may still overlap.)
1896 * Now
1897 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1898 * may help.
1899 */
1900static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001901fix_chain_of_logicals(void)
1902{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001903 int j, oj, ojj, sj, sjj;
1904 struct partition *pj,*pjj,tmp;
1905
1906 /* Stage 1: sort sectors but leave sector of part 4 */
1907 /* (Its sector is the global extended_offset.) */
1908 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001909 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001910 oj = ptes[j].offset;
1911 ojj = ptes[j+1].offset;
1912 if (oj > ojj) {
1913 ptes[j].offset = ojj;
1914 ptes[j+1].offset = oj;
1915 pj = ptes[j].part_table;
1916 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1917 pjj = ptes[j+1].part_table;
1918 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1919 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001920 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001921 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001922 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001923 goto stage1;
1924 }
1925 }
1926
1927 /* Stage 2: sort starting sectors */
1928 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001929 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930 pj = ptes[j].part_table;
1931 pjj = ptes[j+1].part_table;
1932 sj = get_start_sect(pj);
1933 sjj = get_start_sect(pjj);
1934 oj = ptes[j].offset;
1935 ojj = ptes[j+1].offset;
1936 if (oj+sj > ojj+sjj) {
1937 tmp = *pj;
1938 *pj = *pjj;
1939 *pjj = tmp;
1940 set_start_sect(pj, ojj+sjj-oj);
1941 set_start_sect(pjj, oj+sj-ojj);
1942 goto stage2;
1943 }
1944 }
1945
1946 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001947 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001948 ptes[j].changed = 1;
1949}
1950
1951
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001952static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001953fix_partition_table_order(void)
1954{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001955 struct pte *pei, *pek;
1956 int i,k;
1957
1958 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001959 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001960 return;
1961 }
1962
1963 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1964 /* partition i should have come earlier, move it */
1965 /* We have to move data in the MBR */
1966 struct partition *pi, *pk, *pe, pbuf;
1967 pei = &ptes[i];
1968 pek = &ptes[k];
1969
1970 pe = pei->ext_pointer;
1971 pei->ext_pointer = pek->ext_pointer;
1972 pek->ext_pointer = pe;
1973
1974 pi = pei->part_table;
1975 pk = pek->part_table;
1976
1977 memmove(&pbuf, pi, sizeof(struct partition));
1978 memmove(pi, pk, sizeof(struct partition));
1979 memmove(pk, &pbuf, sizeof(struct partition));
1980
1981 pei->changed = pek->changed = 1;
1982 }
1983
1984 if (i)
1985 fix_chain_of_logicals();
1986
1987 printf("Done.\n");
1988
1989}
1990#endif
1991
1992static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001993list_table(int xtra)
1994{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001995 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001996 int i, w;
1997
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001998 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001999 sun_list_table(xtra);
2000 return;
2001 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002002 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002003 sgi_list_table(xtra);
2004 return;
2005 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006
2007 list_disk_geometry();
2008
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002009 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 xbsd_print_disklabel(xtra);
2011 return;
2012 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013
2014 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2015 but if the device name ends in a digit, say /dev/foo1,
2016 then the partition is called /dev/foo1p3. */
2017 w = strlen(disk_device);
2018 if (w && isdigit(disk_device[w-1]))
2019 w++;
2020 if (w < 5)
2021 w = 5;
2022
Denis Vlasenkobd852072007-03-19 14:43:38 +00002023 // 1 12345678901 12345678901 12345678901 12
2024 printf("%*s Boot Start End Blocks Id System\n",
2025 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002026
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002027 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002028 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002029 ullong psects;
2030 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002031 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002032
2033 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002034 if (!p || is_cleared_partition(p))
2035 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002036
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002037 psects = get_nr_sects(p);
2038 pblocks = psects;
2039 podd = 0;
2040
2041 if (sector_size < 1024) {
2042 pblocks /= (1024 / sector_size);
2043 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002044 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002045 if (sector_size > 1024)
2046 pblocks *= (sector_size / 1024);
2047
2048 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2049 partname(disk_device, i+1, w+2),
2050 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2051 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002052 (ullong) cround(get_partition_start(pe)), /* start */
2053 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002054 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002055 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002056 p->sys_ind, /* type id */
2057 partition_type(p->sys_ind)); /* type name */
2058
2059 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002060 }
2061
2062 /* Is partition table in disk order? It need not be, but... */
2063 /* partition table entries are not checked for correct order if this
2064 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002065 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002066 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002067 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002068 }
2069}
2070
Denis Vlasenko834410a2006-11-29 12:00:28 +00002071#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002072static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002073x_list_table(int extend)
2074{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002075 const struct pte *pe;
2076 const struct partition *p;
2077 int i;
2078
Denis Vlasenkobd852072007-03-19 14:43:38 +00002079 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002080 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002081 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002082 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002083 pe = &ptes[i];
2084 p = (extend ? pe->ext_pointer : pe->part_table);
2085 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002086 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002087 i + 1, p->boot_ind, p->head,
2088 sector(p->sector),
2089 cylinder(p->sector, p->cyl), p->end_head,
2090 sector(p->end_sector),
2091 cylinder(p->end_sector, p->end_cyl),
2092 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2093 if (p->sys_ind)
2094 check_consistency(p, i);
2095 }
2096 }
2097}
2098#endif
2099
Denis Vlasenko834410a2006-11-29 12:00:28 +00002100#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002101static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002102fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002103{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002104 int i;
2105 const struct pte *pe = &ptes[0];
2106 const struct partition *p;
2107
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002108 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002109 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002110 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111 first[i] = 0xffffffff;
2112 last[i] = 0;
2113 } else {
2114 first[i] = get_partition_start(pe);
2115 last[i] = first[i] + get_nr_sects(p) - 1;
2116 }
2117 }
2118}
2119
2120static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002121check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002122{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002123 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002124
2125 real_s = sector(s) - 1;
2126 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002127 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002128 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002129 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002130 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002131 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002132 n, h + 1, g_heads);
2133 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002134 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002135 "maximum %d\n", n, s, g_sectors);
2136 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002137 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002138 "maximum %d\n", n, real_c + 1, g_cylinders);
2139 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002140 printf("Partition %d: previous sectors %llu disagrees with "
2141 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142}
2143
2144static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002145verify(void)
2146{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002147 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002148 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002149 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002150 struct partition *p;
2151
2152 if (warn_geometry())
2153 return;
2154
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002155 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156 verify_sun();
2157 return;
2158 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002159 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 verify_sgi(1);
2161 return;
2162 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002163
2164 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002165 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002166 struct pte *pe = &ptes[i];
2167
2168 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002169 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002170 check_consistency(p, i);
2171 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002172 printf("Warning: bad start-of-data in "
2173 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002174 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2175 last[i]);
2176 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002177 for (j = 0; j < i; j++) {
2178 if ((first[i] >= first[j] && first[i] <= last[j])
2179 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2180 printf("Warning: partition %d overlaps "
2181 "partition %d\n", j + 1, i + 1);
2182 total += first[i] >= first[j] ?
2183 first[i] : first[j];
2184 total -= last[i] <= last[j] ?
2185 last[i] : last[j];
2186 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002187 }
2188 }
2189 }
2190
2191 if (extended_offset) {
2192 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002193 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002194 get_nr_sects(pex->part_table) - 1;
2195
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002196 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002197 total++;
2198 p = ptes[i].part_table;
2199 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002200 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002201 printf("Warning: partition %d "
2202 "is empty\n", i + 1);
2203 } else if (first[i] < extended_offset || last[i] > e_last) {
2204 printf("Logical partition %d not entirely in "
2205 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002206 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 }
2208 }
2209
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002210 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002211 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002212 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002213 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002214 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002215 if (total != 0)
2216 printf("%d unallocated sectors\n", total);
2217 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002218}
2219
2220static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002221add_partition(int n, int sys)
2222{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002223 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002224 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 struct partition *p = ptes[n].part_table;
2226 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002227 ullong limit, temp;
2228 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002229 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002230
2231 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002232 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002233 return;
2234 }
2235 fill_bounds(first, last);
2236 if (n < 4) {
2237 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002238 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002239 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002240 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002241 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 if (extended_offset) {
2243 first[ext_index] = extended_offset;
2244 last[ext_index] = get_start_sect(q) +
2245 get_nr_sects(q) - 1;
2246 }
2247 } else {
2248 start = extended_offset + sector_offset;
2249 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2250 }
2251 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002252 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 first[i] = (cround(first[i]) - 1) * units_per_sector;
2254
Denis Vlasenkobd852072007-03-19 14:43:38 +00002255 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002256 do {
2257 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002258 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002259 int lastplusoff;
2260
2261 if (start == ptes[i].offset)
2262 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002263 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002264 if (start >= first[i] && start <= lastplusoff)
2265 start = lastplusoff + 1;
2266 }
2267 if (start > limit)
2268 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002269 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002270 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002271 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002272 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002273 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002274 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002275 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002276
2277 saved_start = start;
2278 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2279 0, mesg);
2280 if (display_in_cyl_units) {
2281 start = (start - 1) * units_per_sector;
2282 if (start < saved_start) start = saved_start;
2283 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002284 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002285 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002286 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 if (n > 4) { /* NOT for fifth partition */
2288 struct pte *pe = &ptes[n];
2289
2290 pe->offset = start - sector_offset;
2291 if (pe->offset == extended_offset) { /* must be corrected */
2292 pe->offset++;
2293 if (sector_offset == 1)
2294 start++;
2295 }
2296 }
2297
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002298 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 struct pte *pe = &ptes[i];
2300
2301 if (start < pe->offset && limit >= pe->offset)
2302 limit = pe->offset - 1;
2303 if (start < first[i] && limit >= first[i])
2304 limit = first[i] - 1;
2305 }
2306 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002307 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002308 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002309 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002310 return;
2311 }
2312 if (cround(start) == cround(limit)) {
2313 stop = limit;
2314 } else {
2315 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002316 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002317 str_units(SINGULAR));
2318 stop = read_int(cround(start), cround(limit), cround(limit),
2319 cround(start), mesg);
2320 if (display_in_cyl_units) {
2321 stop = stop * units_per_sector - 1;
2322 if (stop >limit)
2323 stop = limit;
2324 }
2325 }
2326
2327 set_partition(n, 0, start, stop, sys);
2328 if (n > 4)
2329 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2330
Rob Landleyb73451d2006-02-24 16:29:00 +00002331 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002332 struct pte *pe4 = &ptes[4];
2333 struct pte *pen = &ptes[n];
2334
2335 ext_index = n;
2336 pen->ext_pointer = p;
2337 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002338 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002339 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2340 pe4->ext_pointer = pe4->part_table + 1;
2341 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002342 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002343 }
2344}
2345
2346static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002347add_logical(void)
2348{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002349 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2350 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002351
Rob Landley081e3842006-08-03 20:07:35 +00002352 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002353 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2354 pe->ext_pointer = pe->part_table + 1;
2355 pe->offset = 0;
2356 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002357 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002359 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002360}
2361
2362static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002363new_partition(void)
2364{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 int i, free_primary = 0;
2366
2367 if (warn_geometry())
2368 return;
2369
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002370 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002371 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 return;
2373 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002374 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002375 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 return;
2377 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002378 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002379 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2380"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2381"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002382 return;
2383 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002385 for (i = 0; i < 4; i++)
2386 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002387
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002388 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002389 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002390 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002391 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002392
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002393 if (!free_primary) {
2394 if (extended_offset)
2395 add_logical();
2396 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002397 printf("You must delete some partition and add "
2398 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002399 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002400 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002401 snprintf(line, sizeof(line),
2402 "Command action\n"
2403 " %s\n"
2404 " p primary partition (1-4)\n",
2405 (extended_offset ?
2406 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002407 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002408 c = read_nonempty(line);
2409 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 i = get_nonexisting_partition(0, 4);
2411 if (i >= 0)
2412 add_partition(i, LINUX_NATIVE);
2413 return;
2414 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002415 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002416 add_logical();
2417 return;
2418 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002419 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 i = get_nonexisting_partition(0, 4);
2421 if (i >= 0)
2422 add_partition(i, EXTENDED);
2423 return;
2424 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002425 printf("Invalid partition number "
2426 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002427 }
2428 }
2429}
2430
2431static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002432write_table(void)
2433{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002434 int i;
2435
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002436 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002437 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002438 if (ptes[i].changed)
2439 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002440 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002441 struct pte *pe = &ptes[i];
2442
2443 if (pe->changed) {
2444 write_part_table_flag(pe->sectorbuffer);
2445 write_sector(pe->offset, pe->sectorbuffer);
2446 }
2447 }
2448 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002449 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002450 /* no test on change? the printf below might be mistaken */
2451 sgi_write_table();
2452 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002453 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002454 int needw = 0;
2455
Rob Landleyb73451d2006-02-24 16:29:00 +00002456 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 if (ptes[i].changed)
2458 needw = 1;
2459 if (needw)
2460 sun_write_table();
2461 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462
Denis Vlasenkobd852072007-03-19 14:43:38 +00002463 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464 reread_partition_table(1);
2465}
2466
Rob Landleyb73451d2006-02-24 16:29:00 +00002467static void
2468reread_partition_table(int leave)
2469{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002470 int i;
2471
Denis Vlasenkobd852072007-03-19 14:43:38 +00002472 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002474 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002475 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002476 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002477 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002478#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002479 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002480 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002481 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002483 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002484#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485
2486 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002487 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002488 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002489 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002490 }
2491}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002492#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002493
Denis Vlasenko834410a2006-11-29 12:00:28 +00002494#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002495#define MAX_PER_LINE 16
2496static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002497print_buffer(char *pbuffer)
2498{
2499 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002500
2501 for (i = 0, l = 0; i < sector_size; i++, l++) {
2502 if (l == 0)
2503 printf("0x%03X:", i);
2504 printf(" %02X", (unsigned char) pbuffer[i]);
2505 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002506 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002507 l = -1;
2508 }
2509 }
2510 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002511 bb_putchar('\n');
2512 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002513}
2514
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002515static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002516print_raw(void)
2517{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518 int i;
2519
Denis Vlasenkobd852072007-03-19 14:43:38 +00002520 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002521 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002522 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002523 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002524 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002526 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527}
2528
2529static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002530move_begin(int i)
2531{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002532 struct pte *pe = &ptes[i];
2533 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002534 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002535
2536 if (warn_geometry())
2537 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002538 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002539 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002540 return;
2541 }
2542 first = get_partition_start(pe);
2543 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002544 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002545
2546 if (new != get_nr_sects(p)) {
2547 first = get_nr_sects(p) + get_start_sect(p) - new;
2548 set_nr_sects(p, first);
2549 set_start_sect(p, new);
2550 pe->changed = 1;
2551 }
2552}
2553
2554static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002555xselect(void)
2556{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557 char c;
2558
Rob Landleyb73451d2006-02-24 16:29:00 +00002559 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002560 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002561 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 switch (c) {
2563 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002564 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002566 break;
2567 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002568 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002569 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 break;
2571 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002572 user_cylinders = g_cylinders =
2573 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002574 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002575 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002576 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002577 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578 warn_cylinders();
2579 break;
2580 case 'd':
2581 print_raw();
2582 break;
2583 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002584 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002586 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002588 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 x_list_table(1);
2590 break;
2591 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002592 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593 fix_partition_table_order();
2594 break;
2595 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002596#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 create_sgilabel();
2598#endif
2599 break;
2600 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002601 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002602 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002603 update_units();
2604 break;
2605 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002606 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002607 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 break;
2609 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002610 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002612 break;
2613 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002614 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 list_table(1);
2616 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 x_list_table(0);
2618 break;
2619 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002620 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002621 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002622 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 exit(0);
2624 case 'r':
2625 return;
2626 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002627 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002628 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002629 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002630 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002631 printf("Warning: setting sector offset for DOS "
2632 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 }
2634 update_units();
2635 break;
2636 case 'v':
2637 verify();
2638 break;
2639 case 'w':
2640 write_table(); /* does not return */
2641 break;
2642 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002643 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 break;
2646 default:
2647 xmenu();
2648 }
2649 }
2650}
2651#endif /* ADVANCED mode */
2652
2653static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002654is_ide_cdrom_or_tape(const char *device)
2655{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002656 FILE *procf;
2657 char buf[100];
2658 struct stat statbuf;
2659 int is_ide = 0;
2660
2661 /* No device was given explicitly, and we are trying some
2662 likely things. But opening /dev/hdc may produce errors like
2663 "hdc: tray open or drive not ready"
2664 if it happens to be a CD-ROM drive. It even happens that
2665 the process hangs on the attempt to read a music CD.
2666 So try to be careful. This only works since 2.1.73. */
2667
2668 if (strncmp("/dev/hd", device, 7))
2669 return 0;
2670
2671 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2672 procf = fopen(buf, "r");
2673 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2674 is_ide = (!strncmp(buf, "cdrom", 5) ||
2675 !strncmp(buf, "tape", 4));
2676 else
2677 /* Now when this proc file does not exist, skip the
2678 device when it is read-only. */
2679 if (stat(device, &statbuf) == 0)
2680 is_ide = ((statbuf.st_mode & 0222) == 0);
2681
2682 if (procf)
2683 fclose(procf);
2684 return is_ide;
2685}
2686
Rob Landley5527b912006-02-25 03:46:10 +00002687
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002689open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002690{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002691 int gb;
2692
2693 disk_device = device;
2694 if (setjmp(listingbuf))
2695 return;
2696 if (!user_specified)
2697 if (is_ide_cdrom_or_tape(device))
2698 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002699
2700 /* Open disk_device, save file descriptor to dev_fd */
2701 errno = 0;
2702 gb = get_boot(TRY_ONLY);
2703 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002704 /* Ignore other errors, since we try IDE
2705 and SCSI hard disks which may not be
2706 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002707 if (user_specified || errno == EACCES)
2708 bb_perror_msg("can't open '%s'", device);
2709 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002710 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002711
2712 if (gb < 0) { /* no DOS signature */
2713 list_disk_geometry();
2714 if (LABEL_IS_AIX)
2715 goto ret;
2716#if ENABLE_FEATURE_OSF_LABEL
2717 if (bsd_trydev(device) < 0)
2718#endif
2719 printf("Disk %s doesn't contain a valid "
2720 "partition table\n", device);
2721 } else {
2722 list_table(0);
2723#if ENABLE_FEATURE_FDISK_WRITABLE
2724 if (!LABEL_IS_SUN && g_partitions > 4) {
2725 delete_partition(ext_index);
2726 }
2727#endif
2728 }
2729 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002730 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002731}
2732
2733/* for fdisk -l: try all things in /proc/partitions
2734 that look like a partition name (do not end in a digit) */
2735static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002736list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002737{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 FILE *procpt;
2739 char line[100], ptname[100], devname[120], *s;
2740 int ma, mi, sz;
2741
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002742 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743
2744 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002745 if (sscanf(line, " %d %d %d %[^\n ]",
2746 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002747 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002748 for (s = ptname; *s; s++)
2749 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 if (isdigit(s[-1]))
2751 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002752 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002753 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002754 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002755#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002756 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002757#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758}
2759
Denis Vlasenko834410a2006-11-29 12:00:28 +00002760#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002762unknown_command(int c)
2763{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002764 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002765}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002766#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002767
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002768int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002769int fdisk_main(int argc, char **argv)
2770{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002771 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002772 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002773 * fdisk -v
2774 * fdisk -l [-b sectorsize] [-u] device ...
2775 * fdisk -s [partition] ...
2776 * fdisk [-b sectorsize] [-u] device
2777 *
2778 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002779 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002780 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002781
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002782 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002783
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002784 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002785 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002786 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002787 argc -= optind;
2788 argv += optind;
2789 if (opt & OPT_b) { // -b
2790 /* Ugly: this sector size is really per device,
2791 so cannot be combined with multiple disks,
2792 and the same goes for the C/H/S options.
2793 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002794 if (sector_size != 512 && sector_size != 1024
2795 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002796 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002797 sector_offset = 2;
2798 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002799 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002800 if (user_heads <= 0 || user_heads >= 256)
2801 user_heads = 0;
2802 if (user_sectors <= 0 || user_sectors >= 64)
2803 user_sectors = 0;
2804 if (opt & OPT_u)
2805 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806
Denis Vlasenko834410a2006-11-29 12:00:28 +00002807#if ENABLE_FEATURE_FDISK_WRITABLE
2808 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002810#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002811 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002812 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002813 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002814 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002815 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002816 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002817 /* we don't have device names, */
2818 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002819 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002820 }
2821 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002822#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002823 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002824#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825
Denis Vlasenko834410a2006-11-29 12:00:28 +00002826#if ENABLE_FEATURE_FDISK_BLKSIZE
2827 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002828 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002829
2830 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002831 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002832 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002833 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002834 unsigned long long size;
2835 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002836 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002838 if (argc == 1)
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002839 printf("%lld\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002840 else
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002841 printf("%s: %lld\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002842 }
2843 return 0;
2844 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002845#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846
Denis Vlasenko834410a2006-11-29 12:00:28 +00002847#if ENABLE_FEATURE_FDISK_WRITABLE
2848 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002849 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850
Denis Vlasenko834410a2006-11-29 12:00:28 +00002851 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002852 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002854 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002856 printf("Detected an OSF/1 disklabel on %s, entering "
2857 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002858 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002859 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002860 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861 /* If we return we may want to make an empty DOS label? */
2862 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002863
2864 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002865 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002866 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002867 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 switch (c) {
2869 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002870 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002871 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002872 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002873 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002875 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002877 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878 else
2879 unknown_command(c);
2880 break;
2881 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002882 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002883 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002884 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002885 if (read_maybe_empty("Please enter the name of the "
2886 "new boot file: ") == '\n')
2887 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 else
2889 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002890 }
2891#if ENABLE_FEATURE_OSF_LABEL
2892 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002893 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894#endif
2895 break;
2896 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002897 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002899 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002900 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002901 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002902 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002903 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002904 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002905 else
2906 unknown_command(c);
2907 break;
2908 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002909 {
Eric Andersen040f4402003-07-30 08:40:37 +00002910 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002911 /* If sgi_label then don't use get_existing_partition,
2912 let the user select a partition, since
2913 get_existing_partition() only works for Linux-like
2914 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002915 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002916 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002917 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002918 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002919 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002920 if (j >= 0)
2921 delete_partition(j);
2922 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923 break;
2924 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002925 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926 create_sgiinfo();
2927 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002928 unknown_command(c);
2929 case 'l':
2930 list_types(get_sys_types());
2931 break;
2932 case 'm':
2933 menu();
2934 break;
2935 case 'n':
2936 new_partition();
2937 break;
2938 case 'o':
2939 create_doslabel();
2940 break;
2941 case 'p':
2942 list_table(0);
2943 break;
2944 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002945 if (ENABLE_FEATURE_CLEAN_UP)
2946 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002947 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002948 return 0;
2949 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002950#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951 create_sunlabel();
2952#endif
2953 break;
2954 case 't':
2955 change_sysid();
2956 break;
2957 case 'u':
2958 change_units();
2959 break;
2960 case 'v':
2961 verify();
2962 break;
2963 case 'w':
2964 write_table(); /* does not return */
2965 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002966#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002967 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002968 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002969 printf("\n\tSorry, no experts menu for SGI "
2970 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002971 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002972 xselect();
2973 break;
2974#endif
2975 default:
2976 unknown_command(c);
2977 menu();
2978 }
2979 }
2980 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002981#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982}