blob: 74c7710dad65d3707af5b5f768743a941c32543a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000010#include <assert.h> /* assert */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000011#include "busybox.h"
Denis Vlasenko98ae2162006-10-12 19:30:44 +000012#define _(x) x
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000013
14#define DEFAULT_SECTOR_SIZE 512
15#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000016#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000017#define MAXIMUM_PARTS 60
18
19#define ACTIVE_FLAG 0x80
20
21#define EXTENDED 0x05
22#define WIN98_EXTENDED 0x0f
23#define LINUX_PARTITION 0x81
24#define LINUX_SWAP 0x82
25#define LINUX_NATIVE 0x83
26#define LINUX_EXTENDED 0x85
27#define LINUX_LVM 0x8e
28#define LINUX_RAID 0xfd
29
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000030#define IS_EXTENDED(i) \
31 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
32
33#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
34
35#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
36#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
37
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000038struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000039 unsigned char heads;
40 unsigned char sectors;
41 unsigned short cylinders;
42 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000043};
44
Denis Vlasenko98ae2162006-10-12 19:30:44 +000045#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000046
47struct systypes {
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +000048 const char *name;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000049};
50
Rob Landleyb73451d2006-02-24 16:29:00 +000051static uint sector_size = DEFAULT_SECTOR_SIZE;
52static uint user_set_sector_size;
53static uint sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000054
55/*
56 * Raw disk label. For DOS-type partition tables the MBR,
57 * with descriptions of the primary partitions.
58 */
"Vladimir N. Oleynik"65bb10f2005-11-24 12:10:13 +000059#if (MAX_SECTOR_SIZE) > (BUFSIZ+1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000060static char MBRbuffer[MAX_SECTOR_SIZE];
"Vladimir N. Oleynik"65bb10f2005-11-24 12:10:13 +000061#else
62# define MBRbuffer bb_common_bufsiz1
63#endif
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +000064
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000065#ifdef CONFIG_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
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000069static uint heads, sectors, cylinders;
70static void update_units(void);
71
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072
73/*
74 * return partition name - uses static storage unless buf is supplied
75 */
76static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +000077partname(const char *dev, int pno, int lth)
78{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079 static char buffer[80];
80 const char *p;
81 int w, wp;
82 int bufsiz;
83 char *bufp;
84
85 bufp = buffer;
86 bufsiz = sizeof(buffer);
87
88 w = strlen(dev);
89 p = "";
90
91 if (isdigit(dev[w-1]))
92 p = "p";
93
94 /* devfs kludge - note: fdisk partition names are not supposed
95 to equal kernel names, so there is no reason to do this */
Rob Landleyb73451d2006-02-24 16:29:00 +000096 if (strcmp(dev + w - 4, "disc") == 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000097 w -= 4;
98 p = "part";
99 }
100
101 wp = strlen(p);
102
103 if (lth) {
104 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
105 lth-wp-2, w, dev, p, pno);
106 } else {
107 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
108 }
109 return bufp;
110}
111
112struct partition {
113 unsigned char boot_ind; /* 0x80 - active */
114 unsigned char head; /* starting head */
115 unsigned char sector; /* starting sector */
116 unsigned char cyl; /* starting cylinder */
117 unsigned char sys_ind; /* What partition type */
118 unsigned char end_head; /* end head */
119 unsigned char end_sector; /* end sector */
120 unsigned char end_cyl; /* end cylinder */
121 unsigned char start4[4]; /* starting sector counting from 0 */
122 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000123} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000124
125enum failure {
126 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
127 unable_to_write
128};
129
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000130enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +0000131 label_dos, label_sun, label_sgi, label_aix, label_osf
132};
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000133#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000134
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000135#ifdef CONFIG_FEATURE_SUN_LABEL
136#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000137#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000138#else
139#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000140#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000141#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000142
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000143#ifdef CONFIG_FEATURE_SGI_LABEL
144#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000145#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000146#else
147#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000148#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000149#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000150
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000151#ifdef CONFIG_FEATURE_AIX_LABEL
152#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000153#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000154#else
155#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000156#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000157#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000158
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000159#ifdef CONFIG_FEATURE_OSF_LABEL
160#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000161#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000162#else
163#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000164#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000165#endif
Rob Landley5527b912006-02-25 03:46:10 +0000166
Rob Landleyb73451d2006-02-24 16:29:00 +0000167enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000168
Rob Landley5527b912006-02-25 03:46:10 +0000169static enum label_type current_label_type;
170
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000171static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000172static int fd; /* the disk */
173static int partitions = 4; /* maximum partition + 1 */
174static uint display_in_cyl_units = 1;
175static uint units_per_sector = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000176#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000177static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000178static void reread_partition_table(int leave);
179static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000180static int get_partition(int warn, int max);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000181static void list_types(const struct systypes *sys);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000182static uint read_int(uint low, uint dflt, uint high, uint base, char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000183#endif
184static const char *partition_type(unsigned char type);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000185static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000186static void get_geometry(void);
187static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000188
189#define PLURAL 0
190#define SINGULAR 1
191
192#define hex_val(c) ({ \
193 char _c = (c); \
194 isdigit(_c) ? _c - '0' : \
195 tolower(_c) + 10 - 'a'; \
196 })
197
198
199#define LINE_LENGTH 800
200#define pt_offset(b, n) ((struct partition *)((b) + 0x1be + \
201 (n) * sizeof(struct partition)))
202#define sector(s) ((s) & 0x3f)
203#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
204
205#define hsc2sector(h,s,c) (sector(s) - 1 + sectors * \
206 ((h) + heads * cylinder(s,c)))
207#define set_hsc(h,s,c,sector) { \
208 s = sector % sectors + 1; \
209 sector /= sectors; \
210 h = sector % heads; \
211 sector /= heads; \
212 c = sector & 0xff; \
213 s |= (sector >> 2) & 0xc0; \
214 }
215
216
Eric Andersend9261492004-06-28 23:50:31 +0000217static int32_t get_start_sect(const struct partition *p);
218static int32_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000219
220/*
221 * per partition table entry data
222 *
223 * The four primary partitions have the same sectorbuffer (MBRbuffer)
224 * and have NULL ext_pointer.
225 * Each logical partition table entry has two pointers, one for the
226 * partition and one link to the next one.
227 */
228static struct pte {
229 struct partition *part_table; /* points into sectorbuffer */
230 struct partition *ext_pointer; /* points into sectorbuffer */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000231#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000232 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000233#endif
Eric Andersend9261492004-06-28 23:50:31 +0000234 off_t offset; /* disk sector number */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000235 char *sectorbuffer; /* disk sector contents */
236} ptes[MAXIMUM_PARTS];
237
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000238
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000239#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000240static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000241set_all_unchanged(void)
242{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000243 int i;
244
245 for (i = 0; i < MAXIMUM_PARTS; i++)
246 ptes[i].changed = 0;
247}
248
249static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000250set_changed(int i)
251{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000252 ptes[i].changed = 1;
253}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000254#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000255
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000256static inline struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000257get_part_table(int i)
258{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000259 return ptes[i].part_table;
260}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000261
262static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000263str_units(int n)
264{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000265 if (n == 1)
266 return display_in_cyl_units ? _("cylinder") : _("sector");
267 else
268 return display_in_cyl_units ? _("cylinders") : _("sectors");
269}
270
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000271static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000272valid_part_table_flag(const char *mbuffer)
273{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000274 const unsigned char *b = (const unsigned char *)mbuffer;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000275 return (b[510] == 0x55 && b[511] == 0xaa);
276}
277
278#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000279static char line_buffer[LINE_LENGTH];
280static char *line_ptr;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000281
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000282/* read line; return 0 or first char */
283static int
284read_line(void)
285{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000286 fflush(stdout); /* requested by niles@scyld.com */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000287 line_ptr = line_buffer;
288 if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000289 /* error or eof */
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000290 bb_error_msg_and_die("\ngot EOF, exiting");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000291 }
292 while (*line_ptr && !isgraph(*line_ptr))
293 line_ptr++;
294 return *line_ptr;
295}
296
297static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000298read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000299{
300 do {
301 fputs(mesg, stdout);
302 } while (!read_line());
303 return *line_ptr;
304}
305
306static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000307read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000308{
309 fputs(mesg, stdout);
310 if (!read_line()) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000311 line_ptr = line_buffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000312 *line_ptr = '\n';
313 line_ptr[1] = 0;
314 }
315 return *line_ptr;
316}
317
318static int
319read_hex(const struct systypes *sys)
320{
Rob Landleyb73451d2006-02-24 16:29:00 +0000321 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000322 read_nonempty(_("Hex code (type L to list codes): "));
Rob Landleyb73451d2006-02-24 16:29:00 +0000323 if (*line_ptr == 'l' || *line_ptr == 'L')
324 list_types(sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000325 else if (isxdigit(*line_ptr)) {
326 return strtoul(line_ptr, NULL, 16);
Rob Landleyb73451d2006-02-24 16:29:00 +0000327 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000328 }
329}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000330#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000331
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000332#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000333
334typedef struct {
335 unsigned char info[128]; /* Informative text string */
336 unsigned char spare0[14];
337 struct sun_info {
338 unsigned char spare1;
339 unsigned char id;
340 unsigned char spare2;
341 unsigned char flags;
342 } infos[8];
343 unsigned char spare1[246]; /* Boot information etc. */
344 unsigned short rspeed; /* Disk rotational speed */
345 unsigned short pcylcount; /* Physical cylinder count */
346 unsigned short sparecyl; /* extra sects per cylinder */
347 unsigned char spare2[4]; /* More magic... */
348 unsigned short ilfact; /* Interleave factor */
349 unsigned short ncyl; /* Data cylinder count */
350 unsigned short nacyl; /* Alt. cylinder count */
351 unsigned short ntrks; /* Tracks per cylinder */
352 unsigned short nsect; /* Sectors per track */
353 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000354 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000355 uint32_t start_cylinder;
356 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000357 } partitions[8];
358 unsigned short magic; /* Magic number */
359 unsigned short csum; /* Label xor'd checksum */
360} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000361#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000362#define SUNOS_SWAP 3
363#define SUN_WHOLE_DISK 5
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000364STATIC_OSF void bsd_select(void);
365STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000366#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000367
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000368#define SGI_VOLHDR 0x00
369/* 1 and 2 were used for drive types no longer supported by SGI */
370#define SGI_SWAP 0x03
371/* 4 and 5 were for filesystem types SGI haven't ever supported on MIPS CPUs */
372#define SGI_VOLUME 0x06
373#define SGI_EFS 0x07
374#define SGI_LVOL 0x08
375#define SGI_RLVOL 0x09
376#define SGI_XFS 0x0a
377#define SGI_XFSLOG 0x0b
378#define SGI_XLV 0x0c
379#define SGI_XVM 0x0d
380#define SGI_ENTIRE_DISK SGI_VOLUME
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000381#if defined(CONFIG_FEATURE_SGI_LABEL) || defined(CONFIG_FEATURE_SUN_LABEL)
Rob Landley88621d72006-08-29 19:41:06 +0000382static unsigned short
Rob Landleyb73451d2006-02-24 16:29:00 +0000383__swap16(unsigned short x)
384{
Eric Andersenacd244a2002-12-11 03:49:33 +0000385 return (((uint16_t)(x) & 0xFF) << 8) | (((uint16_t)(x) & 0xFF00) >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000386}
387
Rob Landley88621d72006-08-29 19:41:06 +0000388static uint32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000389__swap32(uint32_t x)
390{
391 return (((x & 0xFF) << 24) |
Eric Andersen040f4402003-07-30 08:40:37 +0000392 ((x & 0xFF00) << 8) |
393 ((x & 0xFF0000) >> 8) |
394 ((x & 0xFF000000) >> 24));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000395}
396#endif
397
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000398STATIC_SGI const struct systypes sgi_sys_types[];
399STATIC_SGI unsigned int sgi_get_num_sectors(int i);
400STATIC_SGI int sgi_get_sysid(int i);
401STATIC_SGI void sgi_delete_partition(int i);
402STATIC_SGI void sgi_change_sysid(int i, int sys);
403STATIC_SGI void sgi_list_table(int xtra);
404STATIC_SGI void sgi_set_xcyl(void);
405STATIC_SGI int verify_sgi(int verbose);
406STATIC_SGI void sgi_add_partition(int n, int sys);
407STATIC_SGI void sgi_set_swappartition(int i);
408STATIC_SGI const char *sgi_get_bootfile(void);
409STATIC_SGI void sgi_set_bootfile(const char* aFile);
410STATIC_SGI void create_sgiinfo(void);
411STATIC_SGI void sgi_write_table(void);
412STATIC_SGI void sgi_set_bootpartition(int i);
413
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000414#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000415
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000416STATIC_SUN const struct systypes sun_sys_types[];
417STATIC_SUN void sun_delete_partition(int i);
418STATIC_SUN void sun_change_sysid(int i, int sys);
419STATIC_SUN void sun_list_table(int xtra);
420STATIC_SUN void sun_set_xcyl(void);
421STATIC_SUN void add_sun_partition(int n, int sys);
422STATIC_SUN void sun_set_alt_cyl(void);
423STATIC_SUN void sun_set_ncyl(int cyl);
424STATIC_SUN void sun_set_xcyl(void);
425STATIC_SUN void sun_set_ilfact(void);
426STATIC_SUN void sun_set_rspeed(void);
427STATIC_SUN void sun_set_pcylcount(void);
428STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
429STATIC_SUN void verify_sun(void);
430STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000431#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000432
433/* DOS partition types */
434
435static const struct systypes i386_sys_types[] = {
Rob Landleyb73451d2006-02-24 16:29:00 +0000436 { "\x00" "Empty" },
437 { "\x01" "FAT12" },
438 { "\x04" "FAT16 <32M" },
439 { "\x05" "Extended" }, /* DOS 3.3+ extended partition */
440 { "\x06" "FAT16" }, /* DOS 16-bit >=32M */
441 { "\x07" "HPFS/NTFS" }, /* OS/2 IFS, eg, HPFS or NTFS or QNX */
442 { "\x0a" "OS/2 Boot Manager" },/* OS/2 Boot Manager */
443 { "\x0b" "Win95 FAT32" },
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000444 { "\x0c" "Win95 FAT32 (LBA)" },/* LBA really is 'Extended Int 13h' */
Rob Landleyb73451d2006-02-24 16:29:00 +0000445 { "\x0e" "Win95 FAT16 (LBA)" },
446 { "\x0f" "Win95 Ext'd (LBA)" },
447 { "\x11" "Hidden FAT12" },
448 { "\x12" "Compaq diagnostics" },
449 { "\x14" "Hidden FAT16 <32M" },
450 { "\x16" "Hidden FAT16" },
451 { "\x17" "Hidden HPFS/NTFS" },
452 { "\x1b" "Hidden Win95 FAT32" },
453 { "\x1c" "Hidden Win95 FAT32 (LBA)" },
454 { "\x1e" "Hidden Win95 FAT16 (LBA)" },
455 { "\x3c" "PartitionMagic recovery" },
456 { "\x41" "PPC PReP Boot" },
457 { "\x42" "SFS" },
458 { "\x63" "GNU HURD or SysV" }, /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
459 { "\x80" "Old Minix" }, /* Minix 1.4a and earlier */
460 { "\x81" "Minix / old Linux" },/* Minix 1.4b and later */
461 { "\x82" "Linux swap" }, /* also Solaris */
462 { "\x83" "Linux" },
463 { "\x84" "OS/2 hidden C: drive" },
464 { "\x85" "Linux extended" },
465 { "\x86" "NTFS volume set" },
466 { "\x87" "NTFS volume set" },
467 { "\x8e" "Linux LVM" },
468 { "\x9f" "BSD/OS" }, /* BSDI */
469 { "\xa0" "IBM Thinkpad hibernation" },
470 { "\xa5" "FreeBSD" }, /* various BSD flavours */
471 { "\xa6" "OpenBSD" },
472 { "\xa8" "Darwin UFS" },
473 { "\xa9" "NetBSD" },
474 { "\xab" "Darwin boot" },
475 { "\xb7" "BSDI fs" },
476 { "\xb8" "BSDI swap" },
477 { "\xbe" "Solaris boot" },
478 { "\xeb" "BeOS fs" },
479 { "\xee" "EFI GPT" }, /* Intel EFI GUID Partition Table */
480 { "\xef" "EFI (FAT-12/16/32)" },/* Intel EFI System Partition */
481 { "\xf0" "Linux/PA-RISC boot" },/* Linux/PA-RISC boot loader */
482 { "\xf2" "DOS secondary" }, /* DOS 3.3+ secondary */
483 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
484 autodetect using persistent
485 superblock */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000486#ifdef CONFIG_WEIRD_PARTITION_TYPES
Rob Landleyb73451d2006-02-24 16:29:00 +0000487 { "\x02" "XENIX root" },
488 { "\x03" "XENIX usr" },
489 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
490 { "\x09" "AIX bootable" }, /* AIX data or Coherent */
491 { "\x10" "OPUS" },
492 { "\x18" "AST SmartSleep" },
493 { "\x24" "NEC DOS" },
494 { "\x39" "Plan 9" },
495 { "\x40" "Venix 80286" },
496 { "\x4d" "QNX4.x" },
497 { "\x4e" "QNX4.x 2nd part" },
498 { "\x4f" "QNX4.x 3rd part" },
499 { "\x50" "OnTrack DM" },
500 { "\x51" "OnTrack DM6 Aux1" }, /* (or Novell) */
501 { "\x52" "CP/M" }, /* CP/M or Microport SysV/AT */
502 { "\x53" "OnTrack DM6 Aux3" },
503 { "\x54" "OnTrackDM6" },
504 { "\x55" "EZ-Drive" },
505 { "\x56" "Golden Bow" },
506 { "\x5c" "Priam Edisk" },
507 { "\x61" "SpeedStor" },
508 { "\x64" "Novell Netware 286" },
509 { "\x65" "Novell Netware 386" },
510 { "\x70" "DiskSecure Multi-Boot" },
511 { "\x75" "PC/IX" },
512 { "\x93" "Amoeba" },
513 { "\x94" "Amoeba BBT" }, /* (bad block table) */
514 { "\xa7" "NeXTSTEP" },
515 { "\xbb" "Boot Wizard hidden" },
516 { "\xc1" "DRDOS/sec (FAT-12)" },
517 { "\xc4" "DRDOS/sec (FAT-16 < 32M)" },
518 { "\xc6" "DRDOS/sec (FAT-16)" },
519 { "\xc7" "Syrinx" },
520 { "\xda" "Non-FS data" },
521 { "\xdb" "CP/M / CTOS / ..." },/* CP/M or Concurrent CP/M or
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000522 Concurrent DOS or CTOS */
Rob Landleyb73451d2006-02-24 16:29:00 +0000523 { "\xde" "Dell Utility" }, /* Dell PowerEdge Server utilities */
524 { "\xdf" "BootIt" }, /* BootIt EMBRM */
525 { "\xe1" "DOS access" }, /* DOS access or SpeedStor 12-bit FAT
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000526 extended partition */
Rob Landleyb73451d2006-02-24 16:29:00 +0000527 { "\xe3" "DOS R/O" }, /* DOS R/O or SpeedStor */
528 { "\xe4" "SpeedStor" }, /* SpeedStor 16-bit FAT extended
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000529 partition < 1024 cyl. */
Rob Landleyb73451d2006-02-24 16:29:00 +0000530 { "\xf1" "SpeedStor" },
531 { "\xf4" "SpeedStor" }, /* SpeedStor large partition */
532 { "\xfe" "LANstep" }, /* SpeedStor >1024 cyl. or LANstep */
533 { "\xff" "BBT" }, /* Xenix Bad Block Table */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000534#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000535 { 0 }
536};
537
538
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000539/* A valid partition table sector ends in 0x55 0xaa */
540static unsigned int
Rob Landleyb73451d2006-02-24 16:29:00 +0000541part_table_flag(const char *b)
542{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000543 return ((uint) b[510]) + (((uint) b[511]) << 8);
544}
545
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000546
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000547#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000548static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000549write_part_table_flag(char *b)
550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000551 b[510] = 0x55;
552 b[511] = 0xaa;
553}
554
555/* start_sect and nr_sects are stored little endian on all machines */
556/* moreover, they are not aligned correctly */
557static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000558store4_little_endian(unsigned char *cp, unsigned int val)
559{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000560 cp[0] = (val & 0xff);
561 cp[1] = ((val >> 8) & 0xff);
562 cp[2] = ((val >> 16) & 0xff);
563 cp[3] = ((val >> 24) & 0xff);
564}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000565#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000566
567static unsigned int
Rob Landleyb73451d2006-02-24 16:29:00 +0000568read4_little_endian(const unsigned char *cp)
569{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000570 return (uint)(cp[0]) + ((uint)(cp[1]) << 8)
571 + ((uint)(cp[2]) << 16) + ((uint)(cp[3]) << 24);
572}
573
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000574#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000575static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000576set_start_sect(struct partition *p, unsigned int start_sect)
577{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000578 store4_little_endian(p->start4, start_sect);
579}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000580#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000581
Eric Andersend9261492004-06-28 23:50:31 +0000582static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000583get_start_sect(const struct partition *p)
584{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000585 return read4_little_endian(p->start4);
586}
587
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000588#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000590set_nr_sects(struct partition *p, int32_t nr_sects)
591{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000592 store4_little_endian(p->size4, nr_sects);
593}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000594#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595
Eric Andersend9261492004-06-28 23:50:31 +0000596static int32_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000597get_nr_sects(const struct partition *p)
598{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599 return read4_little_endian(p->size4);
600}
601
602/* normally O_RDWR, -l option gives O_RDONLY */
603static int type_open = O_RDWR;
604
605
Rob Landleyb73451d2006-02-24 16:29:00 +0000606static int ext_index; /* the prime extended partition */
607static int listing; /* no aborts for fdisk -l */
608static int dos_compatible_flag = ~0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000609#ifdef CONFIG_FEATURE_FDISK_WRITABLE
610static int dos_changed;
611static int nowarn; /* no warnings for fdisk -l/-s */
612#endif
613
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000614
615
Rob Landleyb73451d2006-02-24 16:29:00 +0000616static uint user_cylinders, user_heads, user_sectors;
617static uint pt_heads, pt_sectors;
618static uint kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000619
Eric Andersend9261492004-06-28 23:50:31 +0000620static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000621
Eric Andersen040f4402003-07-30 08:40:37 +0000622static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000623
624
625static jmp_buf listingbuf;
626
Rob Landleyb73451d2006-02-24 16:29:00 +0000627static void fdisk_fatal(enum failure why)
628{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000629 const char *message;
630
631 if (listing) {
632 close(fd);
633 longjmp(listingbuf, 1);
634 }
635
636 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000637 case unable_to_open:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000638 message = "\nUnable to open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000639 break;
640 case unable_to_read:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000641 message = "\nUnable to read %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000642 break;
643 case unable_to_seek:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000644 message = "\nUnable to seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000645 break;
646 case unable_to_write:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000647 message = "\nUnable to write %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000648 break;
649 case ioctl_error:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000650 message = "\nBLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000651 break;
652 default:
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000653 message = "\nFatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000654 }
655
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000656 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657}
658
659static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000660seek_sector(off_t secno)
661{
Eric Andersen0a92f352004-03-30 09:21:54 +0000662 off_t offset = secno * sector_size;
663 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000664 fdisk_fatal(unable_to_seek);
665}
666
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000667#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000668static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000669write_sector(off_t secno, char *buf)
670{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000671 seek_sector(secno);
672 if (write(fd, buf, sector_size) != sector_size)
673 fdisk_fatal(unable_to_write);
674}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000675#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000676
677/* Allocate a buffer and read a partition table sector */
678static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000679read_pte(struct pte *pe, off_t offset)
680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681 pe->offset = offset;
682 pe->sectorbuffer = (char *) xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000683 seek_sector(offset);
684 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
685 fdisk_fatal(unable_to_read);
686#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000688#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000689 pe->part_table = pe->ext_pointer = NULL;
690}
691
692static unsigned int
Rob Landleyb73451d2006-02-24 16:29:00 +0000693get_partition_start(const struct pte *pe)
694{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000695 return pe->offset + get_start_sect(pe->part_table);
696}
697
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000698#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000699/*
700 * Avoid warning about DOS partitions when no DOS partition was changed.
701 * Here a heuristic "is probably dos partition".
702 * We might also do the opposite and warn in all cases except
703 * for "is probably nondos partition".
704 */
705static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000706is_dos_partition(int t)
707{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000708 return (t == 1 || t == 4 || t == 6 ||
709 t == 0x0b || t == 0x0c || t == 0x0e ||
710 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
711 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
712 t == 0xc1 || t == 0xc4 || t == 0xc6);
713}
714
715static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000716menu(void)
717{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000718 if (LABEL_IS_SUN) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000719 puts(_("Command action"));
720 puts(_("\ta\ttoggle a read only flag")); /* sun */
721 puts(_("\tb\tedit bsd disklabel"));
722 puts(_("\tc\ttoggle the mountable flag")); /* sun */
723 puts(_("\td\tdelete a partition"));
724 puts(_("\tl\tlist known partition types"));
725 puts(_("\tm\tprint this menu"));
726 puts(_("\tn\tadd a new partition"));
727 puts(_("\to\tcreate a new empty DOS partition table"));
728 puts(_("\tp\tprint the partition table"));
729 puts(_("\tq\tquit without saving changes"));
730 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
731 puts(_("\tt\tchange a partition's system id"));
732 puts(_("\tu\tchange display/entry units"));
733 puts(_("\tv\tverify the partition table"));
734 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000735#ifdef CONFIG_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000736 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000737#endif
738 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000739 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000740 puts(_("Command action"));
741 puts(_("\ta\tselect bootable partition")); /* sgi flavour */
742 puts(_("\tb\tedit bootfile entry")); /* sgi */
743 puts(_("\tc\tselect sgi swap partition")); /* sgi flavour */
744 puts(_("\td\tdelete a partition"));
745 puts(_("\tl\tlist known partition types"));
746 puts(_("\tm\tprint this menu"));
747 puts(_("\tn\tadd a new partition"));
748 puts(_("\to\tcreate a new empty DOS partition table"));
749 puts(_("\tp\tprint the partition table"));
750 puts(_("\tq\tquit without saving changes"));
751 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
752 puts(_("\tt\tchange a partition's system id"));
753 puts(_("\tu\tchange display/entry units"));
754 puts(_("\tv\tverify the partition table"));
755 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000756 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000757 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000758 puts(_("Command action"));
759 puts(_("\tm\tprint this menu"));
760 puts(_("\to\tcreate a new empty DOS partition table"));
761 puts(_("\tq\tquit without saving changes"));
762 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000763 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000764 {
Rob Landleyb73451d2006-02-24 16:29:00 +0000765 puts(_("Command action"));
766 puts(_("\ta\ttoggle a bootable flag"));
767 puts(_("\tb\tedit bsd disklabel"));
768 puts(_("\tc\ttoggle the dos compatibility flag"));
769 puts(_("\td\tdelete a partition"));
770 puts(_("\tl\tlist known partition types"));
771 puts(_("\tm\tprint this menu"));
772 puts(_("\tn\tadd a new partition"));
773 puts(_("\to\tcreate a new empty DOS partition table"));
774 puts(_("\tp\tprint the partition table"));
775 puts(_("\tq\tquit without saving changes"));
776 puts(_("\ts\tcreate a new empty Sun disklabel")); /* sun */
777 puts(_("\tt\tchange a partition's system id"));
778 puts(_("\tu\tchange display/entry units"));
779 puts(_("\tv\tverify the partition table"));
780 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000781#ifdef CONFIG_FEATURE_FDISK_ADVANCED
Rob Landleyb73451d2006-02-24 16:29:00 +0000782 puts(_("\tx\textra functionality (experts only)"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000783#endif
784 }
785}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000786#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
787
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000788
789#ifdef CONFIG_FEATURE_FDISK_ADVANCED
790static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000791xmenu(void)
792{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000793 if (LABEL_IS_SUN) {
794 puts(_("Command action"));
795 puts(_("\ta\tchange number of alternate cylinders")); /*sun*/
796 puts(_("\tc\tchange number of cylinders"));
797 puts(_("\td\tprint the raw data in the partition table"));
798 puts(_("\te\tchange number of extra sectors per cylinder"));/*sun*/
799 puts(_("\th\tchange number of heads"));
800 puts(_("\ti\tchange interleave factor")); /*sun*/
801 puts(_("\to\tchange rotation speed (rpm)")); /*sun*/
802 puts(_("\tm\tprint this menu"));
803 puts(_("\tp\tprint the partition table"));
804 puts(_("\tq\tquit without saving changes"));
805 puts(_("\tr\treturn to main menu"));
806 puts(_("\ts\tchange number of sectors/track"));
807 puts(_("\tv\tverify the partition table"));
808 puts(_("\tw\twrite table to disk and exit"));
809 puts(_("\ty\tchange number of physical cylinders")); /*sun*/
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000810 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000811 if (LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000812 puts(_("Command action"));
813 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
814 puts(_("\tc\tchange number of cylinders"));
815 puts(_("\td\tprint the raw data in the partition table"));
816 puts(_("\te\tlist extended partitions")); /* !sun */
817 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
818 puts(_("\th\tchange number of heads"));
819 puts(_("\tm\tprint this menu"));
820 puts(_("\tp\tprint the partition table"));
821 puts(_("\tq\tquit without saving changes"));
822 puts(_("\tr\treturn to main menu"));
823 puts(_("\ts\tchange number of sectors/track"));
824 puts(_("\tv\tverify the partition table"));
825 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000826 } else
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000827 if (LABEL_IS_AIX) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000828 puts(_("Command action"));
829 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
830 puts(_("\tc\tchange number of cylinders"));
831 puts(_("\td\tprint the raw data in the partition table"));
832 puts(_("\te\tlist extended partitions")); /* !sun */
833 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
834 puts(_("\th\tchange number of heads"));
835 puts(_("\tm\tprint this menu"));
836 puts(_("\tp\tprint the partition table"));
837 puts(_("\tq\tquit without saving changes"));
838 puts(_("\tr\treturn to main menu"));
839 puts(_("\ts\tchange number of sectors/track"));
840 puts(_("\tv\tverify the partition table"));
841 puts(_("\tw\twrite table to disk and exit"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000842 } else {
Rob Landleyb73451d2006-02-24 16:29:00 +0000843 puts(_("Command action"));
844 puts(_("\tb\tmove beginning of data in a partition")); /* !sun */
845 puts(_("\tc\tchange number of cylinders"));
846 puts(_("\td\tprint the raw data in the partition table"));
847 puts(_("\te\tlist extended partitions")); /* !sun */
848 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000849#ifdef CONFIG_FEATURE_SGI_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +0000850 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000851#endif
Rob Landleyb73451d2006-02-24 16:29:00 +0000852 puts(_("\th\tchange number of heads"));
853 puts(_("\tm\tprint this menu"));
854 puts(_("\tp\tprint the partition table"));
855 puts(_("\tq\tquit without saving changes"));
856 puts(_("\tr\treturn to main menu"));
857 puts(_("\ts\tchange number of sectors/track"));
858 puts(_("\tv\tverify the partition table"));
859 puts(_("\tw\twrite table to disk and exit"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860 }
861}
862#endif /* ADVANCED mode */
863
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000864#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000865static const struct systypes *
Rob Landleyb73451d2006-02-24 16:29:00 +0000866get_sys_types(void)
867{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000868 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000869 LABEL_IS_SUN ? sun_sys_types :
870 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000871 i386_sys_types);
872}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000873#else
874#define get_sys_types() i386_sys_types
875#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000876
877static const char *partition_type(unsigned char type)
878{
879 int i;
880 const struct systypes *types = get_sys_types();
881
Rob Landleyb73451d2006-02-24 16:29:00 +0000882 for (i = 0; types[i].name; i++)
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000883 if ((unsigned char )types[i].name[0] == type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000884 return types[i].name + 1;
885
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000886 return _("Unknown");
887}
888
889
890#ifdef CONFIG_FEATURE_FDISK_WRITABLE
891static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000892get_sysid(int i)
893{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000894 return LABEL_IS_SUN ? sunlabel->infos[i].id :
895 (LABEL_IS_SGI ? sgi_get_sysid(i) :
896 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000897}
898
899void list_types(const struct systypes *sys)
900{
901 uint last[4], done = 0, next = 0, size;
902 int i;
903
904 for (i = 0; sys[i].name; i++);
905 size = i;
906
907 for (i = 3; i >= 0; i--)
908 last[3 - i] = done += (size + i - done) / (i + 1);
909 i = done = 0;
910
911 do {
912 printf("%c%2x %-15.15s", i ? ' ' : '\n',
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +0000913 (unsigned char)sys[next].name[0],
914 partition_type((unsigned char)sys[next].name[0]));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000915 next = last[i++] + done;
916 if (i > 3 || next >= last[i]) {
917 i = 0;
918 next = ++done;
919 }
920 } while (done < last[0]);
921 putchar('\n');
922}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000923#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924
925static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000926is_cleared_partition(const struct partition *p)
927{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000928 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
929 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
930 get_start_sect(p) || get_nr_sects(p));
931}
932
933static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000934clear_partition(struct partition *p)
935{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000936 if (!p)
937 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000938 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000939}
940
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000941#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000942static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000943set_partition(int i, int doext, off_t start, off_t stop, int sysid)
944{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000945 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000946 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947
948 if (doext) {
949 p = ptes[i].ext_pointer;
950 offset = extended_offset;
951 } else {
952 p = ptes[i].part_table;
953 offset = ptes[i].offset;
954 }
955 p->boot_ind = 0;
956 p->sys_ind = sysid;
957 set_start_sect(p, start - offset);
958 set_nr_sects(p, stop - start + 1);
959 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
960 start = heads*sectors*1024 - 1;
961 set_hsc(p->head, p->sector, p->cyl, start);
962 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
963 stop = heads*sectors*1024 - 1;
964 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
965 ptes[i].changed = 1;
966}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000967#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000968
969static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000970test_c(const char **m, const char *mesg)
971{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000972 int val = 0;
973 if (!*m)
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000974 printf(_("You must set"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975 else {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000976 printf(" %s", *m);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000977 val = 1;
978 }
979 *m = mesg;
980 return val;
981}
982
983static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000984warn_geometry(void)
985{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000986 const char *m = NULL;
987 int prev = 0;
988
989 if (!heads)
990 prev = test_c(&m, _("heads"));
991 if (!sectors)
992 prev = test_c(&m, _("sectors"));
993 if (!cylinders)
994 prev = test_c(&m, _("cylinders"));
995 if (!m)
996 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000997
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000998 printf("%s%s.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000999#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001000 "You can do this from the extra functions menu.\n"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001001#endif
1002 , prev ? _(" and ") : " ", m);
1003
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001004 return 1;
1005}
1006
1007static void update_units(void)
1008{
1009 int cyl_units = heads * sectors;
1010
1011 if (display_in_cyl_units && cyl_units)
1012 units_per_sector = cyl_units;
1013 else
1014 units_per_sector = 1; /* in sectors */
1015}
1016
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001017#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001019warn_cylinders(void)
1020{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001021 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
1022 printf(_("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001023"The number of cylinders for this disk is set to %d.\n"
1024"There is nothing wrong with that, but this is larger than 1024,\n"
1025"and could in certain setups cause problems with:\n"
1026"1) software that runs at boot time (e.g., old versions of LILO)\n"
1027"2) booting and partitioning software from other OSs\n"
1028" (e.g., DOS FDISK, OS/2 FDISK)\n"),
1029 cylinders);
1030}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001031#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032
1033static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001034read_extended(int ext)
1035{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001036 int i;
1037 struct pte *pex;
1038 struct partition *p, *q;
1039
1040 ext_index = ext;
1041 pex = &ptes[ext];
1042 pex->ext_pointer = pex->part_table;
1043
1044 p = pex->part_table;
1045 if (!get_start_sect(p)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001046 printf(_("Bad offset in primary extended partition\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 return;
1048 }
1049
Rob Landleyb73451d2006-02-24 16:29:00 +00001050 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 struct pte *pe = &ptes[partitions];
1052
1053 if (partitions >= MAXIMUM_PARTS) {
1054 /* This is not a Linux restriction, but
1055 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001056 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001057 struct pte *pre = &ptes[partitions-1];
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001058#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001059 printf(_("Warning: deleting partitions after %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001061 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001062#endif
1063 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001064 return;
1065 }
1066
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001067 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001068
1069 if (!extended_offset)
1070 extended_offset = get_start_sect(p);
1071
1072 q = p = pt_offset(pe->sectorbuffer, 0);
1073 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001074 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001075 if (pe->ext_pointer)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001076 printf(_("Warning: extra link "
1077 "pointer in partition table"
1078 " %d\n"), partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001079 else
1080 pe->ext_pointer = p;
1081 } else if (p->sys_ind) {
1082 if (pe->part_table)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001083 printf(_("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001084 "data in partition table"
1085 " %d\n"), partitions + 1);
1086 else
1087 pe->part_table = p;
1088 }
1089 }
1090
1091 /* very strange code here... */
1092 if (!pe->part_table) {
1093 if (q != pe->ext_pointer)
1094 pe->part_table = q;
1095 else
1096 pe->part_table = q + 1;
1097 }
1098 if (!pe->ext_pointer) {
1099 if (q != pe->part_table)
1100 pe->ext_pointer = q;
1101 else
1102 pe->ext_pointer = q + 1;
1103 }
1104
1105 p = pe->ext_pointer;
1106 partitions++;
1107 }
1108
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001109#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001110 /* remove empty links */
1111 remove:
1112 for (i = 4; i < partitions; i++) {
1113 struct pte *pe = &ptes[i];
1114
1115 if (!get_nr_sects(pe->part_table) &&
Rob Landleyb73451d2006-02-24 16:29:00 +00001116 (partitions > 5 || ptes[4].part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001117 printf("omitting empty partition (%d)\n", i+1);
1118 delete_partition(i);
1119 goto remove; /* numbering changed */
1120 }
1121 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001122#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001123}
1124
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001125#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001126static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001127create_doslabel(void)
1128{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001129 int i;
1130
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001131 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 _("Building a new DOS disklabel. Changes will remain in memory only,\n"
1133 "until you decide to write them. After that, of course, the previous\n"
1134 "content won't be recoverable.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001135
1136 current_label_type = label_dos;
1137
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001138#ifdef CONFIG_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001139 possibly_osf_label = 0;
1140#endif
1141 partitions = 4;
1142
1143 for (i = 510-64; i < 510; i++)
1144 MBRbuffer[i] = 0;
1145 write_part_table_flag(MBRbuffer);
1146 extended_offset = 0;
1147 set_all_unchanged();
1148 set_changed(0);
1149 get_boot(create_empty_dos);
1150}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001151#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001152
1153static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001154get_sectorsize(void)
1155{
Rob Landley736e5252006-02-25 03:36:00 +00001156 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001157 int arg;
1158 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1159 sector_size = arg;
1160 if (sector_size != DEFAULT_SECTOR_SIZE)
1161 printf(_("Note: sector size is %d (not %d)\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001162 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001163 }
1164}
1165
Rob Landley88621d72006-08-29 19:41:06 +00001166static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001167get_kernel_geometry(void)
1168{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001169 struct hd_geometry geometry;
1170
1171 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1172 kern_heads = geometry.heads;
1173 kern_sectors = geometry.sectors;
1174 /* never use geometry.cylinders - it is truncated */
1175 }
1176}
1177
1178static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001179get_partition_table_geometry(void)
1180{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001181 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001182 struct partition *p;
1183 int i, h, s, hh, ss;
1184 int first = 1;
1185 int bad = 0;
1186
Eric Andersen3496fdc2006-01-30 23:09:20 +00001187 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188 return;
1189
1190 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001191 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001192 p = pt_offset(bufp, i);
1193 if (p->sys_ind != 0) {
1194 h = p->end_head + 1;
1195 s = (p->end_sector & 077);
1196 if (first) {
1197 hh = h;
1198 ss = s;
1199 first = 0;
1200 } else if (hh != h || ss != s)
1201 bad = 1;
1202 }
1203 }
1204
1205 if (!first && !bad) {
1206 pt_heads = hh;
1207 pt_sectors = ss;
1208 }
1209}
1210
Rob Landleyb73451d2006-02-24 16:29:00 +00001211static void
1212get_geometry(void)
1213{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001214 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001215 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001216
1217 get_sectorsize();
1218 sec_fac = sector_size / 512;
1219#ifdef CONFIG_FEATURE_SUN_LABEL
1220 guess_device_type();
1221#endif
1222 heads = cylinders = sectors = 0;
1223 kern_heads = kern_sectors = 0;
1224 pt_heads = pt_sectors = 0;
1225
1226 get_kernel_geometry();
1227 get_partition_table_geometry();
1228
1229 heads = user_heads ? user_heads :
1230 pt_heads ? pt_heads :
1231 kern_heads ? kern_heads : 255;
1232 sectors = user_sectors ? user_sectors :
1233 pt_sectors ? pt_sectors :
1234 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001235 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1236 /* got bytes */
1237 } else {
1238 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001239
1240 if (ioctl(fd, BLKGETSIZE, &longsectors))
1241 longsectors = 0;
Eric Andersen040f4402003-07-30 08:40:37 +00001242 bytes = ((unsigned long long) longsectors) << 9;
1243 }
1244
1245 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001246
1247 sector_offset = 1;
1248 if (dos_compatible_flag)
1249 sector_offset = sectors;
1250
Eric Andersen040f4402003-07-30 08:40:37 +00001251 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 if (!cylinders)
1253 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001254}
1255
1256/*
1257 * Read MBR. Returns:
1258 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1259 * 0: found or created label
1260 * 1: I/O error
1261 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001262static int
1263get_boot(enum action what)
1264{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001265 int i;
1266
1267 partitions = 4;
1268
1269 for (i = 0; i < 4; i++) {
1270 struct pte *pe = &ptes[i];
1271
1272 pe->part_table = pt_offset(MBRbuffer, i);
1273 pe->ext_pointer = NULL;
1274 pe->offset = 0;
1275 pe->sectorbuffer = MBRbuffer;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001276#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001277 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001278#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279 }
1280
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001281#ifdef CONFIG_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001282 if (what == create_empty_sun && check_sun_label())
1283 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001284#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001285
1286 memset(MBRbuffer, 0, 512);
1287
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001288#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289 if (what == create_empty_dos)
1290 goto got_dos_table; /* skip reading disk */
1291
1292 if ((fd = open(disk_device, type_open)) < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001293 if ((fd = open(disk_device, O_RDONLY)) < 0) {
1294 if (what == try_only)
1295 return 1;
1296 fdisk_fatal(unable_to_open);
1297 } else
1298 printf(_("You will not be able to write "
1299 "the partition table.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 }
1301
1302 if (512 != read(fd, MBRbuffer, 512)) {
1303 if (what == try_only)
1304 return 1;
1305 fdisk_fatal(unable_to_read);
1306 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001307#else
1308 if ((fd = open(disk_device, O_RDONLY)) < 0)
1309 return 1;
1310 if (512 != read(fd, MBRbuffer, 512))
1311 return 1;
1312#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001314 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315
1316 update_units();
1317
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001318#ifdef CONFIG_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319 if (check_sun_label())
1320 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001321#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322
1323#ifdef CONFIG_FEATURE_SGI_LABEL
1324 if (check_sgi_label())
1325 return 0;
1326#endif
1327
1328#ifdef CONFIG_FEATURE_AIX_LABEL
1329 if (check_aix_label())
1330 return 0;
1331#endif
1332
1333#ifdef CONFIG_FEATURE_OSF_LABEL
1334 if (check_osf_label()) {
1335 possibly_osf_label = 1;
1336 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001337 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001338 return 0;
1339 }
1340 printf(_("This disk has both DOS and BSD magic.\n"
1341 "Give the 'b' command to go to BSD mode.\n"));
1342 }
1343#endif
1344
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001345#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001346 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001347#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348
1349 if (!valid_part_table_flag(MBRbuffer)) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001350#ifndef CONFIG_FEATURE_FDISK_WRITABLE
1351 return -1;
1352#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001353 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001354 case fdisk:
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001355 printf(_("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001356 "partition table, nor Sun, SGI or OSF "
1357 "disklabel\n"));
1358#ifdef __sparc__
1359#ifdef CONFIG_FEATURE_SUN_LABEL
1360 create_sunlabel();
1361#endif
1362#else
1363 create_doslabel();
1364#endif
1365 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001366 case try_only:
1367 return -1;
1368 case create_empty_dos:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001369#ifdef CONFIG_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001370 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001371#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372 break;
1373 default:
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001374 bb_error_msg_and_die(_("internal error"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001375 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001376#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001377 }
1378
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001379#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001381#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001382 warn_geometry();
1383
1384 for (i = 0; i < 4; i++) {
1385 struct pte *pe = &ptes[i];
1386
Rob Landleyb73451d2006-02-24 16:29:00 +00001387 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001388 if (partitions != 4)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001389 printf(_("Ignoring extra extended "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390 "partition %d\n"), i + 1);
1391 else
1392 read_extended(i);
1393 }
1394 }
1395
1396 for (i = 3; i < partitions; i++) {
1397 struct pte *pe = &ptes[i];
1398
1399 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001400 printf(_("Warning: invalid flag 0x%04x of partition "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001401 "table %d will be corrected by w(rite)\n"),
1402 part_table_flag(pe->sectorbuffer), i + 1);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001403#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001405#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406 }
1407 }
1408
1409 return 0;
1410}
1411
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001412#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413/*
1414 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1415 * If the user hits Enter, DFLT is returned.
1416 * Answers like +10 are interpreted as offsets from BASE.
1417 *
1418 * There is no default if DFLT is not between LOW and HIGH.
1419 */
1420static uint
1421read_int(uint low, uint dflt, uint high, uint base, char *mesg)
1422{
1423 uint i;
1424 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001425 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001426
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001427 if (dflt < low || dflt > high) {
1428 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001429 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001430 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001431
1432 while (1) {
1433 int use_default = default_ok;
1434
1435 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001436 do {
1437 printf(fmt, mesg, low, high, dflt);
1438 read_maybe_empty("");
1439 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1440 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001441
Eric Andersen84bdea82004-05-19 10:49:17 +00001442 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001443 int minus = (*line_ptr == '-');
1444 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001445
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001446 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001447
Rob Landleyb73451d2006-02-24 16:29:00 +00001448 while (isdigit(*++line_ptr))
1449 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001450
Rob Landleyb73451d2006-02-24 16:29:00 +00001451 switch (*line_ptr) {
1452 case 'c':
1453 case 'C':
1454 if (!display_in_cyl_units)
1455 i *= heads * sectors;
1456 break;
1457 case 'K':
1458 absolute = 1024;
1459 break;
1460 case 'k':
1461 absolute = 1000;
1462 break;
1463 case 'm':
1464 case 'M':
1465 absolute = 1000000;
1466 break;
1467 case 'g':
1468 case 'G':
1469 absolute = 1000000000;
1470 break;
1471 default:
1472 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001473 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001474 if (absolute) {
1475 unsigned long long bytes;
1476 unsigned long unit;
1477
1478 bytes = (unsigned long long) i * absolute;
1479 unit = sector_size * units_per_sector;
1480 bytes += unit/2; /* round */
1481 bytes /= unit;
1482 i = bytes;
1483 }
1484 if (minus)
1485 i = -i;
1486 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001487 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001488 i = atoi(line_ptr);
1489 while (isdigit(*line_ptr)) {
1490 line_ptr++;
1491 use_default = 0;
1492 }
1493 }
1494 if (use_default)
Eric Andersen040f4402003-07-30 08:40:37 +00001495 printf(_("Using default value %u\n"), i = dflt);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001496 if (i >= low && i <= high)
1497 break;
1498 else
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001499 printf(_("Value is out of range\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001500 }
1501 return i;
1502}
1503
Rob Landleyb73451d2006-02-24 16:29:00 +00001504static int
1505get_partition(int warn, int max)
1506{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001507 struct pte *pe;
1508 int i;
1509
1510 i = read_int(1, 0, max, 0, _("Partition number")) - 1;
1511 pe = &ptes[i];
1512
1513 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001514 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1515 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1516 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1517 ) {
1518 printf(_("Warning: partition %d has empty type\n"), i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001519 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001520 }
1521 return i;
1522}
1523
1524static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001525get_existing_partition(int warn, int max)
1526{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001527 int pno = -1;
1528 int i;
1529
1530 for (i = 0; i < max; i++) {
1531 struct pte *pe = &ptes[i];
1532 struct partition *p = pe->part_table;
1533
1534 if (p && !is_cleared_partition(p)) {
1535 if (pno >= 0)
1536 goto not_unique;
1537 pno = i;
1538 }
1539 }
1540 if (pno >= 0) {
1541 printf(_("Selected partition %d\n"), pno+1);
1542 return pno;
1543 }
1544 printf(_("No partition is defined yet!\n"));
1545 return -1;
1546
1547 not_unique:
1548 return get_partition(warn, max);
1549}
1550
1551static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001552get_nonexisting_partition(int warn, int max)
1553{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554 int pno = -1;
1555 int i;
1556
1557 for (i = 0; i < max; i++) {
1558 struct pte *pe = &ptes[i];
1559 struct partition *p = pe->part_table;
1560
1561 if (p && is_cleared_partition(p)) {
1562 if (pno >= 0)
1563 goto not_unique;
1564 pno = i;
1565 }
1566 }
1567 if (pno >= 0) {
1568 printf(_("Selected partition %d\n"), pno+1);
1569 return pno;
1570 }
1571 printf(_("All primary partitions have been defined already!\n"));
1572 return -1;
1573
1574 not_unique:
1575 return get_partition(warn, max);
1576}
1577
1578
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001579static void
1580change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001581{
1582 display_in_cyl_units = !display_in_cyl_units;
1583 update_units();
1584 printf(_("Changing display/entry units to %s\n"),
1585 str_units(PLURAL));
1586}
1587
1588static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001589toggle_active(int i)
1590{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001591 struct pte *pe = &ptes[i];
1592 struct partition *p = pe->part_table;
1593
Rob Landleyb73451d2006-02-24 16:29:00 +00001594 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001595 printf(_("WARNING: Partition %d is an extended partition\n"), i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1597 pe->changed = 1;
1598}
1599
1600static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001601toggle_dos_compatibility_flag(void)
1602{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603 dos_compatible_flag = ~dos_compatible_flag;
1604 if (dos_compatible_flag) {
1605 sector_offset = sectors;
1606 printf(_("DOS Compatibility flag is set\n"));
1607 }
1608 else {
1609 sector_offset = 1;
1610 printf(_("DOS Compatibility flag is not set\n"));
1611 }
1612}
1613
1614static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001615delete_partition(int i)
1616{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001617 struct pte *pe = &ptes[i];
1618 struct partition *p = pe->part_table;
1619 struct partition *q = pe->ext_pointer;
1620
1621/* Note that for the fifth partition (i == 4) we don't actually
1622 * decrement partitions.
1623 */
1624
1625 if (warn_geometry())
1626 return; /* C/H/S not set */
1627 pe->changed = 1;
1628
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001629 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001630 sun_delete_partition(i);
1631 return;
1632 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001633 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001634 sgi_delete_partition(i);
1635 return;
1636 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001637
1638 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001639 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001640 partitions = 4;
1641 ptes[ext_index].ext_pointer = NULL;
1642 extended_offset = 0;
1643 }
1644 clear_partition(p);
1645 return;
1646 }
1647
1648 if (!q->sys_ind && i > 4) {
1649 /* the last one in the chain - just delete */
1650 --partitions;
1651 --i;
1652 clear_partition(ptes[i].ext_pointer);
1653 ptes[i].changed = 1;
1654 } else {
1655 /* not the last one - further ones will be moved down */
1656 if (i > 4) {
1657 /* delete this link in the chain */
1658 p = ptes[i-1].ext_pointer;
1659 *p = *q;
1660 set_start_sect(p, get_start_sect(q));
1661 set_nr_sects(p, get_nr_sects(q));
1662 ptes[i-1].changed = 1;
1663 } else if (partitions > 5) { /* 5 will be moved to 4 */
1664 /* the first logical in a longer chain */
1665 pe = &ptes[5];
1666
1667 if (pe->part_table) /* prevent SEGFAULT */
1668 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001669 get_partition_start(pe) -
1670 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001671 pe->offset = extended_offset;
1672 pe->changed = 1;
1673 }
1674
1675 if (partitions > 5) {
1676 partitions--;
1677 while (i < partitions) {
1678 ptes[i] = ptes[i+1];
1679 i++;
1680 }
1681 } else
1682 /* the only logical: clear only */
1683 clear_partition(ptes[i].part_table);
1684 }
1685}
1686
1687static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001688change_sysid(void)
1689{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001690 int i, sys, origsys;
1691 struct partition *p;
1692
Eric Andersen040f4402003-07-30 08:40:37 +00001693 /* If sgi_label then don't use get_existing_partition,
1694 let the user select a partition, since get_existing_partition()
1695 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001696 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001697 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001698 } else {
1699 i = get_partition(0, partitions);
1700 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001701 if (i == -1)
1702 return;
1703 p = ptes[i].part_table;
1704 origsys = sys = get_sysid(i);
1705
1706 /* if changing types T to 0 is allowed, then
1707 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001708 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001709 printf(_("Partition %d does not exist yet!\n"), i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001710 return;
1711 }
1712 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001713 sys = read_hex (get_sys_types());
1714
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001715 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001716 printf(_("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001717 "(but not to Linux). Having partitions of\n"
1718 "type 0 is probably unwise. You can delete\n"
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001719 "a partition using the 'd' command.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001720 /* break; */
1721 }
1722
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001723 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001724 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001725 printf(_("You cannot change a partition into"
Rob Landleyb73451d2006-02-24 16:29:00 +00001726 " an extended one or vice versa\n"
1727 "Delete it first.\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001728 break;
1729 }
1730 }
1731
1732 if (sys < 256) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001733 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001734 printf(_("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001735 "as Whole disk (5),\n"
1736 "as SunOS/Solaris expects it and "
1737 "even Linux likes it.\n\n"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001738 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001739 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001740 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001741 (i == 8 && sys != 0)
1742 )
1743 ){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001744 printf(_("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001745 "as volume header (0),\nand "
1746 "partition 11 as entire volume (6)"
1747 "as IRIX expects it.\n\n"));
Rob Landley5527b912006-02-25 03:46:10 +00001748 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001749 if (sys == origsys)
1750 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001751 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001752 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001753 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001754 sgi_change_sysid(i, sys);
1755 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001757
Rob Landleyb73451d2006-02-24 16:29:00 +00001758 printf(_("Changed system type of partition %d "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 "to %x (%s)\n"), i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001760 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761 ptes[i].changed = 1;
1762 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001763 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001764 dos_changed = 1;
1765 break;
1766 }
1767 }
1768}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001769#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1770
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001771
1772/* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993,
1773 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1774 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1775 * Lubkin Oct. 1991). */
1776
Rob Landleyb73451d2006-02-24 16:29:00 +00001777static void
1778long2chs(ulong ls, uint *c, uint *h, uint *s)
1779{
1780 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001781
1782 *c = ls / spc;
1783 ls = ls % spc;
1784 *h = ls / sectors;
1785 *s = ls % sectors + 1; /* sectors count from 1 */
1786}
1787
Rob Landleyb73451d2006-02-24 16:29:00 +00001788static void
1789check_consistency(const struct partition *p, int partition)
1790{
1791 uint pbc, pbh, pbs; /* physical beginning c, h, s */
1792 uint pec, peh, pes; /* physical ending c, h, s */
1793 uint lbc, lbh, lbs; /* logical beginning c, h, s */
1794 uint lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001795
1796 if (!heads || !sectors || (partition >= 4))
1797 return; /* do not check extended partitions */
1798
1799/* physical beginning c, h, s */
1800 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1801 pbh = p->head;
1802 pbs = p->sector & 0x3f;
1803
1804/* physical ending c, h, s */
1805 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1806 peh = p->end_head;
1807 pes = p->end_sector & 0x3f;
1808
1809/* compute logical beginning (c, h, s) */
1810 long2chs(get_start_sect(p), &lbc, &lbh, &lbs);
1811
1812/* compute logical ending (c, h, s) */
1813 long2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
1814
1815/* Same physical / logical beginning? */
1816 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
1817 printf(_("Partition %d has different physical/logical "
1818 "beginnings (non-Linux?):\n"), partition + 1);
1819 printf(_(" phys=(%d, %d, %d) "), pbc, pbh, pbs);
1820 printf(_("logical=(%d, %d, %d)\n"),lbc, lbh, lbs);
1821 }
1822
1823/* Same physical / logical ending? */
1824 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
1825 printf(_("Partition %d has different physical/logical "
1826 "endings:\n"), partition + 1);
1827 printf(_(" phys=(%d, %d, %d) "), pec, peh, pes);
1828 printf(_("logical=(%d, %d, %d)\n"),lec, leh, les);
1829 }
1830
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001831/* Ending on cylinder boundary? */
1832 if (peh != (heads - 1) || pes != sectors) {
Eric Andersen84bdea82004-05-19 10:49:17 +00001833 printf(_("Partition %i does not end on cylinder boundary.\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001834 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835 }
1836}
1837
1838static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001839list_disk_geometry(void)
1840{
Eric Andersen040f4402003-07-30 08:40:37 +00001841 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001842 long megabytes = bytes/1000000;
1843
1844 if (megabytes < 10000)
1845 printf(_("\nDisk %s: %ld MB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001846 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001847 else
1848 printf(_("\nDisk %s: %ld.%ld GB, %lld bytes\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001849 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001850 printf(_("%d heads, %d sectors/track, %d cylinders"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001851 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001852 if (units_per_sector == 1)
Eric Andersen040f4402003-07-30 08:40:37 +00001853 printf(_(", total %llu sectors"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001854 total_number_of_sectors / (sector_size/512));
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001855 printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00001856 str_units(PLURAL),
1857 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001858}
1859
1860/*
1861 * Check whether partition entries are ordered by their starting positions.
1862 * Return 0 if OK. Return i if partition i should have been earlier.
1863 * Two separate checks: primary and logical partitions.
1864 */
1865static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001866wrong_p_order(int *prev)
1867{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001868 const struct pte *pe;
1869 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001870 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001871 int i, last_i = 0;
1872
1873 for (i = 0 ; i < partitions; i++) {
1874 if (i == 4) {
1875 last_i = 4;
1876 last_p_start_pos = 0;
1877 }
1878 pe = &ptes[i];
1879 if ((p = pe->part_table)->sys_ind) {
1880 p_start_pos = get_partition_start(pe);
1881
1882 if (last_p_start_pos > p_start_pos) {
1883 if (prev)
1884 *prev = last_i;
1885 return i;
1886 }
1887
1888 last_p_start_pos = p_start_pos;
1889 last_i = i;
1890 }
1891 }
1892 return 0;
1893}
1894
1895#ifdef CONFIG_FEATURE_FDISK_ADVANCED
1896/*
1897 * Fix the chain of logicals.
1898 * extended_offset is unchanged, the set of sectors used is unchanged
1899 * The chain is sorted so that sectors increase, and so that
1900 * starting sectors increase.
1901 *
1902 * After this it may still be that cfdisk doesnt like the table.
1903 * (This is because cfdisk considers expanded parts, from link to
1904 * end of partition, and these may still overlap.)
1905 * Now
1906 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1907 * may help.
1908 */
1909static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001910fix_chain_of_logicals(void)
1911{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001912 int j, oj, ojj, sj, sjj;
1913 struct partition *pj,*pjj,tmp;
1914
1915 /* Stage 1: sort sectors but leave sector of part 4 */
1916 /* (Its sector is the global extended_offset.) */
1917 stage1:
1918 for (j = 5; j < partitions-1; j++) {
1919 oj = ptes[j].offset;
1920 ojj = ptes[j+1].offset;
1921 if (oj > ojj) {
1922 ptes[j].offset = ojj;
1923 ptes[j+1].offset = oj;
1924 pj = ptes[j].part_table;
1925 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1926 pjj = ptes[j+1].part_table;
1927 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1928 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001929 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001931 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001932 goto stage1;
1933 }
1934 }
1935
1936 /* Stage 2: sort starting sectors */
1937 stage2:
1938 for (j = 4; j < partitions-1; j++) {
1939 pj = ptes[j].part_table;
1940 pjj = ptes[j+1].part_table;
1941 sj = get_start_sect(pj);
1942 sjj = get_start_sect(pjj);
1943 oj = ptes[j].offset;
1944 ojj = ptes[j+1].offset;
1945 if (oj+sj > ojj+sjj) {
1946 tmp = *pj;
1947 *pj = *pjj;
1948 *pjj = tmp;
1949 set_start_sect(pj, ojj+sjj-oj);
1950 set_start_sect(pjj, oj+sj-ojj);
1951 goto stage2;
1952 }
1953 }
1954
1955 /* Probably something was changed */
1956 for (j = 4; j < partitions; j++)
1957 ptes[j].changed = 1;
1958}
1959
1960
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001961static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001962fix_partition_table_order(void)
1963{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001964 struct pte *pei, *pek;
1965 int i,k;
1966
1967 if (!wrong_p_order(NULL)) {
1968 printf(_("Nothing to do. Ordering is correct already.\n\n"));
1969 return;
1970 }
1971
1972 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1973 /* partition i should have come earlier, move it */
1974 /* We have to move data in the MBR */
1975 struct partition *pi, *pk, *pe, pbuf;
1976 pei = &ptes[i];
1977 pek = &ptes[k];
1978
1979 pe = pei->ext_pointer;
1980 pei->ext_pointer = pek->ext_pointer;
1981 pek->ext_pointer = pe;
1982
1983 pi = pei->part_table;
1984 pk = pek->part_table;
1985
1986 memmove(&pbuf, pi, sizeof(struct partition));
1987 memmove(pi, pk, sizeof(struct partition));
1988 memmove(pk, &pbuf, sizeof(struct partition));
1989
1990 pei->changed = pek->changed = 1;
1991 }
1992
1993 if (i)
1994 fix_chain_of_logicals();
1995
1996 printf("Done.\n");
1997
1998}
1999#endif
2000
2001static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002002list_table(int xtra)
2003{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002004 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 int i, w;
2006
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002007 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002008 sun_list_table(xtra);
2009 return;
2010 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002011 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012 sgi_list_table(xtra);
2013 return;
2014 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002015
2016 list_disk_geometry();
2017
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002018 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002019 xbsd_print_disklabel(xtra);
2020 return;
2021 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002022
2023 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2024 but if the device name ends in a digit, say /dev/foo1,
2025 then the partition is called /dev/foo1p3. */
2026 w = strlen(disk_device);
2027 if (w && isdigit(disk_device[w-1]))
2028 w++;
2029 if (w < 5)
2030 w = 5;
2031
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002032 // 1 12345678901 12345678901 12345678901 12
2033 printf(_("%*s Boot Start End Blocks Id System\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002034 w+1, _("Device"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002035
2036 for (i = 0; i < partitions; i++) {
2037 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002038 off_t psects;
2039 off_t pblocks;
2040 unsigned int podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002041
2042 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002043 if (!p || is_cleared_partition(p))
2044 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002046 psects = get_nr_sects(p);
2047 pblocks = psects;
2048 podd = 0;
2049
2050 if (sector_size < 1024) {
2051 pblocks /= (1024 / sector_size);
2052 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002054 if (sector_size > 1024)
2055 pblocks *= (sector_size / 1024);
2056
2057 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2058 partname(disk_device, i+1, w+2),
2059 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2060 ? '*' : '?',
2061 (unsigned long long) cround(get_partition_start(pe)), /* start */
2062 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2063 - (psects ? 1 : 0)),
2064 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2065 p->sys_ind, /* type id */
2066 partition_type(p->sys_ind)); /* type name */
2067
2068 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002069 }
2070
2071 /* Is partition table in disk order? It need not be, but... */
2072 /* partition table entries are not checked for correct order if this
2073 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002074 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002075 /* FIXME */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002076 printf(_("\nPartition table entries are not in disk order\n"));
2077 }
2078}
2079
2080#ifdef CONFIG_FEATURE_FDISK_ADVANCED
2081static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002082x_list_table(int extend)
2083{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002084 const struct pte *pe;
2085 const struct partition *p;
2086 int i;
2087
2088 printf(_("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n"),
2089 disk_device, heads, sectors, cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002090 printf(_("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002091 for (i = 0 ; i < partitions; i++) {
2092 pe = &ptes[i];
2093 p = (extend ? pe->ext_pointer : pe->part_table);
2094 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002095 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002096 i + 1, p->boot_ind, p->head,
2097 sector(p->sector),
2098 cylinder(p->sector, p->cyl), p->end_head,
2099 sector(p->end_sector),
2100 cylinder(p->end_sector, p->end_cyl),
2101 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2102 if (p->sys_ind)
2103 check_consistency(p, i);
2104 }
2105 }
2106}
2107#endif
2108
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002109#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002110static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002111fill_bounds(off_t *first, off_t *last)
2112{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002113 int i;
2114 const struct pte *pe = &ptes[0];
2115 const struct partition *p;
2116
2117 for (i = 0; i < partitions; pe++,i++) {
2118 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002119 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002120 first[i] = 0xffffffff;
2121 last[i] = 0;
2122 } else {
2123 first[i] = get_partition_start(pe);
2124 last[i] = first[i] + get_nr_sects(p) - 1;
2125 }
2126 }
2127}
2128
2129static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002130check(int n, uint h, uint s, uint c, off_t start)
2131{
Eric Andersend9261492004-06-28 23:50:31 +00002132 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133
2134 real_s = sector(s) - 1;
2135 real_c = cylinder(s, c);
2136 total = (real_c * sectors + real_s) * heads + h;
2137 if (!total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002138 printf(_("Warning: partition %d contains sector 0\n"), n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002139 if (h >= heads)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002140 printf(_("Partition %d: head %d greater than maximum %d\n"),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002141 n, h + 1, heads);
2142 if (real_s >= sectors)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002143 printf(_("Partition %d: sector %d greater than "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 "maximum %d\n"), n, s, sectors);
2145 if (real_c >= cylinders)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002146 printf(_("Partitions %d: cylinder %llu greater than "
Eric Andersend9261492004-06-28 23:50:31 +00002147 "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148 if (cylinders <= 1024 && start != total)
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002149 printf(_("Partition %d: previous sectors %llu disagrees with "
Eric Andersend9261492004-06-28 23:50:31 +00002150 "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002151}
2152
2153static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002154verify(void)
2155{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156 int i, j;
2157 uint total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002158 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002159 struct partition *p;
2160
2161 if (warn_geometry())
2162 return;
2163
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002164 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002165 verify_sun();
2166 return;
2167 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002168 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002169 verify_sgi(1);
2170 return;
2171 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002172
2173 fill_bounds(first, last);
2174 for (i = 0; i < partitions; i++) {
2175 struct pte *pe = &ptes[i];
2176
2177 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002178 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002179 check_consistency(p, i);
2180 if (get_partition_start(pe) < first[i])
2181 printf(_("Warning: bad start-of-data in "
2182 "partition %d\n"), i + 1);
2183 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2184 last[i]);
2185 total += last[i] + 1 - first[i];
2186 for (j = 0; j < i; j++)
2187 if ((first[i] >= first[j] && first[i] <= last[j])
2188 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2189 printf(_("Warning: partition %d overlaps "
2190 "partition %d.\n"), j + 1, i + 1);
2191 total += first[i] >= first[j] ?
2192 first[i] : first[j];
2193 total -= last[i] <= last[j] ?
2194 last[i] : last[j];
2195 }
2196 }
2197 }
2198
2199 if (extended_offset) {
2200 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002201 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202 get_nr_sects(pex->part_table) - 1;
2203
2204 for (i = 4; i < partitions; i++) {
2205 total++;
2206 p = ptes[i].part_table;
2207 if (!p->sys_ind) {
2208 if (i != 4 || i + 1 < partitions)
2209 printf(_("Warning: partition %d "
2210 "is empty\n"), i + 1);
2211 }
2212 else if (first[i] < extended_offset ||
2213 last[i] > e_last)
2214 printf(_("Logical partition %d not entirely in "
2215 "partition %d\n"), i + 1, ext_index + 1);
2216 }
2217 }
2218
2219 if (total > heads * sectors * cylinders)
2220 printf(_("Total allocated sectors %d greater than the maximum "
2221 "%d\n"), total, heads * sectors * cylinders);
2222 else if ((total = heads * sectors * cylinders - total) != 0)
2223 printf(_("%d unallocated sectors\n"), total);
2224}
2225
2226static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002227add_partition(int n, int sys)
2228{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002229 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002230 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002231 struct partition *p = ptes[n].part_table;
2232 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002233 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002234 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002235 first[partitions], last[partitions];
2236
2237 if (p && p->sys_ind) {
2238 printf(_("Partition %d is already defined. Delete "
2239 "it before re-adding it.\n"), n + 1);
2240 return;
2241 }
2242 fill_bounds(first, last);
2243 if (n < 4) {
2244 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002245 if (display_in_cyl_units || !total_number_of_sectors)
2246 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002247 else
Eric Andersen040f4402003-07-30 08:40:37 +00002248 llimit = total_number_of_sectors - 1;
2249 limit = llimit;
2250 if (limit != llimit)
2251 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252 if (extended_offset) {
2253 first[ext_index] = extended_offset;
2254 last[ext_index] = get_start_sect(q) +
2255 get_nr_sects(q) - 1;
2256 }
2257 } else {
2258 start = extended_offset + sector_offset;
2259 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2260 }
2261 if (display_in_cyl_units)
2262 for (i = 0; i < partitions; i++)
2263 first[i] = (cround(first[i]) - 1) * units_per_sector;
2264
2265 snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
2266 do {
2267 temp = start;
2268 for (i = 0; i < partitions; i++) {
2269 int lastplusoff;
2270
2271 if (start == ptes[i].offset)
2272 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002273 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002274 if (start >= first[i] && start <= lastplusoff)
2275 start = lastplusoff + 1;
2276 }
2277 if (start > limit)
2278 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002279 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkocf30cc82006-11-24 14:53:18 +00002280 printf(_("Sector %"OFF_FMT"d is already allocated\n"), temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002282 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002283 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002284 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002285 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002286
2287 saved_start = start;
2288 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2289 0, mesg);
2290 if (display_in_cyl_units) {
2291 start = (start - 1) * units_per_sector;
2292 if (start < saved_start) start = saved_start;
2293 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002294 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002296 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002297 if (n > 4) { /* NOT for fifth partition */
2298 struct pte *pe = &ptes[n];
2299
2300 pe->offset = start - sector_offset;
2301 if (pe->offset == extended_offset) { /* must be corrected */
2302 pe->offset++;
2303 if (sector_offset == 1)
2304 start++;
2305 }
2306 }
2307
2308 for (i = 0; i < partitions; i++) {
2309 struct pte *pe = &ptes[i];
2310
2311 if (start < pe->offset && limit >= pe->offset)
2312 limit = pe->offset - 1;
2313 if (start < first[i] && limit >= first[i])
2314 limit = first[i] - 1;
2315 }
2316 if (start > limit) {
2317 printf(_("No free sectors available\n"));
2318 if (n > 4)
2319 partitions--;
2320 return;
2321 }
2322 if (cround(start) == cround(limit)) {
2323 stop = limit;
2324 } else {
2325 snprintf(mesg, sizeof(mesg),
2326 _("Last %s or +size or +sizeM or +sizeK"),
2327 str_units(SINGULAR));
2328 stop = read_int(cround(start), cround(limit), cround(limit),
2329 cround(start), mesg);
2330 if (display_in_cyl_units) {
2331 stop = stop * units_per_sector - 1;
2332 if (stop >limit)
2333 stop = limit;
2334 }
2335 }
2336
2337 set_partition(n, 0, start, stop, sys);
2338 if (n > 4)
2339 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2340
Rob Landleyb73451d2006-02-24 16:29:00 +00002341 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002342 struct pte *pe4 = &ptes[4];
2343 struct pte *pen = &ptes[n];
2344
2345 ext_index = n;
2346 pen->ext_pointer = p;
2347 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002348 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002349 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2350 pe4->ext_pointer = pe4->part_table + 1;
2351 pe4->changed = 1;
2352 partitions = 5;
2353 }
2354}
2355
2356static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002357add_logical(void)
2358{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002359 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2360 struct pte *pe = &ptes[partitions];
2361
Rob Landley081e3842006-08-03 20:07:35 +00002362 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002363 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2364 pe->ext_pointer = pe->part_table + 1;
2365 pe->offset = 0;
2366 pe->changed = 1;
2367 partitions++;
2368 }
2369 add_partition(partitions - 1, LINUX_NATIVE);
2370}
2371
2372static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002373new_partition(void)
2374{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002375 int i, free_primary = 0;
2376
2377 if (warn_geometry())
2378 return;
2379
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002380 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2382 return;
2383 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002384 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002385 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2386 return;
2387 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002388 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002389 printf(_("\tSorry - this fdisk cannot handle AIX disk labels."
2390 "\n\tIf you want to add DOS-type partitions, create"
2391 "\n\ta new empty DOS partition table first. (Use o.)"
2392 "\n\tWARNING: "
2393 "This will destroy the present disk contents.\n"));
2394 return;
2395 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002396
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 for (i = 0; i < 4; i++)
2398 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002399
Rob Landleyb73451d2006-02-24 16:29:00 +00002400 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Eric Andersen84bdea82004-05-19 10:49:17 +00002401 printf(_("The maximum number of partitions has been created\n"));
2402 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002403 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002404
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 if (!free_primary) {
2406 if (extended_offset)
2407 add_logical();
2408 else
2409 printf(_("You must delete some partition and add "
2410 "an extended partition first\n"));
2411 } else {
2412 char c, line[LINE_LENGTH];
2413 snprintf(line, sizeof(line), "%s\n %s\n p primary "
2414 "partition (1-4)\n",
2415 "Command action", (extended_offset ?
2416 "l logical (5 or over)" : "e extended"));
2417 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002418 c = read_nonempty(line);
2419 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 i = get_nonexisting_partition(0, 4);
2421 if (i >= 0)
2422 add_partition(i, LINUX_NATIVE);
2423 return;
2424 }
2425 else if (c == 'l' && extended_offset) {
2426 add_logical();
2427 return;
2428 }
2429 else if (c == 'e' && !extended_offset) {
2430 i = get_nonexisting_partition(0, 4);
2431 if (i >= 0)
2432 add_partition(i, EXTENDED);
2433 return;
2434 }
2435 else
2436 printf(_("Invalid partition number "
Denis Vlasenko89f0b342006-11-18 22:04:09 +00002437 "for type '%c'\n"), c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002438 }
2439 }
2440}
2441
2442static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002443write_table(void)
2444{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002445 int i;
2446
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002447 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002448 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449 if (ptes[i].changed)
2450 ptes[3].changed = 1;
2451 for (i = 3; i < partitions; i++) {
2452 struct pte *pe = &ptes[i];
2453
2454 if (pe->changed) {
2455 write_part_table_flag(pe->sectorbuffer);
2456 write_sector(pe->offset, pe->sectorbuffer);
2457 }
2458 }
2459 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002460 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002461 /* no test on change? the printf below might be mistaken */
2462 sgi_write_table();
2463 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002464 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465 int needw = 0;
2466
Rob Landleyb73451d2006-02-24 16:29:00 +00002467 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002468 if (ptes[i].changed)
2469 needw = 1;
2470 if (needw)
2471 sun_write_table();
2472 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473
2474 printf(_("The partition table has been altered!\n\n"));
2475 reread_partition_table(1);
2476}
2477
Rob Landleyb73451d2006-02-24 16:29:00 +00002478static void
2479reread_partition_table(int leave)
2480{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002481 int error = 0;
2482 int i;
2483
2484 printf(_("Calling ioctl() to re-read partition table.\n"));
2485 sync();
2486 sleep(2);
2487 if ((i = ioctl(fd, BLKRRPART)) != 0) {
2488 error = errno;
2489 } else {
2490 /* some kernel versions (1.2.x) seem to have trouble
2491 rereading the partition table, but if asked to do it
2492 twice, the second time works. - biro@yggdrasil.com */
2493 sync();
2494 sleep(2);
2495 if ((i = ioctl(fd, BLKRRPART)) != 0)
2496 error = errno;
2497 }
2498
2499 if (i) {
2500 printf(_("\nWARNING: Re-reading the partition table "
2501 "failed with error %d: %s.\n"
2502 "The kernel still uses the old table.\n"
2503 "The new table will be used "
2504 "at the next reboot.\n"),
2505 error, strerror(error));
2506 }
2507
2508 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002509 printf(
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510 _("\nWARNING: If you have created or modified any DOS 6.x\n"
2511 "partitions, please see the fdisk manual page for additional\n"
2512 "information.\n"));
2513
2514 if (leave) {
2515 close(fd);
2516
2517 printf(_("Syncing disks.\n"));
2518 sync();
2519 sleep(4); /* for sync() */
2520 exit(!!i);
2521 }
2522}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002523#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002524
2525#ifdef CONFIG_FEATURE_FDISK_ADVANCED
2526#define MAX_PER_LINE 16
2527static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002528print_buffer(char *pbuffer)
2529{
2530 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002531
2532 for (i = 0, l = 0; i < sector_size; i++, l++) {
2533 if (l == 0)
2534 printf("0x%03X:", i);
2535 printf(" %02X", (unsigned char) pbuffer[i]);
2536 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002537 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538 l = -1;
2539 }
2540 }
2541 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002542 puts("");
2543 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002544}
2545
2546
2547static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002548print_raw(void)
2549{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002550 int i;
2551
2552 printf(_("Device: %s\n"), disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002553 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002555 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556 for (i = 3; i < partitions; i++)
2557 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002558 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559}
2560
2561static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002562move_begin(int i)
2563{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564 struct pte *pe = &ptes[i];
2565 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002566 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002567
2568 if (warn_geometry())
2569 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002570 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002571 printf(_("Partition %d has no data area\n"), i + 1);
2572 return;
2573 }
2574 first = get_partition_start(pe);
2575 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Rob Landleyb73451d2006-02-24 16:29:00 +00002576 _("New beginning of data")) - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577
2578 if (new != get_nr_sects(p)) {
2579 first = get_nr_sects(p) + get_start_sect(p) - new;
2580 set_nr_sects(p, first);
2581 set_start_sect(p, new);
2582 pe->changed = 1;
2583 }
2584}
2585
2586static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002587xselect(void)
2588{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 char c;
2590
Rob Landleyb73451d2006-02-24 16:29:00 +00002591 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002593 c = tolower(read_nonempty(_("Expert command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002594 switch (c) {
2595 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002596 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002598 break;
2599 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002600 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002601 move_begin(get_partition(0, partitions));
2602 break;
2603 case 'c':
2604 user_cylinders = cylinders =
2605 read_int(1, cylinders, 1048576, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002606 _("Number of cylinders"));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002609 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 warn_cylinders();
2611 break;
2612 case 'd':
2613 print_raw();
2614 break;
2615 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002616 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002618 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002620 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002621 x_list_table(1);
2622 break;
2623 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002624 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 fix_partition_table_order();
2626 break;
2627 case 'g':
2628#ifdef CONFIG_FEATURE_SGI_LABEL
2629 create_sgilabel();
2630#endif
2631 break;
2632 case 'h':
2633 user_heads = heads = read_int(1, heads, 256, 0,
Rob Landleyb73451d2006-02-24 16:29:00 +00002634 _("Number of heads"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002635 update_units();
2636 break;
2637 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002638 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002639 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002640 break;
2641 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002642 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002643 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 break;
2645 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002646 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002647 list_table(1);
2648 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002649 x_list_table(0);
2650 break;
2651 case 'q':
2652 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002653 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002654 exit(0);
2655 case 'r':
2656 return;
2657 case 's':
2658 user_sectors = sectors = read_int(1, sectors, 63, 0,
2659 _("Number of sectors"));
2660 if (dos_compatible_flag) {
2661 sector_offset = sectors;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002662 printf(_("Warning: setting sector offset for DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002663 "compatiblity\n"));
2664 }
2665 update_units();
2666 break;
2667 case 'v':
2668 verify();
2669 break;
2670 case 'w':
2671 write_table(); /* does not return */
2672 break;
2673 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002674 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002675 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002676 break;
2677 default:
2678 xmenu();
2679 }
2680 }
2681}
2682#endif /* ADVANCED mode */
2683
2684static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002685is_ide_cdrom_or_tape(const char *device)
2686{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002687 FILE *procf;
2688 char buf[100];
2689 struct stat statbuf;
2690 int is_ide = 0;
2691
2692 /* No device was given explicitly, and we are trying some
2693 likely things. But opening /dev/hdc may produce errors like
2694 "hdc: tray open or drive not ready"
2695 if it happens to be a CD-ROM drive. It even happens that
2696 the process hangs on the attempt to read a music CD.
2697 So try to be careful. This only works since 2.1.73. */
2698
2699 if (strncmp("/dev/hd", device, 7))
2700 return 0;
2701
2702 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2703 procf = fopen(buf, "r");
2704 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2705 is_ide = (!strncmp(buf, "cdrom", 5) ||
2706 !strncmp(buf, "tape", 4));
2707 else
2708 /* Now when this proc file does not exist, skip the
2709 device when it is read-only. */
2710 if (stat(device, &statbuf) == 0)
2711 is_ide = ((statbuf.st_mode & 0222) == 0);
2712
2713 if (procf)
2714 fclose(procf);
2715 return is_ide;
2716}
2717
Rob Landley5527b912006-02-25 03:46:10 +00002718
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002719static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002720try(const char *device, int user_specified)
2721{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002722 int gb;
2723
2724 disk_device = device;
2725 if (setjmp(listingbuf))
2726 return;
2727 if (!user_specified)
2728 if (is_ide_cdrom_or_tape(device))
2729 return;
2730 if ((fd = open(disk_device, type_open)) >= 0) {
2731 gb = get_boot(try_only);
2732 if (gb > 0) { /* I/O error */
2733 close(fd);
2734 } else if (gb < 0) { /* no DOS signature */
2735 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002736 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002737 return;
Rob Landley5527b912006-02-25 03:46:10 +00002738 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002739#ifdef CONFIG_FEATURE_OSF_LABEL
2740 if (btrydev(device) < 0)
2741#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002742 printf(_("Disk %s doesn't contain a valid "
Rob Landleyb73451d2006-02-24 16:29:00 +00002743 "partition table\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002744 close(fd);
2745 } else {
2746 close(fd);
2747 list_table(0);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002748#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002749 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002751 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002752#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002753 }
2754 } else {
2755 /* Ignore other errors, since we try IDE
2756 and SCSI hard disks which may not be
2757 installed on the system. */
2758 if (errno == EACCES) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002759 printf(_("Cannot open %s\n"), device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002760 return;
2761 }
2762 }
2763}
2764
2765/* for fdisk -l: try all things in /proc/partitions
2766 that look like a partition name (do not end in a digit) */
2767static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002768tryprocpt(void)
2769{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002770 FILE *procpt;
2771 char line[100], ptname[100], devname[120], *s;
2772 int ma, mi, sz;
2773
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002774 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002775
2776 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002777 if (sscanf(line, " %d %d %d %[^\n ]",
2778 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002779 continue;
2780 for (s = ptname; *s; s++);
2781 if (isdigit(s[-1]))
2782 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002783 sprintf(devname, "/dev/%s", ptname);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784 try(devname, 0);
2785 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002786#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002787 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002788#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002789}
2790
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002791#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002792static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002793unknown_command(int c)
2794{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002795 printf(_("%c: unknown command\n"), c);
2796}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002797#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002798
Rob Landleyb73451d2006-02-24 16:29:00 +00002799int fdisk_main(int argc, char **argv)
2800{
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002801 int c;
2802#ifdef CONFIG_FEATURE_FDISK_WRITABLE
2803 int optl = 0;
2804#endif
2805#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
2806 int opts = 0;
2807#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002808 /*
2809 * Calls:
2810 * fdisk -v
2811 * fdisk -l [-b sectorsize] [-u] device ...
2812 * fdisk -s [partition] ...
2813 * fdisk [-b sectorsize] [-u] device
2814 *
2815 * Options -C, -H, -S set the geometry.
2816 *
2817 */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002818 while ((c = getopt(argc, argv, "b:C:H:lS:u"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002819#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
2820 "s"
2821#endif
Denis Vlasenko13858992006-10-08 12:49:22 +00002822 )) != -1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002823 switch (c) {
2824 case 'b':
2825 /* Ugly: this sector size is really per device,
2826 so cannot be combined with multiple disks,
2827 and te same goes for the C/H/S options.
2828 */
Denis Vlasenko13858992006-10-08 12:49:22 +00002829 sector_size = xatoi_u(optarg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002830 if (sector_size != 512 && sector_size != 1024 &&
Rob Landleyb73451d2006-02-24 16:29:00 +00002831 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002832 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 sector_offset = 2;
2834 user_set_sector_size = 1;
2835 break;
2836 case 'C':
Denis Vlasenko13858992006-10-08 12:49:22 +00002837 user_cylinders = xatoi_u(optarg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002838 break;
2839 case 'H':
Denis Vlasenko13858992006-10-08 12:49:22 +00002840 user_heads = xatoi_u(optarg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841 if (user_heads <= 0 || user_heads >= 256)
2842 user_heads = 0;
2843 break;
2844 case 'S':
Denis Vlasenko13858992006-10-08 12:49:22 +00002845 user_sectors = xatoi_u(optarg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846 if (user_sectors <= 0 || user_sectors >= 64)
2847 user_sectors = 0;
2848 break;
2849 case 'l':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002850#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 optl = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002852#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 break;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002854#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855 case 's':
2856 opts = 1;
2857 break;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002858#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859 case 'u':
2860 display_in_cyl_units = 0;
2861 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00002863 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 }
2865 }
2866
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 if (user_set_sector_size && argc-optind != 1)
2868 printf(_("Warning: the -b (set sector size) option should"
2869 " be used with one specified device\n"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002870
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002871#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 if (optl) {
2873 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002874#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875 type_open = O_RDONLY;
2876 if (argc > optind) {
2877 int k;
2878#if __GNUC__
2879 /* avoid gcc warning:
2880 variable `k' might be clobbered by `longjmp' */
2881 (void)&k;
2882#endif
2883 listing = 1;
Rob Landleyb73451d2006-02-24 16:29:00 +00002884 for (k = optind; k < argc; k++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885 try(argv[k], 1);
2886 } else {
2887 /* we no longer have default device names */
2888 /* but, we can use /proc/partitions instead */
2889 tryprocpt();
2890 }
2891 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002892#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002894#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002896#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002897 if (opts) {
2898 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002899 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002900
2901 nowarn = 1;
2902 type_open = O_RDONLY;
2903
2904 opts = argc - optind;
2905 if (opts <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002906 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907
2908 for (j = optind; j < argc; j++) {
2909 disk_device = argv[j];
2910 if ((fd = open(disk_device, type_open)) < 0)
2911 fdisk_fatal(unable_to_open);
2912 if (ioctl(fd, BLKGETSIZE, &size))
2913 fdisk_fatal(ioctl_error);
2914 close(fd);
2915 if (opts == 1)
2916 printf("%ld\n", size/2);
2917 else
2918 printf("%s: %ld\n", argv[j], size/2);
2919 }
2920 return 0;
2921 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002922#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002924#ifdef CONFIG_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925 if (argc-optind == 1)
2926 disk_device = argv[optind];
2927 else
Manuel Novoa III cad53642003-03-19 09:13:01 +00002928 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002929
2930 get_boot(fdisk);
2931
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 /* OSF label, and no DOS label */
2934 printf(_("Detected an OSF/1 disklabel on %s, entering "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002935 "disklabel mode.\n"), disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002936 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002937 /*Why do we do this? It seems to be counter-intuitive*/
2938 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002939 /* If we return we may want to make an empty DOS label? */
2940 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002941
2942 while (1) {
2943 putchar('\n');
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002944 c = tolower(read_nonempty(_("Command (m for help): ")));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002945 switch (c) {
2946 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002947 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002948 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002949 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950 toggle_sunflags(get_partition(1, partitions),
2951 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002952 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002953 sgi_set_bootpartition(
2954 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002955 else
2956 unknown_command(c);
2957 break;
2958 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002959 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 printf(_("\nThe current boot file is: %s\n"),
Rob Landleyb73451d2006-02-24 16:29:00 +00002961 sgi_get_bootfile());
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002962 if (read_maybe_empty(_("Please enter the name of the "
Rob Landleyb73451d2006-02-24 16:29:00 +00002963 "new boot file: ")) == '\n')
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002964 printf(_("Boot file unchanged\n"));
2965 else
2966 sgi_set_bootfile(line_ptr);
2967 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002968#ifdef CONFIG_FEATURE_OSF_LABEL
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002969 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002970#endif
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002971
2972/* BUG!? Think what will happen if !CONFIG_FEATURE_OSF_LABEL !!! */
2973
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002974 break;
2975 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002976 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002977 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002978 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002979 toggle_sunflags(get_partition(1, partitions),
2980 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002981 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 sgi_set_swappartition(
2983 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 else
2985 unknown_command(c);
2986 break;
2987 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002988 {
Eric Andersen040f4402003-07-30 08:40:37 +00002989 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002990 /* If sgi_label then don't use get_existing_partition,
2991 let the user select a partition, since
2992 get_existing_partition() only works for Linux-like
2993 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002994 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002995 j = get_existing_partition(1, partitions);
2996 } else {
2997 j = get_partition(1, partitions);
2998 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002999 if (j >= 0)
3000 delete_partition(j);
3001 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003002 break;
3003 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003004 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003005 create_sgiinfo();
3006 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003007 unknown_command(c);
3008 case 'l':
3009 list_types(get_sys_types());
3010 break;
3011 case 'm':
3012 menu();
3013 break;
3014 case 'n':
3015 new_partition();
3016 break;
3017 case 'o':
3018 create_doslabel();
3019 break;
3020 case 'p':
3021 list_table(0);
3022 break;
3023 case 'q':
3024 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00003025 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003026 return 0;
3027 case 's':
3028#ifdef CONFIG_FEATURE_SUN_LABEL
3029 create_sunlabel();
3030#endif
3031 break;
3032 case 't':
3033 change_sysid();
3034 break;
3035 case 'u':
3036 change_units();
3037 break;
3038 case 'v':
3039 verify();
3040 break;
3041 case 'w':
3042 write_table(); /* does not return */
3043 break;
3044#ifdef CONFIG_FEATURE_FDISK_ADVANCED
3045 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003046 if (LABEL_IS_SGI) {
3047 printf(_("\n\tSorry, no experts menu for SGI "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003048 "partition tables available.\n\n"));
3049 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003050 xselect();
3051 break;
3052#endif
3053 default:
3054 unknown_command(c);
3055 menu();
3056 }
3057 }
3058 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003059#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003060}