blob: 2f87f1c6061dd1f5909f7cb84d11c835632196cd [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
Eric Andersend9261492004-06-28 23:50:31 +0000223static int32_t get_start_sect(const struct partition *p);
224static int32_t 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 Vlasenko834410a2006-11-29 12:00:28 +0000255extern 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}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000260#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000261
Denis Vlasenko834410a2006-11-29 12:00:28 +0000262extern 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
284extern inline void
285write_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);
341 if (errno || v > 0xff) continue;
342 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000343 }
344}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000345#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000346
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000347#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000348
349typedef struct {
350 unsigned char info[128]; /* Informative text string */
351 unsigned char spare0[14];
352 struct sun_info {
353 unsigned char spare1;
354 unsigned char id;
355 unsigned char spare2;
356 unsigned char flags;
357 } infos[8];
358 unsigned char spare1[246]; /* Boot information etc. */
359 unsigned short rspeed; /* Disk rotational speed */
360 unsigned short pcylcount; /* Physical cylinder count */
361 unsigned short sparecyl; /* extra sects per cylinder */
362 unsigned char spare2[4]; /* More magic... */
363 unsigned short ilfact; /* Interleave factor */
364 unsigned short ncyl; /* Data cylinder count */
365 unsigned short nacyl; /* Alt. cylinder count */
366 unsigned short ntrks; /* Tracks per cylinder */
367 unsigned short nsect; /* Sectors per track */
368 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000369 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000370 uint32_t start_cylinder;
371 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000372 } partitions[8];
373 unsigned short magic; /* Magic number */
374 unsigned short csum; /* Label xor'd checksum */
375} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000376#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000377#define SUNOS_SWAP 3
378#define SUN_WHOLE_DISK 5
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000379STATIC_OSF void bsd_select(void);
380STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000381#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000382
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000383#define SGI_VOLHDR 0x00
384/* 1 and 2 were used for drive types no longer supported by SGI */
385#define SGI_SWAP 0x03
386/* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
387#define SGI_VOLUME 0x06
388#define SGI_EFS 0x07
389#define SGI_LVOL 0x08
390#define SGI_RLVOL 0x09
391#define SGI_XFS 0x0a
392#define SGI_XFSLOG 0x0b
393#define SGI_XLV 0x0c
394#define SGI_XVM 0x0d
395#define SGI_ENTIRE_DISK SGI_VOLUME
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000396#if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000397static uint16_t
398__swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000399{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000400 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000401}
402
Rob Landley88621d72006-08-29 19:41:06 +0000403static uint32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000404__swap32(uint32_t x)
405{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000406 return (x << 24) |
407 ((x & 0xFF00) << 8) |
408 ((x & 0xFF0000) >> 8) |
409 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000410}
411#endif
412
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000413STATIC_SGI const struct systypes sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000414STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000415STATIC_SGI int sgi_get_sysid(int i);
416STATIC_SGI void sgi_delete_partition(int i);
417STATIC_SGI void sgi_change_sysid(int i, int sys);
418STATIC_SGI void sgi_list_table(int xtra);
419STATIC_SGI void sgi_set_xcyl(void);
420STATIC_SGI int verify_sgi(int verbose);
421STATIC_SGI void sgi_add_partition(int n, int sys);
422STATIC_SGI void sgi_set_swappartition(int i);
423STATIC_SGI const char *sgi_get_bootfile(void);
424STATIC_SGI void sgi_set_bootfile(const char* aFile);
425STATIC_SGI void create_sgiinfo(void);
426STATIC_SGI void sgi_write_table(void);
427STATIC_SGI void sgi_set_bootpartition(int i);
428
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000429#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000430
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000431STATIC_SUN const struct systypes sun_sys_types[];
432STATIC_SUN void sun_delete_partition(int i);
433STATIC_SUN void sun_change_sysid(int i, int sys);
434STATIC_SUN void sun_list_table(int xtra);
435STATIC_SUN void sun_set_xcyl(void);
436STATIC_SUN void add_sun_partition(int n, int sys);
437STATIC_SUN void sun_set_alt_cyl(void);
438STATIC_SUN void sun_set_ncyl(int cyl);
439STATIC_SUN void sun_set_xcyl(void);
440STATIC_SUN void sun_set_ilfact(void);
441STATIC_SUN void sun_set_rspeed(void);
442STATIC_SUN void sun_set_pcylcount(void);
443STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
444STATIC_SUN void verify_sun(void);
445STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000446#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000447
448/* DOS partition types */
449
450static const struct systypes i386_sys_types[] = {
Rob Landleyb73451d2006-02-24 16:29:00 +0000451 { "\x00" "Empty" },
452 { "\x01" "FAT12" },
453 { "\x04" "FAT16 <32M" },
454 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
455 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
456 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
457 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
458 { "\x0b" "Win95 FAT32" },
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000459 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
Rob Landleyb73451d2006-02-24 16:29:00 +0000460 { "\x0e" "Win95 FAT16 (LBA)" },
461 { "\x0f" "Win95 Ext'd (LBA)" },
462 { "\x11" "Hidden FAT12" },
463 { "\x12" "Compaq diagnostics" },
464 { "\x14" "Hidden FAT16 <32M" },
465 { "\x16" "Hidden FAT16" },
466 { "\x17" "Hidden HPFS/NTFS" },
467 { "\x1b" "Hidden Win95 FAT32" },
468 { "\x1c" "Hidden Win95 FAT32 (LBA)" },
469 { "\x1e" "Hidden Win95 FAT16 (LBA)" },
470 { "\x3c" "PartitionMagic recovery" },
471 { "\x41" "PPC PReP Boot" },
472 { "\x42" "SFS" },
473 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
474 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
475 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
476 { "\x82" "Linux swap" }, /* also Solaris */
477 { "\x83" "Linux" },
478 { "\x84" "OS/2 hidden C: drive" },
479 { "\x85" "Linux extended" },
480 { "\x86" "NTFS volume set" },
481 { "\x87" "NTFS volume set" },
482 { "\x8e" "Linux LVM" },
483 { "\x9f" "BSD/OS" }, /* BSDI */
484 { "\xa0" "IBM Thinkpad hibernation" },
485 { "\xa5" "FreeBSD" }, /* various BSD flavours */
486 { "\xa6" "OpenBSD" },
487 { "\xa8" "Darwin UFS" },
488 { "\xa9" "NetBSD" },
489 { "\xab" "Darwin boot" },
490 { "\xb7" "BSDI fs" },
491 { "\xb8" "BSDI swap" },
492 { "\xbe" "Solaris boot" },
493 { "\xeb" "BeOS fs" },
494 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
495 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
496 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
497 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
498 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
499 autodetect using persistent
500 superblock */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000501#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
Rob Landleyb73451d2006-02-24 16:29:00 +0000502 { "\x02" "XENIX root" },
503 { "\x03" "XENIX usr" },
504 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
505 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
506 { "\x10" "OPUS" },
507 { "\x18" "AST SmartSleep" },
508 { "\x24" "NEC DOS" },
509 { "\x39" "Plan 9" },
510 { "\x40" "Venix 80286" },
511 { "\x4d" "QNX4.x" },
512 { "\x4e" "QNX4.x 2nd part" },
513 { "\x4f" "QNX4.x 3rd part" },
514 { "\x50" "OnTrack DM" },
515 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
516 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
517 { "\x53" "OnTrack DM6 Aux3" },
518 { "\x54" "OnTrackDM6" },
519 { "\x55" "EZ-Drive" },
520 { "\x56" "Golden Bow" },
521 { "\x5c" "Priam Edisk" },
522 { "\x61" "SpeedStor" },
523 { "\x64" "Novell Netware 286" },
524 { "\x65" "Novell Netware 386" },
525 { "\x70" "DiskSecure Multi-Boot" },
526 { "\x75" "PC/IX" },
527 { "\x93" "Amoeba" },
528 { "\x94" "Amoeba BBT" }, /* (bad block table) */
529 { "\xa7" "NeXTSTEP" },
530 { "\xbb" "Boot Wizard hidden" },
531 { "\xc1" "DRDOS/sec (FAT-12)" },
532 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
533 { "\xc6" "DRDOS/sec (FAT-16)" },
534 { "\xc7" "Syrinx" },
535 { "\xda" "Non-FS data" },
536 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000537 Concurrent DOS or CTOS */
Rob Landleyb73451d2006-02-24 16:29:00 +0000538 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
539 { "\xdf" "BootIt" }, /* BootIt EMBRM */
540 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000541 extended partition */
Rob Landleyb73451d2006-02-24 16:29:00 +0000542 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
543 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000544 partition < 1024 cyl. */
Rob Landleyb73451d2006-02-24 16:29:00 +0000545 { "\xf1" "SpeedStor" },
546 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
547 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
548 { "\xff" "BBT" }, /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000549#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000550 { 0 }
551};
552
553
Denis Vlasenko834410a2006-11-29 12:00:28 +0000554#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000555/* start_sect and nr_sects are stored little endian on all machines */
556/* moreover, they are not aligned correctly */
557static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000558store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000559{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000560 cp[0] = val;
561 cp[1] = val >> 8;
562 cp[2] = val >> 16;
563 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000564}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000565#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000566
Denis Vlasenko834410a2006-11-29 12:00:28 +0000567static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000568read4_little_endian(const unsigned char *cp)
569{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000570 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000571}
572
Denis Vlasenko834410a2006-11-29 12:00:28 +0000573#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000574static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000575set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000576{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000577 store4_little_endian(p->start4, start_sect);
578}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000579#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000580
Eric Andersend9261492004-06-28 23:50:31 +0000581static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000582get_start_sect(const struct partition *p)
583{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000584 return read4_little_endian(p->start4);
585}
586
Denis Vlasenko834410a2006-11-29 12:00:28 +0000587#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000589set_nr_sects(struct partition *p, int32_t nr_sects)
590{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000591 store4_little_endian(p->size4, nr_sects);
592}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000593#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000594
Eric Andersend9261492004-06-28 23:50:31 +0000595static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000596get_nr_sects(const struct partition *p)
597{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598 return read4_little_endian(p->size4);
599}
600
601/* normally O_RDWR, -l option gives O_RDONLY */
602static int type_open = O_RDWR;
603
604
Rob Landleyb73451d2006-02-24 16:29:00 +0000605static int ext_index; /* the prime extended partition */
606static int listing; /* no aborts for fdisk -l */
607static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000608#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000609static int dos_changed;
610static int nowarn; /* no warnings for fdisk -l/-s */
611#endif
612
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000613
614
Denis Vlasenko834410a2006-11-29 12:00:28 +0000615static unsigned user_cylinders, user_heads, user_sectors;
616static unsigned pt_heads, pt_sectors;
617static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000618
Eric Andersend9261492004-06-28 23:50:31 +0000619static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000620
Eric Andersen040f4402003-07-30 08:40:37 +0000621static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000622
623
624static jmp_buf listingbuf;
625
Rob Landleyb73451d2006-02-24 16:29:00 +0000626static void fdisk_fatal(enum failure why)
627{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000628 const char *message;
629
630 if (listing) {
631 close(fd);
632 longjmp(listingbuf, 1);
633 }
634
635 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000636 case unable_to_open:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000637 message = "\nUnable to open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000638 break;
639 case unable_to_read:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000640 message = "\nUnable to read %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000641 break;
642 case unable_to_seek:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000643 message = "\nUnable to seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000644 break;
645 case unable_to_write:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000646 message = "\nUnable to write %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000647 break;
648 case ioctl_error:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000649 message = "\nBLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000650 break;
651 default:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000652 message = "\nFatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000653 }
654
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000655 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000656}
657
658static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000659seek_sector(off_t secno)
660{
Eric Andersen0a92f352004-03-30 09:21:54 +0000661 off_t offset = secno * sector_size;
662 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000663 fdisk_fatal(unable_to_seek);
664}
665
Denis Vlasenko834410a2006-11-29 12:00:28 +0000666#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000667static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000668write_sector(off_t secno, char *buf)
669{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000670 seek_sector(secno);
671 if (write(fd, buf, sector_size) != sector_size)
672 fdisk_fatal(unable_to_write);
673}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000674#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000675
676/* Allocate a buffer and read a partition table sector */
677static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000678read_pte(struct pte *pe, off_t offset)
679{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000680 pe->offset = offset;
681 pe->sectorbuffer = (char *) xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000682 seek_sector(offset);
683 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
684 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000685#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000686 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000687#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000688 pe->part_table = pe->ext_pointer = NULL;
689}
690
Denis Vlasenko834410a2006-11-29 12:00:28 +0000691static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000692get_partition_start(const struct pte *pe)
693{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000694 return pe->offset + get_start_sect(pe->part_table);
695}
696
Denis Vlasenko834410a2006-11-29 12:00:28 +0000697#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000698/*
699 * Avoid warning about DOS partitions when no DOS partition was changed.
700 * Here a heuristic "is probably dos partition".
701 * We might also do the opposite and warn in all cases except
702 * for "is probably nondos partition".
703 */
704static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000705is_dos_partition(int t)
706{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000707 return (t == 1 || t == 4 || t == 6 ||
708 t == 0x0b || t == 0x0c || t == 0x0e ||
709 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
710 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
711 t == 0xc1 || t == 0xc4 || t == 0xc6);
712}
713
714static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000715menu(void)
716{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000717 if (LABEL_IS_SUN) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000718 puts(_("Command action"));
719 puts(_("\ta\ttoggle a read only flag")); /* sun */
720 puts(_("\tb\tedit bsd disklabel"));
721 puts(_("\tc\ttoggle the mountable flag")); /* sun */
722 puts(_("\td\tdelete a partition"));
723 puts(_("\tl\tlist known partition types"));
724 puts(_("\tm\tprint this menu"));
725 puts(_("\tn\tadd a new partition"));
726 puts(_("\to\tcreate a new empty DOS partition table"));
727 puts(_("\tp\tprint the partition table"));
728 puts(_("\tq\tquit without saving changes"));
729 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
730 puts(_("\tt\tchange a partition's system id"));
731 puts(_("\tu\tchange display/entry units"));
732 puts(_("\tv\tverify the partition table"));
733 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000734#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000735 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000736#endif
737 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000738 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000739 puts(_("Command action"));
740 puts(_("\ta\tselect bootable partition")); /* sgi flavour */
741 puts(_("\tb\tedit bootfile entry")); /* sgi */
742 puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
743 puts(_("\td\tdelete a partition"));
744 puts(_("\tl\tlist known partition types"));
745 puts(_("\tm\tprint this menu"));
746 puts(_("\tn\tadd a new partition"));
747 puts(_("\to\tcreate a new empty DOS partition table"));
748 puts(_("\tp\tprint the partition table"));
749 puts(_("\tq\tquit without saving changes"));
750 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
751 puts(_("\tt\tchange a partition's system id"));
752 puts(_("\tu\tchange display/entry units"));
753 puts(_("\tv\tverify the partition table"));
754 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000755 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000756 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000757 puts(_("Command action"));
758 puts(_("\tm\tprint this menu"));
759 puts(_("\to\tcreate a new empty DOS partition table"));
760 puts(_("\tq\tquit without saving changes"));
761 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000762 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000763 {
Rob Landleyb73451d2006-02-24 16:29:00 +0000764 puts(_("Command action"));
765 puts(_("\ta\ttoggle a bootable flag"));
766 puts(_("\tb\tedit bsd disklabel"));
767 puts(_("\tc\ttoggle the dos compatibility flag"));
768 puts(_("\td\tdelete a partition"));
769 puts(_("\tl\tlist known partition types"));
770 puts(_("\tm\tprint this menu"));
771 puts(_("\tn\tadd a new partition"));
772 puts(_("\to\tcreate a new empty DOS partition table"));
773 puts(_("\tp\tprint the partition table"));
774 puts(_("\tq\tquit without saving changes"));
775 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
776 puts(_("\tt\tchange a partition's system id"));
777 puts(_("\tu\tchange display/entry units"));
778 puts(_("\tv\tverify the partition table"));
779 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000780#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000781 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000782#endif
783 }
784}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000785#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
786
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000787
Denis Vlasenko834410a2006-11-29 12:00:28 +0000788#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000789static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000790xmenu(void)
791{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000792 if (LABEL_IS_SUN) {
793 puts(_("Command action"));
794 puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
795 puts(_("\tc\tchange number of cylinders"));
796 puts(_("\td\tprint the raw data in the partition table"));
797 puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
798 puts(_("\th\tchange number of heads"));
799 puts(_("\ti\tchange interleave factor")); /*sun*/
800 puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
801 puts(_("\tm\tprint this menu"));
802 puts(_("\tp\tprint the partition table"));
803 puts(_("\tq\tquit without saving changes"));
804 puts(_("\tr\treturn to main menu"));
805 puts(_("\ts\tchange number of sectors/track"));
806 puts(_("\tv\tverify the partition table"));
807 puts(_("\tw\twrite table to disk and exit"));
808 puts(_("\ty\tchange number of physical cylinders")); /*sun*/
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000809 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000810 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000811 puts(_("Command action"));
812 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
813 puts(_("\tc\tchange number of cylinders"));
814 puts(_("\td\tprint the raw data in the partition table"));
815 puts(_("\te\tlist extended partitions")); /* !sun */
816 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
817 puts(_("\th\tchange number of heads"));
818 puts(_("\tm\tprint this menu"));
819 puts(_("\tp\tprint the partition table"));
820 puts(_("\tq\tquit without saving changes"));
821 puts(_("\tr\treturn to main menu"));
822 puts(_("\ts\tchange number of sectors/track"));
823 puts(_("\tv\tverify the partition table"));
824 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000825 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000826 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000827 puts(_("Command action"));
828 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
829 puts(_("\tc\tchange number of cylinders"));
830 puts(_("\td\tprint the raw data in the partition table"));
831 puts(_("\te\tlist extended partitions")); /* !sun */
832 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
833 puts(_("\th\tchange number of heads"));
834 puts(_("\tm\tprint this menu"));
835 puts(_("\tp\tprint the partition table"));
836 puts(_("\tq\tquit without saving changes"));
837 puts(_("\tr\treturn to main menu"));
838 puts(_("\ts\tchange number of sectors/track"));
839 puts(_("\tv\tverify the partition table"));
840 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000841 } else {
Rob Landleyb73451d2006-02-24 16:29:00 +0000842 puts(_("Command action"));
843 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
844 puts(_("\tc\tchange number of cylinders"));
845 puts(_("\td\tprint the raw data in the partition table"));
846 puts(_("\te\tlist extended partitions")); /* !sun */
847 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000848#if ENABLE_FEATURE_SGI_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +0000849 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000850#endif
Rob Landleyb73451d2006-02-24 16:29:00 +0000851 puts(_("\th\tchange number of heads"));
852 puts(_("\tm\tprint this menu"));
853 puts(_("\tp\tprint the partition table"));
854 puts(_("\tq\tquit without saving changes"));
855 puts(_("\tr\treturn to main menu"));
856 puts(_("\ts\tchange number of sectors/track"));
857 puts(_("\tv\tverify the partition table"));
858 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000859 }
860}
861#endif /* ADVANCED mode */
862
Denis Vlasenko834410a2006-11-29 12:00:28 +0000863#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000864static const struct systypes *
Rob Landleyb73451d2006-02-24 16:29:00 +0000865get_sys_types(void)
866{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000867 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000868 LABEL_IS_SUN ? sun_sys_types :
869 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000870 i386_sys_types);
871}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000872#else
873#define get_sys_types() i386_sys_types
874#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000875
876static const char *partition_type(unsigned char type)
877{
878 int i;
879 const struct systypes *types = get_sys_types();
880
Rob Landleyb73451d2006-02-24 16:29:00 +0000881 for (i = 0; types[i].name; i++)
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000882 if ((unsigned char )types[i].name[0] == type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883 return types[i].name + 1;
884
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000885 return _("Unknown");
886}
887
888
Denis Vlasenko834410a2006-11-29 12:00:28 +0000889#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000890static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000891get_sysid(int i)
892{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000893 return LABEL_IS_SUN ? sunlabel->infos[i].id :
894 (LABEL_IS_SGI ? sgi_get_sysid(i) :
895 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000896}
897
898void list_types(const struct systypes *sys)
899{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000900 unsigned last[4], done = 0, next = 0, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000901 int i;
902
903 for (i = 0; sys[i].name; i++);
904 size = i;
905
906 for (i = 3; i >= 0; i--)
907 last[3 - i] = done += (size + i - done) / (i + 1);
908 i = done = 0;
909
910 do {
911 printf("%c%2x %-15.15s", i ? ' ' : '\n',
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000912 (unsigned char)sys[next].name[0],
913 partition_type((unsigned char)sys[next].name[0]));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000914 next = last[i++] + done;
915 if (i > 3 || next >= last[i]) {
916 i = 0;
917 next = ++done;
918 }
919 } while (done < last[0]);
920 putchar('\n');
921}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000922#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000923
924static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000925is_cleared_partition(const struct partition *p)
926{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000927 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
928 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
929 get_start_sect(p) || get_nr_sects(p));
930}
931
932static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000933clear_partition(struct partition *p)
934{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935 if (!p)
936 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000937 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938}
939
Denis Vlasenko834410a2006-11-29 12:00:28 +0000940#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000941static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000942set_partition(int i, int doext, off_t start, off_t stop, int sysid)
943{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000944 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000945 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000946
947 if (doext) {
948 p = ptes[i].ext_pointer;
949 offset = extended_offset;
950 } else {
951 p = ptes[i].part_table;
952 offset = ptes[i].offset;
953 }
954 p->boot_ind = 0;
955 p->sys_ind = sysid;
956 set_start_sect(p, start - offset);
957 set_nr_sects(p, stop - start + 1);
958 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
959 start = heads*sectors*1024 - 1;
960 set_hsc(p->head, p->sector, p->cyl, start);
961 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
962 stop = heads*sectors*1024 - 1;
963 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
964 ptes[i].changed = 1;
965}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000966#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000967
968static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000969test_c(const char **m, const char *mesg)
970{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971 int val = 0;
972 if (!*m)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000973 printf(_("You must set"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000974 else {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000975 printf(" %s", *m);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000976 val = 1;
977 }
978 *m = mesg;
979 return val;
980}
981
982static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000983warn_geometry(void)
984{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000985 const char *m = NULL;
986 int prev = 0;
987
988 if (!heads)
989 prev = test_c(&m, _("heads"));
990 if (!sectors)
991 prev = test_c(&m, _("sectors"));
992 if (!cylinders)
993 prev = test_c(&m, _("cylinders"));
994 if (!m)
995 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000996
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000997 printf("%s%s.\n"
Denis Vlasenko834410a2006-11-29 12:00:28 +0000998#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000999 "You can do this from the extra functions menu.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001000#endif
1001 , prev ? _(" and ") : " ", m);
1002
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001003 return 1;
1004}
1005
1006static void update_units(void)
1007{
1008 int cyl_units = heads * sectors;
1009
1010 if (display_in_cyl_units && cyl_units)
1011 units_per_sector = cyl_units;
1012 else
1013 units_per_sector = 1; /* in sectors */
1014}
1015
Denis Vlasenko834410a2006-11-29 12:00:28 +00001016#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001017static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001018warn_cylinders(void)
1019{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001020 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1021 printf(_("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001022"The number of cylinders for this disk is set to %d.\n"
1023"There is nothing wrong with that, but this is larger than 1024,\n"
1024"and could in certain setups cause problems with:\n"
1025"1) software that runs at boot time (e.g., old versions of LILO)\n"
1026"2) booting and partitioning software from other OSs\n"
1027" (e.g., DOS FDISK, OS/2 FDISK)\n"),
1028 cylinders);
1029}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001030#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001031
1032static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001033read_extended(int ext)
1034{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001035 int i;
1036 struct pte *pex;
1037 struct partition *p, *q;
1038
1039 ext_index = ext;
1040 pex = &ptes[ext];
1041 pex->ext_pointer = pex->part_table;
1042
1043 p = pex->part_table;
1044 if (!get_start_sect(p)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001045 printf(_("Bad offset in primary extended partition\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001046 return;
1047 }
1048
Rob Landleyb73451d2006-02-24 16:29:00 +00001049 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050 struct pte *pe = &ptes[partitions];
1051
1052 if (partitions >= MAXIMUM_PARTS) {
1053 /* This is not a Linux restriction, but
1054 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001055 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001057#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001058 printf(_("Warning: deleting partitions after %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001059 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001061#endif
1062 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001063 return;
1064 }
1065
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001066 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001067
1068 if (!extended_offset)
1069 extended_offset = get_start_sect(p);
1070
1071 q = p = pt_offset(pe->sectorbuffer, 0);
1072 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001073 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001074 if (pe->ext_pointer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001075 printf(_("Warning: extra link "
1076 "pointer in partition table"
1077 " %d\n"), partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001078 else
1079 pe->ext_pointer = p;
1080 } else if (p->sys_ind) {
1081 if (pe->part_table)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001082 printf(_("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001083 "data in partition table"
1084 " %d\n"), partitions + 1);
1085 else
1086 pe->part_table = p;
1087 }
1088 }
1089
1090 /* very strange code here... */
1091 if (!pe->part_table) {
1092 if (q != pe->ext_pointer)
1093 pe->part_table = q;
1094 else
1095 pe->part_table = q + 1;
1096 }
1097 if (!pe->ext_pointer) {
1098 if (q != pe->part_table)
1099 pe->ext_pointer = q;
1100 else
1101 pe->ext_pointer = q + 1;
1102 }
1103
1104 p = pe->ext_pointer;
1105 partitions++;
1106 }
1107
Denis Vlasenko834410a2006-11-29 12:00:28 +00001108#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109 /* remove empty links */
1110 remove:
1111 for (i = 4; i < partitions; i++) {
1112 struct pte *pe = &ptes[i];
1113
1114 if (!get_nr_sects(pe->part_table) &&
Rob Landleyb73451d2006-02-24 16:29:00 +00001115 (partitions > 5 || ptes[4].part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116 printf("omitting empty partition (%d)\n", i+1);
1117 delete_partition(i);
1118 goto remove; /* numbering changed */
1119 }
1120 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001121#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122}
1123
Denis Vlasenko834410a2006-11-29 12:00:28 +00001124#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001126create_doslabel(void)
1127{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001128 int i;
1129
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001130 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001131 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1132 "until you decide to write them. After that, of course, the previous\n"
1133 "content won't be recoverable.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001134
1135 current_label_type = label_dos;
1136
Denis Vlasenko834410a2006-11-29 12:00:28 +00001137#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001138 possibly_osf_label = 0;
1139#endif
1140 partitions = 4;
1141
1142 for (i = 510-64; i < 510; i++)
1143 MBRbuffer[i] = 0;
1144 write_part_table_flag(MBRbuffer);
1145 extended_offset = 0;
1146 set_all_unchanged();
1147 set_changed(0);
1148 get_boot(create_empty_dos);
1149}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001150#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001151
1152static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001153get_sectorsize(void)
1154{
Rob Landley736e5252006-02-25 03:36:00 +00001155 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001156 int arg;
1157 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1158 sector_size = arg;
1159 if (sector_size != DEFAULT_SECTOR_SIZE)
1160 printf(_("Note: sector size is %d (not %d)\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001161 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001162 }
1163}
1164
Rob Landley88621d72006-08-29 19:41:06 +00001165static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001166get_kernel_geometry(void)
1167{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001168 struct hd_geometry geometry;
1169
1170 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1171 kern_heads = geometry.heads;
1172 kern_sectors = geometry.sectors;
1173 /* never use geometry.cylinders - it is truncated */
1174 }
1175}
1176
1177static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001178get_partition_table_geometry(void)
1179{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001180 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001181 struct partition *p;
1182 int i, h, s, hh, ss;
1183 int first = 1;
1184 int bad = 0;
1185
Eric Andersen3496fdc2006-01-30 23:09:20 +00001186 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001187 return;
1188
1189 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001190 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191 p = pt_offset(bufp, i);
1192 if (p->sys_ind != 0) {
1193 h = p->end_head + 1;
1194 s = (p->end_sector & 077);
1195 if (first) {
1196 hh = h;
1197 ss = s;
1198 first = 0;
1199 } else if (hh != h || ss != s)
1200 bad = 1;
1201 }
1202 }
1203
1204 if (!first && !bad) {
1205 pt_heads = hh;
1206 pt_sectors = ss;
1207 }
1208}
1209
Rob Landleyb73451d2006-02-24 16:29:00 +00001210static void
1211get_geometry(void)
1212{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001214 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215
1216 get_sectorsize();
1217 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001218#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219 guess_device_type();
1220#endif
1221 heads = cylinders = sectors = 0;
1222 kern_heads = kern_sectors = 0;
1223 pt_heads = pt_sectors = 0;
1224
1225 get_kernel_geometry();
1226 get_partition_table_geometry();
1227
1228 heads = user_heads ? user_heads :
1229 pt_heads ? pt_heads :
1230 kern_heads ? kern_heads : 255;
1231 sectors = user_sectors ? user_sectors :
1232 pt_sectors ? pt_sectors :
1233 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001234 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1235 /* got bytes */
1236 } else {
1237 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001238
1239 if (ioctl(fd, BLKGETSIZE, &longsectors))
1240 longsectors = 0;
Eric Andersen040f4402003-07-30 08:40:37 +00001241 bytes = ((unsigned long long) longsectors) << 9;
1242 }
1243
1244 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245
1246 sector_offset = 1;
1247 if (dos_compatible_flag)
1248 sector_offset = sectors;
1249
Eric Andersen040f4402003-07-30 08:40:37 +00001250 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001251 if (!cylinders)
1252 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253}
1254
1255/*
1256 * Read MBR. Returns:
1257 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1258 * 0: found or created label
1259 * 1: I/O error
1260 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001261static int
1262get_boot(enum action what)
1263{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001264 int i;
1265
1266 partitions = 4;
1267
1268 for (i = 0; i < 4; i++) {
1269 struct pte *pe = &ptes[i];
1270
1271 pe->part_table = pt_offset(MBRbuffer, i);
1272 pe->ext_pointer = NULL;
1273 pe->offset = 0;
1274 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001275#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001276 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001277#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001278 }
1279
Denis Vlasenko834410a2006-11-29 12:00:28 +00001280#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281 if (what == create_empty_sun && check_sun_label())
1282 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001283#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001284
1285 memset(MBRbuffer, 0, 512);
1286
Denis Vlasenko834410a2006-11-29 12:00:28 +00001287#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288 if (what == create_empty_dos)
1289 goto got_dos_table; /* skip reading disk */
1290
1291 if ((fd = open(disk_device, type_open)) < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001292 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1293 if (what == try_only)
1294 return 1;
1295 fdisk_fatal(unable_to_open);
1296 } else
1297 printf(_("You will not be able to write "
1298 "the partition table.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001299 }
1300
1301 if (512 != read(fd, MBRbuffer, 512)) {
1302 if (what == try_only)
1303 return 1;
1304 fdisk_fatal(unable_to_read);
1305 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001306#else
1307 if ((fd = open(disk_device, O_RDONLY)) < 0)
1308 return 1;
1309 if (512 != read(fd, MBRbuffer, 512))
1310 return 1;
1311#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001312
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001313 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001314
1315 update_units();
1316
Denis Vlasenko834410a2006-11-29 12:00:28 +00001317#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001318 if (check_sun_label())
1319 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001320#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001321
Denis Vlasenko834410a2006-11-29 12:00:28 +00001322#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323 if (check_sgi_label())
1324 return 0;
1325#endif
1326
Denis Vlasenko834410a2006-11-29 12:00:28 +00001327#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001328 if (check_aix_label())
1329 return 0;
1330#endif
1331
Denis Vlasenko834410a2006-11-29 12:00:28 +00001332#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001333 if (check_osf_label()) {
1334 possibly_osf_label = 1;
1335 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001336 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 return 0;
1338 }
1339 printf(_("This disk has both DOS and BSD magic.\n"
1340 "Give the 'b' command to go to BSD mode.\n"));
1341 }
1342#endif
1343
Denis Vlasenko834410a2006-11-29 12:00:28 +00001344#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001345 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001346#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347
1348 if (!valid_part_table_flag(MBRbuffer)) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001349#ifndef CONFIG_FEATURE_FDISK_WRITABLE
1350 return -1;
1351#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001352 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 case fdisk:
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001354 printf(_("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 "partition table, nor Sun, SGI or OSF "
1356 "disklabel\n"));
1357#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001358#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 create_sunlabel();
1360#endif
1361#else
1362 create_doslabel();
1363#endif
1364 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 case try_only:
1366 return -1;
1367 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001368#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001370#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001371 break;
1372 default:
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001373 bb_error_msg_and_die(_("internal error"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001375#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001376 }
1377
Denis Vlasenko834410a2006-11-29 12:00:28 +00001378#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001380#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001381 warn_geometry();
1382
1383 for (i = 0; i < 4; i++) {
1384 struct pte *pe = &ptes[i];
1385
Rob Landleyb73451d2006-02-24 16:29:00 +00001386 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001387 if (partitions != 4)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001388 printf(_("Ignoring extra extended "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001389 "partition %d\n"), i + 1);
1390 else
1391 read_extended(i);
1392 }
1393 }
1394
1395 for (i = 3; i < partitions; i++) {
1396 struct pte *pe = &ptes[i];
1397
1398 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00001399 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400 "table %d will be corrected by w(rite)\n"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00001401 pe->sectorbuffer[510],
1402 pe->sectorbuffer[511],
1403 i + 1);
1404#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001405 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001406#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 }
1408 }
1409
1410 return 0;
1411}
1412
Denis Vlasenko834410a2006-11-29 12:00:28 +00001413#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414/*
1415 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1416 * If the user hits Enter, DFLT is returned.
1417 * Answers like +10 are interpreted as offsets from BASE.
1418 *
1419 * There is no default if DFLT is not between LOW and HIGH.
1420 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001421static unsigned
1422read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001423{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001424 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001425 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001426 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001427
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001428 if (dflt < low || dflt > high) {
1429 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001431 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432
1433 while (1) {
1434 int use_default = default_ok;
1435
1436 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001437 do {
1438 printf(fmt, mesg, low, high, dflt);
1439 read_maybe_empty("");
1440 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1441 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001442
Eric Andersen84bdea82004-05-19 10:49:17 +00001443 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001444 int minus = (*line_ptr == '-');
1445 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001446
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001447 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001448
Rob Landleyb73451d2006-02-24 16:29:00 +00001449 while (isdigit(*++line_ptr))
1450 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001451
Rob Landleyb73451d2006-02-24 16:29:00 +00001452 switch (*line_ptr) {
1453 case 'c':
1454 case 'C':
1455 if (!display_in_cyl_units)
1456 i *= heads * sectors;
1457 break;
1458 case 'K':
1459 absolute = 1024;
1460 break;
1461 case 'k':
1462 absolute = 1000;
1463 break;
1464 case 'm':
1465 case 'M':
1466 absolute = 1000000;
1467 break;
1468 case 'g':
1469 case 'G':
1470 absolute = 1000000000;
1471 break;
1472 default:
1473 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001475 if (absolute) {
1476 unsigned long long bytes;
1477 unsigned long unit;
1478
1479 bytes = (unsigned long long) i * absolute;
1480 unit = sector_size * units_per_sector;
1481 bytes += unit/2; /* round */
1482 bytes /= unit;
1483 i = bytes;
1484 }
1485 if (minus)
1486 i = -i;
1487 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001488 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 i = atoi(line_ptr);
1490 while (isdigit(*line_ptr)) {
1491 line_ptr++;
1492 use_default = 0;
1493 }
1494 }
1495 if (use_default)
Eric Andersen040f4402003-07-30 08:40:37 +00001496 printf(_("Using default value %u\n"), i = dflt);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001497 if (i >= low && i <= high)
1498 break;
1499 else
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001500 printf(_("Value is out of range\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501 }
1502 return i;
1503}
1504
Rob Landleyb73451d2006-02-24 16:29:00 +00001505static int
1506get_partition(int warn, int max)
1507{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001508 struct pte *pe;
1509 int i;
1510
1511 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1512 pe = &ptes[i];
1513
1514 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001515 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1516 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1517 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1518 ) {
1519 printf(_("Warning: partition %d has empty type\n"), i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001520 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001521 }
1522 return i;
1523}
1524
1525static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001526get_existing_partition(int warn, int max)
1527{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001528 int pno = -1;
1529 int i;
1530
1531 for (i = 0; i < max; i++) {
1532 struct pte *pe = &ptes[i];
1533 struct partition *p = pe->part_table;
1534
1535 if (p && !is_cleared_partition(p)) {
1536 if (pno >= 0)
1537 goto not_unique;
1538 pno = i;
1539 }
1540 }
1541 if (pno >= 0) {
1542 printf(_("Selected partition %d\n"), pno+1);
1543 return pno;
1544 }
1545 printf(_("No partition is defined yet!\n"));
1546 return -1;
1547
1548 not_unique:
1549 return get_partition(warn, max);
1550}
1551
1552static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001553get_nonexisting_partition(int warn, int max)
1554{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555 int pno = -1;
1556 int i;
1557
1558 for (i = 0; i < max; i++) {
1559 struct pte *pe = &ptes[i];
1560 struct partition *p = pe->part_table;
1561
1562 if (p && is_cleared_partition(p)) {
1563 if (pno >= 0)
1564 goto not_unique;
1565 pno = i;
1566 }
1567 }
1568 if (pno >= 0) {
1569 printf(_("Selected partition %d\n"), pno+1);
1570 return pno;
1571 }
1572 printf(_("All primary partitions have been defined already!\n"));
1573 return -1;
1574
1575 not_unique:
1576 return get_partition(warn, max);
1577}
1578
1579
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001580static void
1581change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001582{
1583 display_in_cyl_units = !display_in_cyl_units;
1584 update_units();
1585 printf(_("Changing display/entry units to %s\n"),
1586 str_units(PLURAL));
1587}
1588
1589static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001590toggle_active(int i)
1591{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001592 struct pte *pe = &ptes[i];
1593 struct partition *p = pe->part_table;
1594
Rob Landleyb73451d2006-02-24 16:29:00 +00001595 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001596 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001597 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1598 pe->changed = 1;
1599}
1600
1601static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001602toggle_dos_compatibility_flag(void)
1603{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001604 dos_compatible_flag = ~dos_compatible_flag;
1605 if (dos_compatible_flag) {
1606 sector_offset = sectors;
1607 printf(_("DOS Compatibility flag is set\n"));
1608 }
1609 else {
1610 sector_offset = 1;
1611 printf(_("DOS Compatibility flag is not set\n"));
1612 }
1613}
1614
1615static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001616delete_partition(int i)
1617{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001618 struct pte *pe = &ptes[i];
1619 struct partition *p = pe->part_table;
1620 struct partition *q = pe->ext_pointer;
1621
1622/* Note that for the fifth partition (i == 4) we don't actually
1623 * decrement partitions.
1624 */
1625
1626 if (warn_geometry())
1627 return; /* C/H/S not set */
1628 pe->changed = 1;
1629
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001630 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001631 sun_delete_partition(i);
1632 return;
1633 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001634 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635 sgi_delete_partition(i);
1636 return;
1637 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001638
1639 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001640 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001641 partitions = 4;
1642 ptes[ext_index].ext_pointer = NULL;
1643 extended_offset = 0;
1644 }
1645 clear_partition(p);
1646 return;
1647 }
1648
1649 if (!q->sys_ind && i > 4) {
1650 /* the last one in the chain - just delete */
1651 --partitions;
1652 --i;
1653 clear_partition(ptes[i].ext_pointer);
1654 ptes[i].changed = 1;
1655 } else {
1656 /* not the last one - further ones will be moved down */
1657 if (i > 4) {
1658 /* delete this link in the chain */
1659 p = ptes[i-1].ext_pointer;
1660 *p = *q;
1661 set_start_sect(p, get_start_sect(q));
1662 set_nr_sects(p, get_nr_sects(q));
1663 ptes[i-1].changed = 1;
1664 } else if (partitions > 5) { /* 5 will be moved to 4 */
1665 /* the first logical in a longer chain */
1666 pe = &ptes[5];
1667
1668 if (pe->part_table) /* prevent SEGFAULT */
1669 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001670 get_partition_start(pe) -
1671 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001672 pe->offset = extended_offset;
1673 pe->changed = 1;
1674 }
1675
1676 if (partitions > 5) {
1677 partitions--;
1678 while (i < partitions) {
1679 ptes[i] = ptes[i+1];
1680 i++;
1681 }
1682 } else
1683 /* the only logical: clear only */
1684 clear_partition(ptes[i].part_table);
1685 }
1686}
1687
1688static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001689change_sysid(void)
1690{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001691 int i, sys, origsys;
1692 struct partition *p;
1693
Eric Andersen040f4402003-07-30 08:40:37 +00001694 /* If sgi_label then don't use get_existing_partition,
1695 let the user select a partition, since get_existing_partition()
1696 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001697 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001698 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001699 } else {
1700 i = get_partition(0, partitions);
1701 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001702 if (i == -1)
1703 return;
1704 p = ptes[i].part_table;
1705 origsys = sys = get_sysid(i);
1706
1707 /* if changing types T to 0 is allowed, then
1708 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001709 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001710 printf(_("Partition %d does not exist yet!\n"), i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001711 return;
1712 }
1713 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001714 sys = read_hex (get_sys_types());
1715
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001716 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 printf(_("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001718 "(but not to Linux). Having partitions of\n"
1719 "type 0 is probably unwise. You can delete\n"
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001720 "a partition using the 'd' command.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001721 /* break; */
1722 }
1723
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001724 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001725 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001726 printf(_("You cannot change a partition into"
Rob Landleyb73451d2006-02-24 16:29:00 +00001727 " an extended one or vice versa\n"
1728 "Delete it first.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 break;
1730 }
1731 }
1732
1733 if (sys < 256) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001734 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001735 printf(_("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001736 "as Whole disk (5),\n"
1737 "as SunOS/Solaris expects it and "
1738 "even Linux likes it.\n\n"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001739 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001740 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001741 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001742 (i == 8 && sys != 0)
1743 )
1744 ){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001745 printf(_("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001746 "as volume header (0),\nand "
1747 "partition 11 as entire volume (6)"
1748 "as IRIX expects it.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001749 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001750 if (sys == origsys)
1751 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001752 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001753 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001754 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001755 sgi_change_sysid(i, sys);
1756 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001758
Rob Landleyb73451d2006-02-24 16:29:00 +00001759 printf(_("Changed system type of partition %d "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001760 "to %x (%s)\n"), i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001761 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001762 ptes[i].changed = 1;
1763 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001764 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001765 dos_changed = 1;
1766 break;
1767 }
1768 }
1769}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001770#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1771
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001772
1773/* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1774 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1775 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1776 * Lubkin Oct. 1991). */
1777
Rob Landleyb73451d2006-02-24 16:29:00 +00001778static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00001779long2chs(ulong ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001780{
1781 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001782
1783 *c = ls / spc;
1784 ls = ls % spc;
1785 *h = ls / sectors;
1786 *s = ls % sectors + 1; /* sectors count from 1 */
1787}
1788
Rob Landleyb73451d2006-02-24 16:29:00 +00001789static void
1790check_consistency(const struct partition *p, int partition)
1791{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001792 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1793 unsigned pec, peh, pes; /* physical ending c, h, s */
1794 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1795 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001796
1797 if (!heads || !sectors || (partition >= 4))
1798 return; /* do not check extended partitions */
1799
1800/* physical beginning c, h, s */
1801 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1802 pbh = p->head;
1803 pbs = p->sector & 0x3f;
1804
1805/* physical ending c, h, s */
1806 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1807 peh = p->end_head;
1808 pes = p->end_sector & 0x3f;
1809
1810/* compute logical beginning (c, h, s) */
1811 long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1812
1813/* compute logical ending (c, h, s) */
1814 long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1815
1816/* Same physical / logical beginning? */
1817 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1818 printf(_("Partition %d has different physical/logical "
1819 "beginnings (non-Linux?):\n"), partition + 1);
1820 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1821 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1822 }
1823
1824/* Same physical / logical ending? */
1825 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1826 printf(_("Partition %d has different physical/logical "
1827 "endings:\n"), partition + 1);
1828 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1829 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1830 }
1831
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001832/* Ending on cylinder boundary? */
1833 if (peh != (heads - 1) || pes != sectors) {
Eric Andersen84bdea82004-05-19 10:49:17 +00001834 printf(_("Partition %i does not end on cylinder boundary.\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001836 }
1837}
1838
1839static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001840list_disk_geometry(void)
1841{
Eric Andersen040f4402003-07-30 08:40:37 +00001842 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001843 long megabytes = bytes/1000000;
1844
1845 if (megabytes < 10000)
1846 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001847 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001848 else
1849 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001850 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001851 printf(_("%d heads, %d sectors/track, %d cylinders"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001852 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001853 if (units_per_sector == 1)
Eric Andersen040f4402003-07-30 08:40:37 +00001854 printf(_(", total %llu sectors"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001855 total_number_of_sectors / (sector_size/512));
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001856 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001857 str_units(PLURAL),
1858 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001859}
1860
1861/*
1862 * Check whether partition entries are ordered by their starting positions.
1863 * Return 0 if OK. Return i if partition i should have been earlier.
1864 * Two separate checks: primary and logical partitions.
1865 */
1866static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001867wrong_p_order(int *prev)
1868{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001869 const struct pte *pe;
1870 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001871 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001872 int i, last_i = 0;
1873
1874 for (i = 0 ; i < partitions; i++) {
1875 if (i == 4) {
1876 last_i = 4;
1877 last_p_start_pos = 0;
1878 }
1879 pe = &ptes[i];
1880 if ((p = pe->part_table)->sys_ind) {
1881 p_start_pos = get_partition_start(pe);
1882
1883 if (last_p_start_pos > p_start_pos) {
1884 if (prev)
1885 *prev = last_i;
1886 return i;
1887 }
1888
1889 last_p_start_pos = p_start_pos;
1890 last_i = i;
1891 }
1892 }
1893 return 0;
1894}
1895
Denis Vlasenko834410a2006-11-29 12:00:28 +00001896#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001897/*
1898 * Fix the chain of logicals.
1899 * extended_offset is unchanged, the set of sectors used is unchanged
1900 * The chain is sorted so that sectors increase, and so that
1901 * starting sectors increase.
1902 *
1903 * After this it may still be that cfdisk doesnt like the table.
1904 * (This is because cfdisk considers expanded parts, from link to
1905 * end of partition, and these may still overlap.)
1906 * Now
1907 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1908 * may help.
1909 */
1910static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001911fix_chain_of_logicals(void)
1912{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001913 int j, oj, ojj, sj, sjj;
1914 struct partition *pj,*pjj,tmp;
1915
1916 /* Stage 1: sort sectors but leave sector of part 4 */
1917 /* (Its sector is the global extended_offset.) */
1918 stage1:
1919 for (j = 5; j < partitions-1; j++) {
1920 oj = ptes[j].offset;
1921 ojj = ptes[j+1].offset;
1922 if (oj > ojj) {
1923 ptes[j].offset = ojj;
1924 ptes[j+1].offset = oj;
1925 pj = ptes[j].part_table;
1926 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1927 pjj = ptes[j+1].part_table;
1928 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1929 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001930 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001931 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001932 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001933 goto stage1;
1934 }
1935 }
1936
1937 /* Stage 2: sort starting sectors */
1938 stage2:
1939 for (j = 4; j < partitions-1; j++) {
1940 pj = ptes[j].part_table;
1941 pjj = ptes[j+1].part_table;
1942 sj = get_start_sect(pj);
1943 sjj = get_start_sect(pjj);
1944 oj = ptes[j].offset;
1945 ojj = ptes[j+1].offset;
1946 if (oj+sj > ojj+sjj) {
1947 tmp = *pj;
1948 *pj = *pjj;
1949 *pjj = tmp;
1950 set_start_sect(pj, ojj+sjj-oj);
1951 set_start_sect(pjj, oj+sj-ojj);
1952 goto stage2;
1953 }
1954 }
1955
1956 /* Probably something was changed */
1957 for (j = 4; j < partitions; j++)
1958 ptes[j].changed = 1;
1959}
1960
1961
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001962static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001963fix_partition_table_order(void)
1964{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001965 struct pte *pei, *pek;
1966 int i,k;
1967
1968 if (!wrong_p_order(NULL)) {
1969 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1970 return;
1971 }
1972
1973 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1974 /* partition i should have come earlier, move it */
1975 /* We have to move data in the MBR */
1976 struct partition *pi, *pk, *pe, pbuf;
1977 pei = &ptes[i];
1978 pek = &ptes[k];
1979
1980 pe = pei->ext_pointer;
1981 pei->ext_pointer = pek->ext_pointer;
1982 pek->ext_pointer = pe;
1983
1984 pi = pei->part_table;
1985 pk = pek->part_table;
1986
1987 memmove(&pbuf, pi, sizeof(struct partition));
1988 memmove(pi, pk, sizeof(struct partition));
1989 memmove(pk, &pbuf, sizeof(struct partition));
1990
1991 pei->changed = pek->changed = 1;
1992 }
1993
1994 if (i)
1995 fix_chain_of_logicals();
1996
1997 printf("Done.\n");
1998
1999}
2000#endif
2001
2002static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002003list_table(int xtra)
2004{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006 int i, w;
2007
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002008 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002009 sun_list_table(xtra);
2010 return;
2011 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002012 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013 sgi_list_table(xtra);
2014 return;
2015 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002016
2017 list_disk_geometry();
2018
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002019 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020 xbsd_print_disklabel(xtra);
2021 return;
2022 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002023
2024 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2025 but if the device name ends in a digit, say /dev/foo1,
2026 then the partition is called /dev/foo1p3. */
2027 w = strlen(disk_device);
2028 if (w && isdigit(disk_device[w-1]))
2029 w++;
2030 if (w < 5)
2031 w = 5;
2032
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002033 // 1 12345678901 12345678901 12345678901 12
2034 printf(_("%*s Boot Start End Blocks Id System\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002035 w+1, _("Device"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002036
2037 for (i = 0; i < partitions; i++) {
2038 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002039 off_t psects;
2040 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002041 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002042
2043 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002044 if (!p || is_cleared_partition(p))
2045 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002047 psects = get_nr_sects(p);
2048 pblocks = psects;
2049 podd = 0;
2050
2051 if (sector_size < 1024) {
2052 pblocks /= (1024 / sector_size);
2053 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002054 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002055 if (sector_size > 1024)
2056 pblocks *= (sector_size / 1024);
2057
2058 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2059 partname(disk_device, i+1, w+2),
2060 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2061 ? '*' : '?',
2062 (unsigned long long) cround(get_partition_start(pe)), /* start */
2063 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2064 - (psects ? 1 : 0)),
2065 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2066 p->sys_ind, /* type id */
2067 partition_type(p->sys_ind)); /* type name */
2068
2069 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002070 }
2071
2072 /* Is partition table in disk order? It need not be, but... */
2073 /* partition table entries are not checked for correct order if this
2074 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002075 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002076 /* FIXME */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002077 printf(_("\nPartition table entries are not in disk order\n"));
2078 }
2079}
2080
Denis Vlasenko834410a2006-11-29 12:00:28 +00002081#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002082static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002083x_list_table(int extend)
2084{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002085 const struct pte *pe;
2086 const struct partition *p;
2087 int i;
2088
2089 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2090 disk_device, heads, sectors, cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002091 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002092 for (i = 0 ; i < partitions; i++) {
2093 pe = &ptes[i];
2094 p = (extend ? pe->ext_pointer : pe->part_table);
2095 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002096 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002097 i + 1, p->boot_ind, p->head,
2098 sector(p->sector),
2099 cylinder(p->sector, p->cyl), p->end_head,
2100 sector(p->end_sector),
2101 cylinder(p->end_sector, p->end_cyl),
2102 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2103 if (p->sys_ind)
2104 check_consistency(p, i);
2105 }
2106 }
2107}
2108#endif
2109
Denis Vlasenko834410a2006-11-29 12:00:28 +00002110#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002112fill_bounds(off_t *first, off_t *last)
2113{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002114 int i;
2115 const struct pte *pe = &ptes[0];
2116 const struct partition *p;
2117
2118 for (i = 0; i < partitions; pe++,i++) {
2119 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002120 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121 first[i] = 0xffffffff;
2122 last[i] = 0;
2123 } else {
2124 first[i] = get_partition_start(pe);
2125 last[i] = first[i] + get_nr_sects(p) - 1;
2126 }
2127 }
2128}
2129
2130static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002131check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002132{
Eric Andersend9261492004-06-28 23:50:31 +00002133 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002134
2135 real_s = sector(s) - 1;
2136 real_c = cylinder(s, c);
2137 total = (real_c * sectors + real_s) * heads + h;
2138 if (!total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002139 printf(_("Warning: partition %d contains sector 0\n"), n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 if (h >= heads)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002141 printf(_("Partition %d: head %d greater than maximum %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142 n, h + 1, heads);
2143 if (real_s >= sectors)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002144 printf(_("Partition %d: sector %d greater than "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002145 "maximum %d\n"), n, s, sectors);
2146 if (real_c >= cylinders)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002147 printf(_("Partitions %d: cylinder %llu greater than "
Eric Andersend9261492004-06-28 23:50:31 +00002148 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149 if (cylinders <= 1024 && start != total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002150 printf(_("Partition %d: previous sectors %llu disagrees with "
Eric Andersend9261492004-06-28 23:50:31 +00002151 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002152}
2153
2154static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002155verify(void)
2156{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002157 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002158 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002159 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 struct partition *p;
2161
2162 if (warn_geometry())
2163 return;
2164
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002165 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002166 verify_sun();
2167 return;
2168 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002169 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002170 verify_sgi(1);
2171 return;
2172 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002173
2174 fill_bounds(first, last);
2175 for (i = 0; i < partitions; i++) {
2176 struct pte *pe = &ptes[i];
2177
2178 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002179 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002180 check_consistency(p, i);
2181 if (get_partition_start(pe) < first[i])
2182 printf(_("Warning: bad start-of-data in "
2183 "partition %d\n"), i + 1);
2184 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2185 last[i]);
2186 total += last[i] + 1 - first[i];
2187 for (j = 0; j < i; j++)
2188 if ((first[i] >= first[j] && first[i] <= last[j])
2189 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2190 printf(_("Warning: partition %d overlaps "
2191 "partition %d.\n"), j + 1, i + 1);
2192 total += first[i] >= first[j] ?
2193 first[i] : first[j];
2194 total -= last[i] <= last[j] ?
2195 last[i] : last[j];
2196 }
2197 }
2198 }
2199
2200 if (extended_offset) {
2201 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002202 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203 get_nr_sects(pex->part_table) - 1;
2204
2205 for (i = 4; i < partitions; i++) {
2206 total++;
2207 p = ptes[i].part_table;
2208 if (!p->sys_ind) {
2209 if (i != 4 || i + 1 < partitions)
2210 printf(_("Warning: partition %d "
2211 "is empty\n"), i + 1);
2212 }
2213 else if (first[i] < extended_offset ||
2214 last[i] > e_last)
2215 printf(_("Logical partition %d not entirely in "
2216 "partition %d\n"), i + 1, ext_index + 1);
2217 }
2218 }
2219
2220 if (total > heads * sectors * cylinders)
2221 printf(_("Total allocated sectors %d greater than the maximum "
2222 "%d\n"), total, heads * sectors * cylinders);
2223 else if ((total = heads * sectors * cylinders - total) != 0)
2224 printf(_("%d unallocated sectors\n"), total);
2225}
2226
2227static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002228add_partition(int n, int sys)
2229{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002230 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002231 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002232 struct partition *p = ptes[n].part_table;
2233 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002234 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002235 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236 first[partitions], last[partitions];
2237
2238 if (p && p->sys_ind) {
2239 printf(_("Partition %d is already defined. Delete "
2240 "it before re-adding it.\n"), n + 1);
2241 return;
2242 }
2243 fill_bounds(first, last);
2244 if (n < 4) {
2245 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002246 if (display_in_cyl_units || !total_number_of_sectors)
2247 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002248 else
Eric Andersen040f4402003-07-30 08:40:37 +00002249 llimit = total_number_of_sectors - 1;
2250 limit = llimit;
2251 if (limit != llimit)
2252 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 if (extended_offset) {
2254 first[ext_index] = extended_offset;
2255 last[ext_index] = get_start_sect(q) +
2256 get_nr_sects(q) - 1;
2257 }
2258 } else {
2259 start = extended_offset + sector_offset;
2260 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2261 }
2262 if (display_in_cyl_units)
2263 for (i = 0; i < partitions; i++)
2264 first[i] = (cround(first[i]) - 1) * units_per_sector;
2265
2266 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2267 do {
2268 temp = start;
2269 for (i = 0; i < partitions; i++) {
2270 int lastplusoff;
2271
2272 if (start == ptes[i].offset)
2273 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002274 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002275 if (start >= first[i] && start <= lastplusoff)
2276 start = lastplusoff + 1;
2277 }
2278 if (start > limit)
2279 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002280 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +00002281 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002282 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002283 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002285 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002286 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287
2288 saved_start = start;
2289 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2290 0, mesg);
2291 if (display_in_cyl_units) {
2292 start = (start - 1) * units_per_sector;
2293 if (start < saved_start) start = saved_start;
2294 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002295 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002296 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002297 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002298 if (n > 4) { /* NOT for fifth partition */
2299 struct pte *pe = &ptes[n];
2300
2301 pe->offset = start - sector_offset;
2302 if (pe->offset == extended_offset) { /* must be corrected */
2303 pe->offset++;
2304 if (sector_offset == 1)
2305 start++;
2306 }
2307 }
2308
2309 for (i = 0; i < partitions; i++) {
2310 struct pte *pe = &ptes[i];
2311
2312 if (start < pe->offset && limit >= pe->offset)
2313 limit = pe->offset - 1;
2314 if (start < first[i] && limit >= first[i])
2315 limit = first[i] - 1;
2316 }
2317 if (start > limit) {
2318 printf(_("No free sectors available\n"));
2319 if (n > 4)
2320 partitions--;
2321 return;
2322 }
2323 if (cround(start) == cround(limit)) {
2324 stop = limit;
2325 } else {
2326 snprintf(mesg, sizeof(mesg),
2327 _("Last %s or +size or +sizeM or +sizeK"),
2328 str_units(SINGULAR));
2329 stop = read_int(cround(start), cround(limit), cround(limit),
2330 cround(start), mesg);
2331 if (display_in_cyl_units) {
2332 stop = stop * units_per_sector - 1;
2333 if (stop >limit)
2334 stop = limit;
2335 }
2336 }
2337
2338 set_partition(n, 0, start, stop, sys);
2339 if (n > 4)
2340 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2341
Rob Landleyb73451d2006-02-24 16:29:00 +00002342 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002343 struct pte *pe4 = &ptes[4];
2344 struct pte *pen = &ptes[n];
2345
2346 ext_index = n;
2347 pen->ext_pointer = p;
2348 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002349 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002350 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2351 pe4->ext_pointer = pe4->part_table + 1;
2352 pe4->changed = 1;
2353 partitions = 5;
2354 }
2355}
2356
2357static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002358add_logical(void)
2359{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002360 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2361 struct pte *pe = &ptes[partitions];
2362
Rob Landley081e3842006-08-03 20:07:35 +00002363 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2365 pe->ext_pointer = pe->part_table + 1;
2366 pe->offset = 0;
2367 pe->changed = 1;
2368 partitions++;
2369 }
2370 add_partition(partitions - 1, LINUX_NATIVE);
2371}
2372
2373static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002374new_partition(void)
2375{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 int i, free_primary = 0;
2377
2378 if (warn_geometry())
2379 return;
2380
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002381 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002382 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2383 return;
2384 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002385 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2387 return;
2388 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002389 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002390 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2391 "\n\tIf you want to add DOS-type partitions, create"
2392 "\n\ta new empty DOS partition table first. (Use o.)"
2393 "\n\tWARNING: "
2394 "This will destroy the present disk contents.\n"));
2395 return;
2396 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002398 for (i = 0; i < 4; i++)
2399 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002400
Rob Landleyb73451d2006-02-24 16:29:00 +00002401 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Eric Andersen84bdea82004-05-19 10:49:17 +00002402 printf(_("The maximum number of partitions has been created\n"));
2403 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002404 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002405
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002406 if (!free_primary) {
2407 if (extended_offset)
2408 add_logical();
2409 else
2410 printf(_("You must delete some partition and add "
2411 "an extended partition first\n"));
2412 } else {
2413 char c, line[LINE_LENGTH];
2414 snprintf(line, sizeof(line), "%s\n %s\n p primary "
2415 "partition (1-4)\n",
2416 "Command action", (extended_offset ?
2417 "l logical (5 or over)" : "e extended"));
2418 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002419 c = read_nonempty(line);
2420 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002421 i = get_nonexisting_partition(0, 4);
2422 if (i >= 0)
2423 add_partition(i, LINUX_NATIVE);
2424 return;
2425 }
2426 else if (c == 'l' && extended_offset) {
2427 add_logical();
2428 return;
2429 }
2430 else if (c == 'e' && !extended_offset) {
2431 i = get_nonexisting_partition(0, 4);
2432 if (i >= 0)
2433 add_partition(i, EXTENDED);
2434 return;
2435 }
2436 else
2437 printf(_("Invalid partition number "
Denis Vlasenko89f0b342006-11-18 22:04:09 +00002438 "for type '%c'\n"), c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 }
2440 }
2441}
2442
2443static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002444write_table(void)
2445{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 int i;
2447
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002448 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002449 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002450 if (ptes[i].changed)
2451 ptes[3].changed = 1;
2452 for (i = 3; i < partitions; i++) {
2453 struct pte *pe = &ptes[i];
2454
2455 if (pe->changed) {
2456 write_part_table_flag(pe->sectorbuffer);
2457 write_sector(pe->offset, pe->sectorbuffer);
2458 }
2459 }
2460 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002461 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 /* no test on change? the printf below might be mistaken */
2463 sgi_write_table();
2464 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002465 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 int needw = 0;
2467
Rob Landleyb73451d2006-02-24 16:29:00 +00002468 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002469 if (ptes[i].changed)
2470 needw = 1;
2471 if (needw)
2472 sun_write_table();
2473 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474
2475 printf(_("The partition table has been altered!\n\n"));
2476 reread_partition_table(1);
2477}
2478
Rob Landleyb73451d2006-02-24 16:29:00 +00002479static void
2480reread_partition_table(int leave)
2481{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 int error = 0;
2483 int i;
2484
2485 printf(_("Calling ioctl() to re-read partition table.\n"));
2486 sync();
2487 sleep(2);
2488 if ((i = ioctl(fd, BLKRRPART)) != 0) {
2489 error = errno;
2490 } else {
2491 /* some kernel versions (1.2.x) seem to have trouble
2492 rereading the partition table, but if asked to do it
2493 twice, the second time works. - biro@yggdrasil.com */
2494 sync();
2495 sleep(2);
2496 if ((i = ioctl(fd, BLKRRPART)) != 0)
2497 error = errno;
2498 }
2499
2500 if (i) {
2501 printf(_("\nWARNING: Re-reading the partition table "
2502 "failed with error %d: %s.\n"
2503 "The kernel still uses the old table.\n"
2504 "The new table will be used "
2505 "at the next reboot.\n"),
2506 error, strerror(error));
2507 }
2508
2509 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"));
2514
2515 if (leave) {
2516 close(fd);
2517
2518 printf(_("Syncing disks.\n"));
2519 sync();
2520 sleep(4); /* for sync() */
2521 exit(!!i);
2522 }
2523}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002524#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525
Denis Vlasenko834410a2006-11-29 12:00:28 +00002526#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527#define MAX_PER_LINE 16
2528static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002529print_buffer(char *pbuffer)
2530{
2531 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002532
2533 for (i = 0, l = 0; i < sector_size; i++, l++) {
2534 if (l == 0)
2535 printf("0x%03X:", i);
2536 printf(" %02X", (unsigned char) pbuffer[i]);
2537 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002538 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002539 l = -1;
2540 }
2541 }
2542 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002543 puts("");
2544 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002545}
2546
2547
2548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002549print_raw(void)
2550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002551 int i;
2552
2553 printf(_("Device: %s\n"), disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002554 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002556 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557 for (i = 3; i < partitions; i++)
2558 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002559 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002560}
2561
2562static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002563move_begin(int i)
2564{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 struct pte *pe = &ptes[i];
2566 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002567 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002568
2569 if (warn_geometry())
2570 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002571 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 printf(_("Partition %d has no data area\n"), i + 1);
2573 return;
2574 }
2575 first = get_partition_start(pe);
2576 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Rob Landleyb73451d2006-02-24 16:29:00 +00002577 _("New beginning of data")) - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578
2579 if (new != get_nr_sects(p)) {
2580 first = get_nr_sects(p) + get_start_sect(p) - new;
2581 set_nr_sects(p, first);
2582 set_start_sect(p, new);
2583 pe->changed = 1;
2584 }
2585}
2586
2587static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002588xselect(void)
2589{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 char c;
2591
Rob Landleyb73451d2006-02-24 16:29:00 +00002592 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 c = tolower(read_nonempty(_("Expert command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 switch (c) {
2596 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002597 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002598 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 break;
2600 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002601 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602 move_begin(get_partition(0, partitions));
2603 break;
2604 case 'c':
2605 user_cylinders = cylinders =
2606 read_int(1, cylinders, 1048576, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002607 _("Number of cylinders"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002608 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002610 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 warn_cylinders();
2612 break;
2613 case 'd':
2614 print_raw();
2615 break;
2616 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002617 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002619 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002620 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002621 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002622 x_list_table(1);
2623 break;
2624 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002626 fix_partition_table_order();
2627 break;
2628 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002629#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002630 create_sgilabel();
2631#endif
2632 break;
2633 case 'h':
2634 user_heads = heads = read_int(1, heads, 256, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002635 _("Number of heads"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002636 update_units();
2637 break;
2638 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002639 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002640 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 break;
2642 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002643 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 break;
2646 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002647 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002648 list_table(1);
2649 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002650 x_list_table(0);
2651 break;
2652 case 'q':
2653 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002654 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002655 exit(0);
2656 case 'r':
2657 return;
2658 case 's':
2659 user_sectors = sectors = read_int(1, sectors, 63, 0,
2660 _("Number of sectors"));
2661 if (dos_compatible_flag) {
2662 sector_offset = sectors;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002663 printf(_("Warning: setting sector offset for DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002664 "compatiblity\n"));
2665 }
2666 update_units();
2667 break;
2668 case 'v':
2669 verify();
2670 break;
2671 case 'w':
2672 write_table(); /* does not return */
2673 break;
2674 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002675 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002676 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002677 break;
2678 default:
2679 xmenu();
2680 }
2681 }
2682}
2683#endif /* ADVANCED mode */
2684
2685static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002686is_ide_cdrom_or_tape(const char *device)
2687{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 FILE *procf;
2689 char buf[100];
2690 struct stat statbuf;
2691 int is_ide = 0;
2692
2693 /* No device was given explicitly, and we are trying some
2694 likely things. But opening /dev/hdc may produce errors like
2695 "hdc: tray open or drive not ready"
2696 if it happens to be a CD-ROM drive. It even happens that
2697 the process hangs on the attempt to read a music CD.
2698 So try to be careful. This only works since 2.1.73. */
2699
2700 if (strncmp("/dev/hd", device, 7))
2701 return 0;
2702
2703 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2704 procf = fopen(buf, "r");
2705 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2706 is_ide = (!strncmp(buf, "cdrom", 5) ||
2707 !strncmp(buf, "tape", 4));
2708 else
2709 /* Now when this proc file does not exist, skip the
2710 device when it is read-only. */
2711 if (stat(device, &statbuf) == 0)
2712 is_ide = ((statbuf.st_mode & 0222) == 0);
2713
2714 if (procf)
2715 fclose(procf);
2716 return is_ide;
2717}
2718
Rob Landley5527b912006-02-25 03:46:10 +00002719
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002720static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002721try(const char *device, int user_specified)
2722{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 int gb;
2724
2725 disk_device = device;
2726 if (setjmp(listingbuf))
2727 return;
2728 if (!user_specified)
2729 if (is_ide_cdrom_or_tape(device))
2730 return;
2731 if ((fd = open(disk_device, type_open)) >= 0) {
2732 gb = get_boot(try_only);
2733 if (gb > 0) { /* I/O error */
2734 close(fd);
2735 } else if (gb < 0) { /* no DOS signature */
2736 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002737 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 return;
Rob Landley5527b912006-02-25 03:46:10 +00002739 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002740#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002741 if (btrydev(device) < 0)
2742#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002743 printf(_("Disk %s doesn't contain a valid "
Rob Landleyb73451d2006-02-24 16:29:00 +00002744 "partition table\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002745 close(fd);
2746 } else {
2747 close(fd);
2748 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002749#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002750 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002752 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002753#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002754 }
2755 } else {
2756 /* Ignore other errors, since we try IDE
2757 and SCSI hard disks which may not be
2758 installed on the system. */
2759 if (errno == EACCES) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002760 printf(_("Cannot open %s\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761 return;
2762 }
2763 }
2764}
2765
2766/* for fdisk -l: try all things in /proc/partitions
2767 that look like a partition name (do not end in a digit) */
2768static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002769tryprocpt(void)
2770{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002771 FILE *procpt;
2772 char line[100], ptname[100], devname[120], *s;
2773 int ma, mi, sz;
2774
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002775 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002776
2777 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002778 if (sscanf(line, " %d %d %d %[^\n ]",
2779 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002780 continue;
2781 for (s = ptname; *s; s++);
2782 if (isdigit(s[-1]))
2783 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002784 sprintf(devname, "/dev/%s", ptname);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002785 try(devname, 0);
2786 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002787#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002788 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002789#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002790}
2791
Denis Vlasenko834410a2006-11-29 12:00:28 +00002792#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002793static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002794unknown_command(int c)
2795{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002796 printf(_("%c: unknown command\n"), c);
2797}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002798#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002799
Rob Landleyb73451d2006-02-24 16:29:00 +00002800int fdisk_main(int argc, char **argv)
2801{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002802 char *str_b, *str_C, *str_H, *str_S;
2803 unsigned opt;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002804 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002805 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806 * fdisk -v
2807 * fdisk -l [-b sectorsize] [-u] device ...
2808 * fdisk -s [partition] ...
2809 * fdisk [-b sectorsize] [-u] device
2810 *
2811 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002812 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002813 enum {
2814 OPT_b = 1 << 0,
2815 OPT_C = 1 << 1,
2816 OPT_H = 1 << 2,
2817 OPT_l = 1 << 3,
2818 OPT_S = 1 << 4,
2819 OPT_u = 1 << 5,
2820 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2821 };
2822 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2823 &str_b, &str_C, &str_H, &str_S);
2824 argc -= optind;
2825 argv += optind;
2826 if (opt & OPT_b) { // -b
2827 /* Ugly: this sector size is really per device,
2828 so cannot be combined with multiple disks,
2829 and the same goes for the C/H/S options.
2830 */
2831 sector_size = xatoi_u(str_b);
2832 if (sector_size != 512 && sector_size != 1024 &&
2833 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002834 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002835 sector_offset = 2;
2836 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002838 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2839 if (opt & OPT_H) { // -H
2840 user_heads = xatoi_u(str_H);
2841 if (user_heads <= 0 || user_heads >= 256)
2842 user_heads = 0;
2843 }
2844 //if (opt & OPT_l) // -l
2845 if (opt & OPT_S) { // -S
2846 user_sectors = xatoi_u(str_S);
2847 if (user_sectors <= 0 || user_sectors >= 64)
2848 user_sectors = 0;
2849 }
2850 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2851 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002852
Denis Vlasenko834410a2006-11-29 12:00:28 +00002853 if (user_set_sector_size && argc != 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854 printf(_("Warning: the -b (set sector size) option should"
2855 " be used with one specified device\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856
Denis Vlasenko834410a2006-11-29 12:00:28 +00002857#if ENABLE_FEATURE_FDISK_WRITABLE
2858 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002860#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002861 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002862 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002863 int k;
2864#if __GNUC__
2865 /* avoid gcc warning:
2866 variable `k' might be clobbered by `longjmp' */
2867 (void)&k;
2868#endif
2869 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002870 for (k = 0; k < argc; k++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002871 try(argv[k], 1);
2872 } else {
2873 /* we no longer have default device names */
2874 /* but, we can use /proc/partitions instead */
2875 tryprocpt();
2876 }
2877 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002878#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002879 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002880#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881
Denis Vlasenko834410a2006-11-29 12:00:28 +00002882#if ENABLE_FEATURE_FDISK_BLKSIZE
2883 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002884 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002885 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002886
2887 nowarn = 1;
2888 type_open = O_RDONLY;
2889
Denis Vlasenko834410a2006-11-29 12:00:28 +00002890 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002891 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892
Denis Vlasenko834410a2006-11-29 12:00:28 +00002893 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002895 fd = open(disk_device, type_open);
2896 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002897 fdisk_fatal(unable_to_open);
2898 if (ioctl(fd, BLKGETSIZE, &size))
2899 fdisk_fatal(ioctl_error);
2900 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002901 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902 printf("%ld\n", size/2);
2903 else
2904 printf("%s: %ld\n", argv[j], size/2);
2905 }
2906 return 0;
2907 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002908#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909
Denis Vlasenko834410a2006-11-29 12:00:28 +00002910#if ENABLE_FEATURE_FDISK_WRITABLE
2911 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002912 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913
Denis Vlasenko834410a2006-11-29 12:00:28 +00002914 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 get_boot(fdisk);
2916
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002917 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002918 /* OSF label, and no DOS label */
2919 printf(_("Detected an OSF/1 disklabel on %s, entering "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002920 "disklabel mode.\n"), disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002921 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002922 /*Why do we do this? It seems to be counter-intuitive*/
2923 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 /* If we return we may want to make an empty DOS label? */
2925 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002926
2927 while (1) {
2928 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002929 c = tolower(read_nonempty(_("Command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 switch (c) {
2931 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 toggle_sunflags(get_partition(1, partitions),
2936 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002937 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 sgi_set_bootpartition(
2939 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 else
2941 unknown_command(c);
2942 break;
2943 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002945 printf(_("\nThe current boot file is: %s\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002946 sgi_get_bootfile());
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002947 if (read_maybe_empty(_("Please enter the name of the "
Rob Landleyb73451d2006-02-24 16:29:00 +00002948 "new boot file: ")) == '\n')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002949 printf(_("Boot file unchanged\n"));
2950 else
2951 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002952 }
2953#if ENABLE_FEATURE_OSF_LABEL
2954 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002955 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002956#endif
2957 break;
2958 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002959 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002961 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002962 toggle_sunflags(get_partition(1, partitions),
2963 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002964 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002965 sgi_set_swappartition(
2966 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002967 else
2968 unknown_command(c);
2969 break;
2970 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002971 {
Eric Andersen040f4402003-07-30 08:40:37 +00002972 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002973 /* If sgi_label then don't use get_existing_partition,
2974 let the user select a partition, since
2975 get_existing_partition() only works for Linux-like
2976 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002977 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002978 j = get_existing_partition(1, partitions);
2979 } else {
2980 j = get_partition(1, partitions);
2981 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002982 if (j >= 0)
2983 delete_partition(j);
2984 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 break;
2986 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002987 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002988 create_sgiinfo();
2989 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990 unknown_command(c);
2991 case 'l':
2992 list_types(get_sys_types());
2993 break;
2994 case 'm':
2995 menu();
2996 break;
2997 case 'n':
2998 new_partition();
2999 break;
3000 case 'o':
3001 create_doslabel();
3002 break;
3003 case 'p':
3004 list_table(0);
3005 break;
3006 case 'q':
3007 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00003008 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003009 return 0;
3010 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003011#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003012 create_sunlabel();
3013#endif
3014 break;
3015 case 't':
3016 change_sysid();
3017 break;
3018 case 'u':
3019 change_units();
3020 break;
3021 case 'v':
3022 verify();
3023 break;
3024 case 'w':
3025 write_table(); /* does not return */
3026 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003027#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003028 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003029 if (LABEL_IS_SGI) {
3030 printf(_("\n\tSorry, no experts menu for SGI "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003031 "partition tables available.\n\n"));
3032 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003033 xselect();
3034 break;
3035#endif
3036 default:
3037 unknown_command(c);
3038 menu();
3039 }
3040 }
3041 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003042#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003043}