blob: 9d7bd4e2cc8ab61622bbc3ad33b001790dabb4f1 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000010#include <assert.h> /* assert */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000011#include "busybox.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000012
Denis Vlasenko834410a2006-11-29 12:00:28 +000013/* Looks like someone forgot to add this to config system */
14#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
15# define ENABLE_FEATURE_FDISK_BLKSIZE 0
16# define USE_FEATURE_FDISK_BLKSIZE(a)
17#endif
18
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000019#define SIZE(a) (sizeof(a)/sizeof((a)[0]))
20
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000021#define DEFAULT_SECTOR_SIZE 512
22#define MAX_SECTOR_SIZE 2048
Denis Vlasenko98ae2162006-10-12 19:30:44 +000023#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000024#define MAXIMUM_PARTS 60
25
26#define ACTIVE_FLAG 0x80
27
28#define EXTENDED 0x05
29#define WIN98_EXTENDED 0x0f
30#define LINUX_PARTITION 0x81
31#define LINUX_SWAP 0x82
32#define LINUX_NATIVE 0x83
33#define LINUX_EXTENDED 0x85
34#define LINUX_LVM 0x8e
35#define LINUX_RAID 0xfd
36
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000037struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000038 unsigned char heads;
39 unsigned char sectors;
40 unsigned short cylinders;
41 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000042};
43
Denis Vlasenko98ae2162006-10-12 19:30:44 +000044#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000045
Denis Vlasenkobd852072007-03-19 14:43:38 +000046static const char msg_building_new_label[] =
47"Building a new %s. Changes will remain in memory only,\n"
48"until you decide to write them. After that the previous content\n"
49"won't be recoverable.\n\n";
50
51static const char msg_part_already_defined[] =
52"Partition %d is already defined, delete it before re-adding\n";
53
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000054
Denis Vlasenko834410a2006-11-29 12:00:28 +000055static unsigned sector_size = DEFAULT_SECTOR_SIZE;
56static unsigned user_set_sector_size;
57static unsigned sector_offset = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000058
Denis Vlasenko834410a2006-11-29 12:00:28 +000059#if ENABLE_FEATURE_OSF_LABEL
Rob Landleyb73451d2006-02-24 16:29:00 +000060static int possibly_osf_label;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000061#endif
62
Denis Vlasenko834410a2006-11-29 12:00:28 +000063static unsigned heads, sectors, cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000064static void update_units(void);
65
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000066
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000067struct partition {
68 unsigned char boot_ind; /* 0x80 - active */
69 unsigned char head; /* starting head */
70 unsigned char sector; /* starting sector */
71 unsigned char cyl; /* starting cylinder */
72 unsigned char sys_ind; /* What partition type */
73 unsigned char end_head; /* end head */
74 unsigned char end_sector; /* end sector */
75 unsigned char end_cyl; /* end cylinder */
76 unsigned char start4[4]; /* starting sector counting from 0 */
77 unsigned char size4[4]; /* nr of sectors in partition */
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000078} ATTRIBUTE_PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000079
80enum failure {
81 ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
82 unable_to_write
83};
84
Denis Vlasenko98ae2162006-10-12 19:30:44 +000085enum label_type {
Rob Landley5527b912006-02-25 03:46:10 +000086 label_dos, label_sun, label_sgi, label_aix, label_osf
87};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000088
Denis Vlasenko98ae2162006-10-12 19:30:44 +000089#define LABEL_IS_DOS (label_dos == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000090
Denis Vlasenko834410a2006-11-29 12:00:28 +000091#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +000092#define LABEL_IS_SUN (label_sun == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000093#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +000094#else
95#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000096#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +000097#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000098
Denis Vlasenko834410a2006-11-29 12:00:28 +000099#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000100#define LABEL_IS_SGI (label_sgi == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000101#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000102#else
103#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000104#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000105#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000106
Denis Vlasenko834410a2006-11-29 12:00:28 +0000107#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000108#define LABEL_IS_AIX (label_aix == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000109#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000110#else
111#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000113#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000114
Denis Vlasenko834410a2006-11-29 12:00:28 +0000115#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#define LABEL_IS_OSF (label_osf == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000117#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000118#else
119#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000120#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000121#endif
Rob Landley5527b912006-02-25 03:46:10 +0000122
Rob Landleyb73451d2006-02-24 16:29:00 +0000123enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000124
Rob Landley5527b912006-02-25 03:46:10 +0000125static enum label_type current_label_type;
126
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000127static const char *disk_device;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000128static int fd; /* the disk */
129static int partitions = 4; /* maximum partition + 1 */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000130static int display_in_cyl_units = 1;
131static unsigned units_per_sector = 1;
132#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000133static void change_units(void);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000134static void reread_partition_table(int leave);
135static void delete_partition(int i);
Rob Landleyb73451d2006-02-24 16:29:00 +0000136static int get_partition(int warn, int max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000137static void list_types(const char *const *sys);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000138static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000139#endif
140static const char *partition_type(unsigned char type);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000141static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000142static void get_geometry(void);
143static int get_boot(enum action what);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000144
145#define PLURAL 0
146#define SINGULAR 1
147
Denis Vlasenko28703012006-12-19 20:32:02 +0000148static unsigned get_start_sect(const struct partition *p);
149static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000150
151/*
152 * per partition table entry data
153 *
154 * The four primary partitions have the same sectorbuffer (MBRbuffer)
155 * and have NULL ext_pointer.
156 * Each logical partition table entry has two pointers, one for the
157 * partition and one link to the next one.
158 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000159struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000160 struct partition *part_table; /* points into sectorbuffer */
161 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000162 off_t offset; /* disk sector number */
163 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000164#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000165 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000166#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000167};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000168
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000169/* DOS partition types */
170
171static const char *const i386_sys_types[] = {
172 "\x00" "Empty",
173 "\x01" "FAT12",
174 "\x04" "FAT16 <32M",
175 "\x05" "Extended", /* DOS 3.3+ extended partition */
176 "\x06" "FAT16", /* DOS 16-bit >=32M */
177 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
178 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
179 "\x0b" "Win95 FAT32",
180 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
181 "\x0e" "Win95 FAT16 (LBA)",
182 "\x0f" "Win95 Ext'd (LBA)",
183 "\x11" "Hidden FAT12",
184 "\x12" "Compaq diagnostics",
185 "\x14" "Hidden FAT16 <32M",
186 "\x16" "Hidden FAT16",
187 "\x17" "Hidden HPFS/NTFS",
188 "\x1b" "Hidden Win95 FAT32",
189 "\x1c" "Hidden W95 FAT32 (LBA)",
190 "\x1e" "Hidden W95 FAT16 (LBA)",
191 "\x3c" "Part.Magic recovery",
192 "\x41" "PPC PReP Boot",
193 "\x42" "SFS",
194 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
195 "\x80" "Old Minix", /* Minix 1.4a and earlier */
196 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
197 "\x82" "Linux swap", /* also Solaris */
198 "\x83" "Linux",
199 "\x84" "OS/2 hidden C: drive",
200 "\x85" "Linux extended",
201 "\x86" "NTFS volume set",
202 "\x87" "NTFS volume set",
203 "\x8e" "Linux LVM",
204 "\x9f" "BSD/OS", /* BSDI */
205 "\xa0" "Thinkpad hibernation",
206 "\xa5" "FreeBSD", /* various BSD flavours */
207 "\xa6" "OpenBSD",
208 "\xa8" "Darwin UFS",
209 "\xa9" "NetBSD",
210 "\xab" "Darwin boot",
211 "\xb7" "BSDI fs",
212 "\xb8" "BSDI swap",
213 "\xbe" "Solaris boot",
214 "\xeb" "BeOS fs",
215 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
216 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
217 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
218 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
219 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
220 autodetect using persistent
221 superblock */
222#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
223 "\x02" "XENIX root",
224 "\x03" "XENIX usr",
225 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
226 "\x09" "AIX bootable", /* AIX data or Coherent */
227 "\x10" "OPUS",
228 "\x18" "AST SmartSleep",
229 "\x24" "NEC DOS",
230 "\x39" "Plan 9",
231 "\x40" "Venix 80286",
232 "\x4d" "QNX4.x",
233 "\x4e" "QNX4.x 2nd part",
234 "\x4f" "QNX4.x 3rd part",
235 "\x50" "OnTrack DM",
236 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
237 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
238 "\x53" "OnTrack DM6 Aux3",
239 "\x54" "OnTrackDM6",
240 "\x55" "EZ-Drive",
241 "\x56" "Golden Bow",
242 "\x5c" "Priam Edisk",
243 "\x61" "SpeedStor",
244 "\x64" "Novell Netware 286",
245 "\x65" "Novell Netware 386",
246 "\x70" "DiskSecure Multi-Boot",
247 "\x75" "PC/IX",
248 "\x93" "Amoeba",
249 "\x94" "Amoeba BBT", /* (bad block table) */
250 "\xa7" "NeXTSTEP",
251 "\xbb" "Boot Wizard hidden",
252 "\xc1" "DRDOS/sec (FAT-12)",
253 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
254 "\xc6" "DRDOS/sec (FAT-16)",
255 "\xc7" "Syrinx",
256 "\xda" "Non-FS data",
257 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
258 Concurrent DOS or CTOS */
259 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
260 "\xdf" "BootIt", /* BootIt EMBRM */
261 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
262 extended partition */
263 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
264 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
265 partition < 1024 cyl. */
266 "\xf1" "SpeedStor",
267 "\xf4" "SpeedStor", /* SpeedStor large partition */
268 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
269 "\xff" "BBT", /* Xenix Bad Block Table */
270#endif
271 NULL
272};
273
274
275/* Globals */
276
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000277struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000278 char *line_ptr;
279 char line_buffer[80];
280 char partname_buffer[80];
281 jmp_buf listingbuf;
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000282 /* Raw disk label. For DOS-type partition tables the MBR,
283 * with descriptions of the primary partitions. */
284 char MBRbuffer[MAX_SECTOR_SIZE];
285 /* Partition tables */
286 struct pte ptes[MAXIMUM_PARTS];
287};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000288/* bb_common_bufsiz1 is too small for this on 64 bit CPUs */
289#define G (*ptr_to_globals)
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000290
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000291#define line_ptr (G.line_ptr)
292#define listingbuf (G.listingbuf)
293#define line_buffer (G.line_buffer)
294#define partname_buffer (G.partname_buffer)
295#define MBRbuffer (G.MBRbuffer)
296#define ptes (G.ptes)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000297
Denis Vlasenkobd852072007-03-19 14:43:38 +0000298
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000299/* Code */
300
301#define IS_EXTENDED(i) \
302 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
303
304#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
305
306#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
307
308#define pt_offset(b, n) \
309 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
310
311#define sector(s) ((s) & 0x3f)
312
313#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
314
315#define hsc2sector(h,s,c) \
316 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
317
318#define set_hsc(h,s,c,sector) \
319 do { \
320 s = sector % sectors + 1; \
321 sector /= sectors; \
322 h = sector % heads; \
323 sector /= heads; \
324 c = sector & 0xff; \
325 s |= (sector >> 2) & 0xc0; \
326 } while (0)
327
328/* read line; return 0 or first printable char */
329static int
330read_line(const char *prompt)
331{
332 int sz;
333
334 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
335 if (sz <= 0)
336 exit(0); /* Ctrl-D or Ctrl-C */
337
338 if (line_buffer[sz-1] == '\n')
339 line_buffer[--sz] = '\0';
340
341 line_ptr = line_buffer;
342 while (*line_ptr && !isgraph(*line_ptr))
343 line_ptr++;
344 return *line_ptr;
345}
346
Denis Vlasenkobd852072007-03-19 14:43:38 +0000347/*
348 * return partition name - uses static storage
349 */
350static const char *
351partname(const char *dev, int pno, int lth)
352{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000353 const char *p;
354 int w, wp;
355 int bufsiz;
356 char *bufp;
357
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000358 bufp = partname_buffer;
359 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000360
361 w = strlen(dev);
362 p = "";
363
364 if (isdigit(dev[w-1]))
365 p = "p";
366
367 /* devfs kludge - note: fdisk partition names are not supposed
368 to equal kernel names, so there is no reason to do this */
369 if (strcmp(dev + w - 4, "disc") == 0) {
370 w -= 4;
371 p = "part";
372 }
373
374 wp = strlen(p);
375
376 if (lth) {
377 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
378 lth-wp-2, w, dev, p, pno);
379 } else {
380 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
381 }
382 return bufp;
383}
384
Denis Vlasenko834410a2006-11-29 12:00:28 +0000385#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000386static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000387set_all_unchanged(void)
388{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000389 int i;
390
391 for (i = 0; i < MAXIMUM_PARTS; i++)
392 ptes[i].changed = 0;
393}
394
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000395static ATTRIBUTE_ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000396set_changed(int i)
397{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000398 ptes[i].changed = 1;
399}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000400#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000401
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000402static ATTRIBUTE_ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000403get_part_table(int i)
404{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000405 return ptes[i].part_table;
406}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000407
408static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000409str_units(int n)
410{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000411 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000412 return display_in_cyl_units ? "cylinder" : "sector";
413 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000414}
415
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000416static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000417valid_part_table_flag(const char *mbuffer)
418{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000419 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000420}
421
Denis Vlasenko834410a2006-11-29 12:00:28 +0000422#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000423static ATTRIBUTE_ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000424write_part_table_flag(char *b)
425{
426 b[510] = 0x55;
427 b[511] = 0xaa;
428}
429
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000430static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000431read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000432{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000433 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000434 return *line_ptr;
435}
436
437static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000438read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000439{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000440 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000441 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000442 line_ptr[0] = '\n';
443 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000444 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000445 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000446}
447
448static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000449read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000450{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000451 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000452 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000453 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000454 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000455 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000456 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000457 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000458 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000459 if (v > 0xff)
460 /* Bad input also triggers this */
461 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000462 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000463 }
464}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000465#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000466
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000467#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000468
469typedef struct {
470 unsigned char info[128]; /* Informative text string */
471 unsigned char spare0[14];
472 struct sun_info {
473 unsigned char spare1;
474 unsigned char id;
475 unsigned char spare2;
476 unsigned char flags;
477 } infos[8];
478 unsigned char spare1[246]; /* Boot information etc. */
479 unsigned short rspeed; /* Disk rotational speed */
480 unsigned short pcylcount; /* Physical cylinder count */
481 unsigned short sparecyl; /* extra sects per cylinder */
482 unsigned char spare2[4]; /* More magic... */
483 unsigned short ilfact; /* Interleave factor */
484 unsigned short ncyl; /* Data cylinder count */
485 unsigned short nacyl; /* Alt. cylinder count */
486 unsigned short ntrks; /* Tracks per cylinder */
487 unsigned short nsect; /* Sectors per track */
488 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000489 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000490 uint32_t start_cylinder;
491 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000492 } partitions[8];
493 unsigned short magic; /* Magic number */
494 unsigned short csum; /* Label xor'd checksum */
495} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000496#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000497STATIC_OSF void bsd_select(void);
498STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000499#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000500
Denis Vlasenko28703012006-12-19 20:32:02 +0000501#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000502static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000503fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000504{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000505 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000506}
507
Rob Landley88621d72006-08-29 19:41:06 +0000508static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000509fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000510{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000511 return (x << 24) |
512 ((x & 0xFF00) << 8) |
513 ((x & 0xFF0000) >> 8) |
514 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000515}
516#endif
517
Denis Vlasenkobd852072007-03-19 14:43:38 +0000518STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000519STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000520STATIC_SGI int sgi_get_sysid(int i);
521STATIC_SGI void sgi_delete_partition(int i);
522STATIC_SGI void sgi_change_sysid(int i, int sys);
523STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000524#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000525STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000526#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000527STATIC_SGI int verify_sgi(int verbose);
528STATIC_SGI void sgi_add_partition(int n, int sys);
529STATIC_SGI void sgi_set_swappartition(int i);
530STATIC_SGI const char *sgi_get_bootfile(void);
531STATIC_SGI void sgi_set_bootfile(const char* aFile);
532STATIC_SGI void create_sgiinfo(void);
533STATIC_SGI void sgi_write_table(void);
534STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000535#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000536
Denis Vlasenkobd852072007-03-19 14:43:38 +0000537STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000538STATIC_SUN void sun_delete_partition(int i);
539STATIC_SUN void sun_change_sysid(int i, int sys);
540STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000541STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000542#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000543STATIC_SUN void sun_set_alt_cyl(void);
544STATIC_SUN void sun_set_ncyl(int cyl);
545STATIC_SUN void sun_set_xcyl(void);
546STATIC_SUN void sun_set_ilfact(void);
547STATIC_SUN void sun_set_rspeed(void);
548STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000549#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000550STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
551STATIC_SUN void verify_sun(void);
552STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000553#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554
Denis Vlasenko834410a2006-11-29 12:00:28 +0000555#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000556/* start_sect and nr_sects are stored little endian on all machines */
557/* moreover, they are not aligned correctly */
558static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000559store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000560{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000561 cp[0] = val;
562 cp[1] = val >> 8;
563 cp[2] = val >> 16;
564 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000565}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000566#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000567
Denis Vlasenko834410a2006-11-29 12:00:28 +0000568static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000569read4_little_endian(const unsigned char *cp)
570{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000571 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000572}
573
Denis Vlasenko834410a2006-11-29 12:00:28 +0000574#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000575static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000576set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000577{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000578 store4_little_endian(p->start4, start_sect);
579}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000580#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000581
Denis Vlasenko28703012006-12-19 20:32:02 +0000582static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000583get_start_sect(const struct partition *p)
584{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000585 return read4_little_endian(p->start4);
586}
587
Denis Vlasenko834410a2006-11-29 12:00:28 +0000588#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000589static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000590set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000591{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000592 store4_little_endian(p->size4, nr_sects);
593}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000594#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000595
Denis Vlasenko28703012006-12-19 20:32:02 +0000596static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000597get_nr_sects(const struct partition *p)
598{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599 return read4_little_endian(p->size4);
600}
601
602/* normally O_RDWR, -l option gives O_RDONLY */
603static int type_open = O_RDWR;
604
605
Rob Landleyb73451d2006-02-24 16:29:00 +0000606static int ext_index; /* the prime extended partition */
607static int listing; /* no aborts for fdisk -l */
608static int dos_compatible_flag = ~0;
Denis Vlasenko834410a2006-11-29 12:00:28 +0000609#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000610static int dos_changed;
611static int nowarn; /* no warnings for fdisk -l/-s */
612#endif
613
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000614
615
Denis Vlasenko834410a2006-11-29 12:00:28 +0000616static unsigned user_cylinders, user_heads, user_sectors;
617static unsigned pt_heads, pt_sectors;
618static unsigned kern_heads, kern_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000619
Eric Andersend9261492004-06-28 23:50:31 +0000620static off_t extended_offset; /* offset of link pointers */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000621
Eric Andersen040f4402003-07-30 08:40:37 +0000622static unsigned long long total_number_of_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000623
624
Rob Landleyb73451d2006-02-24 16:29:00 +0000625static void fdisk_fatal(enum failure why)
626{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000627 const char *message;
628
629 if (listing) {
630 close(fd);
631 longjmp(listingbuf, 1);
632 }
633
634 switch (why) {
Rob Landleyb73451d2006-02-24 16:29:00 +0000635 case unable_to_open:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000636 message = "cannot open %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000637 break;
638 case unable_to_read:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000639 message = "cannot read from %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000640 break;
641 case unable_to_seek:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000642 message = "cannot seek on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000643 break;
644 case unable_to_write:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000645 message = "cannot write to %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000646 break;
647 case ioctl_error:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000648 message = "BLKGETSIZE ioctl failed on %s";
Rob Landleyb73451d2006-02-24 16:29:00 +0000649 break;
650 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +0000651 message = "fatal error";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000652 }
653
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000654 bb_error_msg_and_die(message, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655}
656
657static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000658seek_sector(off_t secno)
659{
Eric Andersen0a92f352004-03-30 09:21:54 +0000660 off_t offset = secno * sector_size;
661 if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000662 fdisk_fatal(unable_to_seek);
663}
664
Denis Vlasenko834410a2006-11-29 12:00:28 +0000665#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000666static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000667write_sector(off_t secno, char *buf)
668{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000669 seek_sector(secno);
670 if (write(fd, buf, sector_size) != sector_size)
671 fdisk_fatal(unable_to_write);
672}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000673#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000674
675/* Allocate a buffer and read a partition table sector */
676static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000677read_pte(struct pte *pe, off_t offset)
678{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000679 pe->offset = offset;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000680 pe->sectorbuffer = xmalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000681 seek_sector(offset);
682 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
683 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000684#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000686#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 pe->part_table = pe->ext_pointer = NULL;
688}
689
Denis Vlasenko834410a2006-11-29 12:00:28 +0000690static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000691get_partition_start(const struct pte *pe)
692{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000693 return pe->offset + get_start_sect(pe->part_table);
694}
695
Denis Vlasenko834410a2006-11-29 12:00:28 +0000696#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000697/*
698 * Avoid warning about DOS partitions when no DOS partition was changed.
699 * Here a heuristic "is probably dos partition".
700 * We might also do the opposite and warn in all cases except
701 * for "is probably nondos partition".
702 */
703static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000704is_dos_partition(int t)
705{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000706 return (t == 1 || t == 4 || t == 6 ||
707 t == 0x0b || t == 0x0c || t == 0x0e ||
708 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
709 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
710 t == 0xc1 || t == 0xc4 || t == 0xc6);
711}
712
713static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000714menu(void)
715{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000716 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000717 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000718 puts("a\ttoggle a read only flag"); /* sun */
719 puts("b\tedit bsd disklabel");
720 puts("c\ttoggle the mountable flag"); /* sun */
721 puts("d\tdelete a partition");
722 puts("l\tlist known partition types");
723 puts("n\tadd a new partition");
724 puts("o\tcreate a new empty DOS partition table");
725 puts("p\tprint the partition table");
726 puts("q\tquit without saving changes");
727 puts("s\tcreate a new empty Sun disklabel"); /* sun */
728 puts("t\tchange a partition's system id");
729 puts("u\tchange display/entry units");
730 puts("v\tverify the partition table");
731 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000732#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000733 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000734#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000735 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000736 puts("a\tselect bootable partition"); /* sgi flavour */
737 puts("b\tedit bootfile entry"); /* sgi */
738 puts("c\tselect sgi swap partition"); /* sgi flavour */
739 puts("d\tdelete a partition");
740 puts("l\tlist known partition types");
741 puts("n\tadd a new partition");
742 puts("o\tcreate a new empty DOS partition table");
743 puts("p\tprint the partition table");
744 puts("q\tquit without saving changes");
745 puts("s\tcreate a new empty Sun disklabel"); /* sun */
746 puts("t\tchange a partition's system id");
747 puts("u\tchange display/entry units");
748 puts("v\tverify the partition table");
749 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000750 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000751 puts("o\tcreate a new empty DOS partition table");
752 puts("q\tquit without saving changes");
753 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000754 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000755 puts("a\ttoggle a bootable flag");
756 puts("b\tedit bsd disklabel");
757 puts("c\ttoggle the dos compatibility flag");
758 puts("d\tdelete a partition");
759 puts("l\tlist known partition types");
760 puts("n\tadd a new partition");
761 puts("o\tcreate a new empty DOS partition table");
762 puts("p\tprint the partition table");
763 puts("q\tquit without saving changes");
764 puts("s\tcreate a new empty Sun disklabel"); /* sun */
765 puts("t\tchange a partition's system id");
766 puts("u\tchange display/entry units");
767 puts("v\tverify the partition table");
768 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000769#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000770 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000771#endif
772 }
773}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000774#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000775
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000776
Denis Vlasenko834410a2006-11-29 12:00:28 +0000777#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000779xmenu(void)
780{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000781 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000782 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000783 puts("a\tchange number of alternate cylinders"); /*sun*/
784 puts("c\tchange number of cylinders");
785 puts("d\tprint the raw data in the partition table");
786 puts("e\tchange number of extra sectors per cylinder");/*sun*/
787 puts("h\tchange number of heads");
788 puts("i\tchange interleave factor"); /*sun*/
789 puts("o\tchange rotation speed (rpm)"); /*sun*/
790 puts("p\tprint the partition table");
791 puts("q\tquit without saving changes");
792 puts("r\treturn to main menu");
793 puts("s\tchange number of sectors/track");
794 puts("v\tverify the partition table");
795 puts("w\twrite table to disk and exit");
796 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000797 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000798 puts("b\tmove beginning of data in a partition"); /* !sun */
799 puts("c\tchange number of cylinders");
800 puts("d\tprint the raw data in the partition table");
801 puts("e\tlist extended partitions"); /* !sun */
802 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
803 puts("h\tchange number of heads");
804 puts("p\tprint the partition table");
805 puts("q\tquit without saving changes");
806 puts("r\treturn to main menu");
807 puts("s\tchange number of sectors/track");
808 puts("v\tverify the partition table");
809 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000810 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000811 puts("b\tmove beginning of data in a partition"); /* !sun */
812 puts("c\tchange number of cylinders");
813 puts("d\tprint the raw data in the partition table");
814 puts("e\tlist extended partitions"); /* !sun */
815 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
816 puts("h\tchange number of heads");
817 puts("p\tprint the partition table");
818 puts("q\tquit without saving changes");
819 puts("r\treturn to main menu");
820 puts("s\tchange number of sectors/track");
821 puts("v\tverify the partition table");
822 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000823 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000824 puts("b\tmove beginning of data in a partition"); /* !sun */
825 puts("c\tchange number of cylinders");
826 puts("d\tprint the raw data in the partition table");
827 puts("e\tlist extended partitions"); /* !sun */
828 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000829#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000830 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000831#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000832 puts("h\tchange number of heads");
833 puts("p\tprint the partition table");
834 puts("q\tquit without saving changes");
835 puts("r\treturn to main menu");
836 puts("s\tchange number of sectors/track");
837 puts("v\tverify the partition table");
838 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000839 }
840}
841#endif /* ADVANCED mode */
842
Denis Vlasenko834410a2006-11-29 12:00:28 +0000843#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000844static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000845get_sys_types(void)
846{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000847 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000848 LABEL_IS_SUN ? sun_sys_types :
849 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000850 i386_sys_types);
851}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000852#else
853#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000854#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000855
Denis Vlasenkobd852072007-03-19 14:43:38 +0000856static const char *
857partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000858{
859 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000860 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861
Denis Vlasenkobd852072007-03-19 14:43:38 +0000862 for (i = 0; types[i]; i++)
863 if ((unsigned char)types[i][0] == type)
864 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000865
Denis Vlasenkobd852072007-03-19 14:43:38 +0000866 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000867}
868
869
Denis Vlasenko834410a2006-11-29 12:00:28 +0000870#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000871static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000872get_sysid(int i)
873{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000874 return LABEL_IS_SUN ? sunlabel->infos[i].id :
875 (LABEL_IS_SGI ? sgi_get_sysid(i) :
876 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000877}
878
Denis Vlasenkobd852072007-03-19 14:43:38 +0000879static void
880list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000881{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000882 enum { COLS = 3 };
883
884 unsigned last[COLS];
885 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000886 int i;
887
Denis Vlasenkobd852072007-03-19 14:43:38 +0000888 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000889
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000890 done = 0;
891 for (i = COLS-1; i >= 0; i--) {
892 done += (size + i - done) / (i + 1);
893 last[COLS-1 - i] = done;
894 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000895
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000896 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000897 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000898 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000899 (unsigned char)sys[next][0],
900 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000901 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000902 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000903 i = 0;
904 next = ++done;
905 }
906 } while (done < last[0]);
907 putchar('\n');
908}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000909#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000910
911static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000912is_cleared_partition(const struct partition *p)
913{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000914 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
915 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
916 get_start_sect(p) || get_nr_sects(p));
917}
918
919static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000920clear_partition(struct partition *p)
921{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000922 if (!p)
923 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000924 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000925}
926
Denis Vlasenko834410a2006-11-29 12:00:28 +0000927#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000928static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000929set_partition(int i, int doext, off_t start, off_t stop, int sysid)
930{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000931 struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +0000932 off_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933
934 if (doext) {
935 p = ptes[i].ext_pointer;
936 offset = extended_offset;
937 } else {
938 p = ptes[i].part_table;
939 offset = ptes[i].offset;
940 }
941 p->boot_ind = 0;
942 p->sys_ind = sysid;
943 set_start_sect(p, start - offset);
944 set_nr_sects(p, stop - start + 1);
945 if (dos_compatible_flag && (start/(sectors*heads) > 1023))
946 start = heads*sectors*1024 - 1;
947 set_hsc(p->head, p->sector, p->cyl, start);
948 if (dos_compatible_flag && (stop/(sectors*heads) > 1023))
949 stop = heads*sectors*1024 - 1;
950 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
951 ptes[i].changed = 1;
952}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000953#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000954
955static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000956warn_geometry(void)
957{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000958 if (heads && sectors && cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000959 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000960
Denis Vlasenkobd852072007-03-19 14:43:38 +0000961 printf("Unknown value(s) for:");
962 if (!heads)
963 printf(" heads");
964 if (!sectors)
965 printf(" sectors");
966 if (!cylinders)
967 printf(" cylinders");
968 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +0000969#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000970 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000971#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000972 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973 return 1;
974}
975
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000976static void
977update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000978{
979 int cyl_units = heads * sectors;
980
981 if (display_in_cyl_units && cyl_units)
982 units_per_sector = cyl_units;
983 else
984 units_per_sector = 1; /* in sectors */
985}
986
Denis Vlasenko834410a2006-11-29 12:00:28 +0000987#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000988static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000989warn_cylinders(void)
990{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000991 if (LABEL_IS_DOS && cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000992 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000993"The number of cylinders for this disk is set to %d.\n"
994"There is nothing wrong with that, but this is larger than 1024,\n"
995"and could in certain setups cause problems with:\n"
996"1) software that runs at boot time (e.g., old versions of LILO)\n"
997"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +0000998" (e.g., DOS FDISK, OS/2 FDISK)\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000999 cylinders);
1000}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001001#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002
1003static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001004read_extended(int ext)
1005{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006 int i;
1007 struct pte *pex;
1008 struct partition *p, *q;
1009
1010 ext_index = ext;
1011 pex = &ptes[ext];
1012 pex->ext_pointer = pex->part_table;
1013
1014 p = pex->part_table;
1015 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001016 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001017 return;
1018 }
1019
Rob Landleyb73451d2006-02-24 16:29:00 +00001020 while (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001021 struct pte *pe = &ptes[partitions];
1022
1023 if (partitions >= MAXIMUM_PARTS) {
1024 /* This is not a Linux restriction, but
1025 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001026 Do not try to 'improve' this test. */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001027 struct pte *pre = &ptes[partitions-1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001028#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001029 printf("Warning: deleting partitions after %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001030 partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001031 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001032#endif
1033 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001034 return;
1035 }
1036
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001037 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001038
1039 if (!extended_offset)
1040 extended_offset = get_start_sect(p);
1041
1042 q = p = pt_offset(pe->sectorbuffer, 0);
1043 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001044 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001046 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001047 "pointer in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001048 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001049 else
1050 pe->ext_pointer = p;
1051 } else if (p->sys_ind) {
1052 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001053 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054 "data in partition table"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 " %d\n", partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056 else
1057 pe->part_table = p;
1058 }
1059 }
1060
1061 /* very strange code here... */
1062 if (!pe->part_table) {
1063 if (q != pe->ext_pointer)
1064 pe->part_table = q;
1065 else
1066 pe->part_table = q + 1;
1067 }
1068 if (!pe->ext_pointer) {
1069 if (q != pe->part_table)
1070 pe->ext_pointer = q;
1071 else
1072 pe->ext_pointer = q + 1;
1073 }
1074
1075 p = pe->ext_pointer;
1076 partitions++;
1077 }
1078
Denis Vlasenko834410a2006-11-29 12:00:28 +00001079#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001080 /* remove empty links */
1081 remove:
1082 for (i = 4; i < partitions; i++) {
1083 struct pte *pe = &ptes[i];
1084
Denis Vlasenkobd852072007-03-19 14:43:38 +00001085 if (!get_nr_sects(pe->part_table)
1086 && (partitions > 5 || ptes[4].part_table->sys_ind)
1087 ) {
1088 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001089 delete_partition(i);
1090 goto remove; /* numbering changed */
1091 }
1092 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001093#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094}
1095
Denis Vlasenko834410a2006-11-29 12:00:28 +00001096#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001097static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001098create_doslabel(void)
1099{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001100 int i;
1101
Denis Vlasenkobd852072007-03-19 14:43:38 +00001102 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001103
1104 current_label_type = label_dos;
1105
Denis Vlasenko834410a2006-11-29 12:00:28 +00001106#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001107 possibly_osf_label = 0;
1108#endif
1109 partitions = 4;
1110
1111 for (i = 510-64; i < 510; i++)
1112 MBRbuffer[i] = 0;
1113 write_part_table_flag(MBRbuffer);
1114 extended_offset = 0;
1115 set_all_unchanged();
1116 set_changed(0);
1117 get_boot(create_empty_dos);
1118}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001119#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001120
1121static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001122get_sectorsize(void)
1123{
Rob Landley736e5252006-02-25 03:36:00 +00001124 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001125 int arg;
1126 if (ioctl(fd, BLKSSZGET, &arg) == 0)
1127 sector_size = arg;
1128 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001129 printf("Note: sector size is %d (not %d)\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001130 sector_size, DEFAULT_SECTOR_SIZE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001131 }
1132}
1133
Rob Landley88621d72006-08-29 19:41:06 +00001134static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001135get_kernel_geometry(void)
1136{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001137 struct hd_geometry geometry;
1138
1139 if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
1140 kern_heads = geometry.heads;
1141 kern_sectors = geometry.sectors;
1142 /* never use geometry.cylinders - it is truncated */
1143 }
1144}
1145
1146static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001147get_partition_table_geometry(void)
1148{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001149 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001150 struct partition *p;
1151 int i, h, s, hh, ss;
1152 int first = 1;
1153 int bad = 0;
1154
Eric Andersen3496fdc2006-01-30 23:09:20 +00001155 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001156 return;
1157
1158 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001159 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001160 p = pt_offset(bufp, i);
1161 if (p->sys_ind != 0) {
1162 h = p->end_head + 1;
1163 s = (p->end_sector & 077);
1164 if (first) {
1165 hh = h;
1166 ss = s;
1167 first = 0;
1168 } else if (hh != h || ss != s)
1169 bad = 1;
1170 }
1171 }
1172
1173 if (!first && !bad) {
1174 pt_heads = hh;
1175 pt_sectors = ss;
1176 }
1177}
1178
Rob Landleyb73451d2006-02-24 16:29:00 +00001179static void
1180get_geometry(void)
1181{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001182 int sec_fac;
Eric Andersen040f4402003-07-30 08:40:37 +00001183 unsigned long long bytes; /* really u64 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184
1185 get_sectorsize();
1186 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001187#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001188 guess_device_type();
1189#endif
1190 heads = cylinders = sectors = 0;
1191 kern_heads = kern_sectors = 0;
1192 pt_heads = pt_sectors = 0;
1193
1194 get_kernel_geometry();
1195 get_partition_table_geometry();
1196
1197 heads = user_heads ? user_heads :
1198 pt_heads ? pt_heads :
1199 kern_heads ? kern_heads : 255;
1200 sectors = user_sectors ? user_sectors :
1201 pt_sectors ? pt_sectors :
1202 kern_sectors ? kern_sectors : 63;
Eric Andersen040f4402003-07-30 08:40:37 +00001203 if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
1204 /* got bytes */
1205 } else {
1206 unsigned long longsectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001207
1208 if (ioctl(fd, BLKGETSIZE, &longsectors))
1209 longsectors = 0;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001210 bytes = ((unsigned long long) longsectors) << 9;
Eric Andersen040f4402003-07-30 08:40:37 +00001211 }
1212
1213 total_number_of_sectors = (bytes >> 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001214
1215 sector_offset = 1;
1216 if (dos_compatible_flag)
1217 sector_offset = sectors;
1218
Eric Andersen040f4402003-07-30 08:40:37 +00001219 cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001220 if (!cylinders)
1221 cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001222}
1223
1224/*
1225 * Read MBR. Returns:
1226 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1227 * 0: found or created label
1228 * 1: I/O error
1229 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001230static int
1231get_boot(enum action what)
1232{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001233 int i;
1234
1235 partitions = 4;
1236
1237 for (i = 0; i < 4; i++) {
1238 struct pte *pe = &ptes[i];
1239
1240 pe->part_table = pt_offset(MBRbuffer, i);
1241 pe->ext_pointer = NULL;
1242 pe->offset = 0;
1243 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001244#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001245 pe->changed = (what == create_empty_dos);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001246#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001247 }
1248
Denis Vlasenko834410a2006-11-29 12:00:28 +00001249#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250 if (what == create_empty_sun && check_sun_label())
1251 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001252#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253
1254 memset(MBRbuffer, 0, 512);
1255
Denis Vlasenko834410a2006-11-29 12:00:28 +00001256#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 if (what == create_empty_dos)
1258 goto got_dos_table; /* skip reading disk */
1259
Denis Vlasenkobd852072007-03-19 14:43:38 +00001260 fd = open(disk_device, type_open);
1261 if (fd < 0) {
1262 fd = open(disk_device, O_RDONLY);
1263 if (fd < 0) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001264 if (what == try_only)
1265 return 1;
1266 fdisk_fatal(unable_to_open);
1267 } else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001268 printf("You will not be able to write "
1269 "the partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001270 }
1271
1272 if (512 != read(fd, MBRbuffer, 512)) {
1273 if (what == try_only)
1274 return 1;
1275 fdisk_fatal(unable_to_read);
1276 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001277#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001278 fd = open(disk_device, O_RDONLY);
1279 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001280 return 1;
1281 if (512 != read(fd, MBRbuffer, 512))
1282 return 1;
1283#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001284
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001285 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286
1287 update_units();
1288
Denis Vlasenko834410a2006-11-29 12:00:28 +00001289#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 if (check_sun_label())
1291 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001292#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293
Denis Vlasenko834410a2006-11-29 12:00:28 +00001294#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 if (check_sgi_label())
1296 return 0;
1297#endif
1298
Denis Vlasenko834410a2006-11-29 12:00:28 +00001299#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001300 if (check_aix_label())
1301 return 0;
1302#endif
1303
Denis Vlasenko834410a2006-11-29 12:00:28 +00001304#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001305 if (check_osf_label()) {
1306 possibly_osf_label = 1;
1307 if (!valid_part_table_flag(MBRbuffer)) {
Rob Landley5527b912006-02-25 03:46:10 +00001308 current_label_type = label_osf;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309 return 0;
1310 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001311 printf("This disk has both DOS and BSD magic.\n"
1312 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001313 }
1314#endif
1315
Denis Vlasenko834410a2006-11-29 12:00:28 +00001316#if ENABLE_FEATURE_FDISK_WRITABLE
Rob Landleyb73451d2006-02-24 16:29:00 +00001317 got_dos_table:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001318#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319
1320 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001321#if !ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001322 return -1;
1323#else
Rob Landleyb73451d2006-02-24 16:29:00 +00001324 switch (what) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001325 case fdisk:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001326 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001328 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329#ifdef __sparc__
Denis Vlasenko834410a2006-11-29 12:00:28 +00001330#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001331 create_sunlabel();
1332#endif
1333#else
1334 create_doslabel();
1335#endif
1336 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001337 case try_only:
1338 return -1;
1339 case create_empty_dos:
Denis Vlasenko834410a2006-11-29 12:00:28 +00001340#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 case create_empty_sun:
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001342#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 break;
1344 default:
Denis Vlasenkobd852072007-03-19 14:43:38 +00001345 bb_error_msg_and_die("internal error");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001347#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001348 }
1349
Denis Vlasenko834410a2006-11-29 12:00:28 +00001350#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 warn_cylinders();
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001352#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001353 warn_geometry();
1354
1355 for (i = 0; i < 4; i++) {
1356 struct pte *pe = &ptes[i];
1357
Rob Landleyb73451d2006-02-24 16:29:00 +00001358 if (IS_EXTENDED(pe->part_table->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 if (partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001360 printf("Ignoring extra extended "
1361 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001362 else
1363 read_extended(i);
1364 }
1365 }
1366
1367 for (i = 3; i < partitions; i++) {
1368 struct pte *pe = &ptes[i];
1369
1370 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001371 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1372 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001373 pe->sectorbuffer[510],
1374 pe->sectorbuffer[511],
1375 i + 1);
1376#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001377 pe->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001378#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001379 }
1380 }
1381
1382 return 0;
1383}
1384
Denis Vlasenko834410a2006-11-29 12:00:28 +00001385#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386/*
1387 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1388 * If the user hits Enter, DFLT is returned.
1389 * Answers like +10 are interpreted as offsets from BASE.
1390 *
1391 * There is no default if DFLT is not between LOW and HIGH.
1392 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001393static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001394read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001395{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001396 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001398 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001400 if (dflt < low || dflt > high) {
1401 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001402 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001403 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001404
1405 while (1) {
1406 int use_default = default_ok;
1407
1408 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001409 do {
1410 printf(fmt, mesg, low, high, dflt);
1411 read_maybe_empty("");
1412 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1413 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414
Eric Andersen84bdea82004-05-19 10:49:17 +00001415 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001416 int minus = (*line_ptr == '-');
1417 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001418
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001419 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001420
Rob Landleyb73451d2006-02-24 16:29:00 +00001421 while (isdigit(*++line_ptr))
1422 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001423
Rob Landleyb73451d2006-02-24 16:29:00 +00001424 switch (*line_ptr) {
1425 case 'c':
1426 case 'C':
1427 if (!display_in_cyl_units)
1428 i *= heads * sectors;
1429 break;
1430 case 'K':
1431 absolute = 1024;
1432 break;
1433 case 'k':
1434 absolute = 1000;
1435 break;
1436 case 'm':
1437 case 'M':
1438 absolute = 1000000;
1439 break;
1440 case 'g':
1441 case 'G':
1442 absolute = 1000000000;
1443 break;
1444 default:
1445 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001446 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001447 if (absolute) {
1448 unsigned long long bytes;
1449 unsigned long unit;
1450
1451 bytes = (unsigned long long) i * absolute;
1452 unit = sector_size * units_per_sector;
1453 bytes += unit/2; /* round */
1454 bytes /= unit;
1455 i = bytes;
1456 }
1457 if (minus)
1458 i = -i;
1459 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001460 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 i = atoi(line_ptr);
1462 while (isdigit(*line_ptr)) {
1463 line_ptr++;
1464 use_default = 0;
1465 }
1466 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001467 if (use_default) {
1468 i = dflt;
1469 printf("Using default value %u\n", i);
1470 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471 if (i >= low && i <= high)
1472 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001473 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 }
1475 return i;
1476}
1477
Rob Landleyb73451d2006-02-24 16:29:00 +00001478static int
1479get_partition(int warn, int max)
1480{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001481 struct pte *pe;
1482 int i;
1483
Denis Vlasenkobd852072007-03-19 14:43:38 +00001484 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001485 pe = &ptes[i];
1486
1487 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001488 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1489 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1490 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1491 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001492 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001493 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001494 }
1495 return i;
1496}
1497
1498static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001499get_existing_partition(int warn, int max)
1500{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501 int pno = -1;
1502 int i;
1503
1504 for (i = 0; i < max; i++) {
1505 struct pte *pe = &ptes[i];
1506 struct partition *p = pe->part_table;
1507
1508 if (p && !is_cleared_partition(p)) {
1509 if (pno >= 0)
1510 goto not_unique;
1511 pno = i;
1512 }
1513 }
1514 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001515 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001516 return pno;
1517 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001518 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001519 return -1;
1520
1521 not_unique:
1522 return get_partition(warn, max);
1523}
1524
1525static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001526get_nonexisting_partition(int warn, int max)
1527{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001528 int pno = -1;
1529 int i;
1530
1531 for (i = 0; i < max; i++) {
1532 struct pte *pe = &ptes[i];
1533 struct partition *p = pe->part_table;
1534
1535 if (p && is_cleared_partition(p)) {
1536 if (pno >= 0)
1537 goto not_unique;
1538 pno = i;
1539 }
1540 }
1541 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001542 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001543 return pno;
1544 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001545 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001546 return -1;
1547
1548 not_unique:
1549 return get_partition(warn, max);
1550}
1551
1552
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001553static void
1554change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555{
1556 display_in_cyl_units = !display_in_cyl_units;
1557 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001558 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001559 str_units(PLURAL));
1560}
1561
1562static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001563toggle_active(int i)
1564{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001565 struct pte *pe = &ptes[i];
1566 struct partition *p = pe->part_table;
1567
Rob Landleyb73451d2006-02-24 16:29:00 +00001568 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001569 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1571 pe->changed = 1;
1572}
1573
1574static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001575toggle_dos_compatibility_flag(void)
1576{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001577 dos_compatible_flag = ~dos_compatible_flag;
1578 if (dos_compatible_flag) {
1579 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001580 printf("DOS Compatibility flag is set\n");
1581 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001582 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001583 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001584 }
1585}
1586
1587static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001588delete_partition(int i)
1589{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001590 struct pte *pe = &ptes[i];
1591 struct partition *p = pe->part_table;
1592 struct partition *q = pe->ext_pointer;
1593
1594/* Note that for the fifth partition (i == 4) we don't actually
1595 * decrement partitions.
1596 */
1597
1598 if (warn_geometry())
1599 return; /* C/H/S not set */
1600 pe->changed = 1;
1601
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001602 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603 sun_delete_partition(i);
1604 return;
1605 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001606 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001607 sgi_delete_partition(i);
1608 return;
1609 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001610
1611 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001612 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613 partitions = 4;
1614 ptes[ext_index].ext_pointer = NULL;
1615 extended_offset = 0;
1616 }
1617 clear_partition(p);
1618 return;
1619 }
1620
1621 if (!q->sys_ind && i > 4) {
1622 /* the last one in the chain - just delete */
1623 --partitions;
1624 --i;
1625 clear_partition(ptes[i].ext_pointer);
1626 ptes[i].changed = 1;
1627 } else {
1628 /* not the last one - further ones will be moved down */
1629 if (i > 4) {
1630 /* delete this link in the chain */
1631 p = ptes[i-1].ext_pointer;
1632 *p = *q;
1633 set_start_sect(p, get_start_sect(q));
1634 set_nr_sects(p, get_nr_sects(q));
1635 ptes[i-1].changed = 1;
1636 } else if (partitions > 5) { /* 5 will be moved to 4 */
1637 /* the first logical in a longer chain */
1638 pe = &ptes[5];
1639
1640 if (pe->part_table) /* prevent SEGFAULT */
1641 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001642 get_partition_start(pe) -
1643 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001644 pe->offset = extended_offset;
1645 pe->changed = 1;
1646 }
1647
1648 if (partitions > 5) {
1649 partitions--;
1650 while (i < partitions) {
1651 ptes[i] = ptes[i+1];
1652 i++;
1653 }
1654 } else
1655 /* the only logical: clear only */
1656 clear_partition(ptes[i].part_table);
1657 }
1658}
1659
1660static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001661change_sysid(void)
1662{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 int i, sys, origsys;
1664 struct partition *p;
1665
Eric Andersen040f4402003-07-30 08:40:37 +00001666 /* If sgi_label then don't use get_existing_partition,
1667 let the user select a partition, since get_existing_partition()
1668 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001669 if (!LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001670 i = get_existing_partition(0, partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001671 } else {
1672 i = get_partition(0, partitions);
1673 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001674 if (i == -1)
1675 return;
1676 p = ptes[i].part_table;
1677 origsys = sys = get_sysid(i);
1678
1679 /* if changing types T to 0 is allowed, then
1680 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001681 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001682 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001683 return;
1684 }
1685 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001686 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001688 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001689 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001690 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001691 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001692 /* break; */
1693 }
1694
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001695 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001696 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001697 printf("You cannot change a partition into"
1698 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001699 break;
1700 }
1701 }
1702
1703 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001704#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001705 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001706 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001707 "as Whole disk (5),\n"
1708 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001709 "even Linux likes it\n\n");
1710#endif
1711#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001712 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001713 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001714 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001715 (i == 8 && sys != 0)
1716 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001717 ) {
1718 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001719 "as volume header (0),\nand "
1720 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001721 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001722 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001723#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001724 if (sys == origsys)
1725 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001726 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001727 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001728 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 sgi_change_sysid(i, sys);
1730 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001732
Denis Vlasenkobd852072007-03-19 14:43:38 +00001733 printf("Changed system type of partition %d "
1734 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001735 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001736 ptes[i].changed = 1;
1737 if (is_dos_partition(origsys) ||
Rob Landleyb73451d2006-02-24 16:29:00 +00001738 is_dos_partition(sys))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001739 dos_changed = 1;
1740 break;
1741 }
1742 }
1743}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001744#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001745
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001746
Denis Vlasenko28703012006-12-19 20:32:02 +00001747/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1749 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1750 * Lubkin Oct. 1991). */
1751
Rob Landleyb73451d2006-02-24 16:29:00 +00001752static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001753linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001754{
1755 int spc = heads * sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756
1757 *c = ls / spc;
1758 ls = ls % spc;
1759 *h = ls / sectors;
1760 *s = ls % sectors + 1; /* sectors count from 1 */
1761}
1762
Rob Landleyb73451d2006-02-24 16:29:00 +00001763static void
1764check_consistency(const struct partition *p, int partition)
1765{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001766 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1767 unsigned pec, peh, pes; /* physical ending c, h, s */
1768 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1769 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001770
1771 if (!heads || !sectors || (partition >= 4))
1772 return; /* do not check extended partitions */
1773
1774/* physical beginning c, h, s */
1775 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1776 pbh = p->head;
1777 pbs = p->sector & 0x3f;
1778
1779/* physical ending c, h, s */
1780 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1781 peh = p->end_head;
1782 pes = p->end_sector & 0x3f;
1783
1784/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001785 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001786
1787/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001788 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001789
1790/* Same physical / logical beginning? */
1791 if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001792 printf("Partition %d has different physical/logical "
1793 "beginnings (non-Linux?):\n", partition + 1);
1794 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
1795 printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001796 }
1797
1798/* Same physical / logical ending? */
1799 if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001800 printf("Partition %d has different physical/logical "
1801 "endings:\n", partition + 1);
1802 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1803 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804 }
1805
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001806/* Ending on cylinder boundary? */
1807 if (peh != (heads - 1) || pes != sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001808 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001809 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001810 }
1811}
1812
1813static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001814list_disk_geometry(void)
1815{
Eric Andersen040f4402003-07-30 08:40:37 +00001816 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001817 long megabytes = bytes/1000000;
1818
1819 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001820 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001821 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001822 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001823 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001824 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001825 printf("%d heads, %d sectors/track, %d cylinders",
Rob Landleyb73451d2006-02-24 16:29:00 +00001826 heads, sectors, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001827 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001828 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001829 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001830 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001831 str_units(PLURAL),
1832 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833}
1834
1835/*
1836 * Check whether partition entries are ordered by their starting positions.
1837 * Return 0 if OK. Return i if partition i should have been earlier.
1838 * Two separate checks: primary and logical partitions.
1839 */
1840static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001841wrong_p_order(int *prev)
1842{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001843 const struct pte *pe;
1844 const struct partition *p;
Eric Andersend9261492004-06-28 23:50:31 +00001845 off_t last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001846 int i, last_i = 0;
1847
1848 for (i = 0 ; i < partitions; i++) {
1849 if (i == 4) {
1850 last_i = 4;
1851 last_p_start_pos = 0;
1852 }
1853 pe = &ptes[i];
1854 if ((p = pe->part_table)->sys_ind) {
1855 p_start_pos = get_partition_start(pe);
1856
1857 if (last_p_start_pos > p_start_pos) {
1858 if (prev)
1859 *prev = last_i;
1860 return i;
1861 }
1862
1863 last_p_start_pos = p_start_pos;
1864 last_i = i;
1865 }
1866 }
1867 return 0;
1868}
1869
Denis Vlasenko834410a2006-11-29 12:00:28 +00001870#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001871/*
1872 * Fix the chain of logicals.
1873 * extended_offset is unchanged, the set of sectors used is unchanged
1874 * The chain is sorted so that sectors increase, and so that
1875 * starting sectors increase.
1876 *
1877 * After this it may still be that cfdisk doesnt like the table.
1878 * (This is because cfdisk considers expanded parts, from link to
1879 * end of partition, and these may still overlap.)
1880 * Now
1881 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1882 * may help.
1883 */
1884static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001885fix_chain_of_logicals(void)
1886{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001887 int j, oj, ojj, sj, sjj;
1888 struct partition *pj,*pjj,tmp;
1889
1890 /* Stage 1: sort sectors but leave sector of part 4 */
1891 /* (Its sector is the global extended_offset.) */
1892 stage1:
1893 for (j = 5; j < partitions-1; j++) {
1894 oj = ptes[j].offset;
1895 ojj = ptes[j+1].offset;
1896 if (oj > ojj) {
1897 ptes[j].offset = ojj;
1898 ptes[j+1].offset = oj;
1899 pj = ptes[j].part_table;
1900 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1901 pjj = ptes[j+1].part_table;
1902 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1903 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001904 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001905 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001906 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001907 goto stage1;
1908 }
1909 }
1910
1911 /* Stage 2: sort starting sectors */
1912 stage2:
1913 for (j = 4; j < partitions-1; j++) {
1914 pj = ptes[j].part_table;
1915 pjj = ptes[j+1].part_table;
1916 sj = get_start_sect(pj);
1917 sjj = get_start_sect(pjj);
1918 oj = ptes[j].offset;
1919 ojj = ptes[j+1].offset;
1920 if (oj+sj > ojj+sjj) {
1921 tmp = *pj;
1922 *pj = *pjj;
1923 *pjj = tmp;
1924 set_start_sect(pj, ojj+sjj-oj);
1925 set_start_sect(pjj, oj+sj-ojj);
1926 goto stage2;
1927 }
1928 }
1929
1930 /* Probably something was changed */
1931 for (j = 4; j < partitions; j++)
1932 ptes[j].changed = 1;
1933}
1934
1935
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001936static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001937fix_partition_table_order(void)
1938{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001939 struct pte *pei, *pek;
1940 int i,k;
1941
1942 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001943 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001944 return;
1945 }
1946
1947 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1948 /* partition i should have come earlier, move it */
1949 /* We have to move data in the MBR */
1950 struct partition *pi, *pk, *pe, pbuf;
1951 pei = &ptes[i];
1952 pek = &ptes[k];
1953
1954 pe = pei->ext_pointer;
1955 pei->ext_pointer = pek->ext_pointer;
1956 pek->ext_pointer = pe;
1957
1958 pi = pei->part_table;
1959 pk = pek->part_table;
1960
1961 memmove(&pbuf, pi, sizeof(struct partition));
1962 memmove(pi, pk, sizeof(struct partition));
1963 memmove(pk, &pbuf, sizeof(struct partition));
1964
1965 pei->changed = pek->changed = 1;
1966 }
1967
1968 if (i)
1969 fix_chain_of_logicals();
1970
1971 printf("Done.\n");
1972
1973}
1974#endif
1975
1976static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001977list_table(int xtra)
1978{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001979 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001980 int i, w;
1981
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001982 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001983 sun_list_table(xtra);
1984 return;
1985 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001986 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001987 sgi_list_table(xtra);
1988 return;
1989 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001990
1991 list_disk_geometry();
1992
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001993 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001994 xbsd_print_disklabel(xtra);
1995 return;
1996 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001997
1998 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
1999 but if the device name ends in a digit, say /dev/foo1,
2000 then the partition is called /dev/foo1p3. */
2001 w = strlen(disk_device);
2002 if (w && isdigit(disk_device[w-1]))
2003 w++;
2004 if (w < 5)
2005 w = 5;
2006
Denis Vlasenkobd852072007-03-19 14:43:38 +00002007 // 1 12345678901 12345678901 12345678901 12
2008 printf("%*s Boot Start End Blocks Id System\n",
2009 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010
2011 for (i = 0; i < partitions; i++) {
2012 const struct pte *pe = &ptes[i];
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002013 off_t psects;
2014 off_t pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002015 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002016
2017 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002018 if (!p || is_cleared_partition(p))
2019 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002021 psects = get_nr_sects(p);
2022 pblocks = psects;
2023 podd = 0;
2024
2025 if (sector_size < 1024) {
2026 pblocks /= (1024 / sector_size);
2027 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002028 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002029 if (sector_size > 1024)
2030 pblocks *= (sector_size / 1024);
2031
2032 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2033 partname(disk_device, i+1, w+2),
2034 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2035 ? '*' : '?',
2036 (unsigned long long) cround(get_partition_start(pe)), /* start */
2037 (unsigned long long) cround(get_partition_start(pe) + psects /* end */
2038 - (psects ? 1 : 0)),
2039 (unsigned long long) pblocks, podd ? '+' : ' ', /* odd flag on end */
2040 p->sys_ind, /* type id */
2041 partition_type(p->sys_ind)); /* type name */
2042
2043 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002044 }
2045
2046 /* Is partition table in disk order? It need not be, but... */
2047 /* partition table entries are not checked for correct order if this
2048 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002049 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002050 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002051 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002052 }
2053}
2054
Denis Vlasenko834410a2006-11-29 12:00:28 +00002055#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002056static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002057x_list_table(int extend)
2058{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002059 const struct pte *pe;
2060 const struct partition *p;
2061 int i;
2062
Denis Vlasenkobd852072007-03-19 14:43:38 +00002063 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002064 disk_device, heads, sectors, cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002065 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002066 for (i = 0 ; i < partitions; i++) {
2067 pe = &ptes[i];
2068 p = (extend ? pe->ext_pointer : pe->part_table);
2069 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002070 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002071 i + 1, p->boot_ind, p->head,
2072 sector(p->sector),
2073 cylinder(p->sector, p->cyl), p->end_head,
2074 sector(p->end_sector),
2075 cylinder(p->end_sector, p->end_cyl),
2076 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2077 if (p->sys_ind)
2078 check_consistency(p, i);
2079 }
2080 }
2081}
2082#endif
2083
Denis Vlasenko834410a2006-11-29 12:00:28 +00002084#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002085static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002086fill_bounds(off_t *first, off_t *last)
2087{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002088 int i;
2089 const struct pte *pe = &ptes[0];
2090 const struct partition *p;
2091
2092 for (i = 0; i < partitions; pe++,i++) {
2093 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002094 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002095 first[i] = 0xffffffff;
2096 last[i] = 0;
2097 } else {
2098 first[i] = get_partition_start(pe);
2099 last[i] = first[i] + get_nr_sects(p) - 1;
2100 }
2101 }
2102}
2103
2104static void
Denis Vlasenko834410a2006-11-29 12:00:28 +00002105check(int n, unsigned h, unsigned s, unsigned c, off_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002106{
Eric Andersend9261492004-06-28 23:50:31 +00002107 off_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002108
2109 real_s = sector(s) - 1;
2110 real_c = cylinder(s, c);
2111 total = (real_c * sectors + real_s) * heads + h;
2112 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002113 printf("Partition %d contains sector 0\n", n);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002114 if (h >= heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002115 printf("Partition %d: head %d greater than maximum %d\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002116 n, h + 1, heads);
2117 if (real_s >= sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002118 printf("Partition %d: sector %d greater than "
2119 "maximum %d\n", n, s, sectors);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002120 if (real_c >= cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002121 printf("Partition %d: cylinder %"OFF_FMT"u greater than "
2122 "maximum %d\n", n, real_c + 1, cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002123 if (cylinders <= 1024 && start != total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002124 printf("Partition %d: previous sectors %"OFF_FMT"u disagrees with "
2125 "total %"OFF_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126}
2127
2128static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002129verify(void)
2130{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002131 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002132 unsigned total = 1;
Eric Andersend9261492004-06-28 23:50:31 +00002133 off_t first[partitions], last[partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002134 struct partition *p;
2135
2136 if (warn_geometry())
2137 return;
2138
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002139 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 verify_sun();
2141 return;
2142 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002143 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002144 verify_sgi(1);
2145 return;
2146 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002147
2148 fill_bounds(first, last);
2149 for (i = 0; i < partitions; i++) {
2150 struct pte *pe = &ptes[i];
2151
2152 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002153 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002154 check_consistency(p, i);
2155 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002156 printf("Warning: bad start-of-data in "
2157 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002158 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2159 last[i]);
2160 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002161 for (j = 0; j < i; j++) {
2162 if ((first[i] >= first[j] && first[i] <= last[j])
2163 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2164 printf("Warning: partition %d overlaps "
2165 "partition %d\n", j + 1, i + 1);
2166 total += first[i] >= first[j] ?
2167 first[i] : first[j];
2168 total -= last[i] <= last[j] ?
2169 last[i] : last[j];
2170 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002171 }
2172 }
2173 }
2174
2175 if (extended_offset) {
2176 struct pte *pex = &ptes[ext_index];
Eric Andersend9261492004-06-28 23:50:31 +00002177 off_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002178 get_nr_sects(pex->part_table) - 1;
2179
2180 for (i = 4; i < partitions; i++) {
2181 total++;
2182 p = ptes[i].part_table;
2183 if (!p->sys_ind) {
2184 if (i != 4 || i + 1 < partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002185 printf("Warning: partition %d "
2186 "is empty\n", i + 1);
2187 } else if (first[i] < extended_offset || last[i] > e_last) {
2188 printf("Logical partition %d not entirely in "
2189 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002190 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002191 }
2192 }
2193
2194 if (total > heads * sectors * cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002195 printf("Total allocated sectors %d greater than the maximum "
2196 "%d\n", total, heads * sectors * cylinders);
2197 else {
2198 total = heads * sectors * cylinders - total;
2199 if (total != 0)
2200 printf("%d unallocated sectors\n", total);
2201 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202}
2203
2204static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002205add_partition(int n, int sys)
2206{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002208 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 struct partition *p = ptes[n].part_table;
2210 struct partition *q = ptes[ext_index].part_table;
Eric Andersen040f4402003-07-30 08:40:37 +00002211 long long llimit;
Eric Andersend9261492004-06-28 23:50:31 +00002212 off_t start, stop = 0, limit, temp,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002213 first[partitions], last[partitions];
2214
2215 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002216 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002217 return;
2218 }
2219 fill_bounds(first, last);
2220 if (n < 4) {
2221 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002222 if (display_in_cyl_units || !total_number_of_sectors)
2223 llimit = heads * sectors * cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002224 else
Eric Andersen040f4402003-07-30 08:40:37 +00002225 llimit = total_number_of_sectors - 1;
2226 limit = llimit;
2227 if (limit != llimit)
2228 limit = 0x7fffffff;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002229 if (extended_offset) {
2230 first[ext_index] = extended_offset;
2231 last[ext_index] = get_start_sect(q) +
2232 get_nr_sects(q) - 1;
2233 }
2234 } else {
2235 start = extended_offset + sector_offset;
2236 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2237 }
2238 if (display_in_cyl_units)
2239 for (i = 0; i < partitions; i++)
2240 first[i] = (cround(first[i]) - 1) * units_per_sector;
2241
Denis Vlasenkobd852072007-03-19 14:43:38 +00002242 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002243 do {
2244 temp = start;
2245 for (i = 0; i < partitions; i++) {
2246 int lastplusoff;
2247
2248 if (start == ptes[i].offset)
2249 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002250 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002251 if (start >= first[i] && start <= lastplusoff)
2252 start = lastplusoff + 1;
2253 }
2254 if (start > limit)
2255 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002256 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002257 printf("Sector %"OFF_FMT"d is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002259 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002260 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002261 if (!num_read && start == temp) {
Eric Andersend9261492004-06-28 23:50:31 +00002262 off_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002263
2264 saved_start = start;
2265 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2266 0, mesg);
2267 if (display_in_cyl_units) {
2268 start = (start - 1) * units_per_sector;
2269 if (start < saved_start) start = saved_start;
2270 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002271 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002272 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002273 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002274 if (n > 4) { /* NOT for fifth partition */
2275 struct pte *pe = &ptes[n];
2276
2277 pe->offset = start - sector_offset;
2278 if (pe->offset == extended_offset) { /* must be corrected */
2279 pe->offset++;
2280 if (sector_offset == 1)
2281 start++;
2282 }
2283 }
2284
2285 for (i = 0; i < partitions; i++) {
2286 struct pte *pe = &ptes[i];
2287
2288 if (start < pe->offset && limit >= pe->offset)
2289 limit = pe->offset - 1;
2290 if (start < first[i] && limit >= first[i])
2291 limit = first[i] - 1;
2292 }
2293 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002294 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002295 if (n > 4)
2296 partitions--;
2297 return;
2298 }
2299 if (cround(start) == cround(limit)) {
2300 stop = limit;
2301 } else {
2302 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002303 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002304 str_units(SINGULAR));
2305 stop = read_int(cround(start), cround(limit), cround(limit),
2306 cround(start), mesg);
2307 if (display_in_cyl_units) {
2308 stop = stop * units_per_sector - 1;
2309 if (stop >limit)
2310 stop = limit;
2311 }
2312 }
2313
2314 set_partition(n, 0, start, stop, sys);
2315 if (n > 4)
2316 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2317
Rob Landleyb73451d2006-02-24 16:29:00 +00002318 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 struct pte *pe4 = &ptes[4];
2320 struct pte *pen = &ptes[n];
2321
2322 ext_index = n;
2323 pen->ext_pointer = p;
2324 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002325 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002326 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2327 pe4->ext_pointer = pe4->part_table + 1;
2328 pe4->changed = 1;
2329 partitions = 5;
2330 }
2331}
2332
2333static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002334add_logical(void)
2335{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002336 if (partitions > 5 || ptes[4].part_table->sys_ind) {
2337 struct pte *pe = &ptes[partitions];
2338
Rob Landley081e3842006-08-03 20:07:35 +00002339 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002340 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2341 pe->ext_pointer = pe->part_table + 1;
2342 pe->offset = 0;
2343 pe->changed = 1;
2344 partitions++;
2345 }
2346 add_partition(partitions - 1, LINUX_NATIVE);
2347}
2348
2349static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002350new_partition(void)
2351{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002352 int i, free_primary = 0;
2353
2354 if (warn_geometry())
2355 return;
2356
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002357 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 add_sun_partition(get_partition(0, partitions), LINUX_NATIVE);
2359 return;
2360 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002361 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362 sgi_add_partition(get_partition(0, partitions), LINUX_NATIVE);
2363 return;
2364 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002365 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002366 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2367"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2368"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002369 return;
2370 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002371
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 for (i = 0; i < 4; i++)
2373 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002374
Rob Landleyb73451d2006-02-24 16:29:00 +00002375 if (!free_primary && partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002376 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002377 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002378 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002379
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002380 if (!free_primary) {
2381 if (extended_offset)
2382 add_logical();
2383 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002384 printf("You must delete some partition and add "
2385 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002387 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002388 snprintf(line, sizeof(line),
2389 "Command action\n"
2390 " %s\n"
2391 " p primary partition (1-4)\n",
2392 (extended_offset ?
2393 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002395 c = read_nonempty(line);
2396 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 i = get_nonexisting_partition(0, 4);
2398 if (i >= 0)
2399 add_partition(i, LINUX_NATIVE);
2400 return;
2401 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002402 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403 add_logical();
2404 return;
2405 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002406 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002407 i = get_nonexisting_partition(0, 4);
2408 if (i >= 0)
2409 add_partition(i, EXTENDED);
2410 return;
2411 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002412 printf("Invalid partition number "
2413 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002414 }
2415 }
2416}
2417
2418static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002419write_table(void)
2420{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002421 int i;
2422
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002423 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002424 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002425 if (ptes[i].changed)
2426 ptes[3].changed = 1;
2427 for (i = 3; i < partitions; i++) {
2428 struct pte *pe = &ptes[i];
2429
2430 if (pe->changed) {
2431 write_part_table_flag(pe->sectorbuffer);
2432 write_sector(pe->offset, pe->sectorbuffer);
2433 }
2434 }
2435 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002436 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002437 /* no test on change? the printf below might be mistaken */
2438 sgi_write_table();
2439 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002440 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002441 int needw = 0;
2442
Rob Landleyb73451d2006-02-24 16:29:00 +00002443 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002444 if (ptes[i].changed)
2445 needw = 1;
2446 if (needw)
2447 sun_write_table();
2448 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449
Denis Vlasenkobd852072007-03-19 14:43:38 +00002450 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 reread_partition_table(1);
2452}
2453
Rob Landleyb73451d2006-02-24 16:29:00 +00002454static void
2455reread_partition_table(int leave)
2456{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 int i;
2458
Denis Vlasenkobd852072007-03-19 14:43:38 +00002459 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002460 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002461 /* sleep(2); Huh? */
Denis Vlasenko28703012006-12-19 20:32:02 +00002462 i = ioctl(fd, BLKRRPART);
2463#if 0
2464 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002465 /* some kernel versions (1.2.x) seem to have trouble
2466 rereading the partition table, but if asked to do it
2467 twice, the second time works. - biro@yggdrasil.com */
2468 sync();
2469 sleep(2);
Denis Vlasenko28703012006-12-19 20:32:02 +00002470 i = ioctl(fd, BLKRRPART);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002471 }
Denis Vlasenko28703012006-12-19 20:32:02 +00002472#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002473
2474 if (i) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002475 bb_perror_msg("WARNING: rereading partition table "
2476 "failed, kernel still uses old table");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002477 }
2478
Denis Vlasenko28703012006-12-19 20:32:02 +00002479#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002480 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002481 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002482 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002484 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002485#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002486
2487 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002488 if (ENABLE_FEATURE_CLEAN_UP)
2489 close(fd);
2490 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491 }
2492}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002493#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494
Denis Vlasenko834410a2006-11-29 12:00:28 +00002495#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496#define MAX_PER_LINE 16
2497static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002498print_buffer(char *pbuffer)
2499{
2500 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002501
2502 for (i = 0, l = 0; i < sector_size; i++, l++) {
2503 if (l == 0)
2504 printf("0x%03X:", i);
2505 printf(" %02X", (unsigned char) pbuffer[i]);
2506 if (l == MAX_PER_LINE - 1) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002507 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002508 l = -1;
2509 }
2510 }
2511 if (l > 0)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002512 puts("");
2513 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002514}
2515
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002516static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002517print_raw(void)
2518{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 int i;
2520
Denis Vlasenkobd852072007-03-19 14:43:38 +00002521 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002522 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002523 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002524 else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525 for (i = 3; i < partitions; i++)
2526 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002527 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002528}
2529
2530static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002531move_begin(int i)
2532{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002533 struct pte *pe = &ptes[i];
2534 struct partition *p = pe->part_table;
Eric Andersend9261492004-06-28 23:50:31 +00002535 off_t new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002536
2537 if (warn_geometry())
2538 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002539 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002540 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002541 return;
2542 }
2543 first = get_partition_start(pe);
2544 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002545 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002546
2547 if (new != get_nr_sects(p)) {
2548 first = get_nr_sects(p) + get_start_sect(p) - new;
2549 set_nr_sects(p, first);
2550 set_start_sect(p, new);
2551 pe->changed = 1;
2552 }
2553}
2554
2555static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002556xselect(void)
2557{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002558 char c;
2559
Rob Landleyb73451d2006-02-24 16:29:00 +00002560 while (1) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002561 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002562 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002563 switch (c) {
2564 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002565 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002566 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002567 break;
2568 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002569 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 move_begin(get_partition(0, partitions));
2571 break;
2572 case 'c':
2573 user_cylinders = cylinders =
2574 read_int(1, cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002575 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002576 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 sun_set_ncyl(cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002578 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002579 warn_cylinders();
2580 break;
2581 case 'd':
2582 print_raw();
2583 break;
2584 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002585 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002586 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002588 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002589 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 x_list_table(1);
2591 break;
2592 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002593 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002594 fix_partition_table_order();
2595 break;
2596 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002597#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002598 create_sgilabel();
2599#endif
2600 break;
2601 case 'h':
2602 user_heads = heads = read_int(1, heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002603 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002604 update_units();
2605 break;
2606 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002607 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002608 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 break;
2610 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002611 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002612 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002613 break;
2614 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002615 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 list_table(1);
2617 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618 x_list_table(0);
2619 break;
2620 case 'q':
2621 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002622 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 exit(0);
2624 case 'r':
2625 return;
2626 case 's':
2627 user_sectors = sectors = read_int(1, sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002628 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002629 if (dos_compatible_flag) {
2630 sector_offset = sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002631 printf("Warning: setting sector offset for DOS "
2632 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 }
2634 update_units();
2635 break;
2636 case 'v':
2637 verify();
2638 break;
2639 case 'w':
2640 write_table(); /* does not return */
2641 break;
2642 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002643 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 break;
2646 default:
2647 xmenu();
2648 }
2649 }
2650}
2651#endif /* ADVANCED mode */
2652
2653static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002654is_ide_cdrom_or_tape(const char *device)
2655{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002656 FILE *procf;
2657 char buf[100];
2658 struct stat statbuf;
2659 int is_ide = 0;
2660
2661 /* No device was given explicitly, and we are trying some
2662 likely things. But opening /dev/hdc may produce errors like
2663 "hdc: tray open or drive not ready"
2664 if it happens to be a CD-ROM drive. It even happens that
2665 the process hangs on the attempt to read a music CD.
2666 So try to be careful. This only works since 2.1.73. */
2667
2668 if (strncmp("/dev/hd", device, 7))
2669 return 0;
2670
2671 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2672 procf = fopen(buf, "r");
2673 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2674 is_ide = (!strncmp(buf, "cdrom", 5) ||
2675 !strncmp(buf, "tape", 4));
2676 else
2677 /* Now when this proc file does not exist, skip the
2678 device when it is read-only. */
2679 if (stat(device, &statbuf) == 0)
2680 is_ide = ((statbuf.st_mode & 0222) == 0);
2681
2682 if (procf)
2683 fclose(procf);
2684 return is_ide;
2685}
2686
Rob Landley5527b912006-02-25 03:46:10 +00002687
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688static void
Denis Vlasenkod5470832007-01-03 02:58:54 +00002689trydev(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002690{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002691 int gb;
2692
2693 disk_device = device;
2694 if (setjmp(listingbuf))
2695 return;
2696 if (!user_specified)
2697 if (is_ide_cdrom_or_tape(device))
2698 return;
Denis Vlasenko28703012006-12-19 20:32:02 +00002699 fd = open(disk_device, type_open);
2700 if (fd >= 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002701 gb = get_boot(try_only);
2702 if (gb > 0) { /* I/O error */
2703 close(fd);
2704 } else if (gb < 0) { /* no DOS signature */
2705 list_disk_geometry();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002706 if (LABEL_IS_AIX) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707 return;
Rob Landley5527b912006-02-25 03:46:10 +00002708 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002709#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenkod5470832007-01-03 02:58:54 +00002710 if (bsd_trydev(device) < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002711#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00002712 printf("Disk %s doesn't contain a valid "
2713 "partition table\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002714 close(fd);
2715 } else {
2716 close(fd);
2717 list_table(0);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002718#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002719 if (!LABEL_IS_SUN && partitions > 4){
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002720 delete_partition(ext_index);
Rob Landley5527b912006-02-25 03:46:10 +00002721 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002722#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 }
2724 } else {
2725 /* Ignore other errors, since we try IDE
2726 and SCSI hard disks which may not be
2727 installed on the system. */
2728 if (errno == EACCES) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002729 printf("Cannot open %s\n", device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002730 return;
2731 }
2732 }
2733}
2734
2735/* for fdisk -l: try all things in /proc/partitions
2736 that look like a partition name (do not end in a digit) */
2737static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002738tryprocpt(void)
2739{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002740 FILE *procpt;
2741 char line[100], ptname[100], devname[120], *s;
2742 int ma, mi, sz;
2743
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002744 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002745
2746 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002747 if (sscanf(line, " %d %d %d %[^\n ]",
2748 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002749 continue;
2750 for (s = ptname; *s; s++);
2751 if (isdigit(s[-1]))
2752 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002753 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenkod5470832007-01-03 02:58:54 +00002754 trydev(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002755 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002756#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002757 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002758#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759}
2760
Denis Vlasenko834410a2006-11-29 12:00:28 +00002761#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002762static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002763unknown_command(int c)
2764{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002765 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002766}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002767#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768
Denis Vlasenko06af2162007-02-03 17:28:39 +00002769int fdisk_main(int argc, char **argv);
Rob Landleyb73451d2006-02-24 16:29:00 +00002770int fdisk_main(int argc, char **argv)
2771{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002772 char *str_b, *str_C, *str_H, *str_S;
2773 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002774 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002775 * fdisk -v
2776 * fdisk -l [-b sectorsize] [-u] device ...
2777 * fdisk -s [partition] ...
2778 * fdisk [-b sectorsize] [-u] device
2779 *
2780 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002781 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00002782 enum {
2783 OPT_b = 1 << 0,
2784 OPT_C = 1 << 1,
2785 OPT_H = 1 << 2,
2786 OPT_l = 1 << 3,
2787 OPT_S = 1 << 4,
2788 OPT_u = 1 << 5,
2789 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2790 };
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002791
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002792 PTR_TO_GLOBALS = xzalloc(sizeof(G));
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002793
Denis Vlasenko834410a2006-11-29 12:00:28 +00002794 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2795 &str_b, &str_C, &str_H, &str_S);
2796 argc -= optind;
2797 argv += optind;
2798 if (opt & OPT_b) { // -b
2799 /* Ugly: this sector size is really per device,
2800 so cannot be combined with multiple disks,
2801 and the same goes for the C/H/S options.
2802 */
2803 sector_size = xatoi_u(str_b);
2804 if (sector_size != 512 && sector_size != 1024 &&
2805 sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002806 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002807 sector_offset = 2;
2808 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002810 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2811 if (opt & OPT_H) { // -H
2812 user_heads = xatoi_u(str_H);
2813 if (user_heads <= 0 || user_heads >= 256)
2814 user_heads = 0;
2815 }
2816 //if (opt & OPT_l) // -l
2817 if (opt & OPT_S) { // -S
2818 user_sectors = xatoi_u(str_S);
2819 if (user_sectors <= 0 || user_sectors >= 64)
2820 user_sectors = 0;
2821 }
2822 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2823 //if (opt & OPT_s) // -s
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824
Denis Vlasenko834410a2006-11-29 12:00:28 +00002825 if (user_set_sector_size && argc != 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002826 printf("Warning: the -b (set sector size) option should"
2827 " be used with one specified device\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828
Denis Vlasenko834410a2006-11-29 12:00:28 +00002829#if ENABLE_FEATURE_FDISK_WRITABLE
2830 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002832#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 type_open = O_RDONLY;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002834 if (argc > 0) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 int k;
Denis Vlasenko28703012006-12-19 20:32:02 +00002836#if defined(__GNUC__)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837 /* avoid gcc warning:
2838 variable `k' might be clobbered by `longjmp' */
2839 (void)&k;
2840#endif
2841 listing = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002842 for (k = 0; k < argc; k++)
Denis Vlasenkod5470832007-01-03 02:58:54 +00002843 trydev(argv[k], 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002844 } else {
2845 /* we no longer have default device names */
2846 /* but, we can use /proc/partitions instead */
2847 tryprocpt();
2848 }
2849 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002851 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002852#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853
Denis Vlasenko834410a2006-11-29 12:00:28 +00002854#if ENABLE_FEATURE_FDISK_BLKSIZE
2855 if (opt & OPT_s) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002856 long size;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002857 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858
2859 nowarn = 1;
2860 type_open = O_RDONLY;
2861
Denis Vlasenko834410a2006-11-29 12:00:28 +00002862 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002863 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864
Denis Vlasenko834410a2006-11-29 12:00:28 +00002865 for (j = 0; j < argc; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002866 disk_device = argv[j];
Denis Vlasenko834410a2006-11-29 12:00:28 +00002867 fd = open(disk_device, type_open);
2868 if (fd < 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002869 fdisk_fatal(unable_to_open);
2870 if (ioctl(fd, BLKGETSIZE, &size))
2871 fdisk_fatal(ioctl_error);
2872 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002873 if (argc == 1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002874 printf("%ld\n", size/2);
2875 else
2876 printf("%s: %ld\n", argv[j], size/2);
2877 }
2878 return 0;
2879 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002880#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881
Denis Vlasenko834410a2006-11-29 12:00:28 +00002882#if ENABLE_FEATURE_FDISK_WRITABLE
2883 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002884 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002885
Denis Vlasenko834410a2006-11-29 12:00:28 +00002886 disk_device = argv[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002887 get_boot(fdisk);
2888
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002889 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002890 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002891 printf("Detected an OSF/1 disklabel on %s, entering "
2892 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002893 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002894 /*Why do we do this? It seems to be counter-intuitive*/
2895 current_label_type = label_dos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896 /* If we return we may want to make an empty DOS label? */
2897 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002898
2899 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002900 int c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002901 putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002902 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002903 switch (c) {
2904 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002905 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906 toggle_active(get_partition(1, partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002907 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002908 toggle_sunflags(get_partition(1, partitions),
2909 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002910 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002911 sgi_set_bootpartition(
2912 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 else
2914 unknown_command(c);
2915 break;
2916 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002917 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002918 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002919 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002920 if (read_maybe_empty("Please enter the name of the "
2921 "new boot file: ") == '\n')
2922 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923 else
2924 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002925 }
2926#if ENABLE_FEATURE_OSF_LABEL
2927 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002928 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002929#endif
2930 break;
2931 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002932 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002933 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002934 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 toggle_sunflags(get_partition(1, partitions),
2936 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002937 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 sgi_set_swappartition(
2939 get_partition(1, partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 else
2941 unknown_command(c);
2942 break;
2943 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002944 {
Eric Andersen040f4402003-07-30 08:40:37 +00002945 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002946 /* If sgi_label then don't use get_existing_partition,
2947 let the user select a partition, since
2948 get_existing_partition() only works for Linux-like
2949 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002950 if (!LABEL_IS_SGI) {
Eric Andersen040f4402003-07-30 08:40:37 +00002951 j = get_existing_partition(1, partitions);
2952 } else {
2953 j = get_partition(1, partitions);
2954 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002955 if (j >= 0)
2956 delete_partition(j);
2957 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002958 break;
2959 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002960 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002961 create_sgiinfo();
2962 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002963 unknown_command(c);
2964 case 'l':
2965 list_types(get_sys_types());
2966 break;
2967 case 'm':
2968 menu();
2969 break;
2970 case 'n':
2971 new_partition();
2972 break;
2973 case 'o':
2974 create_doslabel();
2975 break;
2976 case 'p':
2977 list_table(0);
2978 break;
2979 case 'q':
2980 close(fd);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00002981 puts("");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 return 0;
2983 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002984#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002985 create_sunlabel();
2986#endif
2987 break;
2988 case 't':
2989 change_sysid();
2990 break;
2991 case 'u':
2992 change_units();
2993 break;
2994 case 'v':
2995 verify();
2996 break;
2997 case 'w':
2998 write_table(); /* does not return */
2999 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003000#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003001 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003002 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003003 printf("\n\tSorry, no experts menu for SGI "
3004 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003005 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003006 xselect();
3007 break;
3008#endif
3009 default:
3010 unknown_command(c);
3011 menu();
3012 }
3013 }
3014 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003015#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003016}