blob: a75b4f817031ceb01fb20663e48a2751a783288d [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 { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000312 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000313 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 Vlasenkocdf62772008-03-17 08:42:43 +0000321/* TODO: move to libbb? */
322static ullong bb_BLKGETSIZE_sectors(void)
323{
324 uint64_t v64;
325 unsigned long longsectors;
326
327 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
328 /* got bytes, convert to 512 byte sectors */
329 return (v64 >> 9);
330 }
331 /* Needs temp of type long */
332 if (ioctl(fd, BLKGETSIZE, &longsectors))
333 longsectors = 0;
334 return longsectors;
335}
336
337
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000338#define IS_EXTENDED(i) \
339 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
340
341#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
342
343#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
344
345#define pt_offset(b, n) \
346 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
347
348#define sector(s) ((s) & 0x3f)
349
350#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
351
352#define hsc2sector(h,s,c) \
353 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
354
355#define set_hsc(h,s,c,sector) \
356 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000357 s = sector % g_sectors + 1; \
358 sector /= g_sectors; \
359 h = sector % g_heads; \
360 sector /= g_heads; \
361 c = sector & 0xff; \
362 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000363 } while (0)
364
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000365#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000366/* read line; return 0 or first printable char */
367static int
368read_line(const char *prompt)
369{
370 int sz;
371
372 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
373 if (sz <= 0)
374 exit(0); /* Ctrl-D or Ctrl-C */
375
376 if (line_buffer[sz-1] == '\n')
377 line_buffer[--sz] = '\0';
378
379 line_ptr = line_buffer;
380 while (*line_ptr && !isgraph(*line_ptr))
381 line_ptr++;
382 return *line_ptr;
383}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000384#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000385
Denis Vlasenkobd852072007-03-19 14:43:38 +0000386/*
387 * return partition name - uses static storage
388 */
389static const char *
390partname(const char *dev, int pno, int lth)
391{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000392 const char *p;
393 int w, wp;
394 int bufsiz;
395 char *bufp;
396
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000397 bufp = partname_buffer;
398 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000399
400 w = strlen(dev);
401 p = "";
402
403 if (isdigit(dev[w-1]))
404 p = "p";
405
406 /* devfs kludge - note: fdisk partition names are not supposed
407 to equal kernel names, so there is no reason to do this */
408 if (strcmp(dev + w - 4, "disc") == 0) {
409 w -= 4;
410 p = "part";
411 }
412
413 wp = strlen(p);
414
415 if (lth) {
416 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
417 lth-wp-2, w, dev, p, pno);
418 } else {
419 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
420 }
421 return bufp;
422}
423
Denis Vlasenko834410a2006-11-29 12:00:28 +0000424#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000425static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000426set_all_unchanged(void)
427{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000428 int i;
429
430 for (i = 0; i < MAXIMUM_PARTS; i++)
431 ptes[i].changed = 0;
432}
433
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000434static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000435set_changed(int i)
436{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000437 ptes[i].changed = 1;
438}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000439#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000440
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000441static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000442get_part_table(int i)
443{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000444 return ptes[i].part_table;
445}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000446
447static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000448str_units(int n)
449{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000450 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000451 return display_in_cyl_units ? "cylinder" : "sector";
452 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000453}
454
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000455static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000456valid_part_table_flag(const char *mbuffer)
457{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000458 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000459}
460
Denis Vlasenko834410a2006-11-29 12:00:28 +0000461#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000462static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000463write_part_table_flag(char *b)
464{
465 b[510] = 0x55;
466 b[511] = 0xaa;
467}
468
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000469static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000470read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000471{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000472 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000473 return *line_ptr;
474}
475
476static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000477read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000478{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000479 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000480 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000481 line_ptr[0] = '\n';
482 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000483 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000484 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000485}
486
487static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000488read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000489{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000490 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000491 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000492 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000493 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000494 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000495 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000496 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000497 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000498 if (v > 0xff)
499 /* Bad input also triggers this */
500 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000501 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000502 }
503}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000504#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000505
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000506#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000507
508typedef struct {
509 unsigned char info[128]; /* Informative text string */
510 unsigned char spare0[14];
511 struct sun_info {
512 unsigned char spare1;
513 unsigned char id;
514 unsigned char spare2;
515 unsigned char flags;
516 } infos[8];
517 unsigned char spare1[246]; /* Boot information etc. */
518 unsigned short rspeed; /* Disk rotational speed */
519 unsigned short pcylcount; /* Physical cylinder count */
520 unsigned short sparecyl; /* extra sects per cylinder */
521 unsigned char spare2[4]; /* More magic... */
522 unsigned short ilfact; /* Interleave factor */
523 unsigned short ncyl; /* Data cylinder count */
524 unsigned short nacyl; /* Alt. cylinder count */
525 unsigned short ntrks; /* Tracks per cylinder */
526 unsigned short nsect; /* Sectors per track */
527 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000528 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000529 uint32_t start_cylinder;
530 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000531 } partitions[8];
532 unsigned short magic; /* Magic number */
533 unsigned short csum; /* Label xor'd checksum */
534} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000535#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000536STATIC_OSF void bsd_select(void);
537STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000538#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000539
Denis Vlasenko28703012006-12-19 20:32:02 +0000540#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000541static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000542fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000543{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000544 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000545}
546
Rob Landley88621d72006-08-29 19:41:06 +0000547static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000548fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000549{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000550 return (x << 24) |
551 ((x & 0xFF00) << 8) |
552 ((x & 0xFF0000) >> 8) |
553 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554}
555#endif
556
Denis Vlasenkobd852072007-03-19 14:43:38 +0000557STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000558STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000559STATIC_SGI int sgi_get_sysid(int i);
560STATIC_SGI void sgi_delete_partition(int i);
561STATIC_SGI void sgi_change_sysid(int i, int sys);
562STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000563#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000564STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000565#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000566STATIC_SGI int verify_sgi(int verbose);
567STATIC_SGI void sgi_add_partition(int n, int sys);
568STATIC_SGI void sgi_set_swappartition(int i);
569STATIC_SGI const char *sgi_get_bootfile(void);
570STATIC_SGI void sgi_set_bootfile(const char* aFile);
571STATIC_SGI void create_sgiinfo(void);
572STATIC_SGI void sgi_write_table(void);
573STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000574#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000575
Denis Vlasenkobd852072007-03-19 14:43:38 +0000576STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000577STATIC_SUN void sun_delete_partition(int i);
578STATIC_SUN void sun_change_sysid(int i, int sys);
579STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000580STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000581#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000582STATIC_SUN void sun_set_alt_cyl(void);
583STATIC_SUN void sun_set_ncyl(int cyl);
584STATIC_SUN void sun_set_xcyl(void);
585STATIC_SUN void sun_set_ilfact(void);
586STATIC_SUN void sun_set_rspeed(void);
587STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000588#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000589STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
590STATIC_SUN void verify_sun(void);
591STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000592#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000593
Denis Vlasenko834410a2006-11-29 12:00:28 +0000594#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595/* start_sect and nr_sects are stored little endian on all machines */
596/* moreover, they are not aligned correctly */
597static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000598store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000599{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000600 cp[0] = val;
601 cp[1] = val >> 8;
602 cp[2] = val >> 16;
603 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000604}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000605#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000606
Denis Vlasenko834410a2006-11-29 12:00:28 +0000607static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000608read4_little_endian(const unsigned char *cp)
609{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000610 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000611}
612
Denis Vlasenko834410a2006-11-29 12:00:28 +0000613#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000614static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000615set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000616{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000617 store4_little_endian(p->start4, start_sect);
618}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000619#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000620
Denis Vlasenko28703012006-12-19 20:32:02 +0000621static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000622get_start_sect(const struct partition *p)
623{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624 return read4_little_endian(p->start4);
625}
626
Denis Vlasenko834410a2006-11-29 12:00:28 +0000627#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000628static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000629set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000630{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000631 store4_little_endian(p->size4, nr_sects);
632}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000633#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000634
Denis Vlasenko28703012006-12-19 20:32:02 +0000635static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000636get_nr_sects(const struct partition *p)
637{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000638 return read4_little_endian(p->size4);
639}
640
641/* normally O_RDWR, -l option gives O_RDONLY */
642static int type_open = O_RDWR;
643
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000644static int ext_index; /* the prime extended partition */
645static smallint listing; /* no aborts for fdisk -l */
646static smallint dos_compatible_flag = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000647#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000648//static int dos_changed;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000649static smallint nowarn; /* no warnings for fdisk -l/-s */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000650#endif
651
Denis Vlasenko834410a2006-11-29 12:00:28 +0000652static unsigned user_cylinders, user_heads, user_sectors;
653static unsigned pt_heads, pt_sectors;
654static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000656static ullong extended_offset; /* offset of link pointers */
657static ullong total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000658
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000659static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000660{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000661 if (listing) {
662 close(fd);
663 longjmp(listingbuf, 1);
664 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000665 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000666}
667
668static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000669seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000670{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000671 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000672#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000673 if (lseek64(fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000675#else
676 if (secno > MAXINT(off_t)
677 || lseek(fd, (off_t)secno, SEEK_SET) == (off_t) -1
678 ) {
679 fdisk_fatal(unable_to_seek);
680 }
681#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000682}
683
Denis Vlasenko834410a2006-11-29 12:00:28 +0000684#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000686write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000687{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000688 seek_sector(secno);
689 if (write(fd, buf, sector_size) != sector_size)
690 fdisk_fatal(unable_to_write);
691}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000692#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000693
694/* Allocate a buffer and read a partition table sector */
695static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000696read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000697{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000698 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000699 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000700 seek_sector(offset);
701 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
702 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000703#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000704 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000705#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000706 pe->part_table = pe->ext_pointer = NULL;
707}
708
Denis Vlasenko834410a2006-11-29 12:00:28 +0000709static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000710get_partition_start(const struct pte *pe)
711{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000712 return pe->offset + get_start_sect(pe->part_table);
713}
714
Denis Vlasenko834410a2006-11-29 12:00:28 +0000715#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000716/*
717 * Avoid warning about DOS partitions when no DOS partition was changed.
718 * Here a heuristic "is probably dos partition".
719 * We might also do the opposite and warn in all cases except
720 * for "is probably nondos partition".
721 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000722#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000723static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000724is_dos_partition(int t)
725{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000726 return (t == 1 || t == 4 || t == 6 ||
727 t == 0x0b || t == 0x0c || t == 0x0e ||
728 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
729 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
730 t == 0xc1 || t == 0xc4 || t == 0xc6);
731}
Denis Vlasenko89398812008-01-25 20:18:46 +0000732#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000733
734static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000735menu(void)
736{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000737 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000738 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000739 puts("a\ttoggle a read only flag"); /* sun */
740 puts("b\tedit bsd disklabel");
741 puts("c\ttoggle the mountable flag"); /* sun */
742 puts("d\tdelete a partition");
743 puts("l\tlist known partition types");
744 puts("n\tadd a new partition");
745 puts("o\tcreate a new empty DOS partition table");
746 puts("p\tprint the partition table");
747 puts("q\tquit without saving changes");
748 puts("s\tcreate a new empty Sun disklabel"); /* sun */
749 puts("t\tchange a partition's system id");
750 puts("u\tchange display/entry units");
751 puts("v\tverify the partition table");
752 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000753#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000754 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000755#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000756 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000757 puts("a\tselect bootable partition"); /* sgi flavour */
758 puts("b\tedit bootfile entry"); /* sgi */
759 puts("c\tselect sgi swap partition"); /* sgi flavour */
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 Vlasenko8e1c7152007-01-22 07:21:38 +0000771 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000772 puts("o\tcreate a new empty DOS partition table");
773 puts("q\tquit without saving changes");
774 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000775 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000776 puts("a\ttoggle a bootable flag");
777 puts("b\tedit bsd disklabel");
778 puts("c\ttoggle the dos compatibility flag");
779 puts("d\tdelete a partition");
780 puts("l\tlist known partition types");
781 puts("n\tadd a new partition");
782 puts("o\tcreate a new empty DOS partition table");
783 puts("p\tprint the partition table");
784 puts("q\tquit without saving changes");
785 puts("s\tcreate a new empty Sun disklabel"); /* sun */
786 puts("t\tchange a partition's system id");
787 puts("u\tchange display/entry units");
788 puts("v\tverify the partition table");
789 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000790#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000791 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000792#endif
793 }
794}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000795#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000796
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000797
Denis Vlasenko834410a2006-11-29 12:00:28 +0000798#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000799static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000800xmenu(void)
801{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000802 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000803 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000804 puts("a\tchange number of alternate cylinders"); /*sun*/
805 puts("c\tchange number of cylinders");
806 puts("d\tprint the raw data in the partition table");
807 puts("e\tchange number of extra sectors per cylinder");/*sun*/
808 puts("h\tchange number of heads");
809 puts("i\tchange interleave factor"); /*sun*/
810 puts("o\tchange rotation speed (rpm)"); /*sun*/
811 puts("p\tprint the partition table");
812 puts("q\tquit without saving changes");
813 puts("r\treturn to main menu");
814 puts("s\tchange number of sectors/track");
815 puts("v\tverify the partition table");
816 puts("w\twrite table to disk and exit");
817 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000818 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000819 puts("b\tmove beginning of data in a partition"); /* !sun */
820 puts("c\tchange number of cylinders");
821 puts("d\tprint the raw data in the partition table");
822 puts("e\tlist extended partitions"); /* !sun */
823 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
824 puts("h\tchange number of heads");
825 puts("p\tprint the partition table");
826 puts("q\tquit without saving changes");
827 puts("r\treturn to main menu");
828 puts("s\tchange number of sectors/track");
829 puts("v\tverify the partition table");
830 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000831 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832 puts("b\tmove beginning of data in a partition"); /* !sun */
833 puts("c\tchange number of cylinders");
834 puts("d\tprint the raw data in the partition table");
835 puts("e\tlist extended partitions"); /* !sun */
836 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
837 puts("h\tchange number of heads");
838 puts("p\tprint the partition table");
839 puts("q\tquit without saving changes");
840 puts("r\treturn to main menu");
841 puts("s\tchange number of sectors/track");
842 puts("v\tverify the partition table");
843 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000844 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000845 puts("b\tmove beginning of data in a partition"); /* !sun */
846 puts("c\tchange number of cylinders");
847 puts("d\tprint the raw data in the partition table");
848 puts("e\tlist extended partitions"); /* !sun */
849 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000850#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000851 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000852#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000853 puts("h\tchange number of heads");
854 puts("p\tprint the partition table");
855 puts("q\tquit without saving changes");
856 puts("r\treturn to main menu");
857 puts("s\tchange number of sectors/track");
858 puts("v\tverify the partition table");
859 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860 }
861}
862#endif /* ADVANCED mode */
863
Denis Vlasenko834410a2006-11-29 12:00:28 +0000864#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000865static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000866get_sys_types(void)
867{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000868 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000869 LABEL_IS_SUN ? sun_sys_types :
870 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000871 i386_sys_types);
872}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000873#else
874#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000875#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000876
Denis Vlasenkobd852072007-03-19 14:43:38 +0000877static const char *
878partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000879{
880 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000881 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000882
Denis Vlasenkobd852072007-03-19 14:43:38 +0000883 for (i = 0; types[i]; i++)
884 if ((unsigned char)types[i][0] == type)
885 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000886
Denis Vlasenkobd852072007-03-19 14:43:38 +0000887 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000888}
889
890
Denis Vlasenko834410a2006-11-29 12:00:28 +0000891#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000892static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000893get_sysid(int i)
894{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000895 return LABEL_IS_SUN ? sunlabel->infos[i].id :
896 (LABEL_IS_SGI ? sgi_get_sysid(i) :
897 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000898}
899
Denis Vlasenkobd852072007-03-19 14:43:38 +0000900static void
901list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000903 enum { COLS = 3 };
904
905 unsigned last[COLS];
906 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000907 int i;
908
Denis Vlasenkobd852072007-03-19 14:43:38 +0000909 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000910
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000911 done = 0;
912 for (i = COLS-1; i >= 0; i--) {
913 done += (size + i - done) / (i + 1);
914 last[COLS-1 - i] = done;
915 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000916
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000917 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000918 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000919 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000920 (unsigned char)sys[next][0],
921 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000922 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000923 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924 i = 0;
925 next = ++done;
926 }
927 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000928 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000930#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000931
932static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000933is_cleared_partition(const struct partition *p)
934{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
936 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
937 get_start_sect(p) || get_nr_sects(p));
938}
939
940static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000941clear_partition(struct partition *p)
942{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000943 if (!p)
944 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000945 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000946}
947
Denis Vlasenko834410a2006-11-29 12:00:28 +0000948#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000950set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000951{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000952 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000953 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000954
955 if (doext) {
956 p = ptes[i].ext_pointer;
957 offset = extended_offset;
958 } else {
959 p = ptes[i].part_table;
960 offset = ptes[i].offset;
961 }
962 p->boot_ind = 0;
963 p->sys_ind = sysid;
964 set_start_sect(p, start - offset);
965 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000966 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
967 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000969 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
970 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
972 ptes[i].changed = 1;
973}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000974#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975
976static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000977warn_geometry(void)
978{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000979 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000981
Denis Vlasenkobd852072007-03-19 14:43:38 +0000982 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000983 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000984 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000985 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000986 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000987 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000988 printf(" cylinders");
989 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000990#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000991 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000992#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000993 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994 return 1;
995}
996
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000997static void
998update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000999{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001000 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001001
1002 if (display_in_cyl_units && cyl_units)
1003 units_per_sector = cyl_units;
1004 else
1005 units_per_sector = 1; /* in sectors */
1006}
1007
Denis Vlasenko834410a2006-11-29 12:00:28 +00001008#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001009static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001010warn_cylinders(void)
1011{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001012 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001013 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001014"The number of cylinders for this disk is set to %d.\n"
1015"There is nothing wrong with that, but this is larger than 1024,\n"
1016"and could in certain setups cause problems with:\n"
1017"1) software that runs at boot time (e.g., old versions of LILO)\n"
1018"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001019" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001020 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001022#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001023
1024static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001025read_extended(int ext)
1026{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001027 int i;
1028 struct pte *pex;
1029 struct partition *p, *q;
1030
1031 ext_index = ext;
1032 pex = &ptes[ext];
1033 pex->ext_pointer = pex->part_table;
1034
1035 p = pex->part_table;
1036 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001037 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001038 return;
1039 }
1040
Rob Landleyb73451d2006-02-24 16:29:00 +00001041 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001042 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001043
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001044 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045 /* This is not a Linux restriction, but
1046 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001047 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001048 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001049#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001050 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001051 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001052 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001053#endif
1054 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001055 return;
1056 }
1057
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001058 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001059
1060 if (!extended_offset)
1061 extended_offset = get_start_sect(p);
1062
1063 q = p = pt_offset(pe->sectorbuffer, 0);
1064 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001065 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001066 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001067 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001068 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001069 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001070 else
1071 pe->ext_pointer = p;
1072 } else if (p->sys_ind) {
1073 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001074 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001076 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001077 else
1078 pe->part_table = p;
1079 }
1080 }
1081
1082 /* very strange code here... */
1083 if (!pe->part_table) {
1084 if (q != pe->ext_pointer)
1085 pe->part_table = q;
1086 else
1087 pe->part_table = q + 1;
1088 }
1089 if (!pe->ext_pointer) {
1090 if (q != pe->part_table)
1091 pe->ext_pointer = q;
1092 else
1093 pe->ext_pointer = q + 1;
1094 }
1095
1096 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001097 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001098 }
1099
Denis Vlasenko834410a2006-11-29 12:00:28 +00001100#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001101 /* remove empty links */
1102 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001103 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001104 struct pte *pe = &ptes[i];
1105
Denis Vlasenkobd852072007-03-19 14:43:38 +00001106 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001107 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001108 ) {
1109 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001110 delete_partition(i);
1111 goto remove; /* numbering changed */
1112 }
1113 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001114#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115}
1116
Denis Vlasenko834410a2006-11-29 12:00:28 +00001117#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001118static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001119create_doslabel(void)
1120{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001121 int i;
1122
Denis Vlasenkobd852072007-03-19 14:43:38 +00001123 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001124
1125 current_label_type = label_dos;
1126
Denis Vlasenko834410a2006-11-29 12:00:28 +00001127#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001128 possibly_osf_label = 0;
1129#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001130 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001131
1132 for (i = 510-64; i < 510; i++)
1133 MBRbuffer[i] = 0;
1134 write_part_table_flag(MBRbuffer);
1135 extended_offset = 0;
1136 set_all_unchanged();
1137 set_changed(0);
1138 get_boot(create_empty_dos);
1139}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001140#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001141
1142static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001143get_sectorsize(void)
1144{
Rob Landley736e5252006-02-25 03:36:00 +00001145 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001146 int arg;
1147 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1148 sector_size = arg;
1149 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001150 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001151 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001152 }
1153}
1154
Rob Landley88621d72006-08-29 19:41:06 +00001155static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001156get_kernel_geometry(void)
1157{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001158 struct hd_geometry geometry;
1159
1160 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1161 kern_heads = geometry.heads;
1162 kern_sectors = geometry.sectors;
1163 /* never use geometry.cylinders - it is truncated */
1164 }
1165}
1166
1167static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001168get_partition_table_geometry(void)
1169{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001170 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 struct partition *p;
1172 int i, h, s, hh, ss;
1173 int first = 1;
1174 int bad = 0;
1175
Eric Andersen3496fdc2006-01-30 23:09:20 +00001176 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001177 return;
1178
1179 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001180 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181 p = pt_offset(bufp, i);
1182 if (p->sys_ind != 0) {
1183 h = p->end_head + 1;
1184 s = (p->end_sector & 077);
1185 if (first) {
1186 hh = h;
1187 ss = s;
1188 first = 0;
1189 } else if (hh != h || ss != s)
1190 bad = 1;
1191 }
1192 }
1193
1194 if (!first && !bad) {
1195 pt_heads = hh;
1196 pt_sectors = ss;
1197 }
1198}
1199
Rob Landleyb73451d2006-02-24 16:29:00 +00001200static void
1201get_geometry(void)
1202{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001203 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001204
1205 get_sectorsize();
1206 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001207#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208 guess_device_type();
1209#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001210 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001211 kern_heads = kern_sectors = 0;
1212 pt_heads = pt_sectors = 0;
1213
1214 get_kernel_geometry();
1215 get_partition_table_geometry();
1216
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001217 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001218 pt_heads ? pt_heads :
1219 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001220 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001221 pt_sectors ? pt_sectors :
1222 kern_sectors ? kern_sectors : 63;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001223 total_number_of_sectors = bb_BLKGETSIZE_sectors();
Eric Andersen040f4402003-07-30 08:40:37 +00001224
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001225 sector_offset = 1;
1226 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001227 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001228
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001229 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1230 if (!g_cylinders)
1231 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001232}
1233
1234/*
1235 * Read MBR. Returns:
1236 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1237 * 0: found or created label
1238 * 1: I/O error
1239 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001240static int
1241get_boot(enum action what)
1242{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 int i;
1244
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001245 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001246
1247 for (i = 0; i < 4; i++) {
1248 struct pte *pe = &ptes[i];
1249
1250 pe->part_table = pt_offset(MBRbuffer, i);
1251 pe->ext_pointer = NULL;
1252 pe->offset = 0;
1253 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001254#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001255 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001256#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 }
1258
Denis Vlasenko834410a2006-11-29 12:00:28 +00001259#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001260 if (what == create_empty_sun && check_sun_label())
1261 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001262#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001263
1264 memset(MBRbuffer, 0, 512);
1265
Denis Vlasenko834410a2006-11-29 12:00:28 +00001266#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001267 if (what == create_empty_dos)
1268 goto got_dos_table; /* skip reading disk */
1269
Denis Vlasenkobd852072007-03-19 14:43:38 +00001270 fd = open(disk_device, type_open);
1271 if (fd < 0) {
1272 fd = open(disk_device, O_RDONLY);
1273 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001274 if (what == try_only)
1275 return 1;
1276 fdisk_fatal(unable_to_open);
1277 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001278 printf("You will not be able to write "
1279 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001280 }
1281
1282 if (512 != read(fd, MBRbuffer, 512)) {
1283 if (what == try_only)
1284 return 1;
1285 fdisk_fatal(unable_to_read);
1286 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001287#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001288 fd = open(disk_device, O_RDONLY);
1289 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001290 return 1;
1291 if (512 != read(fd, MBRbuffer, 512))
1292 return 1;
1293#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001294
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001295 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296
1297 update_units();
1298
Denis Vlasenko834410a2006-11-29 12:00:28 +00001299#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 if (check_sun_label())
1301 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001302#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303
Denis Vlasenko834410a2006-11-29 12:00:28 +00001304#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001305 if (check_sgi_label())
1306 return 0;
1307#endif
1308
Denis Vlasenko834410a2006-11-29 12:00:28 +00001309#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001310 if (check_aix_label())
1311 return 0;
1312#endif
1313
Denis Vlasenko834410a2006-11-29 12:00:28 +00001314#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315 if (check_osf_label()) {
1316 possibly_osf_label = 1;
1317 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001318 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319 return 0;
1320 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001321 printf("This disk has both DOS and BSD magic.\n"
1322 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323 }
1324#endif
1325
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001327 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001328#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329
1330 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001331#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001332 return -1;
1333#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001334 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001335 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001336 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001338 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001339#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001340#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 create_sunlabel();
1342#endif
1343#else
1344 create_doslabel();
1345#endif
1346 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347 case try_only:
1348 return -1;
1349 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001350#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001352#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 break;
1354 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001355 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001357#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001358 }
1359
Denis Vlasenko834410a2006-11-29 12:00:28 +00001360#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001361 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001362#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001363 warn_geometry();
1364
1365 for (i = 0; i < 4; i++) {
1366 struct pte *pe = &ptes[i];
1367
Rob Landleyb73451d2006-02-24 16:29:00 +00001368 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001369 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001370 printf("Ignoring extra extended "
1371 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372 else
1373 read_extended(i);
1374 }
1375 }
1376
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001377 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 struct pte *pe = &ptes[i];
1379
1380 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001381 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1382 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001383 pe->sectorbuffer[510],
1384 pe->sectorbuffer[511],
1385 i + 1);
1386#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001388#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001389 }
1390 }
1391
1392 return 0;
1393}
1394
Denis Vlasenko834410a2006-11-29 12:00:28 +00001395#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001396/*
1397 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1398 * If the user hits Enter, DFLT is returned.
1399 * Answers like +10 are interpreted as offsets from BASE.
1400 *
1401 * There is no default if DFLT is not between LOW and HIGH.
1402 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001403static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001404read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001405{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001406 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001408 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001410 if (dflt < low || dflt > high) {
1411 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001412 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001413 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414
1415 while (1) {
1416 int use_default = default_ok;
1417
1418 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001419 do {
1420 printf(fmt, mesg, low, high, dflt);
1421 read_maybe_empty("");
1422 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1423 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001424
Eric Andersen84bdea82004-05-19 10:49:17 +00001425 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001426 int minus = (*line_ptr == '-');
1427 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001428
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001429 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001430
Rob Landleyb73451d2006-02-24 16:29:00 +00001431 while (isdigit(*++line_ptr))
1432 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001433
Rob Landleyb73451d2006-02-24 16:29:00 +00001434 switch (*line_ptr) {
1435 case 'c':
1436 case 'C':
1437 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001438 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001439 break;
1440 case 'K':
1441 absolute = 1024;
1442 break;
1443 case 'k':
1444 absolute = 1000;
1445 break;
1446 case 'm':
1447 case 'M':
1448 absolute = 1000000;
1449 break;
1450 case 'g':
1451 case 'G':
1452 absolute = 1000000000;
1453 break;
1454 default:
1455 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001456 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001457 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001458 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001459 unsigned long unit;
1460
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001461 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001462 unit = sector_size * units_per_sector;
1463 bytes += unit/2; /* round */
1464 bytes /= unit;
1465 i = bytes;
1466 }
1467 if (minus)
1468 i = -i;
1469 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001470 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471 i = atoi(line_ptr);
1472 while (isdigit(*line_ptr)) {
1473 line_ptr++;
1474 use_default = 0;
1475 }
1476 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001477 if (use_default) {
1478 i = dflt;
1479 printf("Using default value %u\n", i);
1480 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481 if (i >= low && i <= high)
1482 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001483 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001484 }
1485 return i;
1486}
1487
Rob Landleyb73451d2006-02-24 16:29:00 +00001488static int
1489get_partition(int warn, int max)
1490{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001491 struct pte *pe;
1492 int i;
1493
Denis Vlasenkobd852072007-03-19 14:43:38 +00001494 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001495 pe = &ptes[i];
1496
1497 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001498 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1499 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1500 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1501 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001502 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001503 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001504 }
1505 return i;
1506}
1507
1508static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001509get_existing_partition(int warn, int max)
1510{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001511 int pno = -1;
1512 int i;
1513
1514 for (i = 0; i < max; i++) {
1515 struct pte *pe = &ptes[i];
1516 struct partition *p = pe->part_table;
1517
1518 if (p && !is_cleared_partition(p)) {
1519 if (pno >= 0)
1520 goto not_unique;
1521 pno = i;
1522 }
1523 }
1524 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001525 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001526 return pno;
1527 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001528 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001529 return -1;
1530
1531 not_unique:
1532 return get_partition(warn, max);
1533}
1534
1535static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001536get_nonexisting_partition(int warn, int max)
1537{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001538 int pno = -1;
1539 int i;
1540
1541 for (i = 0; i < max; i++) {
1542 struct pte *pe = &ptes[i];
1543 struct partition *p = pe->part_table;
1544
1545 if (p && is_cleared_partition(p)) {
1546 if (pno >= 0)
1547 goto not_unique;
1548 pno = i;
1549 }
1550 }
1551 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001552 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001553 return pno;
1554 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001555 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001556 return -1;
1557
1558 not_unique:
1559 return get_partition(warn, max);
1560}
1561
1562
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001563static void
1564change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001565{
1566 display_in_cyl_units = !display_in_cyl_units;
1567 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001568 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001569 str_units(PLURAL));
1570}
1571
1572static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001573toggle_active(int i)
1574{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001575 struct pte *pe = &ptes[i];
1576 struct partition *p = pe->part_table;
1577
Rob Landleyb73451d2006-02-24 16:29:00 +00001578 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001579 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001580 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1581 pe->changed = 1;
1582}
1583
1584static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001585toggle_dos_compatibility_flag(void)
1586{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001587 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001588 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001589 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001590 printf("DOS Compatibility flag is set\n");
1591 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001592 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001593 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594 }
1595}
1596
1597static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001598delete_partition(int i)
1599{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001600 struct pte *pe = &ptes[i];
1601 struct partition *p = pe->part_table;
1602 struct partition *q = pe->ext_pointer;
1603
1604/* Note that for the fifth partition (i == 4) we don't actually
1605 * decrement partitions.
1606 */
1607
1608 if (warn_geometry())
1609 return; /* C/H/S not set */
1610 pe->changed = 1;
1611
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001612 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613 sun_delete_partition(i);
1614 return;
1615 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001616 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001617 sgi_delete_partition(i);
1618 return;
1619 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001620
1621 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001622 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001623 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001624 ptes[ext_index].ext_pointer = NULL;
1625 extended_offset = 0;
1626 }
1627 clear_partition(p);
1628 return;
1629 }
1630
1631 if (!q->sys_ind && i > 4) {
1632 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001633 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001634 --i;
1635 clear_partition(ptes[i].ext_pointer);
1636 ptes[i].changed = 1;
1637 } else {
1638 /* not the last one - further ones will be moved down */
1639 if (i > 4) {
1640 /* delete this link in the chain */
1641 p = ptes[i-1].ext_pointer;
1642 *p = *q;
1643 set_start_sect(p, get_start_sect(q));
1644 set_nr_sects(p, get_nr_sects(q));
1645 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001646 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001647 /* the first logical in a longer chain */
1648 pe = &ptes[5];
1649
1650 if (pe->part_table) /* prevent SEGFAULT */
1651 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001652 get_partition_start(pe) -
1653 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001654 pe->offset = extended_offset;
1655 pe->changed = 1;
1656 }
1657
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001658 if (g_partitions > 5) {
1659 g_partitions--;
1660 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001661 ptes[i] = ptes[i+1];
1662 i++;
1663 }
1664 } else
1665 /* the only logical: clear only */
1666 clear_partition(ptes[i].part_table);
1667 }
1668}
1669
1670static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001671change_sysid(void)
1672{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001673 int i, sys, origsys;
1674 struct partition *p;
1675
Eric Andersen040f4402003-07-30 08:40:37 +00001676 /* If sgi_label then don't use get_existing_partition,
1677 let the user select a partition, since get_existing_partition()
1678 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001679 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001680 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001681 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001682 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001683 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001684 if (i == -1)
1685 return;
1686 p = ptes[i].part_table;
1687 origsys = sys = get_sysid(i);
1688
1689 /* if changing types T to 0 is allowed, then
1690 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001691 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001692 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001693 return;
1694 }
1695 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001696 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001697
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001698 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001699 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001700 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001701 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001702 /* break; */
1703 }
1704
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001705 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001706 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001707 printf("You cannot change a partition into"
1708 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001709 break;
1710 }
1711 }
1712
1713 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001714#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001715 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001716 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001717 "as Whole disk (5),\n"
1718 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001719 "even Linux likes it\n\n");
1720#endif
1721#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001722 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001723 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001724 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001725 (i == 8 && sys != 0)
1726 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001727 ) {
1728 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001729 "as volume header (0),\nand "
1730 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001731 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001732 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001733#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001734 if (sys == origsys)
1735 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001736 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001737 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001738 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001739 sgi_change_sysid(i, sys);
1740 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001742
Denis Vlasenkobd852072007-03-19 14:43:38 +00001743 printf("Changed system type of partition %d "
1744 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001745 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001746 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001747 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1748 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749 break;
1750 }
1751 }
1752}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001753#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001754
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001755
Denis Vlasenko28703012006-12-19 20:32:02 +00001756/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1758 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1759 * Lubkin Oct. 1991). */
1760
Rob Landleyb73451d2006-02-24 16:29:00 +00001761static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001762linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001763{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001764 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001765
1766 *c = ls / spc;
1767 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001768 *h = ls / g_sectors;
1769 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001770}
1771
Rob Landleyb73451d2006-02-24 16:29:00 +00001772static void
1773check_consistency(const struct partition *p, int partition)
1774{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001775 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1776 unsigned pec, peh, pes; /* physical ending c, h, s */
1777 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1778 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001779
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001780 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001781 return; /* do not check extended partitions */
1782
1783/* physical beginning c, h, s */
1784 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1785 pbh = p->head;
1786 pbs = p->sector & 0x3f;
1787
1788/* physical ending c, h, s */
1789 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1790 peh = p->end_head;
1791 pes = p->end_sector & 0x3f;
1792
1793/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001794 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001795
1796/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001797 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798
1799/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001800 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001801 printf("Partition %d has different physical/logical "
1802 "beginnings (non-Linux?):\n", partition + 1);
1803 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1804 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 }
1806
1807/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001808 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001809 printf("Partition %d has different physical/logical "
1810 "endings:\n", partition + 1);
1811 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1812 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 }
1814
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001815/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001816 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001817 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001818 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819 }
1820}
1821
1822static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001823list_disk_geometry(void)
1824{
Eric Andersen040f4402003-07-30 08:40:37 +00001825 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001826 long megabytes = bytes/1000000;
1827
1828 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001829 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001830 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001831 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001832 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001833 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001834 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001835 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001836 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001837 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001838 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001839 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001840 str_units(PLURAL),
1841 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001842}
1843
1844/*
1845 * Check whether partition entries are ordered by their starting positions.
1846 * Return 0 if OK. Return i if partition i should have been earlier.
1847 * Two separate checks: primary and logical partitions.
1848 */
1849static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001850wrong_p_order(int *prev)
1851{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001852 const struct pte *pe;
1853 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001854 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001855 int i, last_i = 0;
1856
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001857 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001858 if (i == 4) {
1859 last_i = 4;
1860 last_p_start_pos = 0;
1861 }
1862 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001863 p = pe->part_table;
1864 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001865 p_start_pos = get_partition_start(pe);
1866
1867 if (last_p_start_pos > p_start_pos) {
1868 if (prev)
1869 *prev = last_i;
1870 return i;
1871 }
1872
1873 last_p_start_pos = p_start_pos;
1874 last_i = i;
1875 }
1876 }
1877 return 0;
1878}
1879
Denis Vlasenko834410a2006-11-29 12:00:28 +00001880#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001881/*
1882 * Fix the chain of logicals.
1883 * extended_offset is unchanged, the set of sectors used is unchanged
1884 * The chain is sorted so that sectors increase, and so that
1885 * starting sectors increase.
1886 *
1887 * After this it may still be that cfdisk doesnt like the table.
1888 * (This is because cfdisk considers expanded parts, from link to
1889 * end of partition, and these may still overlap.)
1890 * Now
1891 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1892 * may help.
1893 */
1894static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001895fix_chain_of_logicals(void)
1896{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001897 int j, oj, ojj, sj, sjj;
1898 struct partition *pj,*pjj,tmp;
1899
1900 /* Stage 1: sort sectors but leave sector of part 4 */
1901 /* (Its sector is the global extended_offset.) */
1902 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001903 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001904 oj = ptes[j].offset;
1905 ojj = ptes[j+1].offset;
1906 if (oj > ojj) {
1907 ptes[j].offset = ojj;
1908 ptes[j+1].offset = oj;
1909 pj = ptes[j].part_table;
1910 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1911 pjj = ptes[j+1].part_table;
1912 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1913 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001914 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001915 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001916 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001917 goto stage1;
1918 }
1919 }
1920
1921 /* Stage 2: sort starting sectors */
1922 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001923 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001924 pj = ptes[j].part_table;
1925 pjj = ptes[j+1].part_table;
1926 sj = get_start_sect(pj);
1927 sjj = get_start_sect(pjj);
1928 oj = ptes[j].offset;
1929 ojj = ptes[j+1].offset;
1930 if (oj+sj > ojj+sjj) {
1931 tmp = *pj;
1932 *pj = *pjj;
1933 *pjj = tmp;
1934 set_start_sect(pj, ojj+sjj-oj);
1935 set_start_sect(pjj, oj+sj-ojj);
1936 goto stage2;
1937 }
1938 }
1939
1940 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001941 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001942 ptes[j].changed = 1;
1943}
1944
1945
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001946static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001947fix_partition_table_order(void)
1948{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001949 struct pte *pei, *pek;
1950 int i,k;
1951
1952 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001953 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001954 return;
1955 }
1956
1957 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1958 /* partition i should have come earlier, move it */
1959 /* We have to move data in the MBR */
1960 struct partition *pi, *pk, *pe, pbuf;
1961 pei = &ptes[i];
1962 pek = &ptes[k];
1963
1964 pe = pei->ext_pointer;
1965 pei->ext_pointer = pek->ext_pointer;
1966 pek->ext_pointer = pe;
1967
1968 pi = pei->part_table;
1969 pk = pek->part_table;
1970
1971 memmove(&pbuf, pi, sizeof(struct partition));
1972 memmove(pi, pk, sizeof(struct partition));
1973 memmove(pk, &pbuf, sizeof(struct partition));
1974
1975 pei->changed = pek->changed = 1;
1976 }
1977
1978 if (i)
1979 fix_chain_of_logicals();
1980
1981 printf("Done.\n");
1982
1983}
1984#endif
1985
1986static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001987list_table(int xtra)
1988{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001989 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001990 int i, w;
1991
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001992 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001993 sun_list_table(xtra);
1994 return;
1995 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001996 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001997 sgi_list_table(xtra);
1998 return;
1999 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002000
2001 list_disk_geometry();
2002
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002003 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002004 xbsd_print_disklabel(xtra);
2005 return;
2006 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002007
2008 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2009 but if the device name ends in a digit, say /dev/foo1,
2010 then the partition is called /dev/foo1p3. */
2011 w = strlen(disk_device);
2012 if (w && isdigit(disk_device[w-1]))
2013 w++;
2014 if (w < 5)
2015 w = 5;
2016
Denis Vlasenkobd852072007-03-19 14:43:38 +00002017 // 1 12345678901 12345678901 12345678901 12
2018 printf("%*s Boot Start End Blocks Id System\n",
2019 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002021 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002022 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002023 ullong psects;
2024 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002025 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002026
2027 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002028 if (!p || is_cleared_partition(p))
2029 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002030
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002031 psects = get_nr_sects(p);
2032 pblocks = psects;
2033 podd = 0;
2034
2035 if (sector_size < 1024) {
2036 pblocks /= (1024 / sector_size);
2037 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002038 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002039 if (sector_size > 1024)
2040 pblocks *= (sector_size / 1024);
2041
2042 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2043 partname(disk_device, i+1, w+2),
2044 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2045 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002046 (ullong) cround(get_partition_start(pe)), /* start */
2047 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002048 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002049 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002050 p->sys_ind, /* type id */
2051 partition_type(p->sys_ind)); /* type name */
2052
2053 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002054 }
2055
2056 /* Is partition table in disk order? It need not be, but... */
2057 /* partition table entries are not checked for correct order if this
2058 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002059 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002060 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002061 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002062 }
2063}
2064
Denis Vlasenko834410a2006-11-29 12:00:28 +00002065#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002066static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002067x_list_table(int extend)
2068{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002069 const struct pte *pe;
2070 const struct partition *p;
2071 int i;
2072
Denis Vlasenkobd852072007-03-19 14:43:38 +00002073 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002074 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002075 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002076 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002077 pe = &ptes[i];
2078 p = (extend ? pe->ext_pointer : pe->part_table);
2079 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002080 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002081 i + 1, p->boot_ind, p->head,
2082 sector(p->sector),
2083 cylinder(p->sector, p->cyl), p->end_head,
2084 sector(p->end_sector),
2085 cylinder(p->end_sector, p->end_cyl),
2086 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2087 if (p->sys_ind)
2088 check_consistency(p, i);
2089 }
2090 }
2091}
2092#endif
2093
Denis Vlasenko834410a2006-11-29 12:00:28 +00002094#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002095static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002096fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002097{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002098 int i;
2099 const struct pte *pe = &ptes[0];
2100 const struct partition *p;
2101
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002102 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002103 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002104 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002105 first[i] = 0xffffffff;
2106 last[i] = 0;
2107 } else {
2108 first[i] = get_partition_start(pe);
2109 last[i] = first[i] + get_nr_sects(p) - 1;
2110 }
2111 }
2112}
2113
2114static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002115check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002116{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002117 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118
2119 real_s = sector(s) - 1;
2120 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002121 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002122 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002123 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002124 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002125 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002126 n, h + 1, g_heads);
2127 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002128 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002129 "maximum %d\n", n, s, g_sectors);
2130 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002131 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002132 "maximum %d\n", n, real_c + 1, g_cylinders);
2133 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002134 printf("Partition %d: previous sectors %llu disagrees with "
2135 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136}
2137
2138static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002139verify(void)
2140{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002141 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002142 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002143 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 struct partition *p;
2145
2146 if (warn_geometry())
2147 return;
2148
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002149 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002150 verify_sun();
2151 return;
2152 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002153 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002154 verify_sgi(1);
2155 return;
2156 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002157
2158 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002159 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 struct pte *pe = &ptes[i];
2161
2162 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002163 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164 check_consistency(p, i);
2165 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002166 printf("Warning: bad start-of-data in "
2167 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002168 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2169 last[i]);
2170 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002171 for (j = 0; j < i; j++) {
2172 if ((first[i] >= first[j] && first[i] <= last[j])
2173 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2174 printf("Warning: partition %d overlaps "
2175 "partition %d\n", j + 1, i + 1);
2176 total += first[i] >= first[j] ?
2177 first[i] : first[j];
2178 total -= last[i] <= last[j] ?
2179 last[i] : last[j];
2180 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002181 }
2182 }
2183 }
2184
2185 if (extended_offset) {
2186 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002187 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002188 get_nr_sects(pex->part_table) - 1;
2189
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002190 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002191 total++;
2192 p = ptes[i].part_table;
2193 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002194 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002195 printf("Warning: partition %d "
2196 "is empty\n", i + 1);
2197 } else if (first[i] < extended_offset || last[i] > e_last) {
2198 printf("Logical partition %d not entirely in "
2199 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002200 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002201 }
2202 }
2203
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002204 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002205 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002206 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002207 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002208 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002209 if (total != 0)
2210 printf("%d unallocated sectors\n", total);
2211 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002212}
2213
2214static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002215add_partition(int n, int sys)
2216{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002218 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219 struct partition *p = ptes[n].part_table;
2220 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002221 ullong limit, temp;
2222 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002223 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002224
2225 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002226 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 return;
2228 }
2229 fill_bounds(first, last);
2230 if (n < 4) {
2231 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002232 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002233 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002234 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002235 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236 if (extended_offset) {
2237 first[ext_index] = extended_offset;
2238 last[ext_index] = get_start_sect(q) +
2239 get_nr_sects(q) - 1;
2240 }
2241 } else {
2242 start = extended_offset + sector_offset;
2243 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2244 }
2245 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002246 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002247 first[i] = (cround(first[i]) - 1) * units_per_sector;
2248
Denis Vlasenkobd852072007-03-19 14:43:38 +00002249 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002250 do {
2251 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002252 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 int lastplusoff;
2254
2255 if (start == ptes[i].offset)
2256 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002257 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 if (start >= first[i] && start <= lastplusoff)
2259 start = lastplusoff + 1;
2260 }
2261 if (start > limit)
2262 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002263 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002264 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002265 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002266 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002267 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002268 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002269 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002270
2271 saved_start = start;
2272 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2273 0, mesg);
2274 if (display_in_cyl_units) {
2275 start = (start - 1) * units_per_sector;
2276 if (start < saved_start) start = saved_start;
2277 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002278 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002279 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002280 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281 if (n > 4) { /* NOT for fifth partition */
2282 struct pte *pe = &ptes[n];
2283
2284 pe->offset = start - sector_offset;
2285 if (pe->offset == extended_offset) { /* must be corrected */
2286 pe->offset++;
2287 if (sector_offset == 1)
2288 start++;
2289 }
2290 }
2291
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002292 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 struct pte *pe = &ptes[i];
2294
2295 if (start < pe->offset && limit >= pe->offset)
2296 limit = pe->offset - 1;
2297 if (start < first[i] && limit >= first[i])
2298 limit = first[i] - 1;
2299 }
2300 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002301 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002302 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002303 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002304 return;
2305 }
2306 if (cround(start) == cround(limit)) {
2307 stop = limit;
2308 } else {
2309 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002310 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002311 str_units(SINGULAR));
2312 stop = read_int(cround(start), cround(limit), cround(limit),
2313 cround(start), mesg);
2314 if (display_in_cyl_units) {
2315 stop = stop * units_per_sector - 1;
2316 if (stop >limit)
2317 stop = limit;
2318 }
2319 }
2320
2321 set_partition(n, 0, start, stop, sys);
2322 if (n > 4)
2323 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2324
Rob Landleyb73451d2006-02-24 16:29:00 +00002325 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002326 struct pte *pe4 = &ptes[4];
2327 struct pte *pen = &ptes[n];
2328
2329 ext_index = n;
2330 pen->ext_pointer = p;
2331 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002332 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002333 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2334 pe4->ext_pointer = pe4->part_table + 1;
2335 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002336 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002337 }
2338}
2339
2340static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002341add_logical(void)
2342{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002343 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2344 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002345
Rob Landley081e3842006-08-03 20:07:35 +00002346 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2348 pe->ext_pointer = pe->part_table + 1;
2349 pe->offset = 0;
2350 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002351 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002352 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002353 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354}
2355
2356static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002357new_partition(void)
2358{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002359 int i, free_primary = 0;
2360
2361 if (warn_geometry())
2362 return;
2363
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002364 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002365 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002366 return;
2367 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002368 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002369 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002370 return;
2371 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002372 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002373 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2374"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2375"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 return;
2377 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002379 for (i = 0; i < 4; i++)
2380 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002381
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002382 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002383 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002384 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002385 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002386
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 if (!free_primary) {
2388 if (extended_offset)
2389 add_logical();
2390 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002391 printf("You must delete some partition and add "
2392 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002393 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002394 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002395 snprintf(line, sizeof(line),
2396 "Command action\n"
2397 " %s\n"
2398 " p primary partition (1-4)\n",
2399 (extended_offset ?
2400 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002402 c = read_nonempty(line);
2403 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002404 i = get_nonexisting_partition(0, 4);
2405 if (i >= 0)
2406 add_partition(i, LINUX_NATIVE);
2407 return;
2408 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002409 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 add_logical();
2411 return;
2412 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002413 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002414 i = get_nonexisting_partition(0, 4);
2415 if (i >= 0)
2416 add_partition(i, EXTENDED);
2417 return;
2418 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002419 printf("Invalid partition number "
2420 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002421 }
2422 }
2423}
2424
2425static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002426write_table(void)
2427{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002428 int i;
2429
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002430 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002431 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 if (ptes[i].changed)
2433 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002434 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002435 struct pte *pe = &ptes[i];
2436
2437 if (pe->changed) {
2438 write_part_table_flag(pe->sectorbuffer);
2439 write_sector(pe->offset, pe->sectorbuffer);
2440 }
2441 }
2442 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002443 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002444 /* no test on change? the printf below might be mistaken */
2445 sgi_write_table();
2446 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002447 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002448 int needw = 0;
2449
Rob Landleyb73451d2006-02-24 16:29:00 +00002450 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 if (ptes[i].changed)
2452 needw = 1;
2453 if (needw)
2454 sun_write_table();
2455 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002456
Denis Vlasenkobd852072007-03-19 14:43:38 +00002457 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002458 reread_partition_table(1);
2459}
2460
Rob Landleyb73451d2006-02-24 16:29:00 +00002461static void
2462reread_partition_table(int leave)
2463{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464 int i;
2465
Denis Vlasenkobd852072007-03-19 14:43:38 +00002466 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002467 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002468 /* sleep(2); Huh? */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002469 i = ioctl_or_perror(fd, BLKRRPART, NULL,
2470 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002471 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002472#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002474 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002475 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002477 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002478#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002479
2480 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002481 if (ENABLE_FEATURE_CLEAN_UP)
2482 close(fd);
2483 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002484 }
2485}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002486#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002487
Denis Vlasenko834410a2006-11-29 12:00:28 +00002488#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489#define MAX_PER_LINE 16
2490static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002491print_buffer(char *pbuffer)
2492{
2493 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494
2495 for (i = 0, l = 0; i < sector_size; i++, l++) {
2496 if (l == 0)
2497 printf("0x%03X:", i);
2498 printf(" %02X", (unsigned char) pbuffer[i]);
2499 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002500 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002501 l = -1;
2502 }
2503 }
2504 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002505 bb_putchar('\n');
2506 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002507}
2508
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002510print_raw(void)
2511{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 int i;
2513
Denis Vlasenkobd852072007-03-19 14:43:38 +00002514 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002515 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002516 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002517 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002518 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002520 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002521}
2522
2523static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002524move_begin(int i)
2525{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 struct pte *pe = &ptes[i];
2527 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002528 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002529
2530 if (warn_geometry())
2531 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002532 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002533 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 return;
2535 }
2536 first = get_partition_start(pe);
2537 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002538 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002539
2540 if (new != get_nr_sects(p)) {
2541 first = get_nr_sects(p) + get_start_sect(p) - new;
2542 set_nr_sects(p, first);
2543 set_start_sect(p, new);
2544 pe->changed = 1;
2545 }
2546}
2547
2548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002549xselect(void)
2550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551 char c;
2552
Rob Landleyb73451d2006-02-24 16:29:00 +00002553 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002554 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002555 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556 switch (c) {
2557 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002558 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002560 break;
2561 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002562 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002563 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564 break;
2565 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002566 user_cylinders = g_cylinders =
2567 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002568 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002569 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002570 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002571 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 warn_cylinders();
2573 break;
2574 case 'd':
2575 print_raw();
2576 break;
2577 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002578 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002579 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002582 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583 x_list_table(1);
2584 break;
2585 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002586 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 fix_partition_table_order();
2588 break;
2589 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002590#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002591 create_sgilabel();
2592#endif
2593 break;
2594 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002595 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002596 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 update_units();
2598 break;
2599 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002600 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002601 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602 break;
2603 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002604 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002605 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 break;
2607 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002608 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 list_table(1);
2610 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 x_list_table(0);
2612 break;
2613 case 'q':
2614 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002615 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 exit(0);
2617 case 'r':
2618 return;
2619 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002620 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002621 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002623 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002624 printf("Warning: setting sector offset for DOS "
2625 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 }
2627 update_units();
2628 break;
2629 case 'v':
2630 verify();
2631 break;
2632 case 'w':
2633 write_table(); /* does not return */
2634 break;
2635 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002638 break;
2639 default:
2640 xmenu();
2641 }
2642 }
2643}
2644#endif /* ADVANCED mode */
2645
2646static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002647is_ide_cdrom_or_tape(const char *device)
2648{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002649 FILE *procf;
2650 char buf[100];
2651 struct stat statbuf;
2652 int is_ide = 0;
2653
2654 /* No device was given explicitly, and we are trying some
2655 likely things. But opening /dev/hdc may produce errors like
2656 "hdc: tray open or drive not ready"
2657 if it happens to be a CD-ROM drive. It even happens that
2658 the process hangs on the attempt to read a music CD.
2659 So try to be careful. This only works since 2.1.73. */
2660
2661 if (strncmp("/dev/hd", device, 7))
2662 return 0;
2663
2664 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2665 procf = fopen(buf, "r");
2666 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2667 is_ide = (!strncmp(buf, "cdrom", 5) ||
2668 !strncmp(buf, "tape", 4));
2669 else
2670 /* Now when this proc file does not exist, skip the
2671 device when it is read-only. */
2672 if (stat(device, &statbuf) == 0)
2673 is_ide = ((statbuf.st_mode & 0222) == 0);
2674
2675 if (procf)
2676 fclose(procf);
2677 return is_ide;
2678}
2679
Rob Landley5527b912006-02-25 03:46:10 +00002680
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002681static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002682trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002683{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002684 int gb;
2685
2686 disk_device = device;
2687 if (setjmp(listingbuf))
2688 return;
2689 if (!user_specified)
2690 if (is_ide_cdrom_or_tape(device))
2691 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002692 fd = open(disk_device, type_open);
2693 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002694 gb = get_boot(try_only);
2695 if (gb > 0) { /* I/O error */
2696 close(fd);
2697 } else if (gb < 0) { /* no DOS signature */
2698 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002699 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002700 return;
Rob Landley5527b912006-02-25 03:46:10 +00002701 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002702#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002703 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002704#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002705 printf("Disk %s doesn't contain a valid "
2706 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707 close(fd);
2708 } else {
2709 close(fd);
2710 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002711#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002712 if (!LABEL_IS_SUN && g_partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002713 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002714 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002715#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002716 }
2717 } else {
2718 /* Ignore other errors, since we try IDE
2719 and SCSI hard disks which may not be
2720 installed on the system. */
2721 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002722 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 return;
2724 }
2725 }
2726}
2727
2728/* for fdisk -l: try all things in /proc/partitions
2729 that look like a partition name (do not end in a digit) */
2730static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002731tryprocpt(void)
2732{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002733 FILE *procpt;
2734 char line[100], ptname[100], devname[120], *s;
2735 int ma, mi, sz;
2736
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002737 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738
2739 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002740 if (sscanf(line, " %d %d %d %[^\n ]",
2741 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002742 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002743 for (s = ptname; *s; s++)
2744 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002745 if (isdigit(s[-1]))
2746 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002747 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002748 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002749 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002750#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002752#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002753}
2754
Denis Vlasenko834410a2006-11-29 12:00:28 +00002755#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002756static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002757unknown_command(int c)
2758{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002759 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002760}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002761#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002762
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002763int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002764int fdisk_main(int argc, char **argv)
2765{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002766 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002767 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768 * fdisk -v
2769 * fdisk -l [-b sectorsize] [-u] device ...
2770 * fdisk -s [partition] ...
2771 * fdisk [-b sectorsize] [-u] device
2772 *
2773 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002774 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002775 enum {
2776 OPT_b = 1 << 0,
2777 OPT_C = 1 << 1,
2778 OPT_H = 1 << 2,
2779 OPT_l = 1 << 3,
2780 OPT_S = 1 << 4,
2781 OPT_u = 1 << 5,
2782 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2783 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002784
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002785 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002786
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002787 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002788 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002789 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002790 argc -= optind;
2791 argv += optind;
2792 if (opt & OPT_b) { // -b
2793 /* Ugly: this sector size is really per device,
2794 so cannot be combined with multiple disks,
2795 and the same goes for the C/H/S options.
2796 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002797 if (sector_size != 512 && sector_size != 1024
2798 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002799 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002800 sector_offset = 2;
2801 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002803 if (user_heads <= 0 || user_heads >= 256)
2804 user_heads = 0;
2805 if (user_sectors <= 0 || user_sectors >= 64)
2806 user_sectors = 0;
2807 if (opt & OPT_u)
2808 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809
Denis Vlasenko834410a2006-11-29 12:00:28 +00002810#if ENABLE_FEATURE_FDISK_WRITABLE
2811 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002812 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002813#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002814 type_open = O_RDONLY;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002815 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002816 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002817 do {
2818 trydev(*argv, 1);
2819 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002820 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002821 /* we don't have device names, */
2822 /* use /proc/partitions instead */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002823 tryprocpt();
2824 }
2825 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002826#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002827 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002828#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002829
Denis Vlasenko834410a2006-11-29 12:00:28 +00002830#if ENABLE_FEATURE_FDISK_BLKSIZE
2831 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002832 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833
2834 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002835 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002836 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002837 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002838 unsigned long long size;
2839 fd = xopen(argv[j], O_RDONLY);
2840 size = bb_BLKGETSIZE_sectors() / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002842 if (argc == 1)
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002843 printf("%lld\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002844 else
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002845 printf("%s: %lld\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846 }
2847 return 0;
2848 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002849#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850
Denis Vlasenko834410a2006-11-29 12:00:28 +00002851#if ENABLE_FEATURE_FDISK_WRITABLE
2852 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002853 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854
Denis Vlasenko834410a2006-11-29 12:00:28 +00002855 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 get_boot(fdisk);
2857
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002858 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002860 printf("Detected an OSF/1 disklabel on %s, entering "
2861 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002862 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002863 /*Why do we do this? It seems to be counter-intuitive*/
2864 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865 /* If we return we may want to make an empty DOS label? */
2866 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867
2868 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002869 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002870 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002871 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 switch (c) {
2873 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002874 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002875 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002876 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002877 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002879 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002881 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882 else
2883 unknown_command(c);
2884 break;
2885 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002886 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002887 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002888 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002889 if (read_maybe_empty("Please enter the name of the "
2890 "new boot file: ") == '\n')
2891 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892 else
2893 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002894 }
2895#if ENABLE_FEATURE_OSF_LABEL
2896 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002897 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898#endif
2899 break;
2900 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002901 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002903 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002904 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002905 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002906 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002908 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 else
2910 unknown_command(c);
2911 break;
2912 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002913 {
Eric Andersen040f4402003-07-30 08:40:37 +00002914 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002915 /* If sgi_label then don't use get_existing_partition,
2916 let the user select a partition, since
2917 get_existing_partition() only works for Linux-like
2918 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002919 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002920 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002921 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002922 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002923 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002924 if (j >= 0)
2925 delete_partition(j);
2926 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002927 break;
2928 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002929 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 create_sgiinfo();
2931 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 unknown_command(c);
2933 case 'l':
2934 list_types(get_sys_types());
2935 break;
2936 case 'm':
2937 menu();
2938 break;
2939 case 'n':
2940 new_partition();
2941 break;
2942 case 'o':
2943 create_doslabel();
2944 break;
2945 case 'p':
2946 list_table(0);
2947 break;
2948 case 'q':
2949 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002950 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951 return 0;
2952 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002953#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002954 create_sunlabel();
2955#endif
2956 break;
2957 case 't':
2958 change_sysid();
2959 break;
2960 case 'u':
2961 change_units();
2962 break;
2963 case 'v':
2964 verify();
2965 break;
2966 case 'w':
2967 write_table(); /* does not return */
2968 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002969#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002970 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002971 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002972 printf("\n\tSorry, no experts menu for SGI "
2973 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002974 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002975 xselect();
2976 break;
2977#endif
2978 default:
2979 unknown_command(c);
2980 menu();
2981 }
2982 }
2983 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002984#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985}