blob: 288b9235f8e2fcc7b420d73d99174695317dcd8e [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko2ab94032017-10-05 15:33:28 +02002/*
3 * fdisk.c -- Partition table manipulator for Linux.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00004 *
5 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00006 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00009 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010010//config:config FDISK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "fdisk (41 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: The fdisk utility is used to divide hard disks into one or more
16//config: logical disks, which are generally called partitions. This utility
17//config: can be used to list and edit the set of partitions or BSD style
18//config: 'disk slices' that are defined on a hard drive.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019//config:
20//config:config FDISK_SUPPORT_LARGE_DISKS
21//config: bool "Support over 4GB disks"
22//config: default y
23//config: depends on FDISK
24//config: depends on !LFS # with LFS no special code is needed
Denys Vlasenkodd898c92016-11-23 11:46:32 +010025//config:
26//config:config FEATURE_FDISK_WRITABLE
27//config: bool "Write support"
28//config: default y
29//config: depends on FDISK
30//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//config: Enabling this option allows you to create or change a partition table
32//config: and write those changes out to disk. If you leave this option
33//config: disabled, you will only be able to view the partition table.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010034//config:
35//config:config FEATURE_AIX_LABEL
36//config: bool "Support AIX disklabels"
37//config: default n
38//config: depends on FDISK && FEATURE_FDISK_WRITABLE
39//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//config: Enabling this option allows you to create or change AIX disklabels.
41//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010042//config:
43//config:config FEATURE_SGI_LABEL
44//config: bool "Support SGI disklabels"
45//config: default n
46//config: depends on FDISK && FEATURE_FDISK_WRITABLE
47//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020048//config: Enabling this option allows you to create or change SGI disklabels.
49//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010050//config:
51//config:config FEATURE_SUN_LABEL
52//config: bool "Support SUN disklabels"
53//config: default n
54//config: depends on FDISK && FEATURE_FDISK_WRITABLE
55//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020056//config: Enabling this option allows you to create or change SUN disklabels.
57//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010058//config:
59//config:config FEATURE_OSF_LABEL
60//config: bool "Support BSD disklabels"
61//config: default n
62//config: depends on FDISK && FEATURE_FDISK_WRITABLE
63//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020064//config: Enabling this option allows you to create or change BSD disklabels
65//config: and define and edit BSD disk slices.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010066//config:
67//config:config FEATURE_GPT_LABEL
68//config: bool "Support GPT disklabels"
69//config: default n
70//config: depends on FDISK && FEATURE_FDISK_WRITABLE
71//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020072//config: Enabling this option allows you to view GUID Partition Table
73//config: disklabels.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010074//config:
75//config:config FEATURE_FDISK_ADVANCED
76//config: bool "Support expert mode"
77//config: default y
78//config: depends on FDISK && FEATURE_FDISK_WRITABLE
79//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020080//config: Enabling this option allows you to do terribly unsafe things like
81//config: define arbitrary drive geometry, move the beginning of data in a
82//config: partition, and similarly evil things. Unless you have a very good
83//config: reason you would be wise to leave this disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010084
85//applet:IF_FDISK(APPLET(fdisk, BB_DIR_SBIN, BB_SUID_DROP))
86
87//kbuild:lib-$(CONFIG_FDISK) += fdisk.o
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000088
Pere Orga5bc8c002011-04-11 03:29:49 +020089/* Looks like someone forgot to add this to config system */
90//usage:#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
91//usage:# define ENABLE_FEATURE_FDISK_BLKSIZE 0
92//usage:# define IF_FEATURE_FDISK_BLKSIZE(a)
93//usage:#endif
94//usage:
95//usage:#define fdisk_trivial_usage
96//usage: "[-ul" IF_FEATURE_FDISK_BLKSIZE("s") "] "
97//usage: "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK"
98//usage:#define fdisk_full_usage "\n\n"
99//usage: "Change partition table\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200100//usage: "\n -u Start and End are in sectors (instead of cylinders)"
101//usage: "\n -l Show partition table for each DISK, then exit"
102//usage: IF_FEATURE_FDISK_BLKSIZE(
103//usage: "\n -s Show partition sizes in kb for each DISK, then exit"
104//usage: )
105//usage: "\n -b 2048 (for certain MO disks) use 2048-byte sectors"
106//usage: "\n -C CYLINDERS Set number of cylinders/heads/sectors"
Denys Vlasenko704c6062017-01-19 14:29:42 +0100107//usage: "\n -H HEADS Typically 255"
108//usage: "\n -S SECTORS Typically 63"
Pere Orga5bc8c002011-04-11 03:29:49 +0200109
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000110#ifndef _LARGEFILE64_SOURCE
111/* For lseek64 */
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +0200112# define _LARGEFILE64_SOURCE
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000113#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000114#include <assert.h> /* assert */
Denys Vlasenkoda49f582009-07-08 02:58:38 +0200115#include <sys/mount.h>
116#if !defined(BLKSSZGET)
117# define BLKSSZGET _IO(0x12, 104)
118#endif
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +0200119#if !defined(BLKGETSIZE64)
120# define BLKGETSIZE64 _IOR(0x12,114,size_t)
121#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000122#include "libbb.h"
Denys Vlasenko29516ac2016-11-27 06:13:43 +0100123#include "unicode.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000124
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200125#if BB_LITTLE_ENDIAN
126# define inline_if_little_endian ALWAYS_INLINE
127#else
128# define inline_if_little_endian /* nothing */
129#endif
130
131
Denis Vlasenko834410a2006-11-29 12:00:28 +0000132/* Looks like someone forgot to add this to config system */
133#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
134# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000135# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +0000136#endif
137
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000138#define DEFAULT_SECTOR_SIZE 512
139#define DEFAULT_SECTOR_SIZE_STR "512"
140#define MAX_SECTOR_SIZE 2048
141#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
142#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000143
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000144#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000145
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000146#define EXTENDED 0x05
147#define WIN98_EXTENDED 0x0f
148#define LINUX_PARTITION 0x81
149#define LINUX_SWAP 0x82
150#define LINUX_NATIVE 0x83
151#define LINUX_EXTENDED 0x85
152#define LINUX_LVM 0x8e
153#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000154
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000155
156enum {
157 OPT_b = 1 << 0,
158 OPT_C = 1 << 1,
159 OPT_H = 1 << 2,
160 OPT_l = 1 << 3,
161 OPT_S = 1 << 4,
162 OPT_u = 1 << 5,
163 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
164};
165
166
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000167typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200168/* Used for sector numbers. Partition formats we know
169 * do not support more than 2^32 sectors
170 */
171typedef uint32_t sector_t;
Denys Vlasenko163e6372018-07-25 12:08:26 +0200172#if UINT_MAX == 0xffffffff
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200173# define SECT_FMT ""
Denys Vlasenko163e6372018-07-25 12:08:26 +0200174#elif ULONG_MAX == 0xffffffff
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200175# define SECT_FMT "l"
176#else
177# error Cant detect sizeof(uint32_t)
178#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000179
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000180struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +0000181 unsigned char heads;
182 unsigned char sectors;
183 unsigned short cylinders;
184 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000185};
186
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000187#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000188
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200189/* TODO: #if ENABLE_FEATURE_FDISK_WRITABLE */
190/* (currently fdisk_sun/sgi.c do not have proper WRITABLE #ifs) */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000191static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +0000192"Building a new %s. Changes will remain in memory only,\n"
193"until you decide to write them. After that the previous content\n"
194"won't be recoverable.\n\n";
195
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000196static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200197"Partition %u is already defined, delete it before re-adding\n";
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200198/* #endif */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000199
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000200
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000201struct partition {
202 unsigned char boot_ind; /* 0x80 - active */
203 unsigned char head; /* starting head */
204 unsigned char sector; /* starting sector */
205 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000206 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000207 unsigned char end_head; /* end head */
208 unsigned char end_sector; /* end sector */
209 unsigned char end_cyl; /* end cylinder */
210 unsigned char start4[4]; /* starting sector counting from 0 */
211 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000212} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000213
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200214/*
215 * per partition table entry data
216 *
217 * The four primary partitions have the same sectorbuffer (MBRbuffer)
218 * and have NULL ext_pointer.
219 * Each logical partition table entry has two pointers, one for the
220 * partition and one link to the next one.
221 */
222struct pte {
223 struct partition *part_table; /* points into sectorbuffer */
224 struct partition *ext_pointer; /* points into sectorbuffer */
225 sector_t offset_from_dev_start; /* disk sector number */
226 char *sectorbuffer; /* disk sector contents */
227#if ENABLE_FEATURE_FDISK_WRITABLE
228 char changed; /* boolean */
229#endif
230};
231
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200232#define unable_to_open "can't open '%s'"
233#define unable_to_read "can't read from %s"
234#define unable_to_seek "can't seek on %s"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000235
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000236enum label_type {
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200237 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF, LABEL_GPT
Rob Landley5527b912006-02-25 03:46:10 +0000238};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000239
Denis Vlasenko4437d192008-04-17 00:12:10 +0000240#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000241
Denis Vlasenko834410a2006-11-29 12:00:28 +0000242#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000243#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000244#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000245#else
246#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000247#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000248#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000249
Denis Vlasenko834410a2006-11-29 12:00:28 +0000250#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000251#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000252#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000253#else
254#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000255#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000256#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000257
Denis Vlasenko834410a2006-11-29 12:00:28 +0000258#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000259#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000260#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000261#else
262#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000263#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000264#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000265
Denis Vlasenko834410a2006-11-29 12:00:28 +0000266#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000267#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000268#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000269#else
270#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000271#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000272#endif
Rob Landley5527b912006-02-25 03:46:10 +0000273
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200274#if ENABLE_FEATURE_GPT_LABEL
275#define LABEL_IS_GPT (LABEL_GPT == current_label_type)
276#define STATIC_GPT static
277#else
278#define LABEL_IS_GPT 0
279#define STATIC_GPT extern
280#endif
281
Denis Vlasenko4437d192008-04-17 00:12:10 +0000282enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000283
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000284static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000285#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000286static void change_units(void);
Denys Vlasenkob347df92011-08-09 22:49:15 +0200287static void reread_partition_table(int leave);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000288static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200289static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000290static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200291static sector_t read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000292#endif
293static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000294static void get_geometry(void);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200295static void read_pte(struct pte *pe, sector_t offset);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000296#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000297static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000298#else
299static int get_boot(void);
300#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000301
302#define PLURAL 0
303#define SINGULAR 1
304
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200305static sector_t get_start_sect(const struct partition *p);
306static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000307
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000308/* DOS partition types */
309
310static const char *const i386_sys_types[] = {
311 "\x00" "Empty",
312 "\x01" "FAT12",
313 "\x04" "FAT16 <32M",
314 "\x05" "Extended", /* DOS 3.3+ extended partition */
315 "\x06" "FAT16", /* DOS 16-bit >=32M */
316 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
317 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
318 "\x0b" "Win95 FAT32",
319 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
320 "\x0e" "Win95 FAT16 (LBA)",
321 "\x0f" "Win95 Ext'd (LBA)",
322 "\x11" "Hidden FAT12",
323 "\x12" "Compaq diagnostics",
324 "\x14" "Hidden FAT16 <32M",
325 "\x16" "Hidden FAT16",
326 "\x17" "Hidden HPFS/NTFS",
327 "\x1b" "Hidden Win95 FAT32",
328 "\x1c" "Hidden W95 FAT32 (LBA)",
329 "\x1e" "Hidden W95 FAT16 (LBA)",
330 "\x3c" "Part.Magic recovery",
331 "\x41" "PPC PReP Boot",
332 "\x42" "SFS",
333 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
334 "\x80" "Old Minix", /* Minix 1.4a and earlier */
335 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
336 "\x82" "Linux swap", /* also Solaris */
337 "\x83" "Linux",
338 "\x84" "OS/2 hidden C: drive",
339 "\x85" "Linux extended",
340 "\x86" "NTFS volume set",
341 "\x87" "NTFS volume set",
342 "\x8e" "Linux LVM",
343 "\x9f" "BSD/OS", /* BSDI */
344 "\xa0" "Thinkpad hibernation",
345 "\xa5" "FreeBSD", /* various BSD flavours */
346 "\xa6" "OpenBSD",
347 "\xa8" "Darwin UFS",
348 "\xa9" "NetBSD",
349 "\xab" "Darwin boot",
350 "\xb7" "BSDI fs",
351 "\xb8" "BSDI swap",
352 "\xbe" "Solaris boot",
353 "\xeb" "BeOS fs",
354 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
355 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
356 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
357 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
358 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
359 autodetect using persistent
360 superblock */
361#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
362 "\x02" "XENIX root",
363 "\x03" "XENIX usr",
364 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
365 "\x09" "AIX bootable", /* AIX data or Coherent */
366 "\x10" "OPUS",
367 "\x18" "AST SmartSleep",
368 "\x24" "NEC DOS",
369 "\x39" "Plan 9",
370 "\x40" "Venix 80286",
371 "\x4d" "QNX4.x",
372 "\x4e" "QNX4.x 2nd part",
373 "\x4f" "QNX4.x 3rd part",
374 "\x50" "OnTrack DM",
375 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
376 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
377 "\x53" "OnTrack DM6 Aux3",
378 "\x54" "OnTrackDM6",
379 "\x55" "EZ-Drive",
380 "\x56" "Golden Bow",
381 "\x5c" "Priam Edisk",
382 "\x61" "SpeedStor",
383 "\x64" "Novell Netware 286",
384 "\x65" "Novell Netware 386",
385 "\x70" "DiskSecure Multi-Boot",
386 "\x75" "PC/IX",
387 "\x93" "Amoeba",
388 "\x94" "Amoeba BBT", /* (bad block table) */
389 "\xa7" "NeXTSTEP",
390 "\xbb" "Boot Wizard hidden",
391 "\xc1" "DRDOS/sec (FAT-12)",
392 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
393 "\xc6" "DRDOS/sec (FAT-16)",
394 "\xc7" "Syrinx",
395 "\xda" "Non-FS data",
396 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
397 Concurrent DOS or CTOS */
398 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
399 "\xdf" "BootIt", /* BootIt EMBRM */
400 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
401 extended partition */
402 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
403 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
404 partition < 1024 cyl. */
405 "\xf1" "SpeedStor",
406 "\xf4" "SpeedStor", /* SpeedStor large partition */
407 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
408 "\xff" "BBT", /* Xenix Bad Block Table */
409#endif
410 NULL
411};
412
Denis Vlasenko4437d192008-04-17 00:12:10 +0000413enum {
414 dev_fd = 3 /* the disk */
415};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000416
417/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000418struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000419 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000420
421 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000422 int g_partitions; // = 4; /* maximum partition + 1 */
423 unsigned units_per_sector; // = 1;
424 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
425 unsigned user_set_sector_size;
426 unsigned sector_offset; // = 1;
427 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000428 smallint /* enum label_type */ current_label_type;
Denys Vlasenkoc1045492018-07-25 13:45:36 +0200429 smallint display_in_cyl_units;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000430#if ENABLE_FEATURE_OSF_LABEL
431 smallint possibly_osf_label;
432#endif
433
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000434 smallint listing; /* no aborts for fdisk -l */
435 smallint dos_compatible_flag; // = 1;
436#if ENABLE_FEATURE_FDISK_WRITABLE
437 //int dos_changed;
438 smallint nowarn; /* no warnings for fdisk -l/-s */
439#endif
440 int ext_index; /* the prime extended partition */
441 unsigned user_cylinders, user_heads, user_sectors;
442 unsigned pt_heads, pt_sectors;
443 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200444 sector_t extended_offset; /* offset of link pointers */
445 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000446
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000447 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000448 char line_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000449 /* Raw disk label. For DOS-type partition tables the MBR,
450 * with descriptions of the primary partitions. */
451 char MBRbuffer[MAX_SECTOR_SIZE];
452 /* Partition tables */
453 struct pte ptes[MAXIMUM_PARTS];
454};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000455#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000456#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000457#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000458#define g_partitions (G.g_partitions )
459#define units_per_sector (G.units_per_sector )
460#define sector_size (G.sector_size )
461#define user_set_sector_size (G.user_set_sector_size)
462#define sector_offset (G.sector_offset )
463#define g_heads (G.g_heads )
464#define g_sectors (G.g_sectors )
465#define g_cylinders (G.g_cylinders )
466#define current_label_type (G.current_label_type )
467#define display_in_cyl_units (G.display_in_cyl_units)
468#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000469#define listing (G.listing )
470#define dos_compatible_flag (G.dos_compatible_flag )
471#define nowarn (G.nowarn )
472#define ext_index (G.ext_index )
473#define user_cylinders (G.user_cylinders )
474#define user_heads (G.user_heads )
475#define user_sectors (G.user_sectors )
476#define pt_heads (G.pt_heads )
477#define pt_sectors (G.pt_sectors )
478#define kern_heads (G.kern_heads )
479#define kern_sectors (G.kern_sectors )
480#define extended_offset (G.extended_offset )
481#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000482#define listingbuf (G.listingbuf )
483#define line_buffer (G.line_buffer )
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000484#define MBRbuffer (G.MBRbuffer )
485#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000486#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000487 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000488 sector_size = DEFAULT_SECTOR_SIZE; \
489 sector_offset = 1; \
490 g_partitions = 4; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000491 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000492 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000493} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000494
Denis Vlasenkobd852072007-03-19 14:43:38 +0000495
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000496/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200497/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
498 * disks > 2^32 sectors
499 */
500static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000501{
502 uint64_t v64;
503 unsigned long longsectors;
504
505 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000506 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200507 v64 >>= 9;
508 if (v64 != (sector_t)v64) {
509 ret_trunc:
510 /* Not only DOS, but all other partition tables
511 * we support can't record more than 32 bit
512 * sector counts or offsets
513 */
514 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
515 v64 = (uint32_t)-1L;
516 }
517 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000518 }
519 /* Needs temp of type long */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200520 if (ioctl(fd, BLKGETSIZE, &longsectors)) {
521 /* Perhaps this is a disk image */
522 off_t sz = lseek(fd, 0, SEEK_END);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000523 longsectors = 0;
Denys Vlasenkod958e902010-04-06 02:32:26 +0200524 if (sz > 0)
525 longsectors = (uoff_t)sz / sector_size;
526 lseek(fd, 0, SEEK_SET);
527 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200528 if (sizeof(long) > sizeof(sector_t)
529 && longsectors != (sector_t)longsectors
530 ) {
531 goto ret_trunc;
532 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000533 return longsectors;
534}
535
536
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000537#define IS_EXTENDED(i) \
538 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
539
540#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
541
542#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
543
544#define pt_offset(b, n) \
545 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
546
547#define sector(s) ((s) & 0x3f)
548
549#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
550
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000551static void
552close_dev_fd(void)
553{
554 /* Not really closing, but making sure it is open, and to harmless place */
555 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
556}
557
Denys Vlasenkob157eb12016-09-17 21:05:06 +0200558/* Return partition name */
559static const char *
560partname(const char *dev, int pno, int lth)
561{
562 const char *p;
563 int w, wp;
564 int bufsiz;
565 char *bufp;
566
567 bufp = auto_string(xzalloc(80));
568 bufsiz = 80;
569
570 w = strlen(dev);
571 p = "";
572
573 if (isdigit(dev[w-1]))
574 p = "p";
575
576 /* devfs kludge - note: fdisk partition names are not supposed
577 to equal kernel names, so there is no reason to do this */
578 if (strcmp(dev + w - 4, "disc") == 0) {
579 w -= 4;
580 p = "part";
581 }
582
583 wp = strlen(p);
584
585 if (lth) {
586 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
587 lth-wp-2, w, dev, p, pno);
588 } else {
589 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
590 }
591 return bufp;
592}
593
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000594static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000595get_part_table(int i)
596{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597 return ptes[i].part_table;
598}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599
600static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000601str_units(int n)
602{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000604 return display_in_cyl_units ? "cylinder" : "sector";
605 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000606}
607
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000608static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000609valid_part_table_flag(const char *mbuffer)
610{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000611 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000612}
613
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200614static void fdisk_fatal(const char *why)
615{
616 if (listing) {
617 close_dev_fd();
618 longjmp(listingbuf, 1);
619 }
620 bb_error_msg_and_die(why, disk_device);
621}
622
623static void
624seek_sector(sector_t secno)
625{
626#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
627 off64_t off = (off64_t)secno * sector_size;
628 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
629 fdisk_fatal(unable_to_seek);
630#else
631 uint64_t off = (uint64_t)secno * sector_size;
632 if (off > MAXINT(off_t)
633 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
634 ) {
635 fdisk_fatal(unable_to_seek);
636 }
637#endif
638}
639
Denis Vlasenko834410a2006-11-29 12:00:28 +0000640#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200641static void
642set_all_unchanged(void)
643{
644 int i;
645
646 for (i = 0; i < MAXIMUM_PARTS; i++)
647 ptes[i].changed = 0;
648}
649
650static ALWAYS_INLINE void
651set_changed(int i)
652{
653 ptes[i].changed = 1;
654}
655
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000656static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000657write_part_table_flag(char *b)
658{
659 b[510] = 0x55;
660 b[511] = 0xaa;
661}
662
Denys Vlasenkoc1045492018-07-25 13:45:36 +0200663/* Read line; return 0 or first printable non-space char */
664static int
665read_line(const char *prompt)
666{
667 int sz;
668
669 sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer));
670 if (sz <= 0)
671 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
672
673 if (line_buffer[sz-1] == '\n')
674 line_buffer[--sz] = '\0';
675
676 line_ptr = line_buffer;
677 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
678 line_ptr++;
679 return *line_ptr;
680}
681
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000682static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000683read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000684{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000685 while (!read_line(mesg))
686 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 return *line_ptr;
688}
689
690static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000691read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000693 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000694 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000695 line_ptr[0] = '\n';
696 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000697 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000698 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000699}
700
701static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000702read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000703{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000704 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000705 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000706 read_nonempty("Hex code (type L to list codes): ");
Denys Vlasenkod958e902010-04-06 02:32:26 +0200707 if ((line_ptr[0] | 0x20) == 'l') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000708 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000709 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000710 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000711 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200712 if (v <= 0xff)
713 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000714 }
715}
716
Denis Vlasenko9764d692008-07-09 21:20:50 +0000717static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200718write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000719{
720 seek_sector(secno);
721 xwrite(dev_fd, buf, sector_size);
722}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200723#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000724
725
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000726#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000727
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100728struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000729 unsigned char info[128]; /* Informative text string */
730 unsigned char spare0[14];
731 struct sun_info {
732 unsigned char spare1;
733 unsigned char id;
734 unsigned char spare2;
735 unsigned char flags;
736 } infos[8];
737 unsigned char spare1[246]; /* Boot information etc. */
738 unsigned short rspeed; /* Disk rotational speed */
739 unsigned short pcylcount; /* Physical cylinder count */
740 unsigned short sparecyl; /* extra sects per cylinder */
741 unsigned char spare2[4]; /* More magic... */
742 unsigned short ilfact; /* Interleave factor */
743 unsigned short ncyl; /* Data cylinder count */
744 unsigned short nacyl; /* Alt. cylinder count */
745 unsigned short ntrks; /* Tracks per cylinder */
746 unsigned short nsect; /* Sectors per track */
747 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000748 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000749 uint32_t start_cylinder;
750 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000751 } partitions[8];
752 unsigned short magic; /* Magic number */
753 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100754} FIX_ALIASING;
755typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000756#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000757STATIC_OSF void bsd_select(void);
758STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000759#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000760
Denys Vlasenkod7559c22010-10-30 02:40:08 +0200761STATIC_GPT void gpt_list_table(int xtra);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200762#include "fdisk_gpt.c"
763
Denis Vlasenko28703012006-12-19 20:32:02 +0000764#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000765static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000766fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000767{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000768 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000769}
770
Rob Landley88621d72006-08-29 19:41:06 +0000771static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000772fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000773{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000774 return (x << 24) |
775 ((x & 0xFF00) << 8) |
776 ((x & 0xFF0000) >> 8) |
777 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778}
779#endif
780
Denis Vlasenkobd852072007-03-19 14:43:38 +0000781STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000782STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000783STATIC_SGI int sgi_get_sysid(int i);
784STATIC_SGI void sgi_delete_partition(int i);
785STATIC_SGI void sgi_change_sysid(int i, int sys);
786STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000787#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000788STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000789#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000790STATIC_SGI int verify_sgi(int verbose);
791STATIC_SGI void sgi_add_partition(int n, int sys);
792STATIC_SGI void sgi_set_swappartition(int i);
793STATIC_SGI const char *sgi_get_bootfile(void);
794STATIC_SGI void sgi_set_bootfile(const char* aFile);
795STATIC_SGI void create_sgiinfo(void);
796STATIC_SGI void sgi_write_table(void);
797STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000798#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000799
Denis Vlasenkobd852072007-03-19 14:43:38 +0000800STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000801STATIC_SUN void sun_delete_partition(int i);
802STATIC_SUN void sun_change_sysid(int i, int sys);
803STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000804STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000805#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000806STATIC_SUN void sun_set_alt_cyl(void);
807STATIC_SUN void sun_set_ncyl(int cyl);
808STATIC_SUN void sun_set_xcyl(void);
809STATIC_SUN void sun_set_ilfact(void);
810STATIC_SUN void sun_set_rspeed(void);
811STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000812#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000813STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
814STATIC_SUN void verify_sun(void);
815STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000816#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000817
Denis Vlasenko9764d692008-07-09 21:20:50 +0000818
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200819static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000820read4_little_endian(const unsigned char *cp)
821{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200822 uint32_t v;
823 move_from_unaligned32(v, cp);
824 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000825}
826
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200827static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000828get_start_sect(const struct partition *p)
829{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000830 return read4_little_endian(p->start4);
831}
832
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200833static sector_t
834get_nr_sects(const struct partition *p)
835{
836 return read4_little_endian(p->size4);
837}
838
Denis Vlasenko834410a2006-11-29 12:00:28 +0000839#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200840/* start_sect and nr_sects are stored little endian on all machines */
841/* moreover, they are not aligned correctly */
842static inline_if_little_endian void
843store4_little_endian(unsigned char *cp, unsigned val)
844{
845 uint32_t v = SWAP_LE32(val);
846 move_to_unaligned32(cp, v);
847}
848
849static void
850set_start_sect(struct partition *p, unsigned start_sect)
851{
852 store4_little_endian(p->start4, start_sect);
853}
854
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000855static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000856set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000857{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000858 store4_little_endian(p->size4, nr_sects);
859}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000860#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000862/* Allocate a buffer and read a partition table sector */
863static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200864read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000865{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200866 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000867 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000868 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000869 /* xread would make us abort - bad for fdisk -l */
870 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000871 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000872#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000873 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000874#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000875 pe->part_table = pe->ext_pointer = NULL;
876}
877
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200878static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200879get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000880{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200881 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000882}
883
Denis Vlasenko834410a2006-11-29 12:00:28 +0000884#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000885/*
886 * Avoid warning about DOS partitions when no DOS partition was changed.
887 * Here a heuristic "is probably dos partition".
888 * We might also do the opposite and warn in all cases except
889 * for "is probably nondos partition".
890 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000891#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000892static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000893is_dos_partition(int t)
894{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000895 return (t == 1 || t == 4 || t == 6 ||
896 t == 0x0b || t == 0x0c || t == 0x0e ||
897 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
898 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
899 t == 0xc1 || t == 0xc4 || t == 0xc6);
900}
Denis Vlasenko89398812008-01-25 20:18:46 +0000901#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902
903static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000904menu(void)
905{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000906 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000907 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000908 puts("a\ttoggle a read only flag"); /* sun */
909 puts("b\tedit bsd disklabel");
910 puts("c\ttoggle the mountable flag"); /* sun */
911 puts("d\tdelete a partition");
912 puts("l\tlist known partition types");
913 puts("n\tadd a new partition");
914 puts("o\tcreate a new empty DOS partition table");
915 puts("p\tprint the partition table");
916 puts("q\tquit without saving changes");
917 puts("s\tcreate a new empty Sun disklabel"); /* sun */
918 puts("t\tchange a partition's system id");
919 puts("u\tchange display/entry units");
920 puts("v\tverify the partition table");
921 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000922#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000923 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000925 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000926 puts("a\tselect bootable partition"); /* sgi flavour */
927 puts("b\tedit bootfile entry"); /* sgi */
928 puts("c\tselect sgi swap partition"); /* sgi flavour */
929 puts("d\tdelete a partition");
930 puts("l\tlist known partition types");
931 puts("n\tadd a new partition");
932 puts("o\tcreate a new empty DOS partition table");
933 puts("p\tprint the partition table");
934 puts("q\tquit without saving changes");
935 puts("s\tcreate a new empty Sun disklabel"); /* sun */
936 puts("t\tchange a partition's system id");
937 puts("u\tchange display/entry units");
938 puts("v\tverify the partition table");
939 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000940 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000941 puts("o\tcreate a new empty DOS partition table");
942 puts("q\tquit without saving changes");
943 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200944 } else if (LABEL_IS_GPT) {
945 puts("o\tcreate a new empty DOS partition table");
946 puts("p\tprint the partition table");
947 puts("q\tquit without saving changes");
948 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000950 puts("a\ttoggle a bootable flag");
951 puts("b\tedit bsd disklabel");
952 puts("c\ttoggle the dos compatibility flag");
953 puts("d\tdelete a partition");
954 puts("l\tlist known partition types");
955 puts("n\tadd a new partition");
956 puts("o\tcreate a new empty DOS partition table");
957 puts("p\tprint the partition table");
958 puts("q\tquit without saving changes");
959 puts("s\tcreate a new empty Sun disklabel"); /* sun */
960 puts("t\tchange a partition's system id");
961 puts("u\tchange display/entry units");
962 puts("v\tverify the partition table");
963 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000964#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000965 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000966#endif
967 }
968}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000969#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000970
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971
Denis Vlasenko834410a2006-11-29 12:00:28 +0000972#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000974xmenu(void)
975{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000976 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000977 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000978 puts("a\tchange number of alternate cylinders"); /*sun*/
979 puts("c\tchange number of cylinders");
980 puts("d\tprint the raw data in the partition table");
981 puts("e\tchange number of extra sectors per cylinder");/*sun*/
982 puts("h\tchange number of heads");
983 puts("i\tchange interleave factor"); /*sun*/
984 puts("o\tchange rotation speed (rpm)"); /*sun*/
985 puts("p\tprint the partition table");
986 puts("q\tquit without saving changes");
987 puts("r\treturn to main menu");
988 puts("s\tchange number of sectors/track");
989 puts("v\tverify the partition table");
990 puts("w\twrite table to disk and exit");
991 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000992 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000993 puts("b\tmove beginning of data in a partition"); /* !sun */
994 puts("c\tchange number of cylinders");
995 puts("d\tprint the raw data in the partition table");
996 puts("e\tlist extended partitions"); /* !sun */
997 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
998 puts("h\tchange number of heads");
999 puts("p\tprint the partition table");
1000 puts("q\tquit without saving changes");
1001 puts("r\treturn to main menu");
1002 puts("s\tchange number of sectors/track");
1003 puts("v\tverify the partition table");
1004 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001005 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001006 puts("b\tmove beginning of data in a partition"); /* !sun */
1007 puts("c\tchange number of cylinders");
1008 puts("d\tprint the raw data in the partition table");
1009 puts("e\tlist extended partitions"); /* !sun */
1010 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
1011 puts("h\tchange number of heads");
1012 puts("p\tprint the partition table");
1013 puts("q\tquit without saving changes");
1014 puts("r\treturn to main menu");
1015 puts("s\tchange number of sectors/track");
1016 puts("v\tverify the partition table");
1017 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001018 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001019 puts("b\tmove beginning of data in a partition"); /* !sun */
1020 puts("c\tchange number of cylinders");
1021 puts("d\tprint the raw data in the partition table");
1022 puts("e\tlist extended partitions"); /* !sun */
1023 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001024#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +00001025 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001027 puts("h\tchange number of heads");
1028 puts("p\tprint the partition table");
1029 puts("q\tquit without saving changes");
1030 puts("r\treturn to main menu");
1031 puts("s\tchange number of sectors/track");
1032 puts("v\tverify the partition table");
1033 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001034 }
1035}
1036#endif /* ADVANCED mode */
1037
Denis Vlasenko834410a2006-11-29 12:00:28 +00001038#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001039static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +00001040get_sys_types(void)
1041{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001043 LABEL_IS_SUN ? sun_sys_types :
1044 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045 i386_sys_types);
1046}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001047#else
1048#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001049#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050
Denis Vlasenkobd852072007-03-19 14:43:38 +00001051static const char *
1052partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001053{
1054 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 for (i = 0; types[i]; i++)
1058 if ((unsigned char)types[i][0] == type)
1059 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060
Denis Vlasenkobd852072007-03-19 14:43:38 +00001061 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001062}
1063
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001064static int
1065is_cleared_partition(const struct partition *p)
1066{
1067 /* We consider partition "cleared" only if it has only zeros */
1068 const char *cp = (const char *)p;
1069 int cnt = sizeof(*p);
1070 char bits = 0;
1071 while (--cnt >= 0)
1072 bits |= *cp++;
1073 return (bits == 0);
1074}
1075
1076static void
1077clear_partition(struct partition *p)
1078{
1079 if (p)
1080 memset(p, 0, sizeof(*p));
1081}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001082
Denis Vlasenko834410a2006-11-29 12:00:28 +00001083#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001084static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001085get_sysid(int i)
1086{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001087 return LABEL_IS_SUN ? sunlabel->infos[i].id :
1088 (LABEL_IS_SGI ? sgi_get_sysid(i) :
1089 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090}
1091
Denis Vlasenkobd852072007-03-19 14:43:38 +00001092static void
1093list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001095 enum { COLS = 3 };
1096
1097 unsigned last[COLS];
1098 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001099 int i;
1100
Denis Vlasenko9764d692008-07-09 21:20:50 +00001101 for (size = 0; sys[size]; size++)
1102 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001103
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001104 done = 0;
1105 for (i = COLS-1; i >= 0; i--) {
1106 done += (size + i - done) / (i + 1);
1107 last[COLS-1 - i] = done;
1108 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001110 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001111 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001112 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001113 (unsigned char)sys[next][0],
1114 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001116 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001117 i = 0;
1118 next = ++done;
1119 }
1120 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001121 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122}
1123
Denys Vlasenkod958e902010-04-06 02:32:26 +02001124#define set_hsc(h, s, c, sector) do \
1125{ \
1126 s = sector % g_sectors + 1; \
1127 sector /= g_sectors; \
1128 h = sector % g_heads; \
1129 sector /= g_heads; \
1130 c = sector & 0xff; \
1131 s |= (sector >> 2) & 0xc0; \
1132} while (0)
1133
1134static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1135{
1136 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1137 start = g_heads * g_sectors * 1024 - 1;
1138 set_hsc(p->head, p->sector, p->cyl, start);
1139
1140 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1141 stop = g_heads * g_sectors * 1024 - 1;
1142 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1143}
1144
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001145static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001146set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001147{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001149 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001150
1151 if (doext) {
1152 p = ptes[i].ext_pointer;
1153 offset = extended_offset;
1154 } else {
1155 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001156 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001157 }
1158 p->boot_ind = 0;
1159 p->sys_ind = sysid;
1160 set_start_sect(p, start - offset);
1161 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001162 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001163 ptes[i].changed = 1;
1164}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001165#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001166
1167static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001168warn_geometry(void)
1169{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001170 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001172
Denis Vlasenkobd852072007-03-19 14:43:38 +00001173 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001174 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001175 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001176 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001177 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001178 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001179 printf(" cylinders");
Denis Vlasenko834410a2006-11-29 12:00:28 +00001180#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001181 puts(" (settable in the extra functions menu)");
1182#else
1183 bb_putchar('\n');
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001184#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185 return 1;
1186}
1187
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001188static void
1189update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001190{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001191 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001192
1193 if (display_in_cyl_units && cyl_units)
1194 units_per_sector = cyl_units;
1195 else
1196 units_per_sector = 1; /* in sectors */
1197}
1198
Denis Vlasenko834410a2006-11-29 12:00:28 +00001199#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001200static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001201warn_cylinders(void)
1202{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001203 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001204 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001205"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206"There is nothing wrong with that, but this is larger than 1024,\n"
1207"and could in certain setups cause problems with:\n"
1208"1) software that runs at boot time (e.g., old versions of LILO)\n"
1209"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001210" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001211 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001213#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001214
1215static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001216read_extended(int ext)
1217{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001218 int i;
1219 struct pte *pex;
1220 struct partition *p, *q;
1221
1222 ext_index = ext;
1223 pex = &ptes[ext];
1224 pex->ext_pointer = pex->part_table;
1225
1226 p = pex->part_table;
1227 if (!get_start_sect(p)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001228 puts("Bad offset in primary extended partition");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229 return;
1230 }
1231
Rob Landleyb73451d2006-02-24 16:29:00 +00001232 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001233 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001234
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001235 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236 /* This is not a Linux restriction, but
1237 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001238 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001239 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001240#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001241 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001242 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001244#endif
1245 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001246 return;
1247 }
1248
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001249 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250
1251 if (!extended_offset)
1252 extended_offset = get_start_sect(p);
1253
1254 q = p = pt_offset(pe->sectorbuffer, 0);
1255 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001256 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001258 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001259 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001260 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001261 else
1262 pe->ext_pointer = p;
1263 } else if (p->sys_ind) {
1264 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001265 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001266 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001267 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001268 else
1269 pe->part_table = p;
1270 }
1271 }
1272
1273 /* very strange code here... */
1274 if (!pe->part_table) {
1275 if (q != pe->ext_pointer)
1276 pe->part_table = q;
1277 else
1278 pe->part_table = q + 1;
1279 }
1280 if (!pe->ext_pointer) {
1281 if (q != pe->part_table)
1282 pe->ext_pointer = q;
1283 else
1284 pe->ext_pointer = q + 1;
1285 }
1286
1287 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001288 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289 }
1290
Denis Vlasenko834410a2006-11-29 12:00:28 +00001291#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292 /* remove empty links */
1293 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001294 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 struct pte *pe = &ptes[i];
1296
Denis Vlasenkobd852072007-03-19 14:43:38 +00001297 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001298 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001299 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001300 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001301 delete_partition(i);
1302 goto remove; /* numbering changed */
1303 }
1304 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001305#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001306}
1307
Denis Vlasenko834410a2006-11-29 12:00:28 +00001308#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001310create_doslabel(void)
1311{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001312 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001313
Denis Vlasenko4437d192008-04-17 00:12:10 +00001314 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001315#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001316 possibly_osf_label = 0;
1317#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001318 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001320 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001321 write_part_table_flag(MBRbuffer);
1322 extended_offset = 0;
1323 set_all_unchanged();
1324 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001325 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001326}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001327#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001328
1329static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001330get_sectorsize(void)
1331{
Rob Landley736e5252006-02-25 03:36:00 +00001332 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001333 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001334 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001335 sector_size = arg;
1336 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001337 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001338 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1339 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001340 }
1341}
1342
Rob Landley88621d72006-08-29 19:41:06 +00001343static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001344get_kernel_geometry(void)
1345{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 struct hd_geometry geometry;
1347
Denis Vlasenko4437d192008-04-17 00:12:10 +00001348 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001349 kern_heads = geometry.heads;
1350 kern_sectors = geometry.sectors;
1351 /* never use geometry.cylinders - it is truncated */
1352 }
1353}
1354
1355static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001356get_partition_table_geometry(void)
1357{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001358 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 struct partition *p;
1360 int i, h, s, hh, ss;
1361 int first = 1;
1362 int bad = 0;
1363
Eric Andersen3496fdc2006-01-30 23:09:20 +00001364 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 return;
1366
1367 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001368 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 p = pt_offset(bufp, i);
1370 if (p->sys_ind != 0) {
1371 h = p->end_head + 1;
1372 s = (p->end_sector & 077);
1373 if (first) {
1374 hh = h;
1375 ss = s;
1376 first = 0;
1377 } else if (hh != h || ss != s)
1378 bad = 1;
1379 }
1380 }
1381
1382 if (!first && !bad) {
1383 pt_heads = hh;
1384 pt_sectors = ss;
1385 }
1386}
1387
Rob Landleyb73451d2006-02-24 16:29:00 +00001388static void
1389get_geometry(void)
1390{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001392
1393 get_sectorsize();
1394 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001395#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001396 guess_device_type();
1397#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001398 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399 kern_heads = kern_sectors = 0;
1400 pt_heads = pt_sectors = 0;
1401
1402 get_kernel_geometry();
1403 get_partition_table_geometry();
1404
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001405 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406 pt_heads ? pt_heads :
1407 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001408 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409 pt_sectors ? pt_sectors :
1410 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001411 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001412
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413 sector_offset = 1;
1414 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001415 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001417 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1418 if (!g_cylinders)
1419 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420}
1421
1422/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001423 * Opens disk_device and optionally reads MBR.
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001424 * If what == OPEN_MAIN:
1425 * Open device, read MBR. Abort program on short read. Create empty
1426 * disklabel if the on-disk structure is invalid (WRITABLE mode).
1427 * If what == TRY_ONLY:
1428 * Open device, read MBR. Return an error if anything is out of place.
1429 * Do not create an empty disklabel. This is used for the "list"
1430 * operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1431 * If what == CREATE_EMPTY_*:
1432 * This means that get_boot() was called recursively from create_*label().
1433 * Do not re-open the device; just set up the ptes array and print
1434 * geometry warnings.
1435 *
Denis Vlasenko4437d192008-04-17 00:12:10 +00001436 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001437 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1438 * 0: found or created label
1439 * 1: I/O error
1440 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001441#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1442static int get_boot(enum action what)
1443#else
1444static int get_boot(void)
1445#define get_boot(what) get_boot()
1446#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001447{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001448 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001449
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001450 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001451 for (i = 0; i < 4; i++) {
1452 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001453 pe->part_table = pt_offset(MBRbuffer, i);
1454 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001455 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001456 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001457#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001458 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001459#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001460 }
1461
Denis Vlasenko834410a2006-11-29 12:00:28 +00001462#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001463// ALERT! highly idiotic design!
1464// We end up here when we call get_boot() recursively
1465// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1466// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1467// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1468// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001469 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001470 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471
Denis Vlasenko4437d192008-04-17 00:12:10 +00001472 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1473
Denis Vlasenkobd852072007-03-19 14:43:38 +00001474 if (fd < 0) {
1475 fd = open(disk_device, O_RDONLY);
1476 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001477 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001478 return 1;
1479 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001480 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001481 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001482 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001483 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001484 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001485 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001486 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001487 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001488 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 fdisk_fatal(unable_to_read);
1490 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001491#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001492 fd = open(disk_device, O_RDONLY);
1493 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001494 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001495 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001496 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001497 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001498 }
1499 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001500#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001502 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001503 update_units();
1504
Denis Vlasenko834410a2006-11-29 12:00:28 +00001505#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001506 if (check_sun_label())
1507 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001508#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001509#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001510 if (check_sgi_label())
1511 return 0;
1512#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001513#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001514 if (check_aix_label())
1515 return 0;
1516#endif
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001517#if ENABLE_FEATURE_GPT_LABEL
1518 if (check_gpt_label())
1519 return 0;
1520#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001521#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001522 if (check_osf_label()) {
1523 possibly_osf_label = 1;
1524 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001525 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001526 return 0;
1527 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001528 puts("This disk has both DOS and BSD magic.\n"
1529 "Give the 'b' command to go to BSD mode.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001530 }
1531#endif
1532
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001533#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001534 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001535 return -1;
1536#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001537 if (!valid_part_table_flag(MBRbuffer)) {
1538 if (what == OPEN_MAIN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001539 puts("Device contains neither a valid DOS "
1540 "partition table, nor Sun, SGI, OSF or GPT "
1541 "disklabel");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001542#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001543 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544#else
1545 create_doslabel();
1546#endif
1547 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001548 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001549 /* TRY_ONLY: */
1550 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001552 created_table:
1553#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554
Denis Vlasenko4437d192008-04-17 00:12:10 +00001555
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001556 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001557 warn_geometry();
1558
1559 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001560 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001561 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001562 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001563 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001564 else
1565 read_extended(i);
1566 }
1567 }
1568
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001569 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001572 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001573 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001574 pe->sectorbuffer[510],
1575 pe->sectorbuffer[511],
1576 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001577 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001578 }
1579 }
1580
1581 return 0;
1582}
1583
Denis Vlasenko834410a2006-11-29 12:00:28 +00001584#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585/*
1586 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1587 * If the user hits Enter, DFLT is returned.
1588 * Answers like +10 are interpreted as offsets from BASE.
1589 *
1590 * There is no default if DFLT is not between LOW and HIGH.
1591 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001592static sector_t
1593read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001595 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001597 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001599 if (dflt < low || dflt > high) {
1600 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001601 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001602 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603
1604 while (1) {
1605 int use_default = default_ok;
1606
1607 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001608 do {
1609 printf(fmt, mesg, low, high, dflt);
1610 read_maybe_empty("");
1611 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1612 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613
Eric Andersen84bdea82004-05-19 10:49:17 +00001614 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001615 int minus = (*line_ptr == '-');
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001616 unsigned scale_shift;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001617
Denys Vlasenko163e6372018-07-25 12:08:26 +02001618 if (sizeof(value) <= sizeof(long))
1619 value = strtoul(line_ptr + 1, NULL, 10);
1620 else
1621 value = strtoull(line_ptr + 1, NULL, 10);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001622
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001623 /* (1) if 2nd char is digit, use_default = 0.
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001624 * (2) move line_ptr to first non-digit.
1625 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001626 while (isdigit(*++line_ptr))
1627 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001628
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001629 scale_shift = 0;
1630 switch (*line_ptr | 0x20) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001631 case 'k':
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001632 scale_shift = 10; /* 1024 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001633 break;
Denys Vlasenko1ab38992018-07-25 14:42:53 +02001634/*
1635 * fdisk from util-linux 2.31 seems to round '+NNNk' and '+NNNK' to megabytes,
1636 * (512-byte) sector count of the partition does not equal NNN*2:
1637 *
1638 * Last sector, +sectors or +size{K,M,G,T,P} (1953792-1000215215, default 1000215215): +9727k
1639 * Device Boot Start End Sectors Size Id Type
1640 * /dev/sdaN 1953792 1972223 18432 9M 83 Linux <-- size exactly 9*1024*1024 bytes
1641 *
1642 * Last sector, +sectors or +size{K,M,G,T,P} (1953792-1000215215, default 1000215215): +9728k
1643 * /dev/sdaN 1953792 1974271 20480 10M 83 Linux <-- size exactly 10*1024*1024 bytes
1644 *
1645 * If 'k' means 1000 bytes (not 1024), then 9728k = 9728*1000 = 9500*1024,
1646 * exactly halfway from 9000 to 10000, which explains why it jumps to next mbyte
1647 * at this value.
1648 *
1649 * 'm' does not seem to behave this way: it means 1024*1024 bytes.
1650 *
1651 * Not sure we want to copy this. If user says he wants 1234kbyte partition,
1652 * we do _exactly that_: 1234kbytes = 2468 sectors.
1653 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001654 case 'm':
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001655 scale_shift = 20; /* 1024*1024 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001656 break;
1657 case 'g':
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001658 scale_shift = 30; /* 1024*1024*1024 */
1659 break;
1660 case 't':
1661 scale_shift = 40; /* 1024*1024*1024*1024 */
Rob Landleyb73451d2006-02-24 16:29:00 +00001662 break;
1663 default:
1664 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001665 }
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001666 if (scale_shift) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001667 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001668 unsigned long unit;
1669
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001670 bytes = (ullong) value << scale_shift;
Rob Landleyb73451d2006-02-24 16:29:00 +00001671 unit = sector_size * units_per_sector;
1672 bytes += unit/2; /* round */
1673 bytes /= unit;
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001674 value = (bytes != 0 ? bytes - 1 : 0);
Rob Landleyb73451d2006-02-24 16:29:00 +00001675 }
1676 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001677 value = -value;
1678 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001679 } else {
Denys Vlasenkoc1045492018-07-25 13:45:36 +02001680 if (sizeof(value) <= sizeof(long))
1681 value = strtoul(line_ptr, NULL, 10);
1682 else
1683 value = strtoull(line_ptr, NULL, 10);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001684 while (isdigit(*line_ptr)) {
1685 line_ptr++;
1686 use_default = 0;
1687 }
1688 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001689 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001690 value = dflt;
1691 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001692 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001693 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001694 break;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001695 puts("Value is out of range");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001696 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001697 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001698}
1699
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001700static unsigned
1701get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001702{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001703 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001704 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001705
Denis Vlasenkobd852072007-03-19 14:43:38 +00001706 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001707 pe = &ptes[i];
1708
1709 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001710 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1711 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1712 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1713 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001714 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001715 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001716 }
1717 return i;
1718}
1719
1720static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001721get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001722{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001723 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001724 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001725
1726 for (i = 0; i < max; i++) {
1727 struct pte *pe = &ptes[i];
1728 struct partition *p = pe->part_table;
1729
1730 if (p && !is_cleared_partition(p)) {
1731 if (pno >= 0)
1732 goto not_unique;
1733 pno = i;
1734 }
1735 }
1736 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001737 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001738 return pno;
1739 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001740 puts("No partition is defined yet!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001741 return -1;
1742
1743 not_unique:
1744 return get_partition(warn, max);
1745}
1746
1747static int
Denys Vlasenko1ab38992018-07-25 14:42:53 +02001748get_nonexisting_partition(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00001749{
Denys Vlasenko1ab38992018-07-25 14:42:53 +02001750 const int max = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001751 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001752 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001753
1754 for (i = 0; i < max; i++) {
1755 struct pte *pe = &ptes[i];
1756 struct partition *p = pe->part_table;
1757
1758 if (p && is_cleared_partition(p)) {
1759 if (pno >= 0)
1760 goto not_unique;
1761 pno = i;
1762 }
1763 }
1764 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001765 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001766 return pno;
1767 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001768 puts("All primary partitions have been defined already!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001769 return -1;
1770
1771 not_unique:
Denys Vlasenko1ab38992018-07-25 14:42:53 +02001772 return get_partition(/*warn*/ 0, max);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001773}
1774
1775
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001776static void
1777change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001778{
1779 display_in_cyl_units = !display_in_cyl_units;
1780 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001781 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001782 str_units(PLURAL));
1783}
1784
1785static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001786toggle_active(int i)
1787{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001788 struct pte *pe = &ptes[i];
1789 struct partition *p = pe->part_table;
1790
Rob Landleyb73451d2006-02-24 16:29:00 +00001791 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001792 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001793 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1794 pe->changed = 1;
1795}
1796
1797static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001798toggle_dos_compatibility_flag(void)
1799{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001800 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001801 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001802 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001803 printf("DOS Compatibility flag is %sset\n", "");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001804 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 sector_offset = 1;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001806 printf("DOS Compatibility flag is %sset\n", "not ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001807 }
1808}
1809
1810static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001811delete_partition(int i)
1812{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001813 struct pte *pe = &ptes[i];
1814 struct partition *p = pe->part_table;
1815 struct partition *q = pe->ext_pointer;
1816
1817/* Note that for the fifth partition (i == 4) we don't actually
1818 * decrement partitions.
1819 */
1820
1821 if (warn_geometry())
1822 return; /* C/H/S not set */
1823 pe->changed = 1;
1824
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001825 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001826 sun_delete_partition(i);
1827 return;
1828 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001829 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001830 sgi_delete_partition(i);
1831 return;
1832 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001833
1834 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001835 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001836 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001837 ptes[ext_index].ext_pointer = NULL;
1838 extended_offset = 0;
1839 }
1840 clear_partition(p);
1841 return;
1842 }
1843
1844 if (!q->sys_ind && i > 4) {
1845 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001846 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001847 --i;
1848 clear_partition(ptes[i].ext_pointer);
1849 ptes[i].changed = 1;
1850 } else {
1851 /* not the last one - further ones will be moved down */
1852 if (i > 4) {
1853 /* delete this link in the chain */
1854 p = ptes[i-1].ext_pointer;
1855 *p = *q;
1856 set_start_sect(p, get_start_sect(q));
1857 set_nr_sects(p, get_nr_sects(q));
1858 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001859 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001860 /* the first logical in a longer chain */
1861 pe = &ptes[5];
1862
1863 if (pe->part_table) /* prevent SEGFAULT */
1864 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001865 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001866 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001867 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001868 pe->changed = 1;
1869 }
1870
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001871 if (g_partitions > 5) {
1872 g_partitions--;
1873 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001874 ptes[i] = ptes[i+1];
1875 i++;
1876 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001877 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001878 /* the only logical: clear only */
1879 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001880 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001881 }
1882}
1883
1884static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001885change_sysid(void)
1886{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001887 int i, sys, origsys;
1888 struct partition *p;
1889
Eric Andersen040f4402003-07-30 08:40:37 +00001890 /* If sgi_label then don't use get_existing_partition,
1891 let the user select a partition, since get_existing_partition()
1892 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001893 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001894 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001895 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001896 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001897 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001898 if (i == -1)
1899 return;
1900 p = ptes[i].part_table;
1901 origsys = sys = get_sysid(i);
1902
1903 /* if changing types T to 0 is allowed, then
1904 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001905 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001906 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001907 return;
1908 }
1909 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001910 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001911
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001912 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001913 puts("Type 0 means free space to many systems\n"
1914 "(but not to Linux). Having partitions of\n"
1915 "type 0 is probably unwise.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001916 /* break; */
1917 }
1918
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001919 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001920 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001921 puts("You cannot change a partition into"
1922 " an extended one or vice versa");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001923 break;
1924 }
1925 }
1926
1927 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001928#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001929 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001930 puts("Consider leaving partition 3 "
1931 "as Whole disk (5),\n"
1932 "as SunOS/Solaris expects it and "
1933 "even Linux likes it\n");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001934#endif
1935#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001936 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001937 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001938 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001939 (i == 8 && sys != 0)
1940 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001941 ) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001942 puts("Consider leaving partition 9 "
1943 "as volume header (0),\nand "
1944 "partition 11 as entire volume (6)"
1945 "as IRIX expects it\n");
Rob Landley5527b912006-02-25 03:46:10 +00001946 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001947#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001948 if (sys == origsys)
1949 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001950 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001951 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001952 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001953 sgi_change_sysid(i, sys);
1954 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001955 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001956
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001957 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001958 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001959 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001960 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001961 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1962 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001963 break;
1964 }
1965 }
1966}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001967#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001968
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001969
Denis Vlasenko28703012006-12-19 20:32:02 +00001970/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001971 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1972 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1973 * Lubkin Oct. 1991). */
1974
Rob Landleyb73451d2006-02-24 16:29:00 +00001975static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001976linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001977{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001978 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001979
1980 *c = ls / spc;
1981 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001982 *h = ls / g_sectors;
1983 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001984}
1985
Rob Landleyb73451d2006-02-24 16:29:00 +00001986static void
1987check_consistency(const struct partition *p, int partition)
1988{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001989 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1990 unsigned pec, peh, pes; /* physical ending c, h, s */
1991 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1992 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001993
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001994 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001995 return; /* do not check extended partitions */
1996
1997/* physical beginning c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001998 pbc = cylinder(p->sector, p->cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001999 pbh = p->head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002000 pbs = sector(p->sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002001
2002/* physical ending c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002003 pec = cylinder(p->end_sector, p->end_cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002004 peh = p->end_head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002005 pes = sector(p->end_sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006
2007/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00002008 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002009
2010/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00002011 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012
2013/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002014 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002015 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002016 "start (non-Linux?):\n", partition + 1);
2017 printf(" phys=(%u,%u,%u) ", pbc, pbh, pbs);
2018 printf("logical=(%u,%u,%u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002019 }
2020
2021/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002022 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002023 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002024 "end:\n", partition + 1);
2025 printf(" phys=(%u,%u,%u) ", pec, peh, pes);
2026 printf("logical=(%u,%u,%u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002027 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002028}
2029
2030static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002031list_disk_geometry(void)
2032{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002033 ullong bytes = ((ullong)total_number_of_sectors << 9);
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002034 ullong xbytes = bytes / (1024*1024);
2035 char x = 'M';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002036
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002037 if (xbytes >= 10000) {
2038 xbytes += 512; /* fdisk util-linux 2.28 does this */
2039 xbytes /= 1024;
2040 x = 'G';
2041 }
2042 printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
2043 "%u cylinders, %u heads, %u sectors/track\n"
2044 "Units: %s of %u * %u = %u bytes\n\n",
2045 disk_device, xbytes, x,
2046 bytes, total_number_of_sectors,
2047 g_cylinders, g_heads, g_sectors,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002048 str_units(PLURAL),
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002049 units_per_sector, sector_size, units_per_sector * sector_size
2050 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002051}
2052
2053/*
2054 * Check whether partition entries are ordered by their starting positions.
2055 * Return 0 if OK. Return i if partition i should have been earlier.
2056 * Two separate checks: primary and logical partitions.
2057 */
2058static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002059wrong_p_order(int *prev)
2060{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002061 const struct pte *pe;
2062 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002063 sector_t last_p_start_pos = 0, p_start_pos;
2064 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002065
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002066 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002067 if (i == 4) {
2068 last_i = 4;
2069 last_p_start_pos = 0;
2070 }
2071 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00002072 p = pe->part_table;
2073 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002074 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002075
2076 if (last_p_start_pos > p_start_pos) {
2077 if (prev)
2078 *prev = last_i;
2079 return i;
2080 }
2081
2082 last_p_start_pos = p_start_pos;
2083 last_i = i;
2084 }
2085 }
2086 return 0;
2087}
2088
Denis Vlasenko834410a2006-11-29 12:00:28 +00002089#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090/*
2091 * Fix the chain of logicals.
2092 * extended_offset is unchanged, the set of sectors used is unchanged
2093 * The chain is sorted so that sectors increase, and so that
2094 * starting sectors increase.
2095 *
2096 * After this it may still be that cfdisk doesnt like the table.
2097 * (This is because cfdisk considers expanded parts, from link to
2098 * end of partition, and these may still overlap.)
2099 * Now
2100 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
2101 * may help.
2102 */
2103static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002104fix_chain_of_logicals(void)
2105{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002106 int j, oj, ojj, sj, sjj;
2107 struct partition *pj,*pjj,tmp;
2108
2109 /* Stage 1: sort sectors but leave sector of part 4 */
2110 /* (Its sector is the global extended_offset.) */
2111 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002112 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002113 oj = ptes[j].offset_from_dev_start;
2114 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002115 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002116 ptes[j].offset_from_dev_start = ojj;
2117 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 pj = ptes[j].part_table;
2119 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2120 pjj = ptes[j+1].part_table;
2121 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2122 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002123 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002124 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002125 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002126 goto stage1;
2127 }
2128 }
2129
2130 /* Stage 2: sort starting sectors */
2131 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002132 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002133 pj = ptes[j].part_table;
2134 pjj = ptes[j+1].part_table;
2135 sj = get_start_sect(pj);
2136 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002137 oj = ptes[j].offset_from_dev_start;
2138 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002139 if (oj+sj > ojj+sjj) {
2140 tmp = *pj;
2141 *pj = *pjj;
2142 *pjj = tmp;
2143 set_start_sect(pj, ojj+sjj-oj);
2144 set_start_sect(pjj, oj+sj-ojj);
2145 goto stage2;
2146 }
2147 }
2148
2149 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002150 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002151 ptes[j].changed = 1;
2152}
2153
2154
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002155static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002156fix_partition_table_order(void)
2157{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002158 struct pte *pei, *pek;
2159 int i,k;
2160
2161 if (!wrong_p_order(NULL)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002162 puts("Ordering is already correct\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002163 return;
2164 }
2165
2166 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2167 /* partition i should have come earlier, move it */
2168 /* We have to move data in the MBR */
2169 struct partition *pi, *pk, *pe, pbuf;
2170 pei = &ptes[i];
2171 pek = &ptes[k];
2172
2173 pe = pei->ext_pointer;
2174 pei->ext_pointer = pek->ext_pointer;
2175 pek->ext_pointer = pe;
2176
2177 pi = pei->part_table;
2178 pk = pek->part_table;
2179
2180 memmove(&pbuf, pi, sizeof(struct partition));
2181 memmove(pi, pk, sizeof(struct partition));
2182 memmove(pk, &pbuf, sizeof(struct partition));
2183
2184 pei->changed = pek->changed = 1;
2185 }
2186
2187 if (i)
2188 fix_chain_of_logicals();
2189
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002190 puts("Done");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002191}
2192#endif
2193
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002194static const char *
2195chs_string11(unsigned cyl, unsigned head, unsigned sect)
2196{
2197 char *buf = auto_string(xzalloc(sizeof(int)*3 * 3));
2198 sprintf(buf, "%u,%u,%u", cylinder(sect,cyl), head, sector(sect));
2199 return buf;
2200}
2201
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002202static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002203list_table(int xtra)
2204{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002205 int i, w;
2206
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002207 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 sun_list_table(xtra);
2209 return;
2210 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002211 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002212 sgi_list_table(xtra);
2213 return;
2214 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002215 if (LABEL_IS_GPT) {
2216 gpt_list_table(xtra);
2217 return;
2218 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002219
2220 list_disk_geometry();
2221
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002222 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002223 xbsd_print_disklabel(xtra);
2224 return;
2225 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002226
2227 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002228 * but if the device name ends in a digit, say /dev/foo1,
2229 * then the partition is called /dev/foo1p3.
2230 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002231 w = strlen(disk_device);
2232 if (w && isdigit(disk_device[w-1]))
2233 w++;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002234 if (w < 7)
2235 w = 7;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002237 printf("%-*s Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type\n",
2238 w-1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002239
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002240 for (i = 0; i < g_partitions; i++) {
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002241 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 const struct pte *pe = &ptes[i];
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002243 char boot4[4];
2244 char numstr6[6];
2245 sector_t start_sect;
2246 sector_t end_sect;
2247 sector_t nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002248
2249 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002250 if (!p || is_cleared_partition(p))
2251 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002252
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002253 sprintf(boot4, "%02x", p->boot_ind);
2254 if ((p->boot_ind & 0x7f) == 0) {
2255 /* 0x80 shown as '*', 0x00 is ' ' */
2256 boot4[0] = p->boot_ind ? '*' : ' ';
2257 boot4[1] = ' ';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002258 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002259
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002260 start_sect = get_partition_start_from_dev_start(pe);
2261 end_sect = start_sect;
2262 nr_sects = get_nr_sects(p);
2263 if (nr_sects != 0)
2264 end_sect += nr_sects - 1;
2265
2266 smart_ulltoa5((ullong)nr_sects * sector_size,
2267 numstr6, " KMGTPEZY")[0] = '\0';
2268
2269#define SFMT SECT_FMT
2270 // Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
2271 printf("%s%s %-11s"/**/" %-11s"/**/" %10"SFMT"u %10"SFMT"u %10"SFMT"u %s %2x %s\n",
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002272 partname(disk_device, i+1, w+2),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002273 boot4,
2274 chs_string11(p->cyl, p->head, p->sector),
2275 chs_string11(p->end_cyl, p->end_head, p->end_sector),
2276 start_sect,
2277 end_sect,
2278 nr_sects,
2279 numstr6,
2280 p->sys_ind,
2281 partition_type(p->sys_ind)
2282 );
2283#undef SFMT
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002284 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002285 }
2286
2287 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002288 /* partition table entries are not checked for correct order
2289 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002290 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002291 /* FIXME */
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002292 puts("\nPartition table entries are not in disk order");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 }
2294}
2295
Denis Vlasenko834410a2006-11-29 12:00:28 +00002296#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002297static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002298x_list_table(int extend)
2299{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002300 const struct pte *pe;
2301 const struct partition *p;
2302 int i;
2303
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002304 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002305 disk_device, g_heads, g_sectors, g_cylinders);
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002306 puts("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002307 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002308 pe = &ptes[i];
2309 p = (extend ? pe->ext_pointer : pe->part_table);
2310 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002311 printf("%2u %02x%4u%4u%5u%4u%4u%5u%11"SECT_FMT"u%11"SECT_FMT"u %02x\n",
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002312 i + 1, p->boot_ind,
2313 p->head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002314 sector(p->sector),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002315 cylinder(p->sector, p->cyl),
2316 p->end_head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002317 sector(p->end_sector),
2318 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002319 get_start_sect(p),
2320 get_nr_sects(p),
2321 p->sys_ind
2322 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323 if (p->sys_ind)
2324 check_consistency(p, i);
2325 }
2326 }
2327}
2328#endif
2329
Denis Vlasenko834410a2006-11-29 12:00:28 +00002330#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002331static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002332fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002333{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002334 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002335 const struct pte *pe = &ptes[0];
2336 const struct partition *p;
2337
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002338 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002339 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002340 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002341 first[i] = 0xffffffff;
2342 last[i] = 0;
2343 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002344 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002345 last[i] = first[i] + get_nr_sects(p) - 1;
2346 }
2347 }
2348}
2349
2350static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002351check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002352{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002353 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002354
2355 real_s = sector(s) - 1;
2356 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002357 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002358 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002359 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002360 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002361 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002362 n, h + 1, g_heads);
2363 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002364 printf("Partition %u: sector %u greater than "
2365 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002366 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002367 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2368 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002369 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002370 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2371 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372}
2373
2374static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002375verify(void)
2376{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002377 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002378 sector_t total = 1;
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002379 sector_t chs_size;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002380 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 struct partition *p;
2382
2383 if (warn_geometry())
2384 return;
2385
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002386 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002387 verify_sun();
2388 return;
2389 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002390 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002391 verify_sgi(1);
2392 return;
2393 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002394
2395 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002396 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002397 struct pte *pe = &ptes[i];
2398
2399 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002400 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002401 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002402 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002403 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002404 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002405 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2406 last[i]);
2407 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002408 for (j = 0; j < i; j++) {
2409 if ((first[i] >= first[j] && first[i] <= last[j])
2410 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002411 printf("Warning: partition %u overlaps "
2412 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002413 total += first[i] >= first[j] ?
2414 first[i] : first[j];
2415 total -= last[i] <= last[j] ?
2416 last[i] : last[j];
2417 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002418 }
2419 }
2420 }
2421
2422 if (extended_offset) {
2423 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002424 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002425 get_nr_sects(pex->part_table) - 1;
2426
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002427 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002428 total++;
2429 p = ptes[i].part_table;
2430 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002431 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002432 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002433 "is empty\n", i + 1);
2434 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002435 printf("Logical partition %u not entirely in "
2436 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002437 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002438 }
2439 }
2440
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002441 chs_size = (sector_t)g_heads * g_sectors * g_cylinders;
2442 if (total > chs_size)
2443 printf("Total allocated sectors %u"
2444 " greater than CHS size %"SECT_FMT"u\n",
2445 total, chs_size
2446 );
Denis Vlasenkobd852072007-03-19 14:43:38 +00002447 else {
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002448 total = chs_size - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002449 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002450 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002451 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452}
2453
2454static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002455add_partition(int n, int sys)
2456{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002457 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002458 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 struct partition *p = ptes[n].part_table;
2460 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002461 sector_t limit, temp;
2462 sector_t start, stop = 0;
2463 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002464
2465 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002466 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002467 return;
2468 }
2469 fill_bounds(first, last);
2470 if (n < 4) {
2471 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002472 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002473 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002475 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002476 if (extended_offset) {
2477 first[ext_index] = extended_offset;
2478 last[ext_index] = get_start_sect(q) +
2479 get_nr_sects(q) - 1;
2480 }
2481 } else {
2482 start = extended_offset + sector_offset;
2483 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2484 }
2485 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002486 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002487 first[i] = (cround(first[i]) - 1) * units_per_sector;
2488
Denis Vlasenkobd852072007-03-19 14:43:38 +00002489 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002490 do {
2491 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002492 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002493 int lastplusoff;
2494
Denys Vlasenkod958e902010-04-06 02:32:26 +02002495 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002496 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002497 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498 if (start >= first[i] && start <= lastplusoff)
2499 start = lastplusoff + 1;
2500 }
2501 if (start > limit)
2502 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002503 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002504 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002506 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002507 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002508 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002509 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002510
2511 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002512 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002513 if (display_in_cyl_units) {
2514 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002515 if (start < saved_start)
2516 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002517 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002518 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002519 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002520 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002521 if (n > 4) { /* NOT for fifth partition */
2522 struct pte *pe = &ptes[n];
2523
Denys Vlasenkod958e902010-04-06 02:32:26 +02002524 pe->offset_from_dev_start = start - sector_offset;
2525 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2526 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 if (sector_offset == 1)
2528 start++;
2529 }
2530 }
2531
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002532 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002533 struct pte *pe = &ptes[i];
2534
Denys Vlasenkod958e902010-04-06 02:32:26 +02002535 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2536 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 if (start < first[i] && limit >= first[i])
2538 limit = first[i] - 1;
2539 }
2540 if (start > limit) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002541 puts("No free sectors available");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002542 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002543 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002544 return;
2545 }
2546 if (cround(start) == cround(limit)) {
2547 stop = limit;
2548 } else {
2549 snprintf(mesg, sizeof(mesg),
Denys Vlasenkoc1045492018-07-25 13:45:36 +02002550 "Last %s or +size{,K,M,G,T}",
2551 str_units(SINGULAR)
2552 );
Denys Vlasenkod958e902010-04-06 02:32:26 +02002553 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002554 if (display_in_cyl_units) {
2555 stop = stop * units_per_sector - 1;
2556 if (stop >limit)
2557 stop = limit;
2558 }
2559 }
2560
2561 set_partition(n, 0, start, stop, sys);
2562 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002563 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002564
Rob Landleyb73451d2006-02-24 16:29:00 +00002565 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002566 struct pte *pe4 = &ptes[4];
2567 struct pte *pen = &ptes[n];
2568
2569 ext_index = n;
2570 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002571 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002572 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2574 pe4->ext_pointer = pe4->part_table + 1;
2575 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002576 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577 }
2578}
2579
2580static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002581add_logical(void)
2582{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002583 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2584 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002585
Rob Landley081e3842006-08-03 20:07:35 +00002586 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002587 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2588 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002589 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002591 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002592 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002593 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002594}
2595
2596static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002597new_partition(void)
2598{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 int i, free_primary = 0;
2600
2601 if (warn_geometry())
2602 return;
2603
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002604 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002605 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002606 return;
2607 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002608 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002609 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 return;
2611 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002612 if (LABEL_IS_AIX) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002613 puts("Sorry - this fdisk cannot handle AIX disk labels.\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002614"If you want to add DOS-type partitions, create a new empty DOS partition\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002615"table first (use 'o'). This will destroy the present disk contents.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 return;
2617 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002618
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002619 for (i = 0; i < 4; i++)
2620 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002621
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002622 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002623 puts("The maximum number of partitions has been created");
Eric Andersen84bdea82004-05-19 10:49:17 +00002624 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002625 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002626
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 if (!free_primary) {
2628 if (extended_offset)
2629 add_logical();
2630 else
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002631 puts("You must delete some partition and add "
2632 "an extended partition first");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002634 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002635 snprintf(line, sizeof(line),
Denys Vlasenkoc1045492018-07-25 13:45:36 +02002636 "Partition type\n"
2637 " p primary partition (1-4)\n"
2638 " %s\n",
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002639 (extended_offset ?
2640 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002641 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002642 c = read_nonempty(line);
Denys Vlasenko1ab38992018-07-25 14:42:53 +02002643 c |= 0x20; /* lowercase */
2644 if (c == 'p') {
2645 i = get_nonexisting_partition();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002646 if (i >= 0)
2647 add_partition(i, LINUX_NATIVE);
2648 return;
2649 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002650 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002651 add_logical();
2652 return;
2653 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002654 if (c == 'e' && !extended_offset) {
Denys Vlasenko1ab38992018-07-25 14:42:53 +02002655 i = get_nonexisting_partition();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002656 if (i >= 0)
2657 add_partition(i, EXTENDED);
2658 return;
2659 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002660 printf("Invalid partition number "
2661 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002662 }
2663 }
2664}
2665
2666static void
Denys Vlasenkob347df92011-08-09 22:49:15 +02002667reread_partition_table(int leave)
2668{
2669 int i;
2670
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002671 puts("Calling ioctl() to re-read partition table");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002672 sync();
2673 /* Users with slow external USB disks on a 320MHz ARM system (year 2011)
2674 * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
2675 */
2676 sleep(1);
2677 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
2678 "WARNING: rereading partition table "
2679 "failed, kernel still uses old table");
2680#if 0
2681 if (dos_changed)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002682 puts(
Denys Vlasenkob347df92011-08-09 22:49:15 +02002683 "\nWARNING: If you have created or modified any DOS 6.x\n"
2684 "partitions, please see the fdisk manual page for additional\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002685 "information");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002686#endif
2687
2688 if (leave) {
2689 if (ENABLE_FEATURE_CLEAN_UP)
2690 close_dev_fd();
2691 exit(i != 0);
2692 }
2693}
2694
2695static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002696write_table(void)
2697{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002698 int i;
2699
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002700 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002701 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002702 if (ptes[i].changed)
2703 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002704 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002705 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706 if (pe->changed) {
2707 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002708 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709 }
2710 }
2711 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002712 else if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002713 /* no test on change? the "altered" msg below might be mistaken */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002714 sgi_write_table();
2715 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002716 else if (LABEL_IS_SUN) {
Denys Vlasenko80856b32011-07-13 09:06:32 +02002717 for (i = 0; i < 8; i++) {
2718 if (ptes[i].changed) {
2719 sun_write_table();
2720 break;
2721 }
2722 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002723 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002724
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002725 puts("The partition table has been altered.");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002726 reread_partition_table(1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002727}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002728#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002729
Denis Vlasenko834410a2006-11-29 12:00:28 +00002730#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002731#define MAX_PER_LINE 16
2732static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002733print_buffer(char *pbuffer)
2734{
2735 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002736
2737 for (i = 0, l = 0; i < sector_size; i++, l++) {
2738 if (l == 0)
2739 printf("0x%03X:", i);
2740 printf(" %02X", (unsigned char) pbuffer[i]);
2741 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002742 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743 l = -1;
2744 }
2745 }
2746 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002747 bb_putchar('\n');
2748 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002749}
2750
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002752print_raw(void)
2753{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002754 int i;
2755
Denis Vlasenkobd852072007-03-19 14:43:38 +00002756 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002757 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002758 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002759 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002760 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002761 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002762 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002763}
2764
2765static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002766move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002767{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002768 struct pte *pe = &ptes[i];
2769 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002770 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002771
2772 if (warn_geometry())
2773 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002774 nr_sects = get_nr_sects(p);
2775 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002776 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002777 return;
2778 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002779 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002780 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2781 if (new != first) {
2782 sector_t new_relative = new - pe->offset_from_dev_start;
2783 nr_sects += (get_start_sect(p) - new_relative);
2784 set_start_sect(p, new_relative);
2785 set_nr_sects(p, nr_sects);
2786 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2787 if ((line_ptr[0] | 0x20) == 'y')
2788 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002789 pe->changed = 1;
2790 }
2791}
2792
2793static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002794xselect(void)
2795{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002796 char c;
2797
Rob Landleyb73451d2006-02-24 16:29:00 +00002798 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002799 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002800 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002801 switch (c) {
2802 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002803 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002804 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002805 break;
2806 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002807 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002808 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 break;
2810 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002811 user_cylinders = g_cylinders =
2812 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002813 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002814 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002815 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002816 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002817 warn_cylinders();
2818 break;
2819 case 'd':
2820 print_raw();
2821 break;
2822 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002823 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002825 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002826 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002827 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 x_list_table(1);
2829 break;
2830 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002831 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 fix_partition_table_order();
2833 break;
2834 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002835#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002836 create_sgilabel();
2837#endif
2838 break;
2839 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002840 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002841 update_units();
2842 break;
2843 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002844 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002845 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002846 break;
2847 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002848 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002850 break;
2851 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002852 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 list_table(1);
2854 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002855 x_list_table(0);
2856 break;
2857 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002858 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002859 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002860 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002861 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002862 case 'r':
2863 return;
2864 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002865 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002866 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002867 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002868 puts("Warning: setting sector offset for DOS "
Denys Vlasenkoc52dc0e2017-08-10 15:12:11 +02002869 "compatibility");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002870 }
2871 update_units();
2872 break;
2873 case 'v':
2874 verify();
2875 break;
2876 case 'w':
2877 write_table(); /* does not return */
2878 break;
2879 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002880 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002881 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002882 break;
2883 default:
2884 xmenu();
2885 }
2886 }
2887}
2888#endif /* ADVANCED mode */
2889
2890static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002891is_ide_cdrom_or_tape(const char *device)
2892{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 FILE *procf;
2894 char buf[100];
2895 struct stat statbuf;
2896 int is_ide = 0;
2897
2898 /* No device was given explicitly, and we are trying some
2899 likely things. But opening /dev/hdc may produce errors like
2900 "hdc: tray open or drive not ready"
2901 if it happens to be a CD-ROM drive. It even happens that
2902 the process hangs on the attempt to read a music CD.
2903 So try to be careful. This only works since 2.1.73. */
2904
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002905 if (!is_prefixed_with(device, "/dev/hd"))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002906 return 0;
2907
2908 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002909 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 if (procf != NULL && fgets(buf, sizeof(buf), procf))
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002911 is_ide = (is_prefixed_with(buf, "cdrom") ||
2912 is_prefixed_with(buf, "tape"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002913 else
2914 /* Now when this proc file does not exist, skip the
2915 device when it is read-only. */
2916 if (stat(device, &statbuf) == 0)
2917 is_ide = ((statbuf.st_mode & 0222) == 0);
2918
2919 if (procf)
2920 fclose(procf);
2921 return is_ide;
2922}
2923
Rob Landley5527b912006-02-25 03:46:10 +00002924
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002925static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002926open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002927{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002928 int gb;
2929
2930 disk_device = device;
2931 if (setjmp(listingbuf))
2932 return;
2933 if (!user_specified)
2934 if (is_ide_cdrom_or_tape(device))
2935 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002936
2937 /* Open disk_device, save file descriptor to dev_fd */
2938 errno = 0;
2939 gb = get_boot(TRY_ONLY);
2940 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002941 /* Ignore other errors, since we try IDE
2942 and SCSI hard disks which may not be
2943 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002944 if (user_specified || errno == EACCES)
2945 bb_perror_msg("can't open '%s'", device);
2946 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002947 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002948
2949 if (gb < 0) { /* no DOS signature */
2950 list_disk_geometry();
2951 if (LABEL_IS_AIX)
2952 goto ret;
2953#if ENABLE_FEATURE_OSF_LABEL
2954 if (bsd_trydev(device) < 0)
2955#endif
2956 printf("Disk %s doesn't contain a valid "
2957 "partition table\n", device);
2958 } else {
2959 list_table(0);
2960#if ENABLE_FEATURE_FDISK_WRITABLE
2961 if (!LABEL_IS_SUN && g_partitions > 4) {
2962 delete_partition(ext_index);
2963 }
2964#endif
2965 }
2966 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002967 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002968}
2969
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002970/* Is it a whole disk? The digit check is still useful
2971 for Xen devices for example. */
2972static int is_whole_disk(const char *disk)
2973{
2974 unsigned len;
2975 int fd = open(disk, O_RDONLY);
2976
2977 if (fd != -1) {
2978 struct hd_geometry geometry;
2979 int err = ioctl(fd, HDIO_GETGEO, &geometry);
2980 close(fd);
2981 if (!err)
2982 return (geometry.start == 0);
2983 }
2984
2985 /* Treat "nameN" as a partition name, not whole disk */
2986 /* note: mmcblk0 should work from the geometry check above */
2987 len = strlen(disk);
2988 if (len != 0 && isdigit(disk[len - 1]))
2989 return 0;
2990
2991 return 1;
2992}
2993
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002994/* for fdisk -l: try all things in /proc/partitions
2995 that look like a partition name (do not end in a digit) */
2996static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002997list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002998{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999 FILE *procpt;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02003000 char line[100], ptname[100], devname[120];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003001 int ma, mi, sz;
3002
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00003003 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003004
3005 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003006 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00003007 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003008 continue;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02003009
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003010 sprintf(devname, "/dev/%s", ptname);
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02003011 if (is_whole_disk(devname))
3012 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003013 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00003014#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003015 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003016#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003017}
3018
Denis Vlasenko834410a2006-11-29 12:00:28 +00003019#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003020static void
Rob Landleyb73451d2006-02-24 16:29:00 +00003021unknown_command(int c)
3022{
Denis Vlasenkobd852072007-03-19 14:43:38 +00003023 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003024}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003025#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003026
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003027int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003028int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00003029{
Denis Vlasenko834410a2006-11-29 12:00:28 +00003030 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003031 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003032 * fdisk -v
3033 * fdisk -l [-b sectorsize] [-u] device ...
3034 * fdisk -s [partition] ...
3035 * fdisk [-b sectorsize] [-u] device
3036 *
3037 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003038 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003039 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00003040
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003041 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003042
Denys Vlasenko237bedd2016-07-06 21:58:02 +02003043 opt = getopt32(argv, "b:+C:+H:+lS:+u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003044 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003045 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02003046 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00003047 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02003048 * so cannot be combined with multiple disks,
3049 * and the same goes for the C/H/S options.
3050 */
3051 if (sector_size < 512
3052 || sector_size > 0x10000
3053 || (sector_size & (sector_size-1)) /* not power of 2 */
3054 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00003055 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02003056 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00003057 sector_offset = 2;
3058 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003059 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003060 if (user_heads <= 0 || user_heads >= 256)
3061 user_heads = 0;
3062 if (user_sectors <= 0 || user_sectors >= 64)
3063 user_sectors = 0;
3064 if (opt & OPT_u)
3065 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003066
Denis Vlasenko834410a2006-11-29 12:00:28 +00003067#if ENABLE_FEATURE_FDISK_WRITABLE
3068 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003069 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003070#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003071 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003072 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003073 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00003074 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003075 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003076 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003077 /* we don't have device names, */
3078 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003079 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003080 }
3081 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003082#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003083 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003084#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003085
Denis Vlasenko834410a2006-11-29 12:00:28 +00003086#if ENABLE_FEATURE_FDISK_BLKSIZE
3087 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003088 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003089
3090 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003091 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003092 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003093 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003094 unsigned long long size;
3095 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00003096 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003097 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003098 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003099 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003100 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003101 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003102 }
3103 return 0;
3104 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003105#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003106
Denis Vlasenko834410a2006-11-29 12:00:28 +00003107#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003108 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003109 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003110
Denis Vlasenko834410a2006-11-29 12:00:28 +00003111 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00003112 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003113
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003114 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003115 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00003116 printf("Detected an OSF/1 disklabel on %s, entering "
3117 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003118 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00003119 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00003120 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003121 /* If we return we may want to make an empty DOS label? */
3122 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003123
3124 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00003125 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00003126 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02003127 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003128 switch (c) {
3129 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003130 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003131 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003132 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003133 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003134 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003135 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003136 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003137 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003138 else
3139 unknown_command(c);
3140 break;
3141 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003142 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003143 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00003144 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00003145 if (read_maybe_empty("Please enter the name of the "
Denys Vlasenko69675782013-01-14 01:34:48 +01003146 "new boot file: ") == '\n')
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003147 puts("Boot file unchanged");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003148 else
3149 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003150 }
3151#if ENABLE_FEATURE_OSF_LABEL
3152 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003153 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003154#endif
3155 break;
3156 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003157 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003158 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003159 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003160 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003161 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003162 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003163 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003164 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003165 else
3166 unknown_command(c);
3167 break;
3168 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003169 {
Eric Andersen040f4402003-07-30 08:40:37 +00003170 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00003171 /* If sgi_label then don't use get_existing_partition,
3172 let the user select a partition, since
3173 get_existing_partition() only works for Linux-like
3174 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003175 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003176 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003177 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003178 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003179 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003180 if (j >= 0)
3181 delete_partition(j);
3182 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003183 break;
3184 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003185 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003186 create_sgiinfo();
3187 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003188 unknown_command(c);
3189 case 'l':
3190 list_types(get_sys_types());
3191 break;
3192 case 'm':
3193 menu();
3194 break;
3195 case 'n':
3196 new_partition();
3197 break;
3198 case 'o':
3199 create_doslabel();
3200 break;
3201 case 'p':
3202 list_table(0);
3203 break;
3204 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003205 if (ENABLE_FEATURE_CLEAN_UP)
3206 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003207 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003208 return 0;
3209 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003210#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003211 create_sunlabel();
3212#endif
3213 break;
3214 case 't':
3215 change_sysid();
3216 break;
3217 case 'u':
3218 change_units();
3219 break;
3220 case 'v':
3221 verify();
3222 break;
3223 case 'w':
Denys Vlasenko80856b32011-07-13 09:06:32 +02003224 write_table(); /* does not return */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003225 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003226#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003227 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003228 if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003229 puts("\n\tSorry, no experts menu for SGI "
3230 "partition tables available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003231 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003232 xselect();
3233 break;
3234#endif
3235 default:
3236 unknown_command(c);
3237 menu();
3238 }
3239 }
3240 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003241#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003242}