blob: f3c7ca4c632b9eed5005d57f2687ff5a532226fc [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000010#include <assert.h> /* assert */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000011#include "busybox.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000012
Denis Vlasenko834410a2006-11-29 12:00:28 +000013/* Looks like someone forgot to add this to config system */
14#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
15# define ENABLE_FEATURE_FDISK_BLKSIZE 0
16# define USE_FEATURE_FDISK_BLKSIZE(a)
17#endif
18
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000019#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
20
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000021#define DEFAULT_SECTOR_SIZE 512
22#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000023#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000024#define MAXIMUM_PARTS 60
25
26#define ACTIVE_FLAG 0x80
27
28#define EXTENDED 0x05
29#define WIN98_EXTENDED 0x0f
30#define LINUX_PARTITION 0x81
31#define LINUX_SWAP 0x82
32#define LINUX_NATIVE 0x83
33#define LINUX_EXTENDED 0x85
34#define LINUX_LVM 0x8e
35#define LINUX_RAID 0xfd
36
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000037struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000038 unsigned char heads;
39 unsigned char sectors;
40 unsigned short cylinders;
41 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042};
43
Denis Vlasenko98ae2162006-10-12 19:30:44 +000044#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000045
Denis Vlasenkobd852072007-03-19 14:43:38 +000046static const char msg_building_new_label[] =
47"Building a new %s. Changes will remain in memory only,\n"
48"until you decide to write them. After that the previous content\n"
49"won't be recoverable.\n\n";
50
51static const char msg_part_already_defined[] =
52"Partition %d is already defined, delete it before re-adding\n";
53
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000054
Denis Vlasenko834410a2006-11-29 12:00:28 +000055static unsigned sector_size = DEFAULT_SECTOR_SIZE;
56static unsigned user_set_sector_size;
57static unsigned sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000058
Denis Vlasenko834410a2006-11-29 12:00:28 +000059#if ENABLE_FEATURE_OSF_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +000060static int possibly_osf_label;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000061#endif
62
Denis Vlasenko834410a2006-11-29 12:00:28 +000063static unsigned heads, sectors, cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000064static void update_units(void);
65
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000066
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000067struct partition {
68 unsigned char boot_ind; /* 0x80 - active */
69 unsigned char head; /* starting head */
70 unsigned char sector; /* starting sector */
71 unsigned char cyl; /* starting cylinder */
72 unsigned char sys_ind; /* What partition type */
73 unsigned char end_head; /* end head */
74 unsigned char end_sector; /* end sector */
75 unsigned char end_cyl; /* end cylinder */
76 unsigned char start4[4]; /* starting sector counting from 0 */
77 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000078} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079
80enum failure {
81 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
82 unable_to_write
83};
84
Denis Vlasenko98ae2162006-10-12 19:30:44 +000085enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +000086 label_dos, label_sun, label_sgi, label_aix, label_osf
87};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000088
Denis Vlasenko98ae2162006-10-12 19:30:44 +000089#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000090
Denis Vlasenko834410a2006-11-29 12:00:28 +000091#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000092#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000093#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +000094#else
95#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000096#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +000097#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000098
Denis Vlasenko834410a2006-11-29 12:00:28 +000099#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000100#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000101#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000102#else
103#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000104#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000105#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000106
Denis Vlasenko834410a2006-11-29 12:00:28 +0000107#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000108#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000109#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000110#else
111#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000113#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000114
Denis Vlasenko834410a2006-11-29 12:00:28 +0000115#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000117#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000118#else
119#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000120#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000121#endif
Rob Landley5527b912006-02-25 03:46:10 +0000122
Rob Landleyb73451d2006-02-24 16:29:00 +0000123enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000124
Rob Landley5527b912006-02-25 03:46:10 +0000125static enum label_type current_label_type;
126
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000127static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000128static int fd; /* the disk */
129static int partitions = 4; /* maximum partition + 1 */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000130static int display_in_cyl_units = 1;
131static unsigned units_per_sector = 1;
132#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000133static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000134static void reread_partition_table(int leave);
135static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000136static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000137static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000138static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000139#endif
140static const char *partition_type(unsigned char type);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000141static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000142static void get_geometry(void);
143static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000144
145#define PLURAL 0
146#define SINGULAR 1
147
Denis Vlasenko28703012006-12-19 20:32:02 +0000148static unsigned get_start_sect(const struct partition *p);
149static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000150
151/*
152 * per partition table entry data
153 *
154 * The four primary partitions have the same sectorbuffer (MBRbuffer)
155 * and have NULL ext_pointer.
156 * Each logical partition table entry has two pointers, one for the
157 * partition and one link to the next one.
158 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000159struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000160 struct partition *part_table; /* points into sectorbuffer */
161 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000162 off_t offset; /* disk sector number */
163 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000164#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000165 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000166#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000167};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000168
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000169/* DOS partition types */
170
171static const char *const i386_sys_types[] = {
172 "\x00" "Empty",
173 "\x01" "FAT12",
174 "\x04" "FAT16 <32M",
175 "\x05" "Extended", /* DOS 3.3+ extended partition */
176 "\x06" "FAT16", /* DOS 16-bit >=32M */
177 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
178 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
179 "\x0b" "Win95 FAT32",
180 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
181 "\x0e" "Win95 FAT16 (LBA)",
182 "\x0f" "Win95 Ext'd (LBA)",
183 "\x11" "Hidden FAT12",
184 "\x12" "Compaq diagnostics",
185 "\x14" "Hidden FAT16 <32M",
186 "\x16" "Hidden FAT16",
187 "\x17" "Hidden HPFS/NTFS",
188 "\x1b" "Hidden Win95 FAT32",
189 "\x1c" "Hidden W95 FAT32 (LBA)",
190 "\x1e" "Hidden W95 FAT16 (LBA)",
191 "\x3c" "Part.Magic recovery",
192 "\x41" "PPC PReP Boot",
193 "\x42" "SFS",
194 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
195 "\x80" "Old Minix", /* Minix 1.4a and earlier */
196 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
197 "\x82" "Linux swap", /* also Solaris */
198 "\x83" "Linux",
199 "\x84" "OS/2 hidden C: drive",
200 "\x85" "Linux extended",
201 "\x86" "NTFS volume set",
202 "\x87" "NTFS volume set",
203 "\x8e" "Linux LVM",
204 "\x9f" "BSD/OS", /* BSDI */
205 "\xa0" "Thinkpad hibernation",
206 "\xa5" "FreeBSD", /* various BSD flavours */
207 "\xa6" "OpenBSD",
208 "\xa8" "Darwin UFS",
209 "\xa9" "NetBSD",
210 "\xab" "Darwin boot",
211 "\xb7" "BSDI fs",
212 "\xb8" "BSDI swap",
213 "\xbe" "Solaris boot",
214 "\xeb" "BeOS fs",
215 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
216 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
217 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
218 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
219 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
220 autodetect using persistent
221 superblock */
222#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
223 "\x02" "XENIX root",
224 "\x03" "XENIX usr",
225 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
226 "\x09" "AIX bootable", /* AIX data or Coherent */
227 "\x10" "OPUS",
228 "\x18" "AST SmartSleep",
229 "\x24" "NEC DOS",
230 "\x39" "Plan 9",
231 "\x40" "Venix 80286",
232 "\x4d" "QNX4.x",
233 "\x4e" "QNX4.x 2nd part",
234 "\x4f" "QNX4.x 3rd part",
235 "\x50" "OnTrack DM",
236 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
237 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
238 "\x53" "OnTrack DM6 Aux3",
239 "\x54" "OnTrackDM6",
240 "\x55" "EZ-Drive",
241 "\x56" "Golden Bow",
242 "\x5c" "Priam Edisk",
243 "\x61" "SpeedStor",
244 "\x64" "Novell Netware 286",
245 "\x65" "Novell Netware 386",
246 "\x70" "DiskSecure Multi-Boot",
247 "\x75" "PC/IX",
248 "\x93" "Amoeba",
249 "\x94" "Amoeba BBT", /* (bad block table) */
250 "\xa7" "NeXTSTEP",
251 "\xbb" "Boot Wizard hidden",
252 "\xc1" "DRDOS/sec (FAT-12)",
253 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
254 "\xc6" "DRDOS/sec (FAT-16)",
255 "\xc7" "Syrinx",
256 "\xda" "Non-FS data",
257 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
258 Concurrent DOS or CTOS */
259 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
260 "\xdf" "BootIt", /* BootIt EMBRM */
261 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
262 extended partition */
263 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
264 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
265 partition < 1024 cyl. */
266 "\xf1" "SpeedStor",
267 "\xf4" "SpeedStor", /* SpeedStor large partition */
268 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
269 "\xff" "BBT", /* Xenix Bad Block Table */
270#endif
271 NULL
272};
273
274
275/* Globals */
276
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000277struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000278 char *line_ptr;
279 char line_buffer[80];
280 char partname_buffer[80];
281 jmp_buf listingbuf;
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000282 /* Raw disk label. For DOS-type partition tables the MBR,
283 * with descriptions of the primary partitions. */
284 char MBRbuffer[MAX_SECTOR_SIZE];
285 /* Partition tables */
286 struct pte ptes[MAXIMUM_PARTS];
287};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000288/* bb_common_bufsiz1 is too small for this on 64 bit CPUs */
289#define G (*ptr_to_globals)
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000290
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000291#define line_ptr (G.line_ptr)
292#define listingbuf (G.listingbuf)
293#define line_buffer (G.line_buffer)
294#define partname_buffer (G.partname_buffer)
295#define MBRbuffer (G.MBRbuffer)
296#define ptes (G.ptes)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000297
Denis Vlasenkobd852072007-03-19 14:43:38 +0000298
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000299/* Code */
300
301#define IS_EXTENDED(i) \
302 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
303
304#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
305
306#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
307
308#define pt_offset(b, n) \
309 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
310
311#define sector(s) ((s) & 0x3f)
312
313#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
314
315#define hsc2sector(h,s,c) \
316 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
317
318#define set_hsc(h,s,c,sector) \
319 do { \
320 s = sector % sectors + 1; \
321 sector /= sectors; \
322 h = sector % heads; \
323 sector /= heads; \
324 c = sector & 0xff; \
325 s |= (sector >> 2) & 0xc0; \
326 } while (0)
327
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000328#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000329/* read line; return 0 or first printable char */
330static int
331read_line(const char *prompt)
332{
333 int sz;
334
335 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
336 if (sz <= 0)
337 exit(0); /* Ctrl-D or Ctrl-C */
338
339 if (line_buffer[sz-1] == '\n')
340 line_buffer[--sz] = '\0';
341
342 line_ptr = line_buffer;
343 while (*line_ptr && !isgraph(*line_ptr))
344 line_ptr++;
345 return *line_ptr;
346}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000347#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000348
Denis Vlasenkobd852072007-03-19 14:43:38 +0000349/*
350 * return partition name - uses static storage
351 */
352static const char *
353partname(const char *dev, int pno, int lth)
354{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000355 const char *p;
356 int w, wp;
357 int bufsiz;
358 char *bufp;
359
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000360 bufp = partname_buffer;
361 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000362
363 w = strlen(dev);
364 p = "";
365
366 if (isdigit(dev[w-1]))
367 p = "p";
368
369 /* devfs kludge - note: fdisk partition names are not supposed
370 to equal kernel names, so there is no reason to do this */
371 if (strcmp(dev + w - 4, "disc") == 0) {
372 w -= 4;
373 p = "part";
374 }
375
376 wp = strlen(p);
377
378 if (lth) {
379 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
380 lth-wp-2, w, dev, p, pno);
381 } else {
382 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
383 }
384 return bufp;
385}
386
Denis Vlasenko834410a2006-11-29 12:00:28 +0000387#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000388static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000389set_all_unchanged(void)
390{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000391 int i;
392
393 for (i = 0; i < MAXIMUM_PARTS; i++)
394 ptes[i].changed = 0;
395}
396
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000397static ATTRIBUTE_ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000398set_changed(int i)
399{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000400 ptes[i].changed = 1;
401}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000402#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000403
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000404static ATTRIBUTE_ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000405get_part_table(int i)
406{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000407 return ptes[i].part_table;
408}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000409
410static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000411str_units(int n)
412{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000413 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000414 return display_in_cyl_units ? "cylinder" : "sector";
415 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000416}
417
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000418static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000419valid_part_table_flag(const char *mbuffer)
420{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000421 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000422}
423
Denis Vlasenko834410a2006-11-29 12:00:28 +0000424#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000425static ATTRIBUTE_ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000426write_part_table_flag(char *b)
427{
428 b[510] = 0x55;
429 b[511] = 0xaa;
430}
431
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000432static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000433read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000434{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000435 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000436 return *line_ptr;
437}
438
439static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000440read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000441{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000442 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000443 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000444 line_ptr[0] = '\n';
445 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000446 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000447 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000448}
449
450static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000451read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000452{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000453 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000454 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000455 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000456 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000457 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000458 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000459 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000460 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000461 if (v > 0xff)
462 /* Bad input also triggers this */
463 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000464 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000465 }
466}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000467#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000468
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000469#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000470
471typedef struct {
472 unsigned char info[128]; /* Informative text string */
473 unsigned char spare0[14];
474 struct sun_info {
475 unsigned char spare1;
476 unsigned char id;
477 unsigned char spare2;
478 unsigned char flags;
479 } infos[8];
480 unsigned char spare1[246]; /* Boot information etc. */
481 unsigned short rspeed; /* Disk rotational speed */
482 unsigned short pcylcount; /* Physical cylinder count */
483 unsigned short sparecyl; /* extra sects per cylinder */
484 unsigned char spare2[4]; /* More magic... */
485 unsigned short ilfact; /* Interleave factor */
486 unsigned short ncyl; /* Data cylinder count */
487 unsigned short nacyl; /* Alt. cylinder count */
488 unsigned short ntrks; /* Tracks per cylinder */
489 unsigned short nsect; /* Sectors per track */
490 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000491 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000492 uint32_t start_cylinder;
493 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000494 } partitions[8];
495 unsigned short magic; /* Magic number */
496 unsigned short csum; /* Label xor'd checksum */
497} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000498#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000499STATIC_OSF void bsd_select(void);
500STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000501#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000502
Denis Vlasenko28703012006-12-19 20:32:02 +0000503#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000504static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000505fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000506{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000507 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000508}
509
Rob Landley88621d72006-08-29 19:41:06 +0000510static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000511fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000512{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000513 return (x << 24) |
514 ((x & 0xFF00) << 8) |
515 ((x & 0xFF0000) >> 8) |
516 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000517}
518#endif
519
Denis Vlasenkobd852072007-03-19 14:43:38 +0000520STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000521STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000522STATIC_SGI int sgi_get_sysid(int i);
523STATIC_SGI void sgi_delete_partition(int i);
524STATIC_SGI void sgi_change_sysid(int i, int sys);
525STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000526#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000527STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000528#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000529STATIC_SGI int verify_sgi(int verbose);
530STATIC_SGI void sgi_add_partition(int n, int sys);
531STATIC_SGI void sgi_set_swappartition(int i);
532STATIC_SGI const char *sgi_get_bootfile(void);
533STATIC_SGI void sgi_set_bootfile(const char* aFile);
534STATIC_SGI void create_sgiinfo(void);
535STATIC_SGI void sgi_write_table(void);
536STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000537#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000538
Denis Vlasenkobd852072007-03-19 14:43:38 +0000539STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000540STATIC_SUN void sun_delete_partition(int i);
541STATIC_SUN void sun_change_sysid(int i, int sys);
542STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000543STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000544#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000545STATIC_SUN void sun_set_alt_cyl(void);
546STATIC_SUN void sun_set_ncyl(int cyl);
547STATIC_SUN void sun_set_xcyl(void);
548STATIC_SUN void sun_set_ilfact(void);
549STATIC_SUN void sun_set_rspeed(void);
550STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000551#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000552STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
553STATIC_SUN void verify_sun(void);
554STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000555#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000556
Denis Vlasenko834410a2006-11-29 12:00:28 +0000557#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000558/* start_sect and nr_sects are stored little endian on all machines */
559/* moreover, they are not aligned correctly */
560static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000561store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000562{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000563 cp[0] = val;
564 cp[1] = val >> 8;
565 cp[2] = val >> 16;
566 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000567}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000568#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000569
Denis Vlasenko834410a2006-11-29 12:00:28 +0000570static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000571read4_little_endian(const unsigned char *cp)
572{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000573 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000574}
575
Denis Vlasenko834410a2006-11-29 12:00:28 +0000576#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000577static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000578set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000579{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000580 store4_little_endian(p->start4, start_sect);
581}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000582#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000583
Denis Vlasenko28703012006-12-19 20:32:02 +0000584static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000585get_start_sect(const struct partition *p)
586{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000587 return read4_little_endian(p->start4);
588}
589
Denis Vlasenko834410a2006-11-29 12:00:28 +0000590#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000591static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000592set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000593{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000594 store4_little_endian(p->size4, nr_sects);
595}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000596#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597
Denis Vlasenko28703012006-12-19 20:32:02 +0000598static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000599get_nr_sects(const struct partition *p)
600{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000601 return read4_little_endian(p->size4);
602}
603
604/* normally O_RDWR, -l option gives O_RDONLY */
605static int type_open = O_RDWR;
606
607
Rob Landleyb73451d2006-02-24 16:29:00 +0000608static int ext_index; /* the prime extended partition */
609static int listing; /* no aborts for fdisk -l */
610static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000611#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000612static int dos_changed;
613static int nowarn; /* no warnings for fdisk -l/-s */
614#endif
615
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000616
617
Denis Vlasenko834410a2006-11-29 12:00:28 +0000618static unsigned user_cylinders, user_heads, user_sectors;
619static unsigned pt_heads, pt_sectors;
620static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000621
Eric Andersend9261492004-06-28 23:50:31 +0000622static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000623
Eric Andersen040f4402003-07-30 08:40:37 +0000624static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000625
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 Vlasenkobd852072007-03-19 14:43:38 +0000638 message = "cannot open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000639 break;
640 case unable_to_read:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000641 message = "cannot read from %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000642 break;
643 case unable_to_seek:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000644 message = "cannot seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000645 break;
646 case unable_to_write:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000647 message = "cannot write to %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000648 break;
649 case ioctl_error:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000650 message = "BLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000651 break;
652 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000653 message = "fatal 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
Denis Vlasenko834410a2006-11-29 12:00:28 +0000667#if ENABLE_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;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000682 pe->sectorbuffer = 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);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000686#if ENABLE_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
Denis Vlasenko834410a2006-11-29 12:00:28 +0000692static unsigned
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
Denis Vlasenko834410a2006-11-29 12:00:28 +0000698#if ENABLE_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 Vlasenkobd852072007-03-19 14:43:38 +0000718 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000719 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000720 puts("a\ttoggle a read only flag"); /* sun */
721 puts("b\tedit bsd disklabel");
722 puts("c\ttoggle the mountable flag"); /* sun */
723 puts("d\tdelete a partition");
724 puts("l\tlist known partition types");
725 puts("n\tadd a new partition");
726 puts("o\tcreate a new empty DOS partition table");
727 puts("p\tprint the partition table");
728 puts("q\tquit without saving changes");
729 puts("s\tcreate a new empty Sun disklabel"); /* sun */
730 puts("t\tchange a partition's system id");
731 puts("u\tchange display/entry units");
732 puts("v\tverify the partition table");
733 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000734#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000735 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000736#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000737 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000738 puts("a\tselect bootable partition"); /* sgi flavour */
739 puts("b\tedit bootfile entry"); /* sgi */
740 puts("c\tselect sgi swap partition"); /* sgi flavour */
741 puts("d\tdelete a partition");
742 puts("l\tlist known partition types");
743 puts("n\tadd a new partition");
744 puts("o\tcreate a new empty DOS partition table");
745 puts("p\tprint the partition table");
746 puts("q\tquit without saving changes");
747 puts("s\tcreate a new empty Sun disklabel"); /* sun */
748 puts("t\tchange a partition's system id");
749 puts("u\tchange display/entry units");
750 puts("v\tverify the partition table");
751 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000752 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000753 puts("o\tcreate a new empty DOS partition table");
754 puts("q\tquit without saving changes");
755 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000756 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000757 puts("a\ttoggle a bootable flag");
758 puts("b\tedit bsd disklabel");
759 puts("c\ttoggle the dos compatibility flag");
760 puts("d\tdelete a partition");
761 puts("l\tlist known partition types");
762 puts("n\tadd a new partition");
763 puts("o\tcreate a new empty DOS partition table");
764 puts("p\tprint the partition table");
765 puts("q\tquit without saving changes");
766 puts("s\tcreate a new empty Sun disklabel"); /* sun */
767 puts("t\tchange a partition's system id");
768 puts("u\tchange display/entry units");
769 puts("v\tverify the partition table");
770 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000771#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000772 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000773#endif
774 }
775}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000776#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000777
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778
Denis Vlasenko834410a2006-11-29 12:00:28 +0000779#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000780static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000781xmenu(void)
782{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000783 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000784 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000785 puts("a\tchange number of alternate cylinders"); /*sun*/
786 puts("c\tchange number of cylinders");
787 puts("d\tprint the raw data in the partition table");
788 puts("e\tchange number of extra sectors per cylinder");/*sun*/
789 puts("h\tchange number of heads");
790 puts("i\tchange interleave factor"); /*sun*/
791 puts("o\tchange rotation speed (rpm)"); /*sun*/
792 puts("p\tprint the partition table");
793 puts("q\tquit without saving changes");
794 puts("r\treturn to main menu");
795 puts("s\tchange number of sectors/track");
796 puts("v\tverify the partition table");
797 puts("w\twrite table to disk and exit");
798 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000799 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000800 puts("b\tmove beginning of data in a partition"); /* !sun */
801 puts("c\tchange number of cylinders");
802 puts("d\tprint the raw data in the partition table");
803 puts("e\tlist extended partitions"); /* !sun */
804 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
805 puts("h\tchange number of heads");
806 puts("p\tprint the partition table");
807 puts("q\tquit without saving changes");
808 puts("r\treturn to main menu");
809 puts("s\tchange number of sectors/track");
810 puts("v\tverify the partition table");
811 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000812 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000813 puts("b\tmove beginning of data in a partition"); /* !sun */
814 puts("c\tchange number of cylinders");
815 puts("d\tprint the raw data in the partition table");
816 puts("e\tlist extended partitions"); /* !sun */
817 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
818 puts("h\tchange number of heads");
819 puts("p\tprint the partition table");
820 puts("q\tquit without saving changes");
821 puts("r\treturn to main menu");
822 puts("s\tchange number of sectors/track");
823 puts("v\tverify the partition table");
824 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000825 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000826 puts("b\tmove beginning of data in a partition"); /* !sun */
827 puts("c\tchange number of cylinders");
828 puts("d\tprint the raw data in the partition table");
829 puts("e\tlist extended partitions"); /* !sun */
830 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000831#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000833#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000834 puts("h\tchange number of heads");
835 puts("p\tprint the partition table");
836 puts("q\tquit without saving changes");
837 puts("r\treturn to main menu");
838 puts("s\tchange number of sectors/track");
839 puts("v\tverify the partition table");
840 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000841 }
842}
843#endif /* ADVANCED mode */
844
Denis Vlasenko834410a2006-11-29 12:00:28 +0000845#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000846static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000847get_sys_types(void)
848{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000849 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000850 LABEL_IS_SUN ? sun_sys_types :
851 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000852 i386_sys_types);
853}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000854#else
855#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000856#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000857
Denis Vlasenkobd852072007-03-19 14:43:38 +0000858static const char *
859partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000860{
861 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000862 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000863
Denis Vlasenkobd852072007-03-19 14:43:38 +0000864 for (i = 0; types[i]; i++)
865 if ((unsigned char)types[i][0] == type)
866 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000867
Denis Vlasenkobd852072007-03-19 14:43:38 +0000868 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000869}
870
871
Denis Vlasenko834410a2006-11-29 12:00:28 +0000872#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000873static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000874get_sysid(int i)
875{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000876 return LABEL_IS_SUN ? sunlabel->infos[i].id :
877 (LABEL_IS_SGI ? sgi_get_sysid(i) :
878 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000879}
880
Denis Vlasenkobd852072007-03-19 14:43:38 +0000881static void
882list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884 enum { COLS = 3 };
885
886 unsigned last[COLS];
887 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000888 int i;
889
Denis Vlasenkobd852072007-03-19 14:43:38 +0000890 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000891
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000892 done = 0;
893 for (i = COLS-1; i >= 0; i--) {
894 done += (size + i - done) / (i + 1);
895 last[COLS-1 - i] = done;
896 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000897
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000898 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000899 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000900 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000901 (unsigned char)sys[next][0],
902 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000903 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000904 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000905 i = 0;
906 next = ++done;
907 }
908 } while (done < last[0]);
909 putchar('\n');
910}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000911#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000912
913static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000914is_cleared_partition(const struct partition *p)
915{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000916 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
917 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
918 get_start_sect(p) || get_nr_sects(p));
919}
920
921static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000922clear_partition(struct partition *p)
923{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924 if (!p)
925 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000926 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000927}
928
Denis Vlasenko834410a2006-11-29 12:00:28 +0000929#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000930static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000931set_partition(int i, int doext, off_t start, off_t stop, int sysid)
932{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000934 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000935
936 if (doext) {
937 p = ptes[i].ext_pointer;
938 offset = extended_offset;
939 } else {
940 p = ptes[i].part_table;
941 offset = ptes[i].offset;
942 }
943 p->boot_ind = 0;
944 p->sys_ind = sysid;
945 set_start_sect(p, start - offset);
946 set_nr_sects(p, stop - start + 1);
947 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
948 start = heads*sectors*1024 - 1;
949 set_hsc(p->head, p->sector, p->cyl, start);
950 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
951 stop = heads*sectors*1024 - 1;
952 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
953 ptes[i].changed = 1;
954}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000955#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000956
957static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000958warn_geometry(void)
959{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000960 if (heads && sectors && cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000961 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000962
Denis Vlasenkobd852072007-03-19 14:43:38 +0000963 printf("Unknown value(s) for:");
964 if (!heads)
965 printf(" heads");
966 if (!sectors)
967 printf(" sectors");
968 if (!cylinders)
969 printf(" cylinders");
970 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000971#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000972 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000973#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000974 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000975 return 1;
976}
977
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000978static void
979update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980{
981 int cyl_units = heads * sectors;
982
983 if (display_in_cyl_units && cyl_units)
984 units_per_sector = cyl_units;
985 else
986 units_per_sector = 1; /* in sectors */
987}
988
Denis Vlasenko834410a2006-11-29 12:00:28 +0000989#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000991warn_cylinders(void)
992{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000993 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000994 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000995"The number of cylinders for this disk is set to %d.\n"
996"There is nothing wrong with that, but this is larger than 1024,\n"
997"and could in certain setups cause problems with:\n"
998"1) software that runs at boot time (e.g., old versions of LILO)\n"
999"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001000" (e.g., DOS FDISK, OS/2 FDISK)\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001001 cylinders);
1002}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001003#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001004
1005static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001006read_extended(int ext)
1007{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001008 int i;
1009 struct pte *pex;
1010 struct partition *p, *q;
1011
1012 ext_index = ext;
1013 pex = &ptes[ext];
1014 pex->ext_pointer = pex->part_table;
1015
1016 p = pex->part_table;
1017 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001018 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019 return;
1020 }
1021
Rob Landleyb73451d2006-02-24 16:29:00 +00001022 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001023 struct pte *pe = &ptes[partitions];
1024
1025 if (partitions >= MAXIMUM_PARTS) {
1026 /* This is not a Linux restriction, but
1027 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001028 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001029 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001030#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001031 printf("Warning: deleting partitions after %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001033 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001034#endif
1035 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001036 return;
1037 }
1038
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001039 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001040
1041 if (!extended_offset)
1042 extended_offset = get_start_sect(p);
1043
1044 q = p = pt_offset(pe->sectorbuffer, 0);
1045 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001046 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001047 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001049 "pointer in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001050 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051 else
1052 pe->ext_pointer = p;
1053 } else if (p->sys_ind) {
1054 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 "data in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058 else
1059 pe->part_table = p;
1060 }
1061 }
1062
1063 /* very strange code here... */
1064 if (!pe->part_table) {
1065 if (q != pe->ext_pointer)
1066 pe->part_table = q;
1067 else
1068 pe->part_table = q + 1;
1069 }
1070 if (!pe->ext_pointer) {
1071 if (q != pe->part_table)
1072 pe->ext_pointer = q;
1073 else
1074 pe->ext_pointer = q + 1;
1075 }
1076
1077 p = pe->ext_pointer;
1078 partitions++;
1079 }
1080
Denis Vlasenko834410a2006-11-29 12:00:28 +00001081#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001082 /* remove empty links */
1083 remove:
1084 for (i = 4; i < partitions; i++) {
1085 struct pte *pe = &ptes[i];
1086
Denis Vlasenkobd852072007-03-19 14:43:38 +00001087 if (!get_nr_sects(pe->part_table)
1088 && (partitions > 5 || ptes[4].part_table->sys_ind)
1089 ) {
1090 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001091 delete_partition(i);
1092 goto remove; /* numbering changed */
1093 }
1094 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001095#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001096}
1097
Denis Vlasenko834410a2006-11-29 12:00:28 +00001098#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001099static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001100create_doslabel(void)
1101{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001102 int i;
1103
Denis Vlasenkobd852072007-03-19 14:43:38 +00001104 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001105
1106 current_label_type = label_dos;
1107
Denis Vlasenko834410a2006-11-29 12:00:28 +00001108#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109 possibly_osf_label = 0;
1110#endif
1111 partitions = 4;
1112
1113 for (i = 510-64; i < 510; i++)
1114 MBRbuffer[i] = 0;
1115 write_part_table_flag(MBRbuffer);
1116 extended_offset = 0;
1117 set_all_unchanged();
1118 set_changed(0);
1119 get_boot(create_empty_dos);
1120}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001121#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122
1123static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001124get_sectorsize(void)
1125{
Rob Landley736e5252006-02-25 03:36:00 +00001126 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001127 int arg;
1128 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1129 sector_size = arg;
1130 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001131 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001132 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001133 }
1134}
1135
Rob Landley88621d72006-08-29 19:41:06 +00001136static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001137get_kernel_geometry(void)
1138{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001139 struct hd_geometry geometry;
1140
1141 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1142 kern_heads = geometry.heads;
1143 kern_sectors = geometry.sectors;
1144 /* never use geometry.cylinders - it is truncated */
1145 }
1146}
1147
1148static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001149get_partition_table_geometry(void)
1150{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001151 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001152 struct partition *p;
1153 int i, h, s, hh, ss;
1154 int first = 1;
1155 int bad = 0;
1156
Eric Andersen3496fdc2006-01-30 23:09:20 +00001157 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001158 return;
1159
1160 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001161 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001162 p = pt_offset(bufp, i);
1163 if (p->sys_ind != 0) {
1164 h = p->end_head + 1;
1165 s = (p->end_sector & 077);
1166 if (first) {
1167 hh = h;
1168 ss = s;
1169 first = 0;
1170 } else if (hh != h || ss != s)
1171 bad = 1;
1172 }
1173 }
1174
1175 if (!first && !bad) {
1176 pt_heads = hh;
1177 pt_sectors = ss;
1178 }
1179}
1180
Rob Landleyb73451d2006-02-24 16:29:00 +00001181static void
1182get_geometry(void)
1183{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001185 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001186
1187 get_sectorsize();
1188 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001189#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001190 guess_device_type();
1191#endif
1192 heads = cylinders = sectors = 0;
1193 kern_heads = kern_sectors = 0;
1194 pt_heads = pt_sectors = 0;
1195
1196 get_kernel_geometry();
1197 get_partition_table_geometry();
1198
1199 heads = user_heads ? user_heads :
1200 pt_heads ? pt_heads :
1201 kern_heads ? kern_heads : 255;
1202 sectors = user_sectors ? user_sectors :
1203 pt_sectors ? pt_sectors :
1204 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001205 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1206 /* got bytes */
1207 } else {
1208 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001209
1210 if (ioctl(fd, BLKGETSIZE, &longsectors))
1211 longsectors = 0;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001212 bytes = ((unsigned long long) longsectors) << 9;
Eric Andersen040f4402003-07-30 08:40:37 +00001213 }
1214
1215 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001216
1217 sector_offset = 1;
1218 if (dos_compatible_flag)
1219 sector_offset = sectors;
1220
Eric Andersen040f4402003-07-30 08:40:37 +00001221 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222 if (!cylinders)
1223 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001224}
1225
1226/*
1227 * Read MBR. Returns:
1228 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1229 * 0: found or created label
1230 * 1: I/O error
1231 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001232static int
1233get_boot(enum action what)
1234{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001235 int i;
1236
1237 partitions = 4;
1238
1239 for (i = 0; i < 4; i++) {
1240 struct pte *pe = &ptes[i];
1241
1242 pe->part_table = pt_offset(MBRbuffer, i);
1243 pe->ext_pointer = NULL;
1244 pe->offset = 0;
1245 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001246#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001247 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001248#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001249 }
1250
Denis Vlasenko834410a2006-11-29 12:00:28 +00001251#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001252 if (what == create_empty_sun && check_sun_label())
1253 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001254#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001255
1256 memset(MBRbuffer, 0, 512);
1257
Denis Vlasenko834410a2006-11-29 12:00:28 +00001258#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001259 if (what == create_empty_dos)
1260 goto got_dos_table; /* skip reading disk */
1261
Denis Vlasenkobd852072007-03-19 14:43:38 +00001262 fd = open(disk_device, type_open);
1263 if (fd < 0) {
1264 fd = open(disk_device, O_RDONLY);
1265 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001266 if (what == try_only)
1267 return 1;
1268 fdisk_fatal(unable_to_open);
1269 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001270 printf("You will not be able to write "
1271 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272 }
1273
1274 if (512 != read(fd, MBRbuffer, 512)) {
1275 if (what == try_only)
1276 return 1;
1277 fdisk_fatal(unable_to_read);
1278 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001279#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001280 fd = open(disk_device, O_RDONLY);
1281 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001282 return 1;
1283 if (512 != read(fd, MBRbuffer, 512))
1284 return 1;
1285#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001287 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001288
1289 update_units();
1290
Denis Vlasenko834410a2006-11-29 12:00:28 +00001291#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292 if (check_sun_label())
1293 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001294#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295
Denis Vlasenko834410a2006-11-29 12:00:28 +00001296#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001297 if (check_sgi_label())
1298 return 0;
1299#endif
1300
Denis Vlasenko834410a2006-11-29 12:00:28 +00001301#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001302 if (check_aix_label())
1303 return 0;
1304#endif
1305
Denis Vlasenko834410a2006-11-29 12:00:28 +00001306#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001307 if (check_osf_label()) {
1308 possibly_osf_label = 1;
1309 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001310 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001311 return 0;
1312 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001313 printf("This disk has both DOS and BSD magic.\n"
1314 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315 }
1316#endif
1317
Denis Vlasenko834410a2006-11-29 12:00:28 +00001318#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001319 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001320#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001321
1322 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001323#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001324 return -1;
1325#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001326 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001328 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001330 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001331#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001332#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001333 create_sunlabel();
1334#endif
1335#else
1336 create_doslabel();
1337#endif
1338 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001339 case try_only:
1340 return -1;
1341 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001342#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001344#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001345 break;
1346 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001347 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001349#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001350 }
1351
Denis Vlasenko834410a2006-11-29 12:00:28 +00001352#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001354#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 warn_geometry();
1356
1357 for (i = 0; i < 4; i++) {
1358 struct pte *pe = &ptes[i];
1359
Rob Landleyb73451d2006-02-24 16:29:00 +00001360 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001361 if (partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001362 printf("Ignoring extra extended "
1363 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001364 else
1365 read_extended(i);
1366 }
1367 }
1368
1369 for (i = 3; i < partitions; i++) {
1370 struct pte *pe = &ptes[i];
1371
1372 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001373 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1374 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001375 pe->sectorbuffer[510],
1376 pe->sectorbuffer[511],
1377 i + 1);
1378#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001380#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001381 }
1382 }
1383
1384 return 0;
1385}
1386
Denis Vlasenko834410a2006-11-29 12:00:28 +00001387#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001388/*
1389 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1390 * If the user hits Enter, DFLT is returned.
1391 * Answers like +10 are interpreted as offsets from BASE.
1392 *
1393 * There is no default if DFLT is not between LOW and HIGH.
1394 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001395static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001396read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001398 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001400 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001401
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001402 if (dflt < low || dflt > high) {
1403 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001405 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406
1407 while (1) {
1408 int use_default = default_ok;
1409
1410 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001411 do {
1412 printf(fmt, mesg, low, high, dflt);
1413 read_maybe_empty("");
1414 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1415 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416
Eric Andersen84bdea82004-05-19 10:49:17 +00001417 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001418 int minus = (*line_ptr == '-');
1419 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001420
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001421 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001422
Rob Landleyb73451d2006-02-24 16:29:00 +00001423 while (isdigit(*++line_ptr))
1424 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001425
Rob Landleyb73451d2006-02-24 16:29:00 +00001426 switch (*line_ptr) {
1427 case 'c':
1428 case 'C':
1429 if (!display_in_cyl_units)
1430 i *= heads * sectors;
1431 break;
1432 case 'K':
1433 absolute = 1024;
1434 break;
1435 case 'k':
1436 absolute = 1000;
1437 break;
1438 case 'm':
1439 case 'M':
1440 absolute = 1000000;
1441 break;
1442 case 'g':
1443 case 'G':
1444 absolute = 1000000000;
1445 break;
1446 default:
1447 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001448 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001449 if (absolute) {
1450 unsigned long long bytes;
1451 unsigned long unit;
1452
1453 bytes = (unsigned long long) i * absolute;
1454 unit = sector_size * units_per_sector;
1455 bytes += unit/2; /* round */
1456 bytes /= unit;
1457 i = bytes;
1458 }
1459 if (minus)
1460 i = -i;
1461 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001462 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001463 i = atoi(line_ptr);
1464 while (isdigit(*line_ptr)) {
1465 line_ptr++;
1466 use_default = 0;
1467 }
1468 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001469 if (use_default) {
1470 i = dflt;
1471 printf("Using default value %u\n", i);
1472 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001473 if (i >= low && i <= high)
1474 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001475 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001476 }
1477 return i;
1478}
1479
Rob Landleyb73451d2006-02-24 16:29:00 +00001480static int
1481get_partition(int warn, int max)
1482{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001483 struct pte *pe;
1484 int i;
1485
Denis Vlasenkobd852072007-03-19 14:43:38 +00001486 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001487 pe = &ptes[i];
1488
1489 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001490 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1491 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1492 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1493 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001494 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001495 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001496 }
1497 return i;
1498}
1499
1500static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001501get_existing_partition(int warn, int max)
1502{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001503 int pno = -1;
1504 int i;
1505
1506 for (i = 0; i < max; i++) {
1507 struct pte *pe = &ptes[i];
1508 struct partition *p = pe->part_table;
1509
1510 if (p && !is_cleared_partition(p)) {
1511 if (pno >= 0)
1512 goto not_unique;
1513 pno = i;
1514 }
1515 }
1516 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001517 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001518 return pno;
1519 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001520 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001521 return -1;
1522
1523 not_unique:
1524 return get_partition(warn, max);
1525}
1526
1527static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001528get_nonexisting_partition(int warn, int max)
1529{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001530 int pno = -1;
1531 int i;
1532
1533 for (i = 0; i < max; i++) {
1534 struct pte *pe = &ptes[i];
1535 struct partition *p = pe->part_table;
1536
1537 if (p && is_cleared_partition(p)) {
1538 if (pno >= 0)
1539 goto not_unique;
1540 pno = i;
1541 }
1542 }
1543 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001544 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001545 return pno;
1546 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001547 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001548 return -1;
1549
1550 not_unique:
1551 return get_partition(warn, max);
1552}
1553
1554
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001555static void
1556change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001557{
1558 display_in_cyl_units = !display_in_cyl_units;
1559 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001560 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001561 str_units(PLURAL));
1562}
1563
1564static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001565toggle_active(int i)
1566{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001567 struct pte *pe = &ptes[i];
1568 struct partition *p = pe->part_table;
1569
Rob Landleyb73451d2006-02-24 16:29:00 +00001570 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001571 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001572 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1573 pe->changed = 1;
1574}
1575
1576static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001577toggle_dos_compatibility_flag(void)
1578{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001579 dos_compatible_flag = ~dos_compatible_flag;
1580 if (dos_compatible_flag) {
1581 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001582 printf("DOS Compatibility flag is set\n");
1583 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001584 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001585 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586 }
1587}
1588
1589static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001590delete_partition(int i)
1591{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001592 struct pte *pe = &ptes[i];
1593 struct partition *p = pe->part_table;
1594 struct partition *q = pe->ext_pointer;
1595
1596/* Note that for the fifth partition (i == 4) we don't actually
1597 * decrement partitions.
1598 */
1599
1600 if (warn_geometry())
1601 return; /* C/H/S not set */
1602 pe->changed = 1;
1603
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001604 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001605 sun_delete_partition(i);
1606 return;
1607 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001608 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001609 sgi_delete_partition(i);
1610 return;
1611 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001612
1613 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001614 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001615 partitions = 4;
1616 ptes[ext_index].ext_pointer = NULL;
1617 extended_offset = 0;
1618 }
1619 clear_partition(p);
1620 return;
1621 }
1622
1623 if (!q->sys_ind && i > 4) {
1624 /* the last one in the chain - just delete */
1625 --partitions;
1626 --i;
1627 clear_partition(ptes[i].ext_pointer);
1628 ptes[i].changed = 1;
1629 } else {
1630 /* not the last one - further ones will be moved down */
1631 if (i > 4) {
1632 /* delete this link in the chain */
1633 p = ptes[i-1].ext_pointer;
1634 *p = *q;
1635 set_start_sect(p, get_start_sect(q));
1636 set_nr_sects(p, get_nr_sects(q));
1637 ptes[i-1].changed = 1;
1638 } else if (partitions > 5) { /* 5 will be moved to 4 */
1639 /* the first logical in a longer chain */
1640 pe = &ptes[5];
1641
1642 if (pe->part_table) /* prevent SEGFAULT */
1643 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001644 get_partition_start(pe) -
1645 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001646 pe->offset = extended_offset;
1647 pe->changed = 1;
1648 }
1649
1650 if (partitions > 5) {
1651 partitions--;
1652 while (i < partitions) {
1653 ptes[i] = ptes[i+1];
1654 i++;
1655 }
1656 } else
1657 /* the only logical: clear only */
1658 clear_partition(ptes[i].part_table);
1659 }
1660}
1661
1662static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001663change_sysid(void)
1664{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001665 int i, sys, origsys;
1666 struct partition *p;
1667
Eric Andersen040f4402003-07-30 08:40:37 +00001668 /* If sgi_label then don't use get_existing_partition,
1669 let the user select a partition, since get_existing_partition()
1670 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001671 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001672 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001673 } else {
1674 i = get_partition(0, partitions);
1675 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676 if (i == -1)
1677 return;
1678 p = ptes[i].part_table;
1679 origsys = sys = get_sysid(i);
1680
1681 /* if changing types T to 0 is allowed, then
1682 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001683 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001684 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001685 return;
1686 }
1687 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001688 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001689
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001690 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001691 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001692 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001693 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001694 /* break; */
1695 }
1696
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001697 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001698 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001699 printf("You cannot change a partition into"
1700 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001701 break;
1702 }
1703 }
1704
1705 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001706#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001707 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001708 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001709 "as Whole disk (5),\n"
1710 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001711 "even Linux likes it\n\n");
1712#endif
1713#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001714 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001715 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001716 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001717 (i == 8 && sys != 0)
1718 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001719 ) {
1720 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001721 "as volume header (0),\nand "
1722 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001723 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001724 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001725#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001726 if (sys == origsys)
1727 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001728 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001730 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731 sgi_change_sysid(i, sys);
1732 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001733 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001734
Denis Vlasenkobd852072007-03-19 14:43:38 +00001735 printf("Changed system type of partition %d "
1736 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001737 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001738 ptes[i].changed = 1;
1739 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001740 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741 dos_changed = 1;
1742 break;
1743 }
1744 }
1745}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001746#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001747
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748
Denis Vlasenko28703012006-12-19 20:32:02 +00001749/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001750 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1751 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1752 * Lubkin Oct. 1991). */
1753
Rob Landleyb73451d2006-02-24 16:29:00 +00001754static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001755linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001756{
1757 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001758
1759 *c = ls / spc;
1760 ls = ls % spc;
1761 *h = ls / sectors;
1762 *s = ls % sectors + 1; /* sectors count from 1 */
1763}
1764
Rob Landleyb73451d2006-02-24 16:29:00 +00001765static void
1766check_consistency(const struct partition *p, int partition)
1767{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001768 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1769 unsigned pec, peh, pes; /* physical ending c, h, s */
1770 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1771 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001772
1773 if (!heads || !sectors || (partition >= 4))
1774 return; /* do not check extended partitions */
1775
1776/* physical beginning c, h, s */
1777 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1778 pbh = p->head;
1779 pbs = p->sector & 0x3f;
1780
1781/* physical ending c, h, s */
1782 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1783 peh = p->end_head;
1784 pes = p->end_sector & 0x3f;
1785
1786/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001787 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001788
1789/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001790 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001791
1792/* Same physical / logical beginning? */
1793 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001794 printf("Partition %d has different physical/logical "
1795 "beginnings (non-Linux?):\n", partition + 1);
1796 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1797 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001798 }
1799
1800/* Same physical / logical ending? */
1801 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001802 printf("Partition %d has different physical/logical "
1803 "endings:\n", partition + 1);
1804 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1805 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806 }
1807
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001808/* Ending on cylinder boundary? */
1809 if (peh != (heads - 1) || pes != sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001810 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001811 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001812 }
1813}
1814
1815static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001816list_disk_geometry(void)
1817{
Eric Andersen040f4402003-07-30 08:40:37 +00001818 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819 long megabytes = bytes/1000000;
1820
1821 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001822 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001823 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001824 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001825 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001826 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001827 printf("%d heads, %d sectors/track, %d cylinders",
Rob Landleyb73451d2006-02-24 16:29:00 +00001828 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001830 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001831 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001832 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001833 str_units(PLURAL),
1834 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835}
1836
1837/*
1838 * Check whether partition entries are ordered by their starting positions.
1839 * Return 0 if OK. Return i if partition i should have been earlier.
1840 * Two separate checks: primary and logical partitions.
1841 */
1842static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001843wrong_p_order(int *prev)
1844{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001845 const struct pte *pe;
1846 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001847 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001848 int i, last_i = 0;
1849
1850 for (i = 0 ; i < partitions; i++) {
1851 if (i == 4) {
1852 last_i = 4;
1853 last_p_start_pos = 0;
1854 }
1855 pe = &ptes[i];
1856 if ((p = pe->part_table)->sys_ind) {
1857 p_start_pos = get_partition_start(pe);
1858
1859 if (last_p_start_pos > p_start_pos) {
1860 if (prev)
1861 *prev = last_i;
1862 return i;
1863 }
1864
1865 last_p_start_pos = p_start_pos;
1866 last_i = i;
1867 }
1868 }
1869 return 0;
1870}
1871
Denis Vlasenko834410a2006-11-29 12:00:28 +00001872#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873/*
1874 * Fix the chain of logicals.
1875 * extended_offset is unchanged, the set of sectors used is unchanged
1876 * The chain is sorted so that sectors increase, and so that
1877 * starting sectors increase.
1878 *
1879 * After this it may still be that cfdisk doesnt like the table.
1880 * (This is because cfdisk considers expanded parts, from link to
1881 * end of partition, and these may still overlap.)
1882 * Now
1883 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1884 * may help.
1885 */
1886static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001887fix_chain_of_logicals(void)
1888{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001889 int j, oj, ojj, sj, sjj;
1890 struct partition *pj,*pjj,tmp;
1891
1892 /* Stage 1: sort sectors but leave sector of part 4 */
1893 /* (Its sector is the global extended_offset.) */
1894 stage1:
1895 for (j = 5; j < partitions-1; j++) {
1896 oj = ptes[j].offset;
1897 ojj = ptes[j+1].offset;
1898 if (oj > ojj) {
1899 ptes[j].offset = ojj;
1900 ptes[j+1].offset = oj;
1901 pj = ptes[j].part_table;
1902 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1903 pjj = ptes[j+1].part_table;
1904 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1905 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001906 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001907 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001908 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001909 goto stage1;
1910 }
1911 }
1912
1913 /* Stage 2: sort starting sectors */
1914 stage2:
1915 for (j = 4; j < partitions-1; j++) {
1916 pj = ptes[j].part_table;
1917 pjj = ptes[j+1].part_table;
1918 sj = get_start_sect(pj);
1919 sjj = get_start_sect(pjj);
1920 oj = ptes[j].offset;
1921 ojj = ptes[j+1].offset;
1922 if (oj+sj > ojj+sjj) {
1923 tmp = *pj;
1924 *pj = *pjj;
1925 *pjj = tmp;
1926 set_start_sect(pj, ojj+sjj-oj);
1927 set_start_sect(pjj, oj+sj-ojj);
1928 goto stage2;
1929 }
1930 }
1931
1932 /* Probably something was changed */
1933 for (j = 4; j < partitions; j++)
1934 ptes[j].changed = 1;
1935}
1936
1937
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001938static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001939fix_partition_table_order(void)
1940{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001941 struct pte *pei, *pek;
1942 int i,k;
1943
1944 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001945 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001946 return;
1947 }
1948
1949 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1950 /* partition i should have come earlier, move it */
1951 /* We have to move data in the MBR */
1952 struct partition *pi, *pk, *pe, pbuf;
1953 pei = &ptes[i];
1954 pek = &ptes[k];
1955
1956 pe = pei->ext_pointer;
1957 pei->ext_pointer = pek->ext_pointer;
1958 pek->ext_pointer = pe;
1959
1960 pi = pei->part_table;
1961 pk = pek->part_table;
1962
1963 memmove(&pbuf, pi, sizeof(struct partition));
1964 memmove(pi, pk, sizeof(struct partition));
1965 memmove(pk, &pbuf, sizeof(struct partition));
1966
1967 pei->changed = pek->changed = 1;
1968 }
1969
1970 if (i)
1971 fix_chain_of_logicals();
1972
1973 printf("Done.\n");
1974
1975}
1976#endif
1977
1978static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001979list_table(int xtra)
1980{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001981 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001982 int i, w;
1983
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001984 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001985 sun_list_table(xtra);
1986 return;
1987 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001988 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001989 sgi_list_table(xtra);
1990 return;
1991 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001992
1993 list_disk_geometry();
1994
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001995 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001996 xbsd_print_disklabel(xtra);
1997 return;
1998 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001999
2000 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2001 but if the device name ends in a digit, say /dev/foo1,
2002 then the partition is called /dev/foo1p3. */
2003 w = strlen(disk_device);
2004 if (w && isdigit(disk_device[w-1]))
2005 w++;
2006 if (w < 5)
2007 w = 5;
2008
Denis Vlasenkobd852072007-03-19 14:43:38 +00002009 // 1 12345678901 12345678901 12345678901 12
2010 printf("%*s Boot Start End Blocks Id System\n",
2011 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012
2013 for (i = 0; i < partitions; i++) {
2014 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002015 off_t psects;
2016 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002017 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002018
2019 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002020 if (!p || is_cleared_partition(p))
2021 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002022
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002023 psects = get_nr_sects(p);
2024 pblocks = psects;
2025 podd = 0;
2026
2027 if (sector_size < 1024) {
2028 pblocks /= (1024 / sector_size);
2029 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002030 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002031 if (sector_size > 1024)
2032 pblocks *= (sector_size / 1024);
2033
2034 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2035 partname(disk_device, i+1, w+2),
2036 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2037 ? '*' : '?',
2038 (unsigned long long) cround(get_partition_start(pe)), /* start */
2039 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2040 - (psects ? 1 : 0)),
2041 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2042 p->sys_ind, /* type id */
2043 partition_type(p->sys_ind)); /* type name */
2044
2045 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046 }
2047
2048 /* Is partition table in disk order? It need not be, but... */
2049 /* partition table entries are not checked for correct order if this
2050 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002051 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002052 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002053 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002054 }
2055}
2056
Denis Vlasenko834410a2006-11-29 12:00:28 +00002057#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002058static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002059x_list_table(int extend)
2060{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002061 const struct pte *pe;
2062 const struct partition *p;
2063 int i;
2064
Denis Vlasenkobd852072007-03-19 14:43:38 +00002065 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002066 disk_device, heads, sectors, cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002067 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002068 for (i = 0 ; i < partitions; i++) {
2069 pe = &ptes[i];
2070 p = (extend ? pe->ext_pointer : pe->part_table);
2071 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002072 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002073 i + 1, p->boot_ind, p->head,
2074 sector(p->sector),
2075 cylinder(p->sector, p->cyl), p->end_head,
2076 sector(p->end_sector),
2077 cylinder(p->end_sector, p->end_cyl),
2078 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2079 if (p->sys_ind)
2080 check_consistency(p, i);
2081 }
2082 }
2083}
2084#endif
2085
Denis Vlasenko834410a2006-11-29 12:00:28 +00002086#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002087static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002088fill_bounds(off_t *first, off_t *last)
2089{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090 int i;
2091 const struct pte *pe = &ptes[0];
2092 const struct partition *p;
2093
2094 for (i = 0; i < partitions; pe++,i++) {
2095 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002096 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002097 first[i] = 0xffffffff;
2098 last[i] = 0;
2099 } else {
2100 first[i] = get_partition_start(pe);
2101 last[i] = first[i] + get_nr_sects(p) - 1;
2102 }
2103 }
2104}
2105
2106static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002107check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002108{
Eric Andersend9261492004-06-28 23:50:31 +00002109 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002110
2111 real_s = sector(s) - 1;
2112 real_c = cylinder(s, c);
2113 total = (real_c * sectors + real_s) * heads + h;
2114 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002115 printf("Partition %d contains sector 0\n", n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002116 if (h >= heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002117 printf("Partition %d: head %d greater than maximum %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 n, h + 1, heads);
2119 if (real_s >= sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002120 printf("Partition %d: sector %d greater than "
2121 "maximum %d\n", n, s, sectors);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002122 if (real_c >= cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002123 printf("Partition %d: cylinder %"OFF_FMT"u greater than "
2124 "maximum %d\n", n, real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002125 if (cylinders <= 1024 && start != total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002126 printf("Partition %d: previous sectors %"OFF_FMT"u disagrees with "
2127 "total %"OFF_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002128}
2129
2130static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002131verify(void)
2132{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002134 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002135 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136 struct partition *p;
2137
2138 if (warn_geometry())
2139 return;
2140
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002141 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142 verify_sun();
2143 return;
2144 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002145 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002146 verify_sgi(1);
2147 return;
2148 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002149
2150 fill_bounds(first, last);
2151 for (i = 0; i < partitions; i++) {
2152 struct pte *pe = &ptes[i];
2153
2154 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002155 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002156 check_consistency(p, i);
2157 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002158 printf("Warning: bad start-of-data in "
2159 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002160 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2161 last[i]);
2162 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002163 for (j = 0; j < i; j++) {
2164 if ((first[i] >= first[j] && first[i] <= last[j])
2165 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2166 printf("Warning: partition %d overlaps "
2167 "partition %d\n", j + 1, i + 1);
2168 total += first[i] >= first[j] ?
2169 first[i] : first[j];
2170 total -= last[i] <= last[j] ?
2171 last[i] : last[j];
2172 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002173 }
2174 }
2175 }
2176
2177 if (extended_offset) {
2178 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002179 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002180 get_nr_sects(pex->part_table) - 1;
2181
2182 for (i = 4; i < partitions; i++) {
2183 total++;
2184 p = ptes[i].part_table;
2185 if (!p->sys_ind) {
2186 if (i != 4 || i + 1 < partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002187 printf("Warning: partition %d "
2188 "is empty\n", i + 1);
2189 } else if (first[i] < extended_offset || last[i] > e_last) {
2190 printf("Logical partition %d not entirely in "
2191 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002192 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002193 }
2194 }
2195
2196 if (total > heads * sectors * cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002197 printf("Total allocated sectors %d greater than the maximum "
2198 "%d\n", total, heads * sectors * cylinders);
2199 else {
2200 total = heads * sectors * cylinders - total;
2201 if (total != 0)
2202 printf("%d unallocated sectors\n", total);
2203 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002204}
2205
2206static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002207add_partition(int n, int sys)
2208{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002210 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002211 struct partition *p = ptes[n].part_table;
2212 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002213 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002214 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002215 first[partitions], last[partitions];
2216
2217 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002218 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219 return;
2220 }
2221 fill_bounds(first, last);
2222 if (n < 4) {
2223 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002224 if (display_in_cyl_units || !total_number_of_sectors)
2225 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002226 else
Eric Andersen040f4402003-07-30 08:40:37 +00002227 llimit = total_number_of_sectors - 1;
2228 limit = llimit;
2229 if (limit != llimit)
2230 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002231 if (extended_offset) {
2232 first[ext_index] = extended_offset;
2233 last[ext_index] = get_start_sect(q) +
2234 get_nr_sects(q) - 1;
2235 }
2236 } else {
2237 start = extended_offset + sector_offset;
2238 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2239 }
2240 if (display_in_cyl_units)
2241 for (i = 0; i < partitions; i++)
2242 first[i] = (cround(first[i]) - 1) * units_per_sector;
2243
Denis Vlasenkobd852072007-03-19 14:43:38 +00002244 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002245 do {
2246 temp = start;
2247 for (i = 0; i < partitions; i++) {
2248 int lastplusoff;
2249
2250 if (start == ptes[i].offset)
2251 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002252 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002253 if (start >= first[i] && start <= lastplusoff)
2254 start = lastplusoff + 1;
2255 }
2256 if (start > limit)
2257 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002258 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002259 printf("Sector %"OFF_FMT"d is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002260 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002261 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002262 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002263 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002264 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002265
2266 saved_start = start;
2267 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2268 0, mesg);
2269 if (display_in_cyl_units) {
2270 start = (start - 1) * units_per_sector;
2271 if (start < saved_start) start = saved_start;
2272 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002273 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002274 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002275 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002276 if (n > 4) { /* NOT for fifth partition */
2277 struct pte *pe = &ptes[n];
2278
2279 pe->offset = start - sector_offset;
2280 if (pe->offset == extended_offset) { /* must be corrected */
2281 pe->offset++;
2282 if (sector_offset == 1)
2283 start++;
2284 }
2285 }
2286
2287 for (i = 0; i < partitions; i++) {
2288 struct pte *pe = &ptes[i];
2289
2290 if (start < pe->offset && limit >= pe->offset)
2291 limit = pe->offset - 1;
2292 if (start < first[i] && limit >= first[i])
2293 limit = first[i] - 1;
2294 }
2295 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002296 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002297 if (n > 4)
2298 partitions--;
2299 return;
2300 }
2301 if (cround(start) == cround(limit)) {
2302 stop = limit;
2303 } else {
2304 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002305 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002306 str_units(SINGULAR));
2307 stop = read_int(cround(start), cround(limit), cround(limit),
2308 cround(start), mesg);
2309 if (display_in_cyl_units) {
2310 stop = stop * units_per_sector - 1;
2311 if (stop >limit)
2312 stop = limit;
2313 }
2314 }
2315
2316 set_partition(n, 0, start, stop, sys);
2317 if (n > 4)
2318 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2319
Rob Landleyb73451d2006-02-24 16:29:00 +00002320 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002321 struct pte *pe4 = &ptes[4];
2322 struct pte *pen = &ptes[n];
2323
2324 ext_index = n;
2325 pen->ext_pointer = p;
2326 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002327 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002328 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2329 pe4->ext_pointer = pe4->part_table + 1;
2330 pe4->changed = 1;
2331 partitions = 5;
2332 }
2333}
2334
2335static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002336add_logical(void)
2337{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002338 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2339 struct pte *pe = &ptes[partitions];
2340
Rob Landley081e3842006-08-03 20:07:35 +00002341 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002342 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2343 pe->ext_pointer = pe->part_table + 1;
2344 pe->offset = 0;
2345 pe->changed = 1;
2346 partitions++;
2347 }
2348 add_partition(partitions - 1, LINUX_NATIVE);
2349}
2350
2351static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002352new_partition(void)
2353{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354 int i, free_primary = 0;
2355
2356 if (warn_geometry())
2357 return;
2358
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002359 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002360 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2361 return;
2362 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002363 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002364 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2365 return;
2366 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002367 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002368 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2369"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2370"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002371 return;
2372 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002373
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002374 for (i = 0; i < 4; i++)
2375 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002376
Rob Landleyb73451d2006-02-24 16:29:00 +00002377 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002378 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002379 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002380 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002381
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002382 if (!free_primary) {
2383 if (extended_offset)
2384 add_logical();
2385 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002386 printf("You must delete some partition and add "
2387 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002388 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002389 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002390 snprintf(line, sizeof(line),
2391 "Command action\n"
2392 " %s\n"
2393 " p primary partition (1-4)\n",
2394 (extended_offset ?
2395 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002396 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002397 c = read_nonempty(line);
2398 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002399 i = get_nonexisting_partition(0, 4);
2400 if (i >= 0)
2401 add_partition(i, LINUX_NATIVE);
2402 return;
2403 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002404 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 add_logical();
2406 return;
2407 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002408 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409 i = get_nonexisting_partition(0, 4);
2410 if (i >= 0)
2411 add_partition(i, EXTENDED);
2412 return;
2413 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002414 printf("Invalid partition number "
2415 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002416 }
2417 }
2418}
2419
2420static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002421write_table(void)
2422{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002423 int i;
2424
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002425 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002426 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002427 if (ptes[i].changed)
2428 ptes[3].changed = 1;
2429 for (i = 3; i < partitions; i++) {
2430 struct pte *pe = &ptes[i];
2431
2432 if (pe->changed) {
2433 write_part_table_flag(pe->sectorbuffer);
2434 write_sector(pe->offset, pe->sectorbuffer);
2435 }
2436 }
2437 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002438 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 /* no test on change? the printf below might be mistaken */
2440 sgi_write_table();
2441 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002442 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 int needw = 0;
2444
Rob Landleyb73451d2006-02-24 16:29:00 +00002445 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 if (ptes[i].changed)
2447 needw = 1;
2448 if (needw)
2449 sun_write_table();
2450 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451
Denis Vlasenkobd852072007-03-19 14:43:38 +00002452 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002453 reread_partition_table(1);
2454}
2455
Rob Landleyb73451d2006-02-24 16:29:00 +00002456static void
2457reread_partition_table(int leave)
2458{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 int i;
2460
Denis Vlasenkobd852072007-03-19 14:43:38 +00002461 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002463 /* sleep(2); Huh? */
Denis Vlasenko28703012006-12-19 20:32:02 +00002464 i = ioctl(fd, BLKRRPART);
2465#if 0
2466 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002467 /* some kernel versions (1.2.x) seem to have trouble
2468 rereading the partition table, but if asked to do it
2469 twice, the second time works. - biro@yggdrasil.com */
2470 sync();
2471 sleep(2);
Denis Vlasenko28703012006-12-19 20:32:02 +00002472 i = ioctl(fd, BLKRRPART);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473 }
Denis Vlasenko28703012006-12-19 20:32:02 +00002474#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475
2476 if (i) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002477 bb_perror_msg("WARNING: rereading partition table "
2478 "failed, kernel still uses old table");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002479 }
2480
Denis Vlasenko28703012006-12-19 20:32:02 +00002481#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002483 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002484 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002486 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002487#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002488
2489 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002490 if (ENABLE_FEATURE_CLEAN_UP)
2491 close(fd);
2492 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002493 }
2494}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002495#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496
Denis Vlasenko834410a2006-11-29 12:00:28 +00002497#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498#define MAX_PER_LINE 16
2499static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002500print_buffer(char *pbuffer)
2501{
2502 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002503
2504 for (i = 0, l = 0; i < sector_size; i++, l++) {
2505 if (l == 0)
2506 printf("0x%03X:", i);
2507 printf(" %02X", (unsigned char) pbuffer[i]);
2508 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002509 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510 l = -1;
2511 }
2512 }
2513 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002514 puts("");
2515 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002516}
2517
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002519print_raw(void)
2520{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002521 int i;
2522
Denis Vlasenkobd852072007-03-19 14:43:38 +00002523 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002524 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002526 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 for (i = 3; i < partitions; i++)
2528 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002529 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530}
2531
2532static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002533move_begin(int i)
2534{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002535 struct pte *pe = &ptes[i];
2536 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002537 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538
2539 if (warn_geometry())
2540 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002541 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002542 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002543 return;
2544 }
2545 first = get_partition_start(pe);
2546 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002547 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548
2549 if (new != get_nr_sects(p)) {
2550 first = get_nr_sects(p) + get_start_sect(p) - new;
2551 set_nr_sects(p, first);
2552 set_start_sect(p, new);
2553 pe->changed = 1;
2554 }
2555}
2556
2557static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002558xselect(void)
2559{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002560 char c;
2561
Rob Landleyb73451d2006-02-24 16:29:00 +00002562 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002564 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002565 switch (c) {
2566 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002567 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002568 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569 break;
2570 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002571 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002572 move_begin(get_partition(0, partitions));
2573 break;
2574 case 'c':
2575 user_cylinders = cylinders =
2576 read_int(1, cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002577 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002578 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002579 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 warn_cylinders();
2582 break;
2583 case 'd':
2584 print_raw();
2585 break;
2586 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002589 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002591 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 x_list_table(1);
2593 break;
2594 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002595 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002596 fix_partition_table_order();
2597 break;
2598 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002599#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002600 create_sgilabel();
2601#endif
2602 break;
2603 case 'h':
2604 user_heads = heads = read_int(1, heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002605 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 update_units();
2607 break;
2608 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002609 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 break;
2612 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002613 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 break;
2616 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002617 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 list_table(1);
2619 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002620 x_list_table(0);
2621 break;
2622 case 'q':
2623 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002624 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 exit(0);
2626 case 'r':
2627 return;
2628 case 's':
2629 user_sectors = sectors = read_int(1, sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002630 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002631 if (dos_compatible_flag) {
2632 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002633 printf("Warning: setting sector offset for DOS "
2634 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002635 }
2636 update_units();
2637 break;
2638 case 'v':
2639 verify();
2640 break;
2641 case 'w':
2642 write_table(); /* does not return */
2643 break;
2644 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002645 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002646 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002647 break;
2648 default:
2649 xmenu();
2650 }
2651 }
2652}
2653#endif /* ADVANCED mode */
2654
2655static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002656is_ide_cdrom_or_tape(const char *device)
2657{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002658 FILE *procf;
2659 char buf[100];
2660 struct stat statbuf;
2661 int is_ide = 0;
2662
2663 /* No device was given explicitly, and we are trying some
2664 likely things. But opening /dev/hdc may produce errors like
2665 "hdc: tray open or drive not ready"
2666 if it happens to be a CD-ROM drive. It even happens that
2667 the process hangs on the attempt to read a music CD.
2668 So try to be careful. This only works since 2.1.73. */
2669
2670 if (strncmp("/dev/hd", device, 7))
2671 return 0;
2672
2673 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2674 procf = fopen(buf, "r");
2675 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2676 is_ide = (!strncmp(buf, "cdrom", 5) ||
2677 !strncmp(buf, "tape", 4));
2678 else
2679 /* Now when this proc file does not exist, skip the
2680 device when it is read-only. */
2681 if (stat(device, &statbuf) == 0)
2682 is_ide = ((statbuf.st_mode & 0222) == 0);
2683
2684 if (procf)
2685 fclose(procf);
2686 return is_ide;
2687}
2688
Rob Landley5527b912006-02-25 03:46:10 +00002689
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002690static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002691trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002692{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002693 int gb;
2694
2695 disk_device = device;
2696 if (setjmp(listingbuf))
2697 return;
2698 if (!user_specified)
2699 if (is_ide_cdrom_or_tape(device))
2700 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002701 fd = open(disk_device, type_open);
2702 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002703 gb = get_boot(try_only);
2704 if (gb > 0) { /* I/O error */
2705 close(fd);
2706 } else if (gb < 0) { /* no DOS signature */
2707 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002708 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 return;
Rob Landley5527b912006-02-25 03:46:10 +00002710 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002711#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002712 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002713#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002714 printf("Disk %s doesn't contain a valid "
2715 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002716 close(fd);
2717 } else {
2718 close(fd);
2719 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002720#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002721 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002722 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002723 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002724#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002725 }
2726 } else {
2727 /* Ignore other errors, since we try IDE
2728 and SCSI hard disks which may not be
2729 installed on the system. */
2730 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002731 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002732 return;
2733 }
2734 }
2735}
2736
2737/* for fdisk -l: try all things in /proc/partitions
2738 that look like a partition name (do not end in a digit) */
2739static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002740tryprocpt(void)
2741{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002742 FILE *procpt;
2743 char line[100], ptname[100], devname[120], *s;
2744 int ma, mi, sz;
2745
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002746 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002747
2748 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002749 if (sscanf(line, " %d %d %d %[^\n ]",
2750 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751 continue;
2752 for (s = ptname; *s; s++);
2753 if (isdigit(s[-1]))
2754 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002755 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002756 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002757 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002758#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002760#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761}
2762
Denis Vlasenko834410a2006-11-29 12:00:28 +00002763#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002764static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002765unknown_command(int c)
2766{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002767 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002769#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002770
Denis Vlasenko06af2162007-02-03 17:28:39 +00002771int fdisk_main(int argc, char **argv);
Rob Landleyb73451d2006-02-24 16:29:00 +00002772int fdisk_main(int argc, char **argv)
2773{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002774 char *str_b, *str_C, *str_H, *str_S;
2775 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002776 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777 * fdisk -v
2778 * fdisk -l [-b sectorsize] [-u] device ...
2779 * fdisk -s [partition] ...
2780 * fdisk [-b sectorsize] [-u] device
2781 *
2782 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002783 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002784 enum {
2785 OPT_b = 1 << 0,
2786 OPT_C = 1 << 1,
2787 OPT_H = 1 << 2,
2788 OPT_l = 1 << 3,
2789 OPT_S = 1 << 4,
2790 OPT_u = 1 << 5,
2791 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2792 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002793
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002794 PTR_TO_GLOBALS = xzalloc(sizeof(G));
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002795
Denis Vlasenko834410a2006-11-29 12:00:28 +00002796 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2797 &str_b, &str_C, &str_H, &str_S);
2798 argc -= optind;
2799 argv += optind;
2800 if (opt & OPT_b) { // -b
2801 /* Ugly: this sector size is really per device,
2802 so cannot be combined with multiple disks,
2803 and the same goes for the C/H/S options.
2804 */
2805 sector_size = xatoi_u(str_b);
2806 if (sector_size != 512 && sector_size != 1024 &&
2807 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002808 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002809 sector_offset = 2;
2810 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002812 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2813 if (opt & OPT_H) { // -H
2814 user_heads = xatoi_u(str_H);
2815 if (user_heads <= 0 || user_heads >= 256)
2816 user_heads = 0;
2817 }
2818 //if (opt & OPT_l) // -l
2819 if (opt & OPT_S) { // -S
2820 user_sectors = xatoi_u(str_S);
2821 if (user_sectors <= 0 || user_sectors >= 64)
2822 user_sectors = 0;
2823 }
2824 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2825 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002826
Denis Vlasenko834410a2006-11-29 12:00:28 +00002827 if (user_set_sector_size && argc != 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002828 printf("Warning: the -b (set sector size) option should"
2829 " be used with one specified device\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002830
Denis Vlasenko834410a2006-11-29 12:00:28 +00002831#if ENABLE_FEATURE_FDISK_WRITABLE
2832 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002834#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002836 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002838#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002839 /* avoid gcc warning:
2840 variable `k' might be clobbered by `longjmp' */
2841 (void)&k;
2842#endif
2843 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002844 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002845 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846 } else {
2847 /* we no longer have default device names */
2848 /* but, we can use /proc/partitions instead */
2849 tryprocpt();
2850 }
2851 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002852#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002854#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855
Denis Vlasenko834410a2006-11-29 12:00:28 +00002856#if ENABLE_FEATURE_FDISK_BLKSIZE
2857 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002859 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002860
2861 nowarn = 1;
2862 type_open = O_RDONLY;
2863
Denis Vlasenko834410a2006-11-29 12:00:28 +00002864 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002865 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002866
Denis Vlasenko834410a2006-11-29 12:00:28 +00002867 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002868 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002869 fd = open(disk_device, type_open);
2870 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002871 fdisk_fatal(unable_to_open);
2872 if (ioctl(fd, BLKGETSIZE, &size))
2873 fdisk_fatal(ioctl_error);
2874 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002875 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 printf("%ld\n", size/2);
2877 else
2878 printf("%s: %ld\n", argv[j], size/2);
2879 }
2880 return 0;
2881 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002882#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002883
Denis Vlasenko834410a2006-11-29 12:00:28 +00002884#if ENABLE_FEATURE_FDISK_WRITABLE
2885 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002886 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002887
Denis Vlasenko834410a2006-11-29 12:00:28 +00002888 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002889 get_boot(fdisk);
2890
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002891 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002893 printf("Detected an OSF/1 disklabel on %s, entering "
2894 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002895 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002896 /*Why do we do this? It seems to be counter-intuitive*/
2897 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898 /* If we return we may want to make an empty DOS label? */
2899 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002900
2901 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002902 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002903 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002904 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002905 switch (c) {
2906 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002907 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002908 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002909 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 toggle_sunflags(get_partition(1, partitions),
2911 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002912 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 sgi_set_bootpartition(
2914 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 else
2916 unknown_command(c);
2917 break;
2918 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002919 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002920 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002921 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002922 if (read_maybe_empty("Please enter the name of the "
2923 "new boot file: ") == '\n')
2924 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925 else
2926 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002927 }
2928#if ENABLE_FEATURE_OSF_LABEL
2929 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002930 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002931#endif
2932 break;
2933 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002936 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002937 toggle_sunflags(get_partition(1, partitions),
2938 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002939 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 sgi_set_swappartition(
2941 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002942 else
2943 unknown_command(c);
2944 break;
2945 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002946 {
Eric Andersen040f4402003-07-30 08:40:37 +00002947 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002948 /* If sgi_label then don't use get_existing_partition,
2949 let the user select a partition, since
2950 get_existing_partition() only works for Linux-like
2951 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002952 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002953 j = get_existing_partition(1, partitions);
2954 } else {
2955 j = get_partition(1, partitions);
2956 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002957 if (j >= 0)
2958 delete_partition(j);
2959 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 break;
2961 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002962 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002963 create_sgiinfo();
2964 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002965 unknown_command(c);
2966 case 'l':
2967 list_types(get_sys_types());
2968 break;
2969 case 'm':
2970 menu();
2971 break;
2972 case 'n':
2973 new_partition();
2974 break;
2975 case 'o':
2976 create_doslabel();
2977 break;
2978 case 'p':
2979 list_table(0);
2980 break;
2981 case 'q':
2982 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002983 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 return 0;
2985 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002986#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002987 create_sunlabel();
2988#endif
2989 break;
2990 case 't':
2991 change_sysid();
2992 break;
2993 case 'u':
2994 change_units();
2995 break;
2996 case 'v':
2997 verify();
2998 break;
2999 case 'w':
3000 write_table(); /* does not return */
3001 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003002#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003003 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003004 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003005 printf("\n\tSorry, no experts menu for SGI "
3006 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003007 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003008 xselect();
3009 break;
3010#endif
3011 default:
3012 unknown_command(c);
3013 menu();
3014 }
3015 }
3016 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003017#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003018}