blob: 7275535e6b08eff070c4de2b6a939994287238ba [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;
172#if UINT_MAX == 4294967295
173# define SECT_FMT ""
174#elif ULONG_MAX == 4294967295
175# 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;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000429 smallint display_in_cyl_units; // = 1;
430#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; \
491 display_in_cyl_units = 1; \
492 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000493 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000494} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000495
Denis Vlasenkobd852072007-03-19 14:43:38 +0000496
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000497/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200498/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
499 * disks > 2^32 sectors
500 */
501static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000502{
503 uint64_t v64;
504 unsigned long longsectors;
505
506 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000507 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200508 v64 >>= 9;
509 if (v64 != (sector_t)v64) {
510 ret_trunc:
511 /* Not only DOS, but all other partition tables
512 * we support can't record more than 32 bit
513 * sector counts or offsets
514 */
515 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
516 v64 = (uint32_t)-1L;
517 }
518 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000519 }
520 /* Needs temp of type long */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200521 if (ioctl(fd, BLKGETSIZE, &longsectors)) {
522 /* Perhaps this is a disk image */
523 off_t sz = lseek(fd, 0, SEEK_END);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000524 longsectors = 0;
Denys Vlasenkod958e902010-04-06 02:32:26 +0200525 if (sz > 0)
526 longsectors = (uoff_t)sz / sector_size;
527 lseek(fd, 0, SEEK_SET);
528 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200529 if (sizeof(long) > sizeof(sector_t)
530 && longsectors != (sector_t)longsectors
531 ) {
532 goto ret_trunc;
533 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000534 return longsectors;
535}
536
537
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000538#define IS_EXTENDED(i) \
539 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
540
541#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
542
543#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
544
545#define pt_offset(b, n) \
546 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
547
548#define sector(s) ((s) & 0x3f)
549
550#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
551
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000552static void
553close_dev_fd(void)
554{
555 /* Not really closing, but making sure it is open, and to harmless place */
556 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
557}
558
Denys Vlasenkob157eb12016-09-17 21:05:06 +0200559/* Return partition name */
560static const char *
561partname(const char *dev, int pno, int lth)
562{
563 const char *p;
564 int w, wp;
565 int bufsiz;
566 char *bufp;
567
568 bufp = auto_string(xzalloc(80));
569 bufsiz = 80;
570
571 w = strlen(dev);
572 p = "";
573
574 if (isdigit(dev[w-1]))
575 p = "p";
576
577 /* devfs kludge - note: fdisk partition names are not supposed
578 to equal kernel names, so there is no reason to do this */
579 if (strcmp(dev + w - 4, "disc") == 0) {
580 w -= 4;
581 p = "part";
582 }
583
584 wp = strlen(p);
585
586 if (lth) {
587 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
588 lth-wp-2, w, dev, p, pno);
589 } else {
590 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
591 }
592 return bufp;
593}
594
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000595static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000596get_part_table(int i)
597{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000598 return ptes[i].part_table;
599}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000600
601static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000602str_units(int n)
603{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000604 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000605 return display_in_cyl_units ? "cylinder" : "sector";
606 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000607}
608
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000609static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000610valid_part_table_flag(const char *mbuffer)
611{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000612 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000613}
614
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200615static void fdisk_fatal(const char *why)
616{
617 if (listing) {
618 close_dev_fd();
619 longjmp(listingbuf, 1);
620 }
621 bb_error_msg_and_die(why, disk_device);
622}
623
624static void
625seek_sector(sector_t secno)
626{
627#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
628 off64_t off = (off64_t)secno * sector_size;
629 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
630 fdisk_fatal(unable_to_seek);
631#else
632 uint64_t off = (uint64_t)secno * sector_size;
633 if (off > MAXINT(off_t)
634 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
635 ) {
636 fdisk_fatal(unable_to_seek);
637 }
638#endif
639}
640
Denis Vlasenko834410a2006-11-29 12:00:28 +0000641#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200642/* Read line; return 0 or first printable char */
643static int
644read_line(const char *prompt)
645{
646 int sz;
647
Denys Vlasenko84ea60e2017-08-02 17:27:28 +0200648 sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer));
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200649 if (sz <= 0)
650 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
651
652 if (line_buffer[sz-1] == '\n')
653 line_buffer[--sz] = '\0';
654
655 line_ptr = line_buffer;
656 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
657 line_ptr++;
658 return *line_ptr;
659}
660
661static void
662set_all_unchanged(void)
663{
664 int i;
665
666 for (i = 0; i < MAXIMUM_PARTS; i++)
667 ptes[i].changed = 0;
668}
669
670static ALWAYS_INLINE void
671set_changed(int i)
672{
673 ptes[i].changed = 1;
674}
675
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000676static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000677write_part_table_flag(char *b)
678{
679 b[510] = 0x55;
680 b[511] = 0xaa;
681}
682
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000683static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000684read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000685{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000686 while (!read_line(mesg))
687 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000688 return *line_ptr;
689}
690
691static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000692read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000693{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000694 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000695 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000696 line_ptr[0] = '\n';
697 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000698 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000699 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000700}
701
702static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000703read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000704{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000705 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000706 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000707 read_nonempty("Hex code (type L to list codes): ");
Denys Vlasenkod958e902010-04-06 02:32:26 +0200708 if ((line_ptr[0] | 0x20) == 'l') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000709 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000710 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000711 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000712 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200713 if (v <= 0xff)
714 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000715 }
716}
717
Denis Vlasenko9764d692008-07-09 21:20:50 +0000718static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200719write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000720{
721 seek_sector(secno);
722 xwrite(dev_fd, buf, sector_size);
723}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200724#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000725
726
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000727#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000728
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100729struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000730 unsigned char info[128]; /* Informative text string */
731 unsigned char spare0[14];
732 struct sun_info {
733 unsigned char spare1;
734 unsigned char id;
735 unsigned char spare2;
736 unsigned char flags;
737 } infos[8];
738 unsigned char spare1[246]; /* Boot information etc. */
739 unsigned short rspeed; /* Disk rotational speed */
740 unsigned short pcylcount; /* Physical cylinder count */
741 unsigned short sparecyl; /* extra sects per cylinder */
742 unsigned char spare2[4]; /* More magic... */
743 unsigned short ilfact; /* Interleave factor */
744 unsigned short ncyl; /* Data cylinder count */
745 unsigned short nacyl; /* Alt. cylinder count */
746 unsigned short ntrks; /* Tracks per cylinder */
747 unsigned short nsect; /* Sectors per track */
748 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000749 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000750 uint32_t start_cylinder;
751 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000752 } partitions[8];
753 unsigned short magic; /* Magic number */
754 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100755} FIX_ALIASING;
756typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000757#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000758STATIC_OSF void bsd_select(void);
759STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000760#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000761
Denys Vlasenkod7559c22010-10-30 02:40:08 +0200762STATIC_GPT void gpt_list_table(int xtra);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200763#include "fdisk_gpt.c"
764
Denis Vlasenko28703012006-12-19 20:32:02 +0000765#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000766static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000767fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000768{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000769 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000770}
771
Rob Landley88621d72006-08-29 19:41:06 +0000772static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000773fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000774{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000775 return (x << 24) |
776 ((x & 0xFF00) << 8) |
777 ((x & 0xFF0000) >> 8) |
778 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000779}
780#endif
781
Denis Vlasenkobd852072007-03-19 14:43:38 +0000782STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000783STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000784STATIC_SGI int sgi_get_sysid(int i);
785STATIC_SGI void sgi_delete_partition(int i);
786STATIC_SGI void sgi_change_sysid(int i, int sys);
787STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000788#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000789STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000790#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000791STATIC_SGI int verify_sgi(int verbose);
792STATIC_SGI void sgi_add_partition(int n, int sys);
793STATIC_SGI void sgi_set_swappartition(int i);
794STATIC_SGI const char *sgi_get_bootfile(void);
795STATIC_SGI void sgi_set_bootfile(const char* aFile);
796STATIC_SGI void create_sgiinfo(void);
797STATIC_SGI void sgi_write_table(void);
798STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000799#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000800
Denis Vlasenkobd852072007-03-19 14:43:38 +0000801STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000802STATIC_SUN void sun_delete_partition(int i);
803STATIC_SUN void sun_change_sysid(int i, int sys);
804STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000805STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000806#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000807STATIC_SUN void sun_set_alt_cyl(void);
808STATIC_SUN void sun_set_ncyl(int cyl);
809STATIC_SUN void sun_set_xcyl(void);
810STATIC_SUN void sun_set_ilfact(void);
811STATIC_SUN void sun_set_rspeed(void);
812STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000813#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000814STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
815STATIC_SUN void verify_sun(void);
816STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000817#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000818
Denis Vlasenko9764d692008-07-09 21:20:50 +0000819
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200820static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000821read4_little_endian(const unsigned char *cp)
822{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200823 uint32_t v;
824 move_from_unaligned32(v, cp);
825 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000826}
827
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200828static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000829get_start_sect(const struct partition *p)
830{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000831 return read4_little_endian(p->start4);
832}
833
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200834static sector_t
835get_nr_sects(const struct partition *p)
836{
837 return read4_little_endian(p->size4);
838}
839
Denis Vlasenko834410a2006-11-29 12:00:28 +0000840#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200841/* start_sect and nr_sects are stored little endian on all machines */
842/* moreover, they are not aligned correctly */
843static inline_if_little_endian void
844store4_little_endian(unsigned char *cp, unsigned val)
845{
846 uint32_t v = SWAP_LE32(val);
847 move_to_unaligned32(cp, v);
848}
849
850static void
851set_start_sect(struct partition *p, unsigned start_sect)
852{
853 store4_little_endian(p->start4, start_sect);
854}
855
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000856static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000857set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000858{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000859 store4_little_endian(p->size4, nr_sects);
860}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000861#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000862
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000863/* Allocate a buffer and read a partition table sector */
864static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200865read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000866{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200867 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000868 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000869 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000870 /* xread would make us abort - bad for fdisk -l */
871 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000872 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000873#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000874 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000875#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000876 pe->part_table = pe->ext_pointer = NULL;
877}
878
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200879static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200880get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000881{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200882 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000883}
884
Denis Vlasenko834410a2006-11-29 12:00:28 +0000885#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000886/*
887 * Avoid warning about DOS partitions when no DOS partition was changed.
888 * Here a heuristic "is probably dos partition".
889 * We might also do the opposite and warn in all cases except
890 * for "is probably nondos partition".
891 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000892#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000893static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000894is_dos_partition(int t)
895{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000896 return (t == 1 || t == 4 || t == 6 ||
897 t == 0x0b || t == 0x0c || t == 0x0e ||
898 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
899 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
900 t == 0xc1 || t == 0xc4 || t == 0xc6);
901}
Denis Vlasenko89398812008-01-25 20:18:46 +0000902#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000903
904static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000905menu(void)
906{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000907 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000908 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000909 puts("a\ttoggle a read only flag"); /* sun */
910 puts("b\tedit bsd disklabel");
911 puts("c\ttoggle the mountable flag"); /* sun */
912 puts("d\tdelete a partition");
913 puts("l\tlist known partition types");
914 puts("n\tadd a new partition");
915 puts("o\tcreate a new empty DOS partition table");
916 puts("p\tprint the partition table");
917 puts("q\tquit without saving changes");
918 puts("s\tcreate a new empty Sun disklabel"); /* sun */
919 puts("t\tchange a partition's system id");
920 puts("u\tchange display/entry units");
921 puts("v\tverify the partition table");
922 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000923#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000924 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000925#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000926 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000927 puts("a\tselect bootable partition"); /* sgi flavour */
928 puts("b\tedit bootfile entry"); /* sgi */
929 puts("c\tselect sgi swap partition"); /* sgi flavour */
930 puts("d\tdelete a partition");
931 puts("l\tlist known partition types");
932 puts("n\tadd a new partition");
933 puts("o\tcreate a new empty DOS partition table");
934 puts("p\tprint the partition table");
935 puts("q\tquit without saving changes");
936 puts("s\tcreate a new empty Sun disklabel"); /* sun */
937 puts("t\tchange a partition's system id");
938 puts("u\tchange display/entry units");
939 puts("v\tverify the partition table");
940 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000941 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000942 puts("o\tcreate a new empty DOS partition table");
943 puts("q\tquit without saving changes");
944 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200945 } else if (LABEL_IS_GPT) {
946 puts("o\tcreate a new empty DOS partition table");
947 puts("p\tprint the partition table");
948 puts("q\tquit without saving changes");
949 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000950 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000951 puts("a\ttoggle a bootable flag");
952 puts("b\tedit bsd disklabel");
953 puts("c\ttoggle the dos compatibility flag");
954 puts("d\tdelete a partition");
955 puts("l\tlist known partition types");
956 puts("n\tadd a new partition");
957 puts("o\tcreate a new empty DOS partition table");
958 puts("p\tprint the partition table");
959 puts("q\tquit without saving changes");
960 puts("s\tcreate a new empty Sun disklabel"); /* sun */
961 puts("t\tchange a partition's system id");
962 puts("u\tchange display/entry units");
963 puts("v\tverify the partition table");
964 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000965#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000966 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000967#endif
968 }
969}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000970#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000971
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000972
Denis Vlasenko834410a2006-11-29 12:00:28 +0000973#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000974static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000975xmenu(void)
976{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000977 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000978 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000979 puts("a\tchange number of alternate cylinders"); /*sun*/
980 puts("c\tchange number of cylinders");
981 puts("d\tprint the raw data in the partition table");
982 puts("e\tchange number of extra sectors per cylinder");/*sun*/
983 puts("h\tchange number of heads");
984 puts("i\tchange interleave factor"); /*sun*/
985 puts("o\tchange rotation speed (rpm)"); /*sun*/
986 puts("p\tprint the partition table");
987 puts("q\tquit without saving changes");
988 puts("r\treturn to main menu");
989 puts("s\tchange number of sectors/track");
990 puts("v\tverify the partition table");
991 puts("w\twrite table to disk and exit");
992 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000993 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000994 puts("b\tmove beginning of data in a partition"); /* !sun */
995 puts("c\tchange number of cylinders");
996 puts("d\tprint the raw data in the partition table");
997 puts("e\tlist extended partitions"); /* !sun */
998 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
999 puts("h\tchange number of heads");
1000 puts("p\tprint the partition table");
1001 puts("q\tquit without saving changes");
1002 puts("r\treturn to main menu");
1003 puts("s\tchange number of sectors/track");
1004 puts("v\tverify the partition table");
1005 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001006 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001007 puts("b\tmove beginning of data in a partition"); /* !sun */
1008 puts("c\tchange number of cylinders");
1009 puts("d\tprint the raw data in the partition table");
1010 puts("e\tlist extended partitions"); /* !sun */
1011 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
1012 puts("h\tchange number of heads");
1013 puts("p\tprint the partition table");
1014 puts("q\tquit without saving changes");
1015 puts("r\treturn to main menu");
1016 puts("s\tchange number of sectors/track");
1017 puts("v\tverify the partition table");
1018 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001019 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001020 puts("b\tmove beginning of data in a partition"); /* !sun */
1021 puts("c\tchange number of cylinders");
1022 puts("d\tprint the raw data in the partition table");
1023 puts("e\tlist extended partitions"); /* !sun */
1024 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001025#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +00001026 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001027#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001028 puts("h\tchange number of heads");
1029 puts("p\tprint the partition table");
1030 puts("q\tquit without saving changes");
1031 puts("r\treturn to main menu");
1032 puts("s\tchange number of sectors/track");
1033 puts("v\tverify the partition table");
1034 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001035 }
1036}
1037#endif /* ADVANCED mode */
1038
Denis Vlasenko834410a2006-11-29 12:00:28 +00001039#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001040static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +00001041get_sys_types(void)
1042{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001043 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001044 LABEL_IS_SUN ? sun_sys_types :
1045 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001046 i386_sys_types);
1047}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001048#else
1049#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001050#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001051
Denis Vlasenkobd852072007-03-19 14:43:38 +00001052static const char *
1053partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001054{
1055 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001056 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001057
Denis Vlasenkobd852072007-03-19 14:43:38 +00001058 for (i = 0; types[i]; i++)
1059 if ((unsigned char)types[i][0] == type)
1060 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001061
Denis Vlasenkobd852072007-03-19 14:43:38 +00001062 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001063}
1064
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001065static int
1066is_cleared_partition(const struct partition *p)
1067{
1068 /* We consider partition "cleared" only if it has only zeros */
1069 const char *cp = (const char *)p;
1070 int cnt = sizeof(*p);
1071 char bits = 0;
1072 while (--cnt >= 0)
1073 bits |= *cp++;
1074 return (bits == 0);
1075}
1076
1077static void
1078clear_partition(struct partition *p)
1079{
1080 if (p)
1081 memset(p, 0, sizeof(*p));
1082}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001083
Denis Vlasenko834410a2006-11-29 12:00:28 +00001084#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001085static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001086get_sysid(int i)
1087{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001088 return LABEL_IS_SUN ? sunlabel->infos[i].id :
1089 (LABEL_IS_SGI ? sgi_get_sysid(i) :
1090 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001091}
1092
Denis Vlasenkobd852072007-03-19 14:43:38 +00001093static void
1094list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001095{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001096 enum { COLS = 3 };
1097
1098 unsigned last[COLS];
1099 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001100 int i;
1101
Denis Vlasenko9764d692008-07-09 21:20:50 +00001102 for (size = 0; sys[size]; size++)
1103 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001104
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001105 done = 0;
1106 for (i = COLS-1; i >= 0; i--) {
1107 done += (size + i - done) / (i + 1);
1108 last[COLS-1 - i] = done;
1109 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001110
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001111 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001112 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001113 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001114 (unsigned char)sys[next][0],
1115 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001116 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001117 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001118 i = 0;
1119 next = ++done;
1120 }
1121 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001122 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001123}
1124
Denys Vlasenkod958e902010-04-06 02:32:26 +02001125#define set_hsc(h, s, c, sector) do \
1126{ \
1127 s = sector % g_sectors + 1; \
1128 sector /= g_sectors; \
1129 h = sector % g_heads; \
1130 sector /= g_heads; \
1131 c = sector & 0xff; \
1132 s |= (sector >> 2) & 0xc0; \
1133} while (0)
1134
1135static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1136{
1137 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1138 start = g_heads * g_sectors * 1024 - 1;
1139 set_hsc(p->head, p->sector, p->cyl, start);
1140
1141 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1142 stop = g_heads * g_sectors * 1024 - 1;
1143 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1144}
1145
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001146static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001147set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001148{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001149 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001150 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001151
1152 if (doext) {
1153 p = ptes[i].ext_pointer;
1154 offset = extended_offset;
1155 } else {
1156 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001157 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001158 }
1159 p->boot_ind = 0;
1160 p->sys_ind = sysid;
1161 set_start_sect(p, start - offset);
1162 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001163 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001164 ptes[i].changed = 1;
1165}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001166#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001167
1168static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001169warn_geometry(void)
1170{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001171 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001172 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001173
Denis Vlasenkobd852072007-03-19 14:43:38 +00001174 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001175 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001176 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001177 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001178 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001179 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001180 printf(" cylinders");
Denis Vlasenko834410a2006-11-29 12:00:28 +00001181#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001182 puts(" (settable in the extra functions menu)");
1183#else
1184 bb_putchar('\n');
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001185#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001186 return 1;
1187}
1188
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001189static void
1190update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001191{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001192 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001193
1194 if (display_in_cyl_units && cyl_units)
1195 units_per_sector = cyl_units;
1196 else
1197 units_per_sector = 1; /* in sectors */
1198}
1199
Denis Vlasenko834410a2006-11-29 12:00:28 +00001200#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001201static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001202warn_cylinders(void)
1203{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001204 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001205 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001206"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001207"There is nothing wrong with that, but this is larger than 1024,\n"
1208"and could in certain setups cause problems with:\n"
1209"1) software that runs at boot time (e.g., old versions of LILO)\n"
1210"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001211" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001212 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001213}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001214#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001215
1216static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001217read_extended(int ext)
1218{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001219 int i;
1220 struct pte *pex;
1221 struct partition *p, *q;
1222
1223 ext_index = ext;
1224 pex = &ptes[ext];
1225 pex->ext_pointer = pex->part_table;
1226
1227 p = pex->part_table;
1228 if (!get_start_sect(p)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001229 puts("Bad offset in primary extended partition");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001230 return;
1231 }
1232
Rob Landleyb73451d2006-02-24 16:29:00 +00001233 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001234 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001235
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001236 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001237 /* This is not a Linux restriction, but
1238 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001239 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001240 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001241#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001242 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001243 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001244 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001245#endif
1246 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001247 return;
1248 }
1249
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001250 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001251
1252 if (!extended_offset)
1253 extended_offset = get_start_sect(p);
1254
1255 q = p = pt_offset(pe->sectorbuffer, 0);
1256 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001257 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001258 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001259 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001260 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001261 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001262 else
1263 pe->ext_pointer = p;
1264 } else if (p->sys_ind) {
1265 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001266 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001267 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001268 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001269 else
1270 pe->part_table = p;
1271 }
1272 }
1273
1274 /* very strange code here... */
1275 if (!pe->part_table) {
1276 if (q != pe->ext_pointer)
1277 pe->part_table = q;
1278 else
1279 pe->part_table = q + 1;
1280 }
1281 if (!pe->ext_pointer) {
1282 if (q != pe->part_table)
1283 pe->ext_pointer = q;
1284 else
1285 pe->ext_pointer = q + 1;
1286 }
1287
1288 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001289 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001290 }
1291
Denis Vlasenko834410a2006-11-29 12:00:28 +00001292#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001293 /* remove empty links */
1294 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001295 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001296 struct pte *pe = &ptes[i];
1297
Denis Vlasenkobd852072007-03-19 14:43:38 +00001298 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001299 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001300 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001301 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001302 delete_partition(i);
1303 goto remove; /* numbering changed */
1304 }
1305 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001306#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001307}
1308
Denis Vlasenko834410a2006-11-29 12:00:28 +00001309#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001310static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001311create_doslabel(void)
1312{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001313 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001314
Denis Vlasenko4437d192008-04-17 00:12:10 +00001315 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001316#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001317 possibly_osf_label = 0;
1318#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001319 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001320
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001321 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001322 write_part_table_flag(MBRbuffer);
1323 extended_offset = 0;
1324 set_all_unchanged();
1325 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001326 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001327}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001328#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001329
1330static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001331get_sectorsize(void)
1332{
Rob Landley736e5252006-02-25 03:36:00 +00001333 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001334 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001335 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001336 sector_size = arg;
1337 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001338 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001339 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1340 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001341 }
1342}
1343
Rob Landley88621d72006-08-29 19:41:06 +00001344static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001345get_kernel_geometry(void)
1346{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001347 struct hd_geometry geometry;
1348
Denis Vlasenko4437d192008-04-17 00:12:10 +00001349 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001350 kern_heads = geometry.heads;
1351 kern_sectors = geometry.sectors;
1352 /* never use geometry.cylinders - it is truncated */
1353 }
1354}
1355
1356static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001357get_partition_table_geometry(void)
1358{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001359 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001360 struct partition *p;
1361 int i, h, s, hh, ss;
1362 int first = 1;
1363 int bad = 0;
1364
Eric Andersen3496fdc2006-01-30 23:09:20 +00001365 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001366 return;
1367
1368 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001369 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001370 p = pt_offset(bufp, i);
1371 if (p->sys_ind != 0) {
1372 h = p->end_head + 1;
1373 s = (p->end_sector & 077);
1374 if (first) {
1375 hh = h;
1376 ss = s;
1377 first = 0;
1378 } else if (hh != h || ss != s)
1379 bad = 1;
1380 }
1381 }
1382
1383 if (!first && !bad) {
1384 pt_heads = hh;
1385 pt_sectors = ss;
1386 }
1387}
1388
Rob Landleyb73451d2006-02-24 16:29:00 +00001389static void
1390get_geometry(void)
1391{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001392 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001393
1394 get_sectorsize();
1395 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001396#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001397 guess_device_type();
1398#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001399 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001400 kern_heads = kern_sectors = 0;
1401 pt_heads = pt_sectors = 0;
1402
1403 get_kernel_geometry();
1404 get_partition_table_geometry();
1405
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001406 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001407 pt_heads ? pt_heads :
1408 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001409 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001410 pt_sectors ? pt_sectors :
1411 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001412 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001413
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001414 sector_offset = 1;
1415 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001416 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001417
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001418 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1419 if (!g_cylinders)
1420 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001421}
1422
1423/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001424 * Opens disk_device and optionally reads MBR.
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001425 * If what == OPEN_MAIN:
1426 * Open device, read MBR. Abort program on short read. Create empty
1427 * disklabel if the on-disk structure is invalid (WRITABLE mode).
1428 * If what == TRY_ONLY:
1429 * Open device, read MBR. Return an error if anything is out of place.
1430 * Do not create an empty disklabel. This is used for the "list"
1431 * operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1432 * If what == CREATE_EMPTY_*:
1433 * This means that get_boot() was called recursively from create_*label().
1434 * Do not re-open the device; just set up the ptes array and print
1435 * geometry warnings.
1436 *
Denis Vlasenko4437d192008-04-17 00:12:10 +00001437 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001438 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1439 * 0: found or created label
1440 * 1: I/O error
1441 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001442#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1443static int get_boot(enum action what)
1444#else
1445static int get_boot(void)
1446#define get_boot(what) get_boot()
1447#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001448{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001449 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001450
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001451 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001452 for (i = 0; i < 4; i++) {
1453 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001454 pe->part_table = pt_offset(MBRbuffer, i);
1455 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001456 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001457 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001458#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001459 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001460#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001461 }
1462
Denis Vlasenko834410a2006-11-29 12:00:28 +00001463#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001464// ALERT! highly idiotic design!
1465// We end up here when we call get_boot() recursively
1466// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1467// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1468// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1469// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001470 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001471 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001472
Denis Vlasenko4437d192008-04-17 00:12:10 +00001473 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1474
Denis Vlasenkobd852072007-03-19 14:43:38 +00001475 if (fd < 0) {
1476 fd = open(disk_device, O_RDONLY);
1477 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001478 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001479 return 1;
1480 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001481 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001482 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001483 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001484 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001485 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001486 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001487 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001488 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001489 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001490 fdisk_fatal(unable_to_read);
1491 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001492#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001493 fd = open(disk_device, O_RDONLY);
1494 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001495 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001496 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001497 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001498 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001499 }
1500 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001501#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001502
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001503 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001504 update_units();
1505
Denis Vlasenko834410a2006-11-29 12:00:28 +00001506#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001507 if (check_sun_label())
1508 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001509#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001510#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001511 if (check_sgi_label())
1512 return 0;
1513#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001514#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001515 if (check_aix_label())
1516 return 0;
1517#endif
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001518#if ENABLE_FEATURE_GPT_LABEL
1519 if (check_gpt_label())
1520 return 0;
1521#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001522#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001523 if (check_osf_label()) {
1524 possibly_osf_label = 1;
1525 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001526 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001527 return 0;
1528 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001529 puts("This disk has both DOS and BSD magic.\n"
1530 "Give the 'b' command to go to BSD mode.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001531 }
1532#endif
1533
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001534#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001535 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001536 return -1;
1537#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001538 if (!valid_part_table_flag(MBRbuffer)) {
1539 if (what == OPEN_MAIN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001540 puts("Device contains neither a valid DOS "
1541 "partition table, nor Sun, SGI, OSF or GPT "
1542 "disklabel");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001543#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001544 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001545#else
1546 create_doslabel();
1547#endif
1548 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001549 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001550 /* TRY_ONLY: */
1551 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001552 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001553 created_table:
1554#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001555
Denis Vlasenko4437d192008-04-17 00:12:10 +00001556
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001557 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001558 warn_geometry();
1559
1560 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001561 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001562 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001563 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001564 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001565 else
1566 read_extended(i);
1567 }
1568 }
1569
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001570 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001572 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001573 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001574 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001575 pe->sectorbuffer[510],
1576 pe->sectorbuffer[511],
1577 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001578 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001579 }
1580 }
1581
1582 return 0;
1583}
1584
Denis Vlasenko834410a2006-11-29 12:00:28 +00001585#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001586/*
1587 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1588 * If the user hits Enter, DFLT is returned.
1589 * Answers like +10 are interpreted as offsets from BASE.
1590 *
1591 * There is no default if DFLT is not between LOW and HIGH.
1592 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001593static sector_t
1594read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001595{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001596 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001597 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001598 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001599
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001600 if (dflt < low || dflt > high) {
1601 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001602 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001603 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001604
1605 while (1) {
1606 int use_default = default_ok;
1607
1608 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001609 do {
1610 printf(fmt, mesg, low, high, dflt);
1611 read_maybe_empty("");
1612 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1613 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001614
Eric Andersen84bdea82004-05-19 10:49:17 +00001615 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001616 int minus = (*line_ptr == '-');
1617 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001618
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001619 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001620
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001621 /* (1) if 2nd char is digit, use_default = 0.
1622 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001623 while (isdigit(*++line_ptr))
1624 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001625
Rob Landleyb73451d2006-02-24 16:29:00 +00001626 switch (*line_ptr) {
1627 case 'c':
1628 case 'C':
1629 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001630 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001631 break;
1632 case 'K':
1633 absolute = 1024;
1634 break;
1635 case 'k':
1636 absolute = 1000;
1637 break;
1638 case 'm':
1639 case 'M':
1640 absolute = 1000000;
1641 break;
1642 case 'g':
1643 case 'G':
1644 absolute = 1000000000;
1645 break;
1646 default:
1647 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001648 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001649 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001650 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001651 unsigned long unit;
1652
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001653 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001654 unit = sector_size * units_per_sector;
1655 bytes += unit/2; /* round */
1656 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001657 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001658 }
1659 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001660 value = -value;
1661 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001662 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001663 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001664 while (isdigit(*line_ptr)) {
1665 line_ptr++;
1666 use_default = 0;
1667 }
1668 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001669 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001670 value = dflt;
1671 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001672 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001673 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001674 break;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001675 puts("Value is out of range");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001676 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001677 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001678}
1679
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001680static unsigned
1681get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001682{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001683 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001684 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001685
Denis Vlasenkobd852072007-03-19 14:43:38 +00001686 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001687 pe = &ptes[i];
1688
1689 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001690 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1691 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1692 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1693 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001694 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001695 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001696 }
1697 return i;
1698}
1699
1700static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001701get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001702{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001703 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001704 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001705
1706 for (i = 0; i < max; i++) {
1707 struct pte *pe = &ptes[i];
1708 struct partition *p = pe->part_table;
1709
1710 if (p && !is_cleared_partition(p)) {
1711 if (pno >= 0)
1712 goto not_unique;
1713 pno = i;
1714 }
1715 }
1716 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001717 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001718 return pno;
1719 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001720 puts("No partition is defined yet!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001721 return -1;
1722
1723 not_unique:
1724 return get_partition(warn, max);
1725}
1726
1727static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001728get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001729{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001730 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001731 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001732
1733 for (i = 0; i < max; i++) {
1734 struct pte *pe = &ptes[i];
1735 struct partition *p = pe->part_table;
1736
1737 if (p && is_cleared_partition(p)) {
1738 if (pno >= 0)
1739 goto not_unique;
1740 pno = i;
1741 }
1742 }
1743 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001744 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001745 return pno;
1746 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001747 puts("All primary partitions have been defined already!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001748 return -1;
1749
1750 not_unique:
1751 return get_partition(warn, max);
1752}
1753
1754
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001755static void
1756change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001757{
1758 display_in_cyl_units = !display_in_cyl_units;
1759 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001760 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001761 str_units(PLURAL));
1762}
1763
1764static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001765toggle_active(int i)
1766{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001767 struct pte *pe = &ptes[i];
1768 struct partition *p = pe->part_table;
1769
Rob Landleyb73451d2006-02-24 16:29:00 +00001770 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001771 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001772 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1773 pe->changed = 1;
1774}
1775
1776static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001777toggle_dos_compatibility_flag(void)
1778{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001779 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001780 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001781 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001782 printf("DOS Compatibility flag is %sset\n", "");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001783 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001784 sector_offset = 1;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001785 printf("DOS Compatibility flag is %sset\n", "not ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001786 }
1787}
1788
1789static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001790delete_partition(int i)
1791{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001792 struct pte *pe = &ptes[i];
1793 struct partition *p = pe->part_table;
1794 struct partition *q = pe->ext_pointer;
1795
1796/* Note that for the fifth partition (i == 4) we don't actually
1797 * decrement partitions.
1798 */
1799
1800 if (warn_geometry())
1801 return; /* C/H/S not set */
1802 pe->changed = 1;
1803
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001804 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001805 sun_delete_partition(i);
1806 return;
1807 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001808 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001809 sgi_delete_partition(i);
1810 return;
1811 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001812
1813 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001814 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001815 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001816 ptes[ext_index].ext_pointer = NULL;
1817 extended_offset = 0;
1818 }
1819 clear_partition(p);
1820 return;
1821 }
1822
1823 if (!q->sys_ind && i > 4) {
1824 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001825 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001826 --i;
1827 clear_partition(ptes[i].ext_pointer);
1828 ptes[i].changed = 1;
1829 } else {
1830 /* not the last one - further ones will be moved down */
1831 if (i > 4) {
1832 /* delete this link in the chain */
1833 p = ptes[i-1].ext_pointer;
1834 *p = *q;
1835 set_start_sect(p, get_start_sect(q));
1836 set_nr_sects(p, get_nr_sects(q));
1837 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001838 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001839 /* the first logical in a longer chain */
1840 pe = &ptes[5];
1841
1842 if (pe->part_table) /* prevent SEGFAULT */
1843 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001844 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001845 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001846 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001847 pe->changed = 1;
1848 }
1849
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001850 if (g_partitions > 5) {
1851 g_partitions--;
1852 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001853 ptes[i] = ptes[i+1];
1854 i++;
1855 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001856 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001857 /* the only logical: clear only */
1858 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001859 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001860 }
1861}
1862
1863static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001864change_sysid(void)
1865{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001866 int i, sys, origsys;
1867 struct partition *p;
1868
Eric Andersen040f4402003-07-30 08:40:37 +00001869 /* If sgi_label then don't use get_existing_partition,
1870 let the user select a partition, since get_existing_partition()
1871 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001872 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001873 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001874 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001875 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001876 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001877 if (i == -1)
1878 return;
1879 p = ptes[i].part_table;
1880 origsys = sys = get_sysid(i);
1881
1882 /* if changing types T to 0 is allowed, then
1883 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001884 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001885 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001886 return;
1887 }
1888 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001889 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001890
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001891 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001892 puts("Type 0 means free space to many systems\n"
1893 "(but not to Linux). Having partitions of\n"
1894 "type 0 is probably unwise.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001895 /* break; */
1896 }
1897
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001898 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001899 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001900 puts("You cannot change a partition into"
1901 " an extended one or vice versa");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001902 break;
1903 }
1904 }
1905
1906 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001907#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001908 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001909 puts("Consider leaving partition 3 "
1910 "as Whole disk (5),\n"
1911 "as SunOS/Solaris expects it and "
1912 "even Linux likes it\n");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001913#endif
1914#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001915 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001916 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001917 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001918 (i == 8 && sys != 0)
1919 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001920 ) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001921 puts("Consider leaving partition 9 "
1922 "as volume header (0),\nand "
1923 "partition 11 as entire volume (6)"
1924 "as IRIX expects it\n");
Rob Landley5527b912006-02-25 03:46:10 +00001925 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001926#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001927 if (sys == origsys)
1928 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001929 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001930 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001931 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001932 sgi_change_sysid(i, sys);
1933 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001934 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001935
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001936 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001937 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001938 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001939 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001940 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1941 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001942 break;
1943 }
1944 }
1945}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001946#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001947
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001948
Denis Vlasenko28703012006-12-19 20:32:02 +00001949/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001950 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1951 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1952 * Lubkin Oct. 1991). */
1953
Rob Landleyb73451d2006-02-24 16:29:00 +00001954static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001955linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001956{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001957 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001958
1959 *c = ls / spc;
1960 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001961 *h = ls / g_sectors;
1962 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001963}
1964
Rob Landleyb73451d2006-02-24 16:29:00 +00001965static void
1966check_consistency(const struct partition *p, int partition)
1967{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001968 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1969 unsigned pec, peh, pes; /* physical ending c, h, s */
1970 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1971 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001972
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001973 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001974 return; /* do not check extended partitions */
1975
1976/* physical beginning c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001977 pbc = cylinder(p->sector, p->cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001978 pbh = p->head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001979 pbs = sector(p->sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001980
1981/* physical ending c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001982 pec = cylinder(p->end_sector, p->end_cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001983 peh = p->end_head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001984 pes = sector(p->end_sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001985
1986/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001987 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001988
1989/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001990 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001991
1992/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001993 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001994 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001995 "start (non-Linux?):\n", partition + 1);
1996 printf(" phys=(%u,%u,%u) ", pbc, pbh, pbs);
1997 printf("logical=(%u,%u,%u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001998 }
1999
2000/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002001 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002002 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002003 "end:\n", partition + 1);
2004 printf(" phys=(%u,%u,%u) ", pec, peh, pes);
2005 printf("logical=(%u,%u,%u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002006 }
2007
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002008/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002009 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002010 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002012 }
2013}
2014
2015static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002016list_disk_geometry(void)
2017{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002018 ullong bytes = ((ullong)total_number_of_sectors << 9);
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002019 ullong xbytes = bytes / (1024*1024);
2020 char x = 'M';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002021
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002022 if (xbytes >= 10000) {
2023 xbytes += 512; /* fdisk util-linux 2.28 does this */
2024 xbytes /= 1024;
2025 x = 'G';
2026 }
2027 printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
2028 "%u cylinders, %u heads, %u sectors/track\n"
2029 "Units: %s of %u * %u = %u bytes\n\n",
2030 disk_device, xbytes, x,
2031 bytes, total_number_of_sectors,
2032 g_cylinders, g_heads, g_sectors,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002033 str_units(PLURAL),
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002034 units_per_sector, sector_size, units_per_sector * sector_size
2035 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002036}
2037
2038/*
2039 * Check whether partition entries are ordered by their starting positions.
2040 * Return 0 if OK. Return i if partition i should have been earlier.
2041 * Two separate checks: primary and logical partitions.
2042 */
2043static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002044wrong_p_order(int *prev)
2045{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002046 const struct pte *pe;
2047 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002048 sector_t last_p_start_pos = 0, p_start_pos;
2049 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002050
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002051 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002052 if (i == 4) {
2053 last_i = 4;
2054 last_p_start_pos = 0;
2055 }
2056 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00002057 p = pe->part_table;
2058 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002059 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002060
2061 if (last_p_start_pos > p_start_pos) {
2062 if (prev)
2063 *prev = last_i;
2064 return i;
2065 }
2066
2067 last_p_start_pos = p_start_pos;
2068 last_i = i;
2069 }
2070 }
2071 return 0;
2072}
2073
Denis Vlasenko834410a2006-11-29 12:00:28 +00002074#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002075/*
2076 * Fix the chain of logicals.
2077 * extended_offset is unchanged, the set of sectors used is unchanged
2078 * The chain is sorted so that sectors increase, and so that
2079 * starting sectors increase.
2080 *
2081 * After this it may still be that cfdisk doesnt like the table.
2082 * (This is because cfdisk considers expanded parts, from link to
2083 * end of partition, and these may still overlap.)
2084 * Now
2085 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
2086 * may help.
2087 */
2088static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002089fix_chain_of_logicals(void)
2090{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002091 int j, oj, ojj, sj, sjj;
2092 struct partition *pj,*pjj,tmp;
2093
2094 /* Stage 1: sort sectors but leave sector of part 4 */
2095 /* (Its sector is the global extended_offset.) */
2096 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002097 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002098 oj = ptes[j].offset_from_dev_start;
2099 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002100 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002101 ptes[j].offset_from_dev_start = ojj;
2102 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002103 pj = ptes[j].part_table;
2104 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2105 pjj = ptes[j+1].part_table;
2106 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2107 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002108 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002109 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002110 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002111 goto stage1;
2112 }
2113 }
2114
2115 /* Stage 2: sort starting sectors */
2116 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002117 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002118 pj = ptes[j].part_table;
2119 pjj = ptes[j+1].part_table;
2120 sj = get_start_sect(pj);
2121 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002122 oj = ptes[j].offset_from_dev_start;
2123 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002124 if (oj+sj > ojj+sjj) {
2125 tmp = *pj;
2126 *pj = *pjj;
2127 *pjj = tmp;
2128 set_start_sect(pj, ojj+sjj-oj);
2129 set_start_sect(pjj, oj+sj-ojj);
2130 goto stage2;
2131 }
2132 }
2133
2134 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002135 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002136 ptes[j].changed = 1;
2137}
2138
2139
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002140static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002141fix_partition_table_order(void)
2142{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002143 struct pte *pei, *pek;
2144 int i,k;
2145
2146 if (!wrong_p_order(NULL)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002147 puts("Ordering is already correct\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002148 return;
2149 }
2150
2151 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2152 /* partition i should have come earlier, move it */
2153 /* We have to move data in the MBR */
2154 struct partition *pi, *pk, *pe, pbuf;
2155 pei = &ptes[i];
2156 pek = &ptes[k];
2157
2158 pe = pei->ext_pointer;
2159 pei->ext_pointer = pek->ext_pointer;
2160 pek->ext_pointer = pe;
2161
2162 pi = pei->part_table;
2163 pk = pek->part_table;
2164
2165 memmove(&pbuf, pi, sizeof(struct partition));
2166 memmove(pi, pk, sizeof(struct partition));
2167 memmove(pk, &pbuf, sizeof(struct partition));
2168
2169 pei->changed = pek->changed = 1;
2170 }
2171
2172 if (i)
2173 fix_chain_of_logicals();
2174
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002175 puts("Done");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002176}
2177#endif
2178
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002179static const char *
2180chs_string11(unsigned cyl, unsigned head, unsigned sect)
2181{
2182 char *buf = auto_string(xzalloc(sizeof(int)*3 * 3));
2183 sprintf(buf, "%u,%u,%u", cylinder(sect,cyl), head, sector(sect));
2184 return buf;
2185}
2186
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002187static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002188list_table(int xtra)
2189{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002190 int i, w;
2191
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002192 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002193 sun_list_table(xtra);
2194 return;
2195 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002196 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002197 sgi_list_table(xtra);
2198 return;
2199 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002200 if (LABEL_IS_GPT) {
2201 gpt_list_table(xtra);
2202 return;
2203 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002204
2205 list_disk_geometry();
2206
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002207 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002208 xbsd_print_disklabel(xtra);
2209 return;
2210 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002211
2212 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002213 * but if the device name ends in a digit, say /dev/foo1,
2214 * then the partition is called /dev/foo1p3.
2215 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002216 w = strlen(disk_device);
2217 if (w && isdigit(disk_device[w-1]))
2218 w++;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002219 if (w < 7)
2220 w = 7;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002221
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002222 printf("%-*s Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type\n",
2223 w-1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002224
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002225 for (i = 0; i < g_partitions; i++) {
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002226 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002227 const struct pte *pe = &ptes[i];
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002228 char boot4[4];
2229 char numstr6[6];
2230 sector_t start_sect;
2231 sector_t end_sect;
2232 sector_t nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002233
2234 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002235 if (!p || is_cleared_partition(p))
2236 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002237
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002238 sprintf(boot4, "%02x", p->boot_ind);
2239 if ((p->boot_ind & 0x7f) == 0) {
2240 /* 0x80 shown as '*', 0x00 is ' ' */
2241 boot4[0] = p->boot_ind ? '*' : ' ';
2242 boot4[1] = ' ';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002243 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002244
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002245 start_sect = get_partition_start_from_dev_start(pe);
2246 end_sect = start_sect;
2247 nr_sects = get_nr_sects(p);
2248 if (nr_sects != 0)
2249 end_sect += nr_sects - 1;
2250
2251 smart_ulltoa5((ullong)nr_sects * sector_size,
2252 numstr6, " KMGTPEZY")[0] = '\0';
2253
2254#define SFMT SECT_FMT
2255 // Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
2256 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 +00002257 partname(disk_device, i+1, w+2),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002258 boot4,
2259 chs_string11(p->cyl, p->head, p->sector),
2260 chs_string11(p->end_cyl, p->end_head, p->end_sector),
2261 start_sect,
2262 end_sect,
2263 nr_sects,
2264 numstr6,
2265 p->sys_ind,
2266 partition_type(p->sys_ind)
2267 );
2268#undef SFMT
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002269 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002270 }
2271
2272 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002273 /* partition table entries are not checked for correct order
2274 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002275 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002276 /* FIXME */
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002277 puts("\nPartition table entries are not in disk order");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002278 }
2279}
2280
Denis Vlasenko834410a2006-11-29 12:00:28 +00002281#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002282static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002283x_list_table(int extend)
2284{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002285 const struct pte *pe;
2286 const struct partition *p;
2287 int i;
2288
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002289 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002290 disk_device, g_heads, g_sectors, g_cylinders);
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002291 puts("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002292 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002293 pe = &ptes[i];
2294 p = (extend ? pe->ext_pointer : pe->part_table);
2295 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002296 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 +02002297 i + 1, p->boot_ind,
2298 p->head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002299 sector(p->sector),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002300 cylinder(p->sector, p->cyl),
2301 p->end_head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002302 sector(p->end_sector),
2303 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002304 get_start_sect(p),
2305 get_nr_sects(p),
2306 p->sys_ind
2307 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002308 if (p->sys_ind)
2309 check_consistency(p, i);
2310 }
2311 }
2312}
2313#endif
2314
Denis Vlasenko834410a2006-11-29 12:00:28 +00002315#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002316static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002317fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002318{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002319 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002320 const struct pte *pe = &ptes[0];
2321 const struct partition *p;
2322
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002323 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002324 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002325 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002326 first[i] = 0xffffffff;
2327 last[i] = 0;
2328 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002329 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002330 last[i] = first[i] + get_nr_sects(p) - 1;
2331 }
2332 }
2333}
2334
2335static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002336check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002337{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002338 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002339
2340 real_s = sector(s) - 1;
2341 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002342 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002343 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002344 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002345 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002346 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002347 n, h + 1, g_heads);
2348 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002349 printf("Partition %u: sector %u greater than "
2350 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002351 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002352 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2353 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002354 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002355 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2356 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002357}
2358
2359static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002360verify(void)
2361{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002362 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002363 sector_t total = 1;
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002364 sector_t chs_size;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002365 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002366 struct partition *p;
2367
2368 if (warn_geometry())
2369 return;
2370
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002371 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002372 verify_sun();
2373 return;
2374 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002375 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002376 verify_sgi(1);
2377 return;
2378 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002379
2380 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002381 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002382 struct pte *pe = &ptes[i];
2383
2384 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002385 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002386 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002387 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002388 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002389 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002390 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2391 last[i]);
2392 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002393 for (j = 0; j < i; j++) {
2394 if ((first[i] >= first[j] && first[i] <= last[j])
2395 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002396 printf("Warning: partition %u overlaps "
2397 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002398 total += first[i] >= first[j] ?
2399 first[i] : first[j];
2400 total -= last[i] <= last[j] ?
2401 last[i] : last[j];
2402 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002403 }
2404 }
2405 }
2406
2407 if (extended_offset) {
2408 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002409 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002410 get_nr_sects(pex->part_table) - 1;
2411
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002412 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002413 total++;
2414 p = ptes[i].part_table;
2415 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002416 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002417 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002418 "is empty\n", i + 1);
2419 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002420 printf("Logical partition %u not entirely in "
2421 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002422 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002423 }
2424 }
2425
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002426 chs_size = (sector_t)g_heads * g_sectors * g_cylinders;
2427 if (total > chs_size)
2428 printf("Total allocated sectors %u"
2429 " greater than CHS size %"SECT_FMT"u\n",
2430 total, chs_size
2431 );
Denis Vlasenkobd852072007-03-19 14:43:38 +00002432 else {
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002433 total = chs_size - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002434 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002435 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002436 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002437}
2438
2439static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002440add_partition(int n, int sys)
2441{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002442 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002443 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002444 struct partition *p = ptes[n].part_table;
2445 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002446 sector_t limit, temp;
2447 sector_t start, stop = 0;
2448 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002449
2450 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002451 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002452 return;
2453 }
2454 fill_bounds(first, last);
2455 if (n < 4) {
2456 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002457 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002458 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002459 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002460 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002461 if (extended_offset) {
2462 first[ext_index] = extended_offset;
2463 last[ext_index] = get_start_sect(q) +
2464 get_nr_sects(q) - 1;
2465 }
2466 } else {
2467 start = extended_offset + sector_offset;
2468 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2469 }
2470 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002471 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002472 first[i] = (cround(first[i]) - 1) * units_per_sector;
2473
Denis Vlasenkobd852072007-03-19 14:43:38 +00002474 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002475 do {
2476 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002477 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002478 int lastplusoff;
2479
Denys Vlasenkod958e902010-04-06 02:32:26 +02002480 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002481 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002482 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002483 if (start >= first[i] && start <= lastplusoff)
2484 start = lastplusoff + 1;
2485 }
2486 if (start > limit)
2487 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002488 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002489 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002490 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002491 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002492 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002493 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002494 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002495
2496 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002497 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002498 if (display_in_cyl_units) {
2499 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002500 if (start < saved_start)
2501 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002502 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002503 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002504 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002505 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002506 if (n > 4) { /* NOT for fifth partition */
2507 struct pte *pe = &ptes[n];
2508
Denys Vlasenkod958e902010-04-06 02:32:26 +02002509 pe->offset_from_dev_start = start - sector_offset;
2510 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2511 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002512 if (sector_offset == 1)
2513 start++;
2514 }
2515 }
2516
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002517 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002518 struct pte *pe = &ptes[i];
2519
Denys Vlasenkod958e902010-04-06 02:32:26 +02002520 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2521 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002522 if (start < first[i] && limit >= first[i])
2523 limit = first[i] - 1;
2524 }
2525 if (start > limit) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002526 puts("No free sectors available");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002527 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002528 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002529 return;
2530 }
2531 if (cround(start) == cround(limit)) {
2532 stop = limit;
2533 } else {
2534 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002535 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002536 str_units(SINGULAR));
Denys Vlasenkod958e902010-04-06 02:32:26 +02002537 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002538 if (display_in_cyl_units) {
2539 stop = stop * units_per_sector - 1;
2540 if (stop >limit)
2541 stop = limit;
2542 }
2543 }
2544
2545 set_partition(n, 0, start, stop, sys);
2546 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002547 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002548
Rob Landleyb73451d2006-02-24 16:29:00 +00002549 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002550 struct pte *pe4 = &ptes[4];
2551 struct pte *pen = &ptes[n];
2552
2553 ext_index = n;
2554 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002555 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002556 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002557 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2558 pe4->ext_pointer = pe4->part_table + 1;
2559 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002560 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002561 }
2562}
2563
2564static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002565add_logical(void)
2566{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002567 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2568 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002569
Rob Landley081e3842006-08-03 20:07:35 +00002570 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002571 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2572 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002573 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002574 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002575 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002576 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002577 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002578}
2579
2580static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002581new_partition(void)
2582{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002583 int i, free_primary = 0;
2584
2585 if (warn_geometry())
2586 return;
2587
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002588 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002589 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002590 return;
2591 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002592 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002593 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002594 return;
2595 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002596 if (LABEL_IS_AIX) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002597 puts("Sorry - this fdisk cannot handle AIX disk labels.\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002598"If you want to add DOS-type partitions, create a new empty DOS partition\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002599"table first (use 'o'). This will destroy the present disk contents.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002600 return;
2601 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002603 for (i = 0; i < 4; i++)
2604 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002605
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002606 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002607 puts("The maximum number of partitions has been created");
Eric Andersen84bdea82004-05-19 10:49:17 +00002608 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002609 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002610
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002611 if (!free_primary) {
2612 if (extended_offset)
2613 add_logical();
2614 else
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002615 puts("You must delete some partition and add "
2616 "an extended partition first");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002617 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002618 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002619 snprintf(line, sizeof(line),
2620 "Command action\n"
2621 " %s\n"
2622 " p primary partition (1-4)\n",
2623 (extended_offset ?
2624 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002625 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002626 c = read_nonempty(line);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002627 if ((c | 0x20) == 'p') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002628 i = get_nonexisting_partition(0, 4);
2629 if (i >= 0)
2630 add_partition(i, LINUX_NATIVE);
2631 return;
2632 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002633 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002634 add_logical();
2635 return;
2636 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002637 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002638 i = get_nonexisting_partition(0, 4);
2639 if (i >= 0)
2640 add_partition(i, EXTENDED);
2641 return;
2642 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002643 printf("Invalid partition number "
2644 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002645 }
2646 }
2647}
2648
2649static void
Denys Vlasenkob347df92011-08-09 22:49:15 +02002650reread_partition_table(int leave)
2651{
2652 int i;
2653
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002654 puts("Calling ioctl() to re-read partition table");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002655 sync();
2656 /* Users with slow external USB disks on a 320MHz ARM system (year 2011)
2657 * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
2658 */
2659 sleep(1);
2660 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
2661 "WARNING: rereading partition table "
2662 "failed, kernel still uses old table");
2663#if 0
2664 if (dos_changed)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002665 puts(
Denys Vlasenkob347df92011-08-09 22:49:15 +02002666 "\nWARNING: If you have created or modified any DOS 6.x\n"
2667 "partitions, please see the fdisk manual page for additional\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002668 "information");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002669#endif
2670
2671 if (leave) {
2672 if (ENABLE_FEATURE_CLEAN_UP)
2673 close_dev_fd();
2674 exit(i != 0);
2675 }
2676}
2677
2678static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002679write_table(void)
2680{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002681 int i;
2682
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002683 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002684 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002685 if (ptes[i].changed)
2686 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002687 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002689 if (pe->changed) {
2690 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002691 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002692 }
2693 }
2694 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002695 else if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002696 /* no test on change? the "altered" msg below might be mistaken */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002697 sgi_write_table();
2698 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002699 else if (LABEL_IS_SUN) {
Denys Vlasenko80856b32011-07-13 09:06:32 +02002700 for (i = 0; i < 8; i++) {
2701 if (ptes[i].changed) {
2702 sun_write_table();
2703 break;
2704 }
2705 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002707
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002708 puts("The partition table has been altered.");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002709 reread_partition_table(1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002710}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002711#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002712
Denis Vlasenko834410a2006-11-29 12:00:28 +00002713#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002714#define MAX_PER_LINE 16
2715static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002716print_buffer(char *pbuffer)
2717{
2718 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002719
2720 for (i = 0, l = 0; i < sector_size; i++, l++) {
2721 if (l == 0)
2722 printf("0x%03X:", i);
2723 printf(" %02X", (unsigned char) pbuffer[i]);
2724 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002725 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002726 l = -1;
2727 }
2728 }
2729 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002730 bb_putchar('\n');
2731 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002732}
2733
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002734static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002735print_raw(void)
2736{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002737 int i;
2738
Denis Vlasenkobd852072007-03-19 14:43:38 +00002739 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002740 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002741 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002742 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002743 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002744 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002745 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002746}
2747
2748static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002749move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002750{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002751 struct pte *pe = &ptes[i];
2752 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002753 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002754
2755 if (warn_geometry())
2756 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002757 nr_sects = get_nr_sects(p);
2758 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002759 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002760 return;
2761 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002762 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002763 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2764 if (new != first) {
2765 sector_t new_relative = new - pe->offset_from_dev_start;
2766 nr_sects += (get_start_sect(p) - new_relative);
2767 set_start_sect(p, new_relative);
2768 set_nr_sects(p, nr_sects);
2769 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2770 if ((line_ptr[0] | 0x20) == 'y')
2771 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002772 pe->changed = 1;
2773 }
2774}
2775
2776static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002777xselect(void)
2778{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002779 char c;
2780
Rob Landleyb73451d2006-02-24 16:29:00 +00002781 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002782 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002783 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002784 switch (c) {
2785 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002786 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002787 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002788 break;
2789 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002790 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002791 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002792 break;
2793 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002794 user_cylinders = g_cylinders =
2795 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002796 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002797 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002798 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002799 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002800 warn_cylinders();
2801 break;
2802 case 'd':
2803 print_raw();
2804 break;
2805 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002806 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002807 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002808 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002809 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002810 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002811 x_list_table(1);
2812 break;
2813 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002814 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002815 fix_partition_table_order();
2816 break;
2817 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002818#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002819 create_sgilabel();
2820#endif
2821 break;
2822 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002823 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002824 update_units();
2825 break;
2826 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002827 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002829 break;
2830 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002831 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002833 break;
2834 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002835 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002836 list_table(1);
2837 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002838 x_list_table(0);
2839 break;
2840 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002841 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002842 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002843 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002844 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002845 case 'r':
2846 return;
2847 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002848 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002849 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002850 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002851 puts("Warning: setting sector offset for DOS "
Denys Vlasenkoc52dc0e2017-08-10 15:12:11 +02002852 "compatibility");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002853 }
2854 update_units();
2855 break;
2856 case 'v':
2857 verify();
2858 break;
2859 case 'w':
2860 write_table(); /* does not return */
2861 break;
2862 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002863 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002865 break;
2866 default:
2867 xmenu();
2868 }
2869 }
2870}
2871#endif /* ADVANCED mode */
2872
2873static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002874is_ide_cdrom_or_tape(const char *device)
2875{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002876 FILE *procf;
2877 char buf[100];
2878 struct stat statbuf;
2879 int is_ide = 0;
2880
2881 /* No device was given explicitly, and we are trying some
2882 likely things. But opening /dev/hdc may produce errors like
2883 "hdc: tray open or drive not ready"
2884 if it happens to be a CD-ROM drive. It even happens that
2885 the process hangs on the attempt to read a music CD.
2886 So try to be careful. This only works since 2.1.73. */
2887
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002888 if (!is_prefixed_with(device, "/dev/hd"))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002889 return 0;
2890
2891 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002892 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002893 if (procf != NULL && fgets(buf, sizeof(buf), procf))
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002894 is_ide = (is_prefixed_with(buf, "cdrom") ||
2895 is_prefixed_with(buf, "tape"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002896 else
2897 /* Now when this proc file does not exist, skip the
2898 device when it is read-only. */
2899 if (stat(device, &statbuf) == 0)
2900 is_ide = ((statbuf.st_mode & 0222) == 0);
2901
2902 if (procf)
2903 fclose(procf);
2904 return is_ide;
2905}
2906
Rob Landley5527b912006-02-25 03:46:10 +00002907
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002908static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002909open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002910{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002911 int gb;
2912
2913 disk_device = device;
2914 if (setjmp(listingbuf))
2915 return;
2916 if (!user_specified)
2917 if (is_ide_cdrom_or_tape(device))
2918 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002919
2920 /* Open disk_device, save file descriptor to dev_fd */
2921 errno = 0;
2922 gb = get_boot(TRY_ONLY);
2923 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002924 /* Ignore other errors, since we try IDE
2925 and SCSI hard disks which may not be
2926 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002927 if (user_specified || errno == EACCES)
2928 bb_perror_msg("can't open '%s'", device);
2929 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002930 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002931
2932 if (gb < 0) { /* no DOS signature */
2933 list_disk_geometry();
2934 if (LABEL_IS_AIX)
2935 goto ret;
2936#if ENABLE_FEATURE_OSF_LABEL
2937 if (bsd_trydev(device) < 0)
2938#endif
2939 printf("Disk %s doesn't contain a valid "
2940 "partition table\n", device);
2941 } else {
2942 list_table(0);
2943#if ENABLE_FEATURE_FDISK_WRITABLE
2944 if (!LABEL_IS_SUN && g_partitions > 4) {
2945 delete_partition(ext_index);
2946 }
2947#endif
2948 }
2949 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002950 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002951}
2952
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002953/* Is it a whole disk? The digit check is still useful
2954 for Xen devices for example. */
2955static int is_whole_disk(const char *disk)
2956{
2957 unsigned len;
2958 int fd = open(disk, O_RDONLY);
2959
2960 if (fd != -1) {
2961 struct hd_geometry geometry;
2962 int err = ioctl(fd, HDIO_GETGEO, &geometry);
2963 close(fd);
2964 if (!err)
2965 return (geometry.start == 0);
2966 }
2967
2968 /* Treat "nameN" as a partition name, not whole disk */
2969 /* note: mmcblk0 should work from the geometry check above */
2970 len = strlen(disk);
2971 if (len != 0 && isdigit(disk[len - 1]))
2972 return 0;
2973
2974 return 1;
2975}
2976
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002977/* for fdisk -l: try all things in /proc/partitions
2978 that look like a partition name (do not end in a digit) */
2979static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002980list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002981{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002982 FILE *procpt;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002983 char line[100], ptname[100], devname[120];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002984 int ma, mi, sz;
2985
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002986 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002987
2988 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002989 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002990 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002991 continue;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002992
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002993 sprintf(devname, "/dev/%s", ptname);
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002994 if (is_whole_disk(devname))
2995 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002996 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002997#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002998 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002999#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003000}
3001
Denis Vlasenko834410a2006-11-29 12:00:28 +00003002#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003003static void
Rob Landleyb73451d2006-02-24 16:29:00 +00003004unknown_command(int c)
3005{
Denis Vlasenkobd852072007-03-19 14:43:38 +00003006 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003007}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003008#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003009
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003010int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003011int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00003012{
Denis Vlasenko834410a2006-11-29 12:00:28 +00003013 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003014 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003015 * fdisk -v
3016 * fdisk -l [-b sectorsize] [-u] device ...
3017 * fdisk -s [partition] ...
3018 * fdisk [-b sectorsize] [-u] device
3019 *
3020 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003021 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003022 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00003023
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003024 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003025
Denys Vlasenko237bedd2016-07-06 21:58:02 +02003026 opt = getopt32(argv, "b:+C:+H:+lS:+u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003027 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003028 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02003029 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00003030 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02003031 * so cannot be combined with multiple disks,
3032 * and the same goes for the C/H/S options.
3033 */
3034 if (sector_size < 512
3035 || sector_size > 0x10000
3036 || (sector_size & (sector_size-1)) /* not power of 2 */
3037 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00003038 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02003039 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00003040 sector_offset = 2;
3041 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003042 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003043 if (user_heads <= 0 || user_heads >= 256)
3044 user_heads = 0;
3045 if (user_sectors <= 0 || user_sectors >= 64)
3046 user_sectors = 0;
3047 if (opt & OPT_u)
3048 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003049
Denis Vlasenko834410a2006-11-29 12:00:28 +00003050#if ENABLE_FEATURE_FDISK_WRITABLE
3051 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003052 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003053#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003054 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003055 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003056 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00003057 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003058 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003059 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003060 /* we don't have device names, */
3061 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003062 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003063 }
3064 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003065#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003066 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003067#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003068
Denis Vlasenko834410a2006-11-29 12:00:28 +00003069#if ENABLE_FEATURE_FDISK_BLKSIZE
3070 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003071 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003072
3073 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003074 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003075 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003076 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003077 unsigned long long size;
3078 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00003079 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003080 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003081 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003082 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003083 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003084 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003085 }
3086 return 0;
3087 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003088#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003089
Denis Vlasenko834410a2006-11-29 12:00:28 +00003090#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003091 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003092 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003093
Denis Vlasenko834410a2006-11-29 12:00:28 +00003094 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00003095 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003096
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003097 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003098 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00003099 printf("Detected an OSF/1 disklabel on %s, entering "
3100 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003101 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00003102 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00003103 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003104 /* If we return we may want to make an empty DOS label? */
3105 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003106
3107 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00003108 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00003109 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02003110 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003111 switch (c) {
3112 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003113 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003114 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003115 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003116 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003117 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003118 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003119 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003120 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003121 else
3122 unknown_command(c);
3123 break;
3124 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003125 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003126 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00003127 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00003128 if (read_maybe_empty("Please enter the name of the "
Denys Vlasenko69675782013-01-14 01:34:48 +01003129 "new boot file: ") == '\n')
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003130 puts("Boot file unchanged");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003131 else
3132 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003133 }
3134#if ENABLE_FEATURE_OSF_LABEL
3135 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003136 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003137#endif
3138 break;
3139 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003140 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003141 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003142 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003143 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003144 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003145 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003146 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003147 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003148 else
3149 unknown_command(c);
3150 break;
3151 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003152 {
Eric Andersen040f4402003-07-30 08:40:37 +00003153 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00003154 /* If sgi_label then don't use get_existing_partition,
3155 let the user select a partition, since
3156 get_existing_partition() only works for Linux-like
3157 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003158 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003159 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003160 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003161 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003162 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003163 if (j >= 0)
3164 delete_partition(j);
3165 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003166 break;
3167 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003168 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003169 create_sgiinfo();
3170 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003171 unknown_command(c);
3172 case 'l':
3173 list_types(get_sys_types());
3174 break;
3175 case 'm':
3176 menu();
3177 break;
3178 case 'n':
3179 new_partition();
3180 break;
3181 case 'o':
3182 create_doslabel();
3183 break;
3184 case 'p':
3185 list_table(0);
3186 break;
3187 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003188 if (ENABLE_FEATURE_CLEAN_UP)
3189 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003190 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003191 return 0;
3192 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003193#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003194 create_sunlabel();
3195#endif
3196 break;
3197 case 't':
3198 change_sysid();
3199 break;
3200 case 'u':
3201 change_units();
3202 break;
3203 case 'v':
3204 verify();
3205 break;
3206 case 'w':
Denys Vlasenko80856b32011-07-13 09:06:32 +02003207 write_table(); /* does not return */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003208 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003209#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003210 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003211 if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003212 puts("\n\tSorry, no experts menu for SGI "
3213 "partition tables available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003214 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003215 xselect();
3216 break;
3217#endif
3218 default:
3219 unknown_command(c);
3220 menu();
3221 }
3222 }
3223 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003224#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003225}