blob: dcfae96f5d118aefd62cf120c660c9d5d1b8dee9 [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);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000131#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000132static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000133#else
134static int get_boot(void);
135#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000136
137#define PLURAL 0
138#define SINGULAR 1
139
Denis Vlasenko28703012006-12-19 20:32:02 +0000140static unsigned get_start_sect(const struct partition *p);
141static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000142
143/*
144 * per partition table entry data
145 *
146 * The four primary partitions have the same sectorbuffer (MBRbuffer)
147 * and have NULL ext_pointer.
148 * Each logical partition table entry has two pointers, one for the
149 * partition and one link to the next one.
150 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000151struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000152 struct partition *part_table; /* points into sectorbuffer */
153 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000154 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000155 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000156#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000157 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000158#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000159};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000160
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000161/* DOS partition types */
162
163static const char *const i386_sys_types[] = {
164 "\x00" "Empty",
165 "\x01" "FAT12",
166 "\x04" "FAT16 <32M",
167 "\x05" "Extended", /* DOS 3.3+ extended partition */
168 "\x06" "FAT16", /* DOS 16-bit >=32M */
169 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
170 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
171 "\x0b" "Win95 FAT32",
172 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
173 "\x0e" "Win95 FAT16 (LBA)",
174 "\x0f" "Win95 Ext'd (LBA)",
175 "\x11" "Hidden FAT12",
176 "\x12" "Compaq diagnostics",
177 "\x14" "Hidden FAT16 <32M",
178 "\x16" "Hidden FAT16",
179 "\x17" "Hidden HPFS/NTFS",
180 "\x1b" "Hidden Win95 FAT32",
181 "\x1c" "Hidden W95 FAT32 (LBA)",
182 "\x1e" "Hidden W95 FAT16 (LBA)",
183 "\x3c" "Part.Magic recovery",
184 "\x41" "PPC PReP Boot",
185 "\x42" "SFS",
186 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
187 "\x80" "Old Minix", /* Minix 1.4a and earlier */
188 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
189 "\x82" "Linux swap", /* also Solaris */
190 "\x83" "Linux",
191 "\x84" "OS/2 hidden C: drive",
192 "\x85" "Linux extended",
193 "\x86" "NTFS volume set",
194 "\x87" "NTFS volume set",
195 "\x8e" "Linux LVM",
196 "\x9f" "BSD/OS", /* BSDI */
197 "\xa0" "Thinkpad hibernation",
198 "\xa5" "FreeBSD", /* various BSD flavours */
199 "\xa6" "OpenBSD",
200 "\xa8" "Darwin UFS",
201 "\xa9" "NetBSD",
202 "\xab" "Darwin boot",
203 "\xb7" "BSDI fs",
204 "\xb8" "BSDI swap",
205 "\xbe" "Solaris boot",
206 "\xeb" "BeOS fs",
207 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
208 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
209 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
210 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
211 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
212 autodetect using persistent
213 superblock */
214#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
215 "\x02" "XENIX root",
216 "\x03" "XENIX usr",
217 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
218 "\x09" "AIX bootable", /* AIX data or Coherent */
219 "\x10" "OPUS",
220 "\x18" "AST SmartSleep",
221 "\x24" "NEC DOS",
222 "\x39" "Plan 9",
223 "\x40" "Venix 80286",
224 "\x4d" "QNX4.x",
225 "\x4e" "QNX4.x 2nd part",
226 "\x4f" "QNX4.x 3rd part",
227 "\x50" "OnTrack DM",
228 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
229 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
230 "\x53" "OnTrack DM6 Aux3",
231 "\x54" "OnTrackDM6",
232 "\x55" "EZ-Drive",
233 "\x56" "Golden Bow",
234 "\x5c" "Priam Edisk",
235 "\x61" "SpeedStor",
236 "\x64" "Novell Netware 286",
237 "\x65" "Novell Netware 386",
238 "\x70" "DiskSecure Multi-Boot",
239 "\x75" "PC/IX",
240 "\x93" "Amoeba",
241 "\x94" "Amoeba BBT", /* (bad block table) */
242 "\xa7" "NeXTSTEP",
243 "\xbb" "Boot Wizard hidden",
244 "\xc1" "DRDOS/sec (FAT-12)",
245 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
246 "\xc6" "DRDOS/sec (FAT-16)",
247 "\xc7" "Syrinx",
248 "\xda" "Non-FS data",
249 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
250 Concurrent DOS or CTOS */
251 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
252 "\xdf" "BootIt", /* BootIt EMBRM */
253 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
254 extended partition */
255 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
256 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
257 partition < 1024 cyl. */
258 "\xf1" "SpeedStor",
259 "\xf4" "SpeedStor", /* SpeedStor large partition */
260 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
261 "\xff" "BBT", /* Xenix Bad Block Table */
262#endif
263 NULL
264};
265
266
267/* Globals */
268
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000269struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000270 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000271
272 const char *disk_device;
273 int fd; /* the disk */
274 int g_partitions; // = 4; /* maximum partition + 1 */
275 unsigned units_per_sector; // = 1;
276 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
277 unsigned user_set_sector_size;
278 unsigned sector_offset; // = 1;
279 unsigned g_heads, g_sectors, g_cylinders;
280 enum label_type current_label_type;
281 smallint display_in_cyl_units; // = 1;
282#if ENABLE_FEATURE_OSF_LABEL
283 smallint possibly_osf_label;
284#endif
285
286 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000287 char line_buffer[80];
288 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000289 /* Raw disk label. For DOS-type partition tables the MBR,
290 * with descriptions of the primary partitions. */
291 char MBRbuffer[MAX_SECTOR_SIZE];
292 /* Partition tables */
293 struct pte ptes[MAXIMUM_PARTS];
294};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000295#define G (*ptr_to_globals)
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000296#define line_ptr (G.line_ptr)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000297#define disk_device (G.disk_device )
298#define fd (G.fd )
299#define g_partitions (G.g_partitions )
300#define units_per_sector (G.units_per_sector )
301#define sector_size (G.sector_size )
302#define user_set_sector_size (G.user_set_sector_size)
303#define sector_offset (G.sector_offset )
304#define g_heads (G.g_heads )
305#define g_sectors (G.g_sectors )
306#define g_cylinders (G.g_cylinders )
307#define current_label_type (G.current_label_type )
308#define display_in_cyl_units (G.display_in_cyl_units)
309#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000310#define listingbuf (G.listingbuf)
311#define line_buffer (G.line_buffer)
312#define partname_buffer (G.partname_buffer)
313#define MBRbuffer (G.MBRbuffer)
314#define ptes (G.ptes)
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000315#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000316 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000317 sector_size = DEFAULT_SECTOR_SIZE; \
318 sector_offset = 1; \
319 g_partitions = 4; \
320 display_in_cyl_units = 1; \
321 units_per_sector = 1; \
322} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000323
Denis Vlasenkobd852072007-03-19 14:43:38 +0000324
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000325/* TODO: move to libbb? */
326static ullong bb_BLKGETSIZE_sectors(void)
327{
328 uint64_t v64;
329 unsigned long longsectors;
330
331 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
332 /* got bytes, convert to 512 byte sectors */
333 return (v64 >> 9);
334 }
335 /* Needs temp of type long */
336 if (ioctl(fd, BLKGETSIZE, &longsectors))
337 longsectors = 0;
338 return longsectors;
339}
340
341
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000342#define IS_EXTENDED(i) \
343 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
344
345#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
346
347#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
348
349#define pt_offset(b, n) \
350 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
351
352#define sector(s) ((s) & 0x3f)
353
354#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
355
356#define hsc2sector(h,s,c) \
357 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
358
359#define set_hsc(h,s,c,sector) \
360 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000361 s = sector % g_sectors + 1; \
362 sector /= g_sectors; \
363 h = sector % g_heads; \
364 sector /= g_heads; \
365 c = sector & 0xff; \
366 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000367 } while (0)
368
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000369#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000370/* read line; return 0 or first printable char */
371static int
372read_line(const char *prompt)
373{
374 int sz;
375
376 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
377 if (sz <= 0)
378 exit(0); /* Ctrl-D or Ctrl-C */
379
380 if (line_buffer[sz-1] == '\n')
381 line_buffer[--sz] = '\0';
382
383 line_ptr = line_buffer;
384 while (*line_ptr && !isgraph(*line_ptr))
385 line_ptr++;
386 return *line_ptr;
387}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000388#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000389
Denis Vlasenkobd852072007-03-19 14:43:38 +0000390/*
391 * return partition name - uses static storage
392 */
393static const char *
394partname(const char *dev, int pno, int lth)
395{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000396 const char *p;
397 int w, wp;
398 int bufsiz;
399 char *bufp;
400
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000401 bufp = partname_buffer;
402 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000403
404 w = strlen(dev);
405 p = "";
406
407 if (isdigit(dev[w-1]))
408 p = "p";
409
410 /* devfs kludge - note: fdisk partition names are not supposed
411 to equal kernel names, so there is no reason to do this */
412 if (strcmp(dev + w - 4, "disc") == 0) {
413 w -= 4;
414 p = "part";
415 }
416
417 wp = strlen(p);
418
419 if (lth) {
420 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
421 lth-wp-2, w, dev, p, pno);
422 } else {
423 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
424 }
425 return bufp;
426}
427
Denis Vlasenko834410a2006-11-29 12:00:28 +0000428#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000429static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000430set_all_unchanged(void)
431{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000432 int i;
433
434 for (i = 0; i < MAXIMUM_PARTS; i++)
435 ptes[i].changed = 0;
436}
437
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000438static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000439set_changed(int i)
440{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000441 ptes[i].changed = 1;
442}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000443#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000444
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000445static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000446get_part_table(int i)
447{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000448 return ptes[i].part_table;
449}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000450
451static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000452str_units(int n)
453{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000454 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000455 return display_in_cyl_units ? "cylinder" : "sector";
456 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000457}
458
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000459static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000460valid_part_table_flag(const char *mbuffer)
461{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000462 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000463}
464
Denis Vlasenko834410a2006-11-29 12:00:28 +0000465#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000466static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000467write_part_table_flag(char *b)
468{
469 b[510] = 0x55;
470 b[511] = 0xaa;
471}
472
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000473static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000474read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000475{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000476 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000477 return *line_ptr;
478}
479
480static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000481read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000482{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000483 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000484 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000485 line_ptr[0] = '\n';
486 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000487 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000488 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000489}
490
491static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000492read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000493{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000494 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000495 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000496 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000497 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000498 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000499 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000500 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000501 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000502 if (v > 0xff)
503 /* Bad input also triggers this */
504 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000505 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000506 }
507}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000508#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000509
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000510#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000511
512typedef struct {
513 unsigned char info[128]; /* Informative text string */
514 unsigned char spare0[14];
515 struct sun_info {
516 unsigned char spare1;
517 unsigned char id;
518 unsigned char spare2;
519 unsigned char flags;
520 } infos[8];
521 unsigned char spare1[246]; /* Boot information etc. */
522 unsigned short rspeed; /* Disk rotational speed */
523 unsigned short pcylcount; /* Physical cylinder count */
524 unsigned short sparecyl; /* extra sects per cylinder */
525 unsigned char spare2[4]; /* More magic... */
526 unsigned short ilfact; /* Interleave factor */
527 unsigned short ncyl; /* Data cylinder count */
528 unsigned short nacyl; /* Alt. cylinder count */
529 unsigned short ntrks; /* Tracks per cylinder */
530 unsigned short nsect; /* Sectors per track */
531 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000532 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000533 uint32_t start_cylinder;
534 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000535 } partitions[8];
536 unsigned short magic; /* Magic number */
537 unsigned short csum; /* Label xor'd checksum */
538} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000539#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000540STATIC_OSF void bsd_select(void);
541STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000542#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000543
Denis Vlasenko28703012006-12-19 20:32:02 +0000544#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000545static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000546fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000547{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000548 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000549}
550
Rob Landley88621d72006-08-29 19:41:06 +0000551static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000552fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000553{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000554 return (x << 24) |
555 ((x & 0xFF00) << 8) |
556 ((x & 0xFF0000) >> 8) |
557 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558}
559#endif
560
Denis Vlasenkobd852072007-03-19 14:43:38 +0000561STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000562STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000563STATIC_SGI int sgi_get_sysid(int i);
564STATIC_SGI void sgi_delete_partition(int i);
565STATIC_SGI void sgi_change_sysid(int i, int sys);
566STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000567#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000568STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000569#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000570STATIC_SGI int verify_sgi(int verbose);
571STATIC_SGI void sgi_add_partition(int n, int sys);
572STATIC_SGI void sgi_set_swappartition(int i);
573STATIC_SGI const char *sgi_get_bootfile(void);
574STATIC_SGI void sgi_set_bootfile(const char* aFile);
575STATIC_SGI void create_sgiinfo(void);
576STATIC_SGI void sgi_write_table(void);
577STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000578#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000579
Denis Vlasenkobd852072007-03-19 14:43:38 +0000580STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000581STATIC_SUN void sun_delete_partition(int i);
582STATIC_SUN void sun_change_sysid(int i, int sys);
583STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000584STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000585#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000586STATIC_SUN void sun_set_alt_cyl(void);
587STATIC_SUN void sun_set_ncyl(int cyl);
588STATIC_SUN void sun_set_xcyl(void);
589STATIC_SUN void sun_set_ilfact(void);
590STATIC_SUN void sun_set_rspeed(void);
591STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000592#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000593STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
594STATIC_SUN void verify_sun(void);
595STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000596#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597
Denis Vlasenko834410a2006-11-29 12:00:28 +0000598#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599/* start_sect and nr_sects are stored little endian on all machines */
600/* moreover, they are not aligned correctly */
601static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000602store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000603{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000604 cp[0] = val;
605 cp[1] = val >> 8;
606 cp[2] = val >> 16;
607 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000608}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000609#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000610
Denis Vlasenko834410a2006-11-29 12:00:28 +0000611static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000612read4_little_endian(const unsigned char *cp)
613{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000614 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000615}
616
Denis Vlasenko834410a2006-11-29 12:00:28 +0000617#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000618static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000619set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000620{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000621 store4_little_endian(p->start4, start_sect);
622}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000623#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624
Denis Vlasenko28703012006-12-19 20:32:02 +0000625static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000626get_start_sect(const struct partition *p)
627{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000628 return read4_little_endian(p->start4);
629}
630
Denis Vlasenko834410a2006-11-29 12:00:28 +0000631#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000632static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000633set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000634{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000635 store4_little_endian(p->size4, nr_sects);
636}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000637#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000638
Denis Vlasenko28703012006-12-19 20:32:02 +0000639static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000640get_nr_sects(const struct partition *p)
641{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000642 return read4_little_endian(p->size4);
643}
644
645/* normally O_RDWR, -l option gives O_RDONLY */
646static int type_open = O_RDWR;
647
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000648static int ext_index; /* the prime extended partition */
649static smallint listing; /* no aborts for fdisk -l */
650static smallint dos_compatible_flag = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000651#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000652//static int dos_changed;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000653static smallint nowarn; /* no warnings for fdisk -l/-s */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000654#endif
655
Denis Vlasenko834410a2006-11-29 12:00:28 +0000656static unsigned user_cylinders, user_heads, user_sectors;
657static unsigned pt_heads, pt_sectors;
658static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000659
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000660static ullong extended_offset; /* offset of link pointers */
661static ullong total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000662
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000663static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000664{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000665 if (listing) {
666 close(fd);
667 longjmp(listingbuf, 1);
668 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000669 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000670}
671
672static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000673seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000674{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000675 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000676#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000677 if (lseek64(fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000678 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000679#else
680 if (secno > MAXINT(off_t)
681 || lseek(fd, (off_t)secno, SEEK_SET) == (off_t) -1
682 ) {
683 fdisk_fatal(unable_to_seek);
684 }
685#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000686}
687
Denis Vlasenko834410a2006-11-29 12:00:28 +0000688#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000689static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000690write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000691{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692 seek_sector(secno);
693 if (write(fd, buf, sector_size) != sector_size)
694 fdisk_fatal(unable_to_write);
695}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000696#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000697
698/* Allocate a buffer and read a partition table sector */
699static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000700read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000701{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000702 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000703 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000704 seek_sector(offset);
705 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
706 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000707#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000708 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000709#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000710 pe->part_table = pe->ext_pointer = NULL;
711}
712
Denis Vlasenko834410a2006-11-29 12:00:28 +0000713static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000714get_partition_start(const struct pte *pe)
715{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000716 return pe->offset + get_start_sect(pe->part_table);
717}
718
Denis Vlasenko834410a2006-11-29 12:00:28 +0000719#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000720/*
721 * Avoid warning about DOS partitions when no DOS partition was changed.
722 * Here a heuristic "is probably dos partition".
723 * We might also do the opposite and warn in all cases except
724 * for "is probably nondos partition".
725 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000726#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000727static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000728is_dos_partition(int t)
729{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000730 return (t == 1 || t == 4 || t == 6 ||
731 t == 0x0b || t == 0x0c || t == 0x0e ||
732 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
733 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
734 t == 0xc1 || t == 0xc4 || t == 0xc6);
735}
Denis Vlasenko89398812008-01-25 20:18:46 +0000736#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000737
738static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000739menu(void)
740{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000741 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000742 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000743 puts("a\ttoggle a read only flag"); /* sun */
744 puts("b\tedit bsd disklabel");
745 puts("c\ttoggle the mountable flag"); /* sun */
746 puts("d\tdelete a partition");
747 puts("l\tlist known partition types");
748 puts("n\tadd a new partition");
749 puts("o\tcreate a new empty DOS partition table");
750 puts("p\tprint the partition table");
751 puts("q\tquit without saving changes");
752 puts("s\tcreate a new empty Sun disklabel"); /* sun */
753 puts("t\tchange a partition's system id");
754 puts("u\tchange display/entry units");
755 puts("v\tverify the partition table");
756 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000757#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000758 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000760 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000761 puts("a\tselect bootable partition"); /* sgi flavour */
762 puts("b\tedit bootfile entry"); /* sgi */
763 puts("c\tselect sgi swap partition"); /* sgi flavour */
764 puts("d\tdelete a partition");
765 puts("l\tlist known partition types");
766 puts("n\tadd a new partition");
767 puts("o\tcreate a new empty DOS partition table");
768 puts("p\tprint the partition table");
769 puts("q\tquit without saving changes");
770 puts("s\tcreate a new empty Sun disklabel"); /* sun */
771 puts("t\tchange a partition's system id");
772 puts("u\tchange display/entry units");
773 puts("v\tverify the partition table");
774 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000775 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000776 puts("o\tcreate a new empty DOS partition table");
777 puts("q\tquit without saving changes");
778 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000779 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000780 puts("a\ttoggle a bootable flag");
781 puts("b\tedit bsd disklabel");
782 puts("c\ttoggle the dos compatibility flag");
783 puts("d\tdelete a partition");
784 puts("l\tlist known partition types");
785 puts("n\tadd a new partition");
786 puts("o\tcreate a new empty DOS partition table");
787 puts("p\tprint the partition table");
788 puts("q\tquit without saving changes");
789 puts("s\tcreate a new empty Sun disklabel"); /* sun */
790 puts("t\tchange a partition's system id");
791 puts("u\tchange display/entry units");
792 puts("v\tverify the partition table");
793 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000794#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000795 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000796#endif
797 }
798}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000799#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000800
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000801
Denis Vlasenko834410a2006-11-29 12:00:28 +0000802#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000803static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000804xmenu(void)
805{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000806 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000807 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000808 puts("a\tchange number of alternate cylinders"); /*sun*/
809 puts("c\tchange number of cylinders");
810 puts("d\tprint the raw data in the partition table");
811 puts("e\tchange number of extra sectors per cylinder");/*sun*/
812 puts("h\tchange number of heads");
813 puts("i\tchange interleave factor"); /*sun*/
814 puts("o\tchange rotation speed (rpm)"); /*sun*/
815 puts("p\tprint the partition table");
816 puts("q\tquit without saving changes");
817 puts("r\treturn to main menu");
818 puts("s\tchange number of sectors/track");
819 puts("v\tverify the partition table");
820 puts("w\twrite table to disk and exit");
821 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000822 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000823 puts("b\tmove beginning of data in a partition"); /* !sun */
824 puts("c\tchange number of cylinders");
825 puts("d\tprint the raw data in the partition table");
826 puts("e\tlist extended partitions"); /* !sun */
827 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
828 puts("h\tchange number of heads");
829 puts("p\tprint the partition table");
830 puts("q\tquit without saving changes");
831 puts("r\treturn to main menu");
832 puts("s\tchange number of sectors/track");
833 puts("v\tverify the partition table");
834 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000835 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000836 puts("b\tmove beginning of data in a partition"); /* !sun */
837 puts("c\tchange number of cylinders");
838 puts("d\tprint the raw data in the partition table");
839 puts("e\tlist extended partitions"); /* !sun */
840 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
841 puts("h\tchange number of heads");
842 puts("p\tprint the partition table");
843 puts("q\tquit without saving changes");
844 puts("r\treturn to main menu");
845 puts("s\tchange number of sectors/track");
846 puts("v\tverify the partition table");
847 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000848 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000849 puts("b\tmove beginning of data in a partition"); /* !sun */
850 puts("c\tchange number of cylinders");
851 puts("d\tprint the raw data in the partition table");
852 puts("e\tlist extended partitions"); /* !sun */
853 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000854#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000855 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000856#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000857 puts("h\tchange number of heads");
858 puts("p\tprint the partition table");
859 puts("q\tquit without saving changes");
860 puts("r\treturn to main menu");
861 puts("s\tchange number of sectors/track");
862 puts("v\tverify the partition table");
863 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000864 }
865}
866#endif /* ADVANCED mode */
867
Denis Vlasenko834410a2006-11-29 12:00:28 +0000868#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000869static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000870get_sys_types(void)
871{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000872 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000873 LABEL_IS_SUN ? sun_sys_types :
874 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000875 i386_sys_types);
876}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000877#else
878#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000879#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000880
Denis Vlasenkobd852072007-03-19 14:43:38 +0000881static const char *
882partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883{
884 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000885 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000886
Denis Vlasenkobd852072007-03-19 14:43:38 +0000887 for (i = 0; types[i]; i++)
888 if ((unsigned char)types[i][0] == type)
889 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000890
Denis Vlasenkobd852072007-03-19 14:43:38 +0000891 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000892}
893
894
Denis Vlasenko834410a2006-11-29 12:00:28 +0000895#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000896static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000897get_sysid(int i)
898{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000899 return LABEL_IS_SUN ? sunlabel->infos[i].id :
900 (LABEL_IS_SGI ? sgi_get_sysid(i) :
901 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902}
903
Denis Vlasenkobd852072007-03-19 14:43:38 +0000904static void
905list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000906{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000907 enum { COLS = 3 };
908
909 unsigned last[COLS];
910 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000911 int i;
912
Denis Vlasenkobd852072007-03-19 14:43:38 +0000913 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000914
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000915 done = 0;
916 for (i = COLS-1; i >= 0; i--) {
917 done += (size + i - done) / (i + 1);
918 last[COLS-1 - i] = done;
919 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000920
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000921 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000922 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000923 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000924 (unsigned char)sys[next][0],
925 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000926 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000927 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000928 i = 0;
929 next = ++done;
930 }
931 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000932 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000934#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935
936static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000937is_cleared_partition(const struct partition *p)
938{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000939 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
940 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
941 get_start_sect(p) || get_nr_sects(p));
942}
943
944static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000945clear_partition(struct partition *p)
946{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947 if (!p)
948 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000949 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000950}
951
Denis Vlasenko834410a2006-11-29 12:00:28 +0000952#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000953static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000954set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000955{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000957 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000958
959 if (doext) {
960 p = ptes[i].ext_pointer;
961 offset = extended_offset;
962 } else {
963 p = ptes[i].part_table;
964 offset = ptes[i].offset;
965 }
966 p->boot_ind = 0;
967 p->sys_ind = sysid;
968 set_start_sect(p, start - offset);
969 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000970 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
971 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000972 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000973 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
974 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
976 ptes[i].changed = 1;
977}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000978#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000979
980static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000981warn_geometry(void)
982{
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000983 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000984 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000985
Denis Vlasenkobd852072007-03-19 14:43:38 +0000986 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000987 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000988 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000989 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000990 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000991 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000992 printf(" cylinders");
993 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000994#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000995 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000996#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000997 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000998 return 1;
999}
1000
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001001static void
1002update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001003{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001004 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001005
1006 if (display_in_cyl_units && cyl_units)
1007 units_per_sector = cyl_units;
1008 else
1009 units_per_sector = 1; /* in sectors */
1010}
1011
Denis Vlasenko834410a2006-11-29 12:00:28 +00001012#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001014warn_cylinders(void)
1015{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001016 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001017 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018"The number of cylinders for this disk is set to %d.\n"
1019"There is nothing wrong with that, but this is larger than 1024,\n"
1020"and could in certain setups cause problems with:\n"
1021"1) software that runs at boot time (e.g., old versions of LILO)\n"
1022"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001023" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001024 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001025}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001026#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001027
1028static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001029read_extended(int ext)
1030{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001031 int i;
1032 struct pte *pex;
1033 struct partition *p, *q;
1034
1035 ext_index = ext;
1036 pex = &ptes[ext];
1037 pex->ext_pointer = pex->part_table;
1038
1039 p = pex->part_table;
1040 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001041 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042 return;
1043 }
1044
Rob Landleyb73451d2006-02-24 16:29:00 +00001045 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001046 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001048 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001049 /* This is not a Linux restriction, but
1050 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001051 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001052 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001053#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001054 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001055 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001057#endif
1058 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001059 return;
1060 }
1061
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001062 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001063
1064 if (!extended_offset)
1065 extended_offset = get_start_sect(p);
1066
1067 q = p = pt_offset(pe->sectorbuffer, 0);
1068 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001069 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001070 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001071 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001072 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001073 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001074 else
1075 pe->ext_pointer = p;
1076 } else if (p->sys_ind) {
1077 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001078 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001080 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001081 else
1082 pe->part_table = p;
1083 }
1084 }
1085
1086 /* very strange code here... */
1087 if (!pe->part_table) {
1088 if (q != pe->ext_pointer)
1089 pe->part_table = q;
1090 else
1091 pe->part_table = q + 1;
1092 }
1093 if (!pe->ext_pointer) {
1094 if (q != pe->part_table)
1095 pe->ext_pointer = q;
1096 else
1097 pe->ext_pointer = q + 1;
1098 }
1099
1100 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001101 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001102 }
1103
Denis Vlasenko834410a2006-11-29 12:00:28 +00001104#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001105 /* remove empty links */
1106 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001107 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001108 struct pte *pe = &ptes[i];
1109
Denis Vlasenkobd852072007-03-19 14:43:38 +00001110 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001111 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001112 ) {
1113 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001114 delete_partition(i);
1115 goto remove; /* numbering changed */
1116 }
1117 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001118#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001119}
1120
Denis Vlasenko834410a2006-11-29 12:00:28 +00001121#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001123create_doslabel(void)
1124{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125 int i;
1126
Denis Vlasenkobd852072007-03-19 14:43:38 +00001127 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001128
1129 current_label_type = label_dos;
1130
Denis Vlasenko834410a2006-11-29 12:00:28 +00001131#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 possibly_osf_label = 0;
1133#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001134 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001135
1136 for (i = 510-64; i < 510; i++)
1137 MBRbuffer[i] = 0;
1138 write_part_table_flag(MBRbuffer);
1139 extended_offset = 0;
1140 set_all_unchanged();
1141 set_changed(0);
1142 get_boot(create_empty_dos);
1143}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001144#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001145
1146static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001147get_sectorsize(void)
1148{
Rob Landley736e5252006-02-25 03:36:00 +00001149 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001150 int arg;
1151 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1152 sector_size = arg;
1153 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001154 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001155 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001156 }
1157}
1158
Rob Landley88621d72006-08-29 19:41:06 +00001159static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001160get_kernel_geometry(void)
1161{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001162 struct hd_geometry geometry;
1163
1164 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1165 kern_heads = geometry.heads;
1166 kern_sectors = geometry.sectors;
1167 /* never use geometry.cylinders - it is truncated */
1168 }
1169}
1170
1171static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001172get_partition_table_geometry(void)
1173{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001174 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001175 struct partition *p;
1176 int i, h, s, hh, ss;
1177 int first = 1;
1178 int bad = 0;
1179
Eric Andersen3496fdc2006-01-30 23:09:20 +00001180 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181 return;
1182
1183 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001184 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185 p = pt_offset(bufp, i);
1186 if (p->sys_ind != 0) {
1187 h = p->end_head + 1;
1188 s = (p->end_sector & 077);
1189 if (first) {
1190 hh = h;
1191 ss = s;
1192 first = 0;
1193 } else if (hh != h || ss != s)
1194 bad = 1;
1195 }
1196 }
1197
1198 if (!first && !bad) {
1199 pt_heads = hh;
1200 pt_sectors = ss;
1201 }
1202}
1203
Rob Landleyb73451d2006-02-24 16:29:00 +00001204static void
1205get_geometry(void)
1206{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001207 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208
1209 get_sectorsize();
1210 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001211#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212 guess_device_type();
1213#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001214 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215 kern_heads = kern_sectors = 0;
1216 pt_heads = pt_sectors = 0;
1217
1218 get_kernel_geometry();
1219 get_partition_table_geometry();
1220
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001221 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222 pt_heads ? pt_heads :
1223 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001224 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001225 pt_sectors ? pt_sectors :
1226 kern_sectors ? kern_sectors : 63;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001227 total_number_of_sectors = bb_BLKGETSIZE_sectors();
Eric Andersen040f4402003-07-30 08:40:37 +00001228
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229 sector_offset = 1;
1230 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001231 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001232
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001233 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1234 if (!g_cylinders)
1235 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236}
1237
1238/*
1239 * Read MBR. Returns:
1240 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1241 * 0: found or created label
1242 * 1: I/O error
1243 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001244#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1245static int get_boot(enum action what)
1246#else
1247static int get_boot(void)
1248#define get_boot(what) get_boot()
1249#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001250{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001251 int i;
1252
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001253 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001254
1255 for (i = 0; i < 4; i++) {
1256 struct pte *pe = &ptes[i];
1257
1258 pe->part_table = pt_offset(MBRbuffer, i);
1259 pe->ext_pointer = NULL;
1260 pe->offset = 0;
1261 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001262#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001263 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001264#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001265 }
1266
Denis Vlasenko834410a2006-11-29 12:00:28 +00001267#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001268 if (what == create_empty_sun && check_sun_label())
1269 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001270#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001271
1272 memset(MBRbuffer, 0, 512);
1273
Denis Vlasenko834410a2006-11-29 12:00:28 +00001274#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001275 if (what == create_empty_dos)
1276 goto got_dos_table; /* skip reading disk */
1277
Denis Vlasenkobd852072007-03-19 14:43:38 +00001278 fd = open(disk_device, type_open);
1279 if (fd < 0) {
1280 fd = open(disk_device, O_RDONLY);
1281 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001282 if (what == try_only)
1283 return 1;
1284 fdisk_fatal(unable_to_open);
1285 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001286 printf("You will not be able to write "
1287 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288 }
1289
1290 if (512 != read(fd, MBRbuffer, 512)) {
1291 if (what == try_only)
1292 return 1;
1293 fdisk_fatal(unable_to_read);
1294 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001295#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001296 fd = open(disk_device, O_RDONLY);
1297 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001298 return 1;
1299 if (512 != read(fd, MBRbuffer, 512))
1300 return 1;
1301#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001302
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001303 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001304
1305 update_units();
1306
Denis Vlasenko834410a2006-11-29 12:00:28 +00001307#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001308 if (check_sun_label())
1309 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001310#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001311
Denis Vlasenko834410a2006-11-29 12:00:28 +00001312#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 if (check_sgi_label())
1314 return 0;
1315#endif
1316
Denis Vlasenko834410a2006-11-29 12:00:28 +00001317#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001318 if (check_aix_label())
1319 return 0;
1320#endif
1321
Denis Vlasenko834410a2006-11-29 12:00:28 +00001322#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323 if (check_osf_label()) {
1324 possibly_osf_label = 1;
1325 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001326 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 return 0;
1328 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001329 printf("This disk has both DOS and BSD magic.\n"
1330 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001331 }
1332#endif
1333
Denis Vlasenko834410a2006-11-29 12:00:28 +00001334#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001335 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001336#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337
1338 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001339#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001340 return -1;
1341#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001342 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001344 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001345 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001346 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001348#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001349 create_sunlabel();
1350#endif
1351#else
1352 create_doslabel();
1353#endif
1354 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 case try_only:
1356 return -1;
1357 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001358#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001360#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001361 break;
1362 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001363 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001364 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001365#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001366 }
1367
Denis Vlasenko834410a2006-11-29 12:00:28 +00001368#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001370#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001371 warn_geometry();
1372
1373 for (i = 0; i < 4; i++) {
1374 struct pte *pe = &ptes[i];
1375
Rob Landleyb73451d2006-02-24 16:29:00 +00001376 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001377 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001378 printf("Ignoring extra extended "
1379 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380 else
1381 read_extended(i);
1382 }
1383 }
1384
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001385 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 struct pte *pe = &ptes[i];
1387
1388 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001389 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1390 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001391 pe->sectorbuffer[510],
1392 pe->sectorbuffer[511],
1393 i + 1);
1394#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001396#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 }
1398 }
1399
1400 return 0;
1401}
1402
Denis Vlasenko834410a2006-11-29 12:00:28 +00001403#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404/*
1405 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1406 * If the user hits Enter, DFLT is returned.
1407 * Answers like +10 are interpreted as offsets from BASE.
1408 *
1409 * There is no default if DFLT is not between LOW and HIGH.
1410 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001411static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001412read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001414 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001415 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001416 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001417
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001418 if (dflt < low || dflt > high) {
1419 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001421 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001422
1423 while (1) {
1424 int use_default = default_ok;
1425
1426 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001427 do {
1428 printf(fmt, mesg, low, high, dflt);
1429 read_maybe_empty("");
1430 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1431 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432
Eric Andersen84bdea82004-05-19 10:49:17 +00001433 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001434 int minus = (*line_ptr == '-');
1435 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001436
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001437 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001438
Rob Landleyb73451d2006-02-24 16:29:00 +00001439 while (isdigit(*++line_ptr))
1440 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001441
Rob Landleyb73451d2006-02-24 16:29:00 +00001442 switch (*line_ptr) {
1443 case 'c':
1444 case 'C':
1445 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001446 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001447 break;
1448 case 'K':
1449 absolute = 1024;
1450 break;
1451 case 'k':
1452 absolute = 1000;
1453 break;
1454 case 'm':
1455 case 'M':
1456 absolute = 1000000;
1457 break;
1458 case 'g':
1459 case 'G':
1460 absolute = 1000000000;
1461 break;
1462 default:
1463 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001464 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001465 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001466 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001467 unsigned long unit;
1468
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001469 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001470 unit = sector_size * units_per_sector;
1471 bytes += unit/2; /* round */
1472 bytes /= unit;
1473 i = bytes;
1474 }
1475 if (minus)
1476 i = -i;
1477 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001478 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001479 i = atoi(line_ptr);
1480 while (isdigit(*line_ptr)) {
1481 line_ptr++;
1482 use_default = 0;
1483 }
1484 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001485 if (use_default) {
1486 i = dflt;
1487 printf("Using default value %u\n", i);
1488 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 if (i >= low && i <= high)
1490 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001491 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001492 }
1493 return i;
1494}
1495
Rob Landleyb73451d2006-02-24 16:29:00 +00001496static int
1497get_partition(int warn, int max)
1498{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001499 struct pte *pe;
1500 int i;
1501
Denis Vlasenkobd852072007-03-19 14:43:38 +00001502 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001503 pe = &ptes[i];
1504
1505 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001506 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1507 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1508 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1509 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001510 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001511 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001512 }
1513 return i;
1514}
1515
1516static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001517get_existing_partition(int warn, int max)
1518{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001519 int pno = -1;
1520 int i;
1521
1522 for (i = 0; i < max; i++) {
1523 struct pte *pe = &ptes[i];
1524 struct partition *p = pe->part_table;
1525
1526 if (p && !is_cleared_partition(p)) {
1527 if (pno >= 0)
1528 goto not_unique;
1529 pno = i;
1530 }
1531 }
1532 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001533 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001534 return pno;
1535 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001536 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001537 return -1;
1538
1539 not_unique:
1540 return get_partition(warn, max);
1541}
1542
1543static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001544get_nonexisting_partition(int warn, int max)
1545{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001546 int pno = -1;
1547 int i;
1548
1549 for (i = 0; i < max; i++) {
1550 struct pte *pe = &ptes[i];
1551 struct partition *p = pe->part_table;
1552
1553 if (p && is_cleared_partition(p)) {
1554 if (pno >= 0)
1555 goto not_unique;
1556 pno = i;
1557 }
1558 }
1559 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001560 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001561 return pno;
1562 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001563 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001564 return -1;
1565
1566 not_unique:
1567 return get_partition(warn, max);
1568}
1569
1570
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001571static void
1572change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001573{
1574 display_in_cyl_units = !display_in_cyl_units;
1575 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001576 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 str_units(PLURAL));
1578}
1579
1580static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001581toggle_active(int i)
1582{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001583 struct pte *pe = &ptes[i];
1584 struct partition *p = pe->part_table;
1585
Rob Landleyb73451d2006-02-24 16:29:00 +00001586 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001587 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001588 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1589 pe->changed = 1;
1590}
1591
1592static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001593toggle_dos_compatibility_flag(void)
1594{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001595 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001597 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001598 printf("DOS Compatibility flag is set\n");
1599 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001600 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001601 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001602 }
1603}
1604
1605static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001606delete_partition(int i)
1607{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001608 struct pte *pe = &ptes[i];
1609 struct partition *p = pe->part_table;
1610 struct partition *q = pe->ext_pointer;
1611
1612/* Note that for the fifth partition (i == 4) we don't actually
1613 * decrement partitions.
1614 */
1615
1616 if (warn_geometry())
1617 return; /* C/H/S not set */
1618 pe->changed = 1;
1619
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001620 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001621 sun_delete_partition(i);
1622 return;
1623 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001624 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001625 sgi_delete_partition(i);
1626 return;
1627 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001628
1629 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001630 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001631 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001632 ptes[ext_index].ext_pointer = NULL;
1633 extended_offset = 0;
1634 }
1635 clear_partition(p);
1636 return;
1637 }
1638
1639 if (!q->sys_ind && i > 4) {
1640 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001641 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001642 --i;
1643 clear_partition(ptes[i].ext_pointer);
1644 ptes[i].changed = 1;
1645 } else {
1646 /* not the last one - further ones will be moved down */
1647 if (i > 4) {
1648 /* delete this link in the chain */
1649 p = ptes[i-1].ext_pointer;
1650 *p = *q;
1651 set_start_sect(p, get_start_sect(q));
1652 set_nr_sects(p, get_nr_sects(q));
1653 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001654 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001655 /* the first logical in a longer chain */
1656 pe = &ptes[5];
1657
1658 if (pe->part_table) /* prevent SEGFAULT */
1659 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001660 get_partition_start(pe) -
1661 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001662 pe->offset = extended_offset;
1663 pe->changed = 1;
1664 }
1665
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001666 if (g_partitions > 5) {
1667 g_partitions--;
1668 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001669 ptes[i] = ptes[i+1];
1670 i++;
1671 }
1672 } else
1673 /* the only logical: clear only */
1674 clear_partition(ptes[i].part_table);
1675 }
1676}
1677
1678static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001679change_sysid(void)
1680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001681 int i, sys, origsys;
1682 struct partition *p;
1683
Eric Andersen040f4402003-07-30 08:40:37 +00001684 /* If sgi_label then don't use get_existing_partition,
1685 let the user select a partition, since get_existing_partition()
1686 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001687 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001688 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001689 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001690 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001691 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001692 if (i == -1)
1693 return;
1694 p = ptes[i].part_table;
1695 origsys = sys = get_sysid(i);
1696
1697 /* if changing types T to 0 is allowed, then
1698 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001699 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001700 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001701 return;
1702 }
1703 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001704 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001705
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001706 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001707 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001708 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001709 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001710 /* break; */
1711 }
1712
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001713 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001714 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001715 printf("You cannot change a partition into"
1716 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 break;
1718 }
1719 }
1720
1721 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001722#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001723 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001724 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001725 "as Whole disk (5),\n"
1726 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001727 "even Linux likes it\n\n");
1728#endif
1729#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001730 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001731 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001732 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001733 (i == 8 && sys != 0)
1734 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001735 ) {
1736 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001737 "as volume header (0),\nand "
1738 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001739 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001740 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001741#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001742 if (sys == origsys)
1743 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001744 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001745 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001746 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001747 sgi_change_sysid(i, sys);
1748 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001750
Denis Vlasenkobd852072007-03-19 14:43:38 +00001751 printf("Changed system type of partition %d "
1752 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001753 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001754 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001755 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1756 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 break;
1758 }
1759 }
1760}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001761#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001762
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001763
Denis Vlasenko28703012006-12-19 20:32:02 +00001764/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001765 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1766 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1767 * Lubkin Oct. 1991). */
1768
Rob Landleyb73451d2006-02-24 16:29:00 +00001769static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001770linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001771{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001772 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001773
1774 *c = ls / spc;
1775 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001776 *h = ls / g_sectors;
1777 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001778}
1779
Rob Landleyb73451d2006-02-24 16:29:00 +00001780static void
1781check_consistency(const struct partition *p, int partition)
1782{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001783 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1784 unsigned pec, peh, pes; /* physical ending c, h, s */
1785 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1786 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001787
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001788 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001789 return; /* do not check extended partitions */
1790
1791/* physical beginning c, h, s */
1792 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1793 pbh = p->head;
1794 pbs = p->sector & 0x3f;
1795
1796/* physical ending c, h, s */
1797 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1798 peh = p->end_head;
1799 pes = p->end_sector & 0x3f;
1800
1801/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001802 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001803
1804/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001805 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806
1807/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001808 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001809 printf("Partition %d has different physical/logical "
1810 "beginnings (non-Linux?):\n", partition + 1);
1811 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1812 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 }
1814
1815/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001816 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001817 printf("Partition %d has different physical/logical "
1818 "endings:\n", partition + 1);
1819 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1820 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001821 }
1822
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001823/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001824 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001825 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001826 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001827 }
1828}
1829
1830static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001831list_disk_geometry(void)
1832{
Eric Andersen040f4402003-07-30 08:40:37 +00001833 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001834 long megabytes = bytes/1000000;
1835
1836 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001837 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001838 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001839 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001840 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001841 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001842 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001843 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001844 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001845 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001846 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001847 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001848 str_units(PLURAL),
1849 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001850}
1851
1852/*
1853 * Check whether partition entries are ordered by their starting positions.
1854 * Return 0 if OK. Return i if partition i should have been earlier.
1855 * Two separate checks: primary and logical partitions.
1856 */
1857static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001858wrong_p_order(int *prev)
1859{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001860 const struct pte *pe;
1861 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001862 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001863 int i, last_i = 0;
1864
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001865 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001866 if (i == 4) {
1867 last_i = 4;
1868 last_p_start_pos = 0;
1869 }
1870 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001871 p = pe->part_table;
1872 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873 p_start_pos = get_partition_start(pe);
1874
1875 if (last_p_start_pos > p_start_pos) {
1876 if (prev)
1877 *prev = last_i;
1878 return i;
1879 }
1880
1881 last_p_start_pos = p_start_pos;
1882 last_i = i;
1883 }
1884 }
1885 return 0;
1886}
1887
Denis Vlasenko834410a2006-11-29 12:00:28 +00001888#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001889/*
1890 * Fix the chain of logicals.
1891 * extended_offset is unchanged, the set of sectors used is unchanged
1892 * The chain is sorted so that sectors increase, and so that
1893 * starting sectors increase.
1894 *
1895 * After this it may still be that cfdisk doesnt like the table.
1896 * (This is because cfdisk considers expanded parts, from link to
1897 * end of partition, and these may still overlap.)
1898 * Now
1899 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1900 * may help.
1901 */
1902static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001903fix_chain_of_logicals(void)
1904{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001905 int j, oj, ojj, sj, sjj;
1906 struct partition *pj,*pjj,tmp;
1907
1908 /* Stage 1: sort sectors but leave sector of part 4 */
1909 /* (Its sector is the global extended_offset.) */
1910 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001911 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001912 oj = ptes[j].offset;
1913 ojj = ptes[j+1].offset;
1914 if (oj > ojj) {
1915 ptes[j].offset = ojj;
1916 ptes[j+1].offset = oj;
1917 pj = ptes[j].part_table;
1918 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1919 pjj = ptes[j+1].part_table;
1920 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1921 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001922 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001923 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001924 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001925 goto stage1;
1926 }
1927 }
1928
1929 /* Stage 2: sort starting sectors */
1930 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001931 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001932 pj = ptes[j].part_table;
1933 pjj = ptes[j+1].part_table;
1934 sj = get_start_sect(pj);
1935 sjj = get_start_sect(pjj);
1936 oj = ptes[j].offset;
1937 ojj = ptes[j+1].offset;
1938 if (oj+sj > ojj+sjj) {
1939 tmp = *pj;
1940 *pj = *pjj;
1941 *pjj = tmp;
1942 set_start_sect(pj, ojj+sjj-oj);
1943 set_start_sect(pjj, oj+sj-ojj);
1944 goto stage2;
1945 }
1946 }
1947
1948 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001949 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001950 ptes[j].changed = 1;
1951}
1952
1953
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001954static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001955fix_partition_table_order(void)
1956{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001957 struct pte *pei, *pek;
1958 int i,k;
1959
1960 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001961 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001962 return;
1963 }
1964
1965 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1966 /* partition i should have come earlier, move it */
1967 /* We have to move data in the MBR */
1968 struct partition *pi, *pk, *pe, pbuf;
1969 pei = &ptes[i];
1970 pek = &ptes[k];
1971
1972 pe = pei->ext_pointer;
1973 pei->ext_pointer = pek->ext_pointer;
1974 pek->ext_pointer = pe;
1975
1976 pi = pei->part_table;
1977 pk = pek->part_table;
1978
1979 memmove(&pbuf, pi, sizeof(struct partition));
1980 memmove(pi, pk, sizeof(struct partition));
1981 memmove(pk, &pbuf, sizeof(struct partition));
1982
1983 pei->changed = pek->changed = 1;
1984 }
1985
1986 if (i)
1987 fix_chain_of_logicals();
1988
1989 printf("Done.\n");
1990
1991}
1992#endif
1993
1994static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001995list_table(int xtra)
1996{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001997 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001998 int i, w;
1999
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002000 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002001 sun_list_table(xtra);
2002 return;
2003 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002004 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 sgi_list_table(xtra);
2006 return;
2007 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002008
2009 list_disk_geometry();
2010
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002011 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012 xbsd_print_disklabel(xtra);
2013 return;
2014 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002015
2016 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2017 but if the device name ends in a digit, say /dev/foo1,
2018 then the partition is called /dev/foo1p3. */
2019 w = strlen(disk_device);
2020 if (w && isdigit(disk_device[w-1]))
2021 w++;
2022 if (w < 5)
2023 w = 5;
2024
Denis Vlasenkobd852072007-03-19 14:43:38 +00002025 // 1 12345678901 12345678901 12345678901 12
2026 printf("%*s Boot Start End Blocks Id System\n",
2027 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002028
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002029 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002030 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002031 ullong psects;
2032 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002033 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002034
2035 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002036 if (!p || is_cleared_partition(p))
2037 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002038
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002039 psects = get_nr_sects(p);
2040 pblocks = psects;
2041 podd = 0;
2042
2043 if (sector_size < 1024) {
2044 pblocks /= (1024 / sector_size);
2045 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002047 if (sector_size > 1024)
2048 pblocks *= (sector_size / 1024);
2049
2050 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2051 partname(disk_device, i+1, w+2),
2052 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2053 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002054 (ullong) cround(get_partition_start(pe)), /* start */
2055 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002056 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002057 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002058 p->sys_ind, /* type id */
2059 partition_type(p->sys_ind)); /* type name */
2060
2061 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002062 }
2063
2064 /* Is partition table in disk order? It need not be, but... */
2065 /* partition table entries are not checked for correct order if this
2066 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002067 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002068 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002069 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002070 }
2071}
2072
Denis Vlasenko834410a2006-11-29 12:00:28 +00002073#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002075x_list_table(int extend)
2076{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002077 const struct pte *pe;
2078 const struct partition *p;
2079 int i;
2080
Denis Vlasenkobd852072007-03-19 14:43:38 +00002081 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002082 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002083 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002084 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002085 pe = &ptes[i];
2086 p = (extend ? pe->ext_pointer : pe->part_table);
2087 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002088 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002089 i + 1, p->boot_ind, p->head,
2090 sector(p->sector),
2091 cylinder(p->sector, p->cyl), p->end_head,
2092 sector(p->end_sector),
2093 cylinder(p->end_sector, p->end_cyl),
2094 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2095 if (p->sys_ind)
2096 check_consistency(p, i);
2097 }
2098 }
2099}
2100#endif
2101
Denis Vlasenko834410a2006-11-29 12:00:28 +00002102#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002103static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002104fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002105{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106 int i;
2107 const struct pte *pe = &ptes[0];
2108 const struct partition *p;
2109
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002110 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002112 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002113 first[i] = 0xffffffff;
2114 last[i] = 0;
2115 } else {
2116 first[i] = get_partition_start(pe);
2117 last[i] = first[i] + get_nr_sects(p) - 1;
2118 }
2119 }
2120}
2121
2122static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002123check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002124{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002125 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126
2127 real_s = sector(s) - 1;
2128 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002129 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002130 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002131 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002132 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002133 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002134 n, h + 1, g_heads);
2135 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002136 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002137 "maximum %d\n", n, s, g_sectors);
2138 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002139 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002140 "maximum %d\n", n, real_c + 1, g_cylinders);
2141 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002142 printf("Partition %d: previous sectors %llu disagrees with "
2143 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144}
2145
2146static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002147verify(void)
2148{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002150 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002151 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152 struct partition *p;
2153
2154 if (warn_geometry())
2155 return;
2156
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002157 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002158 verify_sun();
2159 return;
2160 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002161 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002162 verify_sgi(1);
2163 return;
2164 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002165
2166 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002167 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002168 struct pte *pe = &ptes[i];
2169
2170 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002171 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002172 check_consistency(p, i);
2173 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002174 printf("Warning: bad start-of-data in "
2175 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002176 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2177 last[i]);
2178 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002179 for (j = 0; j < i; j++) {
2180 if ((first[i] >= first[j] && first[i] <= last[j])
2181 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2182 printf("Warning: partition %d overlaps "
2183 "partition %d\n", j + 1, i + 1);
2184 total += first[i] >= first[j] ?
2185 first[i] : first[j];
2186 total -= last[i] <= last[j] ?
2187 last[i] : last[j];
2188 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002189 }
2190 }
2191 }
2192
2193 if (extended_offset) {
2194 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002195 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002196 get_nr_sects(pex->part_table) - 1;
2197
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002198 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002199 total++;
2200 p = ptes[i].part_table;
2201 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002202 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002203 printf("Warning: partition %d "
2204 "is empty\n", i + 1);
2205 } else if (first[i] < extended_offset || last[i] > e_last) {
2206 printf("Logical partition %d not entirely in "
2207 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 }
2210 }
2211
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002212 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002213 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002214 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002215 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002216 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002217 if (total != 0)
2218 printf("%d unallocated sectors\n", total);
2219 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002220}
2221
2222static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002223add_partition(int n, int sys)
2224{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002225 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002226 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 struct partition *p = ptes[n].part_table;
2228 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002229 ullong limit, temp;
2230 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002231 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002232
2233 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002234 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002235 return;
2236 }
2237 fill_bounds(first, last);
2238 if (n < 4) {
2239 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002240 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002241 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002243 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002244 if (extended_offset) {
2245 first[ext_index] = extended_offset;
2246 last[ext_index] = get_start_sect(q) +
2247 get_nr_sects(q) - 1;
2248 }
2249 } else {
2250 start = extended_offset + sector_offset;
2251 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2252 }
2253 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002254 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002255 first[i] = (cround(first[i]) - 1) * units_per_sector;
2256
Denis Vlasenkobd852072007-03-19 14:43:38 +00002257 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 do {
2259 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002260 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002261 int lastplusoff;
2262
2263 if (start == ptes[i].offset)
2264 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002265 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002266 if (start >= first[i] && start <= lastplusoff)
2267 start = lastplusoff + 1;
2268 }
2269 if (start > limit)
2270 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002271 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002272 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002273 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002274 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002275 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002276 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002277 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002278
2279 saved_start = start;
2280 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2281 0, mesg);
2282 if (display_in_cyl_units) {
2283 start = (start - 1) * units_per_sector;
2284 if (start < saved_start) start = saved_start;
2285 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002286 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002288 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002289 if (n > 4) { /* NOT for fifth partition */
2290 struct pte *pe = &ptes[n];
2291
2292 pe->offset = start - sector_offset;
2293 if (pe->offset == extended_offset) { /* must be corrected */
2294 pe->offset++;
2295 if (sector_offset == 1)
2296 start++;
2297 }
2298 }
2299
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002300 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002301 struct pte *pe = &ptes[i];
2302
2303 if (start < pe->offset && limit >= pe->offset)
2304 limit = pe->offset - 1;
2305 if (start < first[i] && limit >= first[i])
2306 limit = first[i] - 1;
2307 }
2308 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002309 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002310 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002311 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002312 return;
2313 }
2314 if (cround(start) == cround(limit)) {
2315 stop = limit;
2316 } else {
2317 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002318 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 str_units(SINGULAR));
2320 stop = read_int(cround(start), cround(limit), cround(limit),
2321 cround(start), mesg);
2322 if (display_in_cyl_units) {
2323 stop = stop * units_per_sector - 1;
2324 if (stop >limit)
2325 stop = limit;
2326 }
2327 }
2328
2329 set_partition(n, 0, start, stop, sys);
2330 if (n > 4)
2331 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2332
Rob Landleyb73451d2006-02-24 16:29:00 +00002333 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002334 struct pte *pe4 = &ptes[4];
2335 struct pte *pen = &ptes[n];
2336
2337 ext_index = n;
2338 pen->ext_pointer = p;
2339 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002340 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002341 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2342 pe4->ext_pointer = pe4->part_table + 1;
2343 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002344 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002345 }
2346}
2347
2348static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002349add_logical(void)
2350{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002351 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2352 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002353
Rob Landley081e3842006-08-03 20:07:35 +00002354 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002355 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2356 pe->ext_pointer = pe->part_table + 1;
2357 pe->offset = 0;
2358 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002359 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002360 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002361 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362}
2363
2364static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002365new_partition(void)
2366{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002367 int i, free_primary = 0;
2368
2369 if (warn_geometry())
2370 return;
2371
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002372 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002373 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002374 return;
2375 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002376 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002377 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378 return;
2379 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002380 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002381 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2382"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2383"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384 return;
2385 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 for (i = 0; i < 4; i++)
2388 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002389
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002390 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002391 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002392 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002393 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002394
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 if (!free_primary) {
2396 if (extended_offset)
2397 add_logical();
2398 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002399 printf("You must delete some partition and add "
2400 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002402 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002403 snprintf(line, sizeof(line),
2404 "Command action\n"
2405 " %s\n"
2406 " p primary partition (1-4)\n",
2407 (extended_offset ?
2408 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002410 c = read_nonempty(line);
2411 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412 i = get_nonexisting_partition(0, 4);
2413 if (i >= 0)
2414 add_partition(i, LINUX_NATIVE);
2415 return;
2416 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002417 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002418 add_logical();
2419 return;
2420 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002421 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002422 i = get_nonexisting_partition(0, 4);
2423 if (i >= 0)
2424 add_partition(i, EXTENDED);
2425 return;
2426 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002427 printf("Invalid partition number "
2428 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002429 }
2430 }
2431}
2432
2433static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002434write_table(void)
2435{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436 int i;
2437
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002438 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002439 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002440 if (ptes[i].changed)
2441 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002442 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 struct pte *pe = &ptes[i];
2444
2445 if (pe->changed) {
2446 write_part_table_flag(pe->sectorbuffer);
2447 write_sector(pe->offset, pe->sectorbuffer);
2448 }
2449 }
2450 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002451 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452 /* no test on change? the printf below might be mistaken */
2453 sgi_write_table();
2454 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002455 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002456 int needw = 0;
2457
Rob Landleyb73451d2006-02-24 16:29:00 +00002458 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 if (ptes[i].changed)
2460 needw = 1;
2461 if (needw)
2462 sun_write_table();
2463 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464
Denis Vlasenkobd852072007-03-19 14:43:38 +00002465 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 reread_partition_table(1);
2467}
2468
Rob Landleyb73451d2006-02-24 16:29:00 +00002469static void
2470reread_partition_table(int leave)
2471{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002472 int i;
2473
Denis Vlasenkobd852072007-03-19 14:43:38 +00002474 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002476 /* sleep(2); Huh? */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002477 i = ioctl_or_perror(fd, BLKRRPART, NULL,
2478 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002479 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002480#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002481 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002482 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002483 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002484 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002485 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002486#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002487
2488 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002489 if (ENABLE_FEATURE_CLEAN_UP)
2490 close(fd);
2491 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 }
2493}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002494#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002495
Denis Vlasenko834410a2006-11-29 12:00:28 +00002496#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002497#define MAX_PER_LINE 16
2498static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002499print_buffer(char *pbuffer)
2500{
2501 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502
2503 for (i = 0, l = 0; i < sector_size; i++, l++) {
2504 if (l == 0)
2505 printf("0x%03X:", i);
2506 printf(" %02X", (unsigned char) pbuffer[i]);
2507 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002508 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 l = -1;
2510 }
2511 }
2512 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002513 bb_putchar('\n');
2514 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002515}
2516
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002517static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002518print_raw(void)
2519{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520 int i;
2521
Denis Vlasenkobd852072007-03-19 14:43:38 +00002522 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002523 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002524 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002525 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002526 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002528 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002529}
2530
2531static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002532move_begin(int i)
2533{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 struct pte *pe = &ptes[i];
2535 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002536 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537
2538 if (warn_geometry())
2539 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002540 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002541 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542 return;
2543 }
2544 first = get_partition_start(pe);
2545 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002546 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547
2548 if (new != get_nr_sects(p)) {
2549 first = get_nr_sects(p) + get_start_sect(p) - new;
2550 set_nr_sects(p, first);
2551 set_start_sect(p, new);
2552 pe->changed = 1;
2553 }
2554}
2555
2556static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002557xselect(void)
2558{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559 char c;
2560
Rob Landleyb73451d2006-02-24 16:29:00 +00002561 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002562 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002563 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564 switch (c) {
2565 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002566 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002567 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002568 break;
2569 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002570 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002571 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 break;
2573 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002574 user_cylinders = g_cylinders =
2575 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002576 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002577 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002578 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002579 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002580 warn_cylinders();
2581 break;
2582 case 'd':
2583 print_raw();
2584 break;
2585 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002586 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002588 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002590 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002591 x_list_table(1);
2592 break;
2593 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 fix_partition_table_order();
2596 break;
2597 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002598#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 create_sgilabel();
2600#endif
2601 break;
2602 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002603 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002604 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002605 update_units();
2606 break;
2607 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002608 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 break;
2611 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002612 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 break;
2615 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 list_table(1);
2618 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 x_list_table(0);
2620 break;
2621 case 'q':
2622 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002623 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002624 exit(0);
2625 case 'r':
2626 return;
2627 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002628 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002629 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002630 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002631 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002632 printf("Warning: setting sector offset for DOS "
2633 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 }
2635 update_units();
2636 break;
2637 case 'v':
2638 verify();
2639 break;
2640 case 'w':
2641 write_table(); /* does not return */
2642 break;
2643 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002644 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002646 break;
2647 default:
2648 xmenu();
2649 }
2650 }
2651}
2652#endif /* ADVANCED mode */
2653
2654static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002655is_ide_cdrom_or_tape(const char *device)
2656{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002657 FILE *procf;
2658 char buf[100];
2659 struct stat statbuf;
2660 int is_ide = 0;
2661
2662 /* No device was given explicitly, and we are trying some
2663 likely things. But opening /dev/hdc may produce errors like
2664 "hdc: tray open or drive not ready"
2665 if it happens to be a CD-ROM drive. It even happens that
2666 the process hangs on the attempt to read a music CD.
2667 So try to be careful. This only works since 2.1.73. */
2668
2669 if (strncmp("/dev/hd", device, 7))
2670 return 0;
2671
2672 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2673 procf = fopen(buf, "r");
2674 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2675 is_ide = (!strncmp(buf, "cdrom", 5) ||
2676 !strncmp(buf, "tape", 4));
2677 else
2678 /* Now when this proc file does not exist, skip the
2679 device when it is read-only. */
2680 if (stat(device, &statbuf) == 0)
2681 is_ide = ((statbuf.st_mode & 0222) == 0);
2682
2683 if (procf)
2684 fclose(procf);
2685 return is_ide;
2686}
2687
Rob Landley5527b912006-02-25 03:46:10 +00002688
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002689static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002690trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002691{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 int gb;
2693
2694 disk_device = device;
2695 if (setjmp(listingbuf))
2696 return;
2697 if (!user_specified)
2698 if (is_ide_cdrom_or_tape(device))
2699 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002700 fd = open(disk_device, type_open);
2701 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002702 gb = get_boot(try_only);
2703 if (gb > 0) { /* I/O error */
2704 close(fd);
2705 } else if (gb < 0) { /* no DOS signature */
2706 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002707 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002708 return;
Rob Landley5527b912006-02-25 03:46:10 +00002709 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002710#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002711 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002712#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002713 printf("Disk %s doesn't contain a valid "
2714 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002715 close(fd);
2716 } else {
2717 close(fd);
2718 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002719#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002720 if (!LABEL_IS_SUN && g_partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002721 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002722 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002723#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002724 }
2725 } else {
2726 /* Ignore other errors, since we try IDE
2727 and SCSI hard disks which may not be
2728 installed on the system. */
2729 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002730 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002731 return;
2732 }
2733 }
2734}
2735
2736/* for fdisk -l: try all things in /proc/partitions
2737 that look like a partition name (do not end in a digit) */
2738static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002739tryprocpt(void)
2740{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002741 FILE *procpt;
2742 char line[100], ptname[100], devname[120], *s;
2743 int ma, mi, sz;
2744
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002745 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002746
2747 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002748 if (sscanf(line, " %d %d %d %[^\n ]",
2749 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002751 for (s = ptname; *s; s++)
2752 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002753 if (isdigit(s[-1]))
2754 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002755 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002756 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002757 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002758#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002760#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761}
2762
Denis Vlasenko834410a2006-11-29 12:00:28 +00002763#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002764static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002765unknown_command(int c)
2766{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002767 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002769#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002770
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002771int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002772int fdisk_main(int argc, char **argv)
2773{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002774 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002775 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002776 * fdisk -v
2777 * fdisk -l [-b sectorsize] [-u] device ...
2778 * fdisk -s [partition] ...
2779 * fdisk [-b sectorsize] [-u] device
2780 *
2781 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002782 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002783 enum {
2784 OPT_b = 1 << 0,
2785 OPT_C = 1 << 1,
2786 OPT_H = 1 << 2,
2787 OPT_l = 1 << 3,
2788 OPT_S = 1 << 4,
2789 OPT_u = 1 << 5,
2790 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2791 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002792
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002793 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002794
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002795 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002796 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002797 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002798 argc -= optind;
2799 argv += optind;
2800 if (opt & OPT_b) { // -b
2801 /* Ugly: this sector size is really per device,
2802 so cannot be combined with multiple disks,
2803 and the same goes for the C/H/S options.
2804 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002805 if (sector_size != 512 && sector_size != 1024
2806 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002807 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002808 sector_offset = 2;
2809 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002810 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002811 if (user_heads <= 0 || user_heads >= 256)
2812 user_heads = 0;
2813 if (user_sectors <= 0 || user_sectors >= 64)
2814 user_sectors = 0;
2815 if (opt & OPT_u)
2816 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002817
Denis Vlasenko834410a2006-11-29 12:00:28 +00002818#if ENABLE_FEATURE_FDISK_WRITABLE
2819 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002820 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002821#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822 type_open = O_RDONLY;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002823 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002825 do {
2826 trydev(*argv, 1);
2827 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002829 /* we don't have device names, */
2830 /* use /proc/partitions instead */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 tryprocpt();
2832 }
2833 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002834#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002836#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837
Denis Vlasenko834410a2006-11-29 12:00:28 +00002838#if ENABLE_FEATURE_FDISK_BLKSIZE
2839 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002840 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841
2842 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002843 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002844 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002845 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002846 unsigned long long size;
2847 fd = xopen(argv[j], O_RDONLY);
2848 size = bb_BLKGETSIZE_sectors() / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850 if (argc == 1)
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002851 printf("%lld\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002852 else
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002853 printf("%s: %lld\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854 }
2855 return 0;
2856 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002857#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859#if ENABLE_FEATURE_FDISK_WRITABLE
2860 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002861 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862
Denis Vlasenko834410a2006-11-29 12:00:28 +00002863 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 get_boot(fdisk);
2865
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002866 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002868 printf("Detected an OSF/1 disklabel on %s, entering "
2869 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002870 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002871 /*Why do we do this? It seems to be counter-intuitive*/
2872 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002873 /* If we return we may want to make an empty DOS label? */
2874 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875
2876 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002877 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002878 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002879 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 switch (c) {
2881 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002882 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002883 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002884 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002885 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002886 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002887 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002889 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002890 else
2891 unknown_command(c);
2892 break;
2893 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002894 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002895 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002896 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002897 if (read_maybe_empty("Please enter the name of the "
2898 "new boot file: ") == '\n')
2899 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002900 else
2901 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002902 }
2903#if ENABLE_FEATURE_OSF_LABEL
2904 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002905 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906#endif
2907 break;
2908 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002909 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002911 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002912 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002914 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002916 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 else
2918 unknown_command(c);
2919 break;
2920 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002921 {
Eric Andersen040f4402003-07-30 08:40:37 +00002922 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002923 /* If sgi_label then don't use get_existing_partition,
2924 let the user select a partition, since
2925 get_existing_partition() only works for Linux-like
2926 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002927 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002928 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002929 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002930 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002931 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002932 if (j >= 0)
2933 delete_partition(j);
2934 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 break;
2936 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002937 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 create_sgiinfo();
2939 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 unknown_command(c);
2941 case 'l':
2942 list_types(get_sys_types());
2943 break;
2944 case 'm':
2945 menu();
2946 break;
2947 case 'n':
2948 new_partition();
2949 break;
2950 case 'o':
2951 create_doslabel();
2952 break;
2953 case 'p':
2954 list_table(0);
2955 break;
2956 case 'q':
2957 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002958 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959 return 0;
2960 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002961#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002962 create_sunlabel();
2963#endif
2964 break;
2965 case 't':
2966 change_sysid();
2967 break;
2968 case 'u':
2969 change_units();
2970 break;
2971 case 'v':
2972 verify();
2973 break;
2974 case 'w':
2975 write_table(); /* does not return */
2976 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002977#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002978 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002979 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002980 printf("\n\tSorry, no experts menu for SGI "
2981 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002983 xselect();
2984 break;
2985#endif
2986 default:
2987 unknown_command(c);
2988 menu();
2989 }
2990 }
2991 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002992#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002993}