blob: cc6dfa57a034cf7ccd0c6598b0685a6a884b1f5f [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
205#define LINE_LENGTH 800
206#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
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000294/* read line; return 0 or first char */
295static int
296read_line(void)
297{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000298 fflush(stdout); /* requested by niles@scyld.com */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000299 line_ptr = line_buffer;
300 if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000301 /* error or eof */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000302 bb_error_msg_and_die("\ngot EOF, exiting");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000303 }
304 while (*line_ptr && !isgraph(*line_ptr))
305 line_ptr++;
306 return *line_ptr;
307}
308
309static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000310read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000311{
312 do {
313 fputs(mesg, stdout);
314 } while (!read_line());
315 return *line_ptr;
316}
317
318static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000319read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000320{
321 fputs(mesg, stdout);
322 if (!read_line()) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000323 line_ptr = line_buffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000324 *line_ptr = '\n';
325 line_ptr[1] = 0;
326 }
327 return *line_ptr;
328}
329
330static int
331read_hex(const struct systypes *sys)
332{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000333 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000334 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000335 read_nonempty(_("Hex code (type L to list codes): "));
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000336 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000337 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000338 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000339 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000340 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000341 if (v > 0xff)
342 /* Bad input also triggers this */
343 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000344 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000345 }
346}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000347#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000348
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000349#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000350
351typedef struct {
352 unsigned char info[128]; /* Informative text string */
353 unsigned char spare0[14];
354 struct sun_info {
355 unsigned char spare1;
356 unsigned char id;
357 unsigned char spare2;
358 unsigned char flags;
359 } infos[8];
360 unsigned char spare1[246]; /* Boot information etc. */
361 unsigned short rspeed; /* Disk rotational speed */
362 unsigned short pcylcount; /* Physical cylinder count */
363 unsigned short sparecyl; /* extra sects per cylinder */
364 unsigned char spare2[4]; /* More magic... */
365 unsigned short ilfact; /* Interleave factor */
366 unsigned short ncyl; /* Data cylinder count */
367 unsigned short nacyl; /* Alt. cylinder count */
368 unsigned short ntrks; /* Tracks per cylinder */
369 unsigned short nsect; /* Sectors per track */
370 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000371 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000372 uint32_t start_cylinder;
373 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000374 } partitions[8];
375 unsigned short magic; /* Magic number */
376 unsigned short csum; /* Label xor'd checksum */
377} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000378#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000379#define SUNOS_SWAP 3
380#define SUN_WHOLE_DISK 5
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000381STATIC_OSF void bsd_select(void);
382STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000383#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000384
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000385#define SGI_VOLHDR 0x00
386/* 1 and 2 were used for drive types no longer supported by SGI */
387#define SGI_SWAP 0x03
388/* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
389#define SGI_VOLUME 0x06
390#define SGI_EFS 0x07
391#define SGI_LVOL 0x08
392#define SGI_RLVOL 0x09
393#define SGI_XFS 0x0a
394#define SGI_XFSLOG 0x0b
395#define SGI_XLV 0x0c
396#define SGI_XVM 0x0d
397#define SGI_ENTIRE_DISK SGI_VOLUME
Denis Vlasenko28703012006-12-19 20:32:02 +0000398#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000399static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000400fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000401{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000402 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000403}
404
Rob Landley88621d72006-08-29 19:41:06 +0000405static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000406fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000407{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000408 return (x << 24) |
409 ((x & 0xFF00) << 8) |
410 ((x & 0xFF0000) >> 8) |
411 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000412}
413#endif
414
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000415STATIC_SGI const struct systypes sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000416STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000417STATIC_SGI int sgi_get_sysid(int i);
418STATIC_SGI void sgi_delete_partition(int i);
419STATIC_SGI void sgi_change_sysid(int i, int sys);
420STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000421#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000422STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000423#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000424STATIC_SGI int verify_sgi(int verbose);
425STATIC_SGI void sgi_add_partition(int n, int sys);
426STATIC_SGI void sgi_set_swappartition(int i);
427STATIC_SGI const char *sgi_get_bootfile(void);
428STATIC_SGI void sgi_set_bootfile(const char* aFile);
429STATIC_SGI void create_sgiinfo(void);
430STATIC_SGI void sgi_write_table(void);
431STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000432#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000433
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000434STATIC_SUN const struct systypes sun_sys_types[];
435STATIC_SUN void sun_delete_partition(int i);
436STATIC_SUN void sun_change_sysid(int i, int sys);
437STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000438STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000439#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000440STATIC_SUN void sun_set_alt_cyl(void);
441STATIC_SUN void sun_set_ncyl(int cyl);
442STATIC_SUN void sun_set_xcyl(void);
443STATIC_SUN void sun_set_ilfact(void);
444STATIC_SUN void sun_set_rspeed(void);
445STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000446#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000447STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
448STATIC_SUN void verify_sun(void);
449STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000450#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000451
452/* DOS partition types */
453
454static const struct systypes i386_sys_types[] = {
Rob Landleyb73451d2006-02-24 16:29:00 +0000455 { "\x00" "Empty" },
456 { "\x01" "FAT12" },
457 { "\x04" "FAT16 <32M" },
458 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
459 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
460 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
461 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
462 { "\x0b" "Win95 FAT32" },
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000463 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
Rob Landleyb73451d2006-02-24 16:29:00 +0000464 { "\x0e" "Win95 FAT16 (LBA)" },
465 { "\x0f" "Win95 Ext'd (LBA)" },
466 { "\x11" "Hidden FAT12" },
467 { "\x12" "Compaq diagnostics" },
468 { "\x14" "Hidden FAT16 <32M" },
469 { "\x16" "Hidden FAT16" },
470 { "\x17" "Hidden HPFS/NTFS" },
471 { "\x1b" "Hidden Win95 FAT32" },
472 { "\x1c" "Hidden Win95 FAT32 (LBA)" },
473 { "\x1e" "Hidden Win95 FAT16 (LBA)" },
474 { "\x3c" "PartitionMagic recovery" },
475 { "\x41" "PPC PReP Boot" },
476 { "\x42" "SFS" },
477 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
478 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
479 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
480 { "\x82" "Linux swap" }, /* also Solaris */
481 { "\x83" "Linux" },
482 { "\x84" "OS/2 hidden C: drive" },
483 { "\x85" "Linux extended" },
484 { "\x86" "NTFS volume set" },
485 { "\x87" "NTFS volume set" },
486 { "\x8e" "Linux LVM" },
487 { "\x9f" "BSD/OS" }, /* BSDI */
488 { "\xa0" "IBM Thinkpad hibernation" },
489 { "\xa5" "FreeBSD" }, /* various BSD flavours */
490 { "\xa6" "OpenBSD" },
491 { "\xa8" "Darwin UFS" },
492 { "\xa9" "NetBSD" },
493 { "\xab" "Darwin boot" },
494 { "\xb7" "BSDI fs" },
495 { "\xb8" "BSDI swap" },
496 { "\xbe" "Solaris boot" },
497 { "\xeb" "BeOS fs" },
498 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
499 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
500 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
501 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
502 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
503 autodetect using persistent
504 superblock */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000505#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
Rob Landleyb73451d2006-02-24 16:29:00 +0000506 { "\x02" "XENIX root" },
507 { "\x03" "XENIX usr" },
508 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
509 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
510 { "\x10" "OPUS" },
511 { "\x18" "AST SmartSleep" },
512 { "\x24" "NEC DOS" },
513 { "\x39" "Plan 9" },
514 { "\x40" "Venix 80286" },
515 { "\x4d" "QNX4.x" },
516 { "\x4e" "QNX4.x 2nd part" },
517 { "\x4f" "QNX4.x 3rd part" },
518 { "\x50" "OnTrack DM" },
519 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
520 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
521 { "\x53" "OnTrack DM6 Aux3" },
522 { "\x54" "OnTrackDM6" },
523 { "\x55" "EZ-Drive" },
524 { "\x56" "Golden Bow" },
525 { "\x5c" "Priam Edisk" },
526 { "\x61" "SpeedStor" },
527 { "\x64" "Novell Netware 286" },
528 { "\x65" "Novell Netware 386" },
529 { "\x70" "DiskSecure Multi-Boot" },
530 { "\x75" "PC/IX" },
531 { "\x93" "Amoeba" },
532 { "\x94" "Amoeba BBT" }, /* (bad block table) */
533 { "\xa7" "NeXTSTEP" },
534 { "\xbb" "Boot Wizard hidden" },
535 { "\xc1" "DRDOS/sec (FAT-12)" },
536 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
537 { "\xc6" "DRDOS/sec (FAT-16)" },
538 { "\xc7" "Syrinx" },
539 { "\xda" "Non-FS data" },
540 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000541 Concurrent DOS or CTOS */
Rob Landleyb73451d2006-02-24 16:29:00 +0000542 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
543 { "\xdf" "BootIt" }, /* BootIt EMBRM */
544 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000545 extended partition */
Rob Landleyb73451d2006-02-24 16:29:00 +0000546 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
547 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000548 partition < 1024 cyl. */
Rob Landleyb73451d2006-02-24 16:29:00 +0000549 { "\xf1" "SpeedStor" },
550 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
551 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
552 { "\xff" "BBT" }, /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000553#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554 { 0 }
555};
556
557
Denis Vlasenko834410a2006-11-29 12:00:28 +0000558#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000559/* start_sect and nr_sects are stored little endian on all machines */
560/* moreover, they are not aligned correctly */
561static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000562store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000563{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000564 cp[0] = val;
565 cp[1] = val >> 8;
566 cp[2] = val >> 16;
567 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000568}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000569#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000570
Denis Vlasenko834410a2006-11-29 12:00:28 +0000571static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000572read4_little_endian(const unsigned char *cp)
573{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000574 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000575}
576
Denis Vlasenko834410a2006-11-29 12:00:28 +0000577#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000578static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000579set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000580{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000581 store4_little_endian(p->start4, start_sect);
582}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000583#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000584
Denis Vlasenko28703012006-12-19 20:32:02 +0000585static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000586get_start_sect(const struct partition *p)
587{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588 return read4_little_endian(p->start4);
589}
590
Denis Vlasenko834410a2006-11-29 12:00:28 +0000591#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000592static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000593set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000594{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595 store4_little_endian(p->size4, nr_sects);
596}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000597#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598
Denis Vlasenko28703012006-12-19 20:32:02 +0000599static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000600get_nr_sects(const struct partition *p)
601{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000602 return read4_little_endian(p->size4);
603}
604
605/* normally O_RDWR, -l option gives O_RDONLY */
606static int type_open = O_RDWR;
607
608
Rob Landleyb73451d2006-02-24 16:29:00 +0000609static int ext_index; /* the prime extended partition */
610static int listing; /* no aborts for fdisk -l */
611static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000612#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000613static int dos_changed;
614static int nowarn; /* no warnings for fdisk -l/-s */
615#endif
616
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000617
618
Denis Vlasenko834410a2006-11-29 12:00:28 +0000619static unsigned user_cylinders, user_heads, user_sectors;
620static unsigned pt_heads, pt_sectors;
621static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000622
Eric Andersend9261492004-06-28 23:50:31 +0000623static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624
Eric Andersen040f4402003-07-30 08:40:37 +0000625static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000626
627
628static jmp_buf listingbuf;
629
Rob Landleyb73451d2006-02-24 16:29:00 +0000630static void fdisk_fatal(enum failure why)
631{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000632 const char *message;
633
634 if (listing) {
635 close(fd);
636 longjmp(listingbuf, 1);
637 }
638
639 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000640 case unable_to_open:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000641 message = "\nUnable to open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000642 break;
643 case unable_to_read:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000644 message = "\nUnable to read %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000645 break;
646 case unable_to_seek:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000647 message = "\nUnable to seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000648 break;
649 case unable_to_write:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000650 message = "\nUnable to write %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000651 break;
652 case ioctl_error:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000653 message = "\nBLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000654 break;
655 default:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000656 message = "\nFatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657 }
658
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000659 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000660}
661
662static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000663seek_sector(off_t secno)
664{
Eric Andersen0a92f352004-03-30 09:21:54 +0000665 off_t offset = secno * sector_size;
666 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000667 fdisk_fatal(unable_to_seek);
668}
669
Denis Vlasenko834410a2006-11-29 12:00:28 +0000670#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000671static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000672write_sector(off_t secno, char *buf)
673{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674 seek_sector(secno);
675 if (write(fd, buf, sector_size) != sector_size)
676 fdisk_fatal(unable_to_write);
677}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000678#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000679
680/* Allocate a buffer and read a partition table sector */
681static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000682read_pte(struct pte *pe, off_t offset)
683{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000684 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000685 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000686 seek_sector(offset);
687 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
688 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000689#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000690 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000691#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692 pe->part_table = pe->ext_pointer = NULL;
693}
694
Denis Vlasenko834410a2006-11-29 12:00:28 +0000695static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000696get_partition_start(const struct pte *pe)
697{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000698 return pe->offset + get_start_sect(pe->part_table);
699}
700
Denis Vlasenko834410a2006-11-29 12:00:28 +0000701#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000702/*
703 * Avoid warning about DOS partitions when no DOS partition was changed.
704 * Here a heuristic "is probably dos partition".
705 * We might also do the opposite and warn in all cases except
706 * for "is probably nondos partition".
707 */
708static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000709is_dos_partition(int t)
710{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000711 return (t == 1 || t == 4 || t == 6 ||
712 t == 0x0b || t == 0x0c || t == 0x0e ||
713 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
714 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
715 t == 0xc1 || t == 0xc4 || t == 0xc6);
716}
717
718static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000719menu(void)
720{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000721 if (LABEL_IS_SUN) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000722 puts(_("Command action"));
723 puts(_("\ta\ttoggle a read only flag")); /* sun */
724 puts(_("\tb\tedit bsd disklabel"));
725 puts(_("\tc\ttoggle the mountable flag")); /* sun */
726 puts(_("\td\tdelete a partition"));
727 puts(_("\tl\tlist known partition types"));
728 puts(_("\tm\tprint this menu"));
729 puts(_("\tn\tadd a new partition"));
730 puts(_("\to\tcreate a new empty DOS partition table"));
731 puts(_("\tp\tprint the partition table"));
732 puts(_("\tq\tquit without saving changes"));
733 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
734 puts(_("\tt\tchange a partition's system id"));
735 puts(_("\tu\tchange display/entry units"));
736 puts(_("\tv\tverify the partition table"));
737 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000738#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000739 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000740#endif
741 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000742 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000743 puts(_("Command action"));
744 puts(_("\ta\tselect bootable partition")); /* sgi flavour */
745 puts(_("\tb\tedit bootfile entry")); /* sgi */
746 puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
747 puts(_("\td\tdelete a partition"));
748 puts(_("\tl\tlist known partition types"));
749 puts(_("\tm\tprint this menu"));
750 puts(_("\tn\tadd a new partition"));
751 puts(_("\to\tcreate a new empty DOS partition table"));
752 puts(_("\tp\tprint the partition table"));
753 puts(_("\tq\tquit without saving changes"));
754 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
755 puts(_("\tt\tchange a partition's system id"));
756 puts(_("\tu\tchange display/entry units"));
757 puts(_("\tv\tverify the partition table"));
758 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000760 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000761 puts(_("Command action"));
762 puts(_("\tm\tprint this menu"));
763 puts(_("\to\tcreate a new empty DOS partition table"));
764 puts(_("\tq\tquit without saving changes"));
765 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000766 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000767 {
Rob Landleyb73451d2006-02-24 16:29:00 +0000768 puts(_("Command action"));
769 puts(_("\ta\ttoggle a bootable flag"));
770 puts(_("\tb\tedit bsd disklabel"));
771 puts(_("\tc\ttoggle the dos compatibility flag"));
772 puts(_("\td\tdelete a partition"));
773 puts(_("\tl\tlist known partition types"));
774 puts(_("\tm\tprint this menu"));
775 puts(_("\tn\tadd a new partition"));
776 puts(_("\to\tcreate a new empty DOS partition table"));
777 puts(_("\tp\tprint the partition table"));
778 puts(_("\tq\tquit without saving changes"));
779 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
780 puts(_("\tt\tchange a partition's system id"));
781 puts(_("\tu\tchange display/entry units"));
782 puts(_("\tv\tverify the partition table"));
783 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000784#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000785 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000786#endif
787 }
788}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000789#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000790
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000791
Denis Vlasenko834410a2006-11-29 12:00:28 +0000792#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000793static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000794xmenu(void)
795{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000796 if (LABEL_IS_SUN) {
797 puts(_("Command action"));
798 puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
799 puts(_("\tc\tchange number of cylinders"));
800 puts(_("\td\tprint the raw data in the partition table"));
801 puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
802 puts(_("\th\tchange number of heads"));
803 puts(_("\ti\tchange interleave factor")); /*sun*/
804 puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
805 puts(_("\tm\tprint this menu"));
806 puts(_("\tp\tprint the partition table"));
807 puts(_("\tq\tquit without saving changes"));
808 puts(_("\tr\treturn to main menu"));
809 puts(_("\ts\tchange number of sectors/track"));
810 puts(_("\tv\tverify the partition table"));
811 puts(_("\tw\twrite table to disk and exit"));
812 puts(_("\ty\tchange number of physical cylinders")); /*sun*/
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000813 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000814 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000815 puts(_("Command action"));
816 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
817 puts(_("\tc\tchange number of cylinders"));
818 puts(_("\td\tprint the raw data in the partition table"));
819 puts(_("\te\tlist extended partitions")); /* !sun */
820 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
821 puts(_("\th\tchange number of heads"));
822 puts(_("\tm\tprint this menu"));
823 puts(_("\tp\tprint the partition table"));
824 puts(_("\tq\tquit without saving changes"));
825 puts(_("\tr\treturn to main menu"));
826 puts(_("\ts\tchange number of sectors/track"));
827 puts(_("\tv\tverify the partition table"));
828 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000829 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000830 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000831 puts(_("Command action"));
832 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
833 puts(_("\tc\tchange number of cylinders"));
834 puts(_("\td\tprint the raw data in the partition table"));
835 puts(_("\te\tlist extended partitions")); /* !sun */
836 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
837 puts(_("\th\tchange number of heads"));
838 puts(_("\tm\tprint this menu"));
839 puts(_("\tp\tprint the partition table"));
840 puts(_("\tq\tquit without saving changes"));
841 puts(_("\tr\treturn to main menu"));
842 puts(_("\ts\tchange number of sectors/track"));
843 puts(_("\tv\tverify the partition table"));
844 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000845 } else {
Rob Landleyb73451d2006-02-24 16:29:00 +0000846 puts(_("Command action"));
847 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
848 puts(_("\tc\tchange number of cylinders"));
849 puts(_("\td\tprint the raw data in the partition table"));
850 puts(_("\te\tlist extended partitions")); /* !sun */
851 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000852#if ENABLE_FEATURE_SGI_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +0000853 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000854#endif
Rob Landleyb73451d2006-02-24 16:29:00 +0000855 puts(_("\th\tchange number of heads"));
856 puts(_("\tm\tprint this menu"));
857 puts(_("\tp\tprint the partition table"));
858 puts(_("\tq\tquit without saving changes"));
859 puts(_("\tr\treturn to main menu"));
860 puts(_("\ts\tchange number of sectors/track"));
861 puts(_("\tv\tverify the partition table"));
862 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000863 }
864}
865#endif /* ADVANCED mode */
866
Denis Vlasenko834410a2006-11-29 12:00:28 +0000867#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000868static const struct systypes *
Rob Landleyb73451d2006-02-24 16:29:00 +0000869get_sys_types(void)
870{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000871 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000872 LABEL_IS_SUN ? sun_sys_types :
873 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000874 i386_sys_types);
875}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000876#else
877#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000878#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000879
880static const char *partition_type(unsigned char type)
881{
882 int i;
883 const struct systypes *types = get_sys_types();
884
Rob Landleyb73451d2006-02-24 16:29:00 +0000885 for (i = 0; types[i].name; i++)
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000886 if ((unsigned char )types[i].name[0] == type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000887 return types[i].name + 1;
888
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000889 return _("Unknown");
890}
891
892
Denis Vlasenko834410a2006-11-29 12:00:28 +0000893#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000894static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000895get_sysid(int i)
896{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000897 return LABEL_IS_SUN ? sunlabel->infos[i].id :
898 (LABEL_IS_SGI ? sgi_get_sysid(i) :
899 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000900}
901
902void list_types(const struct systypes *sys)
903{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000904 unsigned last[4], done = 0, next = 0, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905 int i;
906
907 for (i = 0; sys[i].name; i++);
908 size = i;
909
910 for (i = 3; i >= 0; i--)
911 last[3 - i] = done += (size + i - done) / (i + 1);
912 i = done = 0;
913
914 do {
915 printf("%c%2x %-15.15s", i ? ' ' : '\n',
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000916 (unsigned char)sys[next].name[0],
917 partition_type((unsigned char)sys[next].name[0]));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000918 next = last[i++] + done;
919 if (i > 3 || next >= last[i]) {
920 i = 0;
921 next = ++done;
922 }
923 } while (done < last[0]);
924 putchar('\n');
925}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000926#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000927
928static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000929is_cleared_partition(const struct partition *p)
930{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000931 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
932 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
933 get_start_sect(p) || get_nr_sects(p));
934}
935
936static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000937clear_partition(struct partition *p)
938{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000939 if (!p)
940 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000941 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000942}
943
Denis Vlasenko834410a2006-11-29 12:00:28 +0000944#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000945static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000946set_partition(int i, int doext, off_t start, off_t stop, int sysid)
947{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000948 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000949 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000950
951 if (doext) {
952 p = ptes[i].ext_pointer;
953 offset = extended_offset;
954 } else {
955 p = ptes[i].part_table;
956 offset = ptes[i].offset;
957 }
958 p->boot_ind = 0;
959 p->sys_ind = sysid;
960 set_start_sect(p, start - offset);
961 set_nr_sects(p, stop - start + 1);
962 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
963 start = heads*sectors*1024 - 1;
964 set_hsc(p->head, p->sector, p->cyl, start);
965 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
966 stop = heads*sectors*1024 - 1;
967 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
968 ptes[i].changed = 1;
969}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000970#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971
972static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000973test_c(const char **m, const char *mesg)
974{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975 int val = 0;
976 if (!*m)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000977 printf(_("You must set"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000978 else {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000979 printf(" %s", *m);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980 val = 1;
981 }
982 *m = mesg;
983 return val;
984}
985
986static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000987warn_geometry(void)
988{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000989 const char *m = NULL;
990 int prev = 0;
991
992 if (!heads)
993 prev = test_c(&m, _("heads"));
994 if (!sectors)
995 prev = test_c(&m, _("sectors"));
996 if (!cylinders)
997 prev = test_c(&m, _("cylinders"));
998 if (!m)
999 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001000
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001001 printf("%s%s.\n"
Denis Vlasenko834410a2006-11-29 12:00:28 +00001002#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001003 "You can do this from the extra functions menu.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001004#endif
1005 , prev ? _(" and ") : " ", m);
1006
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001007 return 1;
1008}
1009
1010static void update_units(void)
1011{
1012 int cyl_units = heads * sectors;
1013
1014 if (display_in_cyl_units && cyl_units)
1015 units_per_sector = cyl_units;
1016 else
1017 units_per_sector = 1; /* in sectors */
1018}
1019
Denis Vlasenko834410a2006-11-29 12:00:28 +00001020#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001022warn_cylinders(void)
1023{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001024 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1025 printf(_("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026"The number of cylinders for this disk is set to %d.\n"
1027"There is nothing wrong with that, but this is larger than 1024,\n"
1028"and could in certain setups cause problems with:\n"
1029"1) software that runs at boot time (e.g., old versions of LILO)\n"
1030"2) booting and partitioning software from other OSs\n"
1031" (e.g., DOS FDISK, OS/2 FDISK)\n"),
1032 cylinders);
1033}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001034#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001035
1036static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001037read_extended(int ext)
1038{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001039 int i;
1040 struct pte *pex;
1041 struct partition *p, *q;
1042
1043 ext_index = ext;
1044 pex = &ptes[ext];
1045 pex->ext_pointer = pex->part_table;
1046
1047 p = pex->part_table;
1048 if (!get_start_sect(p)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001049 printf(_("Bad offset in primary extended partition\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050 return;
1051 }
1052
Rob Landleyb73451d2006-02-24 16:29:00 +00001053 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054 struct pte *pe = &ptes[partitions];
1055
1056 if (partitions >= MAXIMUM_PARTS) {
1057 /* This is not a Linux restriction, but
1058 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001059 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001061#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001062 printf(_("Warning: deleting partitions after %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001063 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001064 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001065#endif
1066 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001067 return;
1068 }
1069
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001070 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001071
1072 if (!extended_offset)
1073 extended_offset = get_start_sect(p);
1074
1075 q = p = pt_offset(pe->sectorbuffer, 0);
1076 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001077 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001078 if (pe->ext_pointer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001079 printf(_("Warning: extra link "
1080 "pointer in partition table"
1081 " %d\n"), partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001082 else
1083 pe->ext_pointer = p;
1084 } else if (p->sys_ind) {
1085 if (pe->part_table)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001086 printf(_("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001087 "data in partition table"
1088 " %d\n"), partitions + 1);
1089 else
1090 pe->part_table = p;
1091 }
1092 }
1093
1094 /* very strange code here... */
1095 if (!pe->part_table) {
1096 if (q != pe->ext_pointer)
1097 pe->part_table = q;
1098 else
1099 pe->part_table = q + 1;
1100 }
1101 if (!pe->ext_pointer) {
1102 if (q != pe->part_table)
1103 pe->ext_pointer = q;
1104 else
1105 pe->ext_pointer = q + 1;
1106 }
1107
1108 p = pe->ext_pointer;
1109 partitions++;
1110 }
1111
Denis Vlasenko834410a2006-11-29 12:00:28 +00001112#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113 /* remove empty links */
1114 remove:
1115 for (i = 4; i < partitions; i++) {
1116 struct pte *pe = &ptes[i];
1117
1118 if (!get_nr_sects(pe->part_table) &&
Rob Landleyb73451d2006-02-24 16:29:00 +00001119 (partitions > 5 || ptes[4].part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001120 printf("omitting empty partition (%d)\n", i+1);
1121 delete_partition(i);
1122 goto remove; /* numbering changed */
1123 }
1124 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001125#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001126}
1127
Denis Vlasenko834410a2006-11-29 12:00:28 +00001128#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001129static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001130create_doslabel(void)
1131{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 int i;
1133
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001134 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001135 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1136 "until you decide to write them. After that, of course, the previous\n"
1137 "content won't be recoverable.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001138
1139 current_label_type = label_dos;
1140
Denis Vlasenko834410a2006-11-29 12:00:28 +00001141#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001142 possibly_osf_label = 0;
1143#endif
1144 partitions = 4;
1145
1146 for (i = 510-64; i < 510; i++)
1147 MBRbuffer[i] = 0;
1148 write_part_table_flag(MBRbuffer);
1149 extended_offset = 0;
1150 set_all_unchanged();
1151 set_changed(0);
1152 get_boot(create_empty_dos);
1153}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001154#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001155
1156static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001157get_sectorsize(void)
1158{
Rob Landley736e5252006-02-25 03:36:00 +00001159 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001160 int arg;
1161 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1162 sector_size = arg;
1163 if (sector_size != DEFAULT_SECTOR_SIZE)
1164 printf(_("Note: sector size is %d (not %d)\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001165 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001166 }
1167}
1168
Rob Landley88621d72006-08-29 19:41:06 +00001169static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001170get_kernel_geometry(void)
1171{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 struct hd_geometry geometry;
1173
1174 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1175 kern_heads = geometry.heads;
1176 kern_sectors = geometry.sectors;
1177 /* never use geometry.cylinders - it is truncated */
1178 }
1179}
1180
1181static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001182get_partition_table_geometry(void)
1183{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001184 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185 struct partition *p;
1186 int i, h, s, hh, ss;
1187 int first = 1;
1188 int bad = 0;
1189
Eric Andersen3496fdc2006-01-30 23:09:20 +00001190 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 return;
1192
1193 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001194 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001195 p = pt_offset(bufp, i);
1196 if (p->sys_ind != 0) {
1197 h = p->end_head + 1;
1198 s = (p->end_sector & 077);
1199 if (first) {
1200 hh = h;
1201 ss = s;
1202 first = 0;
1203 } else if (hh != h || ss != s)
1204 bad = 1;
1205 }
1206 }
1207
1208 if (!first && !bad) {
1209 pt_heads = hh;
1210 pt_sectors = ss;
1211 }
1212}
1213
Rob Landleyb73451d2006-02-24 16:29:00 +00001214static void
1215get_geometry(void)
1216{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001217 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001218 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219
1220 get_sectorsize();
1221 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001222#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001223 guess_device_type();
1224#endif
1225 heads = cylinders = sectors = 0;
1226 kern_heads = kern_sectors = 0;
1227 pt_heads = pt_sectors = 0;
1228
1229 get_kernel_geometry();
1230 get_partition_table_geometry();
1231
1232 heads = user_heads ? user_heads :
1233 pt_heads ? pt_heads :
1234 kern_heads ? kern_heads : 255;
1235 sectors = user_sectors ? user_sectors :
1236 pt_sectors ? pt_sectors :
1237 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001238 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1239 /* got bytes */
1240 } else {
1241 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242
1243 if (ioctl(fd, BLKGETSIZE, &longsectors))
1244 longsectors = 0;
Eric Andersen040f4402003-07-30 08:40:37 +00001245 bytes = ((unsigned long long) longsectors) << 9;
1246 }
1247
1248 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001249
1250 sector_offset = 1;
1251 if (dos_compatible_flag)
1252 sector_offset = sectors;
1253
Eric Andersen040f4402003-07-30 08:40:37 +00001254 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001255 if (!cylinders)
1256 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257}
1258
1259/*
1260 * Read MBR. Returns:
1261 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1262 * 0: found or created label
1263 * 1: I/O error
1264 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001265static int
1266get_boot(enum action what)
1267{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001268 int i;
1269
1270 partitions = 4;
1271
1272 for (i = 0; i < 4; i++) {
1273 struct pte *pe = &ptes[i];
1274
1275 pe->part_table = pt_offset(MBRbuffer, i);
1276 pe->ext_pointer = NULL;
1277 pe->offset = 0;
1278 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001279#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001280 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001281#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001282 }
1283
Denis Vlasenko834410a2006-11-29 12:00:28 +00001284#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001285 if (what == create_empty_sun && check_sun_label())
1286 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001287#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288
1289 memset(MBRbuffer, 0, 512);
1290
Denis Vlasenko834410a2006-11-29 12:00:28 +00001291#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292 if (what == create_empty_dos)
1293 goto got_dos_table; /* skip reading disk */
1294
1295 if ((fd = open(disk_device, type_open)) < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001296 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1297 if (what == try_only)
1298 return 1;
1299 fdisk_fatal(unable_to_open);
1300 } else
1301 printf(_("You will not be able to write "
1302 "the partition table.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001303 }
1304
1305 if (512 != read(fd, MBRbuffer, 512)) {
1306 if (what == try_only)
1307 return 1;
1308 fdisk_fatal(unable_to_read);
1309 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001310#else
1311 if ((fd = open(disk_device, O_RDONLY)) < 0)
1312 return 1;
1313 if (512 != read(fd, MBRbuffer, 512))
1314 return 1;
1315#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001316
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001317 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001318
1319 update_units();
1320
Denis Vlasenko834410a2006-11-29 12:00:28 +00001321#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 if (check_sun_label())
1323 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001324#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 if (check_sgi_label())
1328 return 0;
1329#endif
1330
Denis Vlasenko834410a2006-11-29 12:00:28 +00001331#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001332 if (check_aix_label())
1333 return 0;
1334#endif
1335
Denis Vlasenko834410a2006-11-29 12:00:28 +00001336#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 if (check_osf_label()) {
1338 possibly_osf_label = 1;
1339 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001340 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 return 0;
1342 }
1343 printf(_("This disk has both DOS and BSD magic.\n"
1344 "Give the 'b' command to go to BSD mode.\n"));
1345 }
1346#endif
1347
Denis Vlasenko834410a2006-11-29 12:00:28 +00001348#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001349 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001350#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351
1352 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001353#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001354 return -1;
1355#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001356 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001357 case fdisk:
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001358 printf(_("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 "partition table, nor Sun, SGI or OSF "
1360 "disklabel\n"));
1361#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001362#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001363 create_sunlabel();
1364#endif
1365#else
1366 create_doslabel();
1367#endif
1368 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 case try_only:
1370 return -1;
1371 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001372#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001373 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001374#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001375 break;
1376 default:
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001377 bb_error_msg_and_die(_("internal error"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001379#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380 }
1381
Denis Vlasenko834410a2006-11-29 12:00:28 +00001382#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001383 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001384#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001385 warn_geometry();
1386
1387 for (i = 0; i < 4; i++) {
1388 struct pte *pe = &ptes[i];
1389
Rob Landleyb73451d2006-02-24 16:29:00 +00001390 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 if (partitions != 4)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001392 printf(_("Ignoring extra extended "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001393 "partition %d\n"), i + 1);
1394 else
1395 read_extended(i);
1396 }
1397 }
1398
1399 for (i = 3; i < partitions; i++) {
1400 struct pte *pe = &ptes[i];
1401
1402 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00001403 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404 "table %d will be corrected by w(rite)\n"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00001405 pe->sectorbuffer[510],
1406 pe->sectorbuffer[511],
1407 i + 1);
1408#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001410#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001411 }
1412 }
1413
1414 return 0;
1415}
1416
Denis Vlasenko834410a2006-11-29 12:00:28 +00001417#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001418/*
1419 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1420 * If the user hits Enter, DFLT is returned.
1421 * Answers like +10 are interpreted as offsets from BASE.
1422 *
1423 * There is no default if DFLT is not between LOW and HIGH.
1424 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001425static unsigned
1426read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001427{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001428 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001430 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001431
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001432 if (dflt < low || dflt > high) {
1433 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001434 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001435 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001436
1437 while (1) {
1438 int use_default = default_ok;
1439
1440 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001441 do {
1442 printf(fmt, mesg, low, high, dflt);
1443 read_maybe_empty("");
1444 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1445 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001446
Eric Andersen84bdea82004-05-19 10:49:17 +00001447 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001448 int minus = (*line_ptr == '-');
1449 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001450
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001451 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001452
Rob Landleyb73451d2006-02-24 16:29:00 +00001453 while (isdigit(*++line_ptr))
1454 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001455
Rob Landleyb73451d2006-02-24 16:29:00 +00001456 switch (*line_ptr) {
1457 case 'c':
1458 case 'C':
1459 if (!display_in_cyl_units)
1460 i *= heads * sectors;
1461 break;
1462 case 'K':
1463 absolute = 1024;
1464 break;
1465 case 'k':
1466 absolute = 1000;
1467 break;
1468 case 'm':
1469 case 'M':
1470 absolute = 1000000;
1471 break;
1472 case 'g':
1473 case 'G':
1474 absolute = 1000000000;
1475 break;
1476 default:
1477 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001478 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001479 if (absolute) {
1480 unsigned long long bytes;
1481 unsigned long unit;
1482
1483 bytes = (unsigned long long) i * absolute;
1484 unit = sector_size * units_per_sector;
1485 bytes += unit/2; /* round */
1486 bytes /= unit;
1487 i = bytes;
1488 }
1489 if (minus)
1490 i = -i;
1491 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001492 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001493 i = atoi(line_ptr);
1494 while (isdigit(*line_ptr)) {
1495 line_ptr++;
1496 use_default = 0;
1497 }
1498 }
1499 if (use_default)
Eric Andersen040f4402003-07-30 08:40:37 +00001500 printf(_("Using default value %u\n"), i = dflt);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501 if (i >= low && i <= high)
1502 break;
1503 else
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001504 printf(_("Value is out of range\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001505 }
1506 return i;
1507}
1508
Rob Landleyb73451d2006-02-24 16:29:00 +00001509static int
1510get_partition(int warn, int max)
1511{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001512 struct pte *pe;
1513 int i;
1514
1515 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1516 pe = &ptes[i];
1517
1518 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001519 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1520 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1521 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1522 ) {
1523 printf(_("Warning: partition %d has empty type\n"), i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001524 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001525 }
1526 return i;
1527}
1528
1529static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001530get_existing_partition(int warn, int max)
1531{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001532 int pno = -1;
1533 int i;
1534
1535 for (i = 0; i < max; i++) {
1536 struct pte *pe = &ptes[i];
1537 struct partition *p = pe->part_table;
1538
1539 if (p && !is_cleared_partition(p)) {
1540 if (pno >= 0)
1541 goto not_unique;
1542 pno = i;
1543 }
1544 }
1545 if (pno >= 0) {
1546 printf(_("Selected partition %d\n"), pno+1);
1547 return pno;
1548 }
1549 printf(_("No partition is defined yet!\n"));
1550 return -1;
1551
1552 not_unique:
1553 return get_partition(warn, max);
1554}
1555
1556static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001557get_nonexisting_partition(int warn, int max)
1558{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001559 int pno = -1;
1560 int i;
1561
1562 for (i = 0; i < max; i++) {
1563 struct pte *pe = &ptes[i];
1564 struct partition *p = pe->part_table;
1565
1566 if (p && is_cleared_partition(p)) {
1567 if (pno >= 0)
1568 goto not_unique;
1569 pno = i;
1570 }
1571 }
1572 if (pno >= 0) {
1573 printf(_("Selected partition %d\n"), pno+1);
1574 return pno;
1575 }
1576 printf(_("All primary partitions have been defined already!\n"));
1577 return -1;
1578
1579 not_unique:
1580 return get_partition(warn, max);
1581}
1582
1583
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001584static void
1585change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586{
1587 display_in_cyl_units = !display_in_cyl_units;
1588 update_units();
1589 printf(_("Changing display/entry units to %s\n"),
1590 str_units(PLURAL));
1591}
1592
1593static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001594toggle_active(int i)
1595{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596 struct pte *pe = &ptes[i];
1597 struct partition *p = pe->part_table;
1598
Rob Landleyb73451d2006-02-24 16:29:00 +00001599 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001600 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001601 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1602 pe->changed = 1;
1603}
1604
1605static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001606toggle_dos_compatibility_flag(void)
1607{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001608 dos_compatible_flag = ~dos_compatible_flag;
1609 if (dos_compatible_flag) {
1610 sector_offset = sectors;
1611 printf(_("DOS Compatibility flag is set\n"));
1612 }
1613 else {
1614 sector_offset = 1;
1615 printf(_("DOS Compatibility flag is not set\n"));
1616 }
1617}
1618
1619static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001620delete_partition(int i)
1621{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001622 struct pte *pe = &ptes[i];
1623 struct partition *p = pe->part_table;
1624 struct partition *q = pe->ext_pointer;
1625
1626/* Note that for the fifth partition (i == 4) we don't actually
1627 * decrement partitions.
1628 */
1629
1630 if (warn_geometry())
1631 return; /* C/H/S not set */
1632 pe->changed = 1;
1633
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001634 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635 sun_delete_partition(i);
1636 return;
1637 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001638 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001639 sgi_delete_partition(i);
1640 return;
1641 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001642
1643 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001644 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001645 partitions = 4;
1646 ptes[ext_index].ext_pointer = NULL;
1647 extended_offset = 0;
1648 }
1649 clear_partition(p);
1650 return;
1651 }
1652
1653 if (!q->sys_ind && i > 4) {
1654 /* the last one in the chain - just delete */
1655 --partitions;
1656 --i;
1657 clear_partition(ptes[i].ext_pointer);
1658 ptes[i].changed = 1;
1659 } else {
1660 /* not the last one - further ones will be moved down */
1661 if (i > 4) {
1662 /* delete this link in the chain */
1663 p = ptes[i-1].ext_pointer;
1664 *p = *q;
1665 set_start_sect(p, get_start_sect(q));
1666 set_nr_sects(p, get_nr_sects(q));
1667 ptes[i-1].changed = 1;
1668 } else if (partitions > 5) { /* 5 will be moved to 4 */
1669 /* the first logical in a longer chain */
1670 pe = &ptes[5];
1671
1672 if (pe->part_table) /* prevent SEGFAULT */
1673 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001674 get_partition_start(pe) -
1675 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676 pe->offset = extended_offset;
1677 pe->changed = 1;
1678 }
1679
1680 if (partitions > 5) {
1681 partitions--;
1682 while (i < partitions) {
1683 ptes[i] = ptes[i+1];
1684 i++;
1685 }
1686 } else
1687 /* the only logical: clear only */
1688 clear_partition(ptes[i].part_table);
1689 }
1690}
1691
1692static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001693change_sysid(void)
1694{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001695 int i, sys, origsys;
1696 struct partition *p;
1697
Eric Andersen040f4402003-07-30 08:40:37 +00001698 /* If sgi_label then don't use get_existing_partition,
1699 let the user select a partition, since get_existing_partition()
1700 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001701 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001702 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001703 } else {
1704 i = get_partition(0, partitions);
1705 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001706 if (i == -1)
1707 return;
1708 p = ptes[i].part_table;
1709 origsys = sys = get_sysid(i);
1710
1711 /* if changing types T to 0 is allowed, then
1712 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001713 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001714 printf(_("Partition %d does not exist yet!\n"), i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001715 return;
1716 }
1717 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001718 sys = read_hex (get_sys_types());
1719
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001720 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001721 printf(_("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001722 "(but not to Linux). Having partitions of\n"
1723 "type 0 is probably unwise. You can delete\n"
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001724 "a partition using the 'd' command.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001725 /* break; */
1726 }
1727
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001728 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001729 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001730 printf(_("You cannot change a partition into"
Rob Landleyb73451d2006-02-24 16:29:00 +00001731 " an extended one or vice versa\n"
1732 "Delete it first.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001733 break;
1734 }
1735 }
1736
1737 if (sys < 256) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001738 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001739 printf(_("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001740 "as Whole disk (5),\n"
1741 "as SunOS/Solaris expects it and "
1742 "even Linux likes it.\n\n"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001743 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001744 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001745 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001746 (i == 8 && sys != 0)
1747 )
1748 ){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749 printf(_("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001750 "as volume header (0),\nand "
1751 "partition 11 as entire volume (6)"
1752 "as IRIX expects it.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001753 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001754 if (sys == origsys)
1755 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001756 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001758 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 sgi_change_sysid(i, sys);
1760 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001762
Rob Landleyb73451d2006-02-24 16:29:00 +00001763 printf(_("Changed system type of partition %d "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001764 "to %x (%s)\n"), i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001765 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001766 ptes[i].changed = 1;
1767 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001768 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001769 dos_changed = 1;
1770 break;
1771 }
1772 }
1773}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001774#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001775
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001776
Denis Vlasenko28703012006-12-19 20:32:02 +00001777/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001778 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1779 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1780 * Lubkin Oct. 1991). */
1781
Rob Landleyb73451d2006-02-24 16:29:00 +00001782static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001783linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001784{
1785 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001786
1787 *c = ls / spc;
1788 ls = ls % spc;
1789 *h = ls / sectors;
1790 *s = ls % sectors + 1; /* sectors count from 1 */
1791}
1792
Rob Landleyb73451d2006-02-24 16:29:00 +00001793static void
1794check_consistency(const struct partition *p, int partition)
1795{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001796 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1797 unsigned pec, peh, pes; /* physical ending c, h, s */
1798 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1799 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001800
1801 if (!heads || !sectors || (partition >= 4))
1802 return; /* do not check extended partitions */
1803
1804/* physical beginning c, h, s */
1805 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1806 pbh = p->head;
1807 pbs = p->sector & 0x3f;
1808
1809/* physical ending c, h, s */
1810 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1811 peh = p->end_head;
1812 pes = p->end_sector & 0x3f;
1813
1814/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001815 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001816
1817/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001818 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819
1820/* Same physical / logical beginning? */
1821 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1822 printf(_("Partition %d has different physical/logical "
1823 "beginnings (non-Linux?):\n"), partition + 1);
1824 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1825 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1826 }
1827
1828/* Same physical / logical ending? */
1829 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1830 printf(_("Partition %d has different physical/logical "
1831 "endings:\n"), partition + 1);
1832 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1833 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1834 }
1835
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001836/* Ending on cylinder boundary? */
1837 if (peh != (heads - 1) || pes != sectors) {
Eric Andersen84bdea82004-05-19 10:49:17 +00001838 printf(_("Partition %i does not end on cylinder boundary.\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001839 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001840 }
1841}
1842
1843static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001844list_disk_geometry(void)
1845{
Eric Andersen040f4402003-07-30 08:40:37 +00001846 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001847 long megabytes = bytes/1000000;
1848
1849 if (megabytes < 10000)
1850 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001851 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001852 else
1853 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001854 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001855 printf(_("%d heads, %d sectors/track, %d cylinders"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001856 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001857 if (units_per_sector == 1)
Eric Andersen040f4402003-07-30 08:40:37 +00001858 printf(_(", total %llu sectors"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001859 total_number_of_sectors / (sector_size/512));
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001860 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001861 str_units(PLURAL),
1862 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001863}
1864
1865/*
1866 * Check whether partition entries are ordered by their starting positions.
1867 * Return 0 if OK. Return i if partition i should have been earlier.
1868 * Two separate checks: primary and logical partitions.
1869 */
1870static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001871wrong_p_order(int *prev)
1872{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873 const struct pte *pe;
1874 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001875 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001876 int i, last_i = 0;
1877
1878 for (i = 0 ; i < partitions; i++) {
1879 if (i == 4) {
1880 last_i = 4;
1881 last_p_start_pos = 0;
1882 }
1883 pe = &ptes[i];
1884 if ((p = pe->part_table)->sys_ind) {
1885 p_start_pos = get_partition_start(pe);
1886
1887 if (last_p_start_pos > p_start_pos) {
1888 if (prev)
1889 *prev = last_i;
1890 return i;
1891 }
1892
1893 last_p_start_pos = p_start_pos;
1894 last_i = i;
1895 }
1896 }
1897 return 0;
1898}
1899
Denis Vlasenko834410a2006-11-29 12:00:28 +00001900#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001901/*
1902 * Fix the chain of logicals.
1903 * extended_offset is unchanged, the set of sectors used is unchanged
1904 * The chain is sorted so that sectors increase, and so that
1905 * starting sectors increase.
1906 *
1907 * After this it may still be that cfdisk doesnt like the table.
1908 * (This is because cfdisk considers expanded parts, from link to
1909 * end of partition, and these may still overlap.)
1910 * Now
1911 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1912 * may help.
1913 */
1914static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001915fix_chain_of_logicals(void)
1916{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001917 int j, oj, ojj, sj, sjj;
1918 struct partition *pj,*pjj,tmp;
1919
1920 /* Stage 1: sort sectors but leave sector of part 4 */
1921 /* (Its sector is the global extended_offset.) */
1922 stage1:
1923 for (j = 5; j < partitions-1; j++) {
1924 oj = ptes[j].offset;
1925 ojj = ptes[j+1].offset;
1926 if (oj > ojj) {
1927 ptes[j].offset = ojj;
1928 ptes[j+1].offset = oj;
1929 pj = ptes[j].part_table;
1930 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1931 pjj = ptes[j+1].part_table;
1932 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1933 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001934 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001935 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001936 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001937 goto stage1;
1938 }
1939 }
1940
1941 /* Stage 2: sort starting sectors */
1942 stage2:
1943 for (j = 4; j < partitions-1; j++) {
1944 pj = ptes[j].part_table;
1945 pjj = ptes[j+1].part_table;
1946 sj = get_start_sect(pj);
1947 sjj = get_start_sect(pjj);
1948 oj = ptes[j].offset;
1949 ojj = ptes[j+1].offset;
1950 if (oj+sj > ojj+sjj) {
1951 tmp = *pj;
1952 *pj = *pjj;
1953 *pjj = tmp;
1954 set_start_sect(pj, ojj+sjj-oj);
1955 set_start_sect(pjj, oj+sj-ojj);
1956 goto stage2;
1957 }
1958 }
1959
1960 /* Probably something was changed */
1961 for (j = 4; j < partitions; j++)
1962 ptes[j].changed = 1;
1963}
1964
1965
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001966static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001967fix_partition_table_order(void)
1968{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001969 struct pte *pei, *pek;
1970 int i,k;
1971
1972 if (!wrong_p_order(NULL)) {
1973 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1974 return;
1975 }
1976
1977 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1978 /* partition i should have come earlier, move it */
1979 /* We have to move data in the MBR */
1980 struct partition *pi, *pk, *pe, pbuf;
1981 pei = &ptes[i];
1982 pek = &ptes[k];
1983
1984 pe = pei->ext_pointer;
1985 pei->ext_pointer = pek->ext_pointer;
1986 pek->ext_pointer = pe;
1987
1988 pi = pei->part_table;
1989 pk = pek->part_table;
1990
1991 memmove(&pbuf, pi, sizeof(struct partition));
1992 memmove(pi, pk, sizeof(struct partition));
1993 memmove(pk, &pbuf, sizeof(struct partition));
1994
1995 pei->changed = pek->changed = 1;
1996 }
1997
1998 if (i)
1999 fix_chain_of_logicals();
2000
2001 printf("Done.\n");
2002
2003}
2004#endif
2005
2006static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002007list_table(int xtra)
2008{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002009 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 int i, w;
2011
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002012 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 sun_list_table(xtra);
2014 return;
2015 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002016 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002017 sgi_list_table(xtra);
2018 return;
2019 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020
2021 list_disk_geometry();
2022
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002023 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002024 xbsd_print_disklabel(xtra);
2025 return;
2026 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002027
2028 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2029 but if the device name ends in a digit, say /dev/foo1,
2030 then the partition is called /dev/foo1p3. */
2031 w = strlen(disk_device);
2032 if (w && isdigit(disk_device[w-1]))
2033 w++;
2034 if (w < 5)
2035 w = 5;
2036
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002037 // 1 12345678901 12345678901 12345678901 12
2038 printf(_("%*s Boot Start End Blocks Id System\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002039 w+1, _("Device"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002040
2041 for (i = 0; i < partitions; i++) {
2042 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002043 off_t psects;
2044 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002045 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046
2047 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002048 if (!p || is_cleared_partition(p))
2049 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002050
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002051 psects = get_nr_sects(p);
2052 pblocks = psects;
2053 podd = 0;
2054
2055 if (sector_size < 1024) {
2056 pblocks /= (1024 / sector_size);
2057 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002058 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002059 if (sector_size > 1024)
2060 pblocks *= (sector_size / 1024);
2061
2062 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2063 partname(disk_device, i+1, w+2),
2064 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2065 ? '*' : '?',
2066 (unsigned long long) cround(get_partition_start(pe)), /* start */
2067 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2068 - (psects ? 1 : 0)),
2069 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2070 p->sys_ind, /* type id */
2071 partition_type(p->sys_ind)); /* type name */
2072
2073 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074 }
2075
2076 /* Is partition table in disk order? It need not be, but... */
2077 /* partition table entries are not checked for correct order if this
2078 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002079 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002080 /* FIXME */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002081 printf(_("\nPartition table entries are not in disk order\n"));
2082 }
2083}
2084
Denis Vlasenko834410a2006-11-29 12:00:28 +00002085#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002086static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002087x_list_table(int extend)
2088{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002089 const struct pte *pe;
2090 const struct partition *p;
2091 int i;
2092
2093 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2094 disk_device, heads, sectors, cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002095 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002096 for (i = 0 ; i < partitions; i++) {
2097 pe = &ptes[i];
2098 p = (extend ? pe->ext_pointer : pe->part_table);
2099 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002100 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002101 i + 1, p->boot_ind, p->head,
2102 sector(p->sector),
2103 cylinder(p->sector, p->cyl), p->end_head,
2104 sector(p->end_sector),
2105 cylinder(p->end_sector, p->end_cyl),
2106 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2107 if (p->sys_ind)
2108 check_consistency(p, i);
2109 }
2110 }
2111}
2112#endif
2113
Denis Vlasenko834410a2006-11-29 12:00:28 +00002114#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002115static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002116fill_bounds(off_t *first, off_t *last)
2117{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 int i;
2119 const struct pte *pe = &ptes[0];
2120 const struct partition *p;
2121
2122 for (i = 0; i < partitions; pe++,i++) {
2123 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002124 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125 first[i] = 0xffffffff;
2126 last[i] = 0;
2127 } else {
2128 first[i] = get_partition_start(pe);
2129 last[i] = first[i] + get_nr_sects(p) - 1;
2130 }
2131 }
2132}
2133
2134static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002135check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002136{
Eric Andersend9261492004-06-28 23:50:31 +00002137 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002138
2139 real_s = sector(s) - 1;
2140 real_c = cylinder(s, c);
2141 total = (real_c * sectors + real_s) * heads + h;
2142 if (!total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002143 printf(_("Warning: partition %d contains sector 0\n"), n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 if (h >= heads)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002145 printf(_("Partition %d: head %d greater than maximum %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002146 n, h + 1, heads);
2147 if (real_s >= sectors)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002148 printf(_("Partition %d: sector %d greater than "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149 "maximum %d\n"), n, s, sectors);
2150 if (real_c >= cylinders)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002151 printf(_("Partitions %d: cylinder %llu greater than "
Eric Andersend9261492004-06-28 23:50:31 +00002152 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002153 if (cylinders <= 1024 && start != total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002154 printf(_("Partition %d: previous sectors %llu disagrees with "
Eric Andersend9261492004-06-28 23:50:31 +00002155 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156}
2157
2158static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002159verify(void)
2160{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002161 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002162 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002163 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164 struct partition *p;
2165
2166 if (warn_geometry())
2167 return;
2168
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002169 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002170 verify_sun();
2171 return;
2172 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002173 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002174 verify_sgi(1);
2175 return;
2176 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002177
2178 fill_bounds(first, last);
2179 for (i = 0; i < partitions; i++) {
2180 struct pte *pe = &ptes[i];
2181
2182 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002183 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002184 check_consistency(p, i);
2185 if (get_partition_start(pe) < first[i])
2186 printf(_("Warning: bad start-of-data in "
2187 "partition %d\n"), i + 1);
2188 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2189 last[i]);
2190 total += last[i] + 1 - first[i];
2191 for (j = 0; j < i; j++)
2192 if ((first[i] >= first[j] && first[i] <= last[j])
2193 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2194 printf(_("Warning: partition %d overlaps "
2195 "partition %d.\n"), j + 1, i + 1);
2196 total += first[i] >= first[j] ?
2197 first[i] : first[j];
2198 total -= last[i] <= last[j] ?
2199 last[i] : last[j];
2200 }
2201 }
2202 }
2203
2204 if (extended_offset) {
2205 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002206 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 get_nr_sects(pex->part_table) - 1;
2208
2209 for (i = 4; i < partitions; i++) {
2210 total++;
2211 p = ptes[i].part_table;
2212 if (!p->sys_ind) {
2213 if (i != 4 || i + 1 < partitions)
2214 printf(_("Warning: partition %d "
2215 "is empty\n"), i + 1);
2216 }
2217 else if (first[i] < extended_offset ||
2218 last[i] > e_last)
2219 printf(_("Logical partition %d not entirely in "
2220 "partition %d\n"), i + 1, ext_index + 1);
2221 }
2222 }
2223
2224 if (total > heads * sectors * cylinders)
2225 printf(_("Total allocated sectors %d greater than the maximum "
2226 "%d\n"), total, heads * sectors * cylinders);
2227 else if ((total = heads * sectors * cylinders - total) != 0)
2228 printf(_("%d unallocated sectors\n"), total);
2229}
2230
2231static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002232add_partition(int n, int sys)
2233{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002234 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002235 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236 struct partition *p = ptes[n].part_table;
2237 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002238 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002239 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002240 first[partitions], last[partitions];
2241
2242 if (p && p->sys_ind) {
2243 printf(_("Partition %d is already defined. Delete "
2244 "it before re-adding it.\n"), n + 1);
2245 return;
2246 }
2247 fill_bounds(first, last);
2248 if (n < 4) {
2249 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002250 if (display_in_cyl_units || !total_number_of_sectors)
2251 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252 else
Eric Andersen040f4402003-07-30 08:40:37 +00002253 llimit = total_number_of_sectors - 1;
2254 limit = llimit;
2255 if (limit != llimit)
2256 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002257 if (extended_offset) {
2258 first[ext_index] = extended_offset;
2259 last[ext_index] = get_start_sect(q) +
2260 get_nr_sects(q) - 1;
2261 }
2262 } else {
2263 start = extended_offset + sector_offset;
2264 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2265 }
2266 if (display_in_cyl_units)
2267 for (i = 0; i < partitions; i++)
2268 first[i] = (cround(first[i]) - 1) * units_per_sector;
2269
2270 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2271 do {
2272 temp = start;
2273 for (i = 0; i < partitions; i++) {
2274 int lastplusoff;
2275
2276 if (start == ptes[i].offset)
2277 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002278 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002279 if (start >= first[i] && start <= lastplusoff)
2280 start = lastplusoff + 1;
2281 }
2282 if (start > limit)
2283 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002284 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +00002285 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002286 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002287 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002288 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002289 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002290 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002291
2292 saved_start = start;
2293 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2294 0, mesg);
2295 if (display_in_cyl_units) {
2296 start = (start - 1) * units_per_sector;
2297 if (start < saved_start) start = saved_start;
2298 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002299 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002300 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002301 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002302 if (n > 4) { /* NOT for fifth partition */
2303 struct pte *pe = &ptes[n];
2304
2305 pe->offset = start - sector_offset;
2306 if (pe->offset == extended_offset) { /* must be corrected */
2307 pe->offset++;
2308 if (sector_offset == 1)
2309 start++;
2310 }
2311 }
2312
2313 for (i = 0; i < partitions; i++) {
2314 struct pte *pe = &ptes[i];
2315
2316 if (start < pe->offset && limit >= pe->offset)
2317 limit = pe->offset - 1;
2318 if (start < first[i] && limit >= first[i])
2319 limit = first[i] - 1;
2320 }
2321 if (start > limit) {
2322 printf(_("No free sectors available\n"));
2323 if (n > 4)
2324 partitions--;
2325 return;
2326 }
2327 if (cround(start) == cround(limit)) {
2328 stop = limit;
2329 } else {
2330 snprintf(mesg, sizeof(mesg),
2331 _("Last %s or +size or +sizeM or +sizeK"),
2332 str_units(SINGULAR));
2333 stop = read_int(cround(start), cround(limit), cround(limit),
2334 cround(start), mesg);
2335 if (display_in_cyl_units) {
2336 stop = stop * units_per_sector - 1;
2337 if (stop >limit)
2338 stop = limit;
2339 }
2340 }
2341
2342 set_partition(n, 0, start, stop, sys);
2343 if (n > 4)
2344 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2345
Rob Landleyb73451d2006-02-24 16:29:00 +00002346 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 struct pte *pe4 = &ptes[4];
2348 struct pte *pen = &ptes[n];
2349
2350 ext_index = n;
2351 pen->ext_pointer = p;
2352 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002353 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2355 pe4->ext_pointer = pe4->part_table + 1;
2356 pe4->changed = 1;
2357 partitions = 5;
2358 }
2359}
2360
2361static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002362add_logical(void)
2363{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2365 struct pte *pe = &ptes[partitions];
2366
Rob Landley081e3842006-08-03 20:07:35 +00002367 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002368 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2369 pe->ext_pointer = pe->part_table + 1;
2370 pe->offset = 0;
2371 pe->changed = 1;
2372 partitions++;
2373 }
2374 add_partition(partitions - 1, LINUX_NATIVE);
2375}
2376
2377static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002378new_partition(void)
2379{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002380 int i, free_primary = 0;
2381
2382 if (warn_geometry())
2383 return;
2384
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002385 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2387 return;
2388 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002389 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002390 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2391 return;
2392 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002393 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2395 "\n\tIf you want to add DOS-type partitions, create"
2396 "\n\ta new empty DOS partition table first. (Use o.)"
2397 "\n\tWARNING: "
2398 "This will destroy the present disk contents.\n"));
2399 return;
2400 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002402 for (i = 0; i < 4; i++)
2403 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002404
Rob Landleyb73451d2006-02-24 16:29:00 +00002405 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Eric Andersen84bdea82004-05-19 10:49:17 +00002406 printf(_("The maximum number of partitions has been created\n"));
2407 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002408 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002409
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 if (!free_primary) {
2411 if (extended_offset)
2412 add_logical();
2413 else
2414 printf(_("You must delete some partition and add "
2415 "an extended partition first\n"));
2416 } else {
2417 char c, line[LINE_LENGTH];
2418 snprintf(line, sizeof(line), "%s\n %s\n p primary "
2419 "partition (1-4)\n",
2420 "Command action", (extended_offset ?
2421 "l logical (5 or over)" : "e extended"));
2422 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002423 c = read_nonempty(line);
2424 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002425 i = get_nonexisting_partition(0, 4);
2426 if (i >= 0)
2427 add_partition(i, LINUX_NATIVE);
2428 return;
2429 }
2430 else if (c == 'l' && extended_offset) {
2431 add_logical();
2432 return;
2433 }
2434 else if (c == 'e' && !extended_offset) {
2435 i = get_nonexisting_partition(0, 4);
2436 if (i >= 0)
2437 add_partition(i, EXTENDED);
2438 return;
2439 }
2440 else
2441 printf(_("Invalid partition number "
Denis Vlasenko89f0b342006-11-18 22:04:09 +00002442 "for type '%c'\n"), c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 }
2444 }
2445}
2446
2447static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002448write_table(void)
2449{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002450 int i;
2451
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002452 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002453 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002454 if (ptes[i].changed)
2455 ptes[3].changed = 1;
2456 for (i = 3; i < partitions; i++) {
2457 struct pte *pe = &ptes[i];
2458
2459 if (pe->changed) {
2460 write_part_table_flag(pe->sectorbuffer);
2461 write_sector(pe->offset, pe->sectorbuffer);
2462 }
2463 }
2464 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002465 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 /* no test on change? the printf below might be mistaken */
2467 sgi_write_table();
2468 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002469 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002470 int needw = 0;
2471
Rob Landleyb73451d2006-02-24 16:29:00 +00002472 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473 if (ptes[i].changed)
2474 needw = 1;
2475 if (needw)
2476 sun_write_table();
2477 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478
2479 printf(_("The partition table has been altered!\n\n"));
2480 reread_partition_table(1);
2481}
2482
Rob Landleyb73451d2006-02-24 16:29:00 +00002483static void
2484reread_partition_table(int leave)
2485{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486 int i;
2487
Denis Vlasenko28703012006-12-19 20:32:02 +00002488 printf(_("Calling ioctl() to re-read partition table\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 sync();
Denis Vlasenko28703012006-12-19 20:32:02 +00002490 sleep(2); /* Huh? */
2491 i = ioctl(fd, BLKRRPART);
2492#if 0
2493 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494 /* some kernel versions (1.2.x) seem to have trouble
2495 rereading the partition table, but if asked to do it
2496 twice, the second time works. - biro@yggdrasil.com */
2497 sync();
2498 sleep(2);
Denis Vlasenko28703012006-12-19 20:32:02 +00002499 i = ioctl(fd, BLKRRPART);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002500 }
Denis Vlasenko28703012006-12-19 20:32:02 +00002501#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502
2503 if (i) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002504 bb_perror_msg("WARNING: rereading partition table "
2505 "failed, kernel still uses old table");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002506 }
2507
Denis Vlasenko28703012006-12-19 20:32:02 +00002508#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002510 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002511 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2512 "partitions, please see the fdisk manual page for additional\n"
2513 "information.\n"));
Denis Vlasenko28703012006-12-19 20:32:02 +00002514#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002515
2516 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002517 if (ENABLE_FEATURE_CLEAN_UP)
2518 close(fd);
2519 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520 }
2521}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002522#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002523
Denis Vlasenko834410a2006-11-29 12:00:28 +00002524#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525#define MAX_PER_LINE 16
2526static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002527print_buffer(char *pbuffer)
2528{
2529 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530
2531 for (i = 0, l = 0; i < sector_size; i++, l++) {
2532 if (l == 0)
2533 printf("0x%03X:", i);
2534 printf(" %02X", (unsigned char) pbuffer[i]);
2535 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002536 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 l = -1;
2538 }
2539 }
2540 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002541 puts("");
2542 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543}
2544
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002545static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002546print_raw(void)
2547{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548 int i;
2549
2550 printf(_("Device: %s\n"), disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002551 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002552 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002553 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554 for (i = 3; i < partitions; i++)
2555 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002556 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557}
2558
2559static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002560move_begin(int i)
2561{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 struct pte *pe = &ptes[i];
2563 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002564 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565
2566 if (warn_geometry())
2567 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002568 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569 printf(_("Partition %d has no data area\n"), i + 1);
2570 return;
2571 }
2572 first = get_partition_start(pe);
2573 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Rob Landleyb73451d2006-02-24 16:29:00 +00002574 _("New beginning of data")) - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002575
2576 if (new != get_nr_sects(p)) {
2577 first = get_nr_sects(p) + get_start_sect(p) - new;
2578 set_nr_sects(p, first);
2579 set_start_sect(p, new);
2580 pe->changed = 1;
2581 }
2582}
2583
2584static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002585xselect(void)
2586{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 char c;
2588
Rob Landleyb73451d2006-02-24 16:29:00 +00002589 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002591 c = tolower(read_nonempty(_("Expert command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 switch (c) {
2593 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002596 break;
2597 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002598 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 move_begin(get_partition(0, partitions));
2600 break;
2601 case 'c':
2602 user_cylinders = cylinders =
2603 read_int(1, cylinders, 1048576, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002604 _("Number of cylinders"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002605 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 warn_cylinders();
2609 break;
2610 case 'd':
2611 print_raw();
2612 break;
2613 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002614 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002618 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 x_list_table(1);
2620 break;
2621 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002622 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 fix_partition_table_order();
2624 break;
2625 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002626#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 create_sgilabel();
2628#endif
2629 break;
2630 case 'h':
2631 user_heads = heads = read_int(1, heads, 256, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002632 _("Number of heads"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 update_units();
2634 break;
2635 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002638 break;
2639 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002640 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002642 break;
2643 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002644 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 list_table(1);
2646 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002647 x_list_table(0);
2648 break;
2649 case 'q':
2650 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002651 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002652 exit(0);
2653 case 'r':
2654 return;
2655 case 's':
2656 user_sectors = sectors = read_int(1, sectors, 63, 0,
2657 _("Number of sectors"));
2658 if (dos_compatible_flag) {
2659 sector_offset = sectors;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002660 printf(_("Warning: setting sector offset for DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002661 "compatiblity\n"));
2662 }
2663 update_units();
2664 break;
2665 case 'v':
2666 verify();
2667 break;
2668 case 'w':
2669 write_table(); /* does not return */
2670 break;
2671 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002672 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002673 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002674 break;
2675 default:
2676 xmenu();
2677 }
2678 }
2679}
2680#endif /* ADVANCED mode */
2681
2682static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002683is_ide_cdrom_or_tape(const char *device)
2684{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 FILE *procf;
2686 char buf[100];
2687 struct stat statbuf;
2688 int is_ide = 0;
2689
2690 /* No device was given explicitly, and we are trying some
2691 likely things. But opening /dev/hdc may produce errors like
2692 "hdc: tray open or drive not ready"
2693 if it happens to be a CD-ROM drive. It even happens that
2694 the process hangs on the attempt to read a music CD.
2695 So try to be careful. This only works since 2.1.73. */
2696
2697 if (strncmp("/dev/hd", device, 7))
2698 return 0;
2699
2700 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2701 procf = fopen(buf, "r");
2702 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2703 is_ide = (!strncmp(buf, "cdrom", 5) ||
2704 !strncmp(buf, "tape", 4));
2705 else
2706 /* Now when this proc file does not exist, skip the
2707 device when it is read-only. */
2708 if (stat(device, &statbuf) == 0)
2709 is_ide = ((statbuf.st_mode & 0222) == 0);
2710
2711 if (procf)
2712 fclose(procf);
2713 return is_ide;
2714}
2715
Rob Landley5527b912006-02-25 03:46:10 +00002716
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002717static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002718trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002719{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002720 int gb;
2721
2722 disk_device = device;
2723 if (setjmp(listingbuf))
2724 return;
2725 if (!user_specified)
2726 if (is_ide_cdrom_or_tape(device))
2727 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002728 fd = open(disk_device, type_open);
2729 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002730 gb = get_boot(try_only);
2731 if (gb > 0) { /* I/O error */
2732 close(fd);
2733 } else if (gb < 0) { /* no DOS signature */
2734 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002735 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002736 return;
Rob Landley5527b912006-02-25 03:46:10 +00002737 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002738#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002739 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002740#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002741 printf(_("Disk %s doesn't contain a valid "
Rob Landleyb73451d2006-02-24 16:29:00 +00002742 "partition table\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743 close(fd);
2744 } else {
2745 close(fd);
2746 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002747#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002748 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002749 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002750 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002751#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002752 }
2753 } else {
2754 /* Ignore other errors, since we try IDE
2755 and SCSI hard disks which may not be
2756 installed on the system. */
2757 if (errno == EACCES) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002758 printf(_("Cannot open %s\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 return;
2760 }
2761 }
2762}
2763
2764/* for fdisk -l: try all things in /proc/partitions
2765 that look like a partition name (do not end in a digit) */
2766static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002767tryprocpt(void)
2768{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002769 FILE *procpt;
2770 char line[100], ptname[100], devname[120], *s;
2771 int ma, mi, sz;
2772
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002773 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002774
2775 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002776 if (sscanf(line, " %d %d %d %[^\n ]",
2777 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002778 continue;
2779 for (s = ptname; *s; s++);
2780 if (isdigit(s[-1]))
2781 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002782 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002783 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002785#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002787#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002788}
2789
Denis Vlasenko834410a2006-11-29 12:00:28 +00002790#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002792unknown_command(int c)
2793{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002794 printf(_("%c: unknown command\n"), c);
2795}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002796#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002797
Rob Landleyb73451d2006-02-24 16:29:00 +00002798int fdisk_main(int argc, char **argv)
2799{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002800 char *str_b, *str_C, *str_H, *str_S;
2801 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002803 * fdisk -v
2804 * fdisk -l [-b sectorsize] [-u] device ...
2805 * fdisk -s [partition] ...
2806 * fdisk [-b sectorsize] [-u] device
2807 *
2808 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002810 enum {
2811 OPT_b = 1 << 0,
2812 OPT_C = 1 << 1,
2813 OPT_H = 1 << 2,
2814 OPT_l = 1 << 3,
2815 OPT_S = 1 << 4,
2816 OPT_u = 1 << 5,
2817 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2818 };
2819 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2820 &str_b, &str_C, &str_H, &str_S);
2821 argc -= optind;
2822 argv += optind;
2823 if (opt & OPT_b) { // -b
2824 /* Ugly: this sector size is really per device,
2825 so cannot be combined with multiple disks,
2826 and the same goes for the C/H/S options.
2827 */
2828 sector_size = xatoi_u(str_b);
2829 if (sector_size != 512 && sector_size != 1024 &&
2830 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002831 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002832 sector_offset = 2;
2833 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002834 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002835 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2836 if (opt & OPT_H) { // -H
2837 user_heads = xatoi_u(str_H);
2838 if (user_heads <= 0 || user_heads >= 256)
2839 user_heads = 0;
2840 }
2841 //if (opt & OPT_l) // -l
2842 if (opt & OPT_S) { // -S
2843 user_sectors = xatoi_u(str_S);
2844 if (user_sectors <= 0 || user_sectors >= 64)
2845 user_sectors = 0;
2846 }
2847 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2848 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850 if (user_set_sector_size && argc != 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 printf(_("Warning: the -b (set sector size) option should"
2852 " be used with one specified device\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853
Denis Vlasenko834410a2006-11-29 12:00:28 +00002854#if ENABLE_FEATURE_FDISK_WRITABLE
2855 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002857#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002861#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862 /* avoid gcc warning:
2863 variable `k' might be clobbered by `longjmp' */
2864 (void)&k;
2865#endif
2866 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002867 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002868 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002869 } else {
2870 /* we no longer have default device names */
2871 /* but, we can use /proc/partitions instead */
2872 tryprocpt();
2873 }
2874 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002875#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002877#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878
Denis Vlasenko834410a2006-11-29 12:00:28 +00002879#if ENABLE_FEATURE_FDISK_BLKSIZE
2880 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002882 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883
2884 nowarn = 1;
2885 type_open = O_RDONLY;
2886
Denis Vlasenko834410a2006-11-29 12:00:28 +00002887 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002888 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002889
Denis Vlasenko834410a2006-11-29 12:00:28 +00002890 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002891 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002892 fd = open(disk_device, type_open);
2893 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 fdisk_fatal(unable_to_open);
2895 if (ioctl(fd, BLKGETSIZE, &size))
2896 fdisk_fatal(ioctl_error);
2897 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002898 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002899 printf("%ld\n", size/2);
2900 else
2901 printf("%s: %ld\n", argv[j], size/2);
2902 }
2903 return 0;
2904 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002905#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906
Denis Vlasenko834410a2006-11-29 12:00:28 +00002907#if ENABLE_FEATURE_FDISK_WRITABLE
2908 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002909 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910
Denis Vlasenko834410a2006-11-29 12:00:28 +00002911 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002912 get_boot(fdisk);
2913
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002914 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 /* OSF label, and no DOS label */
2916 printf(_("Detected an OSF/1 disklabel on %s, entering "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002917 "disklabel mode.\n"), disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002918 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002919 /*Why do we do this? It seems to be counter-intuitive*/
2920 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002921 /* If we return we may want to make an empty DOS label? */
2922 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923
2924 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002925 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002927 c = tolower(read_nonempty(_("Command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002928 switch (c) {
2929 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002930 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002931 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 toggle_sunflags(get_partition(1, partitions),
2934 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002935 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 sgi_set_bootpartition(
2937 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 else
2939 unknown_command(c);
2940 break;
2941 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002942 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002943 printf(_("\nThe current boot file is: %s\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002944 sgi_get_bootfile());
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002945 if (read_maybe_empty(_("Please enter the name of the "
Rob Landleyb73451d2006-02-24 16:29:00 +00002946 "new boot file: ")) == '\n')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947 printf(_("Boot file unchanged\n"));
2948 else
2949 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002950 }
2951#if ENABLE_FEATURE_OSF_LABEL
2952 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002953 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002954#endif
2955 break;
2956 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002957 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002958 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002959 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 toggle_sunflags(get_partition(1, partitions),
2961 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002962 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002963 sgi_set_swappartition(
2964 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002965 else
2966 unknown_command(c);
2967 break;
2968 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002969 {
Eric Andersen040f4402003-07-30 08:40:37 +00002970 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002971 /* If sgi_label then don't use get_existing_partition,
2972 let the user select a partition, since
2973 get_existing_partition() only works for Linux-like
2974 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002975 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002976 j = get_existing_partition(1, partitions);
2977 } else {
2978 j = get_partition(1, partitions);
2979 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002980 if (j >= 0)
2981 delete_partition(j);
2982 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002983 break;
2984 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002985 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002986 create_sgiinfo();
2987 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002988 unknown_command(c);
2989 case 'l':
2990 list_types(get_sys_types());
2991 break;
2992 case 'm':
2993 menu();
2994 break;
2995 case 'n':
2996 new_partition();
2997 break;
2998 case 'o':
2999 create_doslabel();
3000 break;
3001 case 'p':
3002 list_table(0);
3003 break;
3004 case 'q':
3005 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00003006 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003007 return 0;
3008 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003009#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003010 create_sunlabel();
3011#endif
3012 break;
3013 case 't':
3014 change_sysid();
3015 break;
3016 case 'u':
3017 change_units();
3018 break;
3019 case 'v':
3020 verify();
3021 break;
3022 case 'w':
3023 write_table(); /* does not return */
3024 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003025#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003026 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003027 if (LABEL_IS_SGI) {
3028 printf(_("\n\tSorry, no experts menu for SGI "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003029 "partition tables available.\n\n"));
3030 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003031 xselect();
3032 break;
3033#endif
3034 default:
3035 unknown_command(c);
3036 menu();
3037 }
3038 }
3039 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003040#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003041}