blob: c226d6a004cc751d98250968d1b1513272843c5d [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{
Rob Landleyb73451d2006-02-24 16:29:00 +0000333 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000334 read_nonempty(_("Hex code (type L to list codes): "));
Rob Landleyb73451d2006-02-24 16:29:00 +0000335 if (*line_ptr == 'l' || *line_ptr == 'L')
336 list_types(sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000337 else if (isxdigit(*line_ptr)) {
338 return strtoul(line_ptr, NULL, 16);
Rob Landleyb73451d2006-02-24 16:29:00 +0000339 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000340 }
341}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000342#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000343
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000344#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000345
346typedef struct {
347 unsigned char info[128]; /* Informative text string */
348 unsigned char spare0[14];
349 struct sun_info {
350 unsigned char spare1;
351 unsigned char id;
352 unsigned char spare2;
353 unsigned char flags;
354 } infos[8];
355 unsigned char spare1[246]; /* Boot information etc. */
356 unsigned short rspeed; /* Disk rotational speed */
357 unsigned short pcylcount; /* Physical cylinder count */
358 unsigned short sparecyl; /* extra sects per cylinder */
359 unsigned char spare2[4]; /* More magic... */
360 unsigned short ilfact; /* Interleave factor */
361 unsigned short ncyl; /* Data cylinder count */
362 unsigned short nacyl; /* Alt. cylinder count */
363 unsigned short ntrks; /* Tracks per cylinder */
364 unsigned short nsect; /* Sectors per track */
365 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000366 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000367 uint32_t start_cylinder;
368 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000369 } partitions[8];
370 unsigned short magic; /* Magic number */
371 unsigned short csum; /* Label xor'd checksum */
372} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000373#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000374#define SUNOS_SWAP 3
375#define SUN_WHOLE_DISK 5
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000376STATIC_OSF void bsd_select(void);
377STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000378#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000379
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000380#define SGI_VOLHDR 0x00
381/* 1 and 2 were used for drive types no longer supported by SGI */
382#define SGI_SWAP 0x03
383/* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
384#define SGI_VOLUME 0x06
385#define SGI_EFS 0x07
386#define SGI_LVOL 0x08
387#define SGI_RLVOL 0x09
388#define SGI_XFS 0x0a
389#define SGI_XFSLOG 0x0b
390#define SGI_XLV 0x0c
391#define SGI_XVM 0x0d
392#define SGI_ENTIRE_DISK SGI_VOLUME
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000393#if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000394static uint16_t
395__swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000396{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000397 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000398}
399
Rob Landley88621d72006-08-29 19:41:06 +0000400static uint32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000401__swap32(uint32_t x)
402{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000403 return (x << 24) |
404 ((x & 0xFF00) << 8) |
405 ((x & 0xFF0000) >> 8) |
406 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000407}
408#endif
409
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000410STATIC_SGI const struct systypes sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000411STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000412STATIC_SGI int sgi_get_sysid(int i);
413STATIC_SGI void sgi_delete_partition(int i);
414STATIC_SGI void sgi_change_sysid(int i, int sys);
415STATIC_SGI void sgi_list_table(int xtra);
416STATIC_SGI void sgi_set_xcyl(void);
417STATIC_SGI int verify_sgi(int verbose);
418STATIC_SGI void sgi_add_partition(int n, int sys);
419STATIC_SGI void sgi_set_swappartition(int i);
420STATIC_SGI const char *sgi_get_bootfile(void);
421STATIC_SGI void sgi_set_bootfile(const char* aFile);
422STATIC_SGI void create_sgiinfo(void);
423STATIC_SGI void sgi_write_table(void);
424STATIC_SGI void sgi_set_bootpartition(int i);
425
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000426#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000427
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000428STATIC_SUN const struct systypes sun_sys_types[];
429STATIC_SUN void sun_delete_partition(int i);
430STATIC_SUN void sun_change_sysid(int i, int sys);
431STATIC_SUN void sun_list_table(int xtra);
432STATIC_SUN void sun_set_xcyl(void);
433STATIC_SUN void add_sun_partition(int n, int sys);
434STATIC_SUN void sun_set_alt_cyl(void);
435STATIC_SUN void sun_set_ncyl(int cyl);
436STATIC_SUN void sun_set_xcyl(void);
437STATIC_SUN void sun_set_ilfact(void);
438STATIC_SUN void sun_set_rspeed(void);
439STATIC_SUN void sun_set_pcylcount(void);
440STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
441STATIC_SUN void verify_sun(void);
442STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000443#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000444
445/* DOS partition types */
446
447static const struct systypes i386_sys_types[] = {
Rob Landleyb73451d2006-02-24 16:29:00 +0000448 { "\x00" "Empty" },
449 { "\x01" "FAT12" },
450 { "\x04" "FAT16 <32M" },
451 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
452 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
453 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
454 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
455 { "\x0b" "Win95 FAT32" },
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000456 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
Rob Landleyb73451d2006-02-24 16:29:00 +0000457 { "\x0e" "Win95 FAT16 (LBA)" },
458 { "\x0f" "Win95 Ext'd (LBA)" },
459 { "\x11" "Hidden FAT12" },
460 { "\x12" "Compaq diagnostics" },
461 { "\x14" "Hidden FAT16 <32M" },
462 { "\x16" "Hidden FAT16" },
463 { "\x17" "Hidden HPFS/NTFS" },
464 { "\x1b" "Hidden Win95 FAT32" },
465 { "\x1c" "Hidden Win95 FAT32 (LBA)" },
466 { "\x1e" "Hidden Win95 FAT16 (LBA)" },
467 { "\x3c" "PartitionMagic recovery" },
468 { "\x41" "PPC PReP Boot" },
469 { "\x42" "SFS" },
470 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
471 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
472 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
473 { "\x82" "Linux swap" }, /* also Solaris */
474 { "\x83" "Linux" },
475 { "\x84" "OS/2 hidden C: drive" },
476 { "\x85" "Linux extended" },
477 { "\x86" "NTFS volume set" },
478 { "\x87" "NTFS volume set" },
479 { "\x8e" "Linux LVM" },
480 { "\x9f" "BSD/OS" }, /* BSDI */
481 { "\xa0" "IBM Thinkpad hibernation" },
482 { "\xa5" "FreeBSD" }, /* various BSD flavours */
483 { "\xa6" "OpenBSD" },
484 { "\xa8" "Darwin UFS" },
485 { "\xa9" "NetBSD" },
486 { "\xab" "Darwin boot" },
487 { "\xb7" "BSDI fs" },
488 { "\xb8" "BSDI swap" },
489 { "\xbe" "Solaris boot" },
490 { "\xeb" "BeOS fs" },
491 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
492 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
493 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
494 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
495 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
496 autodetect using persistent
497 superblock */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000498#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
Rob Landleyb73451d2006-02-24 16:29:00 +0000499 { "\x02" "XENIX root" },
500 { "\x03" "XENIX usr" },
501 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
502 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
503 { "\x10" "OPUS" },
504 { "\x18" "AST SmartSleep" },
505 { "\x24" "NEC DOS" },
506 { "\x39" "Plan 9" },
507 { "\x40" "Venix 80286" },
508 { "\x4d" "QNX4.x" },
509 { "\x4e" "QNX4.x 2nd part" },
510 { "\x4f" "QNX4.x 3rd part" },
511 { "\x50" "OnTrack DM" },
512 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
513 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
514 { "\x53" "OnTrack DM6 Aux3" },
515 { "\x54" "OnTrackDM6" },
516 { "\x55" "EZ-Drive" },
517 { "\x56" "Golden Bow" },
518 { "\x5c" "Priam Edisk" },
519 { "\x61" "SpeedStor" },
520 { "\x64" "Novell Netware 286" },
521 { "\x65" "Novell Netware 386" },
522 { "\x70" "DiskSecure Multi-Boot" },
523 { "\x75" "PC/IX" },
524 { "\x93" "Amoeba" },
525 { "\x94" "Amoeba BBT" }, /* (bad block table) */
526 { "\xa7" "NeXTSTEP" },
527 { "\xbb" "Boot Wizard hidden" },
528 { "\xc1" "DRDOS/sec (FAT-12)" },
529 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
530 { "\xc6" "DRDOS/sec (FAT-16)" },
531 { "\xc7" "Syrinx" },
532 { "\xda" "Non-FS data" },
533 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000534 Concurrent DOS or CTOS */
Rob Landleyb73451d2006-02-24 16:29:00 +0000535 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
536 { "\xdf" "BootIt" }, /* BootIt EMBRM */
537 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000538 extended partition */
Rob Landleyb73451d2006-02-24 16:29:00 +0000539 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
540 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000541 partition < 1024 cyl. */
Rob Landleyb73451d2006-02-24 16:29:00 +0000542 { "\xf1" "SpeedStor" },
543 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
544 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
545 { "\xff" "BBT" }, /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000546#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000547 { 0 }
548};
549
550
Denis Vlasenko834410a2006-11-29 12:00:28 +0000551#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000552/* start_sect and nr_sects are stored little endian on all machines */
553/* moreover, they are not aligned correctly */
554static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000555store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000556{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000557 cp[0] = val;
558 cp[1] = val >> 8;
559 cp[2] = val >> 16;
560 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000561}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000562#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000563
Denis Vlasenko834410a2006-11-29 12:00:28 +0000564static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000565read4_little_endian(const unsigned char *cp)
566{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000567 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000568}
569
Denis Vlasenko834410a2006-11-29 12:00:28 +0000570#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000571static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000572set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000573{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000574 store4_little_endian(p->start4, start_sect);
575}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000576#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000577
Eric Andersend9261492004-06-28 23:50:31 +0000578static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000579get_start_sect(const struct partition *p)
580{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000581 return read4_little_endian(p->start4);
582}
583
Denis Vlasenko834410a2006-11-29 12:00:28 +0000584#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000585static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000586set_nr_sects(struct partition *p, int32_t nr_sects)
587{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588 store4_little_endian(p->size4, nr_sects);
589}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000590#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000591
Eric Andersend9261492004-06-28 23:50:31 +0000592static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000593get_nr_sects(const struct partition *p)
594{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595 return read4_little_endian(p->size4);
596}
597
598/* normally O_RDWR, -l option gives O_RDONLY */
599static int type_open = O_RDWR;
600
601
Rob Landleyb73451d2006-02-24 16:29:00 +0000602static int ext_index; /* the prime extended partition */
603static int listing; /* no aborts for fdisk -l */
604static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000605#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000606static int dos_changed;
607static int nowarn; /* no warnings for fdisk -l/-s */
608#endif
609
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000610
611
Denis Vlasenko834410a2006-11-29 12:00:28 +0000612static unsigned user_cylinders, user_heads, user_sectors;
613static unsigned pt_heads, pt_sectors;
614static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000615
Eric Andersend9261492004-06-28 23:50:31 +0000616static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000617
Eric Andersen040f4402003-07-30 08:40:37 +0000618static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000619
620
621static jmp_buf listingbuf;
622
Rob Landleyb73451d2006-02-24 16:29:00 +0000623static void fdisk_fatal(enum failure why)
624{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000625 const char *message;
626
627 if (listing) {
628 close(fd);
629 longjmp(listingbuf, 1);
630 }
631
632 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000633 case unable_to_open:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000634 message = "\nUnable to open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000635 break;
636 case unable_to_read:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000637 message = "\nUnable to read %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000638 break;
639 case unable_to_seek:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000640 message = "\nUnable to seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000641 break;
642 case unable_to_write:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000643 message = "\nUnable to write %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000644 break;
645 case ioctl_error:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000646 message = "\nBLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000647 break;
648 default:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000649 message = "\nFatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000650 }
651
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000652 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000653}
654
655static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000656seek_sector(off_t secno)
657{
Eric Andersen0a92f352004-03-30 09:21:54 +0000658 off_t offset = secno * sector_size;
659 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000660 fdisk_fatal(unable_to_seek);
661}
662
Denis Vlasenko834410a2006-11-29 12:00:28 +0000663#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000664static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000665write_sector(off_t secno, char *buf)
666{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000667 seek_sector(secno);
668 if (write(fd, buf, sector_size) != sector_size)
669 fdisk_fatal(unable_to_write);
670}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000671#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000672
673/* Allocate a buffer and read a partition table sector */
674static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000675read_pte(struct pte *pe, off_t offset)
676{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000677 pe->offset = offset;
678 pe->sectorbuffer = (char *) xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000679 seek_sector(offset);
680 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
681 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000682#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000683 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000684#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685 pe->part_table = pe->ext_pointer = NULL;
686}
687
Denis Vlasenko834410a2006-11-29 12:00:28 +0000688static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000689get_partition_start(const struct pte *pe)
690{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000691 return pe->offset + get_start_sect(pe->part_table);
692}
693
Denis Vlasenko834410a2006-11-29 12:00:28 +0000694#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695/*
696 * Avoid warning about DOS partitions when no DOS partition was changed.
697 * Here a heuristic "is probably dos partition".
698 * We might also do the opposite and warn in all cases except
699 * for "is probably nondos partition".
700 */
701static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000702is_dos_partition(int t)
703{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000704 return (t == 1 || t == 4 || t == 6 ||
705 t == 0x0b || t == 0x0c || t == 0x0e ||
706 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
707 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
708 t == 0xc1 || t == 0xc4 || t == 0xc6);
709}
710
711static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000712menu(void)
713{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000714 if (LABEL_IS_SUN) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000715 puts(_("Command action"));
716 puts(_("\ta\ttoggle a read only flag")); /* sun */
717 puts(_("\tb\tedit bsd disklabel"));
718 puts(_("\tc\ttoggle the mountable flag")); /* sun */
719 puts(_("\td\tdelete a partition"));
720 puts(_("\tl\tlist known partition types"));
721 puts(_("\tm\tprint this menu"));
722 puts(_("\tn\tadd a new partition"));
723 puts(_("\to\tcreate a new empty DOS partition table"));
724 puts(_("\tp\tprint the partition table"));
725 puts(_("\tq\tquit without saving changes"));
726 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
727 puts(_("\tt\tchange a partition's system id"));
728 puts(_("\tu\tchange display/entry units"));
729 puts(_("\tv\tverify the partition table"));
730 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000731#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000732 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000733#endif
734 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000735 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000736 puts(_("Command action"));
737 puts(_("\ta\tselect bootable partition")); /* sgi flavour */
738 puts(_("\tb\tedit bootfile entry")); /* sgi */
739 puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
740 puts(_("\td\tdelete a partition"));
741 puts(_("\tl\tlist known partition types"));
742 puts(_("\tm\tprint this menu"));
743 puts(_("\tn\tadd a new partition"));
744 puts(_("\to\tcreate a new empty DOS partition table"));
745 puts(_("\tp\tprint the partition table"));
746 puts(_("\tq\tquit without saving changes"));
747 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
748 puts(_("\tt\tchange a partition's system id"));
749 puts(_("\tu\tchange display/entry units"));
750 puts(_("\tv\tverify the partition table"));
751 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000752 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000753 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000754 puts(_("Command action"));
755 puts(_("\tm\tprint this menu"));
756 puts(_("\to\tcreate a new empty DOS partition table"));
757 puts(_("\tq\tquit without saving changes"));
758 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000760 {
Rob Landleyb73451d2006-02-24 16:29:00 +0000761 puts(_("Command action"));
762 puts(_("\ta\ttoggle a bootable flag"));
763 puts(_("\tb\tedit bsd disklabel"));
764 puts(_("\tc\ttoggle the dos compatibility flag"));
765 puts(_("\td\tdelete a partition"));
766 puts(_("\tl\tlist known partition types"));
767 puts(_("\tm\tprint this menu"));
768 puts(_("\tn\tadd a new partition"));
769 puts(_("\to\tcreate a new empty DOS partition table"));
770 puts(_("\tp\tprint the partition table"));
771 puts(_("\tq\tquit without saving changes"));
772 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
773 puts(_("\tt\tchange a partition's system id"));
774 puts(_("\tu\tchange display/entry units"));
775 puts(_("\tv\tverify the partition table"));
776 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko834410a2006-11-29 12:00:28 +0000777#if ENABLE_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000778 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000779#endif
780 }
781}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000782#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
783
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000784
Denis Vlasenko834410a2006-11-29 12:00:28 +0000785#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000786static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000787xmenu(void)
788{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000789 if (LABEL_IS_SUN) {
790 puts(_("Command action"));
791 puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
792 puts(_("\tc\tchange number of cylinders"));
793 puts(_("\td\tprint the raw data in the partition table"));
794 puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
795 puts(_("\th\tchange number of heads"));
796 puts(_("\ti\tchange interleave factor")); /*sun*/
797 puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
798 puts(_("\tm\tprint this menu"));
799 puts(_("\tp\tprint the partition table"));
800 puts(_("\tq\tquit without saving changes"));
801 puts(_("\tr\treturn to main menu"));
802 puts(_("\ts\tchange number of sectors/track"));
803 puts(_("\tv\tverify the partition table"));
804 puts(_("\tw\twrite table to disk and exit"));
805 puts(_("\ty\tchange number of physical cylinders")); /*sun*/
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000806 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000807 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000808 puts(_("Command action"));
809 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
810 puts(_("\tc\tchange number of cylinders"));
811 puts(_("\td\tprint the raw data in the partition table"));
812 puts(_("\te\tlist extended partitions")); /* !sun */
813 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
814 puts(_("\th\tchange number of heads"));
815 puts(_("\tm\tprint this menu"));
816 puts(_("\tp\tprint the partition table"));
817 puts(_("\tq\tquit without saving changes"));
818 puts(_("\tr\treturn to main menu"));
819 puts(_("\ts\tchange number of sectors/track"));
820 puts(_("\tv\tverify the partition table"));
821 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000822 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000823 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000824 puts(_("Command action"));
825 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
826 puts(_("\tc\tchange number of cylinders"));
827 puts(_("\td\tprint the raw data in the partition table"));
828 puts(_("\te\tlist extended partitions")); /* !sun */
829 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
830 puts(_("\th\tchange number of heads"));
831 puts(_("\tm\tprint this menu"));
832 puts(_("\tp\tprint the partition table"));
833 puts(_("\tq\tquit without saving changes"));
834 puts(_("\tr\treturn to main menu"));
835 puts(_("\ts\tchange number of sectors/track"));
836 puts(_("\tv\tverify the partition table"));
837 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000838 } else {
Rob Landleyb73451d2006-02-24 16:29:00 +0000839 puts(_("Command action"));
840 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
841 puts(_("\tc\tchange number of cylinders"));
842 puts(_("\td\tprint the raw data in the partition table"));
843 puts(_("\te\tlist extended partitions")); /* !sun */
844 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000845#if ENABLE_FEATURE_SGI_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +0000846 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000847#endif
Rob Landleyb73451d2006-02-24 16:29:00 +0000848 puts(_("\th\tchange number of heads"));
849 puts(_("\tm\tprint this menu"));
850 puts(_("\tp\tprint the partition table"));
851 puts(_("\tq\tquit without saving changes"));
852 puts(_("\tr\treturn to main menu"));
853 puts(_("\ts\tchange number of sectors/track"));
854 puts(_("\tv\tverify the partition table"));
855 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000856 }
857}
858#endif /* ADVANCED mode */
859
Denis Vlasenko834410a2006-11-29 12:00:28 +0000860#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861static const struct systypes *
Rob Landleyb73451d2006-02-24 16:29:00 +0000862get_sys_types(void)
863{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000864 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000865 LABEL_IS_SUN ? sun_sys_types :
866 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000867 i386_sys_types);
868}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000869#else
870#define get_sys_types() i386_sys_types
871#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000872
873static const char *partition_type(unsigned char type)
874{
875 int i;
876 const struct systypes *types = get_sys_types();
877
Rob Landleyb73451d2006-02-24 16:29:00 +0000878 for (i = 0; types[i].name; i++)
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000879 if ((unsigned char )types[i].name[0] == type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000880 return types[i].name + 1;
881
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000882 return _("Unknown");
883}
884
885
Denis Vlasenko834410a2006-11-29 12:00:28 +0000886#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000887static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000888get_sysid(int i)
889{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000890 return LABEL_IS_SUN ? sunlabel->infos[i].id :
891 (LABEL_IS_SGI ? sgi_get_sysid(i) :
892 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000893}
894
895void list_types(const struct systypes *sys)
896{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000897 unsigned last[4], done = 0, next = 0, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000898 int i;
899
900 for (i = 0; sys[i].name; i++);
901 size = i;
902
903 for (i = 3; i >= 0; i--)
904 last[3 - i] = done += (size + i - done) / (i + 1);
905 i = done = 0;
906
907 do {
908 printf("%c%2x %-15.15s", i ? ' ' : '\n',
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000909 (unsigned char)sys[next].name[0],
910 partition_type((unsigned char)sys[next].name[0]));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000911 next = last[i++] + done;
912 if (i > 3 || next >= last[i]) {
913 i = 0;
914 next = ++done;
915 }
916 } while (done < last[0]);
917 putchar('\n');
918}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000919#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000920
921static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000922is_cleared_partition(const struct partition *p)
923{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
925 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
926 get_start_sect(p) || get_nr_sects(p));
927}
928
929static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000930clear_partition(struct partition *p)
931{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000932 if (!p)
933 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000934 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935}
936
Denis Vlasenko834410a2006-11-29 12:00:28 +0000937#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000939set_partition(int i, int doext, off_t start, off_t stop, int sysid)
940{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000941 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000942 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000943
944 if (doext) {
945 p = ptes[i].ext_pointer;
946 offset = extended_offset;
947 } else {
948 p = ptes[i].part_table;
949 offset = ptes[i].offset;
950 }
951 p->boot_ind = 0;
952 p->sys_ind = sysid;
953 set_start_sect(p, start - offset);
954 set_nr_sects(p, stop - start + 1);
955 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
956 start = heads*sectors*1024 - 1;
957 set_hsc(p->head, p->sector, p->cyl, start);
958 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
959 stop = heads*sectors*1024 - 1;
960 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
961 ptes[i].changed = 1;
962}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000963#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000964
965static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000966test_c(const char **m, const char *mesg)
967{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968 int val = 0;
969 if (!*m)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000970 printf(_("You must set"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971 else {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000972 printf(" %s", *m);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973 val = 1;
974 }
975 *m = mesg;
976 return val;
977}
978
979static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000980warn_geometry(void)
981{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000982 const char *m = NULL;
983 int prev = 0;
984
985 if (!heads)
986 prev = test_c(&m, _("heads"));
987 if (!sectors)
988 prev = test_c(&m, _("sectors"));
989 if (!cylinders)
990 prev = test_c(&m, _("cylinders"));
991 if (!m)
992 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000993
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000994 printf("%s%s.\n"
Denis Vlasenko834410a2006-11-29 12:00:28 +0000995#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000996 "You can do this from the extra functions menu.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000997#endif
998 , prev ? _(" and ") : " ", m);
999
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001000 return 1;
1001}
1002
1003static void update_units(void)
1004{
1005 int cyl_units = heads * sectors;
1006
1007 if (display_in_cyl_units && cyl_units)
1008 units_per_sector = cyl_units;
1009 else
1010 units_per_sector = 1; /* in sectors */
1011}
1012
Denis Vlasenko834410a2006-11-29 12:00:28 +00001013#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001014static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001015warn_cylinders(void)
1016{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001017 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1018 printf(_("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019"The number of cylinders for this disk is set to %d.\n"
1020"There is nothing wrong with that, but this is larger than 1024,\n"
1021"and could in certain setups cause problems with:\n"
1022"1) software that runs at boot time (e.g., old versions of LILO)\n"
1023"2) booting and partitioning software from other OSs\n"
1024" (e.g., DOS FDISK, OS/2 FDISK)\n"),
1025 cylinders);
1026}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001027#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001028
1029static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001030read_extended(int ext)
1031{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032 int i;
1033 struct pte *pex;
1034 struct partition *p, *q;
1035
1036 ext_index = ext;
1037 pex = &ptes[ext];
1038 pex->ext_pointer = pex->part_table;
1039
1040 p = pex->part_table;
1041 if (!get_start_sect(p)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001042 printf(_("Bad offset in primary extended partition\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001043 return;
1044 }
1045
Rob Landleyb73451d2006-02-24 16:29:00 +00001046 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 struct pte *pe = &ptes[partitions];
1048
1049 if (partitions >= MAXIMUM_PARTS) {
1050 /* This is not a Linux restriction, but
1051 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001052 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001053 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001054#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001055 printf(_("Warning: deleting partitions after %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001057 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001058#endif
1059 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 return;
1061 }
1062
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001063 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001064
1065 if (!extended_offset)
1066 extended_offset = get_start_sect(p);
1067
1068 q = p = pt_offset(pe->sectorbuffer, 0);
1069 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001070 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001071 if (pe->ext_pointer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001072 printf(_("Warning: extra link "
1073 "pointer in partition table"
1074 " %d\n"), partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 else
1076 pe->ext_pointer = p;
1077 } else if (p->sys_ind) {
1078 if (pe->part_table)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001079 printf(_("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001080 "data in partition table"
1081 " %d\n"), partitions + 1);
1082 else
1083 pe->part_table = p;
1084 }
1085 }
1086
1087 /* very strange code here... */
1088 if (!pe->part_table) {
1089 if (q != pe->ext_pointer)
1090 pe->part_table = q;
1091 else
1092 pe->part_table = q + 1;
1093 }
1094 if (!pe->ext_pointer) {
1095 if (q != pe->part_table)
1096 pe->ext_pointer = q;
1097 else
1098 pe->ext_pointer = q + 1;
1099 }
1100
1101 p = pe->ext_pointer;
1102 partitions++;
1103 }
1104
Denis Vlasenko834410a2006-11-29 12:00:28 +00001105#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001106 /* remove empty links */
1107 remove:
1108 for (i = 4; i < partitions; i++) {
1109 struct pte *pe = &ptes[i];
1110
1111 if (!get_nr_sects(pe->part_table) &&
Rob Landleyb73451d2006-02-24 16:29:00 +00001112 (partitions > 5 || ptes[4].part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113 printf("omitting empty partition (%d)\n", i+1);
1114 delete_partition(i);
1115 goto remove; /* numbering changed */
1116 }
1117 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001118#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001119}
1120
Denis Vlasenko834410a2006-11-29 12:00:28 +00001121#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001123create_doslabel(void)
1124{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125 int i;
1126
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001127 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001128 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1129 "until you decide to write them. After that, of course, the previous\n"
1130 "content won't be recoverable.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001131
1132 current_label_type = label_dos;
1133
Denis Vlasenko834410a2006-11-29 12:00:28 +00001134#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001135 possibly_osf_label = 0;
1136#endif
1137 partitions = 4;
1138
1139 for (i = 510-64; i < 510; i++)
1140 MBRbuffer[i] = 0;
1141 write_part_table_flag(MBRbuffer);
1142 extended_offset = 0;
1143 set_all_unchanged();
1144 set_changed(0);
1145 get_boot(create_empty_dos);
1146}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001147#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148
1149static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001150get_sectorsize(void)
1151{
Rob Landley736e5252006-02-25 03:36:00 +00001152 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001153 int arg;
1154 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1155 sector_size = arg;
1156 if (sector_size != DEFAULT_SECTOR_SIZE)
1157 printf(_("Note: sector size is %d (not %d)\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001158 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001159 }
1160}
1161
Rob Landley88621d72006-08-29 19:41:06 +00001162static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001163get_kernel_geometry(void)
1164{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001165 struct hd_geometry geometry;
1166
1167 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1168 kern_heads = geometry.heads;
1169 kern_sectors = geometry.sectors;
1170 /* never use geometry.cylinders - it is truncated */
1171 }
1172}
1173
1174static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001175get_partition_table_geometry(void)
1176{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001177 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001178 struct partition *p;
1179 int i, h, s, hh, ss;
1180 int first = 1;
1181 int bad = 0;
1182
Eric Andersen3496fdc2006-01-30 23:09:20 +00001183 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184 return;
1185
1186 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001187 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188 p = pt_offset(bufp, i);
1189 if (p->sys_ind != 0) {
1190 h = p->end_head + 1;
1191 s = (p->end_sector & 077);
1192 if (first) {
1193 hh = h;
1194 ss = s;
1195 first = 0;
1196 } else if (hh != h || ss != s)
1197 bad = 1;
1198 }
1199 }
1200
1201 if (!first && !bad) {
1202 pt_heads = hh;
1203 pt_sectors = ss;
1204 }
1205}
1206
Rob Landleyb73451d2006-02-24 16:29:00 +00001207static void
1208get_geometry(void)
1209{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001210 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001211 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212
1213 get_sectorsize();
1214 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001215#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001216 guess_device_type();
1217#endif
1218 heads = cylinders = sectors = 0;
1219 kern_heads = kern_sectors = 0;
1220 pt_heads = pt_sectors = 0;
1221
1222 get_kernel_geometry();
1223 get_partition_table_geometry();
1224
1225 heads = user_heads ? user_heads :
1226 pt_heads ? pt_heads :
1227 kern_heads ? kern_heads : 255;
1228 sectors = user_sectors ? user_sectors :
1229 pt_sectors ? pt_sectors :
1230 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001231 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1232 /* got bytes */
1233 } else {
1234 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001235
1236 if (ioctl(fd, BLKGETSIZE, &longsectors))
1237 longsectors = 0;
Eric Andersen040f4402003-07-30 08:40:37 +00001238 bytes = ((unsigned long long) longsectors) << 9;
1239 }
1240
1241 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242
1243 sector_offset = 1;
1244 if (dos_compatible_flag)
1245 sector_offset = sectors;
1246
Eric Andersen040f4402003-07-30 08:40:37 +00001247 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001248 if (!cylinders)
1249 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250}
1251
1252/*
1253 * Read MBR. Returns:
1254 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1255 * 0: found or created label
1256 * 1: I/O error
1257 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001258static int
1259get_boot(enum action what)
1260{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001261 int i;
1262
1263 partitions = 4;
1264
1265 for (i = 0; i < 4; i++) {
1266 struct pte *pe = &ptes[i];
1267
1268 pe->part_table = pt_offset(MBRbuffer, i);
1269 pe->ext_pointer = NULL;
1270 pe->offset = 0;
1271 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001272#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001273 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001274#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001275 }
1276
Denis Vlasenko834410a2006-11-29 12:00:28 +00001277#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001278 if (what == create_empty_sun && check_sun_label())
1279 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001280#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281
1282 memset(MBRbuffer, 0, 512);
1283
Denis Vlasenko834410a2006-11-29 12:00:28 +00001284#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001285 if (what == create_empty_dos)
1286 goto got_dos_table; /* skip reading disk */
1287
1288 if ((fd = open(disk_device, type_open)) < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001289 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1290 if (what == try_only)
1291 return 1;
1292 fdisk_fatal(unable_to_open);
1293 } else
1294 printf(_("You will not be able to write "
1295 "the partition table.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296 }
1297
1298 if (512 != read(fd, MBRbuffer, 512)) {
1299 if (what == try_only)
1300 return 1;
1301 fdisk_fatal(unable_to_read);
1302 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001303#else
1304 if ((fd = open(disk_device, O_RDONLY)) < 0)
1305 return 1;
1306 if (512 != read(fd, MBRbuffer, 512))
1307 return 1;
1308#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001310 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001311
1312 update_units();
1313
Denis Vlasenko834410a2006-11-29 12:00:28 +00001314#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315 if (check_sun_label())
1316 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001317#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001318
Denis Vlasenko834410a2006-11-29 12:00:28 +00001319#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320 if (check_sgi_label())
1321 return 0;
1322#endif
1323
Denis Vlasenko834410a2006-11-29 12:00:28 +00001324#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325 if (check_aix_label())
1326 return 0;
1327#endif
1328
Denis Vlasenko834410a2006-11-29 12:00:28 +00001329#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001330 if (check_osf_label()) {
1331 possibly_osf_label = 1;
1332 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001333 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001334 return 0;
1335 }
1336 printf(_("This disk has both DOS and BSD magic.\n"
1337 "Give the 'b' command to go to BSD mode.\n"));
1338 }
1339#endif
1340
Denis Vlasenko834410a2006-11-29 12:00:28 +00001341#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001342 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001343#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001344
1345 if (!valid_part_table_flag(MBRbuffer)) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001346#ifndef CONFIG_FEATURE_FDISK_WRITABLE
1347 return -1;
1348#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001349 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001350 case fdisk:
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001351 printf(_("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001352 "partition table, nor Sun, SGI or OSF "
1353 "disklabel\n"));
1354#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001355#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 create_sunlabel();
1357#endif
1358#else
1359 create_doslabel();
1360#endif
1361 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 case try_only:
1363 return -1;
1364 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001365#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001366 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001367#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001368 break;
1369 default:
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001370 bb_error_msg_and_die(_("internal error"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001371 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001372#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001373 }
1374
Denis Vlasenko834410a2006-11-29 12:00:28 +00001375#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001376 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001377#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001378 warn_geometry();
1379
1380 for (i = 0; i < 4; i++) {
1381 struct pte *pe = &ptes[i];
1382
Rob Landleyb73451d2006-02-24 16:29:00 +00001383 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001384 if (partitions != 4)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001385 printf(_("Ignoring extra extended "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 "partition %d\n"), i + 1);
1387 else
1388 read_extended(i);
1389 }
1390 }
1391
1392 for (i = 3; i < partitions; i++) {
1393 struct pte *pe = &ptes[i];
1394
1395 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00001396 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 "table %d will be corrected by w(rite)\n"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00001398 pe->sectorbuffer[510],
1399 pe->sectorbuffer[511],
1400 i + 1);
1401#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001403#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404 }
1405 }
1406
1407 return 0;
1408}
1409
Denis Vlasenko834410a2006-11-29 12:00:28 +00001410#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001411/*
1412 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1413 * If the user hits Enter, DFLT is returned.
1414 * Answers like +10 are interpreted as offsets from BASE.
1415 *
1416 * There is no default if DFLT is not between LOW and HIGH.
1417 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001418static unsigned
1419read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001421 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001422 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001423 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001424
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001425 if (dflt < low || dflt > high) {
1426 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001427 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001428 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429
1430 while (1) {
1431 int use_default = default_ok;
1432
1433 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001434 do {
1435 printf(fmt, mesg, low, high, dflt);
1436 read_maybe_empty("");
1437 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1438 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001439
Eric Andersen84bdea82004-05-19 10:49:17 +00001440 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001441 int minus = (*line_ptr == '-');
1442 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001443
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001444 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001445
Rob Landleyb73451d2006-02-24 16:29:00 +00001446 while (isdigit(*++line_ptr))
1447 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001448
Rob Landleyb73451d2006-02-24 16:29:00 +00001449 switch (*line_ptr) {
1450 case 'c':
1451 case 'C':
1452 if (!display_in_cyl_units)
1453 i *= heads * sectors;
1454 break;
1455 case 'K':
1456 absolute = 1024;
1457 break;
1458 case 'k':
1459 absolute = 1000;
1460 break;
1461 case 'm':
1462 case 'M':
1463 absolute = 1000000;
1464 break;
1465 case 'g':
1466 case 'G':
1467 absolute = 1000000000;
1468 break;
1469 default:
1470 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001472 if (absolute) {
1473 unsigned long long bytes;
1474 unsigned long unit;
1475
1476 bytes = (unsigned long long) i * absolute;
1477 unit = sector_size * units_per_sector;
1478 bytes += unit/2; /* round */
1479 bytes /= unit;
1480 i = bytes;
1481 }
1482 if (minus)
1483 i = -i;
1484 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001485 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001486 i = atoi(line_ptr);
1487 while (isdigit(*line_ptr)) {
1488 line_ptr++;
1489 use_default = 0;
1490 }
1491 }
1492 if (use_default)
Eric Andersen040f4402003-07-30 08:40:37 +00001493 printf(_("Using default value %u\n"), i = dflt);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001494 if (i >= low && i <= high)
1495 break;
1496 else
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001497 printf(_("Value is out of range\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001498 }
1499 return i;
1500}
1501
Rob Landleyb73451d2006-02-24 16:29:00 +00001502static int
1503get_partition(int warn, int max)
1504{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001505 struct pte *pe;
1506 int i;
1507
1508 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1509 pe = &ptes[i];
1510
1511 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001512 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1513 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1514 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1515 ) {
1516 printf(_("Warning: partition %d has empty type\n"), i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001517 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001518 }
1519 return i;
1520}
1521
1522static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001523get_existing_partition(int warn, int max)
1524{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001525 int pno = -1;
1526 int i;
1527
1528 for (i = 0; i < max; i++) {
1529 struct pte *pe = &ptes[i];
1530 struct partition *p = pe->part_table;
1531
1532 if (p && !is_cleared_partition(p)) {
1533 if (pno >= 0)
1534 goto not_unique;
1535 pno = i;
1536 }
1537 }
1538 if (pno >= 0) {
1539 printf(_("Selected partition %d\n"), pno+1);
1540 return pno;
1541 }
1542 printf(_("No partition is defined yet!\n"));
1543 return -1;
1544
1545 not_unique:
1546 return get_partition(warn, max);
1547}
1548
1549static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001550get_nonexisting_partition(int warn, int max)
1551{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001552 int pno = -1;
1553 int i;
1554
1555 for (i = 0; i < max; i++) {
1556 struct pte *pe = &ptes[i];
1557 struct partition *p = pe->part_table;
1558
1559 if (p && is_cleared_partition(p)) {
1560 if (pno >= 0)
1561 goto not_unique;
1562 pno = i;
1563 }
1564 }
1565 if (pno >= 0) {
1566 printf(_("Selected partition %d\n"), pno+1);
1567 return pno;
1568 }
1569 printf(_("All primary partitions have been defined already!\n"));
1570 return -1;
1571
1572 not_unique:
1573 return get_partition(warn, max);
1574}
1575
1576
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001577static void
1578change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001579{
1580 display_in_cyl_units = !display_in_cyl_units;
1581 update_units();
1582 printf(_("Changing display/entry units to %s\n"),
1583 str_units(PLURAL));
1584}
1585
1586static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001587toggle_active(int i)
1588{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 struct pte *pe = &ptes[i];
1590 struct partition *p = pe->part_table;
1591
Rob Landleyb73451d2006-02-24 16:29:00 +00001592 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001593 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1595 pe->changed = 1;
1596}
1597
1598static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001599toggle_dos_compatibility_flag(void)
1600{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001601 dos_compatible_flag = ~dos_compatible_flag;
1602 if (dos_compatible_flag) {
1603 sector_offset = sectors;
1604 printf(_("DOS Compatibility flag is set\n"));
1605 }
1606 else {
1607 sector_offset = 1;
1608 printf(_("DOS Compatibility flag is not set\n"));
1609 }
1610}
1611
1612static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001613delete_partition(int i)
1614{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001615 struct pte *pe = &ptes[i];
1616 struct partition *p = pe->part_table;
1617 struct partition *q = pe->ext_pointer;
1618
1619/* Note that for the fifth partition (i == 4) we don't actually
1620 * decrement partitions.
1621 */
1622
1623 if (warn_geometry())
1624 return; /* C/H/S not set */
1625 pe->changed = 1;
1626
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001627 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001628 sun_delete_partition(i);
1629 return;
1630 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001631 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001632 sgi_delete_partition(i);
1633 return;
1634 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635
1636 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001637 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001638 partitions = 4;
1639 ptes[ext_index].ext_pointer = NULL;
1640 extended_offset = 0;
1641 }
1642 clear_partition(p);
1643 return;
1644 }
1645
1646 if (!q->sys_ind && i > 4) {
1647 /* the last one in the chain - just delete */
1648 --partitions;
1649 --i;
1650 clear_partition(ptes[i].ext_pointer);
1651 ptes[i].changed = 1;
1652 } else {
1653 /* not the last one - further ones will be moved down */
1654 if (i > 4) {
1655 /* delete this link in the chain */
1656 p = ptes[i-1].ext_pointer;
1657 *p = *q;
1658 set_start_sect(p, get_start_sect(q));
1659 set_nr_sects(p, get_nr_sects(q));
1660 ptes[i-1].changed = 1;
1661 } else if (partitions > 5) { /* 5 will be moved to 4 */
1662 /* the first logical in a longer chain */
1663 pe = &ptes[5];
1664
1665 if (pe->part_table) /* prevent SEGFAULT */
1666 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001667 get_partition_start(pe) -
1668 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001669 pe->offset = extended_offset;
1670 pe->changed = 1;
1671 }
1672
1673 if (partitions > 5) {
1674 partitions--;
1675 while (i < partitions) {
1676 ptes[i] = ptes[i+1];
1677 i++;
1678 }
1679 } else
1680 /* the only logical: clear only */
1681 clear_partition(ptes[i].part_table);
1682 }
1683}
1684
1685static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001686change_sysid(void)
1687{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001688 int i, sys, origsys;
1689 struct partition *p;
1690
Eric Andersen040f4402003-07-30 08:40:37 +00001691 /* If sgi_label then don't use get_existing_partition,
1692 let the user select a partition, since get_existing_partition()
1693 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001694 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001695 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001696 } else {
1697 i = get_partition(0, partitions);
1698 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001699 if (i == -1)
1700 return;
1701 p = ptes[i].part_table;
1702 origsys = sys = get_sysid(i);
1703
1704 /* if changing types T to 0 is allowed, then
1705 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001706 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001707 printf(_("Partition %d does not exist yet!\n"), i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001708 return;
1709 }
1710 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001711 sys = read_hex (get_sys_types());
1712
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001713 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001714 printf(_("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001715 "(but not to Linux). Having partitions of\n"
1716 "type 0 is probably unwise. You can delete\n"
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001717 "a partition using the 'd' command.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001718 /* break; */
1719 }
1720
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001721 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001722 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001723 printf(_("You cannot change a partition into"
Rob Landleyb73451d2006-02-24 16:29:00 +00001724 " an extended one or vice versa\n"
1725 "Delete it first.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001726 break;
1727 }
1728 }
1729
1730 if (sys < 256) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001731 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001732 printf(_("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001733 "as Whole disk (5),\n"
1734 "as SunOS/Solaris expects it and "
1735 "even Linux likes it.\n\n"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001736 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001737 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001738 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001739 (i == 8 && sys != 0)
1740 )
1741 ){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001742 printf(_("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001743 "as volume header (0),\nand "
1744 "partition 11 as entire volume (6)"
1745 "as IRIX expects it.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001746 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001747 if (sys == origsys)
1748 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001749 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001750 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001751 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001752 sgi_change_sysid(i, sys);
1753 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001754 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001755
Rob Landleyb73451d2006-02-24 16:29:00 +00001756 printf(_("Changed system type of partition %d "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 "to %x (%s)\n"), i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001758 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 ptes[i].changed = 1;
1760 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001761 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001762 dos_changed = 1;
1763 break;
1764 }
1765 }
1766}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001767#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1768
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001769
1770/* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1771 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1772 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1773 * Lubkin Oct. 1991). */
1774
Rob Landleyb73451d2006-02-24 16:29:00 +00001775static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00001776long2chs(ulong ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001777{
1778 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001779
1780 *c = ls / spc;
1781 ls = ls % spc;
1782 *h = ls / sectors;
1783 *s = ls % sectors + 1; /* sectors count from 1 */
1784}
1785
Rob Landleyb73451d2006-02-24 16:29:00 +00001786static void
1787check_consistency(const struct partition *p, int partition)
1788{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001789 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1790 unsigned pec, peh, pes; /* physical ending c, h, s */
1791 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1792 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793
1794 if (!heads || !sectors || (partition >= 4))
1795 return; /* do not check extended partitions */
1796
1797/* physical beginning c, h, s */
1798 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1799 pbh = p->head;
1800 pbs = p->sector & 0x3f;
1801
1802/* physical ending c, h, s */
1803 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1804 peh = p->end_head;
1805 pes = p->end_sector & 0x3f;
1806
1807/* compute logical beginning (c, h, s) */
1808 long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1809
1810/* compute logical ending (c, h, s) */
1811 long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1812
1813/* Same physical / logical beginning? */
1814 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1815 printf(_("Partition %d has different physical/logical "
1816 "beginnings (non-Linux?):\n"), partition + 1);
1817 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1818 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1819 }
1820
1821/* Same physical / logical ending? */
1822 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1823 printf(_("Partition %d has different physical/logical "
1824 "endings:\n"), partition + 1);
1825 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1826 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1827 }
1828
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829/* Ending on cylinder boundary? */
1830 if (peh != (heads - 1) || pes != sectors) {
Eric Andersen84bdea82004-05-19 10:49:17 +00001831 printf(_("Partition %i does not end on cylinder boundary.\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001832 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833 }
1834}
1835
1836static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001837list_disk_geometry(void)
1838{
Eric Andersen040f4402003-07-30 08:40:37 +00001839 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001840 long megabytes = bytes/1000000;
1841
1842 if (megabytes < 10000)
1843 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001844 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001845 else
1846 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001847 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001848 printf(_("%d heads, %d sectors/track, %d cylinders"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001849 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001850 if (units_per_sector == 1)
Eric Andersen040f4402003-07-30 08:40:37 +00001851 printf(_(", total %llu sectors"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001852 total_number_of_sectors / (sector_size/512));
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001853 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001854 str_units(PLURAL),
1855 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001856}
1857
1858/*
1859 * Check whether partition entries are ordered by their starting positions.
1860 * Return 0 if OK. Return i if partition i should have been earlier.
1861 * Two separate checks: primary and logical partitions.
1862 */
1863static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001864wrong_p_order(int *prev)
1865{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001866 const struct pte *pe;
1867 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001868 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001869 int i, last_i = 0;
1870
1871 for (i = 0 ; i < partitions; i++) {
1872 if (i == 4) {
1873 last_i = 4;
1874 last_p_start_pos = 0;
1875 }
1876 pe = &ptes[i];
1877 if ((p = pe->part_table)->sys_ind) {
1878 p_start_pos = get_partition_start(pe);
1879
1880 if (last_p_start_pos > p_start_pos) {
1881 if (prev)
1882 *prev = last_i;
1883 return i;
1884 }
1885
1886 last_p_start_pos = p_start_pos;
1887 last_i = i;
1888 }
1889 }
1890 return 0;
1891}
1892
Denis Vlasenko834410a2006-11-29 12:00:28 +00001893#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001894/*
1895 * Fix the chain of logicals.
1896 * extended_offset is unchanged, the set of sectors used is unchanged
1897 * The chain is sorted so that sectors increase, and so that
1898 * starting sectors increase.
1899 *
1900 * After this it may still be that cfdisk doesnt like the table.
1901 * (This is because cfdisk considers expanded parts, from link to
1902 * end of partition, and these may still overlap.)
1903 * Now
1904 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1905 * may help.
1906 */
1907static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001908fix_chain_of_logicals(void)
1909{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001910 int j, oj, ojj, sj, sjj;
1911 struct partition *pj,*pjj,tmp;
1912
1913 /* Stage 1: sort sectors but leave sector of part 4 */
1914 /* (Its sector is the global extended_offset.) */
1915 stage1:
1916 for (j = 5; j < partitions-1; j++) {
1917 oj = ptes[j].offset;
1918 ojj = ptes[j+1].offset;
1919 if (oj > ojj) {
1920 ptes[j].offset = ojj;
1921 ptes[j+1].offset = oj;
1922 pj = ptes[j].part_table;
1923 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1924 pjj = ptes[j+1].part_table;
1925 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1926 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001927 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001928 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001929 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930 goto stage1;
1931 }
1932 }
1933
1934 /* Stage 2: sort starting sectors */
1935 stage2:
1936 for (j = 4; j < partitions-1; j++) {
1937 pj = ptes[j].part_table;
1938 pjj = ptes[j+1].part_table;
1939 sj = get_start_sect(pj);
1940 sjj = get_start_sect(pjj);
1941 oj = ptes[j].offset;
1942 ojj = ptes[j+1].offset;
1943 if (oj+sj > ojj+sjj) {
1944 tmp = *pj;
1945 *pj = *pjj;
1946 *pjj = tmp;
1947 set_start_sect(pj, ojj+sjj-oj);
1948 set_start_sect(pjj, oj+sj-ojj);
1949 goto stage2;
1950 }
1951 }
1952
1953 /* Probably something was changed */
1954 for (j = 4; j < partitions; j++)
1955 ptes[j].changed = 1;
1956}
1957
1958
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001959static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001960fix_partition_table_order(void)
1961{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001962 struct pte *pei, *pek;
1963 int i,k;
1964
1965 if (!wrong_p_order(NULL)) {
1966 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1967 return;
1968 }
1969
1970 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1971 /* partition i should have come earlier, move it */
1972 /* We have to move data in the MBR */
1973 struct partition *pi, *pk, *pe, pbuf;
1974 pei = &ptes[i];
1975 pek = &ptes[k];
1976
1977 pe = pei->ext_pointer;
1978 pei->ext_pointer = pek->ext_pointer;
1979 pek->ext_pointer = pe;
1980
1981 pi = pei->part_table;
1982 pk = pek->part_table;
1983
1984 memmove(&pbuf, pi, sizeof(struct partition));
1985 memmove(pi, pk, sizeof(struct partition));
1986 memmove(pk, &pbuf, sizeof(struct partition));
1987
1988 pei->changed = pek->changed = 1;
1989 }
1990
1991 if (i)
1992 fix_chain_of_logicals();
1993
1994 printf("Done.\n");
1995
1996}
1997#endif
1998
1999static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002000list_table(int xtra)
2001{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002002 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002003 int i, w;
2004
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002005 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006 sun_list_table(xtra);
2007 return;
2008 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002009 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 sgi_list_table(xtra);
2011 return;
2012 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002013
2014 list_disk_geometry();
2015
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002016 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002017 xbsd_print_disklabel(xtra);
2018 return;
2019 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020
2021 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2022 but if the device name ends in a digit, say /dev/foo1,
2023 then the partition is called /dev/foo1p3. */
2024 w = strlen(disk_device);
2025 if (w && isdigit(disk_device[w-1]))
2026 w++;
2027 if (w < 5)
2028 w = 5;
2029
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002030 // 1 12345678901 12345678901 12345678901 12
2031 printf(_("%*s Boot Start End Blocks Id System\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002032 w+1, _("Device"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002033
2034 for (i = 0; i < partitions; i++) {
2035 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002036 off_t psects;
2037 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002038 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002039
2040 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002041 if (!p || is_cleared_partition(p))
2042 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002043
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002044 psects = get_nr_sects(p);
2045 pblocks = psects;
2046 podd = 0;
2047
2048 if (sector_size < 1024) {
2049 pblocks /= (1024 / sector_size);
2050 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002051 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002052 if (sector_size > 1024)
2053 pblocks *= (sector_size / 1024);
2054
2055 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2056 partname(disk_device, i+1, w+2),
2057 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2058 ? '*' : '?',
2059 (unsigned long long) cround(get_partition_start(pe)), /* start */
2060 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2061 - (psects ? 1 : 0)),
2062 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2063 p->sys_ind, /* type id */
2064 partition_type(p->sys_ind)); /* type name */
2065
2066 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002067 }
2068
2069 /* Is partition table in disk order? It need not be, but... */
2070 /* partition table entries are not checked for correct order if this
2071 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002072 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002073 /* FIXME */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074 printf(_("\nPartition table entries are not in disk order\n"));
2075 }
2076}
2077
Denis Vlasenko834410a2006-11-29 12:00:28 +00002078#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002079static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002080x_list_table(int extend)
2081{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002082 const struct pte *pe;
2083 const struct partition *p;
2084 int i;
2085
2086 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2087 disk_device, heads, sectors, cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002088 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002089 for (i = 0 ; i < partitions; i++) {
2090 pe = &ptes[i];
2091 p = (extend ? pe->ext_pointer : pe->part_table);
2092 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002093 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094 i + 1, p->boot_ind, p->head,
2095 sector(p->sector),
2096 cylinder(p->sector, p->cyl), p->end_head,
2097 sector(p->end_sector),
2098 cylinder(p->end_sector, p->end_cyl),
2099 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2100 if (p->sys_ind)
2101 check_consistency(p, i);
2102 }
2103 }
2104}
2105#endif
2106
Denis Vlasenko834410a2006-11-29 12:00:28 +00002107#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002108static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002109fill_bounds(off_t *first, off_t *last)
2110{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111 int i;
2112 const struct pte *pe = &ptes[0];
2113 const struct partition *p;
2114
2115 for (i = 0; i < partitions; pe++,i++) {
2116 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002117 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 first[i] = 0xffffffff;
2119 last[i] = 0;
2120 } else {
2121 first[i] = get_partition_start(pe);
2122 last[i] = first[i] + get_nr_sects(p) - 1;
2123 }
2124 }
2125}
2126
2127static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002128check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002129{
Eric Andersend9261492004-06-28 23:50:31 +00002130 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002131
2132 real_s = sector(s) - 1;
2133 real_c = cylinder(s, c);
2134 total = (real_c * sectors + real_s) * heads + h;
2135 if (!total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002136 printf(_("Warning: partition %d contains sector 0\n"), n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002137 if (h >= heads)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002138 printf(_("Partition %d: head %d greater than maximum %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002139 n, h + 1, heads);
2140 if (real_s >= sectors)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002141 printf(_("Partition %d: sector %d greater than "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142 "maximum %d\n"), n, s, sectors);
2143 if (real_c >= cylinders)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002144 printf(_("Partitions %d: cylinder %llu greater than "
Eric Andersend9261492004-06-28 23:50:31 +00002145 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002146 if (cylinders <= 1024 && start != total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002147 printf(_("Partition %d: previous sectors %llu disagrees with "
Eric Andersend9261492004-06-28 23:50:31 +00002148 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149}
2150
2151static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002152verify(void)
2153{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002154 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002155 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002156 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002157 struct partition *p;
2158
2159 if (warn_geometry())
2160 return;
2161
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002162 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002163 verify_sun();
2164 return;
2165 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002166 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002167 verify_sgi(1);
2168 return;
2169 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002170
2171 fill_bounds(first, last);
2172 for (i = 0; i < partitions; i++) {
2173 struct pte *pe = &ptes[i];
2174
2175 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002176 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002177 check_consistency(p, i);
2178 if (get_partition_start(pe) < first[i])
2179 printf(_("Warning: bad start-of-data in "
2180 "partition %d\n"), i + 1);
2181 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2182 last[i]);
2183 total += last[i] + 1 - first[i];
2184 for (j = 0; j < i; j++)
2185 if ((first[i] >= first[j] && first[i] <= last[j])
2186 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2187 printf(_("Warning: partition %d overlaps "
2188 "partition %d.\n"), j + 1, i + 1);
2189 total += first[i] >= first[j] ?
2190 first[i] : first[j];
2191 total -= last[i] <= last[j] ?
2192 last[i] : last[j];
2193 }
2194 }
2195 }
2196
2197 if (extended_offset) {
2198 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002199 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002200 get_nr_sects(pex->part_table) - 1;
2201
2202 for (i = 4; i < partitions; i++) {
2203 total++;
2204 p = ptes[i].part_table;
2205 if (!p->sys_ind) {
2206 if (i != 4 || i + 1 < partitions)
2207 printf(_("Warning: partition %d "
2208 "is empty\n"), i + 1);
2209 }
2210 else if (first[i] < extended_offset ||
2211 last[i] > e_last)
2212 printf(_("Logical partition %d not entirely in "
2213 "partition %d\n"), i + 1, ext_index + 1);
2214 }
2215 }
2216
2217 if (total > heads * sectors * cylinders)
2218 printf(_("Total allocated sectors %d greater than the maximum "
2219 "%d\n"), total, heads * sectors * cylinders);
2220 else if ((total = heads * sectors * cylinders - total) != 0)
2221 printf(_("%d unallocated sectors\n"), total);
2222}
2223
2224static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002225add_partition(int n, int sys)
2226{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002228 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002229 struct partition *p = ptes[n].part_table;
2230 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002231 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002232 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002233 first[partitions], last[partitions];
2234
2235 if (p && p->sys_ind) {
2236 printf(_("Partition %d is already defined. Delete "
2237 "it before re-adding it.\n"), n + 1);
2238 return;
2239 }
2240 fill_bounds(first, last);
2241 if (n < 4) {
2242 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002243 if (display_in_cyl_units || !total_number_of_sectors)
2244 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002245 else
Eric Andersen040f4402003-07-30 08:40:37 +00002246 llimit = total_number_of_sectors - 1;
2247 limit = llimit;
2248 if (limit != llimit)
2249 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002250 if (extended_offset) {
2251 first[ext_index] = extended_offset;
2252 last[ext_index] = get_start_sect(q) +
2253 get_nr_sects(q) - 1;
2254 }
2255 } else {
2256 start = extended_offset + sector_offset;
2257 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2258 }
2259 if (display_in_cyl_units)
2260 for (i = 0; i < partitions; i++)
2261 first[i] = (cround(first[i]) - 1) * units_per_sector;
2262
2263 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2264 do {
2265 temp = start;
2266 for (i = 0; i < partitions; i++) {
2267 int lastplusoff;
2268
2269 if (start == ptes[i].offset)
2270 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002271 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272 if (start >= first[i] && start <= lastplusoff)
2273 start = lastplusoff + 1;
2274 }
2275 if (start > limit)
2276 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002277 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +00002278 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002279 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002280 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002282 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002283 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284
2285 saved_start = start;
2286 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2287 0, mesg);
2288 if (display_in_cyl_units) {
2289 start = (start - 1) * units_per_sector;
2290 if (start < saved_start) start = saved_start;
2291 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002292 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002294 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295 if (n > 4) { /* NOT for fifth partition */
2296 struct pte *pe = &ptes[n];
2297
2298 pe->offset = start - sector_offset;
2299 if (pe->offset == extended_offset) { /* must be corrected */
2300 pe->offset++;
2301 if (sector_offset == 1)
2302 start++;
2303 }
2304 }
2305
2306 for (i = 0; i < partitions; i++) {
2307 struct pte *pe = &ptes[i];
2308
2309 if (start < pe->offset && limit >= pe->offset)
2310 limit = pe->offset - 1;
2311 if (start < first[i] && limit >= first[i])
2312 limit = first[i] - 1;
2313 }
2314 if (start > limit) {
2315 printf(_("No free sectors available\n"));
2316 if (n > 4)
2317 partitions--;
2318 return;
2319 }
2320 if (cround(start) == cround(limit)) {
2321 stop = limit;
2322 } else {
2323 snprintf(mesg, sizeof(mesg),
2324 _("Last %s or +size or +sizeM or +sizeK"),
2325 str_units(SINGULAR));
2326 stop = read_int(cround(start), cround(limit), cround(limit),
2327 cround(start), mesg);
2328 if (display_in_cyl_units) {
2329 stop = stop * units_per_sector - 1;
2330 if (stop >limit)
2331 stop = limit;
2332 }
2333 }
2334
2335 set_partition(n, 0, start, stop, sys);
2336 if (n > 4)
2337 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2338
Rob Landleyb73451d2006-02-24 16:29:00 +00002339 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002340 struct pte *pe4 = &ptes[4];
2341 struct pte *pen = &ptes[n];
2342
2343 ext_index = n;
2344 pen->ext_pointer = p;
2345 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002346 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2348 pe4->ext_pointer = pe4->part_table + 1;
2349 pe4->changed = 1;
2350 partitions = 5;
2351 }
2352}
2353
2354static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002355add_logical(void)
2356{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002357 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2358 struct pte *pe = &ptes[partitions];
2359
Rob Landley081e3842006-08-03 20:07:35 +00002360 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002361 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2362 pe->ext_pointer = pe->part_table + 1;
2363 pe->offset = 0;
2364 pe->changed = 1;
2365 partitions++;
2366 }
2367 add_partition(partitions - 1, LINUX_NATIVE);
2368}
2369
2370static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002371new_partition(void)
2372{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002373 int i, free_primary = 0;
2374
2375 if (warn_geometry())
2376 return;
2377
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002378 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002379 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2380 return;
2381 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002382 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002383 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2384 return;
2385 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002386 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2388 "\n\tIf you want to add DOS-type partitions, create"
2389 "\n\ta new empty DOS partition table first. (Use o.)"
2390 "\n\tWARNING: "
2391 "This will destroy the present disk contents.\n"));
2392 return;
2393 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002395 for (i = 0; i < 4; i++)
2396 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002397
Rob Landleyb73451d2006-02-24 16:29:00 +00002398 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Eric Andersen84bdea82004-05-19 10:49:17 +00002399 printf(_("The maximum number of partitions has been created\n"));
2400 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002401 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002402
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403 if (!free_primary) {
2404 if (extended_offset)
2405 add_logical();
2406 else
2407 printf(_("You must delete some partition and add "
2408 "an extended partition first\n"));
2409 } else {
2410 char c, line[LINE_LENGTH];
2411 snprintf(line, sizeof(line), "%s\n %s\n p primary "
2412 "partition (1-4)\n",
2413 "Command action", (extended_offset ?
2414 "l logical (5 or over)" : "e extended"));
2415 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002416 c = read_nonempty(line);
2417 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002418 i = get_nonexisting_partition(0, 4);
2419 if (i >= 0)
2420 add_partition(i, LINUX_NATIVE);
2421 return;
2422 }
2423 else if (c == 'l' && extended_offset) {
2424 add_logical();
2425 return;
2426 }
2427 else if (c == 'e' && !extended_offset) {
2428 i = get_nonexisting_partition(0, 4);
2429 if (i >= 0)
2430 add_partition(i, EXTENDED);
2431 return;
2432 }
2433 else
2434 printf(_("Invalid partition number "
Denis Vlasenko89f0b342006-11-18 22:04:09 +00002435 "for type '%c'\n"), c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436 }
2437 }
2438}
2439
2440static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002441write_table(void)
2442{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 int i;
2444
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002445 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002446 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002447 if (ptes[i].changed)
2448 ptes[3].changed = 1;
2449 for (i = 3; i < partitions; i++) {
2450 struct pte *pe = &ptes[i];
2451
2452 if (pe->changed) {
2453 write_part_table_flag(pe->sectorbuffer);
2454 write_sector(pe->offset, pe->sectorbuffer);
2455 }
2456 }
2457 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002458 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 /* no test on change? the printf below might be mistaken */
2460 sgi_write_table();
2461 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002462 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002463 int needw = 0;
2464
Rob Landleyb73451d2006-02-24 16:29:00 +00002465 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 if (ptes[i].changed)
2467 needw = 1;
2468 if (needw)
2469 sun_write_table();
2470 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002471
2472 printf(_("The partition table has been altered!\n\n"));
2473 reread_partition_table(1);
2474}
2475
Rob Landleyb73451d2006-02-24 16:29:00 +00002476static void
2477reread_partition_table(int leave)
2478{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002479 int error = 0;
2480 int i;
2481
2482 printf(_("Calling ioctl() to re-read partition table.\n"));
2483 sync();
2484 sleep(2);
2485 if ((i = ioctl(fd, BLKRRPART)) != 0) {
2486 error = errno;
2487 } else {
2488 /* some kernel versions (1.2.x) seem to have trouble
2489 rereading the partition table, but if asked to do it
2490 twice, the second time works. - biro@yggdrasil.com */
2491 sync();
2492 sleep(2);
2493 if ((i = ioctl(fd, BLKRRPART)) != 0)
2494 error = errno;
2495 }
2496
2497 if (i) {
2498 printf(_("\nWARNING: Re-reading the partition table "
2499 "failed with error %d: %s.\n"
2500 "The kernel still uses the old table.\n"
2501 "The new table will be used "
2502 "at the next reboot.\n"),
2503 error, strerror(error));
2504 }
2505
2506 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002507 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002508 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2509 "partitions, please see the fdisk manual page for additional\n"
2510 "information.\n"));
2511
2512 if (leave) {
2513 close(fd);
2514
2515 printf(_("Syncing disks.\n"));
2516 sync();
2517 sleep(4); /* for sync() */
2518 exit(!!i);
2519 }
2520}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002521#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002522
Denis Vlasenko834410a2006-11-29 12:00:28 +00002523#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002524#define MAX_PER_LINE 16
2525static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002526print_buffer(char *pbuffer)
2527{
2528 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002529
2530 for (i = 0, l = 0; i < sector_size; i++, l++) {
2531 if (l == 0)
2532 printf("0x%03X:", i);
2533 printf(" %02X", (unsigned char) pbuffer[i]);
2534 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002535 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002536 l = -1;
2537 }
2538 }
2539 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002540 puts("");
2541 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542}
2543
2544
2545static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002546print_raw(void)
2547{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548 int i;
2549
2550 printf(_("Device: %s\n"), disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002551 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002552 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002553 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554 for (i = 3; i < partitions; i++)
2555 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002556 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557}
2558
2559static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002560move_begin(int i)
2561{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 struct pte *pe = &ptes[i];
2563 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002564 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565
2566 if (warn_geometry())
2567 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002568 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569 printf(_("Partition %d has no data area\n"), i + 1);
2570 return;
2571 }
2572 first = get_partition_start(pe);
2573 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Rob Landleyb73451d2006-02-24 16:29:00 +00002574 _("New beginning of data")) - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002575
2576 if (new != get_nr_sects(p)) {
2577 first = get_nr_sects(p) + get_start_sect(p) - new;
2578 set_nr_sects(p, first);
2579 set_start_sect(p, new);
2580 pe->changed = 1;
2581 }
2582}
2583
2584static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002585xselect(void)
2586{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 char c;
2588
Rob Landleyb73451d2006-02-24 16:29:00 +00002589 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002591 c = tolower(read_nonempty(_("Expert command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 switch (c) {
2593 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002594 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002595 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002596 break;
2597 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002598 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 move_begin(get_partition(0, partitions));
2600 break;
2601 case 'c':
2602 user_cylinders = cylinders =
2603 read_int(1, cylinders, 1048576, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002604 _("Number of cylinders"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002605 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 warn_cylinders();
2609 break;
2610 case 'd':
2611 print_raw();
2612 break;
2613 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002614 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002618 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 x_list_table(1);
2620 break;
2621 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002622 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 fix_partition_table_order();
2624 break;
2625 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002626#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 create_sgilabel();
2628#endif
2629 break;
2630 case 'h':
2631 user_heads = heads = read_int(1, heads, 256, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002632 _("Number of heads"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 update_units();
2634 break;
2635 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002636 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002638 break;
2639 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002640 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002642 break;
2643 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002644 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 list_table(1);
2646 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002647 x_list_table(0);
2648 break;
2649 case 'q':
2650 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002651 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002652 exit(0);
2653 case 'r':
2654 return;
2655 case 's':
2656 user_sectors = sectors = read_int(1, sectors, 63, 0,
2657 _("Number of sectors"));
2658 if (dos_compatible_flag) {
2659 sector_offset = sectors;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002660 printf(_("Warning: setting sector offset for DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002661 "compatiblity\n"));
2662 }
2663 update_units();
2664 break;
2665 case 'v':
2666 verify();
2667 break;
2668 case 'w':
2669 write_table(); /* does not return */
2670 break;
2671 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002672 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002673 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002674 break;
2675 default:
2676 xmenu();
2677 }
2678 }
2679}
2680#endif /* ADVANCED mode */
2681
2682static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002683is_ide_cdrom_or_tape(const char *device)
2684{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 FILE *procf;
2686 char buf[100];
2687 struct stat statbuf;
2688 int is_ide = 0;
2689
2690 /* No device was given explicitly, and we are trying some
2691 likely things. But opening /dev/hdc may produce errors like
2692 "hdc: tray open or drive not ready"
2693 if it happens to be a CD-ROM drive. It even happens that
2694 the process hangs on the attempt to read a music CD.
2695 So try to be careful. This only works since 2.1.73. */
2696
2697 if (strncmp("/dev/hd", device, 7))
2698 return 0;
2699
2700 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2701 procf = fopen(buf, "r");
2702 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2703 is_ide = (!strncmp(buf, "cdrom", 5) ||
2704 !strncmp(buf, "tape", 4));
2705 else
2706 /* Now when this proc file does not exist, skip the
2707 device when it is read-only. */
2708 if (stat(device, &statbuf) == 0)
2709 is_ide = ((statbuf.st_mode & 0222) == 0);
2710
2711 if (procf)
2712 fclose(procf);
2713 return is_ide;
2714}
2715
Rob Landley5527b912006-02-25 03:46:10 +00002716
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002717static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002718try(const char *device, int user_specified)
2719{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002720 int gb;
2721
2722 disk_device = device;
2723 if (setjmp(listingbuf))
2724 return;
2725 if (!user_specified)
2726 if (is_ide_cdrom_or_tape(device))
2727 return;
2728 if ((fd = open(disk_device, type_open)) >= 0) {
2729 gb = get_boot(try_only);
2730 if (gb > 0) { /* I/O error */
2731 close(fd);
2732 } else if (gb < 0) { /* no DOS signature */
2733 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002734 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002735 return;
Rob Landley5527b912006-02-25 03:46:10 +00002736 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002737#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002738 if (btrydev(device) < 0)
2739#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002740 printf(_("Disk %s doesn't contain a valid "
Rob Landleyb73451d2006-02-24 16:29:00 +00002741 "partition table\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002742 close(fd);
2743 } else {
2744 close(fd);
2745 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002746#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002747 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002748 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002749 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002750#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751 }
2752 } else {
2753 /* Ignore other errors, since we try IDE
2754 and SCSI hard disks which may not be
2755 installed on the system. */
2756 if (errno == EACCES) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002757 printf(_("Cannot open %s\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758 return;
2759 }
2760 }
2761}
2762
2763/* for fdisk -l: try all things in /proc/partitions
2764 that look like a partition name (do not end in a digit) */
2765static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002766tryprocpt(void)
2767{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768 FILE *procpt;
2769 char line[100], ptname[100], devname[120], *s;
2770 int ma, mi, sz;
2771
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002772 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002773
2774 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002775 if (sscanf(line, " %d %d %d %[^\n ]",
2776 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777 continue;
2778 for (s = ptname; *s; s++);
2779 if (isdigit(s[-1]))
2780 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002781 sprintf(devname, "/dev/%s", ptname);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002782 try(devname, 0);
2783 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002784#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002785 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002786#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002787}
2788
Denis Vlasenko834410a2006-11-29 12:00:28 +00002789#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002790static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002791unknown_command(int c)
2792{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002793 printf(_("%c: unknown command\n"), c);
2794}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002795#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002796
Rob Landleyb73451d2006-02-24 16:29:00 +00002797int fdisk_main(int argc, char **argv)
2798{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002799 char *str_b, *str_C, *str_H, *str_S;
2800 unsigned opt;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002801 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002803 * fdisk -v
2804 * fdisk -l [-b sectorsize] [-u] device ...
2805 * fdisk -s [partition] ...
2806 * fdisk [-b sectorsize] [-u] device
2807 *
2808 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002810 enum {
2811 OPT_b = 1 << 0,
2812 OPT_C = 1 << 1,
2813 OPT_H = 1 << 2,
2814 OPT_l = 1 << 3,
2815 OPT_S = 1 << 4,
2816 OPT_u = 1 << 5,
2817 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2818 };
2819 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2820 &str_b, &str_C, &str_H, &str_S);
2821 argc -= optind;
2822 argv += optind;
2823 if (opt & OPT_b) { // -b
2824 /* Ugly: this sector size is really per device,
2825 so cannot be combined with multiple disks,
2826 and the same goes for the C/H/S options.
2827 */
2828 sector_size = xatoi_u(str_b);
2829 if (sector_size != 512 && sector_size != 1024 &&
2830 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002831 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002832 sector_offset = 2;
2833 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002834 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002835 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2836 if (opt & OPT_H) { // -H
2837 user_heads = xatoi_u(str_H);
2838 if (user_heads <= 0 || user_heads >= 256)
2839 user_heads = 0;
2840 }
2841 //if (opt & OPT_l) // -l
2842 if (opt & OPT_S) { // -S
2843 user_sectors = xatoi_u(str_S);
2844 if (user_sectors <= 0 || user_sectors >= 64)
2845 user_sectors = 0;
2846 }
2847 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2848 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850 if (user_set_sector_size && argc != 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 printf(_("Warning: the -b (set sector size) option should"
2852 " be used with one specified device\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853
Denis Vlasenko834410a2006-11-29 12:00:28 +00002854#if ENABLE_FEATURE_FDISK_WRITABLE
2855 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002857#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860 int k;
2861#if __GNUC__
2862 /* avoid gcc warning:
2863 variable `k' might be clobbered by `longjmp' */
2864 (void)&k;
2865#endif
2866 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002867 for (k = 0; k < argc; k++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 try(argv[k], 1);
2869 } else {
2870 /* we no longer have default device names */
2871 /* but, we can use /proc/partitions instead */
2872 tryprocpt();
2873 }
2874 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002875#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002877#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002878
Denis Vlasenko834410a2006-11-29 12:00:28 +00002879#if ENABLE_FEATURE_FDISK_BLKSIZE
2880 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002882 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883
2884 nowarn = 1;
2885 type_open = O_RDONLY;
2886
Denis Vlasenko834410a2006-11-29 12:00:28 +00002887 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002888 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002889
Denis Vlasenko834410a2006-11-29 12:00:28 +00002890 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002891 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002892 fd = open(disk_device, type_open);
2893 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 fdisk_fatal(unable_to_open);
2895 if (ioctl(fd, BLKGETSIZE, &size))
2896 fdisk_fatal(ioctl_error);
2897 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002898 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002899 printf("%ld\n", size/2);
2900 else
2901 printf("%s: %ld\n", argv[j], size/2);
2902 }
2903 return 0;
2904 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002905#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906
Denis Vlasenko834410a2006-11-29 12:00:28 +00002907#if ENABLE_FEATURE_FDISK_WRITABLE
2908 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002909 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910
Denis Vlasenko834410a2006-11-29 12:00:28 +00002911 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002912 get_boot(fdisk);
2913
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002914 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 /* OSF label, and no DOS label */
2916 printf(_("Detected an OSF/1 disklabel on %s, entering "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002917 "disklabel mode.\n"), disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002918 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002919 /*Why do we do this? It seems to be counter-intuitive*/
2920 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002921 /* If we return we may want to make an empty DOS label? */
2922 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923
2924 while (1) {
2925 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002926 c = tolower(read_nonempty(_("Command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002927 switch (c) {
2928 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002929 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002931 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 toggle_sunflags(get_partition(1, partitions),
2933 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 sgi_set_bootpartition(
2936 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002937 else
2938 unknown_command(c);
2939 break;
2940 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002941 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942 printf(_("\nThe current boot file is: %s\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002943 sgi_get_bootfile());
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 if (read_maybe_empty(_("Please enter the name of the "
Rob Landleyb73451d2006-02-24 16:29:00 +00002945 "new boot file: ")) == '\n')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002946 printf(_("Boot file unchanged\n"));
2947 else
2948 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002949 }
2950#if ENABLE_FEATURE_OSF_LABEL
2951 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002952 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002953#endif
2954 break;
2955 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002956 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002957 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002958 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959 toggle_sunflags(get_partition(1, partitions),
2960 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002961 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002962 sgi_set_swappartition(
2963 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002964 else
2965 unknown_command(c);
2966 break;
2967 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002968 {
Eric Andersen040f4402003-07-30 08:40:37 +00002969 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002970 /* If sgi_label then don't use get_existing_partition,
2971 let the user select a partition, since
2972 get_existing_partition() only works for Linux-like
2973 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002974 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002975 j = get_existing_partition(1, partitions);
2976 } else {
2977 j = get_partition(1, partitions);
2978 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002979 if (j >= 0)
2980 delete_partition(j);
2981 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 break;
2983 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002984 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 create_sgiinfo();
2986 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002987 unknown_command(c);
2988 case 'l':
2989 list_types(get_sys_types());
2990 break;
2991 case 'm':
2992 menu();
2993 break;
2994 case 'n':
2995 new_partition();
2996 break;
2997 case 'o':
2998 create_doslabel();
2999 break;
3000 case 'p':
3001 list_table(0);
3002 break;
3003 case 'q':
3004 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00003005 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003006 return 0;
3007 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003008#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003009 create_sunlabel();
3010#endif
3011 break;
3012 case 't':
3013 change_sysid();
3014 break;
3015 case 'u':
3016 change_units();
3017 break;
3018 case 'v':
3019 verify();
3020 break;
3021 case 'w':
3022 write_table(); /* does not return */
3023 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003024#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003025 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003026 if (LABEL_IS_SGI) {
3027 printf(_("\n\tSorry, no experts menu for SGI "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003028 "partition tables available.\n\n"));
3029 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003030 xselect();
3031 break;
3032#endif
3033 default:
3034 unknown_command(c);
3035 menu();
3036 }
3037 }
3038 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003039#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003040}