blob: f15b9af919daf92120b4838405cb3672fd26ab4e [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
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000010#include <assert.h> /* assert */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000011#include "busybox.h"
Denis Vlasenko98ae2162006-10-12 19:30:44 +000012#define _(x) x
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000013
Denis Vlasenko834410a2006-11-29 12:00:28 +000014/* Looks like someone forgot to add this to config system */
15#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
16# define ENABLE_FEATURE_FDISK_BLKSIZE 0
17# define USE_FEATURE_FDISK_BLKSIZE(a)
18#endif
19
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000020#define DEFAULT_SECTOR_SIZE 512
21#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000022#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000023#define MAXIMUM_PARTS 60
24
25#define ACTIVE_FLAG 0x80
26
27#define EXTENDED 0x05
28#define WIN98_EXTENDED 0x0f
29#define LINUX_PARTITION 0x81
30#define LINUX_SWAP 0x82
31#define LINUX_NATIVE 0x83
32#define LINUX_EXTENDED 0x85
33#define LINUX_LVM 0x8e
34#define LINUX_RAID 0xfd
35
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000036#define IS_EXTENDED(i) \
37 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
38
39#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
40
41#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
42#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
43
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000044struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000045 unsigned char heads;
46 unsigned char sectors;
47 unsigned short cylinders;
48 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000049};
50
Denis Vlasenko98ae2162006-10-12 19:30:44 +000051#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000052
53struct systypes {
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +000054 const char *name;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000055};
56
Denis Vlasenko834410a2006-11-29 12:00:28 +000057static unsigned sector_size = DEFAULT_SECTOR_SIZE;
58static unsigned user_set_sector_size;
59static unsigned sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000060
61/*
62 * Raw disk label. For DOS-type partition tables the MBR,
63 * with descriptions of the primary partitions.
64 */
"Vladimir N. Oleynik"65bb10f2005-11-24 12:10:13 +000065#if (MAX_SECTOR_SIZE) > (BUFSIZ+1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000066static char MBRbuffer[MAX_SECTOR_SIZE];
"Vladimir N. Oleynik"65bb10f2005-11-24 12:10:13 +000067#else
68# define MBRbuffer bb_common_bufsiz1
69#endif
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +000070
Denis Vlasenko834410a2006-11-29 12:00:28 +000071#if ENABLE_FEATURE_OSF_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +000072static int possibly_osf_label;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000073#endif
74
Denis Vlasenko834410a2006-11-29 12:00:28 +000075static unsigned heads, sectors, cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000076static void update_units(void);
77
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000078
79/*
80 * return partition name - uses static storage unless buf is supplied
81 */
82static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +000083partname(const char *dev, int pno, int lth)
84{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000085 static char buffer[80];
86 const char *p;
87 int w, wp;
88 int bufsiz;
89 char *bufp;
90
91 bufp = buffer;
92 bufsiz = sizeof(buffer);
93
94 w = strlen(dev);
95 p = "";
96
97 if (isdigit(dev[w-1]))
98 p = "p";
99
100 /* devfs kludge - note: fdisk partition names are not supposed
101 to equal kernel names, so there is no reason to do this */
Rob Landleyb73451d2006-02-24 16:29:00 +0000102 if (strcmp(dev + w - 4, "disc") == 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000103 w -= 4;
104 p = "part";
105 }
106
107 wp = strlen(p);
108
109 if (lth) {
110 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
111 lth-wp-2, w, dev, p, pno);
112 } else {
113 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
114 }
115 return bufp;
116}
117
118struct partition {
119 unsigned char boot_ind; /* 0x80 - active */
120 unsigned char head; /* starting head */
121 unsigned char sector; /* starting sector */
122 unsigned char cyl; /* starting cylinder */
123 unsigned char sys_ind; /* What partition type */
124 unsigned char end_head; /* end head */
125 unsigned char end_sector; /* end sector */
126 unsigned char end_cyl; /* end cylinder */
127 unsigned char start4[4]; /* starting sector counting from 0 */
128 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000129} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000130
131enum failure {
132 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
133 unable_to_write
134};
135
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000136enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +0000137 label_dos, label_sun, label_sgi, label_aix, label_osf
138};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000139#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000140
Denis Vlasenko834410a2006-11-29 12:00:28 +0000141#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000142#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000143#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000144#else
145#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000146#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000147#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000148
Denis Vlasenko834410a2006-11-29 12:00:28 +0000149#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000150#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000151#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000152#else
153#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000154#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000155#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000156
Denis Vlasenko834410a2006-11-29 12:00:28 +0000157#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000158#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000159#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000160#else
161#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000162#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000163#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000164
Denis Vlasenko834410a2006-11-29 12:00:28 +0000165#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000166#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000167#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000168#else
169#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000170#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000171#endif
Rob Landley5527b912006-02-25 03:46:10 +0000172
Rob Landleyb73451d2006-02-24 16:29:00 +0000173enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000174
Rob Landley5527b912006-02-25 03:46:10 +0000175static enum label_type current_label_type;
176
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000177static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000178static int fd; /* the disk */
179static int partitions = 4; /* maximum partition + 1 */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000180static int display_in_cyl_units = 1;
181static unsigned units_per_sector = 1;
182#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000183static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000184static void reread_partition_table(int leave);
185static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000186static int get_partition(int warn, int max);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000187static void list_types(const struct systypes *sys);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000188static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000189#endif
190static const char *partition_type(unsigned char type);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000191static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000192static void get_geometry(void);
193static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000194
195#define PLURAL 0
196#define SINGULAR 1
197
198#define hex_val(c) ({ \
199 char _c = (c); \
200 isdigit(_c) ? _c - '0' : \
201 tolower(_c) + 10 - 'a'; \
202 })
203
204
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000205#define LINE_LENGTH 80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000206#define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
207 (n) * sizeof(struct partition)))
208#define sector(s) ((s) & 0x3f)
209#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
210
211#define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
212 ((h) + heads * cylinder(s,c)))
213#define set_hsc(h,s,c,sector) { \
214 s = sector % sectors + 1; \
215 sector /= sectors; \
216 h = sector % heads; \
217 sector /= heads; \
218 c = sector & 0xff; \
219 s |= (sector >> 2) & 0xc0; \
220 }
221
222
Denis Vlasenko28703012006-12-19 20:32:02 +0000223static unsigned get_start_sect(const struct partition *p);
224static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000225
226/*
227 * per partition table entry data
228 *
229 * The four primary partitions have the same sectorbuffer (MBRbuffer)
230 * and have NULL ext_pointer.
231 * Each logical partition table entry has two pointers, one for the
232 * partition and one link to the next one.
233 */
234static struct pte {
235 struct partition *part_table; /* points into sectorbuffer */
236 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000237#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000238 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000239#endif
Eric Andersend9261492004-06-28 23:50:31 +0000240 off_t offset; /* disk sector number */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000241 char *sectorbuffer; /* disk sector contents */
242} ptes[MAXIMUM_PARTS];
243
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000244
Denis Vlasenko834410a2006-11-29 12:00:28 +0000245#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000246static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000247set_all_unchanged(void)
248{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000249 int i;
250
251 for (i = 0; i < MAXIMUM_PARTS; i++)
252 ptes[i].changed = 0;
253}
254
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000255static ATTRIBUTE_ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000256set_changed(int i)
257{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000258 ptes[i].changed = 1;
259}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000260#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000261
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000262static ATTRIBUTE_ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000263get_part_table(int i)
264{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000265 return ptes[i].part_table;
266}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000267
268static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000269str_units(int n)
270{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000271 if (n == 1)
272 return display_in_cyl_units ? _("cylinder") : _("sector");
273 else
274 return display_in_cyl_units ? _("cylinders") : _("sectors");
275}
276
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000277static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000278valid_part_table_flag(const char *mbuffer)
279{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000280 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000281}
282
Denis Vlasenko834410a2006-11-29 12:00:28 +0000283#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000284static ATTRIBUTE_ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000285write_part_table_flag(char *b)
286{
287 b[510] = 0x55;
288 b[511] = 0xaa;
289}
290
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000291static char line_buffer[LINE_LENGTH];
292static char *line_ptr;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000293
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000294/* read line; return 0 or first printable char */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000295static int
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000296read_line(const char *prompt)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000297{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000298 int sz;
299
300 sz = read_line_input(prompt, line_buffer, LINE_LENGTH, NULL);
301 if (sz <= 0)
302 exit(0); /* Ctrl-D or Ctrl-C */
303
304 if (line_buffer[sz-1] == '\n')
305 line_buffer[--sz] = '\0';
306
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000307 line_ptr = line_buffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000308 while (*line_ptr && !isgraph(*line_ptr))
309 line_ptr++;
310 return *line_ptr;
311}
312
313static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000314read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000315{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000316 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000317 return *line_ptr;
318}
319
320static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000321read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000322{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000323 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000324 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000325 line_ptr[0] = '\n';
326 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000327 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000328 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000329}
330
331static int
332read_hex(const struct systypes *sys)
333{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000334 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000335 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000336 read_nonempty(_("Hex code (type L to list codes): "));
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000337 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000338 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000339 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000340 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000341 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000342 if (v > 0xff)
343 /* Bad input also triggers this */
344 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000345 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000346 }
347}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000348#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000349
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000350#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000351
352typedef struct {
353 unsigned char info[128]; /* Informative text string */
354 unsigned char spare0[14];
355 struct sun_info {
356 unsigned char spare1;
357 unsigned char id;
358 unsigned char spare2;
359 unsigned char flags;
360 } infos[8];
361 unsigned char spare1[246]; /* Boot information etc. */
362 unsigned short rspeed; /* Disk rotational speed */
363 unsigned short pcylcount; /* Physical cylinder count */
364 unsigned short sparecyl; /* extra sects per cylinder */
365 unsigned char spare2[4]; /* More magic... */
366 unsigned short ilfact; /* Interleave factor */
367 unsigned short ncyl; /* Data cylinder count */
368 unsigned short nacyl; /* Alt. cylinder count */
369 unsigned short ntrks; /* Tracks per cylinder */
370 unsigned short nsect; /* Sectors per track */
371 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000372 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000373 uint32_t start_cylinder;
374 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000375 } partitions[8];
376 unsigned short magic; /* Magic number */
377 unsigned short csum; /* Label xor'd checksum */
378} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000379#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000380#define SUNOS_SWAP 3
381#define SUN_WHOLE_DISK 5
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000382STATIC_OSF void bsd_select(void);
383STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000384#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000385
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000386#define SGI_VOLHDR 0x00
387/* 1 and 2 were used for drive types no longer supported by SGI */
388#define SGI_SWAP 0x03
389/* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
390#define SGI_VOLUME 0x06
391#define SGI_EFS 0x07
392#define SGI_LVOL 0x08
393#define SGI_RLVOL 0x09
394#define SGI_XFS 0x0a
395#define SGI_XFSLOG 0x0b
396#define SGI_XLV 0x0c
397#define SGI_XVM 0x0d
398#define SGI_ENTIRE_DISK SGI_VOLUME
Denis Vlasenko28703012006-12-19 20:32:02 +0000399#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000400static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000401fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000402{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000403 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000404}
405
Rob Landley88621d72006-08-29 19:41:06 +0000406static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000407fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000408{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000409 return (x << 24) |
410 ((x & 0xFF00) << 8) |
411 ((x & 0xFF0000) >> 8) |
412 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000413}
414#endif
415
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000416STATIC_SGI const struct systypes sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000417STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000418STATIC_SGI int sgi_get_sysid(int i);
419STATIC_SGI void sgi_delete_partition(int i);
420STATIC_SGI void sgi_change_sysid(int i, int sys);
421STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000422#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000423STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000424#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000425STATIC_SGI int verify_sgi(int verbose);
426STATIC_SGI void sgi_add_partition(int n, int sys);
427STATIC_SGI void sgi_set_swappartition(int i);
428STATIC_SGI const char *sgi_get_bootfile(void);
429STATIC_SGI void sgi_set_bootfile(const char* aFile);
430STATIC_SGI void create_sgiinfo(void);
431STATIC_SGI void sgi_write_table(void);
432STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000433#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000434
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000435STATIC_SUN const struct systypes sun_sys_types[];
436STATIC_SUN void sun_delete_partition(int i);
437STATIC_SUN void sun_change_sysid(int i, int sys);
438STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000439STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000440#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000441STATIC_SUN void sun_set_alt_cyl(void);
442STATIC_SUN void sun_set_ncyl(int cyl);
443STATIC_SUN void sun_set_xcyl(void);
444STATIC_SUN void sun_set_ilfact(void);
445STATIC_SUN void sun_set_rspeed(void);
446STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000447#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000448STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
449STATIC_SUN void verify_sun(void);
450STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000451#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000452
453/* DOS partition types */
454
455static const struct systypes i386_sys_types[] = {
Rob Landleyb73451d2006-02-24 16:29:00 +0000456 { "\x00" "Empty" },
457 { "\x01" "FAT12" },
458 { "\x04" "FAT16 <32M" },
459 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
460 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
461 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
462 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
463 { "\x0b" "Win95 FAT32" },
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000464 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
Rob Landleyb73451d2006-02-24 16:29:00 +0000465 { "\x0e" "Win95 FAT16 (LBA)" },
466 { "\x0f" "Win95 Ext'd (LBA)" },
467 { "\x11" "Hidden FAT12" },
468 { "\x12" "Compaq diagnostics" },
469 { "\x14" "Hidden FAT16 <32M" },
470 { "\x16" "Hidden FAT16" },
471 { "\x17" "Hidden HPFS/NTFS" },
472 { "\x1b" "Hidden Win95 FAT32" },
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000473 { "\x1c" "Hidden W95 FAT32 (LBA)" },
474 { "\x1e" "Hidden W95 FAT16 (LBA)" },
475 { "\x3c" "Part.Magic recovery" },
Rob Landleyb73451d2006-02-24 16:29:00 +0000476 { "\x41" "PPC PReP Boot" },
477 { "\x42" "SFS" },
478 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
479 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
480 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
481 { "\x82" "Linux swap" }, /* also Solaris */
482 { "\x83" "Linux" },
483 { "\x84" "OS/2 hidden C: drive" },
484 { "\x85" "Linux extended" },
485 { "\x86" "NTFS volume set" },
486 { "\x87" "NTFS volume set" },
487 { "\x8e" "Linux LVM" },
488 { "\x9f" "BSD/OS" }, /* BSDI */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000489 { "\xa0" "Thinkpad hibernation" },
Rob Landleyb73451d2006-02-24 16:29:00 +0000490 { "\xa5" "FreeBSD" }, /* various BSD flavours */
491 { "\xa6" "OpenBSD" },
492 { "\xa8" "Darwin UFS" },
493 { "\xa9" "NetBSD" },
494 { "\xab" "Darwin boot" },
495 { "\xb7" "BSDI fs" },
496 { "\xb8" "BSDI swap" },
497 { "\xbe" "Solaris boot" },
498 { "\xeb" "BeOS fs" },
499 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
500 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
501 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
502 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
503 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
504 autodetect using persistent
505 superblock */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000506#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
Rob Landleyb73451d2006-02-24 16:29:00 +0000507 { "\x02" "XENIX root" },
508 { "\x03" "XENIX usr" },
509 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
510 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
511 { "\x10" "OPUS" },
512 { "\x18" "AST SmartSleep" },
513 { "\x24" "NEC DOS" },
514 { "\x39" "Plan 9" },
515 { "\x40" "Venix 80286" },
516 { "\x4d" "QNX4.x" },
517 { "\x4e" "QNX4.x 2nd part" },
518 { "\x4f" "QNX4.x 3rd part" },
519 { "\x50" "OnTrack DM" },
520 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
521 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
522 { "\x53" "OnTrack DM6 Aux3" },
523 { "\x54" "OnTrackDM6" },
524 { "\x55" "EZ-Drive" },
525 { "\x56" "Golden Bow" },
526 { "\x5c" "Priam Edisk" },
527 { "\x61" "SpeedStor" },
528 { "\x64" "Novell Netware 286" },
529 { "\x65" "Novell Netware 386" },
530 { "\x70" "DiskSecure Multi-Boot" },
531 { "\x75" "PC/IX" },
532 { "\x93" "Amoeba" },
533 { "\x94" "Amoeba BBT" }, /* (bad block table) */
534 { "\xa7" "NeXTSTEP" },
535 { "\xbb" "Boot Wizard hidden" },
536 { "\xc1" "DRDOS/sec (FAT-12)" },
537 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
538 { "\xc6" "DRDOS/sec (FAT-16)" },
539 { "\xc7" "Syrinx" },
540 { "\xda" "Non-FS data" },
541 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000542 Concurrent DOS or CTOS */
Rob Landleyb73451d2006-02-24 16:29:00 +0000543 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
544 { "\xdf" "BootIt" }, /* BootIt EMBRM */
545 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000546 extended partition */
Rob Landleyb73451d2006-02-24 16:29:00 +0000547 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
548 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000549 partition < 1024 cyl. */
Rob Landleyb73451d2006-02-24 16:29:00 +0000550 { "\xf1" "SpeedStor" },
551 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
552 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
553 { "\xff" "BBT" }, /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000554#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000555 { 0 }
556};
557
558
Denis Vlasenko834410a2006-11-29 12:00:28 +0000559#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000560/* start_sect and nr_sects are stored little endian on all machines */
561/* moreover, they are not aligned correctly */
562static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000563store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000564{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000565 cp[0] = val;
566 cp[1] = val >> 8;
567 cp[2] = val >> 16;
568 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000569}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000570#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000571
Denis Vlasenko834410a2006-11-29 12:00:28 +0000572static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000573read4_little_endian(const unsigned char *cp)
574{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000575 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000576}
577
Denis Vlasenko834410a2006-11-29 12:00:28 +0000578#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000579static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000580set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000581{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000582 store4_little_endian(p->start4, start_sect);
583}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000584#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000585
Denis Vlasenko28703012006-12-19 20:32:02 +0000586static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000587get_start_sect(const struct partition *p)
588{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589 return read4_little_endian(p->start4);
590}
591
Denis Vlasenko834410a2006-11-29 12:00:28 +0000592#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000593static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000594set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000595{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000596 store4_little_endian(p->size4, nr_sects);
597}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000598#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599
Denis Vlasenko28703012006-12-19 20:32:02 +0000600static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000601get_nr_sects(const struct partition *p)
602{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603 return read4_little_endian(p->size4);
604}
605
606/* normally O_RDWR, -l option gives O_RDONLY */
607static int type_open = O_RDWR;
608
609
Rob Landleyb73451d2006-02-24 16:29:00 +0000610static int ext_index; /* the prime extended partition */
611static int listing; /* no aborts for fdisk -l */
612static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000613#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000614static int dos_changed;
615static int nowarn; /* no warnings for fdisk -l/-s */
616#endif
617
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000618
619
Denis Vlasenko834410a2006-11-29 12:00:28 +0000620static unsigned user_cylinders, user_heads, user_sectors;
621static unsigned pt_heads, pt_sectors;
622static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000623
Eric Andersend9261492004-06-28 23:50:31 +0000624static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000625
Eric Andersen040f4402003-07-30 08:40:37 +0000626static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627
628
629static jmp_buf listingbuf;
630
Rob Landleyb73451d2006-02-24 16:29:00 +0000631static void fdisk_fatal(enum failure why)
632{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000633 const char *message;
634
635 if (listing) {
636 close(fd);
637 longjmp(listingbuf, 1);
638 }
639
640 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000641 case unable_to_open:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000642 message = "\nUnable to open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000643 break;
644 case unable_to_read:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000645 message = "\nUnable to read %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000646 break;
647 case unable_to_seek:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000648 message = "\nUnable to seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000649 break;
650 case unable_to_write:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000651 message = "\nUnable to write %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000652 break;
653 case ioctl_error:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000654 message = "\nBLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000655 break;
656 default:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000657 message = "\nFatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000658 }
659
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000660 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000661}
662
663static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000664seek_sector(off_t secno)
665{
Eric Andersen0a92f352004-03-30 09:21:54 +0000666 off_t offset = secno * sector_size;
667 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000668 fdisk_fatal(unable_to_seek);
669}
670
Denis Vlasenko834410a2006-11-29 12:00:28 +0000671#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000672static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000673write_sector(off_t secno, char *buf)
674{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000675 seek_sector(secno);
676 if (write(fd, buf, sector_size) != sector_size)
677 fdisk_fatal(unable_to_write);
678}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000679#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000680
681/* Allocate a buffer and read a partition table sector */
682static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000683read_pte(struct pte *pe, off_t offset)
684{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000686 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000687 seek_sector(offset);
688 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
689 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000690#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000691 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000692#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000693 pe->part_table = pe->ext_pointer = NULL;
694}
695
Denis Vlasenko834410a2006-11-29 12:00:28 +0000696static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000697get_partition_start(const struct pte *pe)
698{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000699 return pe->offset + get_start_sect(pe->part_table);
700}
701
Denis Vlasenko834410a2006-11-29 12:00:28 +0000702#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000703/*
704 * Avoid warning about DOS partitions when no DOS partition was changed.
705 * Here a heuristic "is probably dos partition".
706 * We might also do the opposite and warn in all cases except
707 * for "is probably nondos partition".
708 */
709static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000710is_dos_partition(int t)
711{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000712 return (t == 1 || t == 4 || t == 6 ||
713 t == 0x0b || t == 0x0c || t == 0x0e ||
714 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
715 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
716 t == 0xc1 || t == 0xc4 || t == 0xc6);
717}
718
719static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000720menu(void)
721{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000722 puts(_("Command Action"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000723 if (LABEL_IS_SUN) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000724 puts(_("a\ttoggle a read only flag")); /* sun */
725 puts(_("b\tedit bsd disklabel"));
726 puts(_("c\ttoggle the mountable flag")); /* sun */
727 puts(_("d\tdelete a partition"));
728 puts(_("l\tlist known partition types"));
729 puts(_("n\tadd a new partition"));
730 puts(_("o\tcreate a new empty DOS partition table"));
731 puts(_("p\tprint the partition table"));
732 puts(_("q\tquit without saving changes"));
733 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
734 puts(_("t\tchange a partition's system id"));
735 puts(_("u\tchange display/entry units"));
736 puts(_("v\tverify the partition table"));
737 puts(_("w\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000738#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000739 puts(_("x\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000740#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000741 } else if (LABEL_IS_SGI) {
742 puts(_("a\tselect bootable partition")); /* sgi flavour */
743 puts(_("b\tedit bootfile entry")); /* sgi */
744 puts(_("c\tselect sgi swap partition")); /* sgi flavour */
745 puts(_("d\tdelete a partition"));
746 puts(_("l\tlist known partition types"));
747 puts(_("n\tadd a new partition"));
748 puts(_("o\tcreate a new empty DOS partition table"));
749 puts(_("p\tprint the partition table"));
750 puts(_("q\tquit without saving changes"));
751 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
752 puts(_("t\tchange a partition's system id"));
753 puts(_("u\tchange display/entry units"));
754 puts(_("v\tverify the partition table"));
755 puts(_("w\twrite table to disk and exit"));
756 } else if (LABEL_IS_AIX) {
757 puts(_("o\tcreate a new empty DOS partition table"));
758 puts(_("q\tquit without saving changes"));
759 puts(_("s\tcreate a new empty Sun disklabel")); /* sun */
760 } else {
761 puts(_("a\ttoggle a bootable flag"));
762 puts(_("b\tedit bsd disklabel"));
763 puts(_("c\ttoggle the dos compatibility flag"));
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 Vlasenko834410a2006-11-29 12:00:28 +0000775#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000776 puts(_("x\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000777#endif
778 }
779}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000780#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000781
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000782
Denis Vlasenko834410a2006-11-29 12:00:28 +0000783#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000784static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000785xmenu(void)
786{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000787 puts(_("Command Action"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000788 if (LABEL_IS_SUN) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000789 puts(_("a\tchange number of alternate cylinders")); /*sun*/
790 puts(_("c\tchange number of cylinders"));
791 puts(_("d\tprint the raw data in the partition table"));
792 puts(_("e\tchange number of extra sectors per cylinder"));/*sun*/
793 puts(_("h\tchange number of heads"));
794 puts(_("i\tchange interleave factor")); /*sun*/
795 puts(_("o\tchange rotation speed (rpm)")); /*sun*/
796 puts(_("p\tprint the partition table"));
797 puts(_("q\tquit without saving changes"));
798 puts(_("r\treturn to main menu"));
799 puts(_("s\tchange number of sectors/track"));
800 puts(_("v\tverify the partition table"));
801 puts(_("w\twrite table to disk and exit"));
802 puts(_("y\tchange number of physical cylinders")); /*sun*/
803 } else if (LABEL_IS_SGI) {
804 puts(_("b\tmove beginning of data in a partition")); /* !sun */
805 puts(_("c\tchange number of cylinders"));
806 puts(_("d\tprint the raw data in the partition table"));
807 puts(_("e\tlist extended partitions")); /* !sun */
808 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
809 puts(_("h\tchange number of heads"));
810 puts(_("p\tprint the partition table"));
811 puts(_("q\tquit without saving changes"));
812 puts(_("r\treturn to main menu"));
813 puts(_("s\tchange number of sectors/track"));
814 puts(_("v\tverify the partition table"));
815 puts(_("w\twrite table to disk and exit"));
816 } else if (LABEL_IS_AIX) {
817 puts(_("b\tmove beginning of data in a partition")); /* !sun */
818 puts(_("c\tchange number of cylinders"));
819 puts(_("d\tprint the raw data in the partition table"));
820 puts(_("e\tlist extended partitions")); /* !sun */
821 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
822 puts(_("h\tchange number of heads"));
823 puts(_("p\tprint the partition table"));
824 puts(_("q\tquit without saving changes"));
825 puts(_("r\treturn to main menu"));
826 puts(_("s\tchange number of sectors/track"));
827 puts(_("v\tverify the partition table"));
828 puts(_("w\twrite table to disk and exit"));
829 } else {
830 puts(_("b\tmove beginning of data in a partition")); /* !sun */
831 puts(_("c\tchange number of cylinders"));
832 puts(_("d\tprint the raw data in the partition table"));
833 puts(_("e\tlist extended partitions")); /* !sun */
834 puts(_("f\tfix partition order")); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000835#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000836 puts(_("g\tcreate an IRIX (SGI) partition table"));/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000837#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000838 puts(_("h\tchange number of heads"));
839 puts(_("p\tprint the partition table"));
840 puts(_("q\tquit without saving changes"));
841 puts(_("r\treturn to main menu"));
842 puts(_("s\tchange number of sectors/track"));
843 puts(_("v\tverify the partition table"));
844 puts(_("w\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000845 }
846}
847#endif /* ADVANCED mode */
848
Denis Vlasenko834410a2006-11-29 12:00:28 +0000849#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000850static const struct systypes *
Rob Landleyb73451d2006-02-24 16:29:00 +0000851get_sys_types(void)
852{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000853 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000854 LABEL_IS_SUN ? sun_sys_types :
855 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000856 i386_sys_types);
857}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000858#else
859#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000860#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861
862static const char *partition_type(unsigned char type)
863{
864 int i;
865 const struct systypes *types = get_sys_types();
866
Rob Landleyb73451d2006-02-24 16:29:00 +0000867 for (i = 0; types[i].name; i++)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000868 if ((unsigned char)types[i].name[0] == type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000869 return types[i].name + 1;
870
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000871 return _("Unknown");
872}
873
874
Denis Vlasenko834410a2006-11-29 12:00:28 +0000875#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000876static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000877get_sysid(int i)
878{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000879 return LABEL_IS_SUN ? sunlabel->infos[i].id :
880 (LABEL_IS_SGI ? sgi_get_sysid(i) :
881 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000882}
883
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884static void list_types(const struct systypes *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000885{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000886 enum { COLS = 3 };
887
888 unsigned last[COLS];
889 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000890 int i;
891
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000892 for (size = 0; sys[size].name; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000893
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000894 done = 0;
895 for (i = COLS-1; i >= 0; i--) {
896 done += (size + i - done) / (i + 1);
897 last[COLS-1 - i] = done;
898 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000899
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000900 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000901 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000902 printf("%c%2x %-22.22s", i ? ' ' : '\n',
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000903 (unsigned char)sys[next].name[0],
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000904 sys[next].name + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000906 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000907 i = 0;
908 next = ++done;
909 }
910 } while (done < last[0]);
911 putchar('\n');
912}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000913#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000914
915static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000916is_cleared_partition(const struct partition *p)
917{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000918 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
919 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
920 get_start_sect(p) || get_nr_sects(p));
921}
922
923static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000924clear_partition(struct partition *p)
925{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000926 if (!p)
927 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000928 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929}
930
Denis Vlasenko834410a2006-11-29 12:00:28 +0000931#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000932static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000933set_partition(int i, int doext, off_t start, off_t stop, int sysid)
934{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000936 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000937
938 if (doext) {
939 p = ptes[i].ext_pointer;
940 offset = extended_offset;
941 } else {
942 p = ptes[i].part_table;
943 offset = ptes[i].offset;
944 }
945 p->boot_ind = 0;
946 p->sys_ind = sysid;
947 set_start_sect(p, start - offset);
948 set_nr_sects(p, stop - start + 1);
949 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
950 start = heads*sectors*1024 - 1;
951 set_hsc(p->head, p->sector, p->cyl, start);
952 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
953 stop = heads*sectors*1024 - 1;
954 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
955 ptes[i].changed = 1;
956}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000957#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000958
959static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000960test_c(const char **m, const char *mesg)
961{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000962 int val = 0;
963 if (!*m)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000964 printf(_("You must set"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000965 else {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000966 printf(" %s", *m);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000967 val = 1;
968 }
969 *m = mesg;
970 return val;
971}
972
973static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000974warn_geometry(void)
975{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000976 const char *m = NULL;
977 int prev = 0;
978
979 if (!heads)
980 prev = test_c(&m, _("heads"));
981 if (!sectors)
982 prev = test_c(&m, _("sectors"));
983 if (!cylinders)
984 prev = test_c(&m, _("cylinders"));
985 if (!m)
986 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000987
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000988 printf("%s%s.\n"
Denis Vlasenko834410a2006-11-29 12:00:28 +0000989#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000990 "You can do this from the extra functions menu.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000991#endif
992 , prev ? _(" and ") : " ", m);
993
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994 return 1;
995}
996
997static void update_units(void)
998{
999 int cyl_units = heads * sectors;
1000
1001 if (display_in_cyl_units && cyl_units)
1002 units_per_sector = cyl_units;
1003 else
1004 units_per_sector = 1; /* in sectors */
1005}
1006
Denis Vlasenko834410a2006-11-29 12:00:28 +00001007#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001008static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001009warn_cylinders(void)
1010{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001011 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1012 printf(_("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001013"The number of cylinders for this disk is set to %d.\n"
1014"There is nothing wrong with that, but this is larger than 1024,\n"
1015"and could in certain setups cause problems with:\n"
1016"1) software that runs at boot time (e.g., old versions of LILO)\n"
1017"2) booting and partitioning software from other OSs\n"
1018" (e.g., DOS FDISK, OS/2 FDISK)\n"),
1019 cylinders);
1020}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001021#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001022
1023static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001024read_extended(int ext)
1025{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026 int i;
1027 struct pte *pex;
1028 struct partition *p, *q;
1029
1030 ext_index = ext;
1031 pex = &ptes[ext];
1032 pex->ext_pointer = pex->part_table;
1033
1034 p = pex->part_table;
1035 if (!get_start_sect(p)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001036 printf(_("Bad offset in primary extended partition\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001037 return;
1038 }
1039
Rob Landleyb73451d2006-02-24 16:29:00 +00001040 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001041 struct pte *pe = &ptes[partitions];
1042
1043 if (partitions >= MAXIMUM_PARTS) {
1044 /* This is not a Linux restriction, but
1045 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001046 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001048#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001049 printf(_("Warning: deleting partitions after %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001052#endif
1053 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054 return;
1055 }
1056
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001057 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058
1059 if (!extended_offset)
1060 extended_offset = get_start_sect(p);
1061
1062 q = p = pt_offset(pe->sectorbuffer, 0);
1063 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001064 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001065 if (pe->ext_pointer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001066 printf(_("Warning: extra link "
1067 "pointer in partition table"
1068 " %d\n"), partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001069 else
1070 pe->ext_pointer = p;
1071 } else if (p->sys_ind) {
1072 if (pe->part_table)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001073 printf(_("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001074 "data in partition table"
1075 " %d\n"), partitions + 1);
1076 else
1077 pe->part_table = p;
1078 }
1079 }
1080
1081 /* very strange code here... */
1082 if (!pe->part_table) {
1083 if (q != pe->ext_pointer)
1084 pe->part_table = q;
1085 else
1086 pe->part_table = q + 1;
1087 }
1088 if (!pe->ext_pointer) {
1089 if (q != pe->part_table)
1090 pe->ext_pointer = q;
1091 else
1092 pe->ext_pointer = q + 1;
1093 }
1094
1095 p = pe->ext_pointer;
1096 partitions++;
1097 }
1098
Denis Vlasenko834410a2006-11-29 12:00:28 +00001099#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001100 /* remove empty links */
1101 remove:
1102 for (i = 4; i < partitions; i++) {
1103 struct pte *pe = &ptes[i];
1104
1105 if (!get_nr_sects(pe->part_table) &&
Rob Landleyb73451d2006-02-24 16:29:00 +00001106 (partitions > 5 || ptes[4].part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001107 printf("omitting empty partition (%d)\n", i+1);
1108 delete_partition(i);
1109 goto remove; /* numbering changed */
1110 }
1111 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001112#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113}
1114
Denis Vlasenko834410a2006-11-29 12:00:28 +00001115#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001117create_doslabel(void)
1118{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001119 int i;
1120
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001121 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1123 "until you decide to write them. After that, of course, the previous\n"
1124 "content won't be recoverable.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001125
1126 current_label_type = label_dos;
1127
Denis Vlasenko834410a2006-11-29 12:00:28 +00001128#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001129 possibly_osf_label = 0;
1130#endif
1131 partitions = 4;
1132
1133 for (i = 510-64; i < 510; i++)
1134 MBRbuffer[i] = 0;
1135 write_part_table_flag(MBRbuffer);
1136 extended_offset = 0;
1137 set_all_unchanged();
1138 set_changed(0);
1139 get_boot(create_empty_dos);
1140}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001141#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001142
1143static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001144get_sectorsize(void)
1145{
Rob Landley736e5252006-02-25 03:36:00 +00001146 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001147 int arg;
1148 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1149 sector_size = arg;
1150 if (sector_size != DEFAULT_SECTOR_SIZE)
1151 printf(_("Note: sector size is %d (not %d)\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001152 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001153 }
1154}
1155
Rob Landley88621d72006-08-29 19:41:06 +00001156static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001157get_kernel_geometry(void)
1158{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001159 struct hd_geometry geometry;
1160
1161 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1162 kern_heads = geometry.heads;
1163 kern_sectors = geometry.sectors;
1164 /* never use geometry.cylinders - it is truncated */
1165 }
1166}
1167
1168static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001169get_partition_table_geometry(void)
1170{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001171 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 struct partition *p;
1173 int i, h, s, hh, ss;
1174 int first = 1;
1175 int bad = 0;
1176
Eric Andersen3496fdc2006-01-30 23:09:20 +00001177 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001178 return;
1179
1180 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001181 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001182 p = pt_offset(bufp, i);
1183 if (p->sys_ind != 0) {
1184 h = p->end_head + 1;
1185 s = (p->end_sector & 077);
1186 if (first) {
1187 hh = h;
1188 ss = s;
1189 first = 0;
1190 } else if (hh != h || ss != s)
1191 bad = 1;
1192 }
1193 }
1194
1195 if (!first && !bad) {
1196 pt_heads = hh;
1197 pt_sectors = ss;
1198 }
1199}
1200
Rob Landleyb73451d2006-02-24 16:29:00 +00001201static void
1202get_geometry(void)
1203{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001204 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001205 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206
1207 get_sectorsize();
1208 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001209#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210 guess_device_type();
1211#endif
1212 heads = cylinders = sectors = 0;
1213 kern_heads = kern_sectors = 0;
1214 pt_heads = pt_sectors = 0;
1215
1216 get_kernel_geometry();
1217 get_partition_table_geometry();
1218
1219 heads = user_heads ? user_heads :
1220 pt_heads ? pt_heads :
1221 kern_heads ? kern_heads : 255;
1222 sectors = user_sectors ? user_sectors :
1223 pt_sectors ? pt_sectors :
1224 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001225 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1226 /* got bytes */
1227 } else {
1228 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229
1230 if (ioctl(fd, BLKGETSIZE, &longsectors))
1231 longsectors = 0;
Eric Andersen040f4402003-07-30 08:40:37 +00001232 bytes = ((unsigned long long) longsectors) << 9;
1233 }
1234
1235 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236
1237 sector_offset = 1;
1238 if (dos_compatible_flag)
1239 sector_offset = sectors;
1240
Eric Andersen040f4402003-07-30 08:40:37 +00001241 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242 if (!cylinders)
1243 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001244}
1245
1246/*
1247 * Read MBR. Returns:
1248 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1249 * 0: found or created label
1250 * 1: I/O error
1251 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001252static int
1253get_boot(enum action what)
1254{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001255 int i;
1256
1257 partitions = 4;
1258
1259 for (i = 0; i < 4; i++) {
1260 struct pte *pe = &ptes[i];
1261
1262 pe->part_table = pt_offset(MBRbuffer, i);
1263 pe->ext_pointer = NULL;
1264 pe->offset = 0;
1265 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001266#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001267 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001268#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001269 }
1270
Denis Vlasenko834410a2006-11-29 12:00:28 +00001271#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272 if (what == create_empty_sun && check_sun_label())
1273 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001274#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001275
1276 memset(MBRbuffer, 0, 512);
1277
Denis Vlasenko834410a2006-11-29 12:00:28 +00001278#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279 if (what == create_empty_dos)
1280 goto got_dos_table; /* skip reading disk */
1281
1282 if ((fd = open(disk_device, type_open)) < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001283 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1284 if (what == try_only)
1285 return 1;
1286 fdisk_fatal(unable_to_open);
1287 } else
1288 printf(_("You will not be able to write "
1289 "the partition table.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 }
1291
1292 if (512 != read(fd, MBRbuffer, 512)) {
1293 if (what == try_only)
1294 return 1;
1295 fdisk_fatal(unable_to_read);
1296 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001297#else
1298 if ((fd = open(disk_device, O_RDONLY)) < 0)
1299 return 1;
1300 if (512 != read(fd, MBRbuffer, 512))
1301 return 1;
1302#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001304 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001305
1306 update_units();
1307
Denis Vlasenko834410a2006-11-29 12:00:28 +00001308#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309 if (check_sun_label())
1310 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001311#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001312
Denis Vlasenko834410a2006-11-29 12:00:28 +00001313#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001314 if (check_sgi_label())
1315 return 0;
1316#endif
1317
Denis Vlasenko834410a2006-11-29 12:00:28 +00001318#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319 if (check_aix_label())
1320 return 0;
1321#endif
1322
Denis Vlasenko834410a2006-11-29 12:00:28 +00001323#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324 if (check_osf_label()) {
1325 possibly_osf_label = 1;
1326 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001327 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001328 return 0;
1329 }
1330 printf(_("This disk has both DOS and BSD magic.\n"
1331 "Give the 'b' command to go to BSD mode.\n"));
1332 }
1333#endif
1334
Denis Vlasenko834410a2006-11-29 12:00:28 +00001335#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001336 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001337#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001338
1339 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001340#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001341 return -1;
1342#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001343 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001344 case fdisk:
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001345 printf(_("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 "partition table, nor Sun, SGI or OSF "
1347 "disklabel\n"));
1348#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001349#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001350 create_sunlabel();
1351#endif
1352#else
1353 create_doslabel();
1354#endif
1355 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 case try_only:
1357 return -1;
1358 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001359#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001360 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001361#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 break;
1363 default:
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001364 bb_error_msg_and_die(_("internal error"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001366#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001367 }
1368
Denis Vlasenko834410a2006-11-29 12:00:28 +00001369#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001370 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001371#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372 warn_geometry();
1373
1374 for (i = 0; i < 4; i++) {
1375 struct pte *pe = &ptes[i];
1376
Rob Landleyb73451d2006-02-24 16:29:00 +00001377 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 if (partitions != 4)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001379 printf(_("Ignoring extra extended "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380 "partition %d\n"), i + 1);
1381 else
1382 read_extended(i);
1383 }
1384 }
1385
1386 for (i = 3; i < partitions; i++) {
1387 struct pte *pe = &ptes[i];
1388
1389 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00001390 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 "table %d will be corrected by w(rite)\n"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00001392 pe->sectorbuffer[510],
1393 pe->sectorbuffer[511],
1394 i + 1);
1395#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001396 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001397#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001398 }
1399 }
1400
1401 return 0;
1402}
1403
Denis Vlasenko834410a2006-11-29 12:00:28 +00001404#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001405/*
1406 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1407 * If the user hits Enter, DFLT is returned.
1408 * Answers like +10 are interpreted as offsets from BASE.
1409 *
1410 * There is no default if DFLT is not between LOW and HIGH.
1411 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001412static unsigned
1413read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001415 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001417 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001418
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001419 if (dflt < low || dflt > high) {
1420 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001421 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001422 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001423
1424 while (1) {
1425 int use_default = default_ok;
1426
1427 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001428 do {
1429 printf(fmt, mesg, low, high, dflt);
1430 read_maybe_empty("");
1431 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1432 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001433
Eric Andersen84bdea82004-05-19 10:49:17 +00001434 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001435 int minus = (*line_ptr == '-');
1436 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001437
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001438 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001439
Rob Landleyb73451d2006-02-24 16:29:00 +00001440 while (isdigit(*++line_ptr))
1441 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001442
Rob Landleyb73451d2006-02-24 16:29:00 +00001443 switch (*line_ptr) {
1444 case 'c':
1445 case 'C':
1446 if (!display_in_cyl_units)
1447 i *= heads * sectors;
1448 break;
1449 case 'K':
1450 absolute = 1024;
1451 break;
1452 case 'k':
1453 absolute = 1000;
1454 break;
1455 case 'm':
1456 case 'M':
1457 absolute = 1000000;
1458 break;
1459 case 'g':
1460 case 'G':
1461 absolute = 1000000000;
1462 break;
1463 default:
1464 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001465 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001466 if (absolute) {
1467 unsigned long long bytes;
1468 unsigned long unit;
1469
1470 bytes = (unsigned long long) i * absolute;
1471 unit = sector_size * units_per_sector;
1472 bytes += unit/2; /* round */
1473 bytes /= unit;
1474 i = bytes;
1475 }
1476 if (minus)
1477 i = -i;
1478 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001479 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001480 i = atoi(line_ptr);
1481 while (isdigit(*line_ptr)) {
1482 line_ptr++;
1483 use_default = 0;
1484 }
1485 }
1486 if (use_default)
Eric Andersen040f4402003-07-30 08:40:37 +00001487 printf(_("Using default value %u\n"), i = dflt);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001488 if (i >= low && i <= high)
1489 break;
1490 else
Denis Vlasenko98ae2162006-10-12 19:30:44 +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
1502 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1503 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 ) {
1510 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) {
1533 printf(_("Selected partition %d\n"), pno+1);
1534 return pno;
1535 }
1536 printf(_("No partition is defined yet!\n"));
1537 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) {
1560 printf(_("Selected partition %d\n"), pno+1);
1561 return pno;
1562 }
1563 printf(_("All primary partitions have been defined already!\n"));
1564 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();
1576 printf(_("Changing display/entry units to %s\n"),
1577 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 Vlasenko98ae2162006-10-12 19:30:44 +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{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001595 dos_compatible_flag = ~dos_compatible_flag;
1596 if (dos_compatible_flag) {
1597 sector_offset = sectors;
1598 printf(_("DOS Compatibility flag is set\n"));
1599 }
1600 else {
1601 sector_offset = 1;
1602 printf(_("DOS Compatibility flag is not set\n"));
1603 }
1604}
1605
1606static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001607delete_partition(int i)
1608{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001609 struct pte *pe = &ptes[i];
1610 struct partition *p = pe->part_table;
1611 struct partition *q = pe->ext_pointer;
1612
1613/* Note that for the fifth partition (i == 4) we don't actually
1614 * decrement partitions.
1615 */
1616
1617 if (warn_geometry())
1618 return; /* C/H/S not set */
1619 pe->changed = 1;
1620
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001621 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001622 sun_delete_partition(i);
1623 return;
1624 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001625 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001626 sgi_delete_partition(i);
1627 return;
1628 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001629
1630 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001631 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001632 partitions = 4;
1633 ptes[ext_index].ext_pointer = NULL;
1634 extended_offset = 0;
1635 }
1636 clear_partition(p);
1637 return;
1638 }
1639
1640 if (!q->sys_ind && i > 4) {
1641 /* the last one in the chain - just delete */
1642 --partitions;
1643 --i;
1644 clear_partition(ptes[i].ext_pointer);
1645 ptes[i].changed = 1;
1646 } else {
1647 /* not the last one - further ones will be moved down */
1648 if (i > 4) {
1649 /* delete this link in the chain */
1650 p = ptes[i-1].ext_pointer;
1651 *p = *q;
1652 set_start_sect(p, get_start_sect(q));
1653 set_nr_sects(p, get_nr_sects(q));
1654 ptes[i-1].changed = 1;
1655 } else if (partitions > 5) { /* 5 will be moved to 4 */
1656 /* the first logical in a longer chain */
1657 pe = &ptes[5];
1658
1659 if (pe->part_table) /* prevent SEGFAULT */
1660 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001661 get_partition_start(pe) -
1662 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 pe->offset = extended_offset;
1664 pe->changed = 1;
1665 }
1666
1667 if (partitions > 5) {
1668 partitions--;
1669 while (i < partitions) {
1670 ptes[i] = ptes[i+1];
1671 i++;
1672 }
1673 } else
1674 /* the only logical: clear only */
1675 clear_partition(ptes[i].part_table);
1676 }
1677}
1678
1679static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001680change_sysid(void)
1681{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001682 int i, sys, origsys;
1683 struct partition *p;
1684
Eric Andersen040f4402003-07-30 08:40:37 +00001685 /* If sgi_label then don't use get_existing_partition,
1686 let the user select a partition, since get_existing_partition()
1687 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001688 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001689 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001690 } else {
1691 i = get_partition(0, partitions);
1692 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001693 if (i == -1)
1694 return;
1695 p = ptes[i].part_table;
1696 origsys = sys = get_sysid(i);
1697
1698 /* if changing types T to 0 is allowed, then
1699 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001700 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001701 printf(_("Partition %d does not exist yet!\n"), i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001702 return;
1703 }
1704 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001705 sys = read_hex (get_sys_types());
1706
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001707 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001708 printf(_("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001709 "(but not to Linux). Having partitions of\n"
1710 "type 0 is probably unwise. You can delete\n"
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001711 "a partition using the 'd' command.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001712 /* break; */
1713 }
1714
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001715 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001716 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 printf(_("You cannot change a partition into"
Rob Landleyb73451d2006-02-24 16:29:00 +00001718 " an extended one or vice versa\n"
1719 "Delete it first.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001720 break;
1721 }
1722 }
1723
1724 if (sys < 256) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001725 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001726 printf(_("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001727 "as Whole disk (5),\n"
1728 "as SunOS/Solaris expects it and "
1729 "even Linux likes it.\n\n"));
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 )
1735 ){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001736 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)"
1739 "as IRIX expects it.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001740 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741 if (sys == origsys)
1742 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001743 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001744 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001745 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001746 sgi_change_sysid(i, sys);
1747 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001749
Rob Landleyb73451d2006-02-24 16:29:00 +00001750 printf(_("Changed system type of partition %d "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001751 "to %x (%s)\n"), i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001752 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001753 ptes[i].changed = 1;
1754 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001755 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756 dos_changed = 1;
1757 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{
1772 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001773
1774 *c = ls / spc;
1775 ls = ls % spc;
1776 *h = ls / sectors;
1777 *s = ls % sectors + 1; /* sectors count from 1 */
1778}
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
1788 if (!heads || !sectors || (partition >= 4))
1789 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? */
1808 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1809 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);
1813 }
1814
1815/* Same physical / logical ending? */
1816 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1817 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);
1821 }
1822
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001823/* Ending on cylinder boundary? */
1824 if (peh != (heads - 1) || pes != sectors) {
Eric Andersen84bdea82004-05-19 10:49:17 +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)
1837 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
1840 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);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001842 printf(_("%d heads, %d sectors/track, %d cylinders"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001843 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001844 if (units_per_sector == 1)
Eric Andersen040f4402003-07-30 08:40:37 +00001845 printf(_(", total %llu sectors"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001846 total_number_of_sectors / (sector_size/512));
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +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;
Eric Andersend9261492004-06-28 23:50:31 +00001862 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001863 int i, last_i = 0;
1864
1865 for (i = 0 ; i < partitions; i++) {
1866 if (i == 4) {
1867 last_i = 4;
1868 last_p_start_pos = 0;
1869 }
1870 pe = &ptes[i];
1871 if ((p = pe->part_table)->sys_ind) {
1872 p_start_pos = get_partition_start(pe);
1873
1874 if (last_p_start_pos > p_start_pos) {
1875 if (prev)
1876 *prev = last_i;
1877 return i;
1878 }
1879
1880 last_p_start_pos = p_start_pos;
1881 last_i = i;
1882 }
1883 }
1884 return 0;
1885}
1886
Denis Vlasenko834410a2006-11-29 12:00:28 +00001887#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001888/*
1889 * Fix the chain of logicals.
1890 * extended_offset is unchanged, the set of sectors used is unchanged
1891 * The chain is sorted so that sectors increase, and so that
1892 * starting sectors increase.
1893 *
1894 * After this it may still be that cfdisk doesnt like the table.
1895 * (This is because cfdisk considers expanded parts, from link to
1896 * end of partition, and these may still overlap.)
1897 * Now
1898 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1899 * may help.
1900 */
1901static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001902fix_chain_of_logicals(void)
1903{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001904 int j, oj, ojj, sj, sjj;
1905 struct partition *pj,*pjj,tmp;
1906
1907 /* Stage 1: sort sectors but leave sector of part 4 */
1908 /* (Its sector is the global extended_offset.) */
1909 stage1:
1910 for (j = 5; j < partitions-1; j++) {
1911 oj = ptes[j].offset;
1912 ojj = ptes[j+1].offset;
1913 if (oj > ojj) {
1914 ptes[j].offset = ojj;
1915 ptes[j+1].offset = oj;
1916 pj = ptes[j].part_table;
1917 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1918 pjj = ptes[j+1].part_table;
1919 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1920 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001921 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001923 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001924 goto stage1;
1925 }
1926 }
1927
1928 /* Stage 2: sort starting sectors */
1929 stage2:
1930 for (j = 4; j < partitions-1; j++) {
1931 pj = ptes[j].part_table;
1932 pjj = ptes[j+1].part_table;
1933 sj = get_start_sect(pj);
1934 sjj = get_start_sect(pjj);
1935 oj = ptes[j].offset;
1936 ojj = ptes[j+1].offset;
1937 if (oj+sj > ojj+sjj) {
1938 tmp = *pj;
1939 *pj = *pjj;
1940 *pjj = tmp;
1941 set_start_sect(pj, ojj+sjj-oj);
1942 set_start_sect(pjj, oj+sj-ojj);
1943 goto stage2;
1944 }
1945 }
1946
1947 /* Probably something was changed */
1948 for (j = 4; j < partitions; j++)
1949 ptes[j].changed = 1;
1950}
1951
1952
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001953static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001954fix_partition_table_order(void)
1955{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001956 struct pte *pei, *pek;
1957 int i,k;
1958
1959 if (!wrong_p_order(NULL)) {
1960 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1961 return;
1962 }
1963
1964 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1965 /* partition i should have come earlier, move it */
1966 /* We have to move data in the MBR */
1967 struct partition *pi, *pk, *pe, pbuf;
1968 pei = &ptes[i];
1969 pek = &ptes[k];
1970
1971 pe = pei->ext_pointer;
1972 pei->ext_pointer = pek->ext_pointer;
1973 pek->ext_pointer = pe;
1974
1975 pi = pei->part_table;
1976 pk = pek->part_table;
1977
1978 memmove(&pbuf, pi, sizeof(struct partition));
1979 memmove(pi, pk, sizeof(struct partition));
1980 memmove(pk, &pbuf, sizeof(struct partition));
1981
1982 pei->changed = pek->changed = 1;
1983 }
1984
1985 if (i)
1986 fix_chain_of_logicals();
1987
1988 printf("Done.\n");
1989
1990}
1991#endif
1992
1993static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001994list_table(int xtra)
1995{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001996 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001997 int i, w;
1998
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001999 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002000 sun_list_table(xtra);
2001 return;
2002 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002003 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002004 sgi_list_table(xtra);
2005 return;
2006 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002007
2008 list_disk_geometry();
2009
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002010 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011 xbsd_print_disklabel(xtra);
2012 return;
2013 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002014
2015 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2016 but if the device name ends in a digit, say /dev/foo1,
2017 then the partition is called /dev/foo1p3. */
2018 w = strlen(disk_device);
2019 if (w && isdigit(disk_device[w-1]))
2020 w++;
2021 if (w < 5)
2022 w = 5;
2023
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002024 // 1 12345678901 12345678901 12345678901 12
2025 printf(_("%*s Boot Start End Blocks Id System\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002026 w+1, _("Device"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002027
2028 for (i = 0; i < partitions; i++) {
2029 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002030 off_t psects;
2031 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002032 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002033
2034 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002035 if (!p || is_cleared_partition(p))
2036 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002037
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002038 psects = get_nr_sects(p);
2039 pblocks = psects;
2040 podd = 0;
2041
2042 if (sector_size < 1024) {
2043 pblocks /= (1024 / sector_size);
2044 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002046 if (sector_size > 1024)
2047 pblocks *= (sector_size / 1024);
2048
2049 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2050 partname(disk_device, i+1, w+2),
2051 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2052 ? '*' : '?',
2053 (unsigned long long) cround(get_partition_start(pe)), /* start */
2054 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2055 - (psects ? 1 : 0)),
2056 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2057 p->sys_ind, /* type id */
2058 partition_type(p->sys_ind)); /* type name */
2059
2060 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002061 }
2062
2063 /* Is partition table in disk order? It need not be, but... */
2064 /* partition table entries are not checked for correct order if this
2065 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002066 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002067 /* FIXME */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002068 printf(_("\nPartition table entries are not in disk order\n"));
2069 }
2070}
2071
Denis Vlasenko834410a2006-11-29 12:00:28 +00002072#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002073static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002074x_list_table(int extend)
2075{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002076 const struct pte *pe;
2077 const struct partition *p;
2078 int i;
2079
2080 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2081 disk_device, heads, sectors, cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002082 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002083 for (i = 0 ; i < partitions; i++) {
2084 pe = &ptes[i];
2085 p = (extend ? pe->ext_pointer : pe->part_table);
2086 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002087 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002088 i + 1, p->boot_ind, p->head,
2089 sector(p->sector),
2090 cylinder(p->sector, p->cyl), p->end_head,
2091 sector(p->end_sector),
2092 cylinder(p->end_sector, p->end_cyl),
2093 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2094 if (p->sys_ind)
2095 check_consistency(p, i);
2096 }
2097 }
2098}
2099#endif
2100
Denis Vlasenko834410a2006-11-29 12:00:28 +00002101#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002103fill_bounds(off_t *first, off_t *last)
2104{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002105 int i;
2106 const struct pte *pe = &ptes[0];
2107 const struct partition *p;
2108
2109 for (i = 0; i < partitions; pe++,i++) {
2110 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002111 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002112 first[i] = 0xffffffff;
2113 last[i] = 0;
2114 } else {
2115 first[i] = get_partition_start(pe);
2116 last[i] = first[i] + get_nr_sects(p) - 1;
2117 }
2118 }
2119}
2120
2121static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002122check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002123{
Eric Andersend9261492004-06-28 23:50:31 +00002124 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125
2126 real_s = sector(s) - 1;
2127 real_c = cylinder(s, c);
2128 total = (real_c * sectors + real_s) * heads + h;
2129 if (!total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002130 printf(_("Warning: partition %d contains sector 0\n"), n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002131 if (h >= heads)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002132 printf(_("Partition %d: head %d greater than maximum %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133 n, h + 1, heads);
2134 if (real_s >= sectors)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002135 printf(_("Partition %d: sector %d greater than "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136 "maximum %d\n"), n, s, sectors);
2137 if (real_c >= cylinders)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002138 printf(_("Partitions %d: cylinder %llu greater than "
Eric Andersend9261492004-06-28 23:50:31 +00002139 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 if (cylinders <= 1024 && start != total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002141 printf(_("Partition %d: previous sectors %llu disagrees with "
Eric Andersend9261492004-06-28 23:50:31 +00002142 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002143}
2144
2145static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002146verify(void)
2147{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002149 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002150 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002151 struct partition *p;
2152
2153 if (warn_geometry())
2154 return;
2155
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002156 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002157 verify_sun();
2158 return;
2159 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002160 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002161 verify_sgi(1);
2162 return;
2163 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164
2165 fill_bounds(first, last);
2166 for (i = 0; i < partitions; i++) {
2167 struct pte *pe = &ptes[i];
2168
2169 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002170 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002171 check_consistency(p, i);
2172 if (get_partition_start(pe) < first[i])
2173 printf(_("Warning: bad start-of-data in "
2174 "partition %d\n"), i + 1);
2175 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2176 last[i]);
2177 total += last[i] + 1 - first[i];
2178 for (j = 0; j < i; j++)
2179 if ((first[i] >= first[j] && first[i] <= last[j])
2180 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2181 printf(_("Warning: partition %d overlaps "
2182 "partition %d.\n"), j + 1, i + 1);
2183 total += first[i] >= first[j] ?
2184 first[i] : first[j];
2185 total -= last[i] <= last[j] ?
2186 last[i] : last[j];
2187 }
2188 }
2189 }
2190
2191 if (extended_offset) {
2192 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002193 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002194 get_nr_sects(pex->part_table) - 1;
2195
2196 for (i = 4; i < partitions; i++) {
2197 total++;
2198 p = ptes[i].part_table;
2199 if (!p->sys_ind) {
2200 if (i != 4 || i + 1 < partitions)
2201 printf(_("Warning: partition %d "
2202 "is empty\n"), i + 1);
2203 }
2204 else if (first[i] < extended_offset ||
2205 last[i] > e_last)
2206 printf(_("Logical partition %d not entirely in "
2207 "partition %d\n"), i + 1, ext_index + 1);
2208 }
2209 }
2210
2211 if (total > heads * sectors * cylinders)
2212 printf(_("Total allocated sectors %d greater than the maximum "
2213 "%d\n"), total, heads * sectors * cylinders);
2214 else if ((total = heads * sectors * cylinders - total) != 0)
2215 printf(_("%d unallocated sectors\n"), total);
2216}
2217
2218static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002219add_partition(int n, int sys)
2220{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002222 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002223 struct partition *p = ptes[n].part_table;
2224 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002225 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002226 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 first[partitions], last[partitions];
2228
2229 if (p && p->sys_ind) {
2230 printf(_("Partition %d is already defined. Delete "
2231 "it before re-adding it.\n"), n + 1);
2232 return;
2233 }
2234 fill_bounds(first, last);
2235 if (n < 4) {
2236 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002237 if (display_in_cyl_units || !total_number_of_sectors)
2238 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002239 else
Eric Andersen040f4402003-07-30 08:40:37 +00002240 llimit = total_number_of_sectors - 1;
2241 limit = llimit;
2242 if (limit != llimit)
2243 limit = 0x7fffffff;
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)
2254 for (i = 0; i < partitions; i++)
2255 first[i] = (cround(first[i]) - 1) * units_per_sector;
2256
2257 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2258 do {
2259 temp = start;
2260 for (i = 0; i < partitions; i++) {
2261 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 Vlasenkocf30cc82006-11-24 14:53:18 +00002272 printf(_("Sector %"OFF_FMT"d 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) {
Eric Andersend9261492004-06-28 23:50:31 +00002277 off_t 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
2300 for (i = 0; i < partitions; i++) {
2301 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) {
2309 printf(_("No free sectors available\n"));
2310 if (n > 4)
2311 partitions--;
2312 return;
2313 }
2314 if (cround(start) == cround(limit)) {
2315 stop = limit;
2316 } else {
2317 snprintf(mesg, sizeof(mesg),
2318 _("Last %s or +size or +sizeM or +sizeK"),
2319 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;
2344 partitions = 5;
2345 }
2346}
2347
2348static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002349add_logical(void)
2350{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002351 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2352 struct pte *pe = &ptes[partitions];
2353
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;
2359 partitions++;
2360 }
2361 add_partition(partitions - 1, LINUX_NATIVE);
2362}
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) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002373 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2374 return;
2375 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002376 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2378 return;
2379 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002380 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2382 "\n\tIf you want to add DOS-type partitions, create"
2383 "\n\ta new empty DOS partition table first. (Use o.)"
2384 "\n\tWARNING: "
2385 "This will destroy the present disk contents.\n"));
2386 return;
2387 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002388
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002389 for (i = 0; i < 4; i++)
2390 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002391
Rob Landleyb73451d2006-02-24 16:29:00 +00002392 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Eric Andersen84bdea82004-05-19 10:49:17 +00002393 printf(_("The maximum number of partitions has been created\n"));
2394 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002395 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002396
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 if (!free_primary) {
2398 if (extended_offset)
2399 add_logical();
2400 else
2401 printf(_("You must delete some partition and add "
2402 "an extended partition first\n"));
2403 } else {
2404 char c, line[LINE_LENGTH];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002405 snprintf(line, sizeof(line),
2406 "Command action\n"
2407 " %s\n"
2408 " p primary partition (1-4)\n",
2409 (extended_offset ?
2410 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002411 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002412 c = read_nonempty(line);
2413 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002414 i = get_nonexisting_partition(0, 4);
2415 if (i >= 0)
2416 add_partition(i, LINUX_NATIVE);
2417 return;
2418 }
2419 else if (c == 'l' && extended_offset) {
2420 add_logical();
2421 return;
2422 }
2423 else if (c == 'e' && !extended_offset) {
2424 i = get_nonexisting_partition(0, 4);
2425 if (i >= 0)
2426 add_partition(i, EXTENDED);
2427 return;
2428 }
2429 else
2430 printf(_("Invalid partition number "
Denis Vlasenko89f0b342006-11-18 22:04:09 +00002431 "for type '%c'\n"), c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 }
2433 }
2434}
2435
2436static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002437write_table(void)
2438{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 int i;
2440
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002441 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002442 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 if (ptes[i].changed)
2444 ptes[3].changed = 1;
2445 for (i = 3; i < partitions; i++) {
2446 struct pte *pe = &ptes[i];
2447
2448 if (pe->changed) {
2449 write_part_table_flag(pe->sectorbuffer);
2450 write_sector(pe->offset, pe->sectorbuffer);
2451 }
2452 }
2453 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002454 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455 /* no test on change? the printf below might be mistaken */
2456 sgi_write_table();
2457 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002458 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 int needw = 0;
2460
Rob Landleyb73451d2006-02-24 16:29:00 +00002461 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 if (ptes[i].changed)
2463 needw = 1;
2464 if (needw)
2465 sun_write_table();
2466 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002467
2468 printf(_("The partition table has been altered!\n\n"));
2469 reread_partition_table(1);
2470}
2471
Rob Landleyb73451d2006-02-24 16:29:00 +00002472static void
2473reread_partition_table(int leave)
2474{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 int i;
2476
Denis Vlasenko28703012006-12-19 20:32:02 +00002477 printf(_("Calling ioctl() to re-read partition table\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478 sync();
Denis Vlasenko28703012006-12-19 20:32:02 +00002479 sleep(2); /* Huh? */
2480 i = ioctl(fd, BLKRRPART);
2481#if 0
2482 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483 /* some kernel versions (1.2.x) seem to have trouble
2484 rereading the partition table, but if asked to do it
2485 twice, the second time works. - biro@yggdrasil.com */
2486 sync();
2487 sleep(2);
Denis Vlasenko28703012006-12-19 20:32:02 +00002488 i = ioctl(fd, BLKRRPART);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 }
Denis Vlasenko28703012006-12-19 20:32:02 +00002490#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491
2492 if (i) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002493 bb_perror_msg("WARNING: rereading partition table "
2494 "failed, kernel still uses old table");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002495 }
2496
Denis Vlasenko28703012006-12-19 20:32:02 +00002497#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002499 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002500 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2501 "partitions, please see the fdisk manual page for additional\n"
2502 "information.\n"));
Denis Vlasenko28703012006-12-19 20:32:02 +00002503#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002504
2505 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002506 if (ENABLE_FEATURE_CLEAN_UP)
2507 close(fd);
2508 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 }
2510}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002511#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512
Denis Vlasenko834410a2006-11-29 12:00:28 +00002513#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002514#define MAX_PER_LINE 16
2515static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002516print_buffer(char *pbuffer)
2517{
2518 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519
2520 for (i = 0, l = 0; i < sector_size; i++, l++) {
2521 if (l == 0)
2522 printf("0x%03X:", i);
2523 printf(" %02X", (unsigned char) pbuffer[i]);
2524 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002525 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 l = -1;
2527 }
2528 }
2529 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002530 puts("");
2531 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002532}
2533
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002535print_raw(void)
2536{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 int i;
2538
2539 printf(_("Device: %s\n"), disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002540 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002542 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543 for (i = 3; i < partitions; i++)
2544 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002545 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546}
2547
2548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002549move_begin(int i)
2550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551 struct pte *pe = &ptes[i];
2552 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002553 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554
2555 if (warn_geometry())
2556 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002557 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002558 printf(_("Partition %d has no data area\n"), i + 1);
2559 return;
2560 }
2561 first = get_partition_start(pe);
2562 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Rob Landleyb73451d2006-02-24 16:29:00 +00002563 _("New beginning of data")) - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564
2565 if (new != get_nr_sects(p)) {
2566 first = get_nr_sects(p) + get_start_sect(p) - new;
2567 set_nr_sects(p, first);
2568 set_start_sect(p, new);
2569 pe->changed = 1;
2570 }
2571}
2572
2573static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002574xselect(void)
2575{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002576 char c;
2577
Rob Landleyb73451d2006-02-24 16:29:00 +00002578 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002579 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 c = tolower(read_nonempty(_("Expert command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 switch (c) {
2582 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002583 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002584 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585 break;
2586 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 move_begin(get_partition(0, partitions));
2589 break;
2590 case 'c':
2591 user_cylinders = cylinders =
2592 read_int(1, cylinders, 1048576, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002593 _("Number of cylinders"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002596 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 warn_cylinders();
2598 break;
2599 case 'd':
2600 print_raw();
2601 break;
2602 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002603 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002604 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002605 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 x_list_table(1);
2609 break;
2610 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002611 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002612 fix_partition_table_order();
2613 break;
2614 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002615#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 create_sgilabel();
2617#endif
2618 break;
2619 case 'h':
2620 user_heads = heads = read_int(1, heads, 256, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002621 _("Number of heads"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 update_units();
2623 break;
2624 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 break;
2628 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002629 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002630 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002631 break;
2632 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002633 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 list_table(1);
2635 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002636 x_list_table(0);
2637 break;
2638 case 'q':
2639 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002640 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 exit(0);
2642 case 'r':
2643 return;
2644 case 's':
2645 user_sectors = sectors = read_int(1, sectors, 63, 0,
2646 _("Number of sectors"));
2647 if (dos_compatible_flag) {
2648 sector_offset = sectors;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002649 printf(_("Warning: setting sector offset for DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002650 "compatiblity\n"));
2651 }
2652 update_units();
2653 break;
2654 case 'v':
2655 verify();
2656 break;
2657 case 'w':
2658 write_table(); /* does not return */
2659 break;
2660 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002661 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002662 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 break;
2664 default:
2665 xmenu();
2666 }
2667 }
2668}
2669#endif /* ADVANCED mode */
2670
2671static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002672is_ide_cdrom_or_tape(const char *device)
2673{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002674 FILE *procf;
2675 char buf[100];
2676 struct stat statbuf;
2677 int is_ide = 0;
2678
2679 /* No device was given explicitly, and we are trying some
2680 likely things. But opening /dev/hdc may produce errors like
2681 "hdc: tray open or drive not ready"
2682 if it happens to be a CD-ROM drive. It even happens that
2683 the process hangs on the attempt to read a music CD.
2684 So try to be careful. This only works since 2.1.73. */
2685
2686 if (strncmp("/dev/hd", device, 7))
2687 return 0;
2688
2689 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2690 procf = fopen(buf, "r");
2691 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2692 is_ide = (!strncmp(buf, "cdrom", 5) ||
2693 !strncmp(buf, "tape", 4));
2694 else
2695 /* Now when this proc file does not exist, skip the
2696 device when it is read-only. */
2697 if (stat(device, &statbuf) == 0)
2698 is_ide = ((statbuf.st_mode & 0222) == 0);
2699
2700 if (procf)
2701 fclose(procf);
2702 return is_ide;
2703}
2704
Rob Landley5527b912006-02-25 03:46:10 +00002705
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002707trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002708{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 int gb;
2710
2711 disk_device = device;
2712 if (setjmp(listingbuf))
2713 return;
2714 if (!user_specified)
2715 if (is_ide_cdrom_or_tape(device))
2716 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002717 fd = open(disk_device, type_open);
2718 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002719 gb = get_boot(try_only);
2720 if (gb > 0) { /* I/O error */
2721 close(fd);
2722 } else if (gb < 0) { /* no DOS signature */
2723 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002724 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002725 return;
Rob Landley5527b912006-02-25 03:46:10 +00002726 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002727#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002728 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002729#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002730 printf(_("Disk %s doesn't contain a valid "
Rob Landleyb73451d2006-02-24 16:29:00 +00002731 "partition table\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002732 close(fd);
2733 } else {
2734 close(fd);
2735 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002736#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002737 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002739 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002740#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002741 }
2742 } else {
2743 /* Ignore other errors, since we try IDE
2744 and SCSI hard disks which may not be
2745 installed on the system. */
2746 if (errno == EACCES) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002747 printf(_("Cannot open %s\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002748 return;
2749 }
2750 }
2751}
2752
2753/* for fdisk -l: try all things in /proc/partitions
2754 that look like a partition name (do not end in a digit) */
2755static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002756tryprocpt(void)
2757{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758 FILE *procpt;
2759 char line[100], ptname[100], devname[120], *s;
2760 int ma, mi, sz;
2761
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002762 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002763
2764 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002765 if (sscanf(line, " %d %d %d %[^\n ]",
2766 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002767 continue;
2768 for (s = ptname; *s; s++);
2769 if (isdigit(s[-1]))
2770 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002771 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002772 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002773 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002774#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002775 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002776#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777}
2778
Denis Vlasenko834410a2006-11-29 12:00:28 +00002779#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002780static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002781unknown_command(int c)
2782{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002783 printf(_("%c: unknown command\n"), c);
2784}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002785#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786
Rob Landleyb73451d2006-02-24 16:29:00 +00002787int fdisk_main(int argc, char **argv)
2788{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002789 char *str_b, *str_C, *str_H, *str_S;
2790 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002792 * fdisk -v
2793 * fdisk -l [-b sectorsize] [-u] device ...
2794 * fdisk -s [partition] ...
2795 * fdisk [-b sectorsize] [-u] device
2796 *
2797 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002798 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002799 enum {
2800 OPT_b = 1 << 0,
2801 OPT_C = 1 << 1,
2802 OPT_H = 1 << 2,
2803 OPT_l = 1 << 3,
2804 OPT_S = 1 << 4,
2805 OPT_u = 1 << 5,
2806 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2807 };
2808 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2809 &str_b, &str_C, &str_H, &str_S);
2810 argc -= optind;
2811 argv += optind;
2812 if (opt & OPT_b) { // -b
2813 /* Ugly: this sector size is really per device,
2814 so cannot be combined with multiple disks,
2815 and the same goes for the C/H/S options.
2816 */
2817 sector_size = xatoi_u(str_b);
2818 if (sector_size != 512 && sector_size != 1024 &&
2819 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002820 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002821 sector_offset = 2;
2822 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002823 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002824 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2825 if (opt & OPT_H) { // -H
2826 user_heads = xatoi_u(str_H);
2827 if (user_heads <= 0 || user_heads >= 256)
2828 user_heads = 0;
2829 }
2830 //if (opt & OPT_l) // -l
2831 if (opt & OPT_S) { // -S
2832 user_sectors = xatoi_u(str_S);
2833 if (user_sectors <= 0 || user_sectors >= 64)
2834 user_sectors = 0;
2835 }
2836 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2837 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002838
Denis Vlasenko834410a2006-11-29 12:00:28 +00002839 if (user_set_sector_size && argc != 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002840 printf(_("Warning: the -b (set sector size) option should"
2841 " be used with one specified device\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002842
Denis Vlasenko834410a2006-11-29 12:00:28 +00002843#if ENABLE_FEATURE_FDISK_WRITABLE
2844 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002845 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002846#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002847 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002848 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002850#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 /* avoid gcc warning:
2852 variable `k' might be clobbered by `longjmp' */
2853 (void)&k;
2854#endif
2855 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002856 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002857 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858 } else {
2859 /* we no longer have default device names */
2860 /* but, we can use /proc/partitions instead */
2861 tryprocpt();
2862 }
2863 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002864#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002866#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867
Denis Vlasenko834410a2006-11-29 12:00:28 +00002868#if ENABLE_FEATURE_FDISK_BLKSIZE
2869 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002870 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002871 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872
2873 nowarn = 1;
2874 type_open = O_RDONLY;
2875
Denis Vlasenko834410a2006-11-29 12:00:28 +00002876 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002877 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878
Denis Vlasenko834410a2006-11-29 12:00:28 +00002879 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002881 fd = open(disk_device, type_open);
2882 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883 fdisk_fatal(unable_to_open);
2884 if (ioctl(fd, BLKGETSIZE, &size))
2885 fdisk_fatal(ioctl_error);
2886 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002887 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 printf("%ld\n", size/2);
2889 else
2890 printf("%s: %ld\n", argv[j], size/2);
2891 }
2892 return 0;
2893 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002894#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895
Denis Vlasenko834410a2006-11-29 12:00:28 +00002896#if ENABLE_FEATURE_FDISK_WRITABLE
2897 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002898 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002899
Denis Vlasenko834410a2006-11-29 12:00:28 +00002900 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002901 get_boot(fdisk);
2902
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002903 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002904 /* OSF label, and no DOS label */
2905 printf(_("Detected an OSF/1 disklabel on %s, entering "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002906 "disklabel mode.\n"), disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002907 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002908 /*Why do we do this? It seems to be counter-intuitive*/
2909 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 /* If we return we may want to make an empty DOS label? */
2911 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002912
2913 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002914 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002916 c = tolower(read_nonempty(_("Command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 switch (c) {
2918 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002919 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002920 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002921 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002922 toggle_sunflags(get_partition(1, partitions),
2923 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002924 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925 sgi_set_bootpartition(
2926 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002927 else
2928 unknown_command(c);
2929 break;
2930 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002931 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 printf(_("\nThe current boot file is: %s\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002933 sgi_get_bootfile());
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 if (read_maybe_empty(_("Please enter the name of the "
Rob Landleyb73451d2006-02-24 16:29:00 +00002935 "new boot file: ")) == '\n')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 printf(_("Boot file unchanged\n"));
2937 else
2938 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002939 }
2940#if ENABLE_FEATURE_OSF_LABEL
2941 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002942 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002943#endif
2944 break;
2945 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002946 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002948 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002949 toggle_sunflags(get_partition(1, partitions),
2950 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002951 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002952 sgi_set_swappartition(
2953 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002954 else
2955 unknown_command(c);
2956 break;
2957 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002958 {
Eric Andersen040f4402003-07-30 08:40:37 +00002959 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002960 /* If sgi_label then don't use get_existing_partition,
2961 let the user select a partition, since
2962 get_existing_partition() only works for Linux-like
2963 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002964 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002965 j = get_existing_partition(1, partitions);
2966 } else {
2967 j = get_partition(1, partitions);
2968 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002969 if (j >= 0)
2970 delete_partition(j);
2971 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002972 break;
2973 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002974 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002975 create_sgiinfo();
2976 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002977 unknown_command(c);
2978 case 'l':
2979 list_types(get_sys_types());
2980 break;
2981 case 'm':
2982 menu();
2983 break;
2984 case 'n':
2985 new_partition();
2986 break;
2987 case 'o':
2988 create_doslabel();
2989 break;
2990 case 'p':
2991 list_table(0);
2992 break;
2993 case 'q':
2994 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002995 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002996 return 0;
2997 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002998#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999 create_sunlabel();
3000#endif
3001 break;
3002 case 't':
3003 change_sysid();
3004 break;
3005 case 'u':
3006 change_units();
3007 break;
3008 case 'v':
3009 verify();
3010 break;
3011 case 'w':
3012 write_table(); /* does not return */
3013 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003014#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003015 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003016 if (LABEL_IS_SGI) {
3017 printf(_("\n\tSorry, no experts menu for SGI "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003018 "partition tables available.\n\n"));
3019 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003020 xselect();
3021 break;
3022#endif
3023 default:
3024 unknown_command(c);
3025 menu();
3026 }
3027 }
3028 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003029#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003030}