blob: 01c01bd244f58fd8185e181683e8460ca5e3ef3a [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
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000010#ifndef _LARGEFILE64_SOURCE
11/* For lseek64 */
12#define _LARGEFILE64_SOURCE
13#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000014#include <assert.h> /* assert */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000016
Denis Vlasenko834410a2006-11-29 12:00:28 +000017/* Looks like someone forgot to add this to config system */
18#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
19# define ENABLE_FEATURE_FDISK_BLKSIZE 0
20# define USE_FEATURE_FDISK_BLKSIZE(a)
21#endif
22
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000023#define DEFAULT_SECTOR_SIZE 512
24#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000025#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000026#define MAXIMUM_PARTS 60
27
28#define ACTIVE_FLAG 0x80
29
30#define EXTENDED 0x05
31#define WIN98_EXTENDED 0x0f
32#define LINUX_PARTITION 0x81
33#define LINUX_SWAP 0x82
34#define LINUX_NATIVE 0x83
35#define LINUX_EXTENDED 0x85
36#define LINUX_LVM 0x8e
37#define LINUX_RAID 0xfd
38
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000039/* Used for sector numbers. Today's disk sizes make it necessary */
40typedef unsigned long long ullong;
41
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000043 unsigned char heads;
44 unsigned char sectors;
45 unsigned short cylinders;
46 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000047};
48
Denis Vlasenko98ae2162006-10-12 19:30:44 +000049#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000050
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000051static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000052"Building a new %s. Changes will remain in memory only,\n"
53"until you decide to write them. After that the previous content\n"
54"won't be recoverable.\n\n";
55
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000056static const char msg_part_already_defined[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000057"Partition %d is already defined, delete it before re-adding\n";
58
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000059
Denis Vlasenko834410a2006-11-29 12:00:28 +000060static unsigned sector_size = DEFAULT_SECTOR_SIZE;
61static unsigned user_set_sector_size;
62static unsigned sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000063
Denis Vlasenko834410a2006-11-29 12:00:28 +000064#if ENABLE_FEATURE_OSF_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +000065static int possibly_osf_label;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000066#endif
67
Denis Vlasenko834410a2006-11-29 12:00:28 +000068static unsigned heads, sectors, cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000069static void update_units(void);
70
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000071
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072struct partition {
73 unsigned char boot_ind; /* 0x80 - active */
74 unsigned char head; /* starting head */
75 unsigned char sector; /* starting sector */
76 unsigned char cyl; /* starting cylinder */
77 unsigned char sys_ind; /* What partition type */
78 unsigned char end_head; /* end head */
79 unsigned char end_sector; /* end sector */
80 unsigned char end_cyl; /* end cylinder */
81 unsigned char start4[4]; /* starting sector counting from 0 */
82 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000083} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000084
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000085static const char unable_to_open[] ALIGN1 = "cannot open %s";
86static const char unable_to_read[] ALIGN1 = "cannot read from %s";
87static const char unable_to_seek[] ALIGN1 = "cannot seek on %s";
88static const char unable_to_write[] ALIGN1 = "cannot write to %s";
89static const char ioctl_error[] ALIGN1 = "BLKGETSIZE ioctl failed on %s";
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000090static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000091
Denis Vlasenko98ae2162006-10-12 19:30:44 +000092enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +000093 label_dos, label_sun, label_sgi, label_aix, label_osf
94};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000095
Denis Vlasenko98ae2162006-10-12 19:30:44 +000096#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000097
Denis Vlasenko834410a2006-11-29 12:00:28 +000098#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000099#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000100#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000101#else
102#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000103#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000104#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000105
Denis Vlasenko834410a2006-11-29 12:00:28 +0000106#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000107#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000108#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000109#else
110#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000111#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000112#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000113
Denis Vlasenko834410a2006-11-29 12:00:28 +0000114#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000115#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000116#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000117#else
118#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000119#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000120#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000121
Denis Vlasenko834410a2006-11-29 12:00:28 +0000122#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000123#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000124#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000125#else
126#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000127#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000128#endif
Rob Landley5527b912006-02-25 03:46:10 +0000129
Rob Landleyb73451d2006-02-24 16:29:00 +0000130enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000131
Rob Landley5527b912006-02-25 03:46:10 +0000132static enum label_type current_label_type;
133
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000134static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000135static int fd; /* the disk */
136static int partitions = 4; /* maximum partition + 1 */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000137static int display_in_cyl_units = 1;
138static unsigned units_per_sector = 1;
139#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000140static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000141static void reread_partition_table(int leave);
142static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000143static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000144static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000145static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000146#endif
147static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000148static void get_geometry(void);
149static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000150
151#define PLURAL 0
152#define SINGULAR 1
153
Denis Vlasenko28703012006-12-19 20:32:02 +0000154static unsigned get_start_sect(const struct partition *p);
155static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000156
157/*
158 * per partition table entry data
159 *
160 * The four primary partitions have the same sectorbuffer (MBRbuffer)
161 * and have NULL ext_pointer.
162 * Each logical partition table entry has two pointers, one for the
163 * partition and one link to the next one.
164 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000165struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000166 struct partition *part_table; /* points into sectorbuffer */
167 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000168 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000169 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000170#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000171 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000172#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000173};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000174
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000175/* DOS partition types */
176
177static const char *const i386_sys_types[] = {
178 "\x00" "Empty",
179 "\x01" "FAT12",
180 "\x04" "FAT16 <32M",
181 "\x05" "Extended", /* DOS 3.3+ extended partition */
182 "\x06" "FAT16", /* DOS 16-bit >=32M */
183 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
184 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
185 "\x0b" "Win95 FAT32",
186 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
187 "\x0e" "Win95 FAT16 (LBA)",
188 "\x0f" "Win95 Ext'd (LBA)",
189 "\x11" "Hidden FAT12",
190 "\x12" "Compaq diagnostics",
191 "\x14" "Hidden FAT16 <32M",
192 "\x16" "Hidden FAT16",
193 "\x17" "Hidden HPFS/NTFS",
194 "\x1b" "Hidden Win95 FAT32",
195 "\x1c" "Hidden W95 FAT32 (LBA)",
196 "\x1e" "Hidden W95 FAT16 (LBA)",
197 "\x3c" "Part.Magic recovery",
198 "\x41" "PPC PReP Boot",
199 "\x42" "SFS",
200 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
201 "\x80" "Old Minix", /* Minix 1.4a and earlier */
202 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
203 "\x82" "Linux swap", /* also Solaris */
204 "\x83" "Linux",
205 "\x84" "OS/2 hidden C: drive",
206 "\x85" "Linux extended",
207 "\x86" "NTFS volume set",
208 "\x87" "NTFS volume set",
209 "\x8e" "Linux LVM",
210 "\x9f" "BSD/OS", /* BSDI */
211 "\xa0" "Thinkpad hibernation",
212 "\xa5" "FreeBSD", /* various BSD flavours */
213 "\xa6" "OpenBSD",
214 "\xa8" "Darwin UFS",
215 "\xa9" "NetBSD",
216 "\xab" "Darwin boot",
217 "\xb7" "BSDI fs",
218 "\xb8" "BSDI swap",
219 "\xbe" "Solaris boot",
220 "\xeb" "BeOS fs",
221 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
222 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
223 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
224 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
225 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
226 autodetect using persistent
227 superblock */
228#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
229 "\x02" "XENIX root",
230 "\x03" "XENIX usr",
231 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
232 "\x09" "AIX bootable", /* AIX data or Coherent */
233 "\x10" "OPUS",
234 "\x18" "AST SmartSleep",
235 "\x24" "NEC DOS",
236 "\x39" "Plan 9",
237 "\x40" "Venix 80286",
238 "\x4d" "QNX4.x",
239 "\x4e" "QNX4.x 2nd part",
240 "\x4f" "QNX4.x 3rd part",
241 "\x50" "OnTrack DM",
242 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
243 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
244 "\x53" "OnTrack DM6 Aux3",
245 "\x54" "OnTrackDM6",
246 "\x55" "EZ-Drive",
247 "\x56" "Golden Bow",
248 "\x5c" "Priam Edisk",
249 "\x61" "SpeedStor",
250 "\x64" "Novell Netware 286",
251 "\x65" "Novell Netware 386",
252 "\x70" "DiskSecure Multi-Boot",
253 "\x75" "PC/IX",
254 "\x93" "Amoeba",
255 "\x94" "Amoeba BBT", /* (bad block table) */
256 "\xa7" "NeXTSTEP",
257 "\xbb" "Boot Wizard hidden",
258 "\xc1" "DRDOS/sec (FAT-12)",
259 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
260 "\xc6" "DRDOS/sec (FAT-16)",
261 "\xc7" "Syrinx",
262 "\xda" "Non-FS data",
263 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
264 Concurrent DOS or CTOS */
265 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
266 "\xdf" "BootIt", /* BootIt EMBRM */
267 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
268 extended partition */
269 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
270 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
271 partition < 1024 cyl. */
272 "\xf1" "SpeedStor",
273 "\xf4" "SpeedStor", /* SpeedStor large partition */
274 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
275 "\xff" "BBT", /* Xenix Bad Block Table */
276#endif
277 NULL
278};
279
280
281/* Globals */
282
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000283struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000284 char *line_ptr;
285 char line_buffer[80];
286 char partname_buffer[80];
287 jmp_buf listingbuf;
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000288 /* Raw disk label. For DOS-type partition tables the MBR,
289 * with descriptions of the primary partitions. */
290 char MBRbuffer[MAX_SECTOR_SIZE];
291 /* Partition tables */
292 struct pte ptes[MAXIMUM_PARTS];
293};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000294/* bb_common_bufsiz1 is too small for this on 64 bit CPUs */
295#define G (*ptr_to_globals)
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000296
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000297#define line_ptr (G.line_ptr)
298#define listingbuf (G.listingbuf)
299#define line_buffer (G.line_buffer)
300#define partname_buffer (G.partname_buffer)
301#define MBRbuffer (G.MBRbuffer)
302#define ptes (G.ptes)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000303
Denis Vlasenkobd852072007-03-19 14:43:38 +0000304
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000305/* Code */
306
307#define IS_EXTENDED(i) \
308 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
309
310#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
311
312#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
313
314#define pt_offset(b, n) \
315 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
316
317#define sector(s) ((s) & 0x3f)
318
319#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
320
321#define hsc2sector(h,s,c) \
322 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
323
324#define set_hsc(h,s,c,sector) \
325 do { \
326 s = sector % sectors + 1; \
327 sector /= sectors; \
328 h = sector % heads; \
329 sector /= heads; \
330 c = sector & 0xff; \
331 s |= (sector >> 2) & 0xc0; \
332 } while (0)
333
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000334#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000335/* read line; return 0 or first printable char */
336static int
337read_line(const char *prompt)
338{
339 int sz;
340
341 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
342 if (sz <= 0)
343 exit(0); /* Ctrl-D or Ctrl-C */
344
345 if (line_buffer[sz-1] == '\n')
346 line_buffer[--sz] = '\0';
347
348 line_ptr = line_buffer;
349 while (*line_ptr && !isgraph(*line_ptr))
350 line_ptr++;
351 return *line_ptr;
352}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000353#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000354
Denis Vlasenkobd852072007-03-19 14:43:38 +0000355/*
356 * return partition name - uses static storage
357 */
358static const char *
359partname(const char *dev, int pno, int lth)
360{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000361 const char *p;
362 int w, wp;
363 int bufsiz;
364 char *bufp;
365
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000366 bufp = partname_buffer;
367 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000368
369 w = strlen(dev);
370 p = "";
371
372 if (isdigit(dev[w-1]))
373 p = "p";
374
375 /* devfs kludge - note: fdisk partition names are not supposed
376 to equal kernel names, so there is no reason to do this */
377 if (strcmp(dev + w - 4, "disc") == 0) {
378 w -= 4;
379 p = "part";
380 }
381
382 wp = strlen(p);
383
384 if (lth) {
385 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
386 lth-wp-2, w, dev, p, pno);
387 } else {
388 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
389 }
390 return bufp;
391}
392
Denis Vlasenko834410a2006-11-29 12:00:28 +0000393#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000394static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000395set_all_unchanged(void)
396{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000397 int i;
398
399 for (i = 0; i < MAXIMUM_PARTS; i++)
400 ptes[i].changed = 0;
401}
402
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000403static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000404set_changed(int i)
405{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000406 ptes[i].changed = 1;
407}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000408#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000409
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000410static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000411get_part_table(int i)
412{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000413 return ptes[i].part_table;
414}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000415
416static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000417str_units(int n)
418{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000419 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000420 return display_in_cyl_units ? "cylinder" : "sector";
421 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000422}
423
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000424static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000425valid_part_table_flag(const char *mbuffer)
426{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000427 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000428}
429
Denis Vlasenko834410a2006-11-29 12:00:28 +0000430#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000431static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000432write_part_table_flag(char *b)
433{
434 b[510] = 0x55;
435 b[511] = 0xaa;
436}
437
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000438static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000439read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000440{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000441 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000442 return *line_ptr;
443}
444
445static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000446read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000447{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000448 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000449 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000450 line_ptr[0] = '\n';
451 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000452 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000453 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000454}
455
456static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000457read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000458{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000459 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000460 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000461 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000462 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000463 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000464 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000465 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000466 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000467 if (v > 0xff)
468 /* Bad input also triggers this */
469 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000470 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000471 }
472}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000473#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000474
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000475#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000476
477typedef struct {
478 unsigned char info[128]; /* Informative text string */
479 unsigned char spare0[14];
480 struct sun_info {
481 unsigned char spare1;
482 unsigned char id;
483 unsigned char spare2;
484 unsigned char flags;
485 } infos[8];
486 unsigned char spare1[246]; /* Boot information etc. */
487 unsigned short rspeed; /* Disk rotational speed */
488 unsigned short pcylcount; /* Physical cylinder count */
489 unsigned short sparecyl; /* extra sects per cylinder */
490 unsigned char spare2[4]; /* More magic... */
491 unsigned short ilfact; /* Interleave factor */
492 unsigned short ncyl; /* Data cylinder count */
493 unsigned short nacyl; /* Alt. cylinder count */
494 unsigned short ntrks; /* Tracks per cylinder */
495 unsigned short nsect; /* Sectors per track */
496 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000497 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000498 uint32_t start_cylinder;
499 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000500 } partitions[8];
501 unsigned short magic; /* Magic number */
502 unsigned short csum; /* Label xor'd checksum */
503} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000504#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000505STATIC_OSF void bsd_select(void);
506STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000507#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000508
Denis Vlasenko28703012006-12-19 20:32:02 +0000509#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000510static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000511fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000512{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000513 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000514}
515
Rob Landley88621d72006-08-29 19:41:06 +0000516static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000517fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000518{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000519 return (x << 24) |
520 ((x & 0xFF00) << 8) |
521 ((x & 0xFF0000) >> 8) |
522 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000523}
524#endif
525
Denis Vlasenkobd852072007-03-19 14:43:38 +0000526STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000527STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000528STATIC_SGI int sgi_get_sysid(int i);
529STATIC_SGI void sgi_delete_partition(int i);
530STATIC_SGI void sgi_change_sysid(int i, int sys);
531STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000532#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000533STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000534#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000535STATIC_SGI int verify_sgi(int verbose);
536STATIC_SGI void sgi_add_partition(int n, int sys);
537STATIC_SGI void sgi_set_swappartition(int i);
538STATIC_SGI const char *sgi_get_bootfile(void);
539STATIC_SGI void sgi_set_bootfile(const char* aFile);
540STATIC_SGI void create_sgiinfo(void);
541STATIC_SGI void sgi_write_table(void);
542STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000543#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000544
Denis Vlasenkobd852072007-03-19 14:43:38 +0000545STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000546STATIC_SUN void sun_delete_partition(int i);
547STATIC_SUN void sun_change_sysid(int i, int sys);
548STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000549STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000550#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000551STATIC_SUN void sun_set_alt_cyl(void);
552STATIC_SUN void sun_set_ncyl(int cyl);
553STATIC_SUN void sun_set_xcyl(void);
554STATIC_SUN void sun_set_ilfact(void);
555STATIC_SUN void sun_set_rspeed(void);
556STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000557#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000558STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
559STATIC_SUN void verify_sun(void);
560STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000561#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000562
Denis Vlasenko834410a2006-11-29 12:00:28 +0000563#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000564/* start_sect and nr_sects are stored little endian on all machines */
565/* moreover, they are not aligned correctly */
566static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000567store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000568{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000569 cp[0] = val;
570 cp[1] = val >> 8;
571 cp[2] = val >> 16;
572 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000573}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000574#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000575
Denis Vlasenko834410a2006-11-29 12:00:28 +0000576static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000577read4_little_endian(const unsigned char *cp)
578{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000579 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000580}
581
Denis Vlasenko834410a2006-11-29 12:00:28 +0000582#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000583static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000584set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000585{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000586 store4_little_endian(p->start4, start_sect);
587}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000588#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589
Denis Vlasenko28703012006-12-19 20:32:02 +0000590static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000591get_start_sect(const struct partition *p)
592{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000593 return read4_little_endian(p->start4);
594}
595
Denis Vlasenko834410a2006-11-29 12:00:28 +0000596#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000598set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000599{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000600 store4_little_endian(p->size4, nr_sects);
601}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000602#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603
Denis Vlasenko28703012006-12-19 20:32:02 +0000604static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000605get_nr_sects(const struct partition *p)
606{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000607 return read4_little_endian(p->size4);
608}
609
610/* normally O_RDWR, -l option gives O_RDONLY */
611static int type_open = O_RDWR;
612
Rob Landleyb73451d2006-02-24 16:29:00 +0000613static int ext_index; /* the prime extended partition */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000614static int listing; /* no aborts for fdisk -l */
Rob Landleyb73451d2006-02-24 16:29:00 +0000615static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000616#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000617static int dos_changed;
618static int nowarn; /* no warnings for fdisk -l/-s */
619#endif
620
Denis Vlasenko834410a2006-11-29 12:00:28 +0000621static unsigned user_cylinders, user_heads, user_sectors;
622static unsigned pt_heads, pt_sectors;
623static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000625static ullong extended_offset; /* offset of link pointers */
626static ullong total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000628static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000629{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000630 if (listing) {
631 close(fd);
632 longjmp(listingbuf, 1);
633 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000634 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000635}
636
637static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000638seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000639{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000640 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000641#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000642 if (lseek64(fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000643 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000644#else
645 if (secno > MAXINT(off_t)
646 || lseek(fd, (off_t)secno, SEEK_SET) == (off_t) -1
647 ) {
648 fdisk_fatal(unable_to_seek);
649 }
650#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000651}
652
Denis Vlasenko834410a2006-11-29 12:00:28 +0000653#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000654static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000655write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000656{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000657 seek_sector(secno);
658 if (write(fd, buf, sector_size) != sector_size)
659 fdisk_fatal(unable_to_write);
660}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000661#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000662
663/* Allocate a buffer and read a partition table sector */
664static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000665read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000666{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000667 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000668 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000669 seek_sector(offset);
670 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
671 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000672#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000673 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000674#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000675 pe->part_table = pe->ext_pointer = NULL;
676}
677
Denis Vlasenko834410a2006-11-29 12:00:28 +0000678static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000679get_partition_start(const struct pte *pe)
680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000681 return pe->offset + get_start_sect(pe->part_table);
682}
683
Denis Vlasenko834410a2006-11-29 12:00:28 +0000684#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685/*
686 * Avoid warning about DOS partitions when no DOS partition was changed.
687 * Here a heuristic "is probably dos partition".
688 * We might also do the opposite and warn in all cases except
689 * for "is probably nondos partition".
690 */
691static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000692is_dos_partition(int t)
693{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000694 return (t == 1 || t == 4 || t == 6 ||
695 t == 0x0b || t == 0x0c || t == 0x0e ||
696 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
697 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
698 t == 0xc1 || t == 0xc4 || t == 0xc6);
699}
700
701static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000702menu(void)
703{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000704 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000705 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000706 puts("a\ttoggle a read only flag"); /* sun */
707 puts("b\tedit bsd disklabel");
708 puts("c\ttoggle the mountable flag"); /* sun */
709 puts("d\tdelete a partition");
710 puts("l\tlist known partition types");
711 puts("n\tadd a new partition");
712 puts("o\tcreate a new empty DOS partition table");
713 puts("p\tprint the partition table");
714 puts("q\tquit without saving changes");
715 puts("s\tcreate a new empty Sun disklabel"); /* sun */
716 puts("t\tchange a partition's system id");
717 puts("u\tchange display/entry units");
718 puts("v\tverify the partition table");
719 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000720#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000721 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000722#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000723 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000724 puts("a\tselect bootable partition"); /* sgi flavour */
725 puts("b\tedit bootfile entry"); /* sgi */
726 puts("c\tselect sgi swap partition"); /* sgi flavour */
727 puts("d\tdelete a partition");
728 puts("l\tlist known partition types");
729 puts("n\tadd a new partition");
730 puts("o\tcreate a new empty DOS partition table");
731 puts("p\tprint the partition table");
732 puts("q\tquit without saving changes");
733 puts("s\tcreate a new empty Sun disklabel"); /* sun */
734 puts("t\tchange a partition's system id");
735 puts("u\tchange display/entry units");
736 puts("v\tverify the partition table");
737 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000738 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000739 puts("o\tcreate a new empty DOS partition table");
740 puts("q\tquit without saving changes");
741 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000742 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000743 puts("a\ttoggle a bootable flag");
744 puts("b\tedit bsd disklabel");
745 puts("c\ttoggle the dos compatibility flag");
746 puts("d\tdelete a partition");
747 puts("l\tlist known partition types");
748 puts("n\tadd a new partition");
749 puts("o\tcreate a new empty DOS partition table");
750 puts("p\tprint the partition table");
751 puts("q\tquit without saving changes");
752 puts("s\tcreate a new empty Sun disklabel"); /* sun */
753 puts("t\tchange a partition's system id");
754 puts("u\tchange display/entry units");
755 puts("v\tverify the partition table");
756 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000757#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000758 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000759#endif
760 }
761}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000762#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000763
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000764
Denis Vlasenko834410a2006-11-29 12:00:28 +0000765#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000766static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000767xmenu(void)
768{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000769 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000770 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000771 puts("a\tchange number of alternate cylinders"); /*sun*/
772 puts("c\tchange number of cylinders");
773 puts("d\tprint the raw data in the partition table");
774 puts("e\tchange number of extra sectors per cylinder");/*sun*/
775 puts("h\tchange number of heads");
776 puts("i\tchange interleave factor"); /*sun*/
777 puts("o\tchange rotation speed (rpm)"); /*sun*/
778 puts("p\tprint the partition table");
779 puts("q\tquit without saving changes");
780 puts("r\treturn to main menu");
781 puts("s\tchange number of sectors/track");
782 puts("v\tverify the partition table");
783 puts("w\twrite table to disk and exit");
784 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000785 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000786 puts("b\tmove beginning of data in a partition"); /* !sun */
787 puts("c\tchange number of cylinders");
788 puts("d\tprint the raw data in the partition table");
789 puts("e\tlist extended partitions"); /* !sun */
790 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
791 puts("h\tchange number of heads");
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");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000798 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000799 puts("b\tmove beginning of data in a partition"); /* !sun */
800 puts("c\tchange number of cylinders");
801 puts("d\tprint the raw data in the partition table");
802 puts("e\tlist extended partitions"); /* !sun */
803 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
804 puts("h\tchange number of heads");
805 puts("p\tprint the partition table");
806 puts("q\tquit without saving changes");
807 puts("r\treturn to main menu");
808 puts("s\tchange number of sectors/track");
809 puts("v\tverify the partition table");
810 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000811 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000812 puts("b\tmove beginning of data in a partition"); /* !sun */
813 puts("c\tchange number of cylinders");
814 puts("d\tprint the raw data in the partition table");
815 puts("e\tlist extended partitions"); /* !sun */
816 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000817#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000818 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000819#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000820 puts("h\tchange number of heads");
821 puts("p\tprint the partition table");
822 puts("q\tquit without saving changes");
823 puts("r\treturn to main menu");
824 puts("s\tchange number of sectors/track");
825 puts("v\tverify the partition table");
826 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000827 }
828}
829#endif /* ADVANCED mode */
830
Denis Vlasenko834410a2006-11-29 12:00:28 +0000831#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000833get_sys_types(void)
834{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000835 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000836 LABEL_IS_SUN ? sun_sys_types :
837 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000838 i386_sys_types);
839}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000840#else
841#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000842#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000843
Denis Vlasenkobd852072007-03-19 14:43:38 +0000844static const char *
845partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000846{
847 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000848 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000849
Denis Vlasenkobd852072007-03-19 14:43:38 +0000850 for (i = 0; types[i]; i++)
851 if ((unsigned char)types[i][0] == type)
852 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000853
Denis Vlasenkobd852072007-03-19 14:43:38 +0000854 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000855}
856
857
Denis Vlasenko834410a2006-11-29 12:00:28 +0000858#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000859static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000860get_sysid(int i)
861{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000862 return LABEL_IS_SUN ? sunlabel->infos[i].id :
863 (LABEL_IS_SGI ? sgi_get_sysid(i) :
864 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000865}
866
Denis Vlasenkobd852072007-03-19 14:43:38 +0000867static void
868list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000869{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000870 enum { COLS = 3 };
871
872 unsigned last[COLS];
873 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000874 int i;
875
Denis Vlasenkobd852072007-03-19 14:43:38 +0000876 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000877
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000878 done = 0;
879 for (i = COLS-1; i >= 0; i--) {
880 done += (size + i - done) / (i + 1);
881 last[COLS-1 - i] = done;
882 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000885 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000886 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000887 (unsigned char)sys[next][0],
888 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000889 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000890 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000891 i = 0;
892 next = ++done;
893 }
894 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000895 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000896}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000897#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000898
899static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000900is_cleared_partition(const struct partition *p)
901{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
903 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
904 get_start_sect(p) || get_nr_sects(p));
905}
906
907static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000908clear_partition(struct partition *p)
909{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000910 if (!p)
911 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000912 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000913}
914
Denis Vlasenko834410a2006-11-29 12:00:28 +0000915#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000916static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000917set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000918{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000919 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000920 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000921
922 if (doext) {
923 p = ptes[i].ext_pointer;
924 offset = extended_offset;
925 } else {
926 p = ptes[i].part_table;
927 offset = ptes[i].offset;
928 }
929 p->boot_ind = 0;
930 p->sys_ind = sysid;
931 set_start_sect(p, start - offset);
932 set_nr_sects(p, stop - start + 1);
933 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
934 start = heads*sectors*1024 - 1;
935 set_hsc(p->head, p->sector, p->cyl, start);
936 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
937 stop = heads*sectors*1024 - 1;
938 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
939 ptes[i].changed = 1;
940}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000941#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000942
943static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000944warn_geometry(void)
945{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000946 if (heads && sectors && cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000948
Denis Vlasenkobd852072007-03-19 14:43:38 +0000949 printf("Unknown value(s) for:");
950 if (!heads)
951 printf(" heads");
952 if (!sectors)
953 printf(" sectors");
954 if (!cylinders)
955 printf(" cylinders");
956 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000957#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000958 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000959#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000960 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000961 return 1;
962}
963
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000964static void
965update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000966{
967 int cyl_units = heads * sectors;
968
969 if (display_in_cyl_units && cyl_units)
970 units_per_sector = cyl_units;
971 else
972 units_per_sector = 1; /* in sectors */
973}
974
Denis Vlasenko834410a2006-11-29 12:00:28 +0000975#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000976static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000977warn_cylinders(void)
978{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000979 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000980 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000981"The number of cylinders for this disk is set to %d.\n"
982"There is nothing wrong with that, but this is larger than 1024,\n"
983"and could in certain setups cause problems with:\n"
984"1) software that runs at boot time (e.g., old versions of LILO)\n"
985"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +0000986" (e.g., DOS FDISK, OS/2 FDISK)\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000987 cylinders);
988}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000989#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000990
991static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000992read_extended(int ext)
993{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000994 int i;
995 struct pte *pex;
996 struct partition *p, *q;
997
998 ext_index = ext;
999 pex = &ptes[ext];
1000 pex->ext_pointer = pex->part_table;
1001
1002 p = pex->part_table;
1003 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001004 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001005 return;
1006 }
1007
Rob Landleyb73451d2006-02-24 16:29:00 +00001008 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001009 struct pte *pe = &ptes[partitions];
1010
1011 if (partitions >= MAXIMUM_PARTS) {
1012 /* This is not a Linux restriction, but
1013 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001014 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001015 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001016#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001017 printf("Warning: deleting partitions after %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001018 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001019 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001020#endif
1021 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001022 return;
1023 }
1024
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001025 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026
1027 if (!extended_offset)
1028 extended_offset = get_start_sect(p);
1029
1030 q = p = pt_offset(pe->sectorbuffer, 0);
1031 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001032 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001033 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001034 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001035 "pointer in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001036 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001037 else
1038 pe->ext_pointer = p;
1039 } else if (p->sys_ind) {
1040 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001041 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042 "data in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001043 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001044 else
1045 pe->part_table = p;
1046 }
1047 }
1048
1049 /* very strange code here... */
1050 if (!pe->part_table) {
1051 if (q != pe->ext_pointer)
1052 pe->part_table = q;
1053 else
1054 pe->part_table = q + 1;
1055 }
1056 if (!pe->ext_pointer) {
1057 if (q != pe->part_table)
1058 pe->ext_pointer = q;
1059 else
1060 pe->ext_pointer = q + 1;
1061 }
1062
1063 p = pe->ext_pointer;
1064 partitions++;
1065 }
1066
Denis Vlasenko834410a2006-11-29 12:00:28 +00001067#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001068 /* remove empty links */
1069 remove:
1070 for (i = 4; i < partitions; i++) {
1071 struct pte *pe = &ptes[i];
1072
Denis Vlasenkobd852072007-03-19 14:43:38 +00001073 if (!get_nr_sects(pe->part_table)
1074 && (partitions > 5 || ptes[4].part_table->sys_ind)
1075 ) {
1076 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001077 delete_partition(i);
1078 goto remove; /* numbering changed */
1079 }
1080 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001081#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001082}
1083
Denis Vlasenko834410a2006-11-29 12:00:28 +00001084#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001085static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001086create_doslabel(void)
1087{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001088 int i;
1089
Denis Vlasenkobd852072007-03-19 14:43:38 +00001090 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001091
1092 current_label_type = label_dos;
1093
Denis Vlasenko834410a2006-11-29 12:00:28 +00001094#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001095 possibly_osf_label = 0;
1096#endif
1097 partitions = 4;
1098
1099 for (i = 510-64; i < 510; i++)
1100 MBRbuffer[i] = 0;
1101 write_part_table_flag(MBRbuffer);
1102 extended_offset = 0;
1103 set_all_unchanged();
1104 set_changed(0);
1105 get_boot(create_empty_dos);
1106}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001107#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001108
1109static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001110get_sectorsize(void)
1111{
Rob Landley736e5252006-02-25 03:36:00 +00001112 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001113 int arg;
1114 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1115 sector_size = arg;
1116 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001117 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001118 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001119 }
1120}
1121
Rob Landley88621d72006-08-29 19:41:06 +00001122static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001123get_kernel_geometry(void)
1124{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125 struct hd_geometry geometry;
1126
1127 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1128 kern_heads = geometry.heads;
1129 kern_sectors = geometry.sectors;
1130 /* never use geometry.cylinders - it is truncated */
1131 }
1132}
1133
1134static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001135get_partition_table_geometry(void)
1136{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001137 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001138 struct partition *p;
1139 int i, h, s, hh, ss;
1140 int first = 1;
1141 int bad = 0;
1142
Eric Andersen3496fdc2006-01-30 23:09:20 +00001143 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001144 return;
1145
1146 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001147 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 p = pt_offset(bufp, i);
1149 if (p->sys_ind != 0) {
1150 h = p->end_head + 1;
1151 s = (p->end_sector & 077);
1152 if (first) {
1153 hh = h;
1154 ss = s;
1155 first = 0;
1156 } else if (hh != h || ss != s)
1157 bad = 1;
1158 }
1159 }
1160
1161 if (!first && !bad) {
1162 pt_heads = hh;
1163 pt_sectors = ss;
1164 }
1165}
1166
Rob Landleyb73451d2006-02-24 16:29:00 +00001167static void
1168get_geometry(void)
1169{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001170 int sec_fac;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001171 uint64_t v64;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172
1173 get_sectorsize();
1174 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001175#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001176 guess_device_type();
1177#endif
1178 heads = cylinders = sectors = 0;
1179 kern_heads = kern_sectors = 0;
1180 pt_heads = pt_sectors = 0;
1181
1182 get_kernel_geometry();
1183 get_partition_table_geometry();
1184
1185 heads = user_heads ? user_heads :
1186 pt_heads ? pt_heads :
1187 kern_heads ? kern_heads : 255;
1188 sectors = user_sectors ? user_sectors :
1189 pt_sectors ? pt_sectors :
1190 kern_sectors ? kern_sectors : 63;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001191 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
1192 /* got bytes, convert to 512 byte sectors */
1193 total_number_of_sectors = (v64 >> 9);
Eric Andersen040f4402003-07-30 08:40:37 +00001194 } else {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001195 unsigned long longsectors; /* need temp of type long */
1196 if (ioctl(fd, BLKGETSIZE, &longsectors))
1197 longsectors = 0;
1198 total_number_of_sectors = longsectors;
Eric Andersen040f4402003-07-30 08:40:37 +00001199 }
1200
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001201 sector_offset = 1;
1202 if (dos_compatible_flag)
1203 sector_offset = sectors;
1204
Eric Andersen040f4402003-07-30 08:40:37 +00001205 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206 if (!cylinders)
1207 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001208}
1209
1210/*
1211 * Read MBR. Returns:
1212 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1213 * 0: found or created label
1214 * 1: I/O error
1215 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001216static int
1217get_boot(enum action what)
1218{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219 int i;
1220
1221 partitions = 4;
1222
1223 for (i = 0; i < 4; i++) {
1224 struct pte *pe = &ptes[i];
1225
1226 pe->part_table = pt_offset(MBRbuffer, i);
1227 pe->ext_pointer = NULL;
1228 pe->offset = 0;
1229 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001230#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001231 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001232#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001233 }
1234
Denis Vlasenko834410a2006-11-29 12:00:28 +00001235#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236 if (what == create_empty_sun && check_sun_label())
1237 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001238#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001239
1240 memset(MBRbuffer, 0, 512);
1241
Denis Vlasenko834410a2006-11-29 12:00:28 +00001242#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 if (what == create_empty_dos)
1244 goto got_dos_table; /* skip reading disk */
1245
Denis Vlasenkobd852072007-03-19 14:43:38 +00001246 fd = open(disk_device, type_open);
1247 if (fd < 0) {
1248 fd = open(disk_device, O_RDONLY);
1249 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001250 if (what == try_only)
1251 return 1;
1252 fdisk_fatal(unable_to_open);
1253 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001254 printf("You will not be able to write "
1255 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001256 }
1257
1258 if (512 != read(fd, MBRbuffer, 512)) {
1259 if (what == try_only)
1260 return 1;
1261 fdisk_fatal(unable_to_read);
1262 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001263#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001264 fd = open(disk_device, O_RDONLY);
1265 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001266 return 1;
1267 if (512 != read(fd, MBRbuffer, 512))
1268 return 1;
1269#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001270
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001271 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001272
1273 update_units();
1274
Denis Vlasenko834410a2006-11-29 12:00:28 +00001275#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001276 if (check_sun_label())
1277 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001278#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001279
Denis Vlasenko834410a2006-11-29 12:00:28 +00001280#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001281 if (check_sgi_label())
1282 return 0;
1283#endif
1284
Denis Vlasenko834410a2006-11-29 12:00:28 +00001285#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286 if (check_aix_label())
1287 return 0;
1288#endif
1289
Denis Vlasenko834410a2006-11-29 12:00:28 +00001290#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001291 if (check_osf_label()) {
1292 possibly_osf_label = 1;
1293 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001294 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 return 0;
1296 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001297 printf("This disk has both DOS and BSD magic.\n"
1298 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001299 }
1300#endif
1301
Denis Vlasenko834410a2006-11-29 12:00:28 +00001302#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001303 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001304#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001305
1306 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001307#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001308 return -1;
1309#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001310 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001311 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001312 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001314 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001316#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001317 create_sunlabel();
1318#endif
1319#else
1320 create_doslabel();
1321#endif
1322 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001323 case try_only:
1324 return -1;
1325 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001326#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001328#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329 break;
1330 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001331 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001332 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001333#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001334 }
1335
Denis Vlasenko834410a2006-11-29 12:00:28 +00001336#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001338#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001339 warn_geometry();
1340
1341 for (i = 0; i < 4; i++) {
1342 struct pte *pe = &ptes[i];
1343
Rob Landleyb73451d2006-02-24 16:29:00 +00001344 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001345 if (partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001346 printf("Ignoring extra extended "
1347 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348 else
1349 read_extended(i);
1350 }
1351 }
1352
1353 for (i = 3; i < partitions; i++) {
1354 struct pte *pe = &ptes[i];
1355
1356 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001357 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1358 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001359 pe->sectorbuffer[510],
1360 pe->sectorbuffer[511],
1361 i + 1);
1362#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001363 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001364#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 }
1366 }
1367
1368 return 0;
1369}
1370
Denis Vlasenko834410a2006-11-29 12:00:28 +00001371#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001372/*
1373 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1374 * If the user hits Enter, DFLT is returned.
1375 * Answers like +10 are interpreted as offsets from BASE.
1376 *
1377 * There is no default if DFLT is not between LOW and HIGH.
1378 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001379static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001380read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001381{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001382 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001383 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001384 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001385
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001386 if (dflt < low || dflt > high) {
1387 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001388 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001389 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001390
1391 while (1) {
1392 int use_default = default_ok;
1393
1394 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001395 do {
1396 printf(fmt, mesg, low, high, dflt);
1397 read_maybe_empty("");
1398 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1399 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400
Eric Andersen84bdea82004-05-19 10:49:17 +00001401 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001402 int minus = (*line_ptr == '-');
1403 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001404
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001405 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001406
Rob Landleyb73451d2006-02-24 16:29:00 +00001407 while (isdigit(*++line_ptr))
1408 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001409
Rob Landleyb73451d2006-02-24 16:29:00 +00001410 switch (*line_ptr) {
1411 case 'c':
1412 case 'C':
1413 if (!display_in_cyl_units)
1414 i *= heads * sectors;
1415 break;
1416 case 'K':
1417 absolute = 1024;
1418 break;
1419 case 'k':
1420 absolute = 1000;
1421 break;
1422 case 'm':
1423 case 'M':
1424 absolute = 1000000;
1425 break;
1426 case 'g':
1427 case 'G':
1428 absolute = 1000000000;
1429 break;
1430 default:
1431 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001433 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001434 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001435 unsigned long unit;
1436
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001437 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001438 unit = sector_size * units_per_sector;
1439 bytes += unit/2; /* round */
1440 bytes /= unit;
1441 i = bytes;
1442 }
1443 if (minus)
1444 i = -i;
1445 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001446 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001447 i = atoi(line_ptr);
1448 while (isdigit(*line_ptr)) {
1449 line_ptr++;
1450 use_default = 0;
1451 }
1452 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001453 if (use_default) {
1454 i = dflt;
1455 printf("Using default value %u\n", i);
1456 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001457 if (i >= low && i <= high)
1458 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001459 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001460 }
1461 return i;
1462}
1463
Rob Landleyb73451d2006-02-24 16:29:00 +00001464static int
1465get_partition(int warn, int max)
1466{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001467 struct pte *pe;
1468 int i;
1469
Denis Vlasenkobd852072007-03-19 14:43:38 +00001470 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471 pe = &ptes[i];
1472
1473 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001474 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1475 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1476 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1477 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001478 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001479 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001480 }
1481 return i;
1482}
1483
1484static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001485get_existing_partition(int warn, int max)
1486{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001487 int pno = -1;
1488 int i;
1489
1490 for (i = 0; i < max; i++) {
1491 struct pte *pe = &ptes[i];
1492 struct partition *p = pe->part_table;
1493
1494 if (p && !is_cleared_partition(p)) {
1495 if (pno >= 0)
1496 goto not_unique;
1497 pno = i;
1498 }
1499 }
1500 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001501 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001502 return pno;
1503 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001504 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001505 return -1;
1506
1507 not_unique:
1508 return get_partition(warn, max);
1509}
1510
1511static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001512get_nonexisting_partition(int warn, int max)
1513{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001514 int pno = -1;
1515 int i;
1516
1517 for (i = 0; i < max; i++) {
1518 struct pte *pe = &ptes[i];
1519 struct partition *p = pe->part_table;
1520
1521 if (p && is_cleared_partition(p)) {
1522 if (pno >= 0)
1523 goto not_unique;
1524 pno = i;
1525 }
1526 }
1527 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001528 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001529 return pno;
1530 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001531 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001532 return -1;
1533
1534 not_unique:
1535 return get_partition(warn, max);
1536}
1537
1538
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001539static void
1540change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001541{
1542 display_in_cyl_units = !display_in_cyl_units;
1543 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001544 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001545 str_units(PLURAL));
1546}
1547
1548static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001549toggle_active(int i)
1550{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 struct pte *pe = &ptes[i];
1552 struct partition *p = pe->part_table;
1553
Rob Landleyb73451d2006-02-24 16:29:00 +00001554 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001555 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001556 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1557 pe->changed = 1;
1558}
1559
1560static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001561toggle_dos_compatibility_flag(void)
1562{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001563 dos_compatible_flag = ~dos_compatible_flag;
1564 if (dos_compatible_flag) {
1565 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001566 printf("DOS Compatibility flag is set\n");
1567 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001568 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001569 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 }
1571}
1572
1573static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001574delete_partition(int i)
1575{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001576 struct pte *pe = &ptes[i];
1577 struct partition *p = pe->part_table;
1578 struct partition *q = pe->ext_pointer;
1579
1580/* Note that for the fifth partition (i == 4) we don't actually
1581 * decrement partitions.
1582 */
1583
1584 if (warn_geometry())
1585 return; /* C/H/S not set */
1586 pe->changed = 1;
1587
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001588 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001589 sun_delete_partition(i);
1590 return;
1591 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001592 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001593 sgi_delete_partition(i);
1594 return;
1595 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596
1597 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001598 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001599 partitions = 4;
1600 ptes[ext_index].ext_pointer = NULL;
1601 extended_offset = 0;
1602 }
1603 clear_partition(p);
1604 return;
1605 }
1606
1607 if (!q->sys_ind && i > 4) {
1608 /* the last one in the chain - just delete */
1609 --partitions;
1610 --i;
1611 clear_partition(ptes[i].ext_pointer);
1612 ptes[i].changed = 1;
1613 } else {
1614 /* not the last one - further ones will be moved down */
1615 if (i > 4) {
1616 /* delete this link in the chain */
1617 p = ptes[i-1].ext_pointer;
1618 *p = *q;
1619 set_start_sect(p, get_start_sect(q));
1620 set_nr_sects(p, get_nr_sects(q));
1621 ptes[i-1].changed = 1;
1622 } else if (partitions > 5) { /* 5 will be moved to 4 */
1623 /* the first logical in a longer chain */
1624 pe = &ptes[5];
1625
1626 if (pe->part_table) /* prevent SEGFAULT */
1627 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001628 get_partition_start(pe) -
1629 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001630 pe->offset = extended_offset;
1631 pe->changed = 1;
1632 }
1633
1634 if (partitions > 5) {
1635 partitions--;
1636 while (i < partitions) {
1637 ptes[i] = ptes[i+1];
1638 i++;
1639 }
1640 } else
1641 /* the only logical: clear only */
1642 clear_partition(ptes[i].part_table);
1643 }
1644}
1645
1646static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001647change_sysid(void)
1648{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001649 int i, sys, origsys;
1650 struct partition *p;
1651
Eric Andersen040f4402003-07-30 08:40:37 +00001652 /* If sgi_label then don't use get_existing_partition,
1653 let the user select a partition, since get_existing_partition()
1654 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001655 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001656 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001657 } else {
1658 i = get_partition(0, partitions);
1659 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001660 if (i == -1)
1661 return;
1662 p = ptes[i].part_table;
1663 origsys = sys = get_sysid(i);
1664
1665 /* if changing types T to 0 is allowed, then
1666 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001667 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001668 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001669 return;
1670 }
1671 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001672 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001673
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001674 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001675 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001676 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001677 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001678 /* break; */
1679 }
1680
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001681 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001682 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001683 printf("You cannot change a partition into"
1684 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001685 break;
1686 }
1687 }
1688
1689 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001690#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001691 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001692 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001693 "as Whole disk (5),\n"
1694 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001695 "even Linux likes it\n\n");
1696#endif
1697#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001698 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001699 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001700 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001701 (i == 8 && sys != 0)
1702 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001703 ) {
1704 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001705 "as volume header (0),\nand "
1706 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001707 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001708 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001709#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001710 if (sys == origsys)
1711 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001712 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001713 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001714 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001715 sgi_change_sysid(i, sys);
1716 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001718
Denis Vlasenkobd852072007-03-19 14:43:38 +00001719 printf("Changed system type of partition %d "
1720 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001721 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001722 ptes[i].changed = 1;
1723 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001724 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001725 dos_changed = 1;
1726 break;
1727 }
1728 }
1729}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001730#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001731
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001732
Denis Vlasenko28703012006-12-19 20:32:02 +00001733/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001734 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1735 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1736 * Lubkin Oct. 1991). */
1737
Rob Landleyb73451d2006-02-24 16:29:00 +00001738static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001739linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001740{
1741 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001742
1743 *c = ls / spc;
1744 ls = ls % spc;
1745 *h = ls / sectors;
1746 *s = ls % sectors + 1; /* sectors count from 1 */
1747}
1748
Rob Landleyb73451d2006-02-24 16:29:00 +00001749static void
1750check_consistency(const struct partition *p, int partition)
1751{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001752 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1753 unsigned pec, peh, pes; /* physical ending c, h, s */
1754 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1755 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756
1757 if (!heads || !sectors || (partition >= 4))
1758 return; /* do not check extended partitions */
1759
1760/* physical beginning c, h, s */
1761 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1762 pbh = p->head;
1763 pbs = p->sector & 0x3f;
1764
1765/* physical ending c, h, s */
1766 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1767 peh = p->end_head;
1768 pes = p->end_sector & 0x3f;
1769
1770/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001771 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001772
1773/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001774 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001775
1776/* Same physical / logical beginning? */
1777 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001778 printf("Partition %d has different physical/logical "
1779 "beginnings (non-Linux?):\n", partition + 1);
1780 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1781 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001782 }
1783
1784/* Same physical / logical ending? */
1785 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001786 printf("Partition %d has different physical/logical "
1787 "endings:\n", partition + 1);
1788 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1789 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001790 }
1791
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001792/* Ending on cylinder boundary? */
1793 if (peh != (heads - 1) || pes != sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001794 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001795 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001796 }
1797}
1798
1799static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001800list_disk_geometry(void)
1801{
Eric Andersen040f4402003-07-30 08:40:37 +00001802 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001803 long megabytes = bytes/1000000;
1804
1805 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001806 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001807 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001808 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001809 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001810 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001811 printf("%d heads, %d sectors/track, %d cylinders",
Rob Landleyb73451d2006-02-24 16:29:00 +00001812 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001814 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001815 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001816 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001817 str_units(PLURAL),
1818 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001819}
1820
1821/*
1822 * Check whether partition entries are ordered by their starting positions.
1823 * Return 0 if OK. Return i if partition i should have been earlier.
1824 * Two separate checks: primary and logical partitions.
1825 */
1826static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001827wrong_p_order(int *prev)
1828{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001829 const struct pte *pe;
1830 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001831 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001832 int i, last_i = 0;
1833
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001834 for (i = 0; i < partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001835 if (i == 4) {
1836 last_i = 4;
1837 last_p_start_pos = 0;
1838 }
1839 pe = &ptes[i];
1840 if ((p = pe->part_table)->sys_ind) {
1841 p_start_pos = get_partition_start(pe);
1842
1843 if (last_p_start_pos > p_start_pos) {
1844 if (prev)
1845 *prev = last_i;
1846 return i;
1847 }
1848
1849 last_p_start_pos = p_start_pos;
1850 last_i = i;
1851 }
1852 }
1853 return 0;
1854}
1855
Denis Vlasenko834410a2006-11-29 12:00:28 +00001856#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001857/*
1858 * Fix the chain of logicals.
1859 * extended_offset is unchanged, the set of sectors used is unchanged
1860 * The chain is sorted so that sectors increase, and so that
1861 * starting sectors increase.
1862 *
1863 * After this it may still be that cfdisk doesnt like the table.
1864 * (This is because cfdisk considers expanded parts, from link to
1865 * end of partition, and these may still overlap.)
1866 * Now
1867 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1868 * may help.
1869 */
1870static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001871fix_chain_of_logicals(void)
1872{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873 int j, oj, ojj, sj, sjj;
1874 struct partition *pj,*pjj,tmp;
1875
1876 /* Stage 1: sort sectors but leave sector of part 4 */
1877 /* (Its sector is the global extended_offset.) */
1878 stage1:
1879 for (j = 5; j < partitions-1; j++) {
1880 oj = ptes[j].offset;
1881 ojj = ptes[j+1].offset;
1882 if (oj > ojj) {
1883 ptes[j].offset = ojj;
1884 ptes[j+1].offset = oj;
1885 pj = ptes[j].part_table;
1886 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1887 pjj = ptes[j+1].part_table;
1888 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1889 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001890 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001891 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001892 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001893 goto stage1;
1894 }
1895 }
1896
1897 /* Stage 2: sort starting sectors */
1898 stage2:
1899 for (j = 4; j < partitions-1; j++) {
1900 pj = ptes[j].part_table;
1901 pjj = ptes[j+1].part_table;
1902 sj = get_start_sect(pj);
1903 sjj = get_start_sect(pjj);
1904 oj = ptes[j].offset;
1905 ojj = ptes[j+1].offset;
1906 if (oj+sj > ojj+sjj) {
1907 tmp = *pj;
1908 *pj = *pjj;
1909 *pjj = tmp;
1910 set_start_sect(pj, ojj+sjj-oj);
1911 set_start_sect(pjj, oj+sj-ojj);
1912 goto stage2;
1913 }
1914 }
1915
1916 /* Probably something was changed */
1917 for (j = 4; j < partitions; j++)
1918 ptes[j].changed = 1;
1919}
1920
1921
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001923fix_partition_table_order(void)
1924{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001925 struct pte *pei, *pek;
1926 int i,k;
1927
1928 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001929 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930 return;
1931 }
1932
1933 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1934 /* partition i should have come earlier, move it */
1935 /* We have to move data in the MBR */
1936 struct partition *pi, *pk, *pe, pbuf;
1937 pei = &ptes[i];
1938 pek = &ptes[k];
1939
1940 pe = pei->ext_pointer;
1941 pei->ext_pointer = pek->ext_pointer;
1942 pek->ext_pointer = pe;
1943
1944 pi = pei->part_table;
1945 pk = pek->part_table;
1946
1947 memmove(&pbuf, pi, sizeof(struct partition));
1948 memmove(pi, pk, sizeof(struct partition));
1949 memmove(pk, &pbuf, sizeof(struct partition));
1950
1951 pei->changed = pek->changed = 1;
1952 }
1953
1954 if (i)
1955 fix_chain_of_logicals();
1956
1957 printf("Done.\n");
1958
1959}
1960#endif
1961
1962static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001963list_table(int xtra)
1964{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001965 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001966 int i, w;
1967
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001968 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001969 sun_list_table(xtra);
1970 return;
1971 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001972 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001973 sgi_list_table(xtra);
1974 return;
1975 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001976
1977 list_disk_geometry();
1978
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001979 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001980 xbsd_print_disklabel(xtra);
1981 return;
1982 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001983
1984 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
1985 but if the device name ends in a digit, say /dev/foo1,
1986 then the partition is called /dev/foo1p3. */
1987 w = strlen(disk_device);
1988 if (w && isdigit(disk_device[w-1]))
1989 w++;
1990 if (w < 5)
1991 w = 5;
1992
Denis Vlasenkobd852072007-03-19 14:43:38 +00001993 // 1 12345678901 12345678901 12345678901 12
1994 printf("%*s Boot Start End Blocks Id System\n",
1995 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001996
1997 for (i = 0; i < partitions; i++) {
1998 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001999 ullong psects;
2000 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002001 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002002
2003 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002004 if (!p || is_cleared_partition(p))
2005 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002007 psects = get_nr_sects(p);
2008 pblocks = psects;
2009 podd = 0;
2010
2011 if (sector_size < 1024) {
2012 pblocks /= (1024 / sector_size);
2013 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002014 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002015 if (sector_size > 1024)
2016 pblocks *= (sector_size / 1024);
2017
2018 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2019 partname(disk_device, i+1, w+2),
2020 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2021 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002022 (ullong) cround(get_partition_start(pe)), /* start */
2023 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002024 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002025 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002026 p->sys_ind, /* type id */
2027 partition_type(p->sys_ind)); /* type name */
2028
2029 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002030 }
2031
2032 /* Is partition table in disk order? It need not be, but... */
2033 /* partition table entries are not checked for correct order if this
2034 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002035 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002036 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002037 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002038 }
2039}
2040
Denis Vlasenko834410a2006-11-29 12:00:28 +00002041#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002042static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002043x_list_table(int extend)
2044{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 const struct pte *pe;
2046 const struct partition *p;
2047 int i;
2048
Denis Vlasenkobd852072007-03-19 14:43:38 +00002049 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002050 disk_device, heads, sectors, cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002051 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkob71c6682007-07-21 15:08:09 +00002052 for (i = 0; i < partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002053 pe = &ptes[i];
2054 p = (extend ? pe->ext_pointer : pe->part_table);
2055 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002056 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002057 i + 1, p->boot_ind, p->head,
2058 sector(p->sector),
2059 cylinder(p->sector, p->cyl), p->end_head,
2060 sector(p->end_sector),
2061 cylinder(p->end_sector, p->end_cyl),
2062 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2063 if (p->sys_ind)
2064 check_consistency(p, i);
2065 }
2066 }
2067}
2068#endif
2069
Denis Vlasenko834410a2006-11-29 12:00:28 +00002070#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002071static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002072fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002073{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074 int i;
2075 const struct pte *pe = &ptes[0];
2076 const struct partition *p;
2077
2078 for (i = 0; i < partitions; pe++,i++) {
2079 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002080 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002081 first[i] = 0xffffffff;
2082 last[i] = 0;
2083 } else {
2084 first[i] = get_partition_start(pe);
2085 last[i] = first[i] + get_nr_sects(p) - 1;
2086 }
2087 }
2088}
2089
2090static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002091check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002092{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002093 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002094
2095 real_s = sector(s) - 1;
2096 real_c = cylinder(s, c);
2097 total = (real_c * sectors + real_s) * heads + h;
2098 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002099 printf("Partition %d contains sector 0\n", n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002100 if (h >= heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002101 printf("Partition %d: head %d greater than maximum %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102 n, h + 1, heads);
2103 if (real_s >= sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002104 printf("Partition %d: sector %d greater than "
2105 "maximum %d\n", n, s, sectors);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106 if (real_c >= cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002107 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002108 "maximum %d\n", n, real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002109 if (cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002110 printf("Partition %d: previous sectors %llu disagrees with "
2111 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002112}
2113
2114static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002115verify(void)
2116{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002117 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002118 unsigned total = 1;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002119 ullong first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002120 struct partition *p;
2121
2122 if (warn_geometry())
2123 return;
2124
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002125 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126 verify_sun();
2127 return;
2128 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002129 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002130 verify_sgi(1);
2131 return;
2132 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133
2134 fill_bounds(first, last);
2135 for (i = 0; i < partitions; i++) {
2136 struct pte *pe = &ptes[i];
2137
2138 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002139 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 check_consistency(p, i);
2141 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002142 printf("Warning: bad start-of-data in "
2143 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2145 last[i]);
2146 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002147 for (j = 0; j < i; j++) {
2148 if ((first[i] >= first[j] && first[i] <= last[j])
2149 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2150 printf("Warning: partition %d overlaps "
2151 "partition %d\n", j + 1, i + 1);
2152 total += first[i] >= first[j] ?
2153 first[i] : first[j];
2154 total -= last[i] <= last[j] ?
2155 last[i] : last[j];
2156 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002157 }
2158 }
2159 }
2160
2161 if (extended_offset) {
2162 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002163 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002164 get_nr_sects(pex->part_table) - 1;
2165
2166 for (i = 4; i < partitions; i++) {
2167 total++;
2168 p = ptes[i].part_table;
2169 if (!p->sys_ind) {
2170 if (i != 4 || i + 1 < partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002171 printf("Warning: partition %d "
2172 "is empty\n", i + 1);
2173 } else if (first[i] < extended_offset || last[i] > e_last) {
2174 printf("Logical partition %d not entirely in "
2175 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002176 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002177 }
2178 }
2179
2180 if (total > heads * sectors * cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002181 printf("Total allocated sectors %d greater than the maximum "
2182 "%d\n", total, heads * sectors * cylinders);
2183 else {
2184 total = heads * sectors * cylinders - total;
2185 if (total != 0)
2186 printf("%d unallocated sectors\n", total);
2187 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002188}
2189
2190static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002191add_partition(int n, int sys)
2192{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002193 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002194 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002195 struct partition *p = ptes[n].part_table;
2196 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002197 ullong limit, temp;
2198 ullong start, stop = 0;
2199 ullong first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002200
2201 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002202 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203 return;
2204 }
2205 fill_bounds(first, last);
2206 if (n < 4) {
2207 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002208 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002209 limit = (ullong) heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002210 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002211 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002212 if (extended_offset) {
2213 first[ext_index] = extended_offset;
2214 last[ext_index] = get_start_sect(q) +
2215 get_nr_sects(q) - 1;
2216 }
2217 } else {
2218 start = extended_offset + sector_offset;
2219 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2220 }
2221 if (display_in_cyl_units)
2222 for (i = 0; i < partitions; i++)
2223 first[i] = (cround(first[i]) - 1) * units_per_sector;
2224
Denis Vlasenkobd852072007-03-19 14:43:38 +00002225 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002226 do {
2227 temp = start;
2228 for (i = 0; i < partitions; i++) {
2229 int lastplusoff;
2230
2231 if (start == ptes[i].offset)
2232 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002233 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002234 if (start >= first[i] && start <= lastplusoff)
2235 start = lastplusoff + 1;
2236 }
2237 if (start > limit)
2238 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002239 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002240 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002241 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002242 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002243 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002244 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002245 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002246
2247 saved_start = start;
2248 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2249 0, mesg);
2250 if (display_in_cyl_units) {
2251 start = (start - 1) * units_per_sector;
2252 if (start < saved_start) start = saved_start;
2253 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002254 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002255 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002256 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002257 if (n > 4) { /* NOT for fifth partition */
2258 struct pte *pe = &ptes[n];
2259
2260 pe->offset = start - sector_offset;
2261 if (pe->offset == extended_offset) { /* must be corrected */
2262 pe->offset++;
2263 if (sector_offset == 1)
2264 start++;
2265 }
2266 }
2267
2268 for (i = 0; i < partitions; i++) {
2269 struct pte *pe = &ptes[i];
2270
2271 if (start < pe->offset && limit >= pe->offset)
2272 limit = pe->offset - 1;
2273 if (start < first[i] && limit >= first[i])
2274 limit = first[i] - 1;
2275 }
2276 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002277 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002278 if (n > 4)
2279 partitions--;
2280 return;
2281 }
2282 if (cround(start) == cround(limit)) {
2283 stop = limit;
2284 } else {
2285 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002286 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002287 str_units(SINGULAR));
2288 stop = read_int(cround(start), cround(limit), cround(limit),
2289 cround(start), mesg);
2290 if (display_in_cyl_units) {
2291 stop = stop * units_per_sector - 1;
2292 if (stop >limit)
2293 stop = limit;
2294 }
2295 }
2296
2297 set_partition(n, 0, start, stop, sys);
2298 if (n > 4)
2299 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2300
Rob Landleyb73451d2006-02-24 16:29:00 +00002301 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002302 struct pte *pe4 = &ptes[4];
2303 struct pte *pen = &ptes[n];
2304
2305 ext_index = n;
2306 pen->ext_pointer = p;
2307 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002308 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002309 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2310 pe4->ext_pointer = pe4->part_table + 1;
2311 pe4->changed = 1;
2312 partitions = 5;
2313 }
2314}
2315
2316static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002317add_logical(void)
2318{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2320 struct pte *pe = &ptes[partitions];
2321
Rob Landley081e3842006-08-03 20:07:35 +00002322 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2324 pe->ext_pointer = pe->part_table + 1;
2325 pe->offset = 0;
2326 pe->changed = 1;
2327 partitions++;
2328 }
2329 add_partition(partitions - 1, LINUX_NATIVE);
2330}
2331
2332static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002333new_partition(void)
2334{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 int i, free_primary = 0;
2336
2337 if (warn_geometry())
2338 return;
2339
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002340 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002341 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2342 return;
2343 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002344 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002345 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2346 return;
2347 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002348 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002349 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2350"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2351"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002352 return;
2353 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002355 for (i = 0; i < 4; i++)
2356 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002357
Rob Landleyb73451d2006-02-24 16:29:00 +00002358 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002359 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002360 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002361 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002362
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002363 if (!free_primary) {
2364 if (extended_offset)
2365 add_logical();
2366 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002367 printf("You must delete some partition and add "
2368 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002370 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002371 snprintf(line, sizeof(line),
2372 "Command action\n"
2373 " %s\n"
2374 " p primary partition (1-4)\n",
2375 (extended_offset ?
2376 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002378 c = read_nonempty(line);
2379 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002380 i = get_nonexisting_partition(0, 4);
2381 if (i >= 0)
2382 add_partition(i, LINUX_NATIVE);
2383 return;
2384 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002385 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 add_logical();
2387 return;
2388 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002389 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002390 i = get_nonexisting_partition(0, 4);
2391 if (i >= 0)
2392 add_partition(i, EXTENDED);
2393 return;
2394 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002395 printf("Invalid partition number "
2396 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 }
2398 }
2399}
2400
2401static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002402write_table(void)
2403{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002404 int i;
2405
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002406 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002407 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002408 if (ptes[i].changed)
2409 ptes[3].changed = 1;
2410 for (i = 3; i < partitions; i++) {
2411 struct pte *pe = &ptes[i];
2412
2413 if (pe->changed) {
2414 write_part_table_flag(pe->sectorbuffer);
2415 write_sector(pe->offset, pe->sectorbuffer);
2416 }
2417 }
2418 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002419 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002420 /* no test on change? the printf below might be mistaken */
2421 sgi_write_table();
2422 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002423 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002424 int needw = 0;
2425
Rob Landleyb73451d2006-02-24 16:29:00 +00002426 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002427 if (ptes[i].changed)
2428 needw = 1;
2429 if (needw)
2430 sun_write_table();
2431 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432
Denis Vlasenkobd852072007-03-19 14:43:38 +00002433 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002434 reread_partition_table(1);
2435}
2436
Rob Landleyb73451d2006-02-24 16:29:00 +00002437static void
2438reread_partition_table(int leave)
2439{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002440 int i;
2441
Denis Vlasenkobd852072007-03-19 14:43:38 +00002442 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002444 /* sleep(2); Huh? */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002445 i = ioctl_or_perror(fd, BLKRRPART, NULL,
2446 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002447 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002448#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002450 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002451 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002453 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002454#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002455
2456 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002457 if (ENABLE_FEATURE_CLEAN_UP)
2458 close(fd);
2459 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002460 }
2461}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002462#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002463
Denis Vlasenko834410a2006-11-29 12:00:28 +00002464#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465#define MAX_PER_LINE 16
2466static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002467print_buffer(char *pbuffer)
2468{
2469 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002470
2471 for (i = 0, l = 0; i < sector_size; i++, l++) {
2472 if (l == 0)
2473 printf("0x%03X:", i);
2474 printf(" %02X", (unsigned char) pbuffer[i]);
2475 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002476 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002477 l = -1;
2478 }
2479 }
2480 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002481 bb_putchar('\n');
2482 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483}
2484
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002486print_raw(void)
2487{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002488 int i;
2489
Denis Vlasenkobd852072007-03-19 14:43:38 +00002490 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002491 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002493 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494 for (i = 3; i < partitions; i++)
2495 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002496 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002497}
2498
2499static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002500move_begin(int i)
2501{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 struct pte *pe = &ptes[i];
2503 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002504 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505
2506 if (warn_geometry())
2507 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002508 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002509 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510 return;
2511 }
2512 first = get_partition_start(pe);
2513 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002514 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002515
2516 if (new != get_nr_sects(p)) {
2517 first = get_nr_sects(p) + get_start_sect(p) - new;
2518 set_nr_sects(p, first);
2519 set_start_sect(p, new);
2520 pe->changed = 1;
2521 }
2522}
2523
2524static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002525xselect(void)
2526{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 char c;
2528
Rob Landleyb73451d2006-02-24 16:29:00 +00002529 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002530 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002531 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002532 switch (c) {
2533 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002534 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002535 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002536 break;
2537 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002538 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002539 move_begin(get_partition(0, partitions));
2540 break;
2541 case 'c':
2542 user_cylinders = cylinders =
2543 read_int(1, cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002544 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002545 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002547 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548 warn_cylinders();
2549 break;
2550 case 'd':
2551 print_raw();
2552 break;
2553 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002554 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002555 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002556 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002558 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002559 x_list_table(1);
2560 break;
2561 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002562 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 fix_partition_table_order();
2564 break;
2565 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002566#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002567 create_sgilabel();
2568#endif
2569 break;
2570 case 'h':
2571 user_heads = heads = read_int(1, heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002572 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573 update_units();
2574 break;
2575 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002576 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578 break;
2579 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002581 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582 break;
2583 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002584 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585 list_table(1);
2586 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 x_list_table(0);
2588 break;
2589 case 'q':
2590 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002591 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 exit(0);
2593 case 'r':
2594 return;
2595 case 's':
2596 user_sectors = sectors = read_int(1, sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002597 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002598 if (dos_compatible_flag) {
2599 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002600 printf("Warning: setting sector offset for DOS "
2601 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602 }
2603 update_units();
2604 break;
2605 case 'v':
2606 verify();
2607 break;
2608 case 'w':
2609 write_table(); /* does not return */
2610 break;
2611 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002612 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002614 break;
2615 default:
2616 xmenu();
2617 }
2618 }
2619}
2620#endif /* ADVANCED mode */
2621
2622static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002623is_ide_cdrom_or_tape(const char *device)
2624{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 FILE *procf;
2626 char buf[100];
2627 struct stat statbuf;
2628 int is_ide = 0;
2629
2630 /* No device was given explicitly, and we are trying some
2631 likely things. But opening /dev/hdc may produce errors like
2632 "hdc: tray open or drive not ready"
2633 if it happens to be a CD-ROM drive. It even happens that
2634 the process hangs on the attempt to read a music CD.
2635 So try to be careful. This only works since 2.1.73. */
2636
2637 if (strncmp("/dev/hd", device, 7))
2638 return 0;
2639
2640 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2641 procf = fopen(buf, "r");
2642 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2643 is_ide = (!strncmp(buf, "cdrom", 5) ||
2644 !strncmp(buf, "tape", 4));
2645 else
2646 /* Now when this proc file does not exist, skip the
2647 device when it is read-only. */
2648 if (stat(device, &statbuf) == 0)
2649 is_ide = ((statbuf.st_mode & 0222) == 0);
2650
2651 if (procf)
2652 fclose(procf);
2653 return is_ide;
2654}
2655
Rob Landley5527b912006-02-25 03:46:10 +00002656
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002657static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002658trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002659{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002660 int gb;
2661
2662 disk_device = device;
2663 if (setjmp(listingbuf))
2664 return;
2665 if (!user_specified)
2666 if (is_ide_cdrom_or_tape(device))
2667 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002668 fd = open(disk_device, type_open);
2669 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002670 gb = get_boot(try_only);
2671 if (gb > 0) { /* I/O error */
2672 close(fd);
2673 } else if (gb < 0) { /* no DOS signature */
2674 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002675 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002676 return;
Rob Landley5527b912006-02-25 03:46:10 +00002677 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002678#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002679 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002680#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002681 printf("Disk %s doesn't contain a valid "
2682 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002683 close(fd);
2684 } else {
2685 close(fd);
2686 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002687#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002688 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002689 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002690 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002691#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 }
2693 } else {
2694 /* Ignore other errors, since we try IDE
2695 and SCSI hard disks which may not be
2696 installed on the system. */
2697 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002698 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002699 return;
2700 }
2701 }
2702}
2703
2704/* for fdisk -l: try all things in /proc/partitions
2705 that look like a partition name (do not end in a digit) */
2706static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002707tryprocpt(void)
2708{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 FILE *procpt;
2710 char line[100], ptname[100], devname[120], *s;
2711 int ma, mi, sz;
2712
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002713 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002714
2715 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002716 if (sscanf(line, " %d %d %d %[^\n ]",
2717 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002718 continue;
2719 for (s = ptname; *s; s++);
2720 if (isdigit(s[-1]))
2721 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002722 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002723 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002724 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002725#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002726 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002727#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002728}
2729
Denis Vlasenko834410a2006-11-29 12:00:28 +00002730#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002731static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002732unknown_command(int c)
2733{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002734 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002735}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002736#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002737
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002738int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002739int fdisk_main(int argc, char **argv)
2740{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002741 char *str_b, *str_C, *str_H, *str_S;
2742 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002744 * fdisk -v
2745 * fdisk -l [-b sectorsize] [-u] device ...
2746 * fdisk -s [partition] ...
2747 * fdisk [-b sectorsize] [-u] device
2748 *
2749 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002751 enum {
2752 OPT_b = 1 << 0,
2753 OPT_C = 1 << 1,
2754 OPT_H = 1 << 2,
2755 OPT_l = 1 << 3,
2756 OPT_S = 1 << 4,
2757 OPT_u = 1 << 5,
2758 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2759 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002760
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002761 PTR_TO_GLOBALS = xzalloc(sizeof(G));
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002762
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002763 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko834410a2006-11-29 12:00:28 +00002764 &str_b, &str_C, &str_H, &str_S);
2765 argc -= optind;
2766 argv += optind;
2767 if (opt & OPT_b) { // -b
2768 /* Ugly: this sector size is really per device,
2769 so cannot be combined with multiple disks,
2770 and the same goes for the C/H/S options.
2771 */
2772 sector_size = xatoi_u(str_b);
2773 if (sector_size != 512 && sector_size != 1024 &&
2774 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002775 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002776 sector_offset = 2;
2777 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002778 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002779 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2780 if (opt & OPT_H) { // -H
2781 user_heads = xatoi_u(str_H);
2782 if (user_heads <= 0 || user_heads >= 256)
2783 user_heads = 0;
2784 }
2785 //if (opt & OPT_l) // -l
2786 if (opt & OPT_S) { // -S
2787 user_sectors = xatoi_u(str_S);
2788 if (user_sectors <= 0 || user_sectors >= 64)
2789 user_sectors = 0;
2790 }
2791 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2792 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002793
Denis Vlasenko834410a2006-11-29 12:00:28 +00002794 if (user_set_sector_size && argc != 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002795 printf("Warning: the -b (set sector size) option should"
2796 " be used with one specified device\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002797
Denis Vlasenko834410a2006-11-29 12:00:28 +00002798#if ENABLE_FEATURE_FDISK_WRITABLE
2799 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002800 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002801#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002802 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002803 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002804 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002805#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806 /* avoid gcc warning:
2807 variable `k' might be clobbered by `longjmp' */
2808 (void)&k;
2809#endif
2810 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002811 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002812 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002813 } else {
2814 /* we no longer have default device names */
2815 /* but, we can use /proc/partitions instead */
2816 tryprocpt();
2817 }
2818 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002819#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002820 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002821#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002822
Denis Vlasenko834410a2006-11-29 12:00:28 +00002823#if ENABLE_FEATURE_FDISK_BLKSIZE
2824 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002825 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002826 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002827
2828 nowarn = 1;
2829 type_open = O_RDONLY;
2830
Denis Vlasenko834410a2006-11-29 12:00:28 +00002831 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002832 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833
Denis Vlasenko834410a2006-11-29 12:00:28 +00002834 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002836 fd = open(disk_device, type_open);
2837 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002838 fdisk_fatal(unable_to_open);
2839 if (ioctl(fd, BLKGETSIZE, &size))
2840 fdisk_fatal(ioctl_error);
2841 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002842 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002843 printf("%ld\n", size/2);
2844 else
2845 printf("%s: %ld\n", argv[j], size/2);
2846 }
2847 return 0;
2848 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002849#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850
Denis Vlasenko834410a2006-11-29 12:00:28 +00002851#if ENABLE_FEATURE_FDISK_WRITABLE
2852 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002853 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854
Denis Vlasenko834410a2006-11-29 12:00:28 +00002855 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 get_boot(fdisk);
2857
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002858 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002859 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002860 printf("Detected an OSF/1 disklabel on %s, entering "
2861 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002862 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002863 /*Why do we do this? It seems to be counter-intuitive*/
2864 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865 /* If we return we may want to make an empty DOS label? */
2866 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867
2868 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002869 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002870 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002871 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002872 switch (c) {
2873 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002874 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002876 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002877 toggle_sunflags(get_partition(1, partitions),
2878 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002879 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 sgi_set_bootpartition(
2881 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882 else
2883 unknown_command(c);
2884 break;
2885 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002886 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002887 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002888 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002889 if (read_maybe_empty("Please enter the name of the "
2890 "new boot file: ") == '\n')
2891 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892 else
2893 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002894 }
2895#if ENABLE_FEATURE_OSF_LABEL
2896 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002897 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898#endif
2899 break;
2900 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002901 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002902 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002903 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002904 toggle_sunflags(get_partition(1, partitions),
2905 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002906 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907 sgi_set_swappartition(
2908 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002909 else
2910 unknown_command(c);
2911 break;
2912 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002913 {
Eric Andersen040f4402003-07-30 08:40:37 +00002914 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002915 /* If sgi_label then don't use get_existing_partition,
2916 let the user select a partition, since
2917 get_existing_partition() only works for Linux-like
2918 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002919 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002920 j = get_existing_partition(1, partitions);
2921 } else {
2922 j = get_partition(1, partitions);
2923 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002924 if (j >= 0)
2925 delete_partition(j);
2926 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002927 break;
2928 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002929 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 create_sgiinfo();
2931 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002932 unknown_command(c);
2933 case 'l':
2934 list_types(get_sys_types());
2935 break;
2936 case 'm':
2937 menu();
2938 break;
2939 case 'n':
2940 new_partition();
2941 break;
2942 case 'o':
2943 create_doslabel();
2944 break;
2945 case 'p':
2946 list_table(0);
2947 break;
2948 case 'q':
2949 close(fd);
Denis Vlasenko4daad902007-09-27 10:20:47 +00002950 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951 return 0;
2952 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002953#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002954 create_sunlabel();
2955#endif
2956 break;
2957 case 't':
2958 change_sysid();
2959 break;
2960 case 'u':
2961 change_units();
2962 break;
2963 case 'v':
2964 verify();
2965 break;
2966 case 'w':
2967 write_table(); /* does not return */
2968 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002969#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002970 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002971 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002972 printf("\n\tSorry, no experts menu for SGI "
2973 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002974 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002975 xselect();
2976 break;
2977#endif
2978 default:
2979 unknown_command(c);
2980 menu();
2981 }
2982 }
2983 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002984#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985}