blob: b44a2b42573ffd242928c3a41f820a4147303f20 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Rob Landleyb73451d2006-02-24 16:29:00 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
9
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000010#ifndef _LARGEFILE64_SOURCE
11/* For lseek64 */
12#define _LARGEFILE64_SOURCE
13#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000014#include <assert.h> /* assert */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000016
Denis Vlasenko834410a2006-11-29 12:00:28 +000017/* Looks like someone forgot to add this to config system */
18#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
19# define ENABLE_FEATURE_FDISK_BLKSIZE 0
20# define USE_FEATURE_FDISK_BLKSIZE(a)
21#endif
22
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000023#define DEFAULT_SECTOR_SIZE 512
24#define DEFAULT_SECTOR_SIZE_STR "512"
25#define MAX_SECTOR_SIZE 2048
26#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
27#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000028
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000029#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000030
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +000031#define EXTENDED 0x05
32#define WIN98_EXTENDED 0x0f
33#define LINUX_PARTITION 0x81
34#define LINUX_SWAP 0x82
35#define LINUX_NATIVE 0x83
36#define LINUX_EXTENDED 0x85
37#define LINUX_LVM 0x8e
38#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000039
Denis Vlasenkoc033d512008-04-17 01:52:28 +000040
41enum {
42 OPT_b = 1 << 0,
43 OPT_C = 1 << 1,
44 OPT_H = 1 << 2,
45 OPT_l = 1 << 3,
46 OPT_S = 1 << 4,
47 OPT_u = 1 << 5,
48 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
49};
50
51
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +000052/* Used for sector numbers. Today's disk sizes make it necessary */
53typedef unsigned long long ullong;
54
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000055struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +000056 unsigned char heads;
57 unsigned char sectors;
58 unsigned short cylinders;
59 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000060};
61
Denis Vlasenko98ae2162006-10-12 19:30:44 +000062#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000063
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000064static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000065"Building a new %s. Changes will remain in memory only,\n"
66"until you decide to write them. After that the previous content\n"
67"won't be recoverable.\n\n";
68
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000069static const char msg_part_already_defined[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +000070"Partition %d is already defined, delete it before re-adding\n";
71
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000072
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000073struct partition {
74 unsigned char boot_ind; /* 0x80 - active */
75 unsigned char head; /* starting head */
76 unsigned char sector; /* starting sector */
77 unsigned char cyl; /* starting cylinder */
78 unsigned char sys_ind; /* What partition type */
79 unsigned char end_head; /* end head */
80 unsigned char end_sector; /* end sector */
81 unsigned char end_cyl; /* end cylinder */
82 unsigned char start4[4]; /* starting sector counting from 0 */
83 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000084} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000085
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000086static const char unable_to_open[] ALIGN1 = "cannot open %s";
87static const char unable_to_read[] ALIGN1 = "cannot read from %s";
88static const char unable_to_seek[] ALIGN1 = "cannot seek on %s";
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000089static void fdisk_fatal(const char *why) NORETURN;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000090
Denis Vlasenko98ae2162006-10-12 19:30:44 +000091enum label_type {
Denis Vlasenko4437d192008-04-17 00:12:10 +000092 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
Rob Landley5527b912006-02-25 03:46:10 +000093};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +000094
Denis Vlasenko4437d192008-04-17 00:12:10 +000095#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000096
Denis Vlasenko834410a2006-11-29 12:00:28 +000097#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +000098#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +000099#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000100#else
101#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000102#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000103#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000104
Denis Vlasenko834410a2006-11-29 12:00:28 +0000105#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000106#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000107#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000108#else
109#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000110#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000111#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000112
Denis Vlasenko834410a2006-11-29 12:00:28 +0000113#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000114#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000115#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000116#else
117#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000118#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000119#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000120
Denis Vlasenko834410a2006-11-29 12:00:28 +0000121#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000122#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000123#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000124#else
125#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000126#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000127#endif
Rob Landley5527b912006-02-25 03:46:10 +0000128
Denis Vlasenko4437d192008-04-17 00:12:10 +0000129enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000130
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000131static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000132#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);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000141static void get_geometry(void);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000142#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000143static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000144#else
145static int get_boot(void);
146#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000147
148#define PLURAL 0
149#define SINGULAR 1
150
Denis Vlasenko28703012006-12-19 20:32:02 +0000151static unsigned get_start_sect(const struct partition *p);
152static unsigned get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000153
154/*
155 * per partition table entry data
156 *
157 * The four primary partitions have the same sectorbuffer (MBRbuffer)
158 * and have NULL ext_pointer.
159 * Each logical partition table entry has two pointers, one for the
160 * partition and one link to the next one.
161 */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000162struct pte {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000163 struct partition *part_table; /* points into sectorbuffer */
164 struct partition *ext_pointer; /* points into sectorbuffer */
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000165 ullong offset; /* disk sector number */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000166 char *sectorbuffer; /* disk sector contents */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000167#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000168 char changed; /* boolean */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000169#endif
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000170};
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000171
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000172/* DOS partition types */
173
174static const char *const i386_sys_types[] = {
175 "\x00" "Empty",
176 "\x01" "FAT12",
177 "\x04" "FAT16 <32M",
178 "\x05" "Extended", /* DOS 3.3+ extended partition */
179 "\x06" "FAT16", /* DOS 16-bit >=32M */
180 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
181 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
182 "\x0b" "Win95 FAT32",
183 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
184 "\x0e" "Win95 FAT16 (LBA)",
185 "\x0f" "Win95 Ext'd (LBA)",
186 "\x11" "Hidden FAT12",
187 "\x12" "Compaq diagnostics",
188 "\x14" "Hidden FAT16 <32M",
189 "\x16" "Hidden FAT16",
190 "\x17" "Hidden HPFS/NTFS",
191 "\x1b" "Hidden Win95 FAT32",
192 "\x1c" "Hidden W95 FAT32 (LBA)",
193 "\x1e" "Hidden W95 FAT16 (LBA)",
194 "\x3c" "Part.Magic recovery",
195 "\x41" "PPC PReP Boot",
196 "\x42" "SFS",
197 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
198 "\x80" "Old Minix", /* Minix 1.4a and earlier */
199 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
200 "\x82" "Linux swap", /* also Solaris */
201 "\x83" "Linux",
202 "\x84" "OS/2 hidden C: drive",
203 "\x85" "Linux extended",
204 "\x86" "NTFS volume set",
205 "\x87" "NTFS volume set",
206 "\x8e" "Linux LVM",
207 "\x9f" "BSD/OS", /* BSDI */
208 "\xa0" "Thinkpad hibernation",
209 "\xa5" "FreeBSD", /* various BSD flavours */
210 "\xa6" "OpenBSD",
211 "\xa8" "Darwin UFS",
212 "\xa9" "NetBSD",
213 "\xab" "Darwin boot",
214 "\xb7" "BSDI fs",
215 "\xb8" "BSDI swap",
216 "\xbe" "Solaris boot",
217 "\xeb" "BeOS fs",
218 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
219 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
220 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
221 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
222 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
223 autodetect using persistent
224 superblock */
225#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
226 "\x02" "XENIX root",
227 "\x03" "XENIX usr",
228 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
229 "\x09" "AIX bootable", /* AIX data or Coherent */
230 "\x10" "OPUS",
231 "\x18" "AST SmartSleep",
232 "\x24" "NEC DOS",
233 "\x39" "Plan 9",
234 "\x40" "Venix 80286",
235 "\x4d" "QNX4.x",
236 "\x4e" "QNX4.x 2nd part",
237 "\x4f" "QNX4.x 3rd part",
238 "\x50" "OnTrack DM",
239 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
240 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
241 "\x53" "OnTrack DM6 Aux3",
242 "\x54" "OnTrackDM6",
243 "\x55" "EZ-Drive",
244 "\x56" "Golden Bow",
245 "\x5c" "Priam Edisk",
246 "\x61" "SpeedStor",
247 "\x64" "Novell Netware 286",
248 "\x65" "Novell Netware 386",
249 "\x70" "DiskSecure Multi-Boot",
250 "\x75" "PC/IX",
251 "\x93" "Amoeba",
252 "\x94" "Amoeba BBT", /* (bad block table) */
253 "\xa7" "NeXTSTEP",
254 "\xbb" "Boot Wizard hidden",
255 "\xc1" "DRDOS/sec (FAT-12)",
256 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
257 "\xc6" "DRDOS/sec (FAT-16)",
258 "\xc7" "Syrinx",
259 "\xda" "Non-FS data",
260 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
261 Concurrent DOS or CTOS */
262 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
263 "\xdf" "BootIt", /* BootIt EMBRM */
264 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
265 extended partition */
266 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
267 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
268 partition < 1024 cyl. */
269 "\xf1" "SpeedStor",
270 "\xf4" "SpeedStor", /* SpeedStor large partition */
271 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
272 "\xff" "BBT", /* Xenix Bad Block Table */
273#endif
274 NULL
275};
276
Denis Vlasenko4437d192008-04-17 00:12:10 +0000277enum {
278 dev_fd = 3 /* the disk */
279};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000280
281/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000282struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000283 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000284
285 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000286 int g_partitions; // = 4; /* maximum partition + 1 */
287 unsigned units_per_sector; // = 1;
288 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
289 unsigned user_set_sector_size;
290 unsigned sector_offset; // = 1;
291 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000292 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000293 smallint display_in_cyl_units; // = 1;
294#if ENABLE_FEATURE_OSF_LABEL
295 smallint possibly_osf_label;
296#endif
297
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000298 smallint listing; /* no aborts for fdisk -l */
299 smallint dos_compatible_flag; // = 1;
300#if ENABLE_FEATURE_FDISK_WRITABLE
301 //int dos_changed;
302 smallint nowarn; /* no warnings for fdisk -l/-s */
303#endif
304 int ext_index; /* the prime extended partition */
305 unsigned user_cylinders, user_heads, user_sectors;
306 unsigned pt_heads, pt_sectors;
307 unsigned kern_heads, kern_sectors;
308 ullong extended_offset; /* offset of link pointers */
309 ullong total_number_of_sectors;
310
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000311 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000312 char line_buffer[80];
313 char partname_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000314 /* Raw disk label. For DOS-type partition tables the MBR,
315 * with descriptions of the primary partitions. */
316 char MBRbuffer[MAX_SECTOR_SIZE];
317 /* Partition tables */
318 struct pte ptes[MAXIMUM_PARTS];
319};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000320#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000321#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000322#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000323#define g_partitions (G.g_partitions )
324#define units_per_sector (G.units_per_sector )
325#define sector_size (G.sector_size )
326#define user_set_sector_size (G.user_set_sector_size)
327#define sector_offset (G.sector_offset )
328#define g_heads (G.g_heads )
329#define g_sectors (G.g_sectors )
330#define g_cylinders (G.g_cylinders )
331#define current_label_type (G.current_label_type )
332#define display_in_cyl_units (G.display_in_cyl_units)
333#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000334#define listing (G.listing )
335#define dos_compatible_flag (G.dos_compatible_flag )
336#define nowarn (G.nowarn )
337#define ext_index (G.ext_index )
338#define user_cylinders (G.user_cylinders )
339#define user_heads (G.user_heads )
340#define user_sectors (G.user_sectors )
341#define pt_heads (G.pt_heads )
342#define pt_sectors (G.pt_sectors )
343#define kern_heads (G.kern_heads )
344#define kern_sectors (G.kern_sectors )
345#define extended_offset (G.extended_offset )
346#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000347#define listingbuf (G.listingbuf )
348#define line_buffer (G.line_buffer )
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000349#define partname_buffer (G.partname_buffer)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000350#define MBRbuffer (G.MBRbuffer )
351#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000352#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000353 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000354 sector_size = DEFAULT_SECTOR_SIZE; \
355 sector_offset = 1; \
356 g_partitions = 4; \
357 display_in_cyl_units = 1; \
358 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000359 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000360} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000361
Denis Vlasenkobd852072007-03-19 14:43:38 +0000362
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000363/* TODO: move to libbb? */
Denis Vlasenko4437d192008-04-17 00:12:10 +0000364static ullong bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000365{
366 uint64_t v64;
367 unsigned long longsectors;
368
369 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000370 /* Got bytes, convert to 512 byte sectors */
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000371 return (v64 >> 9);
372 }
373 /* Needs temp of type long */
374 if (ioctl(fd, BLKGETSIZE, &longsectors))
375 longsectors = 0;
376 return longsectors;
377}
378
379
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000380#define IS_EXTENDED(i) \
381 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
382
383#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
384
385#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
386
387#define pt_offset(b, n) \
388 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
389
390#define sector(s) ((s) & 0x3f)
391
392#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
393
394#define hsc2sector(h,s,c) \
395 (sector(s) - 1 + sectors * ((h) + heads * cylinder(s,c)))
396
397#define set_hsc(h,s,c,sector) \
398 do { \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000399 s = sector % g_sectors + 1; \
400 sector /= g_sectors; \
401 h = sector % g_heads; \
402 sector /= g_heads; \
403 c = sector & 0xff; \
404 s |= (sector >> 2) & 0xc0; \
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000405 } while (0)
406
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000407static void
408close_dev_fd(void)
409{
410 /* Not really closing, but making sure it is open, and to harmless place */
411 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
412}
413
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000414#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000415/* Read line; return 0 or first printable char */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000416static int
417read_line(const char *prompt)
418{
419 int sz;
420
421 sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
422 if (sz <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000423 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000424
425 if (line_buffer[sz-1] == '\n')
426 line_buffer[--sz] = '\0';
427
428 line_ptr = line_buffer;
429 while (*line_ptr && !isgraph(*line_ptr))
430 line_ptr++;
431 return *line_ptr;
432}
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000433#endif
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000434
Denis Vlasenkobd852072007-03-19 14:43:38 +0000435/*
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000436 * Return partition name - uses static storage
Denis Vlasenkobd852072007-03-19 14:43:38 +0000437 */
438static const char *
439partname(const char *dev, int pno, int lth)
440{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000441 const char *p;
442 int w, wp;
443 int bufsiz;
444 char *bufp;
445
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000446 bufp = partname_buffer;
447 bufsiz = sizeof(partname_buffer);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000448
449 w = strlen(dev);
450 p = "";
451
452 if (isdigit(dev[w-1]))
453 p = "p";
454
455 /* devfs kludge - note: fdisk partition names are not supposed
456 to equal kernel names, so there is no reason to do this */
457 if (strcmp(dev + w - 4, "disc") == 0) {
458 w -= 4;
459 p = "part";
460 }
461
462 wp = strlen(p);
463
464 if (lth) {
465 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
466 lth-wp-2, w, dev, p, pno);
467 } else {
468 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
469 }
470 return bufp;
471}
472
Denis Vlasenko834410a2006-11-29 12:00:28 +0000473#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000474static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000475set_all_unchanged(void)
476{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000477 int i;
478
479 for (i = 0; i < MAXIMUM_PARTS; i++)
480 ptes[i].changed = 0;
481}
482
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000483static ALWAYS_INLINE void
Rob Landleyb73451d2006-02-24 16:29:00 +0000484set_changed(int i)
485{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000486 ptes[i].changed = 1;
487}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000488#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000489
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000490static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000491get_part_table(int i)
492{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000493 return ptes[i].part_table;
494}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000495
496static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000497str_units(int n)
498{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000499 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000500 return display_in_cyl_units ? "cylinder" : "sector";
501 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000502}
503
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000504static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000505valid_part_table_flag(const char *mbuffer)
506{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000507 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000508}
509
Denis Vlasenko834410a2006-11-29 12:00:28 +0000510#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000511static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000512write_part_table_flag(char *b)
513{
514 b[510] = 0x55;
515 b[511] = 0xaa;
516}
517
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000518static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000519read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000520{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000521 while (!read_line(mesg)) /* repeat */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000522 return *line_ptr;
523}
524
525static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000526read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000527{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000528 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000529 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000530 line_ptr[0] = '\n';
531 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000532 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000533 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000534}
535
536static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000537read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000538{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000539 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000540 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000541 read_nonempty("Hex code (type L to list codes): ");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000542 if (*line_ptr == 'l' || *line_ptr == 'L') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000543 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000544 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000545 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000546 v = bb_strtoul(line_ptr, NULL, 16);
Denis Vlasenko28703012006-12-19 20:32:02 +0000547 if (v > 0xff)
548 /* Bad input also triggers this */
549 continue;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000550 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000551 }
552}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000553#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000554
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000555#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000556
557typedef struct {
558 unsigned char info[128]; /* Informative text string */
559 unsigned char spare0[14];
560 struct sun_info {
561 unsigned char spare1;
562 unsigned char id;
563 unsigned char spare2;
564 unsigned char flags;
565 } infos[8];
566 unsigned char spare1[246]; /* Boot information etc. */
567 unsigned short rspeed; /* Disk rotational speed */
568 unsigned short pcylcount; /* Physical cylinder count */
569 unsigned short sparecyl; /* extra sects per cylinder */
570 unsigned char spare2[4]; /* More magic... */
571 unsigned short ilfact; /* Interleave factor */
572 unsigned short ncyl; /* Data cylinder count */
573 unsigned short nacyl; /* Alt. cylinder count */
574 unsigned short ntrks; /* Tracks per cylinder */
575 unsigned short nsect; /* Sectors per track */
576 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000577 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000578 uint32_t start_cylinder;
579 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000580 } partitions[8];
581 unsigned short magic; /* Magic number */
582 unsigned short csum; /* Label xor'd checksum */
583} sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000584#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000585STATIC_OSF void bsd_select(void);
586STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000587#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000588
Denis Vlasenko28703012006-12-19 20:32:02 +0000589#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000590static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000591fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000592{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000593 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000594}
595
Rob Landley88621d72006-08-29 19:41:06 +0000596static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000597fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000598{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000599 return (x << 24) |
600 ((x & 0xFF00) << 8) |
601 ((x & 0xFF0000) >> 8) |
602 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603}
604#endif
605
Denis Vlasenkobd852072007-03-19 14:43:38 +0000606STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000607STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000608STATIC_SGI int sgi_get_sysid(int i);
609STATIC_SGI void sgi_delete_partition(int i);
610STATIC_SGI void sgi_change_sysid(int i, int sys);
611STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000612#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000613STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000614#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000615STATIC_SGI int verify_sgi(int verbose);
616STATIC_SGI void sgi_add_partition(int n, int sys);
617STATIC_SGI void sgi_set_swappartition(int i);
618STATIC_SGI const char *sgi_get_bootfile(void);
619STATIC_SGI void sgi_set_bootfile(const char* aFile);
620STATIC_SGI void create_sgiinfo(void);
621STATIC_SGI void sgi_write_table(void);
622STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000623#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000624
Denis Vlasenkobd852072007-03-19 14:43:38 +0000625STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000626STATIC_SUN void sun_delete_partition(int i);
627STATIC_SUN void sun_change_sysid(int i, int sys);
628STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000629STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000630#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000631STATIC_SUN void sun_set_alt_cyl(void);
632STATIC_SUN void sun_set_ncyl(int cyl);
633STATIC_SUN void sun_set_xcyl(void);
634STATIC_SUN void sun_set_ilfact(void);
635STATIC_SUN void sun_set_rspeed(void);
636STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000637#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000638STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
639STATIC_SUN void verify_sun(void);
640STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000641#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000642
Denis Vlasenko834410a2006-11-29 12:00:28 +0000643#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000644/* start_sect and nr_sects are stored little endian on all machines */
645/* moreover, they are not aligned correctly */
646static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000647store4_little_endian(unsigned char *cp, unsigned val)
Rob Landleyb73451d2006-02-24 16:29:00 +0000648{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000649 cp[0] = val;
650 cp[1] = val >> 8;
651 cp[2] = val >> 16;
652 cp[3] = val >> 24;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000653}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000654#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000655
Denis Vlasenko834410a2006-11-29 12:00:28 +0000656static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000657read4_little_endian(const unsigned char *cp)
658{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000659 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000660}
661
Denis Vlasenko834410a2006-11-29 12:00:28 +0000662#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000663static void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000664set_start_sect(struct partition *p, unsigned start_sect)
Rob Landleyb73451d2006-02-24 16:29:00 +0000665{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000666 store4_little_endian(p->start4, start_sect);
667}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000668#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000669
Denis Vlasenko28703012006-12-19 20:32:02 +0000670static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000671get_start_sect(const struct partition *p)
672{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000673 return read4_little_endian(p->start4);
674}
675
Denis Vlasenko834410a2006-11-29 12:00:28 +0000676#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000677static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000678set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000679{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000680 store4_little_endian(p->size4, nr_sects);
681}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000682#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000683
Denis Vlasenko28703012006-12-19 20:32:02 +0000684static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000685get_nr_sects(const struct partition *p)
686{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 return read4_little_endian(p->size4);
688}
689
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000690static void fdisk_fatal(const char *why)
Rob Landleyb73451d2006-02-24 16:29:00 +0000691{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692 if (listing) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000693 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000694 longjmp(listingbuf, 1);
695 }
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000696 bb_error_msg_and_die(why, disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000697}
698
699static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000700seek_sector(ullong secno)
Rob Landleyb73451d2006-02-24 16:29:00 +0000701{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000702 secno *= sector_size;
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000703#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
Denis Vlasenko4437d192008-04-17 00:12:10 +0000704 if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000705 fdisk_fatal(unable_to_seek);
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000706#else
707 if (secno > MAXINT(off_t)
Denis Vlasenko4437d192008-04-17 00:12:10 +0000708 || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
Denis Vlasenko06b3cc22007-09-23 14:05:54 +0000709 ) {
710 fdisk_fatal(unable_to_seek);
711 }
712#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000713}
714
Denis Vlasenko834410a2006-11-29 12:00:28 +0000715#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000716static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000717write_sector(ullong secno, char *buf)
Rob Landleyb73451d2006-02-24 16:29:00 +0000718{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000719 seek_sector(secno);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000720 xwrite(dev_fd, buf, sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000721}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000722#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000723
724/* Allocate a buffer and read a partition table sector */
725static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000726read_pte(struct pte *pe, ullong offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000727{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000728 pe->offset = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000729 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000730 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000731 /* xread would make us abort - bad for fdisk -l */
732 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000733 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000734#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000735 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000736#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000737 pe->part_table = pe->ext_pointer = NULL;
738}
739
Denis Vlasenko834410a2006-11-29 12:00:28 +0000740static unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000741get_partition_start(const struct pte *pe)
742{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000743 return pe->offset + get_start_sect(pe->part_table);
744}
745
Denis Vlasenko834410a2006-11-29 12:00:28 +0000746#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000747/*
748 * Avoid warning about DOS partitions when no DOS partition was changed.
749 * Here a heuristic "is probably dos partition".
750 * We might also do the opposite and warn in all cases except
751 * for "is probably nondos partition".
752 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000753#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000754static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000755is_dos_partition(int t)
756{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000757 return (t == 1 || t == 4 || t == 6 ||
758 t == 0x0b || t == 0x0c || t == 0x0e ||
759 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
760 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
761 t == 0xc1 || t == 0xc4 || t == 0xc6);
762}
Denis Vlasenko89398812008-01-25 20:18:46 +0000763#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000764
765static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000766menu(void)
767{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000768 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000769 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000770 puts("a\ttoggle a read only flag"); /* sun */
771 puts("b\tedit bsd disklabel");
772 puts("c\ttoggle the mountable flag"); /* sun */
773 puts("d\tdelete a partition");
774 puts("l\tlist known partition types");
775 puts("n\tadd a new partition");
776 puts("o\tcreate a new empty DOS partition table");
777 puts("p\tprint the partition table");
778 puts("q\tquit without saving changes");
779 puts("s\tcreate a new empty Sun disklabel"); /* sun */
780 puts("t\tchange a partition's system id");
781 puts("u\tchange display/entry units");
782 puts("v\tverify the partition table");
783 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000784#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000785 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000786#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000787 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000788 puts("a\tselect bootable partition"); /* sgi flavour */
789 puts("b\tedit bootfile entry"); /* sgi */
790 puts("c\tselect sgi swap partition"); /* sgi flavour */
791 puts("d\tdelete a partition");
792 puts("l\tlist known partition types");
793 puts("n\tadd a new partition");
794 puts("o\tcreate a new empty DOS partition table");
795 puts("p\tprint the partition table");
796 puts("q\tquit without saving changes");
797 puts("s\tcreate a new empty Sun disklabel"); /* sun */
798 puts("t\tchange a partition's system id");
799 puts("u\tchange display/entry units");
800 puts("v\tverify the partition table");
801 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000802 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000803 puts("o\tcreate a new empty DOS partition table");
804 puts("q\tquit without saving changes");
805 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000806 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000807 puts("a\ttoggle a bootable flag");
808 puts("b\tedit bsd disklabel");
809 puts("c\ttoggle the dos compatibility flag");
810 puts("d\tdelete a partition");
811 puts("l\tlist known partition types");
812 puts("n\tadd a new partition");
813 puts("o\tcreate a new empty DOS partition table");
814 puts("p\tprint the partition table");
815 puts("q\tquit without saving changes");
816 puts("s\tcreate a new empty Sun disklabel"); /* sun */
817 puts("t\tchange a partition's system id");
818 puts("u\tchange display/entry units");
819 puts("v\tverify the partition table");
820 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000821#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000822 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000823#endif
824 }
825}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000826#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000827
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000828
Denis Vlasenko834410a2006-11-29 12:00:28 +0000829#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000830static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000831xmenu(void)
832{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000833 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000834 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000835 puts("a\tchange number of alternate cylinders"); /*sun*/
836 puts("c\tchange number of cylinders");
837 puts("d\tprint the raw data in the partition table");
838 puts("e\tchange number of extra sectors per cylinder");/*sun*/
839 puts("h\tchange number of heads");
840 puts("i\tchange interleave factor"); /*sun*/
841 puts("o\tchange rotation speed (rpm)"); /*sun*/
842 puts("p\tprint the partition table");
843 puts("q\tquit without saving changes");
844 puts("r\treturn to main menu");
845 puts("s\tchange number of sectors/track");
846 puts("v\tverify the partition table");
847 puts("w\twrite table to disk and exit");
848 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000849 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000850 puts("b\tmove beginning of data in a partition"); /* !sun */
851 puts("c\tchange number of cylinders");
852 puts("d\tprint the raw data in the partition table");
853 puts("e\tlist extended partitions"); /* !sun */
854 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
855 puts("h\tchange number of heads");
856 puts("p\tprint the partition table");
857 puts("q\tquit without saving changes");
858 puts("r\treturn to main menu");
859 puts("s\tchange number of sectors/track");
860 puts("v\tverify the partition table");
861 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000862 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000863 puts("b\tmove beginning of data in a partition"); /* !sun */
864 puts("c\tchange number of cylinders");
865 puts("d\tprint the raw data in the partition table");
866 puts("e\tlist extended partitions"); /* !sun */
867 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
868 puts("h\tchange number of heads");
869 puts("p\tprint the partition table");
870 puts("q\tquit without saving changes");
871 puts("r\treturn to main menu");
872 puts("s\tchange number of sectors/track");
873 puts("v\tverify the partition table");
874 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000875 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000876 puts("b\tmove beginning of data in a partition"); /* !sun */
877 puts("c\tchange number of cylinders");
878 puts("d\tprint the raw data in the partition table");
879 puts("e\tlist extended partitions"); /* !sun */
880 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +0000881#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +0000882 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +0000884 puts("h\tchange number of heads");
885 puts("p\tprint the partition table");
886 puts("q\tquit without saving changes");
887 puts("r\treturn to main menu");
888 puts("s\tchange number of sectors/track");
889 puts("v\tverify the partition table");
890 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000891 }
892}
893#endif /* ADVANCED mode */
894
Denis Vlasenko834410a2006-11-29 12:00:28 +0000895#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +0000896static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +0000897get_sys_types(void)
898{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000899 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000900 LABEL_IS_SUN ? sun_sys_types :
901 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902 i386_sys_types);
903}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000904#else
905#define get_sys_types() i386_sys_types
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000906#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000907
Denis Vlasenkobd852072007-03-19 14:43:38 +0000908static const char *
909partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000910{
911 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +0000912 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000913
Denis Vlasenkobd852072007-03-19 14:43:38 +0000914 for (i = 0; types[i]; i++)
915 if ((unsigned char)types[i][0] == type)
916 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000917
Denis Vlasenkobd852072007-03-19 14:43:38 +0000918 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000919}
920
921
Denis Vlasenko834410a2006-11-29 12:00:28 +0000922#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000923static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000924get_sysid(int i)
925{
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000926 return LABEL_IS_SUN ? sunlabel->infos[i].id :
927 (LABEL_IS_SGI ? sgi_get_sysid(i) :
928 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000929}
930
Denis Vlasenkobd852072007-03-19 14:43:38 +0000931static void
932list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000933{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000934 enum { COLS = 3 };
935
936 unsigned last[COLS];
937 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000938 int i;
939
Denis Vlasenkobd852072007-03-19 14:43:38 +0000940 for (size = 0; sys[size]; size++) /* */;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000941
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000942 done = 0;
943 for (i = COLS-1; i >= 0; i--) {
944 done += (size + i - done) / (i + 1);
945 last[COLS-1 - i] = done;
946 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000947
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000948 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000949 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000950 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +0000951 (unsigned char)sys[next][0],
952 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000953 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000954 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000955 i = 0;
956 next = ++done;
957 }
958 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000959 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000960}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000961#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000962
963static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000964is_cleared_partition(const struct partition *p)
965{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000966 return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
967 p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
968 get_start_sect(p) || get_nr_sects(p));
969}
970
971static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000972clear_partition(struct partition *p)
973{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000974 if (!p)
975 return;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000976 memset(p, 0, sizeof(struct partition));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000977}
978
Denis Vlasenko834410a2006-11-29 12:00:28 +0000979#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000980static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000981set_partition(int i, int doext, ullong start, ullong stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +0000982{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000983 struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000984 ullong offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000985
986 if (doext) {
987 p = ptes[i].ext_pointer;
988 offset = extended_offset;
989 } else {
990 p = ptes[i].part_table;
991 offset = ptes[i].offset;
992 }
993 p->boot_ind = 0;
994 p->sys_ind = sysid;
995 set_start_sect(p, start - offset);
996 set_nr_sects(p, stop - start + 1);
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000997 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
998 start = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000999 set_hsc(p->head, p->sector, p->cyl, start);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001000 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1001 stop = g_heads * g_sectors * 1024 - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001002 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1003 ptes[i].changed = 1;
1004}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001005#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001006
1007static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001008warn_geometry(void)
1009{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001010 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001011 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001012
Denis Vlasenkobd852072007-03-19 14:43:38 +00001013 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001014 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001015 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001016 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001017 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001018 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001019 printf(" cylinders");
1020 printf(
Denis Vlasenko834410a2006-11-29 12:00:28 +00001021#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001022 " (settable in the extra functions menu)"
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001023#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001024 "\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001025 return 1;
1026}
1027
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001028static void
1029update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001030{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001031 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001032
1033 if (display_in_cyl_units && cyl_units)
1034 units_per_sector = cyl_units;
1035 else
1036 units_per_sector = 1; /* in sectors */
1037}
1038
Denis Vlasenko834410a2006-11-29 12:00:28 +00001039#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001040static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001041warn_cylinders(void)
1042{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001043 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001044 printf("\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045"The number of cylinders for this disk is set to %d.\n"
1046"There is nothing wrong with that, but this is larger than 1024,\n"
1047"and could in certain setups cause problems with:\n"
1048"1) software that runs at boot time (e.g., old versions of LILO)\n"
1049"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001050" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001051 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001052}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001053#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054
1055static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001056read_extended(int ext)
1057{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001058 int i;
1059 struct pte *pex;
1060 struct partition *p, *q;
1061
1062 ext_index = ext;
1063 pex = &ptes[ext];
1064 pex->ext_pointer = pex->part_table;
1065
1066 p = pex->part_table;
1067 if (!get_start_sect(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001068 printf("Bad offset in primary extended partition\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001069 return;
1070 }
1071
Rob Landleyb73451d2006-02-24 16:29:00 +00001072 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001073 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001074
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001075 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001076 /* This is not a Linux restriction, but
1077 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001078 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001079 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001080#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001081 printf("Warning: deleting partitions after %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001082 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001083 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001084#endif
1085 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001086 return;
1087 }
1088
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001089 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090
1091 if (!extended_offset)
1092 extended_offset = get_start_sect(p);
1093
1094 q = p = pt_offset(pe->sectorbuffer, 0);
1095 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001096 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001097 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001098 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001099 "pointer in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001100 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001101 else
1102 pe->ext_pointer = p;
1103 } else if (p->sys_ind) {
1104 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001105 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001106 "data in partition table"
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001107 " %d\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001108 else
1109 pe->part_table = p;
1110 }
1111 }
1112
1113 /* very strange code here... */
1114 if (!pe->part_table) {
1115 if (q != pe->ext_pointer)
1116 pe->part_table = q;
1117 else
1118 pe->part_table = q + 1;
1119 }
1120 if (!pe->ext_pointer) {
1121 if (q != pe->part_table)
1122 pe->ext_pointer = q;
1123 else
1124 pe->ext_pointer = q + 1;
1125 }
1126
1127 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001128 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001129 }
1130
Denis Vlasenko834410a2006-11-29 12:00:28 +00001131#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001132 /* remove empty links */
1133 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001134 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001135 struct pte *pe = &ptes[i];
1136
Denis Vlasenkobd852072007-03-19 14:43:38 +00001137 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001138 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001139 ) {
1140 printf("Omitting empty partition (%d)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001141 delete_partition(i);
1142 goto remove; /* numbering changed */
1143 }
1144 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001145#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001146}
1147
Denis Vlasenko834410a2006-11-29 12:00:28 +00001148#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001149static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001150create_doslabel(void)
1151{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001152 int i;
1153
Denis Vlasenkobd852072007-03-19 14:43:38 +00001154 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001155
Denis Vlasenko4437d192008-04-17 00:12:10 +00001156 current_label_type = LABEL_DOS;
Rob Landley5527b912006-02-25 03:46:10 +00001157
Denis Vlasenko834410a2006-11-29 12:00:28 +00001158#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001159 possibly_osf_label = 0;
1160#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001161 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001162
1163 for (i = 510-64; i < 510; i++)
1164 MBRbuffer[i] = 0;
1165 write_part_table_flag(MBRbuffer);
1166 extended_offset = 0;
1167 set_all_unchanged();
1168 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001169 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001170}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001171#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172
1173static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001174get_sectorsize(void)
1175{
Rob Landley736e5252006-02-25 03:36:00 +00001176 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001177 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001178 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001179 sector_size = arg;
1180 if (sector_size != DEFAULT_SECTOR_SIZE)
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001181 printf("Note: sector size is %d "
1182 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1183 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001184 }
1185}
1186
Rob Landley88621d72006-08-29 19:41:06 +00001187static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001188get_kernel_geometry(void)
1189{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001190 struct hd_geometry geometry;
1191
Denis Vlasenko4437d192008-04-17 00:12:10 +00001192 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001193 kern_heads = geometry.heads;
1194 kern_sectors = geometry.sectors;
1195 /* never use geometry.cylinders - it is truncated */
1196 }
1197}
1198
1199static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001200get_partition_table_geometry(void)
1201{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001202 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001203 struct partition *p;
1204 int i, h, s, hh, ss;
1205 int first = 1;
1206 int bad = 0;
1207
Eric Andersen3496fdc2006-01-30 23:09:20 +00001208 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001209 return;
1210
1211 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001212 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213 p = pt_offset(bufp, i);
1214 if (p->sys_ind != 0) {
1215 h = p->end_head + 1;
1216 s = (p->end_sector & 077);
1217 if (first) {
1218 hh = h;
1219 ss = s;
1220 first = 0;
1221 } else if (hh != h || ss != s)
1222 bad = 1;
1223 }
1224 }
1225
1226 if (!first && !bad) {
1227 pt_heads = hh;
1228 pt_sectors = ss;
1229 }
1230}
1231
Rob Landleyb73451d2006-02-24 16:29:00 +00001232static void
1233get_geometry(void)
1234{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001235 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236
1237 get_sectorsize();
1238 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001239#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001240 guess_device_type();
1241#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001242 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 kern_heads = kern_sectors = 0;
1244 pt_heads = pt_sectors = 0;
1245
1246 get_kernel_geometry();
1247 get_partition_table_geometry();
1248
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001249 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250 pt_heads ? pt_heads :
1251 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001252 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001253 pt_sectors ? pt_sectors :
1254 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001255 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001256
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 sector_offset = 1;
1258 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001259 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001260
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001261 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1262 if (!g_cylinders)
1263 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001264}
1265
1266/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001267 * Opens disk_device and optionally reads MBR.
1268 * FIXME: document what each 'what' value will do!
1269 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001270 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1271 * 0: found or created label
1272 * 1: I/O error
1273 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001274#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1275static int get_boot(enum action what)
1276#else
1277static int get_boot(void)
1278#define get_boot(what) get_boot()
1279#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001280{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001281 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001282
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001283 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001284 for (i = 0; i < 4; i++) {
1285 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001286 pe->part_table = pt_offset(MBRbuffer, i);
1287 pe->ext_pointer = NULL;
1288 pe->offset = 0;
1289 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001290#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001291 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001292#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 }
1294
Denis Vlasenko834410a2006-11-29 12:00:28 +00001295#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001296// ALERT! highly idiotic design!
1297// We end up here when we call get_boot() recursively
1298// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1299// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1300// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1301// So skip opening device _again_...
1302 if (what == CREATE_EMPTY_DOS USE_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
1303 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001304
Denis Vlasenko4437d192008-04-17 00:12:10 +00001305 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1306
Denis Vlasenkobd852072007-03-19 14:43:38 +00001307 if (fd < 0) {
1308 fd = open(disk_device, O_RDONLY);
1309 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001310 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001311 return 1;
1312 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001313 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001314 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001315 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001316 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001317 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001318 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001319 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001321 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 fdisk_fatal(unable_to_read);
1323 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001324#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001325 fd = open(disk_device, O_RDONLY);
1326 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001327 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001328 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001329 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001330 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001331 }
1332 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001333#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001334
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001335 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001336 update_units();
1337
Denis Vlasenko834410a2006-11-29 12:00:28 +00001338#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001339 if (check_sun_label())
1340 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001341#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001342#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001343 if (check_sgi_label())
1344 return 0;
1345#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001346#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347 if (check_aix_label())
1348 return 0;
1349#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001350#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001351 if (check_osf_label()) {
1352 possibly_osf_label = 1;
1353 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001354 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001355 return 0;
1356 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001357 printf("This disk has both DOS and BSD magic.\n"
1358 "Give the 'b' command to go to BSD mode.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 }
1360#endif
1361
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001362#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001363 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001364 return -1;
1365#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001366 if (!valid_part_table_flag(MBRbuffer)) {
1367 if (what == OPEN_MAIN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001368 printf("Device contains neither a valid DOS "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 "partition table, nor Sun, SGI or OSF "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001370 "disklabel\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001371#ifdef __sparc__
Denis Vlasenko4437d192008-04-17 00:12:10 +00001372 USE_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001373#else
1374 create_doslabel();
1375#endif
1376 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001377 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001378 /* TRY_ONLY: */
1379 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001380 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001381 created_table:
1382#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001383
Denis Vlasenko4437d192008-04-17 00:12:10 +00001384
1385 USE_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001386 warn_geometry();
1387
1388 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001389 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001390 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001391 printf("Ignoring extra extended "
1392 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001393 else
1394 read_extended(i);
1395 }
1396 }
1397
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001398 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001401 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1402 "table %d will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001403 pe->sectorbuffer[510],
1404 pe->sectorbuffer[511],
1405 i + 1);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001406 USE_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 }
1408 }
1409
1410 return 0;
1411}
1412
Denis Vlasenko834410a2006-11-29 12:00:28 +00001413#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414/*
1415 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1416 * If the user hits Enter, DFLT is returned.
1417 * Answers like +10 are interpreted as offsets from BASE.
1418 *
1419 * There is no default if DFLT is not between LOW and HIGH.
1420 */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001421static unsigned
Denis Vlasenko06c0a712007-01-29 22:51:44 +00001422read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001423{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001424 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001425 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001426 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001427
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001428 if (dflt < low || dflt > high) {
1429 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001430 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001431 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001432
1433 while (1) {
1434 int use_default = default_ok;
1435
1436 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001437 do {
1438 printf(fmt, mesg, low, high, dflt);
1439 read_maybe_empty("");
1440 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1441 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001442
Eric Andersen84bdea82004-05-19 10:49:17 +00001443 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001444 int minus = (*line_ptr == '-');
1445 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001446
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001447 i = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001448
Rob Landleyb73451d2006-02-24 16:29:00 +00001449 while (isdigit(*++line_ptr))
1450 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001451
Rob Landleyb73451d2006-02-24 16:29:00 +00001452 switch (*line_ptr) {
1453 case 'c':
1454 case 'C':
1455 if (!display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001456 i *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001457 break;
1458 case 'K':
1459 absolute = 1024;
1460 break;
1461 case 'k':
1462 absolute = 1000;
1463 break;
1464 case 'm':
1465 case 'M':
1466 absolute = 1000000;
1467 break;
1468 case 'g':
1469 case 'G':
1470 absolute = 1000000000;
1471 break;
1472 default:
1473 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001474 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001475 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001476 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001477 unsigned long unit;
1478
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001479 bytes = (ullong) i * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001480 unit = sector_size * units_per_sector;
1481 bytes += unit/2; /* round */
1482 bytes /= unit;
1483 i = bytes;
1484 }
1485 if (minus)
1486 i = -i;
1487 i += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001488 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 i = atoi(line_ptr);
1490 while (isdigit(*line_ptr)) {
1491 line_ptr++;
1492 use_default = 0;
1493 }
1494 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001495 if (use_default) {
1496 i = dflt;
1497 printf("Using default value %u\n", i);
1498 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001499 if (i >= low && i <= high)
1500 break;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001501 printf("Value is out of range\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001502 }
1503 return i;
1504}
1505
Rob Landleyb73451d2006-02-24 16:29:00 +00001506static int
1507get_partition(int warn, int max)
1508{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001509 struct pte *pe;
1510 int i;
1511
Denis Vlasenkobd852072007-03-19 14:43:38 +00001512 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001513 pe = &ptes[i];
1514
1515 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001516 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1517 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1518 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1519 ) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001520 printf("Warning: partition %d has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001521 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001522 }
1523 return i;
1524}
1525
1526static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001527get_existing_partition(int warn, int max)
1528{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001529 int pno = -1;
1530 int i;
1531
1532 for (i = 0; i < max; i++) {
1533 struct pte *pe = &ptes[i];
1534 struct partition *p = pe->part_table;
1535
1536 if (p && !is_cleared_partition(p)) {
1537 if (pno >= 0)
1538 goto not_unique;
1539 pno = i;
1540 }
1541 }
1542 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001543 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544 return pno;
1545 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001546 printf("No partition is defined yet!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001547 return -1;
1548
1549 not_unique:
1550 return get_partition(warn, max);
1551}
1552
1553static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001554get_nonexisting_partition(int warn, int max)
1555{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001556 int pno = -1;
1557 int i;
1558
1559 for (i = 0; i < max; i++) {
1560 struct pte *pe = &ptes[i];
1561 struct partition *p = pe->part_table;
1562
1563 if (p && is_cleared_partition(p)) {
1564 if (pno >= 0)
1565 goto not_unique;
1566 pno = i;
1567 }
1568 }
1569 if (pno >= 0) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001570 printf("Selected partition %d\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571 return pno;
1572 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001573 printf("All primary partitions have been defined already!\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001574 return -1;
1575
1576 not_unique:
1577 return get_partition(warn, max);
1578}
1579
1580
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001581static void
1582change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001583{
1584 display_in_cyl_units = !display_in_cyl_units;
1585 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001586 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001587 str_units(PLURAL));
1588}
1589
1590static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001591toggle_active(int i)
1592{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001593 struct pte *pe = &ptes[i];
1594 struct partition *p = pe->part_table;
1595
Rob Landleyb73451d2006-02-24 16:29:00 +00001596 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001597 printf("WARNING: Partition %d is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1599 pe->changed = 1;
1600}
1601
1602static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001603toggle_dos_compatibility_flag(void)
1604{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001605 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001606 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001607 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001608 printf("DOS Compatibility flag is set\n");
1609 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001610 sector_offset = 1;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001611 printf("DOS Compatibility flag is not set\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001612 }
1613}
1614
1615static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001616delete_partition(int i)
1617{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001618 struct pte *pe = &ptes[i];
1619 struct partition *p = pe->part_table;
1620 struct partition *q = pe->ext_pointer;
1621
1622/* Note that for the fifth partition (i == 4) we don't actually
1623 * decrement partitions.
1624 */
1625
1626 if (warn_geometry())
1627 return; /* C/H/S not set */
1628 pe->changed = 1;
1629
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001630 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001631 sun_delete_partition(i);
1632 return;
1633 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001634 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001635 sgi_delete_partition(i);
1636 return;
1637 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001638
1639 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001640 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001641 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001642 ptes[ext_index].ext_pointer = NULL;
1643 extended_offset = 0;
1644 }
1645 clear_partition(p);
1646 return;
1647 }
1648
1649 if (!q->sys_ind && i > 4) {
1650 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001651 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001652 --i;
1653 clear_partition(ptes[i].ext_pointer);
1654 ptes[i].changed = 1;
1655 } else {
1656 /* not the last one - further ones will be moved down */
1657 if (i > 4) {
1658 /* delete this link in the chain */
1659 p = ptes[i-1].ext_pointer;
1660 *p = *q;
1661 set_start_sect(p, get_start_sect(q));
1662 set_nr_sects(p, get_nr_sects(q));
1663 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001664 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001665 /* the first logical in a longer chain */
1666 pe = &ptes[5];
1667
1668 if (pe->part_table) /* prevent SEGFAULT */
1669 set_start_sect(pe->part_table,
Rob Landleyb73451d2006-02-24 16:29:00 +00001670 get_partition_start(pe) -
1671 extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001672 pe->offset = extended_offset;
1673 pe->changed = 1;
1674 }
1675
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001676 if (g_partitions > 5) {
1677 g_partitions--;
1678 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001679 ptes[i] = ptes[i+1];
1680 i++;
1681 }
1682 } else
1683 /* the only logical: clear only */
1684 clear_partition(ptes[i].part_table);
1685 }
1686}
1687
1688static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001689change_sysid(void)
1690{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001691 int i, sys, origsys;
1692 struct partition *p;
1693
Eric Andersen040f4402003-07-30 08:40:37 +00001694 /* If sgi_label then don't use get_existing_partition,
1695 let the user select a partition, since get_existing_partition()
1696 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001697 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001698 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001699 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001700 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001701 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001702 if (i == -1)
1703 return;
1704 p = ptes[i].part_table;
1705 origsys = sys = get_sysid(i);
1706
1707 /* if changing types T to 0 is allowed, then
1708 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001709 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001710 printf("Partition %d does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001711 return;
1712 }
1713 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001714 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001715
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001716 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001717 printf("Type 0 means free space to many systems\n"
Rob Landleyb73451d2006-02-24 16:29:00 +00001718 "(but not to Linux). Having partitions of\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001719 "type 0 is probably unwise.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001720 /* break; */
1721 }
1722
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001723 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001724 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001725 printf("You cannot change a partition into"
1726 " an extended one or vice versa\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001727 break;
1728 }
1729 }
1730
1731 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001732#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001733 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001734 printf("Consider leaving partition 3 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001735 "as Whole disk (5),\n"
1736 "as SunOS/Solaris expects it and "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001737 "even Linux likes it\n\n");
1738#endif
1739#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001740 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001741 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001742 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001743 (i == 8 && sys != 0)
1744 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001745 ) {
1746 printf("Consider leaving partition 9 "
Rob Landleyb73451d2006-02-24 16:29:00 +00001747 "as volume header (0),\nand "
1748 "partition 11 as entire volume (6)"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001749 "as IRIX expects it\n\n");
Rob Landley5527b912006-02-25 03:46:10 +00001750 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001751#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001752 if (sys == origsys)
1753 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001754 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001755 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001756 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757 sgi_change_sysid(i, sys);
1758 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001759 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001760
Denis Vlasenkobd852072007-03-19 14:43:38 +00001761 printf("Changed system type of partition %d "
1762 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001763 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001764 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001765 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1766 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001767 break;
1768 }
1769 }
1770}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001771#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001772
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001773
Denis Vlasenko28703012006-12-19 20:32:02 +00001774/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001775 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1776 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1777 * Lubkin Oct. 1991). */
1778
Rob Landleyb73451d2006-02-24 16:29:00 +00001779static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001780linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001781{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001782 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001783
1784 *c = ls / spc;
1785 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001786 *h = ls / g_sectors;
1787 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001788}
1789
Rob Landleyb73451d2006-02-24 16:29:00 +00001790static void
1791check_consistency(const struct partition *p, int partition)
1792{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001793 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1794 unsigned pec, peh, pes; /* physical ending c, h, s */
1795 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1796 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001797
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001798 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001799 return; /* do not check extended partitions */
1800
1801/* physical beginning c, h, s */
1802 pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300);
1803 pbh = p->head;
1804 pbs = p->sector & 0x3f;
1805
1806/* physical ending c, h, s */
1807 pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300);
1808 peh = p->end_head;
1809 pes = p->end_sector & 0x3f;
1810
1811/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001812 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813
1814/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001815 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001816
1817/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001818 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001819 printf("Partition %d has different physical/logical "
1820 "beginnings (non-Linux?):\n", partition + 1);
1821 printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs);
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001822 printf("logical=(%d, %d, %d)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001823 }
1824
1825/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001826 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001827 printf("Partition %d has different physical/logical "
1828 "endings:\n", partition + 1);
1829 printf(" phys=(%d, %d, %d) ", pec, peh, pes);
1830 printf("logical=(%d, %d, %d)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001831 }
1832
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001834 if (peh != (g_heads - 1) || pes != g_sectors) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001835 printf("Partition %i does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001836 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001837 }
1838}
1839
1840static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001841list_disk_geometry(void)
1842{
Eric Andersen040f4402003-07-30 08:40:37 +00001843 long long bytes = (total_number_of_sectors << 9);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001844 long megabytes = bytes/1000000;
1845
1846 if (megabytes < 10000)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001847 printf("\nDisk %s: %ld MB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001848 disk_device, megabytes, bytes);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001849 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001850 printf("\nDisk %s: %ld.%ld GB, %lld bytes\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001851 disk_device, megabytes/1000, (megabytes/100)%10, bytes);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001852 printf("%d heads, %d sectors/track, %d cylinders",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001853 g_heads, g_sectors, g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001854 if (units_per_sector == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001855 printf(", total %llu sectors",
Rob Landleyb73451d2006-02-24 16:29:00 +00001856 total_number_of_sectors / (sector_size/512));
Denis Vlasenkobd852072007-03-19 14:43:38 +00001857 printf("\nUnits = %s of %d * %d = %d bytes\n\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00001858 str_units(PLURAL),
1859 units_per_sector, sector_size, units_per_sector * sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001860}
1861
1862/*
1863 * Check whether partition entries are ordered by their starting positions.
1864 * Return 0 if OK. Return i if partition i should have been earlier.
1865 * Two separate checks: primary and logical partitions.
1866 */
1867static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001868wrong_p_order(int *prev)
1869{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001870 const struct pte *pe;
1871 const struct partition *p;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001872 ullong last_p_start_pos = 0, p_start_pos;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001873 int i, last_i = 0;
1874
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001875 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001876 if (i == 4) {
1877 last_i = 4;
1878 last_p_start_pos = 0;
1879 }
1880 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001881 p = pe->part_table;
1882 if (p->sys_ind) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001883 p_start_pos = get_partition_start(pe);
1884
1885 if (last_p_start_pos > p_start_pos) {
1886 if (prev)
1887 *prev = last_i;
1888 return i;
1889 }
1890
1891 last_p_start_pos = p_start_pos;
1892 last_i = i;
1893 }
1894 }
1895 return 0;
1896}
1897
Denis Vlasenko834410a2006-11-29 12:00:28 +00001898#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001899/*
1900 * Fix the chain of logicals.
1901 * extended_offset is unchanged, the set of sectors used is unchanged
1902 * The chain is sorted so that sectors increase, and so that
1903 * starting sectors increase.
1904 *
1905 * After this it may still be that cfdisk doesnt like the table.
1906 * (This is because cfdisk considers expanded parts, from link to
1907 * end of partition, and these may still overlap.)
1908 * Now
1909 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
1910 * may help.
1911 */
1912static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001913fix_chain_of_logicals(void)
1914{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001915 int j, oj, ojj, sj, sjj;
1916 struct partition *pj,*pjj,tmp;
1917
1918 /* Stage 1: sort sectors but leave sector of part 4 */
1919 /* (Its sector is the global extended_offset.) */
1920 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001921 for (j = 5; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001922 oj = ptes[j].offset;
1923 ojj = ptes[j+1].offset;
1924 if (oj > ojj) {
1925 ptes[j].offset = ojj;
1926 ptes[j+1].offset = oj;
1927 pj = ptes[j].part_table;
1928 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
1929 pjj = ptes[j+1].part_table;
1930 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
1931 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001932 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001933 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00001934 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001935 goto stage1;
1936 }
1937 }
1938
1939 /* Stage 2: sort starting sectors */
1940 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001941 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001942 pj = ptes[j].part_table;
1943 pjj = ptes[j+1].part_table;
1944 sj = get_start_sect(pj);
1945 sjj = get_start_sect(pjj);
1946 oj = ptes[j].offset;
1947 ojj = ptes[j+1].offset;
1948 if (oj+sj > ojj+sjj) {
1949 tmp = *pj;
1950 *pj = *pjj;
1951 *pjj = tmp;
1952 set_start_sect(pj, ojj+sjj-oj);
1953 set_start_sect(pjj, oj+sj-ojj);
1954 goto stage2;
1955 }
1956 }
1957
1958 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001959 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001960 ptes[j].changed = 1;
1961}
1962
1963
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001964static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001965fix_partition_table_order(void)
1966{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001967 struct pte *pei, *pek;
1968 int i,k;
1969
1970 if (!wrong_p_order(NULL)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001971 printf("Ordering is already correct\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001972 return;
1973 }
1974
1975 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
1976 /* partition i should have come earlier, move it */
1977 /* We have to move data in the MBR */
1978 struct partition *pi, *pk, *pe, pbuf;
1979 pei = &ptes[i];
1980 pek = &ptes[k];
1981
1982 pe = pei->ext_pointer;
1983 pei->ext_pointer = pek->ext_pointer;
1984 pek->ext_pointer = pe;
1985
1986 pi = pei->part_table;
1987 pk = pek->part_table;
1988
1989 memmove(&pbuf, pi, sizeof(struct partition));
1990 memmove(pi, pk, sizeof(struct partition));
1991 memmove(pk, &pbuf, sizeof(struct partition));
1992
1993 pei->changed = pek->changed = 1;
1994 }
1995
1996 if (i)
1997 fix_chain_of_logicals();
1998
1999 printf("Done.\n");
2000
2001}
2002#endif
2003
2004static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002005list_table(int xtra)
2006{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002007 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002008 int i, w;
2009
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002010 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011 sun_list_table(xtra);
2012 return;
2013 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002014 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002015 sgi_list_table(xtra);
2016 return;
2017 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002018
2019 list_disk_geometry();
2020
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002021 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002022 xbsd_print_disklabel(xtra);
2023 return;
2024 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002025
2026 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
2027 but if the device name ends in a digit, say /dev/foo1,
2028 then the partition is called /dev/foo1p3. */
2029 w = strlen(disk_device);
2030 if (w && isdigit(disk_device[w-1]))
2031 w++;
2032 if (w < 5)
2033 w = 5;
2034
Denis Vlasenkobd852072007-03-19 14:43:38 +00002035 // 1 12345678901 12345678901 12345678901 12
2036 printf("%*s Boot Start End Blocks Id System\n",
2037 w+1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002038
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002039 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002040 const struct pte *pe = &ptes[i];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002041 ullong psects;
2042 ullong pblocks;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002043 unsigned podd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002044
2045 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002046 if (!p || is_cleared_partition(p))
2047 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002048
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002049 psects = get_nr_sects(p);
2050 pblocks = psects;
2051 podd = 0;
2052
2053 if (sector_size < 1024) {
2054 pblocks /= (1024 / sector_size);
2055 podd = psects % (1024 / sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002056 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002057 if (sector_size > 1024)
2058 pblocks *= (sector_size / 1024);
2059
2060 printf("%s %c %11llu %11llu %11llu%c %2x %s\n",
2061 partname(disk_device, i+1, w+2),
2062 !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG /* boot flag */
2063 ? '*' : '?',
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002064 (ullong) cround(get_partition_start(pe)), /* start */
2065 (ullong) cround(get_partition_start(pe) + psects /* end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002066 - (psects ? 1 : 0)),
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002067 (ullong) pblocks, podd ? '+' : ' ', /* odd flag on end */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002068 p->sys_ind, /* type id */
2069 partition_type(p->sys_ind)); /* type name */
2070
2071 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002072 }
2073
2074 /* Is partition table in disk order? It need not be, but... */
2075 /* partition table entries are not checked for correct order if this
2076 is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002077 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002078 /* FIXME */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002079 printf("\nPartition table entries are not in disk order\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002080 }
2081}
2082
Denis Vlasenko834410a2006-11-29 12:00:28 +00002083#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002084static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002085x_list_table(int extend)
2086{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002087 const struct pte *pe;
2088 const struct partition *p;
2089 int i;
2090
Denis Vlasenkobd852072007-03-19 14:43:38 +00002091 printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002092 disk_device, g_heads, g_sectors, g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002093 printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002094 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002095 pe = &ptes[i];
2096 p = (extend ? pe->ext_pointer : pe->part_table);
2097 if (p != NULL) {
Eric Andersen040f4402003-07-30 08:40:37 +00002098 printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002099 i + 1, p->boot_ind, p->head,
2100 sector(p->sector),
2101 cylinder(p->sector, p->cyl), p->end_head,
2102 sector(p->end_sector),
2103 cylinder(p->end_sector, p->end_cyl),
2104 get_start_sect(p), get_nr_sects(p), p->sys_ind);
2105 if (p->sys_ind)
2106 check_consistency(p, i);
2107 }
2108 }
2109}
2110#endif
2111
Denis Vlasenko834410a2006-11-29 12:00:28 +00002112#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002113static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002114fill_bounds(ullong *first, ullong *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002115{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002116 int i;
2117 const struct pte *pe = &ptes[0];
2118 const struct partition *p;
2119
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002120 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002121 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002122 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002123 first[i] = 0xffffffff;
2124 last[i] = 0;
2125 } else {
2126 first[i] = get_partition_start(pe);
2127 last[i] = first[i] + get_nr_sects(p) - 1;
2128 }
2129 }
2130}
2131
2132static void
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002133check(int n, unsigned h, unsigned s, unsigned c, ullong start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002134{
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002135 ullong total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136
2137 real_s = sector(s) - 1;
2138 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002139 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140 if (!total)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002141 printf("Partition %d contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002142 if (h >= g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002143 printf("Partition %d: head %d greater than maximum %d\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002144 n, h + 1, g_heads);
2145 if (real_s >= g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002146 printf("Partition %d: sector %d greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002147 "maximum %d\n", n, s, g_sectors);
2148 if (real_c >= g_cylinders)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002149 printf("Partition %d: cylinder %llu greater than "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002150 "maximum %d\n", n, real_c + 1, g_cylinders);
2151 if (g_cylinders <= 1024 && start != total)
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002152 printf("Partition %d: previous sectors %llu disagrees with "
2153 "total %llu\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002154}
2155
2156static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002157verify(void)
2158{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002159 int i, j;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002160 unsigned total = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002161 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002162 struct partition *p;
2163
2164 if (warn_geometry())
2165 return;
2166
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002167 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002168 verify_sun();
2169 return;
2170 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002171 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002172 verify_sgi(1);
2173 return;
2174 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175
2176 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002177 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002178 struct pte *pe = &ptes[i];
2179
2180 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002181 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002182 check_consistency(p, i);
2183 if (get_partition_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002184 printf("Warning: bad start-of-data in "
2185 "partition %d\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002186 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2187 last[i]);
2188 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002189 for (j = 0; j < i; j++) {
2190 if ((first[i] >= first[j] && first[i] <= last[j])
2191 || ((last[i] <= last[j] && last[i] >= first[j]))) {
2192 printf("Warning: partition %d overlaps "
2193 "partition %d\n", j + 1, i + 1);
2194 total += first[i] >= first[j] ?
2195 first[i] : first[j];
2196 total -= last[i] <= last[j] ?
2197 last[i] : last[j];
2198 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002199 }
2200 }
2201 }
2202
2203 if (extended_offset) {
2204 struct pte *pex = &ptes[ext_index];
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002205 ullong e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002206 get_nr_sects(pex->part_table) - 1;
2207
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002208 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002209 total++;
2210 p = ptes[i].part_table;
2211 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002212 if (i != 4 || i + 1 < g_partitions)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002213 printf("Warning: partition %d "
2214 "is empty\n", i + 1);
2215 } else if (first[i] < extended_offset || last[i] > e_last) {
2216 printf("Logical partition %d not entirely in "
2217 "partition %d\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002218 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219 }
2220 }
2221
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002222 if (total > g_heads * g_sectors * g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00002223 printf("Total allocated sectors %d greater than the maximum "
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002224 "%d\n", total, g_heads * g_sectors * g_cylinders);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002225 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002226 total = g_heads * g_sectors * g_cylinders - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002227 if (total != 0)
2228 printf("%d unallocated sectors\n", total);
2229 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002230}
2231
2232static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002233add_partition(int n, int sys)
2234{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002235 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002236 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002237 struct partition *p = ptes[n].part_table;
2238 struct partition *q = ptes[ext_index].part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002239 ullong limit, temp;
2240 ullong start, stop = 0;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002241 ullong first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242
2243 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002244 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002245 return;
2246 }
2247 fill_bounds(first, last);
2248 if (n < 4) {
2249 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002250 if (display_in_cyl_units || !total_number_of_sectors)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002251 limit = (ullong) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002253 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002254 if (extended_offset) {
2255 first[ext_index] = extended_offset;
2256 last[ext_index] = get_start_sect(q) +
2257 get_nr_sects(q) - 1;
2258 }
2259 } else {
2260 start = extended_offset + sector_offset;
2261 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2262 }
2263 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002264 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002265 first[i] = (cround(first[i]) - 1) * units_per_sector;
2266
Denis Vlasenkobd852072007-03-19 14:43:38 +00002267 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002268 do {
2269 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002270 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002271 int lastplusoff;
2272
2273 if (start == ptes[i].offset)
2274 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002275 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002276 if (start >= first[i] && start <= lastplusoff)
2277 start = lastplusoff + 1;
2278 }
2279 if (start > limit)
2280 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002281 if (start >= temp+units_per_sector && num_read) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002282 printf("Sector %lld is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002283 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002284 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002285 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002286 if (!num_read && start == temp) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002287 ullong saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002288
2289 saved_start = start;
2290 start = read_int(cround(saved_start), cround(saved_start), cround(limit),
2291 0, mesg);
2292 if (display_in_cyl_units) {
2293 start = (start - 1) * units_per_sector;
2294 if (start < saved_start) start = saved_start;
2295 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002296 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002297 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002298 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 if (n > 4) { /* NOT for fifth partition */
2300 struct pte *pe = &ptes[n];
2301
2302 pe->offset = start - sector_offset;
2303 if (pe->offset == extended_offset) { /* must be corrected */
2304 pe->offset++;
2305 if (sector_offset == 1)
2306 start++;
2307 }
2308 }
2309
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002310 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002311 struct pte *pe = &ptes[i];
2312
2313 if (start < pe->offset && limit >= pe->offset)
2314 limit = pe->offset - 1;
2315 if (start < first[i] && limit >= first[i])
2316 limit = first[i] - 1;
2317 }
2318 if (start > limit) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002319 printf("No free sectors available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002320 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002321 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002322 return;
2323 }
2324 if (cround(start) == cround(limit)) {
2325 stop = limit;
2326 } else {
2327 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002328 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002329 str_units(SINGULAR));
2330 stop = read_int(cround(start), cround(limit), cround(limit),
2331 cround(start), mesg);
2332 if (display_in_cyl_units) {
2333 stop = stop * units_per_sector - 1;
2334 if (stop >limit)
2335 stop = limit;
2336 }
2337 }
2338
2339 set_partition(n, 0, start, stop, sys);
2340 if (n > 4)
2341 set_partition(n - 1, 1, ptes[n].offset, stop, EXTENDED);
2342
Rob Landleyb73451d2006-02-24 16:29:00 +00002343 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002344 struct pte *pe4 = &ptes[4];
2345 struct pte *pen = &ptes[n];
2346
2347 ext_index = n;
2348 pen->ext_pointer = p;
2349 pe4->offset = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002350 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002351 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2352 pe4->ext_pointer = pe4->part_table + 1;
2353 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002354 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002355 }
2356}
2357
2358static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002359add_logical(void)
2360{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002361 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2362 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002363
Rob Landley081e3842006-08-03 20:07:35 +00002364 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2366 pe->ext_pointer = pe->part_table + 1;
2367 pe->offset = 0;
2368 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002369 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002370 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002371 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372}
2373
2374static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002375new_partition(void)
2376{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 int i, free_primary = 0;
2378
2379 if (warn_geometry())
2380 return;
2381
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002382 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002383 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002384 return;
2385 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002386 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002387 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002388 return;
2389 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002390 if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002391 printf("Sorry - this fdisk cannot handle AIX disk labels.\n"
2392"If you want to add DOS-type partitions, create a new empty DOS partition\n"
2393"table first (use 'o'). This will destroy the present disk contents.\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394 return;
2395 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002396
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 for (i = 0; i < 4; i++)
2398 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002399
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002400 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002401 printf("The maximum number of partitions has been created\n");
Eric Andersen84bdea82004-05-19 10:49:17 +00002402 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002403 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002404
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 if (!free_primary) {
2406 if (extended_offset)
2407 add_logical();
2408 else
Denis Vlasenkobd852072007-03-19 14:43:38 +00002409 printf("You must delete some partition and add "
2410 "an extended partition first\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002411 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002412 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002413 snprintf(line, sizeof(line),
2414 "Command action\n"
2415 " %s\n"
2416 " p primary partition (1-4)\n",
2417 (extended_offset ?
2418 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002419 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002420 c = read_nonempty(line);
2421 if (c == 'p' || c == 'P') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002422 i = get_nonexisting_partition(0, 4);
2423 if (i >= 0)
2424 add_partition(i, LINUX_NATIVE);
2425 return;
2426 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002427 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002428 add_logical();
2429 return;
2430 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002431 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002432 i = get_nonexisting_partition(0, 4);
2433 if (i >= 0)
2434 add_partition(i, EXTENDED);
2435 return;
2436 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002437 printf("Invalid partition number "
2438 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002439 }
2440 }
2441}
2442
2443static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002444write_table(void)
2445{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002446 int i;
2447
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002448 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002449 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002450 if (ptes[i].changed)
2451 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002452 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002453 struct pte *pe = &ptes[i];
2454
2455 if (pe->changed) {
2456 write_part_table_flag(pe->sectorbuffer);
2457 write_sector(pe->offset, pe->sectorbuffer);
2458 }
2459 }
2460 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002461 else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002462 /* no test on change? the printf below might be mistaken */
2463 sgi_write_table();
2464 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002465 else if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002466 int needw = 0;
2467
Rob Landleyb73451d2006-02-24 16:29:00 +00002468 for (i = 0; i < 8; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002469 if (ptes[i].changed)
2470 needw = 1;
2471 if (needw)
2472 sun_write_table();
2473 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474
Denis Vlasenkobd852072007-03-19 14:43:38 +00002475 printf("The partition table has been altered!\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476 reread_partition_table(1);
2477}
2478
Rob Landleyb73451d2006-02-24 16:29:00 +00002479static void
2480reread_partition_table(int leave)
2481{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 int i;
2483
Denis Vlasenkobd852072007-03-19 14:43:38 +00002484 printf("Calling ioctl() to re-read partition table\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002485 sync();
Denis Vlasenkobd852072007-03-19 14:43:38 +00002486 /* sleep(2); Huh? */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002487 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00002488 "WARNING: rereading partition table "
Denis Vlasenko28703012006-12-19 20:32:02 +00002489 "failed, kernel still uses old table");
Denis Vlasenko28703012006-12-19 20:32:02 +00002490#if 0
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491 if (dos_changed)
Rob Landleyb73451d2006-02-24 16:29:00 +00002492 printf(
Denis Vlasenkobd852072007-03-19 14:43:38 +00002493 "\nWARNING: If you have created or modified any DOS 6.x\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494 "partitions, please see the fdisk manual page for additional\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002495 "information\n");
Denis Vlasenko28703012006-12-19 20:32:02 +00002496#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002497
2498 if (leave) {
Denis Vlasenko28703012006-12-19 20:32:02 +00002499 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002500 close_dev_fd();
Denis Vlasenko28703012006-12-19 20:32:02 +00002501 exit(i != 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 }
2503}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002504#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505
Denis Vlasenko834410a2006-11-29 12:00:28 +00002506#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002507#define MAX_PER_LINE 16
2508static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002509print_buffer(char *pbuffer)
2510{
2511 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512
2513 for (i = 0, l = 0; i < sector_size; i++, l++) {
2514 if (l == 0)
2515 printf("0x%03X:", i);
2516 printf(" %02X", (unsigned char) pbuffer[i]);
2517 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002518 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 l = -1;
2520 }
2521 }
2522 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002523 bb_putchar('\n');
2524 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002525}
2526
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002528print_raw(void)
2529{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002530 int i;
2531
Denis Vlasenkobd852072007-03-19 14:43:38 +00002532 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002533 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002534 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002535 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002536 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002538 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002539}
2540
2541static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002542move_begin(int i)
2543{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002544 struct pte *pe = &ptes[i];
2545 struct partition *p = pe->part_table;
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002546 ullong new, first;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547
2548 if (warn_geometry())
2549 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002550 if (!p->sys_ind || !get_nr_sects(p) || IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002551 printf("Partition %d has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002552 return;
2553 }
2554 first = get_partition_start(pe);
2555 new = read_int(first, first, first + get_nr_sects(p) - 1, first,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002556 "New beginning of data") - pe->offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557
2558 if (new != get_nr_sects(p)) {
2559 first = get_nr_sects(p) + get_start_sect(p) - new;
2560 set_nr_sects(p, first);
2561 set_start_sect(p, new);
2562 pe->changed = 1;
2563 }
2564}
2565
2566static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002567xselect(void)
2568{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569 char c;
2570
Rob Landleyb73451d2006-02-24 16:29:00 +00002571 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002572 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002573 c = tolower(read_nonempty("Expert command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 switch (c) {
2575 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002576 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578 break;
2579 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002580 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002581 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582 break;
2583 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002584 user_cylinders = g_cylinders =
2585 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002586 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002588 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002589 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 warn_cylinders();
2591 break;
2592 case 'd':
2593 print_raw();
2594 break;
2595 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002596 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002597 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002598 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002600 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002601 x_list_table(1);
2602 break;
2603 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002604 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002605 fix_partition_table_order();
2606 break;
2607 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002608#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002609 create_sgilabel();
2610#endif
2611 break;
2612 case 'h':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002613 user_heads = g_heads = read_int(1, g_heads, 256, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002614 "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002615 update_units();
2616 break;
2617 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002618 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002620 break;
2621 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002622 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002623 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002624 break;
2625 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002626 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 list_table(1);
2628 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002629 x_list_table(0);
2630 break;
2631 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002632 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002633 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002634 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002635 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002636 case 'r':
2637 return;
2638 case 's':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002639 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002640 "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002642 sector_offset = g_sectors;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002643 printf("Warning: setting sector offset for DOS "
2644 "compatiblity\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 }
2646 update_units();
2647 break;
2648 case 'v':
2649 verify();
2650 break;
2651 case 'w':
2652 write_table(); /* does not return */
2653 break;
2654 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002655 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002656 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002657 break;
2658 default:
2659 xmenu();
2660 }
2661 }
2662}
2663#endif /* ADVANCED mode */
2664
2665static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002666is_ide_cdrom_or_tape(const char *device)
2667{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002668 FILE *procf;
2669 char buf[100];
2670 struct stat statbuf;
2671 int is_ide = 0;
2672
2673 /* No device was given explicitly, and we are trying some
2674 likely things. But opening /dev/hdc may produce errors like
2675 "hdc: tray open or drive not ready"
2676 if it happens to be a CD-ROM drive. It even happens that
2677 the process hangs on the attempt to read a music CD.
2678 So try to be careful. This only works since 2.1.73. */
2679
2680 if (strncmp("/dev/hd", device, 7))
2681 return 0;
2682
2683 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2684 procf = fopen(buf, "r");
2685 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2686 is_ide = (!strncmp(buf, "cdrom", 5) ||
2687 !strncmp(buf, "tape", 4));
2688 else
2689 /* Now when this proc file does not exist, skip the
2690 device when it is read-only. */
2691 if (stat(device, &statbuf) == 0)
2692 is_ide = ((statbuf.st_mode & 0222) == 0);
2693
2694 if (procf)
2695 fclose(procf);
2696 return is_ide;
2697}
2698
Rob Landley5527b912006-02-25 03:46:10 +00002699
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002700static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002701open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002702{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002703 int gb;
2704
2705 disk_device = device;
2706 if (setjmp(listingbuf))
2707 return;
2708 if (!user_specified)
2709 if (is_ide_cdrom_or_tape(device))
2710 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002711
2712 /* Open disk_device, save file descriptor to dev_fd */
2713 errno = 0;
2714 gb = get_boot(TRY_ONLY);
2715 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002716 /* Ignore other errors, since we try IDE
2717 and SCSI hard disks which may not be
2718 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002719 if (user_specified || errno == EACCES)
2720 bb_perror_msg("can't open '%s'", device);
2721 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002722 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002723
2724 if (gb < 0) { /* no DOS signature */
2725 list_disk_geometry();
2726 if (LABEL_IS_AIX)
2727 goto ret;
2728#if ENABLE_FEATURE_OSF_LABEL
2729 if (bsd_trydev(device) < 0)
2730#endif
2731 printf("Disk %s doesn't contain a valid "
2732 "partition table\n", device);
2733 } else {
2734 list_table(0);
2735#if ENABLE_FEATURE_FDISK_WRITABLE
2736 if (!LABEL_IS_SUN && g_partitions > 4) {
2737 delete_partition(ext_index);
2738 }
2739#endif
2740 }
2741 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002742 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743}
2744
2745/* for fdisk -l: try all things in /proc/partitions
2746 that look like a partition name (do not end in a digit) */
2747static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002748list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002749{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 FILE *procpt;
2751 char line[100], ptname[100], devname[120], *s;
2752 int ma, mi, sz;
2753
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002754 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002755
2756 while (fgets(line, sizeof(line), procpt)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002757 if (sscanf(line, " %d %d %d %[^\n ]",
2758 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002760 for (s = ptname; *s; s++)
2761 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002762 if (isdigit(s[-1]))
2763 continue;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002764 sprintf(devname, "/dev/%s", ptname);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002765 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002766 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002767#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002769#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002770}
2771
Denis Vlasenko834410a2006-11-29 12:00:28 +00002772#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002773static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002774unknown_command(int c)
2775{
Denis Vlasenkobd852072007-03-19 14:43:38 +00002776 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002778#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002779
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002780int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleyb73451d2006-02-24 16:29:00 +00002781int fdisk_main(int argc, char **argv)
2782{
Denis Vlasenko834410a2006-11-29 12:00:28 +00002783 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002785 * fdisk -v
2786 * fdisk -l [-b sectorsize] [-u] device ...
2787 * fdisk -s [partition] ...
2788 * fdisk [-b sectorsize] [-u] device
2789 *
2790 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002792 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00002793
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002794 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002795
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002796 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00002797 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002798 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002799 argc -= optind;
2800 argv += optind;
2801 if (opt & OPT_b) { // -b
2802 /* Ugly: this sector size is really per device,
2803 so cannot be combined with multiple disks,
2804 and the same goes for the C/H/S options.
2805 */
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002806 if (sector_size != 512 && sector_size != 1024
2807 && sector_size != 2048)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002808 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002809 sector_offset = 2;
2810 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00002812 if (user_heads <= 0 || user_heads >= 256)
2813 user_heads = 0;
2814 if (user_sectors <= 0 || user_sectors >= 64)
2815 user_sectors = 0;
2816 if (opt & OPT_u)
2817 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002818
Denis Vlasenko834410a2006-11-29 12:00:28 +00002819#if ENABLE_FEATURE_FDISK_WRITABLE
2820 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002821 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002822#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002823 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002825 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00002826 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002827 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002829 /* we don't have device names, */
2830 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002831 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 }
2833 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002834#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002836#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837
Denis Vlasenko834410a2006-11-29 12:00:28 +00002838#if ENABLE_FEATURE_FDISK_BLKSIZE
2839 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002840 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841
2842 nowarn = 1;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002843 if (argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002844 bb_show_usage();
Denis Vlasenko834410a2006-11-29 12:00:28 +00002845 for (j = 0; j < argc; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002846 unsigned long long size;
2847 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00002848 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 close(fd);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002850 if (argc == 1)
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002851 printf("%lld\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002852 else
Denis Vlasenkocdf62772008-03-17 08:42:43 +00002853 printf("%s: %lld\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002854 }
2855 return 0;
2856 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002857#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002858
Denis Vlasenko834410a2006-11-29 12:00:28 +00002859#if ENABLE_FEATURE_FDISK_WRITABLE
2860 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002861 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862
Denis Vlasenko834410a2006-11-29 12:00:28 +00002863 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00002864 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002866 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002867 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00002868 printf("Detected an OSF/1 disklabel on %s, entering "
2869 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002870 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00002871 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00002872 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002873 /* If we return we may want to make an empty DOS label? */
2874 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875
2876 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00002877 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00002878 bb_putchar('\n');
Denis Vlasenkobd852072007-03-19 14:43:38 +00002879 c = tolower(read_nonempty("Command (m for help): "));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002880 switch (c) {
2881 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002882 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002883 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002884 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002885 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002886 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002887 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002889 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002890 else
2891 unknown_command(c);
2892 break;
2893 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002894 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002895 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00002896 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00002897 if (read_maybe_empty("Please enter the name of the "
2898 "new boot file: ") == '\n')
2899 printf("Boot file unchanged\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002900 else
2901 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00002902 }
2903#if ENABLE_FEATURE_OSF_LABEL
2904 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00002905 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906#endif
2907 break;
2908 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002909 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002911 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002912 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002914 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002915 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002916 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002917 else
2918 unknown_command(c);
2919 break;
2920 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002921 {
Eric Andersen040f4402003-07-30 08:40:37 +00002922 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00002923 /* If sgi_label then don't use get_existing_partition,
2924 let the user select a partition, since
2925 get_existing_partition() only works for Linux-like
2926 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002927 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002928 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002929 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002930 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00002931 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002932 if (j >= 0)
2933 delete_partition(j);
2934 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002935 break;
2936 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002937 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002938 create_sgiinfo();
2939 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002940 unknown_command(c);
2941 case 'l':
2942 list_types(get_sys_types());
2943 break;
2944 case 'm':
2945 menu();
2946 break;
2947 case 'n':
2948 new_partition();
2949 break;
2950 case 'o':
2951 create_doslabel();
2952 break;
2953 case 'p':
2954 list_table(0);
2955 break;
2956 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002957 if (ENABLE_FEATURE_CLEAN_UP)
2958 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002959 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002960 return 0;
2961 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002962#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002963 create_sunlabel();
2964#endif
2965 break;
2966 case 't':
2967 change_sysid();
2968 break;
2969 case 'u':
2970 change_units();
2971 break;
2972 case 'v':
2973 verify();
2974 break;
2975 case 'w':
2976 write_table(); /* does not return */
2977 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00002978#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002979 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002980 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002981 printf("\n\tSorry, no experts menu for SGI "
2982 "partition tables available\n\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002983 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 xselect();
2985 break;
2986#endif
2987 default:
2988 unknown_command(c);
2989 menu();
2990 }
2991 }
2992 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002993#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002994}