blob: 493c6740b2ddd129ac59d97ef33fe8984f5941a2 [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"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000012
Denis Vlasenko834410a2006-11-29 12:00:28 +000013/* Looks like someone forgot to add this to config system */
14#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
15# define ENABLE_FEATURE_FDISK_BLKSIZE 0
16# define USE_FEATURE_FDISK_BLKSIZE(a)
17#endif
18
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000019#define DEFAULT_SECTOR_SIZE 512
20#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000021#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000022#define MAXIMUM_PARTS 60
23
24#define ACTIVE_FLAG 0x80
25
26#define EXTENDED 0x05
27#define WIN98_EXTENDED 0x0f
28#define LINUX_PARTITION 0x81
29#define LINUX_SWAP 0x82
30#define LINUX_NATIVE 0x83
31#define LINUX_EXTENDED 0x85
32#define LINUX_LVM 0x8e
33#define LINUX_RAID 0xfd
34
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000035#define IS_EXTENDED(i) \
36 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
37
38#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
39
40#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
41#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
42
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000043struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000044 unsigned char heads;
45 unsigned char sectors;
46 unsigned short cylinders;
47 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000048};
49
Denis Vlasenko98ae2162006-10-12 19:30:44 +000050#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000051
Denis Vlasenkobd852072007-03-19 14:43:38 +000052static const char msg_building_new_label[] =
53"Building a new %s. Changes will remain in memory only,\n"
54"until you decide to write them. After that the previous content\n"
55"won't be recoverable.\n\n";
56
57static const char msg_part_already_defined[] =
58"Partition %d is already defined, delete it before re-adding\n";
59
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000060
Denis Vlasenko834410a2006-11-29 12:00:28 +000061static unsigned sector_size = DEFAULT_SECTOR_SIZE;
62static unsigned user_set_sector_size;
63static unsigned sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000064
Denis Vlasenko834410a2006-11-29 12:00:28 +000065#if ENABLE_FEATURE_OSF_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +000066static int possibly_osf_label;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000067#endif
68
Denis Vlasenko834410a2006-11-29 12:00:28 +000069static unsigned heads, sectors, cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000070static void update_units(void);
71
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000073struct partition {
74 unsigned char boot_ind; /* 0x80 - active */
75 unsigned char head; /* starting head */
76 unsigned char sector; /* starting sector */
77 unsigned char cyl; /* starting cylinder */
78 unsigned char sys_ind; /* What partition type */
79 unsigned char end_head; /* end head */
80 unsigned char end_sector; /* end sector */
81 unsigned char end_cyl; /* end cylinder */
82 unsigned char start4[4]; /* starting sector counting from 0 */
83 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000084} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000085
86enum failure {
87 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
88 unable_to_write
89};
90
Denis Vlasenko98ae2162006-10-12 19:30:44 +000091enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +000092 label_dos, label_sun, label_sgi, label_aix, label_osf
93};
Denis Vlasenko98ae2162006-10-12 19:30:44 +000094#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000095
Denis Vlasenko834410a2006-11-29 12:00:28 +000096#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000097#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000098#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +000099#else
100#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000101#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000102#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000103
Denis Vlasenko834410a2006-11-29 12:00:28 +0000104#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000105#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000106#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000107#else
108#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000109#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000110#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000111
Denis Vlasenko834410a2006-11-29 12:00:28 +0000112#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000113#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000114#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000115#else
116#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000117#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000118#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000119
Denis Vlasenko834410a2006-11-29 12:00:28 +0000120#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000121#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000122#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000123#else
124#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000125#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000126#endif
Rob Landley5527b912006-02-25 03:46:10 +0000127
Rob Landleyb73451d2006-02-24 16:29:00 +0000128enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000129
Rob Landley5527b912006-02-25 03:46:10 +0000130static enum label_type current_label_type;
131
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000132static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000133static int fd; /* the disk */
134static int partitions = 4; /* maximum partition + 1 */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000135static int display_in_cyl_units = 1;
136static unsigned units_per_sector = 1;
137#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000138static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000139static void reread_partition_table(int leave);
140static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000141static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000142static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000143static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000144#endif
145static const char *partition_type(unsigned char type);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000146static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000147static void get_geometry(void);
148static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000149
150#define PLURAL 0
151#define SINGULAR 1
152
153#define hex_val(c) ({ \
154 char _c = (c); \
155 isdigit(_c) ? _c - '0' : \
156 tolower(_c) + 10 - 'a'; \
157 })
158
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000159#define LINE_LENGTH 80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000160#define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
161 (n) * sizeof(struct partition)))
162#define sector(s) ((s) & 0x3f)
163#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
164
165#define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
166 ((h) + heads * cylinder(s,c)))
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000167#define set_hsc(h,s,c,sector) \
168 do { \
169 s = sector % sectors + 1; \
170 sector /= sectors; \
171 h = sector % heads; \
172 sector /= heads; \
173 c = sector & 0xff; \
174 s |= (sector >> 2) & 0xc0; \
175 } while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000176
Denis Vlasenko28703012006-12-19 20:32:02 +0000177static unsigned get_start_sect(const struct partition *p);
178static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000179
180/*
181 * per partition table entry data
182 *
183 * The four primary partitions have the same sectorbuffer (MBRbuffer)
184 * and have NULL ext_pointer.
185 * Each logical partition table entry has two pointers, one for the
186 * partition and one link to the next one.
187 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000188struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000189 struct partition *part_table; /* points into sectorbuffer */
190 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000191#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000192 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000193#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000194 off_t offset; /* disk sector number */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000195 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000196};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000197
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000198struct globals {
199 /* Raw disk label. For DOS-type partition tables the MBR,
200 * with descriptions of the primary partitions. */
201 char MBRbuffer[MAX_SECTOR_SIZE];
202 /* Partition tables */
203 struct pte ptes[MAXIMUM_PARTS];
204};
205
206#define G (*(struct globals*)bb_common_bufsiz1)
207#define MBRbuffer (G.MBRbuffer)
208#define ptes (G.ptes)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000209
Denis Vlasenkobd852072007-03-19 14:43:38 +0000210
211/*
212 * return partition name - uses static storage
213 */
214static const char *
215partname(const char *dev, int pno, int lth)
216{
217 static char buffer[80];
218 const char *p;
219 int w, wp;
220 int bufsiz;
221 char *bufp;
222
223 bufp = buffer;
224 bufsiz = sizeof(buffer);
225
226 w = strlen(dev);
227 p = "";
228
229 if (isdigit(dev[w-1]))
230 p = "p";
231
232 /* devfs kludge - note: fdisk partition names are not supposed
233 to equal kernel names, so there is no reason to do this */
234 if (strcmp(dev + w - 4, "disc") == 0) {
235 w -= 4;
236 p = "part";
237 }
238
239 wp = strlen(p);
240
241 if (lth) {
242 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
243 lth-wp-2, w, dev, p, pno);
244 } else {
245 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
246 }
247 return bufp;
248}
249
Denis Vlasenko834410a2006-11-29 12:00:28 +0000250#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000251static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000252set_all_unchanged(void)
253{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000254 int i;
255
256 for (i = 0; i < MAXIMUM_PARTS; i++)
257 ptes[i].changed = 0;
258}
259
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000260static ATTRIBUTE_ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000261set_changed(int i)
262{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000263 ptes[i].changed = 1;
264}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000265#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000266
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000267static ATTRIBUTE_ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000268get_part_table(int i)
269{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000270 return ptes[i].part_table;
271}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000272
273static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000274str_units(int n)
275{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000276 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000277 return display_in_cyl_units ? "cylinder" : "sector";
278 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000279}
280
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000281static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000282valid_part_table_flag(const char *mbuffer)
283{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000284 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000285}
286
Denis Vlasenko834410a2006-11-29 12:00:28 +0000287#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000288static ATTRIBUTE_ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000289write_part_table_flag(char *b)
290{
291 b[510] = 0x55;
292 b[511] = 0xaa;
293}
294
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000295static char line_buffer[LINE_LENGTH];
296static char *line_ptr;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000297
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000298/* read line; return 0 or first printable char */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000299static int
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000300read_line(const char *prompt)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000301{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000302 int sz;
303
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000304 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000305 if (sz <= 0)
306 exit(0); /* Ctrl-D or Ctrl-C */
307
308 if (line_buffer[sz-1] == '\n')
309 line_buffer[--sz] = '\0';
310
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000311 line_ptr = line_buffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000312 while (*line_ptr && !isgraph(*line_ptr))
313 line_ptr++;
314 return *line_ptr;
315}
316
317static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000318read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000319{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000320 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000321 return *line_ptr;
322}
323
324static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000325read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000326{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000327 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000328 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000329 line_ptr[0] = '\n';
330 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000331 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000332 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000333}
334
335static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000336read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000337{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000338 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000339 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000340 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000341 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000342 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000343 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000344 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000345 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000346 if (v > 0xff)
347 /* Bad input also triggers this */
348 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000349 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000350 }
351}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000352#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000353
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000354#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000355
356typedef struct {
357 unsigned char info[128]; /* Informative text string */
358 unsigned char spare0[14];
359 struct sun_info {
360 unsigned char spare1;
361 unsigned char id;
362 unsigned char spare2;
363 unsigned char flags;
364 } infos[8];
365 unsigned char spare1[246]; /* Boot information etc. */
366 unsigned short rspeed; /* Disk rotational speed */
367 unsigned short pcylcount; /* Physical cylinder count */
368 unsigned short sparecyl; /* extra sects per cylinder */
369 unsigned char spare2[4]; /* More magic... */
370 unsigned short ilfact; /* Interleave factor */
371 unsigned short ncyl; /* Data cylinder count */
372 unsigned short nacyl; /* Alt. cylinder count */
373 unsigned short ntrks; /* Tracks per cylinder */
374 unsigned short nsect; /* Sectors per track */
375 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000376 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000377 uint32_t start_cylinder;
378 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000379 } partitions[8];
380 unsigned short magic; /* Magic number */
381 unsigned short csum; /* Label xor'd checksum */
382} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000383#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000384STATIC_OSF void bsd_select(void);
385STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000386#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000387
Denis Vlasenko28703012006-12-19 20:32:02 +0000388#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000389static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000390fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000391{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000392 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000393}
394
Rob Landley88621d72006-08-29 19:41:06 +0000395static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000396fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000397{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000398 return (x << 24) |
399 ((x & 0xFF00) << 8) |
400 ((x & 0xFF0000) >> 8) |
401 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000402}
403#endif
404
Denis Vlasenkobd852072007-03-19 14:43:38 +0000405STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000406STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000407STATIC_SGI int sgi_get_sysid(int i);
408STATIC_SGI void sgi_delete_partition(int i);
409STATIC_SGI void sgi_change_sysid(int i, int sys);
410STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000411#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000412STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000413#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000414STATIC_SGI int verify_sgi(int verbose);
415STATIC_SGI void sgi_add_partition(int n, int sys);
416STATIC_SGI void sgi_set_swappartition(int i);
417STATIC_SGI const char *sgi_get_bootfile(void);
418STATIC_SGI void sgi_set_bootfile(const char* aFile);
419STATIC_SGI void create_sgiinfo(void);
420STATIC_SGI void sgi_write_table(void);
421STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000422#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000423
Denis Vlasenkobd852072007-03-19 14:43:38 +0000424STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000425STATIC_SUN void sun_delete_partition(int i);
426STATIC_SUN void sun_change_sysid(int i, int sys);
427STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000428STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000429#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000430STATIC_SUN void sun_set_alt_cyl(void);
431STATIC_SUN void sun_set_ncyl(int cyl);
432STATIC_SUN void sun_set_xcyl(void);
433STATIC_SUN void sun_set_ilfact(void);
434STATIC_SUN void sun_set_rspeed(void);
435STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000436#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000437STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
438STATIC_SUN void verify_sun(void);
439STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000440#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000441
442/* DOS partition types */
443
Denis Vlasenkobd852072007-03-19 14:43:38 +0000444static const char *const i386_sys_types[] = {
445 "\x00" "Empty",
446 "\x01" "FAT12",
447 "\x04" "FAT16 <32M",
448 "\x05" "Extended", /* DOS 3.3+ extended partition */
449 "\x06" "FAT16", /* DOS 16-bit >=32M */
450 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
451 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
452 "\x0b" "Win95 FAT32",
453 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
454 "\x0e" "Win95 FAT16 (LBA)",
455 "\x0f" "Win95 Ext'd (LBA)",
456 "\x11" "Hidden FAT12",
457 "\x12" "Compaq diagnostics",
458 "\x14" "Hidden FAT16 <32M",
459 "\x16" "Hidden FAT16",
460 "\x17" "Hidden HPFS/NTFS",
461 "\x1b" "Hidden Win95 FAT32",
462 "\x1c" "Hidden W95 FAT32 (LBA)",
463 "\x1e" "Hidden W95 FAT16 (LBA)",
464 "\x3c" "Part.Magic recovery",
465 "\x41" "PPC PReP Boot",
466 "\x42" "SFS",
467 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
468 "\x80" "Old Minix", /* Minix 1.4a and earlier */
469 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
470 "\x82" "Linux swap", /* also Solaris */
471 "\x83" "Linux",
472 "\x84" "OS/2 hidden C: drive",
473 "\x85" "Linux extended",
474 "\x86" "NTFS volume set",
475 "\x87" "NTFS volume set",
476 "\x8e" "Linux LVM",
477 "\x9f" "BSD/OS", /* BSDI */
478 "\xa0" "Thinkpad hibernation",
479 "\xa5" "FreeBSD", /* various BSD flavours */
480 "\xa6" "OpenBSD",
481 "\xa8" "Darwin UFS",
482 "\xa9" "NetBSD",
483 "\xab" "Darwin boot",
484 "\xb7" "BSDI fs",
485 "\xb8" "BSDI swap",
486 "\xbe" "Solaris boot",
487 "\xeb" "BeOS fs",
488 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
489 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
490 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
491 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
492 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
Rob Landleyb73451d2006-02-24 16:29:00 +0000493 autodetect using persistent
494 superblock */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000495#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000496 "\x02" "XENIX root",
497 "\x03" "XENIX usr",
498 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
499 "\x09" "AIX bootable", /* AIX data or Coherent */
500 "\x10" "OPUS",
501 "\x18" "AST SmartSleep",
502 "\x24" "NEC DOS",
503 "\x39" "Plan 9",
504 "\x40" "Venix 80286",
505 "\x4d" "QNX4.x",
506 "\x4e" "QNX4.x 2nd part",
507 "\x4f" "QNX4.x 3rd part",
508 "\x50" "OnTrack DM",
509 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
510 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
511 "\x53" "OnTrack DM6 Aux3",
512 "\x54" "OnTrackDM6",
513 "\x55" "EZ-Drive",
514 "\x56" "Golden Bow",
515 "\x5c" "Priam Edisk",
516 "\x61" "SpeedStor",
517 "\x64" "Novell Netware 286",
518 "\x65" "Novell Netware 386",
519 "\x70" "DiskSecure Multi-Boot",
520 "\x75" "PC/IX",
521 "\x93" "Amoeba",
522 "\x94" "Amoeba BBT", /* (bad block table) */
523 "\xa7" "NeXTSTEP",
524 "\xbb" "Boot Wizard hidden",
525 "\xc1" "DRDOS/sec (FAT-12)",
526 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
527 "\xc6" "DRDOS/sec (FAT-16)",
528 "\xc7" "Syrinx",
529 "\xda" "Non-FS data",
530 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
531 Concurrent DOS or CTOS */
532 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
533 "\xdf" "BootIt", /* BootIt EMBRM */
534 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
535 extended partition */
536 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
537 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
538 partition < 1024 cyl. */
539 "\xf1" "SpeedStor",
540 "\xf4" "SpeedStor", /* SpeedStor large partition */
541 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
542 "\xff" "BBT", /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000543#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000544 NULL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000545};
546
547
Denis Vlasenko834410a2006-11-29 12:00:28 +0000548#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000549/* start_sect and nr_sects are stored little endian on all machines */
550/* moreover, they are not aligned correctly */
551static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000552store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000553{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000554 cp[0] = val;
555 cp[1] = val >> 8;
556 cp[2] = val >> 16;
557 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000559#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000560
Denis Vlasenko834410a2006-11-29 12:00:28 +0000561static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000562read4_little_endian(const unsigned char *cp)
563{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000564 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000565}
566
Denis Vlasenko834410a2006-11-29 12:00:28 +0000567#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000568static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000569set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000570{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000571 store4_little_endian(p->start4, start_sect);
572}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000573#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000574
Denis Vlasenko28703012006-12-19 20:32:02 +0000575static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000576get_start_sect(const struct partition *p)
577{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000578 return read4_little_endian(p->start4);
579}
580
Denis Vlasenko834410a2006-11-29 12:00:28 +0000581#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000582static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000583set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000584{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000585 store4_little_endian(p->size4, nr_sects);
586}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000587#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588
Denis Vlasenko28703012006-12-19 20:32:02 +0000589static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000590get_nr_sects(const struct partition *p)
591{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000592 return read4_little_endian(p->size4);
593}
594
595/* normally O_RDWR, -l option gives O_RDONLY */
596static int type_open = O_RDWR;
597
598
Rob Landleyb73451d2006-02-24 16:29:00 +0000599static int ext_index; /* the prime extended partition */
600static int listing; /* no aborts for fdisk -l */
601static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000602#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000603static int dos_changed;
604static int nowarn; /* no warnings for fdisk -l/-s */
605#endif
606
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000607
608
Denis Vlasenko834410a2006-11-29 12:00:28 +0000609static unsigned user_cylinders, user_heads, user_sectors;
610static unsigned pt_heads, pt_sectors;
611static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000612
Eric Andersend9261492004-06-28 23:50:31 +0000613static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000614
Eric Andersen040f4402003-07-30 08:40:37 +0000615static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000616
617
618static jmp_buf listingbuf;
619
Rob Landleyb73451d2006-02-24 16:29:00 +0000620static void fdisk_fatal(enum failure why)
621{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000622 const char *message;
623
624 if (listing) {
625 close(fd);
626 longjmp(listingbuf, 1);
627 }
628
629 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000630 case unable_to_open:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000631 message = "cannot open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000632 break;
633 case unable_to_read:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000634 message = "cannot read from %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000635 break;
636 case unable_to_seek:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000637 message = "cannot seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000638 break;
639 case unable_to_write:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000640 message = "cannot write to %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000641 break;
642 case ioctl_error:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000643 message = "BLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000644 break;
645 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000646 message = "fatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000647 }
648
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000649 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000650}
651
652static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000653seek_sector(off_t secno)
654{
Eric Andersen0a92f352004-03-30 09:21:54 +0000655 off_t offset = secno * sector_size;
656 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657 fdisk_fatal(unable_to_seek);
658}
659
Denis Vlasenko834410a2006-11-29 12:00:28 +0000660#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000661static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000662write_sector(off_t secno, char *buf)
663{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000664 seek_sector(secno);
665 if (write(fd, buf, sector_size) != sector_size)
666 fdisk_fatal(unable_to_write);
667}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000668#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000669
670/* Allocate a buffer and read a partition table sector */
671static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000672read_pte(struct pte *pe, off_t offset)
673{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000675 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000676 seek_sector(offset);
677 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
678 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000679#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000680 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000681#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000682 pe->part_table = pe->ext_pointer = NULL;
683}
684
Denis Vlasenko834410a2006-11-29 12:00:28 +0000685static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000686get_partition_start(const struct pte *pe)
687{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000688 return pe->offset + get_start_sect(pe->part_table);
689}
690
Denis Vlasenko834410a2006-11-29 12:00:28 +0000691#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692/*
693 * Avoid warning about DOS partitions when no DOS partition was changed.
694 * Here a heuristic "is probably dos partition".
695 * We might also do the opposite and warn in all cases except
696 * for "is probably nondos partition".
697 */
698static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000699is_dos_partition(int t)
700{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000701 return (t == 1 || t == 4 || t == 6 ||
702 t == 0x0b || t == 0x0c || t == 0x0e ||
703 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
704 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
705 t == 0xc1 || t == 0xc4 || t == 0xc6);
706}
707
708static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000709menu(void)
710{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000711 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000712 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000713 puts("a\ttoggle a read only flag"); /* sun */
714 puts("b\tedit bsd disklabel");
715 puts("c\ttoggle the mountable flag"); /* sun */
716 puts("d\tdelete a partition");
717 puts("l\tlist known partition types");
718 puts("n\tadd a new partition");
719 puts("o\tcreate a new empty DOS partition table");
720 puts("p\tprint the partition table");
721 puts("q\tquit without saving changes");
722 puts("s\tcreate a new empty Sun disklabel"); /* sun */
723 puts("t\tchange a partition's system id");
724 puts("u\tchange display/entry units");
725 puts("v\tverify the partition table");
726 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000727#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000728 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000729#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000730 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000731 puts("a\tselect bootable partition"); /* sgi flavour */
732 puts("b\tedit bootfile entry"); /* sgi */
733 puts("c\tselect sgi swap partition"); /* sgi flavour */
734 puts("d\tdelete a partition");
735 puts("l\tlist known partition types");
736 puts("n\tadd a new partition");
737 puts("o\tcreate a new empty DOS partition table");
738 puts("p\tprint the partition table");
739 puts("q\tquit without saving changes");
740 puts("s\tcreate a new empty Sun disklabel"); /* sun */
741 puts("t\tchange a partition's system id");
742 puts("u\tchange display/entry units");
743 puts("v\tverify the partition table");
744 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000745 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000746 puts("o\tcreate a new empty DOS partition table");
747 puts("q\tquit without saving changes");
748 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000749 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000750 puts("a\ttoggle a bootable flag");
751 puts("b\tedit bsd disklabel");
752 puts("c\ttoggle the dos compatibility flag");
753 puts("d\tdelete a partition");
754 puts("l\tlist known partition types");
755 puts("n\tadd a new partition");
756 puts("o\tcreate a new empty DOS partition table");
757 puts("p\tprint the partition table");
758 puts("q\tquit without saving changes");
759 puts("s\tcreate a new empty Sun disklabel"); /* sun */
760 puts("t\tchange a partition's system id");
761 puts("u\tchange display/entry units");
762 puts("v\tverify the partition table");
763 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000764#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000765 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000766#endif
767 }
768}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000769#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000770
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000771
Denis Vlasenko834410a2006-11-29 12:00:28 +0000772#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000773static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000774xmenu(void)
775{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000776 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000777 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000778 puts("a\tchange number of alternate cylinders"); /*sun*/
779 puts("c\tchange number of cylinders");
780 puts("d\tprint the raw data in the partition table");
781 puts("e\tchange number of extra sectors per cylinder");/*sun*/
782 puts("h\tchange number of heads");
783 puts("i\tchange interleave factor"); /*sun*/
784 puts("o\tchange rotation speed (rpm)"); /*sun*/
785 puts("p\tprint the partition table");
786 puts("q\tquit without saving changes");
787 puts("r\treturn to main menu");
788 puts("s\tchange number of sectors/track");
789 puts("v\tverify the partition table");
790 puts("w\twrite table to disk and exit");
791 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000792 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000793 puts("b\tmove beginning of data in a partition"); /* !sun */
794 puts("c\tchange number of cylinders");
795 puts("d\tprint the raw data in the partition table");
796 puts("e\tlist extended partitions"); /* !sun */
797 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
798 puts("h\tchange number of heads");
799 puts("p\tprint the partition table");
800 puts("q\tquit without saving changes");
801 puts("r\treturn to main menu");
802 puts("s\tchange number of sectors/track");
803 puts("v\tverify the partition table");
804 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000805 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000806 puts("b\tmove beginning of data in a partition"); /* !sun */
807 puts("c\tchange number of cylinders");
808 puts("d\tprint the raw data in the partition table");
809 puts("e\tlist extended partitions"); /* !sun */
810 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
811 puts("h\tchange number of heads");
812 puts("p\tprint the partition table");
813 puts("q\tquit without saving changes");
814 puts("r\treturn to main menu");
815 puts("s\tchange number of sectors/track");
816 puts("v\tverify the partition table");
817 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000818 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000819 puts("b\tmove beginning of data in a partition"); /* !sun */
820 puts("c\tchange number of cylinders");
821 puts("d\tprint the raw data in the partition table");
822 puts("e\tlist extended partitions"); /* !sun */
823 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000824#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000825 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000826#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000827 puts("h\tchange number of heads");
828 puts("p\tprint the partition table");
829 puts("q\tquit without saving changes");
830 puts("r\treturn to main menu");
831 puts("s\tchange number of sectors/track");
832 puts("v\tverify the partition table");
833 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000834 }
835}
836#endif /* ADVANCED mode */
837
Denis Vlasenko834410a2006-11-29 12:00:28 +0000838#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000839static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000840get_sys_types(void)
841{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000842 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000843 LABEL_IS_SUN ? sun_sys_types :
844 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000845 i386_sys_types);
846}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000847#else
848#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000849#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000850
Denis Vlasenkobd852072007-03-19 14:43:38 +0000851static const char *
852partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000853{
854 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000855 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000856
Denis Vlasenkobd852072007-03-19 14:43:38 +0000857 for (i = 0; types[i]; i++)
858 if ((unsigned char)types[i][0] == type)
859 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860
Denis Vlasenkobd852072007-03-19 14:43:38 +0000861 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000862}
863
864
Denis Vlasenko834410a2006-11-29 12:00:28 +0000865#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000866static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000867get_sysid(int i)
868{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000869 return LABEL_IS_SUN ? sunlabel->infos[i].id :
870 (LABEL_IS_SGI ? sgi_get_sysid(i) :
871 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000872}
873
Denis Vlasenkobd852072007-03-19 14:43:38 +0000874static void
875list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000876{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000877 enum { COLS = 3 };
878
879 unsigned last[COLS];
880 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000881 int i;
882
Denis Vlasenkobd852072007-03-19 14:43:38 +0000883 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000884
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000885 done = 0;
886 for (i = COLS-1; i >= 0; i--) {
887 done += (size + i - done) / (i + 1);
888 last[COLS-1 - i] = done;
889 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000890
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000891 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000892 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000893 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000894 (unsigned char)sys[next][0],
895 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000896 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000897 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000898 i = 0;
899 next = ++done;
900 }
901 } while (done < last[0]);
902 putchar('\n');
903}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000904#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905
906static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000907is_cleared_partition(const struct partition *p)
908{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000909 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
910 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
911 get_start_sect(p) || get_nr_sects(p));
912}
913
914static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000915clear_partition(struct partition *p)
916{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000917 if (!p)
918 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000919 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000920}
921
Denis Vlasenko834410a2006-11-29 12:00:28 +0000922#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000923static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000924set_partition(int i, int doext, off_t start, off_t stop, int sysid)
925{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000926 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000927 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000928
929 if (doext) {
930 p = ptes[i].ext_pointer;
931 offset = extended_offset;
932 } else {
933 p = ptes[i].part_table;
934 offset = ptes[i].offset;
935 }
936 p->boot_ind = 0;
937 p->sys_ind = sysid;
938 set_start_sect(p, start - offset);
939 set_nr_sects(p, stop - start + 1);
940 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
941 start = heads*sectors*1024 - 1;
942 set_hsc(p->head, p->sector, p->cyl, start);
943 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
944 stop = heads*sectors*1024 - 1;
945 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
946 ptes[i].changed = 1;
947}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000948#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949
950static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000951warn_geometry(void)
952{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000953 if (heads && sectors && cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000954 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000955
Denis Vlasenkobd852072007-03-19 14:43:38 +0000956 printf("Unknown value(s) for:");
957 if (!heads)
958 printf(" heads");
959 if (!sectors)
960 printf(" sectors");
961 if (!cylinders)
962 printf(" cylinders");
963 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000964#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000965 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000966#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000967 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968 return 1;
969}
970
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000971static void
972update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973{
974 int cyl_units = heads * sectors;
975
976 if (display_in_cyl_units && cyl_units)
977 units_per_sector = cyl_units;
978 else
979 units_per_sector = 1; /* in sectors */
980}
981
Denis Vlasenko834410a2006-11-29 12:00:28 +0000982#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000983static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000984warn_cylinders(void)
985{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000986 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000987 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000988"The number of cylinders for this disk is set to %d.\n"
989"There is nothing wrong with that, but this is larger than 1024,\n"
990"and could in certain setups cause problems with:\n"
991"1) software that runs at boot time (e.g., old versions of LILO)\n"
992"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +0000993" (e.g., DOS FDISK, OS/2 FDISK)\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994 cylinders);
995}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000996#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000997
998static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000999read_extended(int ext)
1000{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001001 int i;
1002 struct pte *pex;
1003 struct partition *p, *q;
1004
1005 ext_index = ext;
1006 pex = &ptes[ext];
1007 pex->ext_pointer = pex->part_table;
1008
1009 p = pex->part_table;
1010 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001011 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001012 return;
1013 }
1014
Rob Landleyb73451d2006-02-24 16:29:00 +00001015 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001016 struct pte *pe = &ptes[partitions];
1017
1018 if (partitions >= MAXIMUM_PARTS) {
1019 /* This is not a Linux restriction, but
1020 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001021 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001022 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001023#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001024 printf("Warning: deleting partitions after %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001025 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001027#endif
1028 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001029 return;
1030 }
1031
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001032 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001033
1034 if (!extended_offset)
1035 extended_offset = get_start_sect(p);
1036
1037 q = p = pt_offset(pe->sectorbuffer, 0);
1038 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001039 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001040 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001041 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001042 "pointer in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001043 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001044 else
1045 pe->ext_pointer = p;
1046 } else if (p->sys_ind) {
1047 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001049 "data in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001050 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 else
1052 pe->part_table = p;
1053 }
1054 }
1055
1056 /* very strange code here... */
1057 if (!pe->part_table) {
1058 if (q != pe->ext_pointer)
1059 pe->part_table = q;
1060 else
1061 pe->part_table = q + 1;
1062 }
1063 if (!pe->ext_pointer) {
1064 if (q != pe->part_table)
1065 pe->ext_pointer = q;
1066 else
1067 pe->ext_pointer = q + 1;
1068 }
1069
1070 p = pe->ext_pointer;
1071 partitions++;
1072 }
1073
Denis Vlasenko834410a2006-11-29 12:00:28 +00001074#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 /* remove empty links */
1076 remove:
1077 for (i = 4; i < partitions; i++) {
1078 struct pte *pe = &ptes[i];
1079
Denis Vlasenkobd852072007-03-19 14:43:38 +00001080 if (!get_nr_sects(pe->part_table)
1081 && (partitions > 5 || ptes[4].part_table->sys_ind)
1082 ) {
1083 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001084 delete_partition(i);
1085 goto remove; /* numbering changed */
1086 }
1087 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001088#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001089}
1090
Denis Vlasenko834410a2006-11-29 12:00:28 +00001091#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001092static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001093create_doslabel(void)
1094{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001095 int i;
1096
Denis Vlasenkobd852072007-03-19 14:43:38 +00001097 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001098
1099 current_label_type = label_dos;
1100
Denis Vlasenko834410a2006-11-29 12:00:28 +00001101#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001102 possibly_osf_label = 0;
1103#endif
1104 partitions = 4;
1105
1106 for (i = 510-64; i < 510; i++)
1107 MBRbuffer[i] = 0;
1108 write_part_table_flag(MBRbuffer);
1109 extended_offset = 0;
1110 set_all_unchanged();
1111 set_changed(0);
1112 get_boot(create_empty_dos);
1113}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001114#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115
1116static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001117get_sectorsize(void)
1118{
Rob Landley736e5252006-02-25 03:36:00 +00001119 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001120 int arg;
1121 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1122 sector_size = arg;
1123 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001124 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001125 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001126 }
1127}
1128
Rob Landley88621d72006-08-29 19:41:06 +00001129static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001130get_kernel_geometry(void)
1131{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 struct hd_geometry geometry;
1133
1134 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1135 kern_heads = geometry.heads;
1136 kern_sectors = geometry.sectors;
1137 /* never use geometry.cylinders - it is truncated */
1138 }
1139}
1140
1141static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001142get_partition_table_geometry(void)
1143{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001144 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001145 struct partition *p;
1146 int i, h, s, hh, ss;
1147 int first = 1;
1148 int bad = 0;
1149
Eric Andersen3496fdc2006-01-30 23:09:20 +00001150 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001151 return;
1152
1153 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001154 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001155 p = pt_offset(bufp, i);
1156 if (p->sys_ind != 0) {
1157 h = p->end_head + 1;
1158 s = (p->end_sector & 077);
1159 if (first) {
1160 hh = h;
1161 ss = s;
1162 first = 0;
1163 } else if (hh != h || ss != s)
1164 bad = 1;
1165 }
1166 }
1167
1168 if (!first && !bad) {
1169 pt_heads = hh;
1170 pt_sectors = ss;
1171 }
1172}
1173
Rob Landleyb73451d2006-02-24 16:29:00 +00001174static void
1175get_geometry(void)
1176{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001177 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001178 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001179
1180 get_sectorsize();
1181 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001182#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001183 guess_device_type();
1184#endif
1185 heads = cylinders = sectors = 0;
1186 kern_heads = kern_sectors = 0;
1187 pt_heads = pt_sectors = 0;
1188
1189 get_kernel_geometry();
1190 get_partition_table_geometry();
1191
1192 heads = user_heads ? user_heads :
1193 pt_heads ? pt_heads :
1194 kern_heads ? kern_heads : 255;
1195 sectors = user_sectors ? user_sectors :
1196 pt_sectors ? pt_sectors :
1197 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001198 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1199 /* got bytes */
1200 } else {
1201 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001202
1203 if (ioctl(fd, BLKGETSIZE, &longsectors))
1204 longsectors = 0;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001205 bytes = ((unsigned long long) longsectors) << 9;
Eric Andersen040f4402003-07-30 08:40:37 +00001206 }
1207
1208 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001209
1210 sector_offset = 1;
1211 if (dos_compatible_flag)
1212 sector_offset = sectors;
1213
Eric Andersen040f4402003-07-30 08:40:37 +00001214 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215 if (!cylinders)
1216 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001217}
1218
1219/*
1220 * Read MBR. Returns:
1221 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1222 * 0: found or created label
1223 * 1: I/O error
1224 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001225static int
1226get_boot(enum action what)
1227{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001228 int i;
1229
1230 partitions = 4;
1231
1232 for (i = 0; i < 4; i++) {
1233 struct pte *pe = &ptes[i];
1234
1235 pe->part_table = pt_offset(MBRbuffer, i);
1236 pe->ext_pointer = NULL;
1237 pe->offset = 0;
1238 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001239#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001240 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001241#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001242 }
1243
Denis Vlasenko834410a2006-11-29 12:00:28 +00001244#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245 if (what == create_empty_sun && check_sun_label())
1246 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001247#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001248
1249 memset(MBRbuffer, 0, 512);
1250
Denis Vlasenko834410a2006-11-29 12:00:28 +00001251#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 if (what == create_empty_dos)
1253 goto got_dos_table; /* skip reading disk */
1254
Denis Vlasenkobd852072007-03-19 14:43:38 +00001255 fd = open(disk_device, type_open);
1256 if (fd < 0) {
1257 fd = open(disk_device, O_RDONLY);
1258 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001259 if (what == try_only)
1260 return 1;
1261 fdisk_fatal(unable_to_open);
1262 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001263 printf("You will not be able to write "
1264 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001265 }
1266
1267 if (512 != read(fd, MBRbuffer, 512)) {
1268 if (what == try_only)
1269 return 1;
1270 fdisk_fatal(unable_to_read);
1271 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001272#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001273 fd = open(disk_device, O_RDONLY);
1274 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001275 return 1;
1276 if (512 != read(fd, MBRbuffer, 512))
1277 return 1;
1278#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001280 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281
1282 update_units();
1283
Denis Vlasenko834410a2006-11-29 12:00:28 +00001284#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001285 if (check_sun_label())
1286 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001287#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288
Denis Vlasenko834410a2006-11-29 12:00:28 +00001289#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 if (check_sgi_label())
1291 return 0;
1292#endif
1293
Denis Vlasenko834410a2006-11-29 12:00:28 +00001294#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 if (check_aix_label())
1296 return 0;
1297#endif
1298
Denis Vlasenko834410a2006-11-29 12:00:28 +00001299#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 if (check_osf_label()) {
1301 possibly_osf_label = 1;
1302 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001303 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001304 return 0;
1305 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001306 printf("This disk has both DOS and BSD magic.\n"
1307 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001308 }
1309#endif
1310
Denis Vlasenko834410a2006-11-29 12:00:28 +00001311#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001312 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001313#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001314
1315 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001316#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001317 return -1;
1318#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001319 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001321 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001323 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001324#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001325#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001326 create_sunlabel();
1327#endif
1328#else
1329 create_doslabel();
1330#endif
1331 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001332 case try_only:
1333 return -1;
1334 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001335#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001336 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001337#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001338 break;
1339 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001340 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001342#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 }
1344
Denis Vlasenko834410a2006-11-29 12:00:28 +00001345#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001347#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348 warn_geometry();
1349
1350 for (i = 0; i < 4; i++) {
1351 struct pte *pe = &ptes[i];
1352
Rob Landleyb73451d2006-02-24 16:29:00 +00001353 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001354 if (partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001355 printf("Ignoring extra extended "
1356 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001357 else
1358 read_extended(i);
1359 }
1360 }
1361
1362 for (i = 3; i < partitions; i++) {
1363 struct pte *pe = &ptes[i];
1364
1365 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001366 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1367 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001368 pe->sectorbuffer[510],
1369 pe->sectorbuffer[511],
1370 i + 1);
1371#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001373#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001374 }
1375 }
1376
1377 return 0;
1378}
1379
Denis Vlasenko834410a2006-11-29 12:00:28 +00001380#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001381/*
1382 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1383 * If the user hits Enter, DFLT is returned.
1384 * Answers like +10 are interpreted as offsets from BASE.
1385 *
1386 * There is no default if DFLT is not between LOW and HIGH.
1387 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001388static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001389read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001391 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001392 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001393 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001394
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001395 if (dflt < low || dflt > high) {
1396 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001398 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399
1400 while (1) {
1401 int use_default = default_ok;
1402
1403 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001404 do {
1405 printf(fmt, mesg, low, high, dflt);
1406 read_maybe_empty("");
1407 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1408 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409
Eric Andersen84bdea82004-05-19 10:49:17 +00001410 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001411 int minus = (*line_ptr == '-');
1412 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001413
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001414 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001415
Rob Landleyb73451d2006-02-24 16:29:00 +00001416 while (isdigit(*++line_ptr))
1417 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001418
Rob Landleyb73451d2006-02-24 16:29:00 +00001419 switch (*line_ptr) {
1420 case 'c':
1421 case 'C':
1422 if (!display_in_cyl_units)
1423 i *= heads * sectors;
1424 break;
1425 case 'K':
1426 absolute = 1024;
1427 break;
1428 case 'k':
1429 absolute = 1000;
1430 break;
1431 case 'm':
1432 case 'M':
1433 absolute = 1000000;
1434 break;
1435 case 'g':
1436 case 'G':
1437 absolute = 1000000000;
1438 break;
1439 default:
1440 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001441 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001442 if (absolute) {
1443 unsigned long long bytes;
1444 unsigned long unit;
1445
1446 bytes = (unsigned long long) i * absolute;
1447 unit = sector_size * units_per_sector;
1448 bytes += unit/2; /* round */
1449 bytes /= unit;
1450 i = bytes;
1451 }
1452 if (minus)
1453 i = -i;
1454 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001455 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001456 i = atoi(line_ptr);
1457 while (isdigit(*line_ptr)) {
1458 line_ptr++;
1459 use_default = 0;
1460 }
1461 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001462 if (use_default) {
1463 i = dflt;
1464 printf("Using default value %u\n", i);
1465 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001466 if (i >= low && i <= high)
1467 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001468 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001469 }
1470 return i;
1471}
1472
Rob Landleyb73451d2006-02-24 16:29:00 +00001473static int
1474get_partition(int warn, int max)
1475{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001476 struct pte *pe;
1477 int i;
1478
Denis Vlasenkobd852072007-03-19 14:43:38 +00001479 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001480 pe = &ptes[i];
1481
1482 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001483 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1484 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1485 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1486 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001487 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001488 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 }
1490 return i;
1491}
1492
1493static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001494get_existing_partition(int warn, int max)
1495{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001496 int pno = -1;
1497 int i;
1498
1499 for (i = 0; i < max; i++) {
1500 struct pte *pe = &ptes[i];
1501 struct partition *p = pe->part_table;
1502
1503 if (p && !is_cleared_partition(p)) {
1504 if (pno >= 0)
1505 goto not_unique;
1506 pno = i;
1507 }
1508 }
1509 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001510 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001511 return pno;
1512 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001513 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001514 return -1;
1515
1516 not_unique:
1517 return get_partition(warn, max);
1518}
1519
1520static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001521get_nonexisting_partition(int warn, int max)
1522{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001523 int pno = -1;
1524 int i;
1525
1526 for (i = 0; i < max; i++) {
1527 struct pte *pe = &ptes[i];
1528 struct partition *p = pe->part_table;
1529
1530 if (p && is_cleared_partition(p)) {
1531 if (pno >= 0)
1532 goto not_unique;
1533 pno = i;
1534 }
1535 }
1536 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001537 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001538 return pno;
1539 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001540 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001541 return -1;
1542
1543 not_unique:
1544 return get_partition(warn, max);
1545}
1546
1547
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001548static void
1549change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001550{
1551 display_in_cyl_units = !display_in_cyl_units;
1552 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001553 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554 str_units(PLURAL));
1555}
1556
1557static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001558toggle_active(int i)
1559{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001560 struct pte *pe = &ptes[i];
1561 struct partition *p = pe->part_table;
1562
Rob Landleyb73451d2006-02-24 16:29:00 +00001563 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001564 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001565 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1566 pe->changed = 1;
1567}
1568
1569static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001570toggle_dos_compatibility_flag(void)
1571{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001572 dos_compatible_flag = ~dos_compatible_flag;
1573 if (dos_compatible_flag) {
1574 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001575 printf("DOS Compatibility flag is set\n");
1576 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001578 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001579 }
1580}
1581
1582static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001583delete_partition(int i)
1584{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585 struct pte *pe = &ptes[i];
1586 struct partition *p = pe->part_table;
1587 struct partition *q = pe->ext_pointer;
1588
1589/* Note that for the fifth partition (i == 4) we don't actually
1590 * decrement partitions.
1591 */
1592
1593 if (warn_geometry())
1594 return; /* C/H/S not set */
1595 pe->changed = 1;
1596
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001597 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598 sun_delete_partition(i);
1599 return;
1600 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001601 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001602 sgi_delete_partition(i);
1603 return;
1604 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001605
1606 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001607 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001608 partitions = 4;
1609 ptes[ext_index].ext_pointer = NULL;
1610 extended_offset = 0;
1611 }
1612 clear_partition(p);
1613 return;
1614 }
1615
1616 if (!q->sys_ind && i > 4) {
1617 /* the last one in the chain - just delete */
1618 --partitions;
1619 --i;
1620 clear_partition(ptes[i].ext_pointer);
1621 ptes[i].changed = 1;
1622 } else {
1623 /* not the last one - further ones will be moved down */
1624 if (i > 4) {
1625 /* delete this link in the chain */
1626 p = ptes[i-1].ext_pointer;
1627 *p = *q;
1628 set_start_sect(p, get_start_sect(q));
1629 set_nr_sects(p, get_nr_sects(q));
1630 ptes[i-1].changed = 1;
1631 } else if (partitions > 5) { /* 5 will be moved to 4 */
1632 /* the first logical in a longer chain */
1633 pe = &ptes[5];
1634
1635 if (pe->part_table) /* prevent SEGFAULT */
1636 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001637 get_partition_start(pe) -
1638 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001639 pe->offset = extended_offset;
1640 pe->changed = 1;
1641 }
1642
1643 if (partitions > 5) {
1644 partitions--;
1645 while (i < partitions) {
1646 ptes[i] = ptes[i+1];
1647 i++;
1648 }
1649 } else
1650 /* the only logical: clear only */
1651 clear_partition(ptes[i].part_table);
1652 }
1653}
1654
1655static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001656change_sysid(void)
1657{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001658 int i, sys, origsys;
1659 struct partition *p;
1660
Eric Andersen040f4402003-07-30 08:40:37 +00001661 /* If sgi_label then don't use get_existing_partition,
1662 let the user select a partition, since get_existing_partition()
1663 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001664 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001665 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001666 } else {
1667 i = get_partition(0, partitions);
1668 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001669 if (i == -1)
1670 return;
1671 p = ptes[i].part_table;
1672 origsys = sys = get_sysid(i);
1673
1674 /* if changing types T to 0 is allowed, then
1675 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001676 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001677 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001678 return;
1679 }
1680 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001681 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001682
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001683 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001684 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001685 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001686 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687 /* break; */
1688 }
1689
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001690 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001691 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001692 printf("You cannot change a partition into"
1693 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001694 break;
1695 }
1696 }
1697
1698 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001699#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001700 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001701 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001702 "as Whole disk (5),\n"
1703 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001704 "even Linux likes it\n\n");
1705#endif
1706#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001707 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001708 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001709 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001710 (i == 8 && sys != 0)
1711 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001712 ) {
1713 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001714 "as volume header (0),\nand "
1715 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001716 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001717 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001718#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001719 if (sys == origsys)
1720 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001721 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001722 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001723 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001724 sgi_change_sysid(i, sys);
1725 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001726 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001727
Denis Vlasenkobd852072007-03-19 14:43:38 +00001728 printf("Changed system type of partition %d "
1729 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001730 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731 ptes[i].changed = 1;
1732 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001733 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001734 dos_changed = 1;
1735 break;
1736 }
1737 }
1738}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001739#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001740
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741
Denis Vlasenko28703012006-12-19 20:32:02 +00001742/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001743 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1744 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1745 * Lubkin Oct. 1991). */
1746
Rob Landleyb73451d2006-02-24 16:29:00 +00001747static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001748linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001749{
1750 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001751
1752 *c = ls / spc;
1753 ls = ls % spc;
1754 *h = ls / sectors;
1755 *s = ls % sectors + 1; /* sectors count from 1 */
1756}
1757
Rob Landleyb73451d2006-02-24 16:29:00 +00001758static void
1759check_consistency(const struct partition *p, int partition)
1760{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001761 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1762 unsigned pec, peh, pes; /* physical ending c, h, s */
1763 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1764 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001765
1766 if (!heads || !sectors || (partition >= 4))
1767 return; /* do not check extended partitions */
1768
1769/* physical beginning c, h, s */
1770 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1771 pbh = p->head;
1772 pbs = p->sector & 0x3f;
1773
1774/* physical ending c, h, s */
1775 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1776 peh = p->end_head;
1777 pes = p->end_sector & 0x3f;
1778
1779/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001780 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001781
1782/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001783 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001784
1785/* Same physical / logical beginning? */
1786 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001787 printf("Partition %d has different physical/logical "
1788 "beginnings (non-Linux?):\n", partition + 1);
1789 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1790 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001791 }
1792
1793/* Same physical / logical ending? */
1794 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001795 printf("Partition %d has different physical/logical "
1796 "endings:\n", partition + 1);
1797 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1798 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001799 }
1800
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801/* Ending on cylinder boundary? */
1802 if (peh != (heads - 1) || pes != sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001803 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 }
1806}
1807
1808static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001809list_disk_geometry(void)
1810{
Eric Andersen040f4402003-07-30 08:40:37 +00001811 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001812 long megabytes = bytes/1000000;
1813
1814 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001815 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001816 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001817 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001818 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001819 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001820 printf("%d heads, %d sectors/track, %d cylinders",
Rob Landleyb73451d2006-02-24 16:29:00 +00001821 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001822 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001823 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001824 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001825 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001826 str_units(PLURAL),
1827 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001828}
1829
1830/*
1831 * Check whether partition entries are ordered by their starting positions.
1832 * Return 0 if OK. Return i if partition i should have been earlier.
1833 * Two separate checks: primary and logical partitions.
1834 */
1835static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001836wrong_p_order(int *prev)
1837{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001838 const struct pte *pe;
1839 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001840 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001841 int i, last_i = 0;
1842
1843 for (i = 0 ; i < partitions; i++) {
1844 if (i == 4) {
1845 last_i = 4;
1846 last_p_start_pos = 0;
1847 }
1848 pe = &ptes[i];
1849 if ((p = pe->part_table)->sys_ind) {
1850 p_start_pos = get_partition_start(pe);
1851
1852 if (last_p_start_pos > p_start_pos) {
1853 if (prev)
1854 *prev = last_i;
1855 return i;
1856 }
1857
1858 last_p_start_pos = p_start_pos;
1859 last_i = i;
1860 }
1861 }
1862 return 0;
1863}
1864
Denis Vlasenko834410a2006-11-29 12:00:28 +00001865#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001866/*
1867 * Fix the chain of logicals.
1868 * extended_offset is unchanged, the set of sectors used is unchanged
1869 * The chain is sorted so that sectors increase, and so that
1870 * starting sectors increase.
1871 *
1872 * After this it may still be that cfdisk doesnt like the table.
1873 * (This is because cfdisk considers expanded parts, from link to
1874 * end of partition, and these may still overlap.)
1875 * Now
1876 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1877 * may help.
1878 */
1879static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001880fix_chain_of_logicals(void)
1881{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001882 int j, oj, ojj, sj, sjj;
1883 struct partition *pj,*pjj,tmp;
1884
1885 /* Stage 1: sort sectors but leave sector of part 4 */
1886 /* (Its sector is the global extended_offset.) */
1887 stage1:
1888 for (j = 5; j < partitions-1; j++) {
1889 oj = ptes[j].offset;
1890 ojj = ptes[j+1].offset;
1891 if (oj > ojj) {
1892 ptes[j].offset = ojj;
1893 ptes[j+1].offset = oj;
1894 pj = ptes[j].part_table;
1895 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1896 pjj = ptes[j+1].part_table;
1897 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1898 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001899 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001900 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001901 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001902 goto stage1;
1903 }
1904 }
1905
1906 /* Stage 2: sort starting sectors */
1907 stage2:
1908 for (j = 4; j < partitions-1; j++) {
1909 pj = ptes[j].part_table;
1910 pjj = ptes[j+1].part_table;
1911 sj = get_start_sect(pj);
1912 sjj = get_start_sect(pjj);
1913 oj = ptes[j].offset;
1914 ojj = ptes[j+1].offset;
1915 if (oj+sj > ojj+sjj) {
1916 tmp = *pj;
1917 *pj = *pjj;
1918 *pjj = tmp;
1919 set_start_sect(pj, ojj+sjj-oj);
1920 set_start_sect(pjj, oj+sj-ojj);
1921 goto stage2;
1922 }
1923 }
1924
1925 /* Probably something was changed */
1926 for (j = 4; j < partitions; j++)
1927 ptes[j].changed = 1;
1928}
1929
1930
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001931static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001932fix_partition_table_order(void)
1933{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001934 struct pte *pei, *pek;
1935 int i,k;
1936
1937 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001938 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001939 return;
1940 }
1941
1942 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1943 /* partition i should have come earlier, move it */
1944 /* We have to move data in the MBR */
1945 struct partition *pi, *pk, *pe, pbuf;
1946 pei = &ptes[i];
1947 pek = &ptes[k];
1948
1949 pe = pei->ext_pointer;
1950 pei->ext_pointer = pek->ext_pointer;
1951 pek->ext_pointer = pe;
1952
1953 pi = pei->part_table;
1954 pk = pek->part_table;
1955
1956 memmove(&pbuf, pi, sizeof(struct partition));
1957 memmove(pi, pk, sizeof(struct partition));
1958 memmove(pk, &pbuf, sizeof(struct partition));
1959
1960 pei->changed = pek->changed = 1;
1961 }
1962
1963 if (i)
1964 fix_chain_of_logicals();
1965
1966 printf("Done.\n");
1967
1968}
1969#endif
1970
1971static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001972list_table(int xtra)
1973{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001974 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001975 int i, w;
1976
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001977 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001978 sun_list_table(xtra);
1979 return;
1980 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001981 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001982 sgi_list_table(xtra);
1983 return;
1984 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001985
1986 list_disk_geometry();
1987
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001988 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001989 xbsd_print_disklabel(xtra);
1990 return;
1991 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001992
1993 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
1994 but if the device name ends in a digit, say /dev/foo1,
1995 then the partition is called /dev/foo1p3. */
1996 w = strlen(disk_device);
1997 if (w && isdigit(disk_device[w-1]))
1998 w++;
1999 if (w < 5)
2000 w = 5;
2001
Denis Vlasenkobd852072007-03-19 14:43:38 +00002002 // 1 12345678901 12345678901 12345678901 12
2003 printf("%*s Boot Start End Blocks Id System\n",
2004 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005
2006 for (i = 0; i < partitions; i++) {
2007 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002008 off_t psects;
2009 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002010 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011
2012 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002013 if (!p || is_cleared_partition(p))
2014 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002015
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002016 psects = get_nr_sects(p);
2017 pblocks = psects;
2018 podd = 0;
2019
2020 if (sector_size < 1024) {
2021 pblocks /= (1024 / sector_size);
2022 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002023 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002024 if (sector_size > 1024)
2025 pblocks *= (sector_size / 1024);
2026
2027 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2028 partname(disk_device, i+1, w+2),
2029 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2030 ? '*' : '?',
2031 (unsigned long long) cround(get_partition_start(pe)), /* start */
2032 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2033 - (psects ? 1 : 0)),
2034 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2035 p->sys_ind, /* type id */
2036 partition_type(p->sys_ind)); /* type name */
2037
2038 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002039 }
2040
2041 /* Is partition table in disk order? It need not be, but... */
2042 /* partition table entries are not checked for correct order if this
2043 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002044 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002045 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002046 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002047 }
2048}
2049
Denis Vlasenko834410a2006-11-29 12:00:28 +00002050#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002051static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002052x_list_table(int extend)
2053{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002054 const struct pte *pe;
2055 const struct partition *p;
2056 int i;
2057
Denis Vlasenkobd852072007-03-19 14:43:38 +00002058 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002059 disk_device, heads, sectors, cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002060 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002061 for (i = 0 ; i < partitions; i++) {
2062 pe = &ptes[i];
2063 p = (extend ? pe->ext_pointer : pe->part_table);
2064 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002065 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002066 i + 1, p->boot_ind, p->head,
2067 sector(p->sector),
2068 cylinder(p->sector, p->cyl), p->end_head,
2069 sector(p->end_sector),
2070 cylinder(p->end_sector, p->end_cyl),
2071 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2072 if (p->sys_ind)
2073 check_consistency(p, i);
2074 }
2075 }
2076}
2077#endif
2078
Denis Vlasenko834410a2006-11-29 12:00:28 +00002079#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002080static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002081fill_bounds(off_t *first, off_t *last)
2082{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002083 int i;
2084 const struct pte *pe = &ptes[0];
2085 const struct partition *p;
2086
2087 for (i = 0; i < partitions; pe++,i++) {
2088 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002089 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090 first[i] = 0xffffffff;
2091 last[i] = 0;
2092 } else {
2093 first[i] = get_partition_start(pe);
2094 last[i] = first[i] + get_nr_sects(p) - 1;
2095 }
2096 }
2097}
2098
2099static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002100check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002101{
Eric Andersend9261492004-06-28 23:50:31 +00002102 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002103
2104 real_s = sector(s) - 1;
2105 real_c = cylinder(s, c);
2106 total = (real_c * sectors + real_s) * heads + h;
2107 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002108 printf("Partition %d contains sector 0\n", n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002109 if (h >= heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002110 printf("Partition %d: head %d greater than maximum %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111 n, h + 1, heads);
2112 if (real_s >= sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002113 printf("Partition %d: sector %d greater than "
2114 "maximum %d\n", n, s, sectors);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002115 if (real_c >= cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002116 printf("Partition %d: cylinder %"OFF_FMT"u greater than "
2117 "maximum %d\n", n, real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 if (cylinders <= 1024 && start != total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002119 printf("Partition %d: previous sectors %"OFF_FMT"u disagrees with "
2120 "total %"OFF_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121}
2122
2123static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002124verify(void)
2125{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002127 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002128 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002129 struct partition *p;
2130
2131 if (warn_geometry())
2132 return;
2133
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002134 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002135 verify_sun();
2136 return;
2137 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002138 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002139 verify_sgi(1);
2140 return;
2141 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142
2143 fill_bounds(first, last);
2144 for (i = 0; i < partitions; i++) {
2145 struct pte *pe = &ptes[i];
2146
2147 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002148 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149 check_consistency(p, i);
2150 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002151 printf("Warning: bad start-of-data in "
2152 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002153 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2154 last[i]);
2155 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002156 for (j = 0; j < i; j++) {
2157 if ((first[i] >= first[j] && first[i] <= last[j])
2158 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2159 printf("Warning: partition %d overlaps "
2160 "partition %d\n", j + 1, i + 1);
2161 total += first[i] >= first[j] ?
2162 first[i] : first[j];
2163 total -= last[i] <= last[j] ?
2164 last[i] : last[j];
2165 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002166 }
2167 }
2168 }
2169
2170 if (extended_offset) {
2171 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002172 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002173 get_nr_sects(pex->part_table) - 1;
2174
2175 for (i = 4; i < partitions; i++) {
2176 total++;
2177 p = ptes[i].part_table;
2178 if (!p->sys_ind) {
2179 if (i != 4 || i + 1 < partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002180 printf("Warning: partition %d "
2181 "is empty\n", i + 1);
2182 } else if (first[i] < extended_offset || last[i] > e_last) {
2183 printf("Logical partition %d not entirely in "
2184 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002185 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002186 }
2187 }
2188
2189 if (total > heads * sectors * cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002190 printf("Total allocated sectors %d greater than the maximum "
2191 "%d\n", total, heads * sectors * cylinders);
2192 else {
2193 total = heads * sectors * cylinders - total;
2194 if (total != 0)
2195 printf("%d unallocated sectors\n", total);
2196 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002197}
2198
2199static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002200add_partition(int n, int sys)
2201{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002203 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002204 struct partition *p = ptes[n].part_table;
2205 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002206 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002207 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 first[partitions], last[partitions];
2209
2210 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002211 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002212 return;
2213 }
2214 fill_bounds(first, last);
2215 if (n < 4) {
2216 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002217 if (display_in_cyl_units || !total_number_of_sectors)
2218 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219 else
Eric Andersen040f4402003-07-30 08:40:37 +00002220 llimit = total_number_of_sectors - 1;
2221 limit = llimit;
2222 if (limit != llimit)
2223 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002224 if (extended_offset) {
2225 first[ext_index] = extended_offset;
2226 last[ext_index] = get_start_sect(q) +
2227 get_nr_sects(q) - 1;
2228 }
2229 } else {
2230 start = extended_offset + sector_offset;
2231 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2232 }
2233 if (display_in_cyl_units)
2234 for (i = 0; i < partitions; i++)
2235 first[i] = (cround(first[i]) - 1) * units_per_sector;
2236
Denis Vlasenkobd852072007-03-19 14:43:38 +00002237 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002238 do {
2239 temp = start;
2240 for (i = 0; i < partitions; i++) {
2241 int lastplusoff;
2242
2243 if (start == ptes[i].offset)
2244 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002245 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002246 if (start >= first[i] && start <= lastplusoff)
2247 start = lastplusoff + 1;
2248 }
2249 if (start > limit)
2250 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002251 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002252 printf("Sector %"OFF_FMT"d is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002254 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002255 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002256 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002257 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258
2259 saved_start = start;
2260 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2261 0, mesg);
2262 if (display_in_cyl_units) {
2263 start = (start - 1) * units_per_sector;
2264 if (start < saved_start) start = saved_start;
2265 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002266 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002267 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002268 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002269 if (n > 4) { /* NOT for fifth partition */
2270 struct pte *pe = &ptes[n];
2271
2272 pe->offset = start - sector_offset;
2273 if (pe->offset == extended_offset) { /* must be corrected */
2274 pe->offset++;
2275 if (sector_offset == 1)
2276 start++;
2277 }
2278 }
2279
2280 for (i = 0; i < partitions; i++) {
2281 struct pte *pe = &ptes[i];
2282
2283 if (start < pe->offset && limit >= pe->offset)
2284 limit = pe->offset - 1;
2285 if (start < first[i] && limit >= first[i])
2286 limit = first[i] - 1;
2287 }
2288 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002289 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002290 if (n > 4)
2291 partitions--;
2292 return;
2293 }
2294 if (cround(start) == cround(limit)) {
2295 stop = limit;
2296 } else {
2297 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002298 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 str_units(SINGULAR));
2300 stop = read_int(cround(start), cround(limit), cround(limit),
2301 cround(start), mesg);
2302 if (display_in_cyl_units) {
2303 stop = stop * units_per_sector - 1;
2304 if (stop >limit)
2305 stop = limit;
2306 }
2307 }
2308
2309 set_partition(n, 0, start, stop, sys);
2310 if (n > 4)
2311 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2312
Rob Landleyb73451d2006-02-24 16:29:00 +00002313 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002314 struct pte *pe4 = &ptes[4];
2315 struct pte *pen = &ptes[n];
2316
2317 ext_index = n;
2318 pen->ext_pointer = p;
2319 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002320 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002321 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2322 pe4->ext_pointer = pe4->part_table + 1;
2323 pe4->changed = 1;
2324 partitions = 5;
2325 }
2326}
2327
2328static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002329add_logical(void)
2330{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002331 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2332 struct pte *pe = &ptes[partitions];
2333
Rob Landley081e3842006-08-03 20:07:35 +00002334 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2336 pe->ext_pointer = pe->part_table + 1;
2337 pe->offset = 0;
2338 pe->changed = 1;
2339 partitions++;
2340 }
2341 add_partition(partitions - 1, LINUX_NATIVE);
2342}
2343
2344static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002345new_partition(void)
2346{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002347 int i, free_primary = 0;
2348
2349 if (warn_geometry())
2350 return;
2351
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002352 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002353 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2354 return;
2355 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002356 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002357 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2358 return;
2359 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002360 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002361 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2362"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2363"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 return;
2365 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002366
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002367 for (i = 0; i < 4; i++)
2368 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002369
Rob Landleyb73451d2006-02-24 16:29:00 +00002370 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002371 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002372 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002373 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002374
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002375 if (!free_primary) {
2376 if (extended_offset)
2377 add_logical();
2378 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002379 printf("You must delete some partition and add "
2380 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 } else {
2382 char c, line[LINE_LENGTH];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002383 snprintf(line, sizeof(line),
2384 "Command action\n"
2385 " %s\n"
2386 " p primary partition (1-4)\n",
2387 (extended_offset ?
2388 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002389 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002390 c = read_nonempty(line);
2391 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002392 i = get_nonexisting_partition(0, 4);
2393 if (i >= 0)
2394 add_partition(i, LINUX_NATIVE);
2395 return;
2396 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002397 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002398 add_logical();
2399 return;
2400 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002401 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002402 i = get_nonexisting_partition(0, 4);
2403 if (i >= 0)
2404 add_partition(i, EXTENDED);
2405 return;
2406 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002407 printf("Invalid partition number "
2408 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409 }
2410 }
2411}
2412
2413static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002414write_table(void)
2415{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002416 int i;
2417
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002418 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002419 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 if (ptes[i].changed)
2421 ptes[3].changed = 1;
2422 for (i = 3; i < partitions; i++) {
2423 struct pte *pe = &ptes[i];
2424
2425 if (pe->changed) {
2426 write_part_table_flag(pe->sectorbuffer);
2427 write_sector(pe->offset, pe->sectorbuffer);
2428 }
2429 }
2430 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002431 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 /* no test on change? the printf below might be mistaken */
2433 sgi_write_table();
2434 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002435 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436 int needw = 0;
2437
Rob Landleyb73451d2006-02-24 16:29:00 +00002438 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 if (ptes[i].changed)
2440 needw = 1;
2441 if (needw)
2442 sun_write_table();
2443 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002444
Denis Vlasenkobd852072007-03-19 14:43:38 +00002445 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 reread_partition_table(1);
2447}
2448
Rob Landleyb73451d2006-02-24 16:29:00 +00002449static void
2450reread_partition_table(int leave)
2451{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452 int i;
2453
Denis Vlasenkobd852072007-03-19 14:43:38 +00002454 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002456 /* sleep(2); Huh? */
Denis Vlasenko28703012006-12-19 20:32:02 +00002457 i = ioctl(fd, BLKRRPART);
2458#if 0
2459 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002460 /* some kernel versions (1.2.x) seem to have trouble
2461 rereading the partition table, but if asked to do it
2462 twice, the second time works. - biro@yggdrasil.com */
2463 sync();
2464 sleep(2);
Denis Vlasenko28703012006-12-19 20:32:02 +00002465 i = ioctl(fd, BLKRRPART);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 }
Denis Vlasenko28703012006-12-19 20:32:02 +00002467#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468
2469 if (i) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002470 bb_perror_msg("WARNING: rereading partition table "
2471 "failed, kernel still uses old table");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002472 }
2473
Denis Vlasenko28703012006-12-19 20:32:02 +00002474#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002476 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002477 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002479 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002480#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002481
2482 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002483 if (ENABLE_FEATURE_CLEAN_UP)
2484 close(fd);
2485 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486 }
2487}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002488#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489
Denis Vlasenko834410a2006-11-29 12:00:28 +00002490#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491#define MAX_PER_LINE 16
2492static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002493print_buffer(char *pbuffer)
2494{
2495 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496
2497 for (i = 0, l = 0; i < sector_size; i++, l++) {
2498 if (l == 0)
2499 printf("0x%03X:", i);
2500 printf(" %02X", (unsigned char) pbuffer[i]);
2501 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002502 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002503 l = -1;
2504 }
2505 }
2506 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002507 puts("");
2508 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002509}
2510
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002511static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002512print_raw(void)
2513{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002514 int i;
2515
Denis Vlasenkobd852072007-03-19 14:43:38 +00002516 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002517 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002519 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002520 for (i = 3; i < partitions; i++)
2521 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002522 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002523}
2524
2525static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002526move_begin(int i)
2527{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002528 struct pte *pe = &ptes[i];
2529 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002530 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002531
2532 if (warn_geometry())
2533 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002534 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002535 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002536 return;
2537 }
2538 first = get_partition_start(pe);
2539 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002540 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541
2542 if (new != get_nr_sects(p)) {
2543 first = get_nr_sects(p) + get_start_sect(p) - new;
2544 set_nr_sects(p, first);
2545 set_start_sect(p, new);
2546 pe->changed = 1;
2547 }
2548}
2549
2550static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002551xselect(void)
2552{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002553 char c;
2554
Rob Landleyb73451d2006-02-24 16:29:00 +00002555 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002557 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002558 switch (c) {
2559 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002560 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002561 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002562 break;
2563 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002564 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 move_begin(get_partition(0, partitions));
2566 break;
2567 case 'c':
2568 user_cylinders = cylinders =
2569 read_int(1, cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002570 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002571 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002573 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 warn_cylinders();
2575 break;
2576 case 'd':
2577 print_raw();
2578 break;
2579 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002582 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002584 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585 x_list_table(1);
2586 break;
2587 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002588 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 fix_partition_table_order();
2590 break;
2591 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002592#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593 create_sgilabel();
2594#endif
2595 break;
2596 case 'h':
2597 user_heads = heads = read_int(1, heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002598 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 update_units();
2600 break;
2601 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002602 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002603 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002604 break;
2605 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002606 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002607 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 break;
2609 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002610 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 list_table(1);
2612 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 x_list_table(0);
2614 break;
2615 case 'q':
2616 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002617 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 exit(0);
2619 case 'r':
2620 return;
2621 case 's':
2622 user_sectors = sectors = read_int(1, sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002623 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002624 if (dos_compatible_flag) {
2625 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002626 printf("Warning: setting sector offset for DOS "
2627 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002628 }
2629 update_units();
2630 break;
2631 case 'v':
2632 verify();
2633 break;
2634 case 'w':
2635 write_table(); /* does not return */
2636 break;
2637 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002638 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002639 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002640 break;
2641 default:
2642 xmenu();
2643 }
2644 }
2645}
2646#endif /* ADVANCED mode */
2647
2648static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002649is_ide_cdrom_or_tape(const char *device)
2650{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002651 FILE *procf;
2652 char buf[100];
2653 struct stat statbuf;
2654 int is_ide = 0;
2655
2656 /* No device was given explicitly, and we are trying some
2657 likely things. But opening /dev/hdc may produce errors like
2658 "hdc: tray open or drive not ready"
2659 if it happens to be a CD-ROM drive. It even happens that
2660 the process hangs on the attempt to read a music CD.
2661 So try to be careful. This only works since 2.1.73. */
2662
2663 if (strncmp("/dev/hd", device, 7))
2664 return 0;
2665
2666 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2667 procf = fopen(buf, "r");
2668 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2669 is_ide = (!strncmp(buf, "cdrom", 5) ||
2670 !strncmp(buf, "tape", 4));
2671 else
2672 /* Now when this proc file does not exist, skip the
2673 device when it is read-only. */
2674 if (stat(device, &statbuf) == 0)
2675 is_ide = ((statbuf.st_mode & 0222) == 0);
2676
2677 if (procf)
2678 fclose(procf);
2679 return is_ide;
2680}
2681
Rob Landley5527b912006-02-25 03:46:10 +00002682
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002683static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002684trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002685{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002686 int gb;
2687
2688 disk_device = device;
2689 if (setjmp(listingbuf))
2690 return;
2691 if (!user_specified)
2692 if (is_ide_cdrom_or_tape(device))
2693 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002694 fd = open(disk_device, type_open);
2695 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002696 gb = get_boot(try_only);
2697 if (gb > 0) { /* I/O error */
2698 close(fd);
2699 } else if (gb < 0) { /* no DOS signature */
2700 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002701 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002702 return;
Rob Landley5527b912006-02-25 03:46:10 +00002703 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002704#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002705 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002707 printf("Disk %s doesn't contain a valid "
2708 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 close(fd);
2710 } else {
2711 close(fd);
2712 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002713#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002714 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002715 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002716 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002717#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002718 }
2719 } else {
2720 /* Ignore other errors, since we try IDE
2721 and SCSI hard disks which may not be
2722 installed on the system. */
2723 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002724 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002725 return;
2726 }
2727 }
2728}
2729
2730/* for fdisk -l: try all things in /proc/partitions
2731 that look like a partition name (do not end in a digit) */
2732static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002733tryprocpt(void)
2734{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002735 FILE *procpt;
2736 char line[100], ptname[100], devname[120], *s;
2737 int ma, mi, sz;
2738
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002739 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002740
2741 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002742 if (sscanf(line, " %d %d %d %[^\n ]",
2743 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002744 continue;
2745 for (s = ptname; *s; s++);
2746 if (isdigit(s[-1]))
2747 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002748 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002749 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002751#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002752 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002753#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002754}
2755
Denis Vlasenko834410a2006-11-29 12:00:28 +00002756#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002757static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002758unknown_command(int c)
2759{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002760 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002762#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002763
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002764void BUG_fdisk_globals_overflow(void);
2765
Denis Vlasenko06af2162007-02-03 17:28:39 +00002766int fdisk_main(int argc, char **argv);
Rob Landleyb73451d2006-02-24 16:29:00 +00002767int fdisk_main(int argc, char **argv)
2768{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002769 char *str_b, *str_C, *str_H, *str_S;
2770 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002771 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002772 * fdisk -v
2773 * fdisk -l [-b sectorsize] [-u] device ...
2774 * fdisk -s [partition] ...
2775 * fdisk [-b sectorsize] [-u] device
2776 *
2777 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002778 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002779 enum {
2780 OPT_b = 1 << 0,
2781 OPT_C = 1 << 1,
2782 OPT_H = 1 << 2,
2783 OPT_l = 1 << 3,
2784 OPT_S = 1 << 4,
2785 OPT_u = 1 << 5,
2786 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2787 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002788
2789 if (sizeof(G) > sizeof(bb_common_bufsiz1))
2790 BUG_fdisk_globals_overflow();
2791
Denis Vlasenko834410a2006-11-29 12:00:28 +00002792 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2793 &str_b, &str_C, &str_H, &str_S);
2794 argc -= optind;
2795 argv += optind;
2796 if (opt & OPT_b) { // -b
2797 /* Ugly: this sector size is really per device,
2798 so cannot be combined with multiple disks,
2799 and the same goes for the C/H/S options.
2800 */
2801 sector_size = xatoi_u(str_b);
2802 if (sector_size != 512 && sector_size != 1024 &&
2803 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002804 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002805 sector_offset = 2;
2806 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002807 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002808 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2809 if (opt & OPT_H) { // -H
2810 user_heads = xatoi_u(str_H);
2811 if (user_heads <= 0 || user_heads >= 256)
2812 user_heads = 0;
2813 }
2814 //if (opt & OPT_l) // -l
2815 if (opt & OPT_S) { // -S
2816 user_sectors = xatoi_u(str_S);
2817 if (user_sectors <= 0 || user_sectors >= 64)
2818 user_sectors = 0;
2819 }
2820 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2821 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822
Denis Vlasenko834410a2006-11-29 12:00:28 +00002823 if (user_set_sector_size && argc != 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002824 printf("Warning: the -b (set sector size) option should"
2825 " be used with one specified device\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002826
Denis Vlasenko834410a2006-11-29 12:00:28 +00002827#if ENABLE_FEATURE_FDISK_WRITABLE
2828 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002829 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002830#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002832 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002834#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 /* avoid gcc warning:
2836 variable `k' might be clobbered by `longjmp' */
2837 (void)&k;
2838#endif
2839 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002840 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002841 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002842 } else {
2843 /* we no longer have default device names */
2844 /* but, we can use /proc/partitions instead */
2845 tryprocpt();
2846 }
2847 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002848#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002850#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851
Denis Vlasenko834410a2006-11-29 12:00:28 +00002852#if ENABLE_FEATURE_FDISK_BLKSIZE
2853 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002855 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856
2857 nowarn = 1;
2858 type_open = O_RDONLY;
2859
Denis Vlasenko834410a2006-11-29 12:00:28 +00002860 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002861 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862
Denis Vlasenko834410a2006-11-29 12:00:28 +00002863 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002865 fd = open(disk_device, type_open);
2866 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 fdisk_fatal(unable_to_open);
2868 if (ioctl(fd, BLKGETSIZE, &size))
2869 fdisk_fatal(ioctl_error);
2870 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002871 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 printf("%ld\n", size/2);
2873 else
2874 printf("%s: %ld\n", argv[j], size/2);
2875 }
2876 return 0;
2877 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002878#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002879
Denis Vlasenko834410a2006-11-29 12:00:28 +00002880#if ENABLE_FEATURE_FDISK_WRITABLE
2881 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002882 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883
Denis Vlasenko834410a2006-11-29 12:00:28 +00002884 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885 get_boot(fdisk);
2886
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002887 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002889 printf("Detected an OSF/1 disklabel on %s, entering "
2890 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002891 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002892 /*Why do we do this? It seems to be counter-intuitive*/
2893 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002894 /* If we return we may want to make an empty DOS label? */
2895 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896
2897 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002898 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002899 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002900 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002901 switch (c) {
2902 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002903 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002904 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002905 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906 toggle_sunflags(get_partition(1, partitions),
2907 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002908 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 sgi_set_bootpartition(
2910 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002911 else
2912 unknown_command(c);
2913 break;
2914 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002915 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002916 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002917 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002918 if (read_maybe_empty("Please enter the name of the "
2919 "new boot file: ") == '\n')
2920 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002921 else
2922 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002923 }
2924#if ENABLE_FEATURE_OSF_LABEL
2925 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002926 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002927#endif
2928 break;
2929 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002930 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002931 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 toggle_sunflags(get_partition(1, partitions),
2934 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002935 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002936 sgi_set_swappartition(
2937 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 else
2939 unknown_command(c);
2940 break;
2941 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002942 {
Eric Andersen040f4402003-07-30 08:40:37 +00002943 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002944 /* If sgi_label then don't use get_existing_partition,
2945 let the user select a partition, since
2946 get_existing_partition() only works for Linux-like
2947 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002948 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002949 j = get_existing_partition(1, partitions);
2950 } else {
2951 j = get_partition(1, partitions);
2952 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002953 if (j >= 0)
2954 delete_partition(j);
2955 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002956 break;
2957 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002958 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002959 create_sgiinfo();
2960 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002961 unknown_command(c);
2962 case 'l':
2963 list_types(get_sys_types());
2964 break;
2965 case 'm':
2966 menu();
2967 break;
2968 case 'n':
2969 new_partition();
2970 break;
2971 case 'o':
2972 create_doslabel();
2973 break;
2974 case 'p':
2975 list_table(0);
2976 break;
2977 case 'q':
2978 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002979 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002980 return 0;
2981 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002982#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002983 create_sunlabel();
2984#endif
2985 break;
2986 case 't':
2987 change_sysid();
2988 break;
2989 case 'u':
2990 change_units();
2991 break;
2992 case 'v':
2993 verify();
2994 break;
2995 case 'w':
2996 write_table(); /* does not return */
2997 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002998#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003000 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003001 printf("\n\tSorry, no experts menu for SGI "
3002 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003003 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003004 xselect();
3005 break;
3006#endif
3007 default:
3008 unknown_command(c);
3009 menu();
3010 }
3011 }
3012 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003013#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003014}