blob: 8271f60f263dfd43e0017731640e820bfe94fd12 [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 Vlasenko3f22b7f2007-06-02 12:46:55 +000039/* Used for sector numbers. Today's disk sizes make it necessary */
40typedef unsigned long long ullong;
41
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000043 unsigned char heads;
44 unsigned char sectors;
45 unsigned short cylinders;
46 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000047};
48
Denis Vlasenko98ae2162006-10-12 19:30:44 +000049#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000050
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000051static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000052"Building a new %s. Changes will remain in memory only,\n"
53"until you decide to write them. After that the previous content\n"
54"won't be recoverable.\n\n";
55
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000056static const char msg_part_already_defined[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000057"Partition %d is already defined, delete it before re-adding\n";
58
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000059
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000060struct partition {
61 unsigned char boot_ind; /* 0x80 - active */
62 unsigned char head; /* starting head */
63 unsigned char sector; /* starting sector */
64 unsigned char cyl; /* starting cylinder */
65 unsigned char sys_ind; /* What partition type */
66 unsigned char end_head; /* end head */
67 unsigned char end_sector; /* end sector */
68 unsigned char end_cyl; /* end cylinder */
69 unsigned char start4[4]; /* starting sector counting from 0 */
70 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000071} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000073static const char unable_to_open[] ALIGN1 = "cannot open %s";
74static const char unable_to_read[] ALIGN1 = "cannot read from %s";
75static const char unable_to_seek[] ALIGN1 = "cannot seek on %s";
76static const char unable_to_write[] ALIGN1 = "cannot write to %s";
77static const char ioctl_error[] ALIGN1 = "BLKGETSIZE ioctl failed on %s";
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000078static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079
Denis Vlasenko98ae2162006-10-12 19:30:44 +000080enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +000081 label_dos, label_sun, label_sgi, label_aix, label_osf
82};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000083
Denis Vlasenko98ae2162006-10-12 19:30:44 +000084#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000085
Denis Vlasenko834410a2006-11-29 12:00:28 +000086#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000087#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000088#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +000089#else
90#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000091#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +000092#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000093
Denis Vlasenko834410a2006-11-29 12:00:28 +000094#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000095#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000096#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +000097#else
98#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000099#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000100#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000101
Denis Vlasenko834410a2006-11-29 12:00:28 +0000102#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000103#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000104#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000105#else
106#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000107#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000108#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000109
Denis Vlasenko834410a2006-11-29 12:00:28 +0000110#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000111#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000113#else
114#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000115#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#endif
Rob Landley5527b912006-02-25 03:46:10 +0000117
Rob Landleyb73451d2006-02-24 16:29:00 +0000118enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000119
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000120static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000121#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000122static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000123static void reread_partition_table(int leave);
124static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000125static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000126static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000127static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000128#endif
129static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000130static void get_geometry(void);
131static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000132
133#define PLURAL 0
134#define SINGULAR 1
135
Denis Vlasenko28703012006-12-19 20:32:02 +0000136static unsigned get_start_sect(const struct partition *p);
137static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000138
139/*
140 * per partition table entry data
141 *
142 * The four primary partitions have the same sectorbuffer (MBRbuffer)
143 * and have NULL ext_pointer.
144 * Each logical partition table entry has two pointers, one for the
145 * partition and one link to the next one.
146 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000147struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000148 struct partition *part_table; /* points into sectorbuffer */
149 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000150 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000151 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000152#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000153 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000154#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000155};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000156
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000157/* DOS partition types */
158
159static const char *const i386_sys_types[] = {
160 "\x00" "Empty",
161 "\x01" "FAT12",
162 "\x04" "FAT16 <32M",
163 "\x05" "Extended", /* DOS 3.3+ extended partition */
164 "\x06" "FAT16", /* DOS 16-bit >=32M */
165 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
166 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
167 "\x0b" "Win95 FAT32",
168 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
169 "\x0e" "Win95 FAT16 (LBA)",
170 "\x0f" "Win95 Ext'd (LBA)",
171 "\x11" "Hidden FAT12",
172 "\x12" "Compaq diagnostics",
173 "\x14" "Hidden FAT16 <32M",
174 "\x16" "Hidden FAT16",
175 "\x17" "Hidden HPFS/NTFS",
176 "\x1b" "Hidden Win95 FAT32",
177 "\x1c" "Hidden W95 FAT32 (LBA)",
178 "\x1e" "Hidden W95 FAT16 (LBA)",
179 "\x3c" "Part.Magic recovery",
180 "\x41" "PPC PReP Boot",
181 "\x42" "SFS",
182 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
183 "\x80" "Old Minix", /* Minix 1.4a and earlier */
184 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
185 "\x82" "Linux swap", /* also Solaris */
186 "\x83" "Linux",
187 "\x84" "OS/2 hidden C: drive",
188 "\x85" "Linux extended",
189 "\x86" "NTFS volume set",
190 "\x87" "NTFS volume set",
191 "\x8e" "Linux LVM",
192 "\x9f" "BSD/OS", /* BSDI */
193 "\xa0" "Thinkpad hibernation",
194 "\xa5" "FreeBSD", /* various BSD flavours */
195 "\xa6" "OpenBSD",
196 "\xa8" "Darwin UFS",
197 "\xa9" "NetBSD",
198 "\xab" "Darwin boot",
199 "\xb7" "BSDI fs",
200 "\xb8" "BSDI swap",
201 "\xbe" "Solaris boot",
202 "\xeb" "BeOS fs",
203 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
204 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
205 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
206 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
207 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
208 autodetect using persistent
209 superblock */
210#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
211 "\x02" "XENIX root",
212 "\x03" "XENIX usr",
213 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
214 "\x09" "AIX bootable", /* AIX data or Coherent */
215 "\x10" "OPUS",
216 "\x18" "AST SmartSleep",
217 "\x24" "NEC DOS",
218 "\x39" "Plan 9",
219 "\x40" "Venix 80286",
220 "\x4d" "QNX4.x",
221 "\x4e" "QNX4.x 2nd part",
222 "\x4f" "QNX4.x 3rd part",
223 "\x50" "OnTrack DM",
224 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
225 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
226 "\x53" "OnTrack DM6 Aux3",
227 "\x54" "OnTrackDM6",
228 "\x55" "EZ-Drive",
229 "\x56" "Golden Bow",
230 "\x5c" "Priam Edisk",
231 "\x61" "SpeedStor",
232 "\x64" "Novell Netware 286",
233 "\x65" "Novell Netware 386",
234 "\x70" "DiskSecure Multi-Boot",
235 "\x75" "PC/IX",
236 "\x93" "Amoeba",
237 "\x94" "Amoeba BBT", /* (bad block table) */
238 "\xa7" "NeXTSTEP",
239 "\xbb" "Boot Wizard hidden",
240 "\xc1" "DRDOS/sec (FAT-12)",
241 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
242 "\xc6" "DRDOS/sec (FAT-16)",
243 "\xc7" "Syrinx",
244 "\xda" "Non-FS data",
245 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
246 Concurrent DOS or CTOS */
247 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
248 "\xdf" "BootIt", /* BootIt EMBRM */
249 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
250 extended partition */
251 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
252 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
253 partition < 1024 cyl. */
254 "\xf1" "SpeedStor",
255 "\xf4" "SpeedStor", /* SpeedStor large partition */
256 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
257 "\xff" "BBT", /* Xenix Bad Block Table */
258#endif
259 NULL
260};
261
262
263/* Globals */
264
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000265struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000266 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000267
268 const char *disk_device;
269 int fd; /* the disk */
270 int g_partitions; // = 4; /* maximum partition + 1 */
271 unsigned units_per_sector; // = 1;
272 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
273 unsigned user_set_sector_size;
274 unsigned sector_offset; // = 1;
275 unsigned g_heads, g_sectors, g_cylinders;
276 enum label_type current_label_type;
277 smallint display_in_cyl_units; // = 1;
278#if ENABLE_FEATURE_OSF_LABEL
279 smallint possibly_osf_label;
280#endif
281
282 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000283 char line_buffer[80];
284 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000285 /* Raw disk label. For DOS-type partition tables the MBR,
286 * with descriptions of the primary partitions. */
287 char MBRbuffer[MAX_SECTOR_SIZE];
288 /* Partition tables */
289 struct pte ptes[MAXIMUM_PARTS];
290};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000291#define G (*ptr_to_globals)
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000292#define line_ptr (G.line_ptr)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000293#define disk_device (G.disk_device )
294#define fd (G.fd )
295#define g_partitions (G.g_partitions )
296#define units_per_sector (G.units_per_sector )
297#define sector_size (G.sector_size )
298#define user_set_sector_size (G.user_set_sector_size)
299#define sector_offset (G.sector_offset )
300#define g_heads (G.g_heads )
301#define g_sectors (G.g_sectors )
302#define g_cylinders (G.g_cylinders )
303#define current_label_type (G.current_label_type )
304#define display_in_cyl_units (G.display_in_cyl_units)
305#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000306#define listingbuf (G.listingbuf)
307#define line_buffer (G.line_buffer)
308#define partname_buffer (G.partname_buffer)
309#define MBRbuffer (G.MBRbuffer)
310#define ptes (G.ptes)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000311#define INIT_G() do { \
312 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
313 sector_size = DEFAULT_SECTOR_SIZE; \
314 sector_offset = 1; \
315 g_partitions = 4; \
316 display_in_cyl_units = 1; \
317 units_per_sector = 1; \
318} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000319
Denis Vlasenkobd852072007-03-19 14:43:38 +0000320
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000321#define IS_EXTENDED(i) \
322 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
323
324#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
325
326#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
327
328#define pt_offset(b, n) \
329 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
330
331#define sector(s) ((s) & 0x3f)
332
333#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
334
335#define hsc2sector(h,s,c) \
336 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
337
338#define set_hsc(h,s,c,sector) \
339 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000340 s = sector % g_sectors + 1; \
341 sector /= g_sectors; \
342 h = sector % g_heads; \
343 sector /= g_heads; \
344 c = sector & 0xff; \
345 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000346 } while (0)
347
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000348#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000349/* read line; return 0 or first printable char */
350static int
351read_line(const char *prompt)
352{
353 int sz;
354
355 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
356 if (sz <= 0)
357 exit(0); /* Ctrl-D or Ctrl-C */
358
359 if (line_buffer[sz-1] == '\n')
360 line_buffer[--sz] = '\0';
361
362 line_ptr = line_buffer;
363 while (*line_ptr && !isgraph(*line_ptr))
364 line_ptr++;
365 return *line_ptr;
366}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000367#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000368
Denis Vlasenkobd852072007-03-19 14:43:38 +0000369/*
370 * return partition name - uses static storage
371 */
372static const char *
373partname(const char *dev, int pno, int lth)
374{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000375 const char *p;
376 int w, wp;
377 int bufsiz;
378 char *bufp;
379
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000380 bufp = partname_buffer;
381 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000382
383 w = strlen(dev);
384 p = "";
385
386 if (isdigit(dev[w-1]))
387 p = "p";
388
389 /* devfs kludge - note: fdisk partition names are not supposed
390 to equal kernel names, so there is no reason to do this */
391 if (strcmp(dev + w - 4, "disc") == 0) {
392 w -= 4;
393 p = "part";
394 }
395
396 wp = strlen(p);
397
398 if (lth) {
399 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
400 lth-wp-2, w, dev, p, pno);
401 } else {
402 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
403 }
404 return bufp;
405}
406
Denis Vlasenko834410a2006-11-29 12:00:28 +0000407#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000408static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000409set_all_unchanged(void)
410{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000411 int i;
412
413 for (i = 0; i < MAXIMUM_PARTS; i++)
414 ptes[i].changed = 0;
415}
416
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000417static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000418set_changed(int i)
419{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000420 ptes[i].changed = 1;
421}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000422#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000423
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000424static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000425get_part_table(int i)
426{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000427 return ptes[i].part_table;
428}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000429
430static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000431str_units(int n)
432{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000433 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000434 return display_in_cyl_units ? "cylinder" : "sector";
435 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000436}
437
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000438static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000439valid_part_table_flag(const char *mbuffer)
440{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000441 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000442}
443
Denis Vlasenko834410a2006-11-29 12:00:28 +0000444#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000445static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000446write_part_table_flag(char *b)
447{
448 b[510] = 0x55;
449 b[511] = 0xaa;
450}
451
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000452static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000453read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000454{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000455 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000456 return *line_ptr;
457}
458
459static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000460read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000461{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000462 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000463 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000464 line_ptr[0] = '\n';
465 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000466 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000467 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000468}
469
470static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000471read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000472{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000473 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000474 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000475 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000476 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000477 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000478 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000479 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000480 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000481 if (v > 0xff)
482 /* Bad input also triggers this */
483 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000484 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000485 }
486}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000487#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000488
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000489#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000490
491typedef struct {
492 unsigned char info[128]; /* Informative text string */
493 unsigned char spare0[14];
494 struct sun_info {
495 unsigned char spare1;
496 unsigned char id;
497 unsigned char spare2;
498 unsigned char flags;
499 } infos[8];
500 unsigned char spare1[246]; /* Boot information etc. */
501 unsigned short rspeed; /* Disk rotational speed */
502 unsigned short pcylcount; /* Physical cylinder count */
503 unsigned short sparecyl; /* extra sects per cylinder */
504 unsigned char spare2[4]; /* More magic... */
505 unsigned short ilfact; /* Interleave factor */
506 unsigned short ncyl; /* Data cylinder count */
507 unsigned short nacyl; /* Alt. cylinder count */
508 unsigned short ntrks; /* Tracks per cylinder */
509 unsigned short nsect; /* Sectors per track */
510 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000511 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000512 uint32_t start_cylinder;
513 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000514 } partitions[8];
515 unsigned short magic; /* Magic number */
516 unsigned short csum; /* Label xor'd checksum */
517} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000518#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000519STATIC_OSF void bsd_select(void);
520STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000521#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000522
Denis Vlasenko28703012006-12-19 20:32:02 +0000523#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000524static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000525fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000526{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000527 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000528}
529
Rob Landley88621d72006-08-29 19:41:06 +0000530static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000531fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000532{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000533 return (x << 24) |
534 ((x & 0xFF00) << 8) |
535 ((x & 0xFF0000) >> 8) |
536 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000537}
538#endif
539
Denis Vlasenkobd852072007-03-19 14:43:38 +0000540STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000541STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000542STATIC_SGI int sgi_get_sysid(int i);
543STATIC_SGI void sgi_delete_partition(int i);
544STATIC_SGI void sgi_change_sysid(int i, int sys);
545STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000546#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000547STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000548#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000549STATIC_SGI int verify_sgi(int verbose);
550STATIC_SGI void sgi_add_partition(int n, int sys);
551STATIC_SGI void sgi_set_swappartition(int i);
552STATIC_SGI const char *sgi_get_bootfile(void);
553STATIC_SGI void sgi_set_bootfile(const char* aFile);
554STATIC_SGI void create_sgiinfo(void);
555STATIC_SGI void sgi_write_table(void);
556STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000557#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558
Denis Vlasenkobd852072007-03-19 14:43:38 +0000559STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000560STATIC_SUN void sun_delete_partition(int i);
561STATIC_SUN void sun_change_sysid(int i, int sys);
562STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000563STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000564#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000565STATIC_SUN void sun_set_alt_cyl(void);
566STATIC_SUN void sun_set_ncyl(int cyl);
567STATIC_SUN void sun_set_xcyl(void);
568STATIC_SUN void sun_set_ilfact(void);
569STATIC_SUN void sun_set_rspeed(void);
570STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000571#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000572STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
573STATIC_SUN void verify_sun(void);
574STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000575#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000576
Denis Vlasenko834410a2006-11-29 12:00:28 +0000577#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000578/* start_sect and nr_sects are stored little endian on all machines */
579/* moreover, they are not aligned correctly */
580static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000581store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000582{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000583 cp[0] = val;
584 cp[1] = val >> 8;
585 cp[2] = val >> 16;
586 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000587}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000588#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589
Denis Vlasenko834410a2006-11-29 12:00:28 +0000590static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000591read4_little_endian(const unsigned char *cp)
592{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000593 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000594}
595
Denis Vlasenko834410a2006-11-29 12:00:28 +0000596#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000598set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000599{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000600 store4_little_endian(p->start4, start_sect);
601}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000602#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603
Denis Vlasenko28703012006-12-19 20:32:02 +0000604static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000605get_start_sect(const struct partition *p)
606{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000607 return read4_little_endian(p->start4);
608}
609
Denis Vlasenko834410a2006-11-29 12:00:28 +0000610#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000611static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000612set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000613{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000614 store4_little_endian(p->size4, nr_sects);
615}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000616#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000617
Denis Vlasenko28703012006-12-19 20:32:02 +0000618static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000619get_nr_sects(const struct partition *p)
620{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000621 return read4_little_endian(p->size4);
622}
623
624/* normally O_RDWR, -l option gives O_RDONLY */
625static int type_open = O_RDWR;
626
Rob Landleyb73451d2006-02-24 16:29:00 +0000627static int ext_index; /* the prime extended partition */
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000628static smallint listing; /* no aborts for fdisk -l */
Rob Landleyb73451d2006-02-24 16:29:00 +0000629static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000630#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000631//static int dos_changed;
632static smallint nowarn; /* no warnings for fdisk -l/-s */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000633#endif
634
Denis Vlasenko834410a2006-11-29 12:00:28 +0000635static unsigned user_cylinders, user_heads, user_sectors;
636static unsigned pt_heads, pt_sectors;
637static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000638
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000639static ullong extended_offset; /* offset of link pointers */
640static ullong total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000641
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000642static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000643{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000644 if (listing) {
645 close(fd);
646 longjmp(listingbuf, 1);
647 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000648 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000649}
650
651static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000652seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000653{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000654 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000655#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000656 if (lseek64(fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000658#else
659 if (secno > MAXINT(off_t)
660 || lseek(fd, (off_t)secno, SEEK_SET) == (off_t) -1
661 ) {
662 fdisk_fatal(unable_to_seek);
663 }
664#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000665}
666
Denis Vlasenko834410a2006-11-29 12:00:28 +0000667#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000668static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000669write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000670{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000671 seek_sector(secno);
672 if (write(fd, buf, sector_size) != sector_size)
673 fdisk_fatal(unable_to_write);
674}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000675#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000676
677/* Allocate a buffer and read a partition table sector */
678static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000679read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000682 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000683 seek_sector(offset);
684 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
685 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000686#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000688#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000689 pe->part_table = pe->ext_pointer = NULL;
690}
691
Denis Vlasenko834410a2006-11-29 12:00:28 +0000692static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000693get_partition_start(const struct pte *pe)
694{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695 return pe->offset + get_start_sect(pe->part_table);
696}
697
Denis Vlasenko834410a2006-11-29 12:00:28 +0000698#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000699/*
700 * Avoid warning about DOS partitions when no DOS partition was changed.
701 * Here a heuristic "is probably dos partition".
702 * We might also do the opposite and warn in all cases except
703 * for "is probably nondos partition".
704 */
705static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000706is_dos_partition(int t)
707{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000708 return (t == 1 || t == 4 || t == 6 ||
709 t == 0x0b || t == 0x0c || t == 0x0e ||
710 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
711 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
712 t == 0xc1 || t == 0xc4 || t == 0xc6);
713}
714
715static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000716menu(void)
717{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000718 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000719 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000720 puts("a\ttoggle a read only flag"); /* sun */
721 puts("b\tedit bsd disklabel");
722 puts("c\ttoggle the mountable flag"); /* sun */
723 puts("d\tdelete a partition");
724 puts("l\tlist known partition types");
725 puts("n\tadd a new partition");
726 puts("o\tcreate a new empty DOS partition table");
727 puts("p\tprint the partition table");
728 puts("q\tquit without saving changes");
729 puts("s\tcreate a new empty Sun disklabel"); /* sun */
730 puts("t\tchange a partition's system id");
731 puts("u\tchange display/entry units");
732 puts("v\tverify the partition table");
733 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000734#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000735 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000736#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000737 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000738 puts("a\tselect bootable partition"); /* sgi flavour */
739 puts("b\tedit bootfile entry"); /* sgi */
740 puts("c\tselect sgi swap partition"); /* sgi flavour */
741 puts("d\tdelete a partition");
742 puts("l\tlist known partition types");
743 puts("n\tadd a new partition");
744 puts("o\tcreate a new empty DOS partition table");
745 puts("p\tprint the partition table");
746 puts("q\tquit without saving changes");
747 puts("s\tcreate a new empty Sun disklabel"); /* sun */
748 puts("t\tchange a partition's system id");
749 puts("u\tchange display/entry units");
750 puts("v\tverify the partition table");
751 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000752 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000753 puts("o\tcreate a new empty DOS partition table");
754 puts("q\tquit without saving changes");
755 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000756 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000757 puts("a\ttoggle a bootable flag");
758 puts("b\tedit bsd disklabel");
759 puts("c\ttoggle the dos compatibility flag");
760 puts("d\tdelete a partition");
761 puts("l\tlist known partition types");
762 puts("n\tadd a new partition");
763 puts("o\tcreate a new empty DOS partition table");
764 puts("p\tprint the partition table");
765 puts("q\tquit without saving changes");
766 puts("s\tcreate a new empty Sun disklabel"); /* sun */
767 puts("t\tchange a partition's system id");
768 puts("u\tchange display/entry units");
769 puts("v\tverify the partition table");
770 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000771#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000772 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000773#endif
774 }
775}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000776#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000777
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778
Denis Vlasenko834410a2006-11-29 12:00:28 +0000779#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000780static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000781xmenu(void)
782{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000783 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000784 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000785 puts("a\tchange number of alternate cylinders"); /*sun*/
786 puts("c\tchange number of cylinders");
787 puts("d\tprint the raw data in the partition table");
788 puts("e\tchange number of extra sectors per cylinder");/*sun*/
789 puts("h\tchange number of heads");
790 puts("i\tchange interleave factor"); /*sun*/
791 puts("o\tchange rotation speed (rpm)"); /*sun*/
792 puts("p\tprint the partition table");
793 puts("q\tquit without saving changes");
794 puts("r\treturn to main menu");
795 puts("s\tchange number of sectors/track");
796 puts("v\tverify the partition table");
797 puts("w\twrite table to disk and exit");
798 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000799 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000800 puts("b\tmove beginning of data in a partition"); /* !sun */
801 puts("c\tchange number of cylinders");
802 puts("d\tprint the raw data in the partition table");
803 puts("e\tlist extended partitions"); /* !sun */
804 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
805 puts("h\tchange number of heads");
806 puts("p\tprint the partition table");
807 puts("q\tquit without saving changes");
808 puts("r\treturn to main menu");
809 puts("s\tchange number of sectors/track");
810 puts("v\tverify the partition table");
811 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000812 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000813 puts("b\tmove beginning of data in a partition"); /* !sun */
814 puts("c\tchange number of cylinders");
815 puts("d\tprint the raw data in the partition table");
816 puts("e\tlist extended partitions"); /* !sun */
817 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
818 puts("h\tchange number of heads");
819 puts("p\tprint the partition table");
820 puts("q\tquit without saving changes");
821 puts("r\treturn to main menu");
822 puts("s\tchange number of sectors/track");
823 puts("v\tverify the partition table");
824 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000825 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000826 puts("b\tmove beginning of data in a partition"); /* !sun */
827 puts("c\tchange number of cylinders");
828 puts("d\tprint the raw data in the partition table");
829 puts("e\tlist extended partitions"); /* !sun */
830 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000831#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000833#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000834 puts("h\tchange number of heads");
835 puts("p\tprint the partition table");
836 puts("q\tquit without saving changes");
837 puts("r\treturn to main menu");
838 puts("s\tchange number of sectors/track");
839 puts("v\tverify the partition table");
840 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000841 }
842}
843#endif /* ADVANCED mode */
844
Denis Vlasenko834410a2006-11-29 12:00:28 +0000845#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000846static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000847get_sys_types(void)
848{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000849 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000850 LABEL_IS_SUN ? sun_sys_types :
851 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000852 i386_sys_types);
853}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000854#else
855#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000856#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000857
Denis Vlasenkobd852072007-03-19 14:43:38 +0000858static const char *
859partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860{
861 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000862 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000863
Denis Vlasenkobd852072007-03-19 14:43:38 +0000864 for (i = 0; types[i]; i++)
865 if ((unsigned char)types[i][0] == type)
866 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000867
Denis Vlasenkobd852072007-03-19 14:43:38 +0000868 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000869}
870
871
Denis Vlasenko834410a2006-11-29 12:00:28 +0000872#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000873static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000874get_sysid(int i)
875{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000876 return LABEL_IS_SUN ? sunlabel->infos[i].id :
877 (LABEL_IS_SGI ? sgi_get_sysid(i) :
878 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000879}
880
Denis Vlasenkobd852072007-03-19 14:43:38 +0000881static void
882list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884 enum { COLS = 3 };
885
886 unsigned last[COLS];
887 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000888 int i;
889
Denis Vlasenkobd852072007-03-19 14:43:38 +0000890 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000891
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000892 done = 0;
893 for (i = COLS-1; i >= 0; i--) {
894 done += (size + i - done) / (i + 1);
895 last[COLS-1 - i] = done;
896 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000897
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000898 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000899 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000900 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000901 (unsigned char)sys[next][0],
902 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000903 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000904 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905 i = 0;
906 next = ++done;
907 }
908 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000909 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000910}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000911#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000912
913static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000914is_cleared_partition(const struct partition *p)
915{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000916 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
917 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
918 get_start_sect(p) || get_nr_sects(p));
919}
920
921static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000922clear_partition(struct partition *p)
923{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924 if (!p)
925 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000926 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000927}
928
Denis Vlasenko834410a2006-11-29 12:00:28 +0000929#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000930static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000931set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000932{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000934 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935
936 if (doext) {
937 p = ptes[i].ext_pointer;
938 offset = extended_offset;
939 } else {
940 p = ptes[i].part_table;
941 offset = ptes[i].offset;
942 }
943 p->boot_ind = 0;
944 p->sys_ind = sysid;
945 set_start_sect(p, start - offset);
946 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000947 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
948 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000950 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
951 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000952 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
953 ptes[i].changed = 1;
954}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000955#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956
957static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000958warn_geometry(void)
959{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000960 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000961 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000962
Denis Vlasenkobd852072007-03-19 14:43:38 +0000963 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000964 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000965 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000966 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000967 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000968 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000969 printf(" cylinders");
970 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000971#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000972 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000973#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000974 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975 return 1;
976}
977
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000978static void
979update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000981 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000982
983 if (display_in_cyl_units && cyl_units)
984 units_per_sector = cyl_units;
985 else
986 units_per_sector = 1; /* in sectors */
987}
988
Denis Vlasenko834410a2006-11-29 12:00:28 +0000989#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000991warn_cylinders(void)
992{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000993 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000994 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000995"The number of cylinders for this disk is set to %d.\n"
996"There is nothing wrong with that, but this is larger than 1024,\n"
997"and could in certain setups cause problems with:\n"
998"1) software that runs at boot time (e.g., old versions of LILO)\n"
999"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001000" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001001 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001003#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001004
1005static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001006read_extended(int ext)
1007{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001008 int i;
1009 struct pte *pex;
1010 struct partition *p, *q;
1011
1012 ext_index = ext;
1013 pex = &ptes[ext];
1014 pex->ext_pointer = pex->part_table;
1015
1016 p = pex->part_table;
1017 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001018 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019 return;
1020 }
1021
Rob Landleyb73451d2006-02-24 16:29:00 +00001022 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001023 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001024
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001025 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026 /* This is not a Linux restriction, but
1027 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001028 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001029 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001030#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001031 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001032 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001033 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001034#endif
1035 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001036 return;
1037 }
1038
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001039 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001040
1041 if (!extended_offset)
1042 extended_offset = get_start_sect(p);
1043
1044 q = p = pt_offset(pe->sectorbuffer, 0);
1045 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001046 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001049 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001050 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 else
1052 pe->ext_pointer = p;
1053 } else if (p->sys_ind) {
1054 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001057 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058 else
1059 pe->part_table = p;
1060 }
1061 }
1062
1063 /* very strange code here... */
1064 if (!pe->part_table) {
1065 if (q != pe->ext_pointer)
1066 pe->part_table = q;
1067 else
1068 pe->part_table = q + 1;
1069 }
1070 if (!pe->ext_pointer) {
1071 if (q != pe->part_table)
1072 pe->ext_pointer = q;
1073 else
1074 pe->ext_pointer = q + 1;
1075 }
1076
1077 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001078 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079 }
1080
Denis Vlasenko834410a2006-11-29 12:00:28 +00001081#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001082 /* remove empty links */
1083 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001084 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001085 struct pte *pe = &ptes[i];
1086
Denis Vlasenkobd852072007-03-19 14:43:38 +00001087 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001088 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001089 ) {
1090 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001091 delete_partition(i);
1092 goto remove; /* numbering changed */
1093 }
1094 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001095#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001096}
1097
Denis Vlasenko834410a2006-11-29 12:00:28 +00001098#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001099static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001100create_doslabel(void)
1101{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001102 int i;
1103
Denis Vlasenkobd852072007-03-19 14:43:38 +00001104 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001105
1106 current_label_type = label_dos;
1107
Denis Vlasenko834410a2006-11-29 12:00:28 +00001108#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109 possibly_osf_label = 0;
1110#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001111 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001112
1113 for (i = 510-64; i < 510; i++)
1114 MBRbuffer[i] = 0;
1115 write_part_table_flag(MBRbuffer);
1116 extended_offset = 0;
1117 set_all_unchanged();
1118 set_changed(0);
1119 get_boot(create_empty_dos);
1120}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001121#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122
1123static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001124get_sectorsize(void)
1125{
Rob Landley736e5252006-02-25 03:36:00 +00001126 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001127 int arg;
1128 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1129 sector_size = arg;
1130 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001131 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001132 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001133 }
1134}
1135
Rob Landley88621d72006-08-29 19:41:06 +00001136static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001137get_kernel_geometry(void)
1138{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001139 struct hd_geometry geometry;
1140
1141 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1142 kern_heads = geometry.heads;
1143 kern_sectors = geometry.sectors;
1144 /* never use geometry.cylinders - it is truncated */
1145 }
1146}
1147
1148static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001149get_partition_table_geometry(void)
1150{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001151 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001152 struct partition *p;
1153 int i, h, s, hh, ss;
1154 int first = 1;
1155 int bad = 0;
1156
Eric Andersen3496fdc2006-01-30 23:09:20 +00001157 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001158 return;
1159
1160 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001161 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001162 p = pt_offset(bufp, i);
1163 if (p->sys_ind != 0) {
1164 h = p->end_head + 1;
1165 s = (p->end_sector & 077);
1166 if (first) {
1167 hh = h;
1168 ss = s;
1169 first = 0;
1170 } else if (hh != h || ss != s)
1171 bad = 1;
1172 }
1173 }
1174
1175 if (!first && !bad) {
1176 pt_heads = hh;
1177 pt_sectors = ss;
1178 }
1179}
1180
Rob Landleyb73451d2006-02-24 16:29:00 +00001181static void
1182get_geometry(void)
1183{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184 int sec_fac;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001185 uint64_t v64;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001186
1187 get_sectorsize();
1188 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001189#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001190 guess_device_type();
1191#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001192 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001193 kern_heads = kern_sectors = 0;
1194 pt_heads = pt_sectors = 0;
1195
1196 get_kernel_geometry();
1197 get_partition_table_geometry();
1198
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001199 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001200 pt_heads ? pt_heads :
1201 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001202 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001203 pt_sectors ? pt_sectors :
1204 kern_sectors ? kern_sectors : 63;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001205 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
1206 /* got bytes, convert to 512 byte sectors */
1207 total_number_of_sectors = (v64 >> 9);
Eric Andersen040f4402003-07-30 08:40:37 +00001208 } else {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001209 unsigned long longsectors; /* need temp of type long */
1210 if (ioctl(fd, BLKGETSIZE, &longsectors))
1211 longsectors = 0;
1212 total_number_of_sectors = longsectors;
Eric Andersen040f4402003-07-30 08:40:37 +00001213 }
1214
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215 sector_offset = 1;
1216 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001217 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001218
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001219 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1220 if (!g_cylinders)
1221 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222}
1223
1224/*
1225 * Read MBR. Returns:
1226 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1227 * 0: found or created label
1228 * 1: I/O error
1229 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001230static int
1231get_boot(enum action what)
1232{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001233 int i;
1234
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001235 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236
1237 for (i = 0; i < 4; i++) {
1238 struct pte *pe = &ptes[i];
1239
1240 pe->part_table = pt_offset(MBRbuffer, i);
1241 pe->ext_pointer = NULL;
1242 pe->offset = 0;
1243 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001244#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001246#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001247 }
1248
Denis Vlasenko834410a2006-11-29 12:00:28 +00001249#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250 if (what == create_empty_sun && check_sun_label())
1251 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001252#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253
1254 memset(MBRbuffer, 0, 512);
1255
Denis Vlasenko834410a2006-11-29 12:00:28 +00001256#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 if (what == create_empty_dos)
1258 goto got_dos_table; /* skip reading disk */
1259
Denis Vlasenkobd852072007-03-19 14:43:38 +00001260 fd = open(disk_device, type_open);
1261 if (fd < 0) {
1262 fd = open(disk_device, O_RDONLY);
1263 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001264 if (what == try_only)
1265 return 1;
1266 fdisk_fatal(unable_to_open);
1267 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001268 printf("You will not be able to write "
1269 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001270 }
1271
1272 if (512 != read(fd, MBRbuffer, 512)) {
1273 if (what == try_only)
1274 return 1;
1275 fdisk_fatal(unable_to_read);
1276 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001277#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001278 fd = open(disk_device, O_RDONLY);
1279 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001280 return 1;
1281 if (512 != read(fd, MBRbuffer, 512))
1282 return 1;
1283#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001284
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001285 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286
1287 update_units();
1288
Denis Vlasenko834410a2006-11-29 12:00:28 +00001289#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 if (check_sun_label())
1291 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001292#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293
Denis Vlasenko834410a2006-11-29 12:00:28 +00001294#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 if (check_sgi_label())
1296 return 0;
1297#endif
1298
Denis Vlasenko834410a2006-11-29 12:00:28 +00001299#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 if (check_aix_label())
1301 return 0;
1302#endif
1303
Denis Vlasenko834410a2006-11-29 12:00:28 +00001304#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001305 if (check_osf_label()) {
1306 possibly_osf_label = 1;
1307 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001308 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309 return 0;
1310 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001311 printf("This disk has both DOS and BSD magic.\n"
1312 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 }
1314#endif
1315
Denis Vlasenko834410a2006-11-29 12:00:28 +00001316#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001317 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001318#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319
1320 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001321#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001322 return -1;
1323#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001324 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001326 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001328 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001330#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001331 create_sunlabel();
1332#endif
1333#else
1334 create_doslabel();
1335#endif
1336 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 case try_only:
1338 return -1;
1339 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001340#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001342#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 break;
1344 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001345 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001347#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348 }
1349
Denis Vlasenko834410a2006-11-29 12:00:28 +00001350#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001352#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 warn_geometry();
1354
1355 for (i = 0; i < 4; i++) {
1356 struct pte *pe = &ptes[i];
1357
Rob Landleyb73451d2006-02-24 16:29:00 +00001358 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001359 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001360 printf("Ignoring extra extended "
1361 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 else
1363 read_extended(i);
1364 }
1365 }
1366
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001367 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001368 struct pte *pe = &ptes[i];
1369
1370 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001371 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1372 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001373 pe->sectorbuffer[510],
1374 pe->sectorbuffer[511],
1375 i + 1);
1376#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001377 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001378#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 }
1380 }
1381
1382 return 0;
1383}
1384
Denis Vlasenko834410a2006-11-29 12:00:28 +00001385#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386/*
1387 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1388 * If the user hits Enter, DFLT is returned.
1389 * Answers like +10 are interpreted as offsets from BASE.
1390 *
1391 * There is no default if DFLT is not between LOW and HIGH.
1392 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001393static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001394read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001396 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001398 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001400 if (dflt < low || dflt > high) {
1401 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001403 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404
1405 while (1) {
1406 int use_default = default_ok;
1407
1408 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001409 do {
1410 printf(fmt, mesg, low, high, dflt);
1411 read_maybe_empty("");
1412 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1413 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414
Eric Andersen84bdea82004-05-19 10:49:17 +00001415 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001416 int minus = (*line_ptr == '-');
1417 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001418
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001419 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001420
Rob Landleyb73451d2006-02-24 16:29:00 +00001421 while (isdigit(*++line_ptr))
1422 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001423
Rob Landleyb73451d2006-02-24 16:29:00 +00001424 switch (*line_ptr) {
1425 case 'c':
1426 case 'C':
1427 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001428 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001429 break;
1430 case 'K':
1431 absolute = 1024;
1432 break;
1433 case 'k':
1434 absolute = 1000;
1435 break;
1436 case 'm':
1437 case 'M':
1438 absolute = 1000000;
1439 break;
1440 case 'g':
1441 case 'G':
1442 absolute = 1000000000;
1443 break;
1444 default:
1445 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001446 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001447 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001448 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001449 unsigned long unit;
1450
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001451 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001452 unit = sector_size * units_per_sector;
1453 bytes += unit/2; /* round */
1454 bytes /= unit;
1455 i = bytes;
1456 }
1457 if (minus)
1458 i = -i;
1459 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001460 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 i = atoi(line_ptr);
1462 while (isdigit(*line_ptr)) {
1463 line_ptr++;
1464 use_default = 0;
1465 }
1466 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001467 if (use_default) {
1468 i = dflt;
1469 printf("Using default value %u\n", i);
1470 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471 if (i >= low && i <= high)
1472 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001473 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 }
1475 return i;
1476}
1477
Rob Landleyb73451d2006-02-24 16:29:00 +00001478static int
1479get_partition(int warn, int max)
1480{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481 struct pte *pe;
1482 int i;
1483
Denis Vlasenkobd852072007-03-19 14:43:38 +00001484 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001485 pe = &ptes[i];
1486
1487 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001488 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1489 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1490 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1491 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001492 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001493 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001494 }
1495 return i;
1496}
1497
1498static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001499get_existing_partition(int warn, int max)
1500{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501 int pno = -1;
1502 int i;
1503
1504 for (i = 0; i < max; i++) {
1505 struct pte *pe = &ptes[i];
1506 struct partition *p = pe->part_table;
1507
1508 if (p && !is_cleared_partition(p)) {
1509 if (pno >= 0)
1510 goto not_unique;
1511 pno = i;
1512 }
1513 }
1514 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001515 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001516 return pno;
1517 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001518 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001519 return -1;
1520
1521 not_unique:
1522 return get_partition(warn, max);
1523}
1524
1525static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001526get_nonexisting_partition(int warn, int max)
1527{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001528 int pno = -1;
1529 int i;
1530
1531 for (i = 0; i < max; i++) {
1532 struct pte *pe = &ptes[i];
1533 struct partition *p = pe->part_table;
1534
1535 if (p && is_cleared_partition(p)) {
1536 if (pno >= 0)
1537 goto not_unique;
1538 pno = i;
1539 }
1540 }
1541 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001542 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001543 return pno;
1544 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001545 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001546 return -1;
1547
1548 not_unique:
1549 return get_partition(warn, max);
1550}
1551
1552
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001553static void
1554change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555{
1556 display_in_cyl_units = !display_in_cyl_units;
1557 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001558 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001559 str_units(PLURAL));
1560}
1561
1562static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001563toggle_active(int i)
1564{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001565 struct pte *pe = &ptes[i];
1566 struct partition *p = pe->part_table;
1567
Rob Landleyb73451d2006-02-24 16:29:00 +00001568 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001569 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1571 pe->changed = 1;
1572}
1573
1574static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001575toggle_dos_compatibility_flag(void)
1576{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 dos_compatible_flag = ~dos_compatible_flag;
1578 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001579 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001580 printf("DOS Compatibility flag is set\n");
1581 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001582 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001583 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001584 }
1585}
1586
1587static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001588delete_partition(int i)
1589{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001590 struct pte *pe = &ptes[i];
1591 struct partition *p = pe->part_table;
1592 struct partition *q = pe->ext_pointer;
1593
1594/* Note that for the fifth partition (i == 4) we don't actually
1595 * decrement partitions.
1596 */
1597
1598 if (warn_geometry())
1599 return; /* C/H/S not set */
1600 pe->changed = 1;
1601
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001602 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603 sun_delete_partition(i);
1604 return;
1605 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001606 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001607 sgi_delete_partition(i);
1608 return;
1609 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001610
1611 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001612 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001613 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001614 ptes[ext_index].ext_pointer = NULL;
1615 extended_offset = 0;
1616 }
1617 clear_partition(p);
1618 return;
1619 }
1620
1621 if (!q->sys_ind && i > 4) {
1622 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001623 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001624 --i;
1625 clear_partition(ptes[i].ext_pointer);
1626 ptes[i].changed = 1;
1627 } else {
1628 /* not the last one - further ones will be moved down */
1629 if (i > 4) {
1630 /* delete this link in the chain */
1631 p = ptes[i-1].ext_pointer;
1632 *p = *q;
1633 set_start_sect(p, get_start_sect(q));
1634 set_nr_sects(p, get_nr_sects(q));
1635 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001636 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001637 /* the first logical in a longer chain */
1638 pe = &ptes[5];
1639
1640 if (pe->part_table) /* prevent SEGFAULT */
1641 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001642 get_partition_start(pe) -
1643 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001644 pe->offset = extended_offset;
1645 pe->changed = 1;
1646 }
1647
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001648 if (g_partitions > 5) {
1649 g_partitions--;
1650 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001651 ptes[i] = ptes[i+1];
1652 i++;
1653 }
1654 } else
1655 /* the only logical: clear only */
1656 clear_partition(ptes[i].part_table);
1657 }
1658}
1659
1660static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001661change_sysid(void)
1662{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 int i, sys, origsys;
1664 struct partition *p;
1665
Eric Andersen040f4402003-07-30 08:40:37 +00001666 /* If sgi_label then don't use get_existing_partition,
1667 let the user select a partition, since get_existing_partition()
1668 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001669 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001670 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001671 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001672 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001673 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001674 if (i == -1)
1675 return;
1676 p = ptes[i].part_table;
1677 origsys = sys = get_sysid(i);
1678
1679 /* if changing types T to 0 is allowed, then
1680 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001681 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001682 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001683 return;
1684 }
1685 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001686 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001688 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001689 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001690 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001691 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001692 /* break; */
1693 }
1694
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001695 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001696 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001697 printf("You cannot change a partition into"
1698 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001699 break;
1700 }
1701 }
1702
1703 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001704#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001705 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001706 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001707 "as Whole disk (5),\n"
1708 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001709 "even Linux likes it\n\n");
1710#endif
1711#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001712 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001713 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001714 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001715 (i == 8 && sys != 0)
1716 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001717 ) {
1718 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001719 "as volume header (0),\nand "
1720 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001721 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001722 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001723#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001724 if (sys == origsys)
1725 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001726 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001727 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001728 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 sgi_change_sysid(i, sys);
1730 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001732
Denis Vlasenkobd852072007-03-19 14:43:38 +00001733 printf("Changed system type of partition %d "
1734 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001735 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001736 ptes[i].changed = 1;
1737 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001738 is_dos_partition(sys))
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001739 //dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001740 break;
1741 }
1742 }
1743}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001744#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001745
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001746
Denis Vlasenko28703012006-12-19 20:32:02 +00001747/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1749 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1750 * Lubkin Oct. 1991). */
1751
Rob Landleyb73451d2006-02-24 16:29:00 +00001752static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001753linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001754{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001755 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756
1757 *c = ls / spc;
1758 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001759 *h = ls / g_sectors;
1760 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761}
1762
Rob Landleyb73451d2006-02-24 16:29:00 +00001763static void
1764check_consistency(const struct partition *p, int partition)
1765{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001766 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1767 unsigned pec, peh, pes; /* physical ending c, h, s */
1768 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1769 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001770
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001771 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001772 return; /* do not check extended partitions */
1773
1774/* physical beginning c, h, s */
1775 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1776 pbh = p->head;
1777 pbs = p->sector & 0x3f;
1778
1779/* physical ending c, h, s */
1780 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1781 peh = p->end_head;
1782 pes = p->end_sector & 0x3f;
1783
1784/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001785 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001786
1787/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001788 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001789
1790/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001791 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001792 printf("Partition %d has different physical/logical "
1793 "beginnings (non-Linux?):\n", partition + 1);
1794 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1795 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001796 }
1797
1798/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001799 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001800 printf("Partition %d has different physical/logical "
1801 "endings:\n", partition + 1);
1802 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1803 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804 }
1805
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001807 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001808 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001809 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001810 }
1811}
1812
1813static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001814list_disk_geometry(void)
1815{
Eric Andersen040f4402003-07-30 08:40:37 +00001816 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001817 long megabytes = bytes/1000000;
1818
1819 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001820 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001821 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001822 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001823 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001824 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001825 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001826 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001827 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001828 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001829 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001830 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001831 str_units(PLURAL),
1832 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833}
1834
1835/*
1836 * Check whether partition entries are ordered by their starting positions.
1837 * Return 0 if OK. Return i if partition i should have been earlier.
1838 * Two separate checks: primary and logical partitions.
1839 */
1840static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001841wrong_p_order(int *prev)
1842{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001843 const struct pte *pe;
1844 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001845 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001846 int i, last_i = 0;
1847
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001848 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001849 if (i == 4) {
1850 last_i = 4;
1851 last_p_start_pos = 0;
1852 }
1853 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001854 p = pe->part_table;
1855 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001856 p_start_pos = get_partition_start(pe);
1857
1858 if (last_p_start_pos > p_start_pos) {
1859 if (prev)
1860 *prev = last_i;
1861 return i;
1862 }
1863
1864 last_p_start_pos = p_start_pos;
1865 last_i = i;
1866 }
1867 }
1868 return 0;
1869}
1870
Denis Vlasenko834410a2006-11-29 12:00:28 +00001871#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001872/*
1873 * Fix the chain of logicals.
1874 * extended_offset is unchanged, the set of sectors used is unchanged
1875 * The chain is sorted so that sectors increase, and so that
1876 * starting sectors increase.
1877 *
1878 * After this it may still be that cfdisk doesnt like the table.
1879 * (This is because cfdisk considers expanded parts, from link to
1880 * end of partition, and these may still overlap.)
1881 * Now
1882 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1883 * may help.
1884 */
1885static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001886fix_chain_of_logicals(void)
1887{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001888 int j, oj, ojj, sj, sjj;
1889 struct partition *pj,*pjj,tmp;
1890
1891 /* Stage 1: sort sectors but leave sector of part 4 */
1892 /* (Its sector is the global extended_offset.) */
1893 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001894 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001895 oj = ptes[j].offset;
1896 ojj = ptes[j+1].offset;
1897 if (oj > ojj) {
1898 ptes[j].offset = ojj;
1899 ptes[j+1].offset = oj;
1900 pj = ptes[j].part_table;
1901 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1902 pjj = ptes[j+1].part_table;
1903 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1904 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001905 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001906 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001907 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001908 goto stage1;
1909 }
1910 }
1911
1912 /* Stage 2: sort starting sectors */
1913 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001914 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001915 pj = ptes[j].part_table;
1916 pjj = ptes[j+1].part_table;
1917 sj = get_start_sect(pj);
1918 sjj = get_start_sect(pjj);
1919 oj = ptes[j].offset;
1920 ojj = ptes[j+1].offset;
1921 if (oj+sj > ojj+sjj) {
1922 tmp = *pj;
1923 *pj = *pjj;
1924 *pjj = tmp;
1925 set_start_sect(pj, ojj+sjj-oj);
1926 set_start_sect(pjj, oj+sj-ojj);
1927 goto stage2;
1928 }
1929 }
1930
1931 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001932 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001933 ptes[j].changed = 1;
1934}
1935
1936
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001937static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001938fix_partition_table_order(void)
1939{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001940 struct pte *pei, *pek;
1941 int i,k;
1942
1943 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001944 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001945 return;
1946 }
1947
1948 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1949 /* partition i should have come earlier, move it */
1950 /* We have to move data in the MBR */
1951 struct partition *pi, *pk, *pe, pbuf;
1952 pei = &ptes[i];
1953 pek = &ptes[k];
1954
1955 pe = pei->ext_pointer;
1956 pei->ext_pointer = pek->ext_pointer;
1957 pek->ext_pointer = pe;
1958
1959 pi = pei->part_table;
1960 pk = pek->part_table;
1961
1962 memmove(&pbuf, pi, sizeof(struct partition));
1963 memmove(pi, pk, sizeof(struct partition));
1964 memmove(pk, &pbuf, sizeof(struct partition));
1965
1966 pei->changed = pek->changed = 1;
1967 }
1968
1969 if (i)
1970 fix_chain_of_logicals();
1971
1972 printf("Done.\n");
1973
1974}
1975#endif
1976
1977static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001978list_table(int xtra)
1979{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001980 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001981 int i, w;
1982
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001983 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001984 sun_list_table(xtra);
1985 return;
1986 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001987 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001988 sgi_list_table(xtra);
1989 return;
1990 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001991
1992 list_disk_geometry();
1993
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001994 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001995 xbsd_print_disklabel(xtra);
1996 return;
1997 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001998
1999 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2000 but if the device name ends in a digit, say /dev/foo1,
2001 then the partition is called /dev/foo1p3. */
2002 w = strlen(disk_device);
2003 if (w && isdigit(disk_device[w-1]))
2004 w++;
2005 if (w < 5)
2006 w = 5;
2007
Denis Vlasenkobd852072007-03-19 14:43:38 +00002008 // 1 12345678901 12345678901 12345678901 12
2009 printf("%*s Boot Start End Blocks Id System\n",
2010 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002012 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002014 ullong psects;
2015 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002016 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002017
2018 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002019 if (!p || is_cleared_partition(p))
2020 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002021
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002022 psects = get_nr_sects(p);
2023 pblocks = psects;
2024 podd = 0;
2025
2026 if (sector_size < 1024) {
2027 pblocks /= (1024 / sector_size);
2028 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002029 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002030 if (sector_size > 1024)
2031 pblocks *= (sector_size / 1024);
2032
2033 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2034 partname(disk_device, i+1, w+2),
2035 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2036 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002037 (ullong) cround(get_partition_start(pe)), /* start */
2038 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002039 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002040 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002041 p->sys_ind, /* type id */
2042 partition_type(p->sys_ind)); /* type name */
2043
2044 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 }
2046
2047 /* Is partition table in disk order? It need not be, but... */
2048 /* partition table entries are not checked for correct order if this
2049 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002050 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002051 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002052 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 }
2054}
2055
Denis Vlasenko834410a2006-11-29 12:00:28 +00002056#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002057static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002058x_list_table(int extend)
2059{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002060 const struct pte *pe;
2061 const struct partition *p;
2062 int i;
2063
Denis Vlasenkobd852072007-03-19 14:43:38 +00002064 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002065 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002066 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002067 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002068 pe = &ptes[i];
2069 p = (extend ? pe->ext_pointer : pe->part_table);
2070 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002071 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002072 i + 1, p->boot_ind, p->head,
2073 sector(p->sector),
2074 cylinder(p->sector, p->cyl), p->end_head,
2075 sector(p->end_sector),
2076 cylinder(p->end_sector, p->end_cyl),
2077 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2078 if (p->sys_ind)
2079 check_consistency(p, i);
2080 }
2081 }
2082}
2083#endif
2084
Denis Vlasenko834410a2006-11-29 12:00:28 +00002085#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002086static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002087fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002088{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002089 int i;
2090 const struct pte *pe = &ptes[0];
2091 const struct partition *p;
2092
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002093 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002095 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002096 first[i] = 0xffffffff;
2097 last[i] = 0;
2098 } else {
2099 first[i] = get_partition_start(pe);
2100 last[i] = first[i] + get_nr_sects(p) - 1;
2101 }
2102 }
2103}
2104
2105static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002106check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002107{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002108 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002109
2110 real_s = sector(s) - 1;
2111 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002112 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002113 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002114 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002115 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002116 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002117 n, h + 1, g_heads);
2118 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002119 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002120 "maximum %d\n", n, s, g_sectors);
2121 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002122 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002123 "maximum %d\n", n, real_c + 1, g_cylinders);
2124 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002125 printf("Partition %d: previous sectors %llu disagrees with "
2126 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002127}
2128
2129static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002130verify(void)
2131{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002132 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002133 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002134 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002135 struct partition *p;
2136
2137 if (warn_geometry())
2138 return;
2139
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002140 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002141 verify_sun();
2142 return;
2143 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002144 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002145 verify_sgi(1);
2146 return;
2147 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148
2149 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002150 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002151 struct pte *pe = &ptes[i];
2152
2153 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002154 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002155 check_consistency(p, i);
2156 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002157 printf("Warning: bad start-of-data in "
2158 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002159 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2160 last[i]);
2161 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002162 for (j = 0; j < i; j++) {
2163 if ((first[i] >= first[j] && first[i] <= last[j])
2164 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2165 printf("Warning: partition %d overlaps "
2166 "partition %d\n", j + 1, i + 1);
2167 total += first[i] >= first[j] ?
2168 first[i] : first[j];
2169 total -= last[i] <= last[j] ?
2170 last[i] : last[j];
2171 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002172 }
2173 }
2174 }
2175
2176 if (extended_offset) {
2177 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002178 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002179 get_nr_sects(pex->part_table) - 1;
2180
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002181 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002182 total++;
2183 p = ptes[i].part_table;
2184 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002185 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002186 printf("Warning: partition %d "
2187 "is empty\n", i + 1);
2188 } else if (first[i] < extended_offset || last[i] > e_last) {
2189 printf("Logical partition %d not entirely in "
2190 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002191 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002192 }
2193 }
2194
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002195 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002196 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002197 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002198 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002199 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002200 if (total != 0)
2201 printf("%d unallocated sectors\n", total);
2202 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203}
2204
2205static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002206add_partition(int n, int sys)
2207{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002209 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002210 struct partition *p = ptes[n].part_table;
2211 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002212 ullong limit, temp;
2213 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002214 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002215
2216 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002217 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002218 return;
2219 }
2220 fill_bounds(first, last);
2221 if (n < 4) {
2222 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002223 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002224 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002226 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 if (extended_offset) {
2228 first[ext_index] = extended_offset;
2229 last[ext_index] = get_start_sect(q) +
2230 get_nr_sects(q) - 1;
2231 }
2232 } else {
2233 start = extended_offset + sector_offset;
2234 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2235 }
2236 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002237 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002238 first[i] = (cround(first[i]) - 1) * units_per_sector;
2239
Denis Vlasenkobd852072007-03-19 14:43:38 +00002240 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002241 do {
2242 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002243 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002244 int lastplusoff;
2245
2246 if (start == ptes[i].offset)
2247 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002248 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002249 if (start >= first[i] && start <= lastplusoff)
2250 start = lastplusoff + 1;
2251 }
2252 if (start > limit)
2253 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002254 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002255 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002256 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002257 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002259 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002260 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002261
2262 saved_start = start;
2263 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2264 0, mesg);
2265 if (display_in_cyl_units) {
2266 start = (start - 1) * units_per_sector;
2267 if (start < saved_start) start = saved_start;
2268 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002269 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002270 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002271 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272 if (n > 4) { /* NOT for fifth partition */
2273 struct pte *pe = &ptes[n];
2274
2275 pe->offset = start - sector_offset;
2276 if (pe->offset == extended_offset) { /* must be corrected */
2277 pe->offset++;
2278 if (sector_offset == 1)
2279 start++;
2280 }
2281 }
2282
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002283 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284 struct pte *pe = &ptes[i];
2285
2286 if (start < pe->offset && limit >= pe->offset)
2287 limit = pe->offset - 1;
2288 if (start < first[i] && limit >= first[i])
2289 limit = first[i] - 1;
2290 }
2291 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002292 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002294 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295 return;
2296 }
2297 if (cround(start) == cround(limit)) {
2298 stop = limit;
2299 } else {
2300 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002301 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002302 str_units(SINGULAR));
2303 stop = read_int(cround(start), cround(limit), cround(limit),
2304 cround(start), mesg);
2305 if (display_in_cyl_units) {
2306 stop = stop * units_per_sector - 1;
2307 if (stop >limit)
2308 stop = limit;
2309 }
2310 }
2311
2312 set_partition(n, 0, start, stop, sys);
2313 if (n > 4)
2314 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2315
Rob Landleyb73451d2006-02-24 16:29:00 +00002316 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002317 struct pte *pe4 = &ptes[4];
2318 struct pte *pen = &ptes[n];
2319
2320 ext_index = n;
2321 pen->ext_pointer = p;
2322 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002323 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002324 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2325 pe4->ext_pointer = pe4->part_table + 1;
2326 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002327 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002328 }
2329}
2330
2331static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002332add_logical(void)
2333{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002334 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2335 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002336
Rob Landley081e3842006-08-03 20:07:35 +00002337 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002338 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2339 pe->ext_pointer = pe->part_table + 1;
2340 pe->offset = 0;
2341 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002342 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002343 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002344 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002345}
2346
2347static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002348new_partition(void)
2349{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002350 int i, free_primary = 0;
2351
2352 if (warn_geometry())
2353 return;
2354
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002355 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002356 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002357 return;
2358 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002359 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002360 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002361 return;
2362 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002363 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002364 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2365"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2366"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002367 return;
2368 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002370 for (i = 0; i < 4; i++)
2371 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002372
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002373 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002374 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002375 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002376 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002377
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378 if (!free_primary) {
2379 if (extended_offset)
2380 add_logical();
2381 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002382 printf("You must delete some partition and add "
2383 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002385 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002386 snprintf(line, sizeof(line),
2387 "Command action\n"
2388 " %s\n"
2389 " p primary partition (1-4)\n",
2390 (extended_offset ?
2391 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002392 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002393 c = read_nonempty(line);
2394 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 i = get_nonexisting_partition(0, 4);
2396 if (i >= 0)
2397 add_partition(i, LINUX_NATIVE);
2398 return;
2399 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002400 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 add_logical();
2402 return;
2403 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002404 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 i = get_nonexisting_partition(0, 4);
2406 if (i >= 0)
2407 add_partition(i, EXTENDED);
2408 return;
2409 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002410 printf("Invalid partition number "
2411 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412 }
2413 }
2414}
2415
2416static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002417write_table(void)
2418{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002419 int i;
2420
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002421 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002422 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002423 if (ptes[i].changed)
2424 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002425 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002426 struct pte *pe = &ptes[i];
2427
2428 if (pe->changed) {
2429 write_part_table_flag(pe->sectorbuffer);
2430 write_sector(pe->offset, pe->sectorbuffer);
2431 }
2432 }
2433 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002434 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002435 /* no test on change? the printf below might be mistaken */
2436 sgi_write_table();
2437 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002438 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 int needw = 0;
2440
Rob Landleyb73451d2006-02-24 16:29:00 +00002441 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002442 if (ptes[i].changed)
2443 needw = 1;
2444 if (needw)
2445 sun_write_table();
2446 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002447
Denis Vlasenkobd852072007-03-19 14:43:38 +00002448 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449 reread_partition_table(1);
2450}
2451
Rob Landleyb73451d2006-02-24 16:29:00 +00002452static void
2453reread_partition_table(int leave)
2454{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455 int i;
2456
Denis Vlasenkobd852072007-03-19 14:43:38 +00002457 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002458 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002459 /* sleep(2); Huh? */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002460 i = ioctl_or_perror(fd, BLKRRPART, NULL,
2461 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002462 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002463#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002465 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002466 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002467 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002468 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002469#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002470
2471 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002472 if (ENABLE_FEATURE_CLEAN_UP)
2473 close(fd);
2474 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 }
2476}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002477#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478
Denis Vlasenko834410a2006-11-29 12:00:28 +00002479#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002480#define MAX_PER_LINE 16
2481static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002482print_buffer(char *pbuffer)
2483{
2484 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485
2486 for (i = 0, l = 0; i < sector_size; i++, l++) {
2487 if (l == 0)
2488 printf("0x%03X:", i);
2489 printf(" %02X", (unsigned char) pbuffer[i]);
2490 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002491 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 l = -1;
2493 }
2494 }
2495 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002496 bb_putchar('\n');
2497 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498}
2499
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002500static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002501print_raw(void)
2502{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002503 int i;
2504
Denis Vlasenkobd852072007-03-19 14:43:38 +00002505 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002506 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002507 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002508 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002509 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002511 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512}
2513
2514static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002515move_begin(int i)
2516{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002517 struct pte *pe = &ptes[i];
2518 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002519 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520
2521 if (warn_geometry())
2522 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002523 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002524 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 return;
2526 }
2527 first = get_partition_start(pe);
2528 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002529 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530
2531 if (new != get_nr_sects(p)) {
2532 first = get_nr_sects(p) + get_start_sect(p) - new;
2533 set_nr_sects(p, first);
2534 set_start_sect(p, new);
2535 pe->changed = 1;
2536 }
2537}
2538
2539static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002540xselect(void)
2541{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542 char c;
2543
Rob Landleyb73451d2006-02-24 16:29:00 +00002544 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002545 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002546 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547 switch (c) {
2548 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002549 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002550 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551 break;
2552 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002553 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002554 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555 break;
2556 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002557 user_cylinders = g_cylinders =
2558 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002559 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002560 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002561 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002562 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 warn_cylinders();
2564 break;
2565 case 'd':
2566 print_raw();
2567 break;
2568 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002569 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002571 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002573 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 x_list_table(1);
2575 break;
2576 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002577 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578 fix_partition_table_order();
2579 break;
2580 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002581#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582 create_sgilabel();
2583#endif
2584 break;
2585 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002586 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002587 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 update_units();
2589 break;
2590 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002591 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593 break;
2594 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002595 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002596 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 break;
2598 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002599 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002600 list_table(1);
2601 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602 x_list_table(0);
2603 break;
2604 case 'q':
2605 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002606 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002607 exit(0);
2608 case 'r':
2609 return;
2610 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002611 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002612 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002614 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002615 printf("Warning: setting sector offset for DOS "
2616 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 }
2618 update_units();
2619 break;
2620 case 'v':
2621 verify();
2622 break;
2623 case 'w':
2624 write_table(); /* does not return */
2625 break;
2626 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002627 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002628 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002629 break;
2630 default:
2631 xmenu();
2632 }
2633 }
2634}
2635#endif /* ADVANCED mode */
2636
2637static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002638is_ide_cdrom_or_tape(const char *device)
2639{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002640 FILE *procf;
2641 char buf[100];
2642 struct stat statbuf;
2643 int is_ide = 0;
2644
2645 /* No device was given explicitly, and we are trying some
2646 likely things. But opening /dev/hdc may produce errors like
2647 "hdc: tray open or drive not ready"
2648 if it happens to be a CD-ROM drive. It even happens that
2649 the process hangs on the attempt to read a music CD.
2650 So try to be careful. This only works since 2.1.73. */
2651
2652 if (strncmp("/dev/hd", device, 7))
2653 return 0;
2654
2655 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2656 procf = fopen(buf, "r");
2657 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2658 is_ide = (!strncmp(buf, "cdrom", 5) ||
2659 !strncmp(buf, "tape", 4));
2660 else
2661 /* Now when this proc file does not exist, skip the
2662 device when it is read-only. */
2663 if (stat(device, &statbuf) == 0)
2664 is_ide = ((statbuf.st_mode & 0222) == 0);
2665
2666 if (procf)
2667 fclose(procf);
2668 return is_ide;
2669}
2670
Rob Landley5527b912006-02-25 03:46:10 +00002671
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002672static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002673trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002674{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002675 int gb;
2676
2677 disk_device = device;
2678 if (setjmp(listingbuf))
2679 return;
2680 if (!user_specified)
2681 if (is_ide_cdrom_or_tape(device))
2682 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002683 fd = open(disk_device, type_open);
2684 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 gb = get_boot(try_only);
2686 if (gb > 0) { /* I/O error */
2687 close(fd);
2688 } else if (gb < 0) { /* no DOS signature */
2689 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002690 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002691 return;
Rob Landley5527b912006-02-25 03:46:10 +00002692 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002693#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002694 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002695#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002696 printf("Disk %s doesn't contain a valid "
2697 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002698 close(fd);
2699 } else {
2700 close(fd);
2701 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002702#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002703 if (!LABEL_IS_SUN && g_partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002704 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002705 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002706#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707 }
2708 } else {
2709 /* Ignore other errors, since we try IDE
2710 and SCSI hard disks which may not be
2711 installed on the system. */
2712 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002713 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002714 return;
2715 }
2716 }
2717}
2718
2719/* for fdisk -l: try all things in /proc/partitions
2720 that look like a partition name (do not end in a digit) */
2721static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002722tryprocpt(void)
2723{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002724 FILE *procpt;
2725 char line[100], ptname[100], devname[120], *s;
2726 int ma, mi, sz;
2727
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002728 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002729
2730 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002731 if (sscanf(line, " %d %d %d %[^\n ]",
2732 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002733 continue;
2734 for (s = ptname; *s; s++);
2735 if (isdigit(s[-1]))
2736 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002737 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002738 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002739 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002740#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002741 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002742#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743}
2744
Denis Vlasenko834410a2006-11-29 12:00:28 +00002745#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002746static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002747unknown_command(int c)
2748{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002749 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002751#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002752
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002753int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002754int fdisk_main(int argc, char **argv)
2755{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002756 char *str_b, *str_C, *str_H, *str_S;
2757 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 * fdisk -v
2760 * fdisk -l [-b sectorsize] [-u] device ...
2761 * fdisk -s [partition] ...
2762 * fdisk [-b sectorsize] [-u] device
2763 *
2764 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002765 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002766 enum {
2767 OPT_b = 1 << 0,
2768 OPT_C = 1 << 1,
2769 OPT_H = 1 << 2,
2770 OPT_l = 1 << 3,
2771 OPT_S = 1 << 4,
2772 OPT_u = 1 << 5,
2773 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2774 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002775
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002776 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002777
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002778 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00002779 &str_b, &str_C, &str_H, &str_S);
2780 argc -= optind;
2781 argv += optind;
2782 if (opt & OPT_b) { // -b
2783 /* Ugly: this sector size is really per device,
2784 so cannot be combined with multiple disks,
2785 and the same goes for the C/H/S options.
2786 */
2787 sector_size = xatoi_u(str_b);
2788 if (sector_size != 512 && sector_size != 1024 &&
2789 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002790 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002791 sector_offset = 2;
2792 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002793 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002794 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2795 if (opt & OPT_H) { // -H
2796 user_heads = xatoi_u(str_H);
2797 if (user_heads <= 0 || user_heads >= 256)
2798 user_heads = 0;
2799 }
2800 //if (opt & OPT_l) // -l
2801 if (opt & OPT_S) { // -S
2802 user_sectors = xatoi_u(str_S);
2803 if (user_sectors <= 0 || user_sectors >= 64)
2804 user_sectors = 0;
2805 }
2806 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2807 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002808
Denis Vlasenko834410a2006-11-29 12:00:28 +00002809 if (user_set_sector_size && argc != 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002810 printf("Warning: the -b (set sector size) option should"
2811 " be used with one specified device\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002812
Denis Vlasenko834410a2006-11-29 12:00:28 +00002813#if ENABLE_FEATURE_FDISK_WRITABLE
2814 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002815 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002816#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002817 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002818 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002819 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002820#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002821 /* avoid gcc warning:
2822 variable `k' might be clobbered by `longjmp' */
2823 (void)&k;
2824#endif
2825 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002826 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002827 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 } else {
2829 /* we no longer have default device names */
2830 /* but, we can use /proc/partitions instead */
2831 tryprocpt();
2832 }
2833 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002834#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002836#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837
Denis Vlasenko834410a2006-11-29 12:00:28 +00002838#if ENABLE_FEATURE_FDISK_BLKSIZE
2839 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002840 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002841 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002842
2843 nowarn = 1;
2844 type_open = O_RDONLY;
2845
Denis Vlasenko834410a2006-11-29 12:00:28 +00002846 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002847 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002848
Denis Vlasenko834410a2006-11-29 12:00:28 +00002849 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002851 fd = open(disk_device, type_open);
2852 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 fdisk_fatal(unable_to_open);
2854 if (ioctl(fd, BLKGETSIZE, &size))
2855 fdisk_fatal(ioctl_error);
2856 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002857 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858 printf("%ld\n", size/2);
2859 else
2860 printf("%s: %ld\n", argv[j], size/2);
2861 }
2862 return 0;
2863 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002864#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865
Denis Vlasenko834410a2006-11-29 12:00:28 +00002866#if ENABLE_FEATURE_FDISK_WRITABLE
2867 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002868 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002869
Denis Vlasenko834410a2006-11-29 12:00:28 +00002870 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002871 get_boot(fdisk);
2872
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002873 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002875 printf("Detected an OSF/1 disklabel on %s, entering "
2876 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002877 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002878 /*Why do we do this? It seems to be counter-intuitive*/
2879 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 /* If we return we may want to make an empty DOS label? */
2881 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882
2883 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002884 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002885 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002886 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002887 switch (c) {
2888 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002889 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002890 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002891 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002892 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002894 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002896 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002897 else
2898 unknown_command(c);
2899 break;
2900 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002901 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002902 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002903 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002904 if (read_maybe_empty("Please enter the name of the "
2905 "new boot file: ") == '\n')
2906 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907 else
2908 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002909 }
2910#if ENABLE_FEATURE_OSF_LABEL
2911 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002912 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913#endif
2914 break;
2915 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002916 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002918 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002919 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002920 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002921 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002923 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 else
2925 unknown_command(c);
2926 break;
2927 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002928 {
Eric Andersen040f4402003-07-30 08:40:37 +00002929 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002930 /* If sgi_label then don't use get_existing_partition,
2931 let the user select a partition, since
2932 get_existing_partition() only works for Linux-like
2933 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002935 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002936 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002937 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002938 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002939 if (j >= 0)
2940 delete_partition(j);
2941 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942 break;
2943 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002945 create_sgiinfo();
2946 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947 unknown_command(c);
2948 case 'l':
2949 list_types(get_sys_types());
2950 break;
2951 case 'm':
2952 menu();
2953 break;
2954 case 'n':
2955 new_partition();
2956 break;
2957 case 'o':
2958 create_doslabel();
2959 break;
2960 case 'p':
2961 list_table(0);
2962 break;
2963 case 'q':
2964 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002965 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002966 return 0;
2967 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002968#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002969 create_sunlabel();
2970#endif
2971 break;
2972 case 't':
2973 change_sysid();
2974 break;
2975 case 'u':
2976 change_units();
2977 break;
2978 case 'v':
2979 verify();
2980 break;
2981 case 'w':
2982 write_table(); /* does not return */
2983 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002984#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002986 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002987 printf("\n\tSorry, no experts menu for SGI "
2988 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002989 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990 xselect();
2991 break;
2992#endif
2993 default:
2994 unknown_command(c);
2995 menu();
2996 }
2997 }
2998 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002999#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003000}