blob: c4318b6c4131a84c9efbae34d63899403b388cbc [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002/* fdisk.c -- Partition table manipulator for Linux.
3 *
4 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
Mike Frysinger983e0ca2006-02-25 07:42:02 +00005 * Copyright (C) 2001,2002 Vladimir Oleynik <dzo@simtreas.ru> (initial bb port)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00008 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009//config:config FDISK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "fdisk (41 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: The fdisk utility is used to divide hard disks into one or more
15//config: logical disks, which are generally called partitions. This utility
16//config: can be used to list and edit the set of partitions or BSD style
17//config: 'disk slices' that are defined on a hard drive.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//config:
19//config:config FDISK_SUPPORT_LARGE_DISKS
20//config: bool "Support over 4GB disks"
21//config: default y
22//config: depends on FDISK
23//config: depends on !LFS # with LFS no special code is needed
Denys Vlasenkodd898c92016-11-23 11:46:32 +010024//config:
25//config:config FEATURE_FDISK_WRITABLE
26//config: bool "Write support"
27//config: default y
28//config: depends on FDISK
29//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//config: Enabling this option allows you to create or change a partition table
31//config: and write those changes out to disk. If you leave this option
32//config: disabled, you will only be able to view the partition table.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010033//config:
34//config:config FEATURE_AIX_LABEL
35//config: bool "Support AIX disklabels"
36//config: default n
37//config: depends on FDISK && FEATURE_FDISK_WRITABLE
38//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: Enabling this option allows you to create or change AIX disklabels.
40//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010041//config:
42//config:config FEATURE_SGI_LABEL
43//config: bool "Support SGI disklabels"
44//config: default n
45//config: depends on FDISK && FEATURE_FDISK_WRITABLE
46//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020047//config: Enabling this option allows you to create or change SGI disklabels.
48//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010049//config:
50//config:config FEATURE_SUN_LABEL
51//config: bool "Support SUN disklabels"
52//config: default n
53//config: depends on FDISK && FEATURE_FDISK_WRITABLE
54//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020055//config: Enabling this option allows you to create or change SUN disklabels.
56//config: Most people can safely leave this option disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010057//config:
58//config:config FEATURE_OSF_LABEL
59//config: bool "Support BSD disklabels"
60//config: default n
61//config: depends on FDISK && FEATURE_FDISK_WRITABLE
62//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020063//config: Enabling this option allows you to create or change BSD disklabels
64//config: and define and edit BSD disk slices.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010065//config:
66//config:config FEATURE_GPT_LABEL
67//config: bool "Support GPT disklabels"
68//config: default n
69//config: depends on FDISK && FEATURE_FDISK_WRITABLE
70//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020071//config: Enabling this option allows you to view GUID Partition Table
72//config: disklabels.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010073//config:
74//config:config FEATURE_FDISK_ADVANCED
75//config: bool "Support expert mode"
76//config: default y
77//config: depends on FDISK && FEATURE_FDISK_WRITABLE
78//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020079//config: Enabling this option allows you to do terribly unsafe things like
80//config: define arbitrary drive geometry, move the beginning of data in a
81//config: partition, and similarly evil things. Unless you have a very good
82//config: reason you would be wise to leave this disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010083
84//applet:IF_FDISK(APPLET(fdisk, BB_DIR_SBIN, BB_SUID_DROP))
85
86//kbuild:lib-$(CONFIG_FDISK) += fdisk.o
Glenn L McGrath441e7ef2002-11-26 22:00:21 +000087
Pere Orga5bc8c002011-04-11 03:29:49 +020088/* Looks like someone forgot to add this to config system */
89//usage:#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
90//usage:# define ENABLE_FEATURE_FDISK_BLKSIZE 0
91//usage:# define IF_FEATURE_FDISK_BLKSIZE(a)
92//usage:#endif
93//usage:
94//usage:#define fdisk_trivial_usage
95//usage: "[-ul" IF_FEATURE_FDISK_BLKSIZE("s") "] "
96//usage: "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK"
97//usage:#define fdisk_full_usage "\n\n"
98//usage: "Change partition table\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020099//usage: "\n -u Start and End are in sectors (instead of cylinders)"
100//usage: "\n -l Show partition table for each DISK, then exit"
101//usage: IF_FEATURE_FDISK_BLKSIZE(
102//usage: "\n -s Show partition sizes in kb for each DISK, then exit"
103//usage: )
104//usage: "\n -b 2048 (for certain MO disks) use 2048-byte sectors"
105//usage: "\n -C CYLINDERS Set number of cylinders/heads/sectors"
Denys Vlasenko704c6062017-01-19 14:29:42 +0100106//usage: "\n -H HEADS Typically 255"
107//usage: "\n -S SECTORS Typically 63"
Pere Orga5bc8c002011-04-11 03:29:49 +0200108
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000109#ifndef _LARGEFILE64_SOURCE
110/* For lseek64 */
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +0200111# define _LARGEFILE64_SOURCE
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000112#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000113#include <assert.h> /* assert */
Denys Vlasenkoda49f582009-07-08 02:58:38 +0200114#include <sys/mount.h>
115#if !defined(BLKSSZGET)
116# define BLKSSZGET _IO(0x12, 104)
117#endif
Denys Vlasenkoaf3fd142009-09-22 23:16:39 +0200118#if !defined(BLKGETSIZE64)
119# define BLKGETSIZE64 _IOR(0x12,114,size_t)
120#endif
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000121#include "libbb.h"
Denys Vlasenko29516ac2016-11-27 06:13:43 +0100122#include "unicode.h"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000123
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200124#if BB_LITTLE_ENDIAN
125# define inline_if_little_endian ALWAYS_INLINE
126#else
127# define inline_if_little_endian /* nothing */
128#endif
129
130
Denis Vlasenko834410a2006-11-29 12:00:28 +0000131/* Looks like someone forgot to add this to config system */
132#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
133# define ENABLE_FEATURE_FDISK_BLKSIZE 0
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000134# define IF_FEATURE_FDISK_BLKSIZE(a)
Denis Vlasenko834410a2006-11-29 12:00:28 +0000135#endif
136
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000137#define DEFAULT_SECTOR_SIZE 512
138#define DEFAULT_SECTOR_SIZE_STR "512"
139#define MAX_SECTOR_SIZE 2048
140#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
141#define MAXIMUM_PARTS 60
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000142
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000143#define ACTIVE_FLAG 0x80
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000144
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000145#define EXTENDED 0x05
146#define WIN98_EXTENDED 0x0f
147#define LINUX_PARTITION 0x81
148#define LINUX_SWAP 0x82
149#define LINUX_NATIVE 0x83
150#define LINUX_EXTENDED 0x85
151#define LINUX_LVM 0x8e
152#define LINUX_RAID 0xfd
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000153
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000154
155enum {
156 OPT_b = 1 << 0,
157 OPT_C = 1 << 1,
158 OPT_H = 1 << 2,
159 OPT_l = 1 << 3,
160 OPT_S = 1 << 4,
161 OPT_u = 1 << 5,
162 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
163};
164
165
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000166typedef unsigned long long ullong;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200167/* Used for sector numbers. Partition formats we know
168 * do not support more than 2^32 sectors
169 */
170typedef uint32_t sector_t;
171#if UINT_MAX == 4294967295
172# define SECT_FMT ""
173#elif ULONG_MAX == 4294967295
174# define SECT_FMT "l"
175#else
176# error Cant detect sizeof(uint32_t)
177#endif
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +0000178
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000179struct hd_geometry {
Rob Landleyb73451d2006-02-24 16:29:00 +0000180 unsigned char heads;
181 unsigned char sectors;
182 unsigned short cylinders;
183 unsigned long start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000184};
185
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000186#define HDIO_GETGEO 0x0301 /* get device geometry */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000187
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200188/* TODO: #if ENABLE_FEATURE_FDISK_WRITABLE */
189/* (currently fdisk_sun/sgi.c do not have proper WRITABLE #ifs) */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000190static const char msg_building_new_label[] ALIGN1 =
Denis Vlasenkobd852072007-03-19 14:43:38 +0000191"Building a new %s. Changes will remain in memory only,\n"
192"until you decide to write them. After that the previous content\n"
193"won't be recoverable.\n\n";
194
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000195static const char msg_part_already_defined[] ALIGN1 =
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200196"Partition %u is already defined, delete it before re-adding\n";
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200197/* #endif */
Denis Vlasenkobd852072007-03-19 14:43:38 +0000198
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000199
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000200struct partition {
201 unsigned char boot_ind; /* 0x80 - active */
202 unsigned char head; /* starting head */
203 unsigned char sector; /* starting sector */
204 unsigned char cyl; /* starting cylinder */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000205 unsigned char sys_ind; /* what partition type */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000206 unsigned char end_head; /* end head */
207 unsigned char end_sector; /* end sector */
208 unsigned char end_cyl; /* end cylinder */
209 unsigned char start4[4]; /* starting sector counting from 0 */
210 unsigned char size4[4]; /* nr of sectors in partition */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000211} PACKED;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000212
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200213/*
214 * per partition table entry data
215 *
216 * The four primary partitions have the same sectorbuffer (MBRbuffer)
217 * and have NULL ext_pointer.
218 * Each logical partition table entry has two pointers, one for the
219 * partition and one link to the next one.
220 */
221struct pte {
222 struct partition *part_table; /* points into sectorbuffer */
223 struct partition *ext_pointer; /* points into sectorbuffer */
224 sector_t offset_from_dev_start; /* disk sector number */
225 char *sectorbuffer; /* disk sector contents */
226#if ENABLE_FEATURE_FDISK_WRITABLE
227 char changed; /* boolean */
228#endif
229};
230
Denys Vlasenkoea8b2522010-06-02 12:57:26 +0200231#define unable_to_open "can't open '%s'"
232#define unable_to_read "can't read from %s"
233#define unable_to_seek "can't seek on %s"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000234
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000235enum label_type {
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200236 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF, LABEL_GPT
Rob Landley5527b912006-02-25 03:46:10 +0000237};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000238
Denis Vlasenko4437d192008-04-17 00:12:10 +0000239#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000240
Denis Vlasenko834410a2006-11-29 12:00:28 +0000241#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000242#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000243#define STATIC_SUN static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000244#else
245#define LABEL_IS_SUN 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000246#define STATIC_SUN extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000247#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000248
Denis Vlasenko834410a2006-11-29 12:00:28 +0000249#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000250#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000251#define STATIC_SGI static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000252#else
253#define LABEL_IS_SGI 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000254#define STATIC_SGI extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000255#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000256
Denis Vlasenko834410a2006-11-29 12:00:28 +0000257#if ENABLE_FEATURE_AIX_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000258#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000259#define STATIC_AIX static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000260#else
261#define LABEL_IS_AIX 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000262#define STATIC_AIX extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000263#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000264
Denis Vlasenko834410a2006-11-29 12:00:28 +0000265#if ENABLE_FEATURE_OSF_LABEL
Denis Vlasenko4437d192008-04-17 00:12:10 +0000266#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000267#define STATIC_OSF static
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000268#else
269#define LABEL_IS_OSF 0
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000270#define STATIC_OSF extern
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000271#endif
Rob Landley5527b912006-02-25 03:46:10 +0000272
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200273#if ENABLE_FEATURE_GPT_LABEL
274#define LABEL_IS_GPT (LABEL_GPT == current_label_type)
275#define STATIC_GPT static
276#else
277#define LABEL_IS_GPT 0
278#define STATIC_GPT extern
279#endif
280
Denis Vlasenko4437d192008-04-17 00:12:10 +0000281enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000282
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000283static void update_units(void);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000284#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000285static void change_units(void);
Denys Vlasenkob347df92011-08-09 22:49:15 +0200286static void reread_partition_table(int leave);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000287static void delete_partition(int i);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200288static unsigned get_partition(int warn, unsigned max);
Denis Vlasenkobd852072007-03-19 14:43:38 +0000289static void list_types(const char *const *sys);
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200290static 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 +0000291#endif
292static const char *partition_type(unsigned char type);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000293static void get_geometry(void);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200294static void read_pte(struct pte *pe, sector_t offset);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000295#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000296static int get_boot(enum action what);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000297#else
298static int get_boot(void);
299#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000300
301#define PLURAL 0
302#define SINGULAR 1
303
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200304static sector_t get_start_sect(const struct partition *p);
305static sector_t get_nr_sects(const struct partition *p);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000306
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000307/* DOS partition types */
308
309static const char *const i386_sys_types[] = {
310 "\x00" "Empty",
311 "\x01" "FAT12",
312 "\x04" "FAT16 <32M",
313 "\x05" "Extended", /* DOS 3.3+ extended partition */
314 "\x06" "FAT16", /* DOS 16-bit >=32M */
315 "\x07" "HPFS/NTFS", /* OS/2 IFS, eg, HPFS or NTFS or QNX */
316 "\x0a" "OS/2 Boot Manager",/* OS/2 Boot Manager */
317 "\x0b" "Win95 FAT32",
318 "\x0c" "Win95 FAT32 (LBA)",/* LBA really is 'Extended Int 13h' */
319 "\x0e" "Win95 FAT16 (LBA)",
320 "\x0f" "Win95 Ext'd (LBA)",
321 "\x11" "Hidden FAT12",
322 "\x12" "Compaq diagnostics",
323 "\x14" "Hidden FAT16 <32M",
324 "\x16" "Hidden FAT16",
325 "\x17" "Hidden HPFS/NTFS",
326 "\x1b" "Hidden Win95 FAT32",
327 "\x1c" "Hidden W95 FAT32 (LBA)",
328 "\x1e" "Hidden W95 FAT16 (LBA)",
329 "\x3c" "Part.Magic recovery",
330 "\x41" "PPC PReP Boot",
331 "\x42" "SFS",
332 "\x63" "GNU HURD or SysV", /* GNU HURD or Mach or Sys V/386 (such as ISC UNIX) */
333 "\x80" "Old Minix", /* Minix 1.4a and earlier */
334 "\x81" "Minix / old Linux",/* Minix 1.4b and later */
335 "\x82" "Linux swap", /* also Solaris */
336 "\x83" "Linux",
337 "\x84" "OS/2 hidden C: drive",
338 "\x85" "Linux extended",
339 "\x86" "NTFS volume set",
340 "\x87" "NTFS volume set",
341 "\x8e" "Linux LVM",
342 "\x9f" "BSD/OS", /* BSDI */
343 "\xa0" "Thinkpad hibernation",
344 "\xa5" "FreeBSD", /* various BSD flavours */
345 "\xa6" "OpenBSD",
346 "\xa8" "Darwin UFS",
347 "\xa9" "NetBSD",
348 "\xab" "Darwin boot",
349 "\xb7" "BSDI fs",
350 "\xb8" "BSDI swap",
351 "\xbe" "Solaris boot",
352 "\xeb" "BeOS fs",
353 "\xee" "EFI GPT", /* Intel EFI GUID Partition Table */
354 "\xef" "EFI (FAT-12/16/32)", /* Intel EFI System Partition */
355 "\xf0" "Linux/PA-RISC boot", /* Linux/PA-RISC boot loader */
356 "\xf2" "DOS secondary", /* DOS 3.3+ secondary */
357 "\xfd" "Linux raid autodetect", /* New (2.2.x) raid partition with
358 autodetect using persistent
359 superblock */
360#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
361 "\x02" "XENIX root",
362 "\x03" "XENIX usr",
363 "\x08" "AIX", /* AIX boot (AIX -- PS/2 port) or SplitDrive */
364 "\x09" "AIX bootable", /* AIX data or Coherent */
365 "\x10" "OPUS",
366 "\x18" "AST SmartSleep",
367 "\x24" "NEC DOS",
368 "\x39" "Plan 9",
369 "\x40" "Venix 80286",
370 "\x4d" "QNX4.x",
371 "\x4e" "QNX4.x 2nd part",
372 "\x4f" "QNX4.x 3rd part",
373 "\x50" "OnTrack DM",
374 "\x51" "OnTrack DM6 Aux1", /* (or Novell) */
375 "\x52" "CP/M", /* CP/M or Microport SysV/AT */
376 "\x53" "OnTrack DM6 Aux3",
377 "\x54" "OnTrackDM6",
378 "\x55" "EZ-Drive",
379 "\x56" "Golden Bow",
380 "\x5c" "Priam Edisk",
381 "\x61" "SpeedStor",
382 "\x64" "Novell Netware 286",
383 "\x65" "Novell Netware 386",
384 "\x70" "DiskSecure Multi-Boot",
385 "\x75" "PC/IX",
386 "\x93" "Amoeba",
387 "\x94" "Amoeba BBT", /* (bad block table) */
388 "\xa7" "NeXTSTEP",
389 "\xbb" "Boot Wizard hidden",
390 "\xc1" "DRDOS/sec (FAT-12)",
391 "\xc4" "DRDOS/sec (FAT-16 < 32M)",
392 "\xc6" "DRDOS/sec (FAT-16)",
393 "\xc7" "Syrinx",
394 "\xda" "Non-FS data",
395 "\xdb" "CP/M / CTOS / ...",/* CP/M or Concurrent CP/M or
396 Concurrent DOS or CTOS */
397 "\xde" "Dell Utility", /* Dell PowerEdge Server utilities */
398 "\xdf" "BootIt", /* BootIt EMBRM */
399 "\xe1" "DOS access", /* DOS access or SpeedStor 12-bit FAT
400 extended partition */
401 "\xe3" "DOS R/O", /* DOS R/O or SpeedStor */
402 "\xe4" "SpeedStor", /* SpeedStor 16-bit FAT extended
403 partition < 1024 cyl. */
404 "\xf1" "SpeedStor",
405 "\xf4" "SpeedStor", /* SpeedStor large partition */
406 "\xfe" "LANstep", /* SpeedStor >1024 cyl. or LANstep */
407 "\xff" "BBT", /* Xenix Bad Block Table */
408#endif
409 NULL
410};
411
Denis Vlasenko4437d192008-04-17 00:12:10 +0000412enum {
413 dev_fd = 3 /* the disk */
414};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000415
416/* Globals */
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000417struct globals {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000418 char *line_ptr;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000419
420 const char *disk_device;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000421 int g_partitions; // = 4; /* maximum partition + 1 */
422 unsigned units_per_sector; // = 1;
423 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
424 unsigned user_set_sector_size;
425 unsigned sector_offset; // = 1;
426 unsigned g_heads, g_sectors, g_cylinders;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000427 smallint /* enum label_type */ current_label_type;
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000428 smallint display_in_cyl_units; // = 1;
429#if ENABLE_FEATURE_OSF_LABEL
430 smallint possibly_osf_label;
431#endif
432
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000433 smallint listing; /* no aborts for fdisk -l */
434 smallint dos_compatible_flag; // = 1;
435#if ENABLE_FEATURE_FDISK_WRITABLE
436 //int dos_changed;
437 smallint nowarn; /* no warnings for fdisk -l/-s */
438#endif
439 int ext_index; /* the prime extended partition */
440 unsigned user_cylinders, user_heads, user_sectors;
441 unsigned pt_heads, pt_sectors;
442 unsigned kern_heads, kern_sectors;
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200443 sector_t extended_offset; /* offset of link pointers */
444 sector_t total_number_of_sectors;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000445
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000446 jmp_buf listingbuf;
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000447 char line_buffer[80];
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +0000448 /* Raw disk label. For DOS-type partition tables the MBR,
449 * with descriptions of the primary partitions. */
450 char MBRbuffer[MAX_SECTOR_SIZE];
451 /* Partition tables */
452 struct pte ptes[MAXIMUM_PARTS];
453};
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000454#define G (*ptr_to_globals)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000455#define line_ptr (G.line_ptr )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000456#define disk_device (G.disk_device )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000457#define g_partitions (G.g_partitions )
458#define units_per_sector (G.units_per_sector )
459#define sector_size (G.sector_size )
460#define user_set_sector_size (G.user_set_sector_size)
461#define sector_offset (G.sector_offset )
462#define g_heads (G.g_heads )
463#define g_sectors (G.g_sectors )
464#define g_cylinders (G.g_cylinders )
465#define current_label_type (G.current_label_type )
466#define display_in_cyl_units (G.display_in_cyl_units)
467#define possibly_osf_label (G.possibly_osf_label )
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000468#define listing (G.listing )
469#define dos_compatible_flag (G.dos_compatible_flag )
470#define nowarn (G.nowarn )
471#define ext_index (G.ext_index )
472#define user_cylinders (G.user_cylinders )
473#define user_heads (G.user_heads )
474#define user_sectors (G.user_sectors )
475#define pt_heads (G.pt_heads )
476#define pt_sectors (G.pt_sectors )
477#define kern_heads (G.kern_heads )
478#define kern_sectors (G.kern_sectors )
479#define extended_offset (G.extended_offset )
480#define total_number_of_sectors (G.total_number_of_sectors)
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000481#define listingbuf (G.listingbuf )
482#define line_buffer (G.line_buffer )
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000483#define MBRbuffer (G.MBRbuffer )
484#define ptes (G.ptes )
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000485#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000486 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000487 sector_size = DEFAULT_SECTOR_SIZE; \
488 sector_offset = 1; \
489 g_partitions = 4; \
490 display_in_cyl_units = 1; \
491 units_per_sector = 1; \
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000492 dos_compatible_flag = 1; \
Denis Vlasenkof77f3692007-12-16 17:22:33 +0000493} while (0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000494
Denis Vlasenkobd852072007-03-19 14:43:38 +0000495
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000496/* TODO: move to libbb? */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200497/* TODO: return unsigned long long, FEATURE_FDISK_BLKSIZE _can_ handle
498 * disks > 2^32 sectors
499 */
500static sector_t bb_BLKGETSIZE_sectors(int fd)
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000501{
502 uint64_t v64;
503 unsigned long longsectors;
504
505 if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
Denis Vlasenko270d5d72008-06-29 05:16:45 +0000506 /* Got bytes, convert to 512 byte sectors */
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200507 v64 >>= 9;
508 if (v64 != (sector_t)v64) {
509 ret_trunc:
510 /* Not only DOS, but all other partition tables
511 * we support can't record more than 32 bit
512 * sector counts or offsets
513 */
514 bb_error_msg("device has more than 2^32 sectors, can't use all of them");
515 v64 = (uint32_t)-1L;
516 }
517 return v64;
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000518 }
519 /* Needs temp of type long */
Denys Vlasenkod958e902010-04-06 02:32:26 +0200520 if (ioctl(fd, BLKGETSIZE, &longsectors)) {
521 /* Perhaps this is a disk image */
522 off_t sz = lseek(fd, 0, SEEK_END);
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000523 longsectors = 0;
Denys Vlasenkod958e902010-04-06 02:32:26 +0200524 if (sz > 0)
525 longsectors = (uoff_t)sz / sector_size;
526 lseek(fd, 0, SEEK_SET);
527 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200528 if (sizeof(long) > sizeof(sector_t)
529 && longsectors != (sector_t)longsectors
530 ) {
531 goto ret_trunc;
532 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +0000533 return longsectors;
534}
535
536
Denis Vlasenkodfce08f2007-03-19 14:45:10 +0000537#define IS_EXTENDED(i) \
538 ((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
539
540#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
541
542#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
543
544#define pt_offset(b, n) \
545 ((struct partition *)((b) + 0x1be + (n) * sizeof(struct partition)))
546
547#define sector(s) ((s) & 0x3f)
548
549#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2))
550
Denis Vlasenkoc033d512008-04-17 01:52:28 +0000551static void
552close_dev_fd(void)
553{
554 /* Not really closing, but making sure it is open, and to harmless place */
555 xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
556}
557
Denys Vlasenkob157eb12016-09-17 21:05:06 +0200558/* Return partition name */
559static const char *
560partname(const char *dev, int pno, int lth)
561{
562 const char *p;
563 int w, wp;
564 int bufsiz;
565 char *bufp;
566
567 bufp = auto_string(xzalloc(80));
568 bufsiz = 80;
569
570 w = strlen(dev);
571 p = "";
572
573 if (isdigit(dev[w-1]))
574 p = "p";
575
576 /* devfs kludge - note: fdisk partition names are not supposed
577 to equal kernel names, so there is no reason to do this */
578 if (strcmp(dev + w - 4, "disc") == 0) {
579 w -= 4;
580 p = "part";
581 }
582
583 wp = strlen(p);
584
585 if (lth) {
586 snprintf(bufp, bufsiz, "%*.*s%s%-2u",
587 lth-wp-2, w, dev, p, pno);
588 } else {
589 snprintf(bufp, bufsiz, "%.*s%s%-2u", w, dev, p, pno);
590 }
591 return bufp;
592}
593
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000594static ALWAYS_INLINE struct partition *
Rob Landleyb73451d2006-02-24 16:29:00 +0000595get_part_table(int i)
596{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000597 return ptes[i].part_table;
598}
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000599
600static const char *
Rob Landleyb73451d2006-02-24 16:29:00 +0000601str_units(int n)
602{ /* n==1: use singular */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000603 if (n == 1)
Denis Vlasenkobd852072007-03-19 14:43:38 +0000604 return display_in_cyl_units ? "cylinder" : "sector";
605 return display_in_cyl_units ? "cylinders" : "sectors";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000606}
607
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000608static int
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000609valid_part_table_flag(const char *mbuffer)
610{
Denis Vlasenko834410a2006-11-29 12:00:28 +0000611 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000612}
613
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200614static void fdisk_fatal(const char *why)
615{
616 if (listing) {
617 close_dev_fd();
618 longjmp(listingbuf, 1);
619 }
620 bb_error_msg_and_die(why, disk_device);
621}
622
623static void
624seek_sector(sector_t secno)
625{
626#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
627 off64_t off = (off64_t)secno * sector_size;
628 if (lseek64(dev_fd, off, SEEK_SET) == (off64_t) -1)
629 fdisk_fatal(unable_to_seek);
630#else
631 uint64_t off = (uint64_t)secno * sector_size;
632 if (off > MAXINT(off_t)
633 || lseek(dev_fd, (off_t)off, SEEK_SET) == (off_t) -1
634 ) {
635 fdisk_fatal(unable_to_seek);
636 }
637#endif
638}
639
Denis Vlasenko834410a2006-11-29 12:00:28 +0000640#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200641/* Read line; return 0 or first printable char */
642static int
643read_line(const char *prompt)
644{
645 int sz;
646
Denys Vlasenko84ea60e2017-08-02 17:27:28 +0200647 sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer));
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200648 if (sz <= 0)
649 exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
650
651 if (line_buffer[sz-1] == '\n')
652 line_buffer[--sz] = '\0';
653
654 line_ptr = line_buffer;
655 while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
656 line_ptr++;
657 return *line_ptr;
658}
659
660static void
661set_all_unchanged(void)
662{
663 int i;
664
665 for (i = 0; i < MAXIMUM_PARTS; i++)
666 ptes[i].changed = 0;
667}
668
669static ALWAYS_INLINE void
670set_changed(int i)
671{
672 ptes[i].changed = 1;
673}
674
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000675static ALWAYS_INLINE void
Denis Vlasenko834410a2006-11-29 12:00:28 +0000676write_part_table_flag(char *b)
677{
678 b[510] = 0x55;
679 b[511] = 0xaa;
680}
681
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000682static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000683read_nonempty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000684{
Denis Vlasenko9764d692008-07-09 21:20:50 +0000685 while (!read_line(mesg))
686 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000687 return *line_ptr;
688}
689
690static char
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000691read_maybe_empty(const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000692{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000693 if (!read_line(mesg)) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000694 line_ptr = line_buffer;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000695 line_ptr[0] = '\n';
696 line_ptr[1] = '\0';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000697 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000698 return line_ptr[0];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000699}
700
701static int
Denis Vlasenkobd852072007-03-19 14:43:38 +0000702read_hex(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000703{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000704 unsigned long v;
Rob Landleyb73451d2006-02-24 16:29:00 +0000705 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000706 read_nonempty("Hex code (type L to list codes): ");
Denys Vlasenkod958e902010-04-06 02:32:26 +0200707 if ((line_ptr[0] | 0x20) == 'l') {
Rob Landleyb73451d2006-02-24 16:29:00 +0000708 list_types(sys);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000709 continue;
Rob Landleyb73451d2006-02-24 16:29:00 +0000710 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000711 v = bb_strtoul(line_ptr, NULL, 16);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200712 if (v <= 0xff)
713 return v;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000714 }
715}
716
Denis Vlasenko9764d692008-07-09 21:20:50 +0000717static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200718write_sector(sector_t secno, const void *buf)
Denis Vlasenko9764d692008-07-09 21:20:50 +0000719{
720 seek_sector(secno);
721 xwrite(dev_fd, buf, sector_size);
722}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200723#endif /* FEATURE_FDISK_WRITABLE */
Denis Vlasenko9764d692008-07-09 21:20:50 +0000724
725
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000726#include "fdisk_aix.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000727
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100728struct sun_partition {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000729 unsigned char info[128]; /* Informative text string */
730 unsigned char spare0[14];
731 struct sun_info {
732 unsigned char spare1;
733 unsigned char id;
734 unsigned char spare2;
735 unsigned char flags;
736 } infos[8];
737 unsigned char spare1[246]; /* Boot information etc. */
738 unsigned short rspeed; /* Disk rotational speed */
739 unsigned short pcylcount; /* Physical cylinder count */
740 unsigned short sparecyl; /* extra sects per cylinder */
741 unsigned char spare2[4]; /* More magic... */
742 unsigned short ilfact; /* Interleave factor */
743 unsigned short ncyl; /* Data cylinder count */
744 unsigned short nacyl; /* Alt. cylinder count */
745 unsigned short ntrks; /* Tracks per cylinder */
746 unsigned short nsect; /* Sectors per track */
747 unsigned char spare3[4]; /* Even more magic... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000748 struct sun_partinfo {
Eric Andersenacd244a2002-12-11 03:49:33 +0000749 uint32_t start_cylinder;
750 uint32_t num_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000751 } partitions[8];
752 unsigned short magic; /* Magic number */
753 unsigned short csum; /* Label xor'd checksum */
Denys Vlasenko36659fd2010-02-05 14:40:23 +0100754} FIX_ALIASING;
755typedef struct sun_partition sun_partition;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000756#define sunlabel ((sun_partition *)MBRbuffer)
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000757STATIC_OSF void bsd_select(void);
758STATIC_OSF void xbsd_print_disklabel(int);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000759#include "fdisk_osf.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000760
Denys Vlasenkod7559c22010-10-30 02:40:08 +0200761STATIC_GPT void gpt_list_table(int xtra);
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200762#include "fdisk_gpt.c"
763
Denis Vlasenko28703012006-12-19 20:32:02 +0000764#if ENABLE_FEATURE_SGI_LABEL || ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000765static uint16_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000766fdisk_swap16(uint16_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000767{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000768 return (x << 8) | (x >> 8);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000769}
770
Rob Landley88621d72006-08-29 19:41:06 +0000771static uint32_t
Denis Vlasenko28703012006-12-19 20:32:02 +0000772fdisk_swap32(uint32_t x)
Rob Landleyb73451d2006-02-24 16:29:00 +0000773{
Denis Vlasenko10d0d4e2006-11-27 16:48:17 +0000774 return (x << 24) |
775 ((x & 0xFF00) << 8) |
776 ((x & 0xFF0000) >> 8) |
777 (x >> 24);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000778}
779#endif
780
Denis Vlasenkobd852072007-03-19 14:43:38 +0000781STATIC_SGI const char *const sgi_sys_types[];
Denis Vlasenko834410a2006-11-29 12:00:28 +0000782STATIC_SGI unsigned sgi_get_num_sectors(int i);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000783STATIC_SGI int sgi_get_sysid(int i);
784STATIC_SGI void sgi_delete_partition(int i);
785STATIC_SGI void sgi_change_sysid(int i, int sys);
786STATIC_SGI void sgi_list_table(int xtra);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000787#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000788STATIC_SGI void sgi_set_xcyl(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000789#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000790STATIC_SGI int verify_sgi(int verbose);
791STATIC_SGI void sgi_add_partition(int n, int sys);
792STATIC_SGI void sgi_set_swappartition(int i);
793STATIC_SGI const char *sgi_get_bootfile(void);
794STATIC_SGI void sgi_set_bootfile(const char* aFile);
795STATIC_SGI void create_sgiinfo(void);
796STATIC_SGI void sgi_write_table(void);
797STATIC_SGI void sgi_set_bootpartition(int i);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000798#include "fdisk_sgi.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000799
Denis Vlasenkobd852072007-03-19 14:43:38 +0000800STATIC_SUN const char *const sun_sys_types[];
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000801STATIC_SUN void sun_delete_partition(int i);
802STATIC_SUN void sun_change_sysid(int i, int sys);
803STATIC_SUN void sun_list_table(int xtra);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000804STATIC_SUN void add_sun_partition(int n, int sys);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000805#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000806STATIC_SUN void sun_set_alt_cyl(void);
807STATIC_SUN void sun_set_ncyl(int cyl);
808STATIC_SUN void sun_set_xcyl(void);
809STATIC_SUN void sun_set_ilfact(void);
810STATIC_SUN void sun_set_rspeed(void);
811STATIC_SUN void sun_set_pcylcount(void);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000812#endif
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +0000813STATIC_SUN void toggle_sunflags(int i, unsigned char mask);
814STATIC_SUN void verify_sun(void);
815STATIC_SUN void sun_write_table(void);
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000816#include "fdisk_sun.c"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000817
Denis Vlasenko9764d692008-07-09 21:20:50 +0000818
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200819static inline_if_little_endian unsigned
Rob Landleyb73451d2006-02-24 16:29:00 +0000820read4_little_endian(const unsigned char *cp)
821{
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200822 uint32_t v;
823 move_from_unaligned32(v, cp);
824 return SWAP_LE32(v);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000825}
826
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200827static sector_t
Rob Landleyb73451d2006-02-24 16:29:00 +0000828get_start_sect(const struct partition *p)
829{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000830 return read4_little_endian(p->start4);
831}
832
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200833static sector_t
834get_nr_sects(const struct partition *p)
835{
836 return read4_little_endian(p->size4);
837}
838
Denis Vlasenko834410a2006-11-29 12:00:28 +0000839#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenko5ea1de22010-04-06 02:31:43 +0200840/* start_sect and nr_sects are stored little endian on all machines */
841/* moreover, they are not aligned correctly */
842static inline_if_little_endian void
843store4_little_endian(unsigned char *cp, unsigned val)
844{
845 uint32_t v = SWAP_LE32(val);
846 move_to_unaligned32(cp, v);
847}
848
849static void
850set_start_sect(struct partition *p, unsigned start_sect)
851{
852 store4_little_endian(p->start4, start_sect);
853}
854
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000855static void
Denis Vlasenko28703012006-12-19 20:32:02 +0000856set_nr_sects(struct partition *p, unsigned nr_sects)
Rob Landleyb73451d2006-02-24 16:29:00 +0000857{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000858 store4_little_endian(p->size4, nr_sects);
859}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000860#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000861
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000862/* Allocate a buffer and read a partition table sector */
863static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200864read_pte(struct pte *pe, sector_t offset)
Rob Landleyb73451d2006-02-24 16:29:00 +0000865{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200866 pe->offset_from_dev_start = offset;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000867 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000868 seek_sector(offset);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +0000869 /* xread would make us abort - bad for fdisk -l */
870 if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000871 fdisk_fatal(unable_to_read);
Denis Vlasenko834410a2006-11-29 12:00:28 +0000872#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000873 pe->changed = 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000874#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000875 pe->part_table = pe->ext_pointer = NULL;
876}
877
Denys Vlasenkoddf78502009-09-16 03:03:13 +0200878static sector_t
Denys Vlasenkod958e902010-04-06 02:32:26 +0200879get_partition_start_from_dev_start(const struct pte *pe)
Rob Landleyb73451d2006-02-24 16:29:00 +0000880{
Denys Vlasenkod958e902010-04-06 02:32:26 +0200881 return pe->offset_from_dev_start + get_start_sect(pe->part_table);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000882}
883
Denis Vlasenko834410a2006-11-29 12:00:28 +0000884#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000885/*
886 * Avoid warning about DOS partitions when no DOS partition was changed.
887 * Here a heuristic "is probably dos partition".
888 * We might also do the opposite and warn in all cases except
889 * for "is probably nondos partition".
890 */
Denis Vlasenko89398812008-01-25 20:18:46 +0000891#ifdef UNUSED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000892static int
Rob Landleyb73451d2006-02-24 16:29:00 +0000893is_dos_partition(int t)
894{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000895 return (t == 1 || t == 4 || t == 6 ||
896 t == 0x0b || t == 0x0c || t == 0x0e ||
897 t == 0x11 || t == 0x12 || t == 0x14 || t == 0x16 ||
898 t == 0x1b || t == 0x1c || t == 0x1e || t == 0x24 ||
899 t == 0xc1 || t == 0xc4 || t == 0xc6);
900}
Denis Vlasenko89398812008-01-25 20:18:46 +0000901#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000902
903static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000904menu(void)
905{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000906 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000907 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000908 puts("a\ttoggle a read only flag"); /* sun */
909 puts("b\tedit bsd disklabel");
910 puts("c\ttoggle the mountable flag"); /* sun */
911 puts("d\tdelete a partition");
912 puts("l\tlist known partition types");
913 puts("n\tadd a new partition");
914 puts("o\tcreate a new empty DOS partition table");
915 puts("p\tprint the partition table");
916 puts("q\tquit without saving changes");
917 puts("s\tcreate a new empty Sun disklabel"); /* sun */
918 puts("t\tchange a partition's system id");
919 puts("u\tchange display/entry units");
920 puts("v\tverify the partition table");
921 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000922#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000923 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000924#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000925 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000926 puts("a\tselect bootable partition"); /* sgi flavour */
927 puts("b\tedit bootfile entry"); /* sgi */
928 puts("c\tselect sgi swap partition"); /* sgi flavour */
929 puts("d\tdelete a partition");
930 puts("l\tlist known partition types");
931 puts("n\tadd a new partition");
932 puts("o\tcreate a new empty DOS partition table");
933 puts("p\tprint the partition table");
934 puts("q\tquit without saving changes");
935 puts("s\tcreate a new empty Sun disklabel"); /* sun */
936 puts("t\tchange a partition's system id");
937 puts("u\tchange display/entry units");
938 puts("v\tverify the partition table");
939 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000940 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000941 puts("o\tcreate a new empty DOS partition table");
942 puts("q\tquit without saving changes");
943 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Kevin Cernekeeccb07042010-10-25 02:00:24 +0200944 } else if (LABEL_IS_GPT) {
945 puts("o\tcreate a new empty DOS partition table");
946 puts("p\tprint the partition table");
947 puts("q\tquit without saving changes");
948 puts("s\tcreate a new empty Sun disklabel"); /* sun */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000949 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000950 puts("a\ttoggle a bootable flag");
951 puts("b\tedit bsd disklabel");
952 puts("c\ttoggle the dos compatibility flag");
953 puts("d\tdelete a partition");
954 puts("l\tlist known partition types");
955 puts("n\tadd a new partition");
956 puts("o\tcreate a new empty DOS partition table");
957 puts("p\tprint the partition table");
958 puts("q\tquit without saving changes");
959 puts("s\tcreate a new empty Sun disklabel"); /* sun */
960 puts("t\tchange a partition's system id");
961 puts("u\tchange display/entry units");
962 puts("v\tverify the partition table");
963 puts("w\twrite table to disk and exit");
Denis Vlasenko834410a2006-11-29 12:00:28 +0000964#if ENABLE_FEATURE_FDISK_ADVANCED
Denis Vlasenkobd852072007-03-19 14:43:38 +0000965 puts("x\textra functionality (experts only)");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000966#endif
967 }
968}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000969#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +0000970
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000971
Denis Vlasenko834410a2006-11-29 12:00:28 +0000972#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +0000973static void
Rob Landleyb73451d2006-02-24 16:29:00 +0000974xmenu(void)
975{
Denis Vlasenkobd852072007-03-19 14:43:38 +0000976 puts("Command Action");
Denis Vlasenko98ae2162006-10-12 19:30:44 +0000977 if (LABEL_IS_SUN) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000978 puts("a\tchange number of alternate cylinders"); /*sun*/
979 puts("c\tchange number of cylinders");
980 puts("d\tprint the raw data in the partition table");
981 puts("e\tchange number of extra sectors per cylinder");/*sun*/
982 puts("h\tchange number of heads");
983 puts("i\tchange interleave factor"); /*sun*/
984 puts("o\tchange rotation speed (rpm)"); /*sun*/
985 puts("p\tprint the partition table");
986 puts("q\tquit without saving changes");
987 puts("r\treturn to main menu");
988 puts("s\tchange number of sectors/track");
989 puts("v\tverify the partition table");
990 puts("w\twrite table to disk and exit");
991 puts("y\tchange number of physical cylinders"); /*sun*/
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000992 } else if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +0000993 puts("b\tmove beginning of data in a partition"); /* !sun */
994 puts("c\tchange number of cylinders");
995 puts("d\tprint the raw data in the partition table");
996 puts("e\tlist extended partitions"); /* !sun */
997 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
998 puts("h\tchange number of heads");
999 puts("p\tprint the partition table");
1000 puts("q\tquit without saving changes");
1001 puts("r\treturn to main menu");
1002 puts("s\tchange number of sectors/track");
1003 puts("v\tverify the partition table");
1004 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001005 } else if (LABEL_IS_AIX) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001006 puts("b\tmove beginning of data in a partition"); /* !sun */
1007 puts("c\tchange number of cylinders");
1008 puts("d\tprint the raw data in the partition table");
1009 puts("e\tlist extended partitions"); /* !sun */
1010 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
1011 puts("h\tchange number of heads");
1012 puts("p\tprint the partition table");
1013 puts("q\tquit without saving changes");
1014 puts("r\treturn to main menu");
1015 puts("s\tchange number of sectors/track");
1016 puts("v\tverify the partition table");
1017 puts("w\twrite table to disk and exit");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001018 } else {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001019 puts("b\tmove beginning of data in a partition"); /* !sun */
1020 puts("c\tchange number of cylinders");
1021 puts("d\tprint the raw data in the partition table");
1022 puts("e\tlist extended partitions"); /* !sun */
1023 puts("f\tfix partition order"); /* !sun, !aix, !sgi */
Denis Vlasenko834410a2006-11-29 12:00:28 +00001024#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenkobd852072007-03-19 14:43:38 +00001025 puts("g\tcreate an IRIX (SGI) partition table");/* sgi */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001026#endif
Denis Vlasenkobd852072007-03-19 14:43:38 +00001027 puts("h\tchange number of heads");
1028 puts("p\tprint the partition table");
1029 puts("q\tquit without saving changes");
1030 puts("r\treturn to main menu");
1031 puts("s\tchange number of sectors/track");
1032 puts("v\tverify the partition table");
1033 puts("w\twrite table to disk and exit");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001034 }
1035}
1036#endif /* ADVANCED mode */
1037
Denis Vlasenko834410a2006-11-29 12:00:28 +00001038#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenkobd852072007-03-19 14:43:38 +00001039static const char *const *
Rob Landleyb73451d2006-02-24 16:29:00 +00001040get_sys_types(void)
1041{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001042 return (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001043 LABEL_IS_SUN ? sun_sys_types :
1044 LABEL_IS_SGI ? sgi_sys_types :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001045 i386_sys_types);
1046}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001047#else
1048#define get_sys_types() i386_sys_types
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001049#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001050
Denis Vlasenkobd852072007-03-19 14:43:38 +00001051static const char *
1052partition_type(unsigned char type)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001053{
1054 int i;
Denis Vlasenkobd852072007-03-19 14:43:38 +00001055 const char *const *types = get_sys_types();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001056
Denis Vlasenkobd852072007-03-19 14:43:38 +00001057 for (i = 0; types[i]; i++)
1058 if ((unsigned char)types[i][0] == type)
1059 return types[i] + 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001060
Denis Vlasenkobd852072007-03-19 14:43:38 +00001061 return "Unknown";
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001062}
1063
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001064static int
1065is_cleared_partition(const struct partition *p)
1066{
1067 /* We consider partition "cleared" only if it has only zeros */
1068 const char *cp = (const char *)p;
1069 int cnt = sizeof(*p);
1070 char bits = 0;
1071 while (--cnt >= 0)
1072 bits |= *cp++;
1073 return (bits == 0);
1074}
1075
1076static void
1077clear_partition(struct partition *p)
1078{
1079 if (p)
1080 memset(p, 0, sizeof(*p));
1081}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001082
Denis Vlasenko834410a2006-11-29 12:00:28 +00001083#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001084static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001085get_sysid(int i)
1086{
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001087 return LABEL_IS_SUN ? sunlabel->infos[i].id :
1088 (LABEL_IS_SGI ? sgi_get_sysid(i) :
1089 ptes[i].part_table->sys_ind);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001090}
1091
Denis Vlasenkobd852072007-03-19 14:43:38 +00001092static void
1093list_types(const char *const *sys)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001094{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001095 enum { COLS = 3 };
1096
1097 unsigned last[COLS];
1098 unsigned done, next, size;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001099 int i;
1100
Denis Vlasenko9764d692008-07-09 21:20:50 +00001101 for (size = 0; sys[size]; size++)
1102 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001103
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001104 done = 0;
1105 for (i = COLS-1; i >= 0; i--) {
1106 done += (size + i - done) / (i + 1);
1107 last[COLS-1 - i] = done;
1108 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001109
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001110 i = done = next = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001111 do {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001112 printf("%c%2x %-22.22s", i ? ' ' : '\n',
Denis Vlasenkobd852072007-03-19 14:43:38 +00001113 (unsigned char)sys[next][0],
1114 sys[next] + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001115 next = last[i++] + done;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001116 if (i >= COLS || next >= last[i]) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001117 i = 0;
1118 next = ++done;
1119 }
1120 } while (done < last[0]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001121 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001122}
1123
Denys Vlasenkod958e902010-04-06 02:32:26 +02001124#define set_hsc(h, s, c, sector) do \
1125{ \
1126 s = sector % g_sectors + 1; \
1127 sector /= g_sectors; \
1128 h = sector % g_heads; \
1129 sector /= g_heads; \
1130 c = sector & 0xff; \
1131 s |= (sector >> 2) & 0xc0; \
1132} while (0)
1133
1134static void set_hsc_start_end(struct partition *p, sector_t start, sector_t stop)
1135{
1136 if (dos_compatible_flag && (start / (g_sectors * g_heads) > 1023))
1137 start = g_heads * g_sectors * 1024 - 1;
1138 set_hsc(p->head, p->sector, p->cyl, start);
1139
1140 if (dos_compatible_flag && (stop / (g_sectors * g_heads) > 1023))
1141 stop = g_heads * g_sectors * 1024 - 1;
1142 set_hsc(p->end_head, p->end_sector, p->end_cyl, stop);
1143}
1144
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001145static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001146set_partition(int i, int doext, sector_t start, sector_t stop, int sysid)
Rob Landleyb73451d2006-02-24 16:29:00 +00001147{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001148 struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001149 sector_t offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001150
1151 if (doext) {
1152 p = ptes[i].ext_pointer;
1153 offset = extended_offset;
1154 } else {
1155 p = ptes[i].part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001156 offset = ptes[i].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001157 }
1158 p->boot_ind = 0;
1159 p->sys_ind = sysid;
1160 set_start_sect(p, start - offset);
1161 set_nr_sects(p, stop - start + 1);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001162 set_hsc_start_end(p, start, stop);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001163 ptes[i].changed = 1;
1164}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001165#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001166
1167static int
Rob Landleyb73451d2006-02-24 16:29:00 +00001168warn_geometry(void)
1169{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001170 if (g_heads && g_sectors && g_cylinders)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001171 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001172
Denis Vlasenkobd852072007-03-19 14:43:38 +00001173 printf("Unknown value(s) for:");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001174 if (!g_heads)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001175 printf(" heads");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001176 if (!g_sectors)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001177 printf(" sectors");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001178 if (!g_cylinders)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001179 printf(" cylinders");
Denis Vlasenko834410a2006-11-29 12:00:28 +00001180#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001181 puts(" (settable in the extra functions menu)");
1182#else
1183 bb_putchar('\n');
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001184#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001185 return 1;
1186}
1187
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00001188static void
1189update_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001190{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001191 int cyl_units = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001192
1193 if (display_in_cyl_units && cyl_units)
1194 units_per_sector = cyl_units;
1195 else
1196 units_per_sector = 1; /* in sectors */
1197}
1198
Denis Vlasenko834410a2006-11-29 12:00:28 +00001199#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001200static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001201warn_cylinders(void)
1202{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001203 if (LABEL_IS_DOS && g_cylinders > 1024 && !nowarn)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001204 printf("\n"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001205"The number of cylinders for this disk is set to %u.\n"
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001206"There is nothing wrong with that, but this is larger than 1024,\n"
1207"and could in certain setups cause problems with:\n"
1208"1) software that runs at boot time (e.g., old versions of LILO)\n"
1209"2) booting and partitioning software from other OSs\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00001210" (e.g., DOS FDISK, OS/2 FDISK)\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001211 g_cylinders);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001212}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001213#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001214
1215static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001216read_extended(int ext)
1217{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001218 int i;
1219 struct pte *pex;
1220 struct partition *p, *q;
1221
1222 ext_index = ext;
1223 pex = &ptes[ext];
1224 pex->ext_pointer = pex->part_table;
1225
1226 p = pex->part_table;
1227 if (!get_start_sect(p)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001228 puts("Bad offset in primary extended partition");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001229 return;
1230 }
1231
Rob Landleyb73451d2006-02-24 16:29:00 +00001232 while (IS_EXTENDED(p->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001233 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001234
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001235 if (g_partitions >= MAXIMUM_PARTS) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001236 /* This is not a Linux restriction, but
1237 this program uses arrays of size MAXIMUM_PARTS.
Denis Vlasenko89f0b342006-11-18 22:04:09 +00001238 Do not try to 'improve' this test. */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001239 struct pte *pre = &ptes[g_partitions - 1];
Denis Vlasenko834410a2006-11-29 12:00:28 +00001240#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001241 printf("Warning: deleting partitions after %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001242 g_partitions);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001243 pre->changed = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001244#endif
1245 clear_partition(pre->ext_pointer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001246 return;
1247 }
1248
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001249 read_pte(pe, extended_offset + get_start_sect(p));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001250
1251 if (!extended_offset)
1252 extended_offset = get_start_sect(p);
1253
1254 q = p = pt_offset(pe->sectorbuffer, 0);
1255 for (i = 0; i < 4; i++, p++) if (get_nr_sects(p)) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001256 if (IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001257 if (pe->ext_pointer)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001258 printf("Warning: extra link "
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001259 "pointer in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001260 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001261 else
1262 pe->ext_pointer = p;
1263 } else if (p->sys_ind) {
1264 if (pe->part_table)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001265 printf("Warning: ignoring extra "
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001266 "data in partition table"
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001267 " %u\n", g_partitions + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001268 else
1269 pe->part_table = p;
1270 }
1271 }
1272
1273 /* very strange code here... */
1274 if (!pe->part_table) {
1275 if (q != pe->ext_pointer)
1276 pe->part_table = q;
1277 else
1278 pe->part_table = q + 1;
1279 }
1280 if (!pe->ext_pointer) {
1281 if (q != pe->part_table)
1282 pe->ext_pointer = q;
1283 else
1284 pe->ext_pointer = q + 1;
1285 }
1286
1287 p = pe->ext_pointer;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001288 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001289 }
1290
Denis Vlasenko834410a2006-11-29 12:00:28 +00001291#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001292 /* remove empty links */
1293 remove:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001294 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001295 struct pte *pe = &ptes[i];
1296
Denis Vlasenkobd852072007-03-19 14:43:38 +00001297 if (!get_nr_sects(pe->part_table)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001298 && (g_partitions > 5 || ptes[4].part_table->sys_ind)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001299 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001300 printf("Omitting empty partition (%u)\n", i+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001301 delete_partition(i);
1302 goto remove; /* numbering changed */
1303 }
1304 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001305#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001306}
1307
Denis Vlasenko834410a2006-11-29 12:00:28 +00001308#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001309static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001310create_doslabel(void)
1311{
Denis Vlasenkobd852072007-03-19 14:43:38 +00001312 printf(msg_building_new_label, "DOS disklabel");
Rob Landley5527b912006-02-25 03:46:10 +00001313
Denis Vlasenko4437d192008-04-17 00:12:10 +00001314 current_label_type = LABEL_DOS;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001315#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001316 possibly_osf_label = 0;
1317#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001318 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001319
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001320 memset(&MBRbuffer[510 - 4*16], 0, 4*16);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001321 write_part_table_flag(MBRbuffer);
1322 extended_offset = 0;
1323 set_all_unchanged();
1324 set_changed(0);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001325 get_boot(CREATE_EMPTY_DOS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001326}
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001327#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001328
1329static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001330get_sectorsize(void)
1331{
Rob Landley736e5252006-02-25 03:36:00 +00001332 if (!user_set_sector_size) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001333 int arg;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001334 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001335 sector_size = arg;
1336 if (sector_size != DEFAULT_SECTOR_SIZE)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001337 printf("Note: sector size is %u "
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001338 "(not " DEFAULT_SECTOR_SIZE_STR ")\n",
1339 sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001340 }
1341}
1342
Rob Landley88621d72006-08-29 19:41:06 +00001343static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001344get_kernel_geometry(void)
1345{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001346 struct hd_geometry geometry;
1347
Denis Vlasenko4437d192008-04-17 00:12:10 +00001348 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001349 kern_heads = geometry.heads;
1350 kern_sectors = geometry.sectors;
1351 /* never use geometry.cylinders - it is truncated */
1352 }
1353}
1354
1355static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001356get_partition_table_geometry(void)
1357{
"Vladimir N. Oleynik"a972c872005-12-02 10:06:04 +00001358 const unsigned char *bufp = (const unsigned char *)MBRbuffer;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001359 struct partition *p;
1360 int i, h, s, hh, ss;
1361 int first = 1;
1362 int bad = 0;
1363
Eric Andersen3496fdc2006-01-30 23:09:20 +00001364 if (!(valid_part_table_flag((char*)bufp)))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001365 return;
1366
1367 hh = ss = 0;
Rob Landleyb73451d2006-02-24 16:29:00 +00001368 for (i = 0; i < 4; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001369 p = pt_offset(bufp, i);
1370 if (p->sys_ind != 0) {
1371 h = p->end_head + 1;
1372 s = (p->end_sector & 077);
1373 if (first) {
1374 hh = h;
1375 ss = s;
1376 first = 0;
1377 } else if (hh != h || ss != s)
1378 bad = 1;
1379 }
1380 }
1381
1382 if (!first && !bad) {
1383 pt_heads = hh;
1384 pt_sectors = ss;
1385 }
1386}
1387
Rob Landleyb73451d2006-02-24 16:29:00 +00001388static void
1389get_geometry(void)
1390{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001391 int sec_fac;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001392
1393 get_sectorsize();
1394 sec_fac = sector_size / 512;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001395#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001396 guess_device_type();
1397#endif
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001398 g_heads = g_cylinders = g_sectors = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001399 kern_heads = kern_sectors = 0;
1400 pt_heads = pt_sectors = 0;
1401
1402 get_kernel_geometry();
1403 get_partition_table_geometry();
1404
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001405 g_heads = user_heads ? user_heads :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001406 pt_heads ? pt_heads :
1407 kern_heads ? kern_heads : 255;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001408 g_sectors = user_sectors ? user_sectors :
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001409 pt_sectors ? pt_sectors :
1410 kern_sectors ? kern_sectors : 63;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001411 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
Eric Andersen040f4402003-07-30 08:40:37 +00001412
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001413 sector_offset = 1;
1414 if (dos_compatible_flag)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001415 sector_offset = g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001416
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001417 g_cylinders = total_number_of_sectors / (g_heads * g_sectors * sec_fac);
1418 if (!g_cylinders)
1419 g_cylinders = user_cylinders;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001420}
1421
1422/*
Denis Vlasenko4437d192008-04-17 00:12:10 +00001423 * Opens disk_device and optionally reads MBR.
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001424 * If what == OPEN_MAIN:
1425 * Open device, read MBR. Abort program on short read. Create empty
1426 * disklabel if the on-disk structure is invalid (WRITABLE mode).
1427 * If what == TRY_ONLY:
1428 * Open device, read MBR. Return an error if anything is out of place.
1429 * Do not create an empty disklabel. This is used for the "list"
1430 * operations: "fdisk -l /dev/sda" and "fdisk -l" (all devices).
1431 * If what == CREATE_EMPTY_*:
1432 * This means that get_boot() was called recursively from create_*label().
1433 * Do not re-open the device; just set up the ptes array and print
1434 * geometry warnings.
1435 *
Denis Vlasenko4437d192008-04-17 00:12:10 +00001436 * Returns:
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001437 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1438 * 0: found or created label
1439 * 1: I/O error
1440 */
Denis Vlasenko85c24712008-03-17 09:04:04 +00001441#if ENABLE_FEATURE_SUN_LABEL || ENABLE_FEATURE_FDISK_WRITABLE
1442static int get_boot(enum action what)
1443#else
1444static int get_boot(void)
1445#define get_boot(what) get_boot()
1446#endif
Rob Landleyb73451d2006-02-24 16:29:00 +00001447{
Denis Vlasenko4437d192008-04-17 00:12:10 +00001448 int i, fd;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001449
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001450 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001451 for (i = 0; i < 4; i++) {
1452 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001453 pe->part_table = pt_offset(MBRbuffer, i);
1454 pe->ext_pointer = NULL;
Denys Vlasenkod958e902010-04-06 02:32:26 +02001455 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001456 pe->sectorbuffer = MBRbuffer;
Denis Vlasenko834410a2006-11-29 12:00:28 +00001457#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001458 pe->changed = (what == CREATE_EMPTY_DOS);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001459#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001460 }
1461
Denis Vlasenko834410a2006-11-29 12:00:28 +00001462#if ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001463// ALERT! highly idiotic design!
1464// We end up here when we call get_boot() recursively
1465// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1466// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1467// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1468// So skip opening device _again_...
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001469 if (what == CREATE_EMPTY_DOS IF_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
Denis Vlasenko4437d192008-04-17 00:12:10 +00001470 goto created_table;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001471
Denis Vlasenko4437d192008-04-17 00:12:10 +00001472 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1473
Denis Vlasenkobd852072007-03-19 14:43:38 +00001474 if (fd < 0) {
1475 fd = open(disk_device, O_RDONLY);
1476 if (fd < 0) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001477 if (what == TRY_ONLY)
Rob Landleyb73451d2006-02-24 16:29:00 +00001478 return 1;
1479 fdisk_fatal(unable_to_open);
Denis Vlasenko4437d192008-04-17 00:12:10 +00001480 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001481 printf("'%s' is opened for read only\n", disk_device);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001482 }
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001483 xmove_fd(fd, dev_fd);
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001484 if (512 != full_read(dev_fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001485 if (what == TRY_ONLY) {
Denis Vlasenkoc033d512008-04-17 01:52:28 +00001486 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001487 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001488 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001489 fdisk_fatal(unable_to_read);
1490 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001491#else
Denis Vlasenkobd852072007-03-19 14:43:38 +00001492 fd = open(disk_device, O_RDONLY);
1493 if (fd < 0)
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001494 return 1;
Denis Vlasenko6eaf0a92008-06-29 05:10:47 +00001495 if (512 != full_read(fd, MBRbuffer, 512)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001496 close(fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001497 return 1;
Denis Vlasenko4437d192008-04-17 00:12:10 +00001498 }
1499 xmove_fd(fd, dev_fd);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001500#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001501
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001502 get_geometry();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001503 update_units();
1504
Denis Vlasenko834410a2006-11-29 12:00:28 +00001505#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001506 if (check_sun_label())
1507 return 0;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001508#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001509#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001510 if (check_sgi_label())
1511 return 0;
1512#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001513#if ENABLE_FEATURE_AIX_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001514 if (check_aix_label())
1515 return 0;
1516#endif
Kevin Cernekeeccb07042010-10-25 02:00:24 +02001517#if ENABLE_FEATURE_GPT_LABEL
1518 if (check_gpt_label())
1519 return 0;
1520#endif
Denis Vlasenko834410a2006-11-29 12:00:28 +00001521#if ENABLE_FEATURE_OSF_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001522 if (check_osf_label()) {
1523 possibly_osf_label = 1;
1524 if (!valid_part_table_flag(MBRbuffer)) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001525 current_label_type = LABEL_OSF;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001526 return 0;
1527 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001528 puts("This disk has both DOS and BSD magic.\n"
1529 "Give the 'b' command to go to BSD mode.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001530 }
1531#endif
1532
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001533#if !ENABLE_FEATURE_FDISK_WRITABLE
Denis Vlasenko4437d192008-04-17 00:12:10 +00001534 if (!valid_part_table_flag(MBRbuffer))
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001535 return -1;
1536#else
Denis Vlasenko4437d192008-04-17 00:12:10 +00001537 if (!valid_part_table_flag(MBRbuffer)) {
1538 if (what == OPEN_MAIN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001539 puts("Device contains neither a valid DOS "
1540 "partition table, nor Sun, SGI, OSF or GPT "
1541 "disklabel");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001542#ifdef __sparc__
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001543 IF_FEATURE_SUN_LABEL(create_sunlabel();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001544#else
1545 create_doslabel();
1546#endif
1547 return 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001548 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001549 /* TRY_ONLY: */
1550 return -1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001551 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00001552 created_table:
1553#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001554
Denis Vlasenko4437d192008-04-17 00:12:10 +00001555
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001556 IF_FEATURE_FDISK_WRITABLE(warn_cylinders();)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001557 warn_geometry();
1558
1559 for (i = 0; i < 4; i++) {
Denis Vlasenko4437d192008-04-17 00:12:10 +00001560 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001561 if (g_partitions != 4)
Denis Vlasenkobd852072007-03-19 14:43:38 +00001562 printf("Ignoring extra extended "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001563 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001564 else
1565 read_extended(i);
1566 }
1567 }
1568
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001569 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001570 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001571 if (!valid_part_table_flag(pe->sectorbuffer)) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001572 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001573 "table %u will be corrected by w(rite)\n",
Denis Vlasenko834410a2006-11-29 12:00:28 +00001574 pe->sectorbuffer[510],
1575 pe->sectorbuffer[511],
1576 i + 1);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001577 IF_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001578 }
1579 }
1580
1581 return 0;
1582}
1583
Denis Vlasenko834410a2006-11-29 12:00:28 +00001584#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001585/*
1586 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1587 * If the user hits Enter, DFLT is returned.
1588 * Answers like +10 are interpreted as offsets from BASE.
1589 *
1590 * There is no default if DFLT is not between LOW and HIGH.
1591 */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001592static sector_t
1593read_int(sector_t low, sector_t dflt, sector_t high, sector_t base, const char *mesg)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001594{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001595 sector_t value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001596 int default_ok = 1;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001597 const char *fmt = "%s (%u-%u, default %u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001598
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001599 if (dflt < low || dflt > high) {
1600 fmt = "%s (%u-%u): ";
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001601 default_ok = 0;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001602 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001603
1604 while (1) {
1605 int use_default = default_ok;
1606
1607 /* ask question and read answer */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001608 do {
1609 printf(fmt, mesg, low, high, dflt);
1610 read_maybe_empty("");
1611 } while (*line_ptr != '\n' && !isdigit(*line_ptr)
1612 && *line_ptr != '-' && *line_ptr != '+');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001613
Eric Andersen84bdea82004-05-19 10:49:17 +00001614 if (*line_ptr == '+' || *line_ptr == '-') {
Rob Landleyb73451d2006-02-24 16:29:00 +00001615 int minus = (*line_ptr == '-');
1616 int absolute = 0;
Eric Andersenc48d49a2003-07-03 10:02:32 +00001617
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001618 value = atoi(line_ptr + 1);
Eric Andersenc48d49a2003-07-03 10:02:32 +00001619
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001620 /* (1) if 2nd char is digit, use_default = 0.
1621 * (2) move line_ptr to first non-digit. */
Rob Landleyb73451d2006-02-24 16:29:00 +00001622 while (isdigit(*++line_ptr))
1623 use_default = 0;
Eric Andersen84bdea82004-05-19 10:49:17 +00001624
Rob Landleyb73451d2006-02-24 16:29:00 +00001625 switch (*line_ptr) {
1626 case 'c':
1627 case 'C':
1628 if (!display_in_cyl_units)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001629 value *= g_heads * g_sectors;
Rob Landleyb73451d2006-02-24 16:29:00 +00001630 break;
1631 case 'K':
1632 absolute = 1024;
1633 break;
1634 case 'k':
1635 absolute = 1000;
1636 break;
1637 case 'm':
1638 case 'M':
1639 absolute = 1000000;
1640 break;
1641 case 'g':
1642 case 'G':
1643 absolute = 1000000000;
1644 break;
1645 default:
1646 break;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001647 }
Rob Landleyb73451d2006-02-24 16:29:00 +00001648 if (absolute) {
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00001649 ullong bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001650 unsigned long unit;
1651
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001652 bytes = (ullong) value * absolute;
Rob Landleyb73451d2006-02-24 16:29:00 +00001653 unit = sector_size * units_per_sector;
1654 bytes += unit/2; /* round */
1655 bytes /= unit;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001656 value = bytes;
Rob Landleyb73451d2006-02-24 16:29:00 +00001657 }
1658 if (minus)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001659 value = -value;
1660 value += base;
Eric Andersen84bdea82004-05-19 10:49:17 +00001661 } else {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001662 value = atoi(line_ptr);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001663 while (isdigit(*line_ptr)) {
1664 line_ptr++;
1665 use_default = 0;
1666 }
1667 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001668 if (use_default) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001669 value = dflt;
1670 printf("Using default value %u\n", value);
Denis Vlasenkobd852072007-03-19 14:43:38 +00001671 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001672 if (value >= low && value <= high)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001673 break;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001674 puts("Value is out of range");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001675 }
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001676 return value;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001677}
1678
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001679static unsigned
1680get_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001681{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001682 struct pte *pe;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001683 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001684
Denis Vlasenkobd852072007-03-19 14:43:38 +00001685 i = read_int(1, 0, max, 0, "Partition number") - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001686 pe = &ptes[i];
1687
1688 if (warn) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001689 if ((!LABEL_IS_SUN && !LABEL_IS_SGI && !pe->part_table->sys_ind)
1690 || (LABEL_IS_SUN && (!sunlabel->partitions[i].num_sectors || !sunlabel->infos[i].id))
1691 || (LABEL_IS_SGI && !sgi_get_num_sectors(i))
1692 ) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001693 printf("Warning: partition %u has empty type\n", i+1);
Rob Landley5527b912006-02-25 03:46:10 +00001694 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001695 }
1696 return i;
1697}
1698
1699static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001700get_existing_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001701{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001702 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001703 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001704
1705 for (i = 0; i < max; i++) {
1706 struct pte *pe = &ptes[i];
1707 struct partition *p = pe->part_table;
1708
1709 if (p && !is_cleared_partition(p)) {
1710 if (pno >= 0)
1711 goto not_unique;
1712 pno = i;
1713 }
1714 }
1715 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001716 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001717 return pno;
1718 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001719 puts("No partition is defined yet!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001720 return -1;
1721
1722 not_unique:
1723 return get_partition(warn, max);
1724}
1725
1726static int
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001727get_nonexisting_partition(int warn, unsigned max)
Rob Landleyb73451d2006-02-24 16:29:00 +00001728{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001729 int pno = -1;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001730 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001731
1732 for (i = 0; i < max; i++) {
1733 struct pte *pe = &ptes[i];
1734 struct partition *p = pe->part_table;
1735
1736 if (p && is_cleared_partition(p)) {
1737 if (pno >= 0)
1738 goto not_unique;
1739 pno = i;
1740 }
1741 }
1742 if (pno >= 0) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001743 printf("Selected partition %u\n", pno+1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001744 return pno;
1745 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001746 puts("All primary partitions have been defined already!");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001747 return -1;
1748
1749 not_unique:
1750 return get_partition(warn, max);
1751}
1752
1753
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001754static void
1755change_units(void)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001756{
1757 display_in_cyl_units = !display_in_cyl_units;
1758 update_units();
Denis Vlasenkobd852072007-03-19 14:43:38 +00001759 printf("Changing display/entry units to %s\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001760 str_units(PLURAL));
1761}
1762
1763static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001764toggle_active(int i)
1765{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001766 struct pte *pe = &ptes[i];
1767 struct partition *p = pe->part_table;
1768
Rob Landleyb73451d2006-02-24 16:29:00 +00001769 if (IS_EXTENDED(p->sys_ind) && !p->boot_ind)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001770 printf("WARNING: Partition %u is an extended partition\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001771 p->boot_ind = (p->boot_ind ? 0 : ACTIVE_FLAG);
1772 pe->changed = 1;
1773}
1774
1775static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001776toggle_dos_compatibility_flag(void)
1777{
Denis Vlasenkocdf62772008-03-17 08:42:43 +00001778 dos_compatible_flag = 1 - dos_compatible_flag;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001779 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001780 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001781 printf("DOS Compatibility flag is %sset\n", "");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001782 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001783 sector_offset = 1;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001784 printf("DOS Compatibility flag is %sset\n", "not ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001785 }
1786}
1787
1788static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001789delete_partition(int i)
1790{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001791 struct pte *pe = &ptes[i];
1792 struct partition *p = pe->part_table;
1793 struct partition *q = pe->ext_pointer;
1794
1795/* Note that for the fifth partition (i == 4) we don't actually
1796 * decrement partitions.
1797 */
1798
1799 if (warn_geometry())
1800 return; /* C/H/S not set */
1801 pe->changed = 1;
1802
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001803 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001804 sun_delete_partition(i);
1805 return;
1806 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001807 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001808 sgi_delete_partition(i);
1809 return;
1810 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001811
1812 if (i < 4) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001813 if (IS_EXTENDED(p->sys_ind) && i == ext_index) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001814 g_partitions = 4;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001815 ptes[ext_index].ext_pointer = NULL;
1816 extended_offset = 0;
1817 }
1818 clear_partition(p);
1819 return;
1820 }
1821
1822 if (!q->sys_ind && i > 4) {
1823 /* the last one in the chain - just delete */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001824 --g_partitions;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001825 --i;
1826 clear_partition(ptes[i].ext_pointer);
1827 ptes[i].changed = 1;
1828 } else {
1829 /* not the last one - further ones will be moved down */
1830 if (i > 4) {
1831 /* delete this link in the chain */
1832 p = ptes[i-1].ext_pointer;
1833 *p = *q;
1834 set_start_sect(p, get_start_sect(q));
1835 set_nr_sects(p, get_nr_sects(q));
1836 ptes[i-1].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001837 } else if (g_partitions > 5) { /* 5 will be moved to 4 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001838 /* the first logical in a longer chain */
1839 pe = &ptes[5];
1840
1841 if (pe->part_table) /* prevent SEGFAULT */
1842 set_start_sect(pe->part_table,
Denys Vlasenkod958e902010-04-06 02:32:26 +02001843 get_partition_start_from_dev_start(pe) -
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001844 extended_offset);
Denys Vlasenkod958e902010-04-06 02:32:26 +02001845 pe->offset_from_dev_start = extended_offset;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001846 pe->changed = 1;
1847 }
1848
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001849 if (g_partitions > 5) {
1850 g_partitions--;
1851 while (i < g_partitions) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001852 ptes[i] = ptes[i+1];
1853 i++;
1854 }
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001855 } else {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001856 /* the only logical: clear only */
1857 clear_partition(ptes[i].part_table);
Denys Vlasenko5ea1de22010-04-06 02:31:43 +02001858 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001859 }
1860}
1861
1862static void
Rob Landleyb73451d2006-02-24 16:29:00 +00001863change_sysid(void)
1864{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001865 int i, sys, origsys;
1866 struct partition *p;
1867
Eric Andersen040f4402003-07-30 08:40:37 +00001868 /* If sgi_label then don't use get_existing_partition,
1869 let the user select a partition, since get_existing_partition()
1870 only works for Linux like partition tables. */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001871 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001872 i = get_existing_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001873 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001874 i = get_partition(0, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00001875 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001876 if (i == -1)
1877 return;
1878 p = ptes[i].part_table;
1879 origsys = sys = get_sysid(i);
1880
1881 /* if changing types T to 0 is allowed, then
1882 the reverse change must be allowed, too */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001883 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN && !get_nr_sects(p)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001884 printf("Partition %u does not exist yet!\n", i + 1);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001885 return;
1886 }
1887 while (1) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001888 sys = read_hex(get_sys_types());
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001889
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001890 if (!sys && !LABEL_IS_SGI && !LABEL_IS_SUN) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001891 puts("Type 0 means free space to many systems\n"
1892 "(but not to Linux). Having partitions of\n"
1893 "type 0 is probably unwise.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001894 /* break; */
1895 }
1896
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001897 if (!LABEL_IS_SUN && !LABEL_IS_SGI) {
Rob Landleyb73451d2006-02-24 16:29:00 +00001898 if (IS_EXTENDED(sys) != IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001899 puts("You cannot change a partition into"
1900 " an extended one or vice versa");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001901 break;
1902 }
1903 }
1904
1905 if (sys < 256) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00001906#if ENABLE_FEATURE_SUN_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001907 if (LABEL_IS_SUN && i == 2 && sys != SUN_WHOLE_DISK)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001908 puts("Consider leaving partition 3 "
1909 "as Whole disk (5),\n"
1910 "as SunOS/Solaris expects it and "
1911 "even Linux likes it\n");
Denis Vlasenkobd852072007-03-19 14:43:38 +00001912#endif
1913#if ENABLE_FEATURE_SGI_LABEL
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001914 if (LABEL_IS_SGI &&
Rob Landley5527b912006-02-25 03:46:10 +00001915 (
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001916 (i == 10 && sys != SGI_ENTIRE_DISK) ||
Rob Landley5527b912006-02-25 03:46:10 +00001917 (i == 8 && sys != 0)
1918 )
Denis Vlasenkobd852072007-03-19 14:43:38 +00001919 ) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001920 puts("Consider leaving partition 9 "
1921 "as volume header (0),\nand "
1922 "partition 11 as entire volume (6)"
1923 "as IRIX expects it\n");
Rob Landley5527b912006-02-25 03:46:10 +00001924 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00001925#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001926 if (sys == origsys)
1927 break;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001928 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001929 sun_change_sysid(i, sys);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00001930 } else if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001931 sgi_change_sysid(i, sys);
1932 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001933 p->sys_ind = sys;
Rob Landley5527b912006-02-25 03:46:10 +00001934
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001935 printf("Changed system type of partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00001936 "to %x (%s)\n", i + 1, sys,
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001937 partition_type(sys));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001938 ptes[i].changed = 1;
Denis Vlasenkoa5549c92008-01-24 22:49:15 +00001939 //if (is_dos_partition(origsys) || is_dos_partition(sys))
1940 // dos_changed = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001941 break;
1942 }
1943 }
1944}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001945#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00001946
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001947
Denis Vlasenko28703012006-12-19 20:32:02 +00001948/* check_consistency() and linear2chs() added Sat Mar 6 12:28:16 1993,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001949 * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross,
1950 * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S.
1951 * Lubkin Oct. 1991). */
1952
Rob Landleyb73451d2006-02-24 16:29:00 +00001953static void
Denis Vlasenko28703012006-12-19 20:32:02 +00001954linear2chs(unsigned ls, unsigned *c, unsigned *h, unsigned *s)
Rob Landleyb73451d2006-02-24 16:29:00 +00001955{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001956 int spc = g_heads * g_sectors;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001957
1958 *c = ls / spc;
1959 ls = ls % spc;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001960 *h = ls / g_sectors;
1961 *s = ls % g_sectors + 1; /* sectors count from 1 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001962}
1963
Rob Landleyb73451d2006-02-24 16:29:00 +00001964static void
1965check_consistency(const struct partition *p, int partition)
1966{
Denis Vlasenko834410a2006-11-29 12:00:28 +00001967 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1968 unsigned pec, peh, pes; /* physical ending c, h, s */
1969 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1970 unsigned lec, leh, les; /* logical ending c, h, s */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001971
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001972 if (!g_heads || !g_sectors || (partition >= 4))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001973 return; /* do not check extended partitions */
1974
1975/* physical beginning c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001976 pbc = cylinder(p->sector, p->cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001977 pbh = p->head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001978 pbs = sector(p->sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001979
1980/* physical ending c, h, s */
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001981 pec = cylinder(p->end_sector, p->end_cyl);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001982 peh = p->end_head;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001983 pes = sector(p->end_sector);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001984
1985/* compute logical beginning (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001986 linear2chs(get_start_sect(p), &lbc, &lbh, &lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001987
1988/* compute logical ending (c, h, s) */
Denis Vlasenko28703012006-12-19 20:32:02 +00001989 linear2chs(get_start_sect(p) + get_nr_sects(p) - 1, &lec, &leh, &les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001990
1991/* Same physical / logical beginning? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00001992 if (g_cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02001993 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02001994 "start (non-Linux?):\n", partition + 1);
1995 printf(" phys=(%u,%u,%u) ", pbc, pbh, pbs);
1996 printf("logical=(%u,%u,%u)\n", lbc, lbh, lbs);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00001997 }
1998
1999/* Same physical / logical ending? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002000 if (g_cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002001 printf("Partition %u has different physical/logical "
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002002 "end:\n", partition + 1);
2003 printf(" phys=(%u,%u,%u) ", pec, peh, pes);
2004 printf("logical=(%u,%u,%u)\n", lec, leh, les);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002005 }
2006
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002007/* Ending on cylinder boundary? */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002008 if (peh != (g_heads - 1) || pes != g_sectors) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002009 printf("Partition %u does not end on cylinder boundary\n",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002010 partition + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002011 }
2012}
2013
2014static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002015list_disk_geometry(void)
2016{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002017 ullong bytes = ((ullong)total_number_of_sectors << 9);
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002018 ullong xbytes = bytes / (1024*1024);
2019 char x = 'M';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002020
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002021 if (xbytes >= 10000) {
2022 xbytes += 512; /* fdisk util-linux 2.28 does this */
2023 xbytes /= 1024;
2024 x = 'G';
2025 }
2026 printf("Disk %s: %llu %cB, %llu bytes, %"SECT_FMT"u sectors\n"
2027 "%u cylinders, %u heads, %u sectors/track\n"
2028 "Units: %s of %u * %u = %u bytes\n\n",
2029 disk_device, xbytes, x,
2030 bytes, total_number_of_sectors,
2031 g_cylinders, g_heads, g_sectors,
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002032 str_units(PLURAL),
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002033 units_per_sector, sector_size, units_per_sector * sector_size
2034 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002035}
2036
2037/*
2038 * Check whether partition entries are ordered by their starting positions.
2039 * Return 0 if OK. Return i if partition i should have been earlier.
2040 * Two separate checks: primary and logical partitions.
2041 */
2042static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002043wrong_p_order(int *prev)
2044{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002045 const struct pte *pe;
2046 const struct partition *p;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002047 sector_t last_p_start_pos = 0, p_start_pos;
2048 unsigned i, last_i = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002049
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002050 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002051 if (i == 4) {
2052 last_i = 4;
2053 last_p_start_pos = 0;
2054 }
2055 pe = &ptes[i];
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00002056 p = pe->part_table;
2057 if (p->sys_ind) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002058 p_start_pos = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002059
2060 if (last_p_start_pos > p_start_pos) {
2061 if (prev)
2062 *prev = last_i;
2063 return i;
2064 }
2065
2066 last_p_start_pos = p_start_pos;
2067 last_i = i;
2068 }
2069 }
2070 return 0;
2071}
2072
Denis Vlasenko834410a2006-11-29 12:00:28 +00002073#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002074/*
2075 * Fix the chain of logicals.
2076 * extended_offset is unchanged, the set of sectors used is unchanged
2077 * The chain is sorted so that sectors increase, and so that
2078 * starting sectors increase.
2079 *
2080 * After this it may still be that cfdisk doesnt like the table.
2081 * (This is because cfdisk considers expanded parts, from link to
2082 * end of partition, and these may still overlap.)
2083 * Now
2084 * sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
2085 * may help.
2086 */
2087static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002088fix_chain_of_logicals(void)
2089{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002090 int j, oj, ojj, sj, sjj;
2091 struct partition *pj,*pjj,tmp;
2092
2093 /* Stage 1: sort sectors but leave sector of part 4 */
2094 /* (Its sector is the global extended_offset.) */
2095 stage1:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002096 for (j = 5; j < g_partitions - 1; j++) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002097 oj = ptes[j].offset_from_dev_start;
2098 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002099 if (oj > ojj) {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002100 ptes[j].offset_from_dev_start = ojj;
2101 ptes[j+1].offset_from_dev_start = oj;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002102 pj = ptes[j].part_table;
2103 set_start_sect(pj, get_start_sect(pj)+oj-ojj);
2104 pjj = ptes[j+1].part_table;
2105 set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
2106 set_start_sect(ptes[j-1].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002107 ojj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002108 set_start_sect(ptes[j].ext_pointer,
Rob Landleyb73451d2006-02-24 16:29:00 +00002109 oj-extended_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002110 goto stage1;
2111 }
2112 }
2113
2114 /* Stage 2: sort starting sectors */
2115 stage2:
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002116 for (j = 4; j < g_partitions - 1; j++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002117 pj = ptes[j].part_table;
2118 pjj = ptes[j+1].part_table;
2119 sj = get_start_sect(pj);
2120 sjj = get_start_sect(pjj);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002121 oj = ptes[j].offset_from_dev_start;
2122 ojj = ptes[j+1].offset_from_dev_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002123 if (oj+sj > ojj+sjj) {
2124 tmp = *pj;
2125 *pj = *pjj;
2126 *pjj = tmp;
2127 set_start_sect(pj, ojj+sjj-oj);
2128 set_start_sect(pjj, oj+sj-ojj);
2129 goto stage2;
2130 }
2131 }
2132
2133 /* Probably something was changed */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002134 for (j = 4; j < g_partitions; j++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002135 ptes[j].changed = 1;
2136}
2137
2138
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002139static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002140fix_partition_table_order(void)
2141{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002142 struct pte *pei, *pek;
2143 int i,k;
2144
2145 if (!wrong_p_order(NULL)) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002146 puts("Ordering is already correct\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002147 return;
2148 }
2149
2150 while ((i = wrong_p_order(&k)) != 0 && i < 4) {
2151 /* partition i should have come earlier, move it */
2152 /* We have to move data in the MBR */
2153 struct partition *pi, *pk, *pe, pbuf;
2154 pei = &ptes[i];
2155 pek = &ptes[k];
2156
2157 pe = pei->ext_pointer;
2158 pei->ext_pointer = pek->ext_pointer;
2159 pek->ext_pointer = pe;
2160
2161 pi = pei->part_table;
2162 pk = pek->part_table;
2163
2164 memmove(&pbuf, pi, sizeof(struct partition));
2165 memmove(pi, pk, sizeof(struct partition));
2166 memmove(pk, &pbuf, sizeof(struct partition));
2167
2168 pei->changed = pek->changed = 1;
2169 }
2170
2171 if (i)
2172 fix_chain_of_logicals();
2173
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002174 puts("Done");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002175}
2176#endif
2177
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002178static const char *
2179chs_string11(unsigned cyl, unsigned head, unsigned sect)
2180{
2181 char *buf = auto_string(xzalloc(sizeof(int)*3 * 3));
2182 sprintf(buf, "%u,%u,%u", cylinder(sect,cyl), head, sector(sect));
2183 return buf;
2184}
2185
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002186static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002187list_table(int xtra)
2188{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002189 int i, w;
2190
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002191 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002192 sun_list_table(xtra);
2193 return;
2194 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002195 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002196 sgi_list_table(xtra);
2197 return;
2198 }
Kevin Cernekeeccb07042010-10-25 02:00:24 +02002199 if (LABEL_IS_GPT) {
2200 gpt_list_table(xtra);
2201 return;
2202 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002203
2204 list_disk_geometry();
2205
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002206 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002207 xbsd_print_disklabel(xtra);
2208 return;
2209 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002210
2211 /* Heuristic: we list partition 3 of /dev/foo as /dev/foo3,
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002212 * but if the device name ends in a digit, say /dev/foo1,
2213 * then the partition is called /dev/foo1p3.
2214 */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002215 w = strlen(disk_device);
2216 if (w && isdigit(disk_device[w-1]))
2217 w++;
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002218 if (w < 7)
2219 w = 7;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002220
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002221 printf("%-*s Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type\n",
2222 w-1, "Device");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002223
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002224 for (i = 0; i < g_partitions; i++) {
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002225 const struct partition *p;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002226 const struct pte *pe = &ptes[i];
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002227 char boot4[4];
2228 char numstr6[6];
2229 sector_t start_sect;
2230 sector_t end_sect;
2231 sector_t nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002232
2233 p = pe->part_table;
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002234 if (!p || is_cleared_partition(p))
2235 continue;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002236
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002237 sprintf(boot4, "%02x", p->boot_ind);
2238 if ((p->boot_ind & 0x7f) == 0) {
2239 /* 0x80 shown as '*', 0x00 is ' ' */
2240 boot4[0] = p->boot_ind ? '*' : ' ';
2241 boot4[1] = ' ';
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002242 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002243
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002244 start_sect = get_partition_start_from_dev_start(pe);
2245 end_sect = start_sect;
2246 nr_sects = get_nr_sects(p);
2247 if (nr_sects != 0)
2248 end_sect += nr_sects - 1;
2249
2250 smart_ulltoa5((ullong)nr_sects * sector_size,
2251 numstr6, " KMGTPEZY")[0] = '\0';
2252
2253#define SFMT SECT_FMT
2254 // Boot StartCHS EndCHS StartLBA EndLBA Sectors Size Id Type
2255 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 +00002256 partname(disk_device, i+1, w+2),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002257 boot4,
2258 chs_string11(p->cyl, p->head, p->sector),
2259 chs_string11(p->end_cyl, p->end_head, p->end_sector),
2260 start_sect,
2261 end_sect,
2262 nr_sects,
2263 numstr6,
2264 p->sys_ind,
2265 partition_type(p->sys_ind)
2266 );
2267#undef SFMT
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002268 check_consistency(p, i);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002269 }
2270
2271 /* Is partition table in disk order? It need not be, but... */
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002272 /* partition table entries are not checked for correct order
2273 * if this is a sgi, sun or aix labeled disk... */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002274 if (LABEL_IS_DOS && wrong_p_order(NULL)) {
Rob Landley5527b912006-02-25 03:46:10 +00002275 /* FIXME */
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002276 puts("\nPartition table entries are not in disk order");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002277 }
2278}
2279
Denis Vlasenko834410a2006-11-29 12:00:28 +00002280#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002281static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002282x_list_table(int extend)
2283{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002284 const struct pte *pe;
2285 const struct partition *p;
2286 int i;
2287
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002288 printf("\nDisk %s: %u heads, %u sectors, %u cylinders\n\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002289 disk_device, g_heads, g_sectors, g_cylinders);
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002290 puts("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID");
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002291 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002292 pe = &ptes[i];
2293 p = (extend ? pe->ext_pointer : pe->part_table);
2294 if (p != NULL) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002295 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 +02002296 i + 1, p->boot_ind,
2297 p->head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002298 sector(p->sector),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002299 cylinder(p->sector, p->cyl),
2300 p->end_head,
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002301 sector(p->end_sector),
2302 cylinder(p->end_sector, p->end_cyl),
Denys Vlasenko607f2b42016-08-23 16:13:33 +02002303 get_start_sect(p),
2304 get_nr_sects(p),
2305 p->sys_ind
2306 );
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002307 if (p->sys_ind)
2308 check_consistency(p, i);
2309 }
2310 }
2311}
2312#endif
2313
Denis Vlasenko834410a2006-11-29 12:00:28 +00002314#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002315static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002316fill_bounds(sector_t *first, sector_t *last)
Rob Landleyb73451d2006-02-24 16:29:00 +00002317{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002318 unsigned i;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002319 const struct pte *pe = &ptes[0];
2320 const struct partition *p;
2321
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002322 for (i = 0; i < g_partitions; pe++,i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002323 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002324 if (!p->sys_ind || IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002325 first[i] = 0xffffffff;
2326 last[i] = 0;
2327 } else {
Denys Vlasenkod958e902010-04-06 02:32:26 +02002328 first[i] = get_partition_start_from_dev_start(pe);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002329 last[i] = first[i] + get_nr_sects(p) - 1;
2330 }
2331 }
2332}
2333
2334static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002335check(int n, unsigned h, unsigned s, unsigned c, sector_t start)
Rob Landleyb73451d2006-02-24 16:29:00 +00002336{
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002337 sector_t total, real_s, real_c;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002338
2339 real_s = sector(s) - 1;
2340 real_c = cylinder(s, c);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002341 total = (real_c * g_sectors + real_s) * g_heads + h;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002342 if (!total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002343 printf("Partition %u contains sector 0\n", n);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002344 if (h >= g_heads)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002345 printf("Partition %u: head %u greater than maximum %u\n",
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002346 n, h + 1, g_heads);
2347 if (real_s >= g_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002348 printf("Partition %u: sector %u greater than "
2349 "maximum %u\n", n, s, g_sectors);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002350 if (real_c >= g_cylinders)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002351 printf("Partition %u: cylinder %"SECT_FMT"u greater than "
2352 "maximum %u\n", n, real_c + 1, g_cylinders);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002353 if (g_cylinders <= 1024 && start != total)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002354 printf("Partition %u: previous sectors %"SECT_FMT"u disagrees with "
2355 "total %"SECT_FMT"u\n", n, start, total);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002356}
2357
2358static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002359verify(void)
2360{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002361 int i, j;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002362 sector_t total = 1;
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002363 sector_t chs_size;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002364 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002365 struct partition *p;
2366
2367 if (warn_geometry())
2368 return;
2369
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002370 if (LABEL_IS_SUN) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002371 verify_sun();
2372 return;
2373 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002374 if (LABEL_IS_SGI) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002375 verify_sgi(1);
2376 return;
2377 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002378
2379 fill_bounds(first, last);
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002380 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002381 struct pte *pe = &ptes[i];
2382
2383 p = pe->part_table;
Rob Landleyb73451d2006-02-24 16:29:00 +00002384 if (p->sys_ind && !IS_EXTENDED(p->sys_ind)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002385 check_consistency(p, i);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002386 if (get_partition_start_from_dev_start(pe) < first[i])
Denis Vlasenkobd852072007-03-19 14:43:38 +00002387 printf("Warning: bad start-of-data in "
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002388 "partition %u\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002389 check(i + 1, p->end_head, p->end_sector, p->end_cyl,
2390 last[i]);
2391 total += last[i] + 1 - first[i];
Denis Vlasenkobd852072007-03-19 14:43:38 +00002392 for (j = 0; j < i; j++) {
2393 if ((first[i] >= first[j] && first[i] <= last[j])
2394 || ((last[i] <= last[j] && last[i] >= first[j]))) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002395 printf("Warning: partition %u overlaps "
2396 "partition %u\n", j + 1, i + 1);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002397 total += first[i] >= first[j] ?
2398 first[i] : first[j];
2399 total -= last[i] <= last[j] ?
2400 last[i] : last[j];
2401 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002402 }
2403 }
2404 }
2405
2406 if (extended_offset) {
2407 struct pte *pex = &ptes[ext_index];
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002408 sector_t e_last = get_start_sect(pex->part_table) +
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002409 get_nr_sects(pex->part_table) - 1;
2410
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002411 for (i = 4; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002412 total++;
2413 p = ptes[i].part_table;
2414 if (!p->sys_ind) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002415 if (i != 4 || i + 1 < g_partitions)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002416 printf("Warning: partition %u "
Denis Vlasenkobd852072007-03-19 14:43:38 +00002417 "is empty\n", i + 1);
2418 } else if (first[i] < extended_offset || last[i] > e_last) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002419 printf("Logical partition %u not entirely in "
2420 "partition %u\n", i + 1, ext_index + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002421 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002422 }
2423 }
2424
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002425 chs_size = (sector_t)g_heads * g_sectors * g_cylinders;
2426 if (total > chs_size)
2427 printf("Total allocated sectors %u"
2428 " greater than CHS size %"SECT_FMT"u\n",
2429 total, chs_size
2430 );
Denis Vlasenkobd852072007-03-19 14:43:38 +00002431 else {
Denys Vlasenko29483ff2016-08-23 17:18:45 +02002432 total = chs_size - total;
Denis Vlasenkobd852072007-03-19 14:43:38 +00002433 if (total != 0)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002434 printf("%"SECT_FMT"u unallocated sectors\n", total);
Denis Vlasenkobd852072007-03-19 14:43:38 +00002435 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002436}
2437
2438static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002439add_partition(int n, int sys)
2440{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002441 char mesg[256]; /* 48 does not suffice in Japanese */
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002442 int i, num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002443 struct partition *p = ptes[n].part_table;
2444 struct partition *q = ptes[ext_index].part_table;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002445 sector_t limit, temp;
2446 sector_t start, stop = 0;
2447 sector_t first[g_partitions], last[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002448
2449 if (p && p->sys_ind) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00002450 printf(msg_part_already_defined, n + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002451 return;
2452 }
2453 fill_bounds(first, last);
2454 if (n < 4) {
2455 start = sector_offset;
Eric Andersen040f4402003-07-30 08:40:37 +00002456 if (display_in_cyl_units || !total_number_of_sectors)
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002457 limit = (sector_t) g_heads * g_sectors * g_cylinders - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002458 else
Denis Vlasenko3f22b7f2007-06-02 12:46:55 +00002459 limit = total_number_of_sectors - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002460 if (extended_offset) {
2461 first[ext_index] = extended_offset;
2462 last[ext_index] = get_start_sect(q) +
2463 get_nr_sects(q) - 1;
2464 }
2465 } else {
2466 start = extended_offset + sector_offset;
2467 limit = get_start_sect(q) + get_nr_sects(q) - 1;
2468 }
2469 if (display_in_cyl_units)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002470 for (i = 0; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002471 first[i] = (cround(first[i]) - 1) * units_per_sector;
2472
Denis Vlasenkobd852072007-03-19 14:43:38 +00002473 snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002474 do {
2475 temp = start;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002476 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002477 int lastplusoff;
2478
Denys Vlasenkod958e902010-04-06 02:32:26 +02002479 if (start == ptes[i].offset_from_dev_start)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002480 start += sector_offset;
Rob Landleyb73451d2006-02-24 16:29:00 +00002481 lastplusoff = last[i] + ((n < 4) ? 0 : sector_offset);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002482 if (start >= first[i] && start <= lastplusoff)
2483 start = lastplusoff + 1;
2484 }
2485 if (start > limit)
2486 break;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002487 if (start >= temp+units_per_sector && num_read) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002488 printf("Sector %"SECT_FMT"u is already allocated\n", temp);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002489 temp = start;
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002490 num_read = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002491 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002492 if (!num_read && start == temp) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002493 sector_t saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002494
2495 saved_start = start;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002496 start = read_int(cround(saved_start), cround(saved_start), cround(limit), 0, mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002497 if (display_in_cyl_units) {
2498 start = (start - 1) * units_per_sector;
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002499 if (start < saved_start)
2500 start = saved_start;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002501 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002502 num_read = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002503 }
Mike Frysingerfa6c4842006-05-26 01:48:17 +00002504 } while (start != temp || !num_read);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002505 if (n > 4) { /* NOT for fifth partition */
2506 struct pte *pe = &ptes[n];
2507
Denys Vlasenkod958e902010-04-06 02:32:26 +02002508 pe->offset_from_dev_start = start - sector_offset;
2509 if (pe->offset_from_dev_start == extended_offset) { /* must be corrected */
2510 pe->offset_from_dev_start++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002511 if (sector_offset == 1)
2512 start++;
2513 }
2514 }
2515
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002516 for (i = 0; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002517 struct pte *pe = &ptes[i];
2518
Denys Vlasenkod958e902010-04-06 02:32:26 +02002519 if (start < pe->offset_from_dev_start && limit >= pe->offset_from_dev_start)
2520 limit = pe->offset_from_dev_start - 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002521 if (start < first[i] && limit >= first[i])
2522 limit = first[i] - 1;
2523 }
2524 if (start > limit) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002525 puts("No free sectors available");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002526 if (n > 4)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002527 g_partitions--;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002528 return;
2529 }
2530 if (cround(start) == cround(limit)) {
2531 stop = limit;
2532 } else {
2533 snprintf(mesg, sizeof(mesg),
Denis Vlasenkobd852072007-03-19 14:43:38 +00002534 "Last %s or +size or +sizeM or +sizeK",
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002535 str_units(SINGULAR));
Denys Vlasenkod958e902010-04-06 02:32:26 +02002536 stop = read_int(cround(start), cround(limit), cround(limit), cround(start), mesg);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002537 if (display_in_cyl_units) {
2538 stop = stop * units_per_sector - 1;
2539 if (stop >limit)
2540 stop = limit;
2541 }
2542 }
2543
2544 set_partition(n, 0, start, stop, sys);
2545 if (n > 4)
Denys Vlasenkod958e902010-04-06 02:32:26 +02002546 set_partition(n - 1, 1, ptes[n].offset_from_dev_start, stop, EXTENDED);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002547
Rob Landleyb73451d2006-02-24 16:29:00 +00002548 if (IS_EXTENDED(sys)) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002549 struct pte *pe4 = &ptes[4];
2550 struct pte *pen = &ptes[n];
2551
2552 ext_index = n;
2553 pen->ext_pointer = p;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002554 pe4->offset_from_dev_start = extended_offset = start;
Rob Landley081e3842006-08-03 20:07:35 +00002555 pe4->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002556 pe4->part_table = pt_offset(pe4->sectorbuffer, 0);
2557 pe4->ext_pointer = pe4->part_table + 1;
2558 pe4->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002559 g_partitions = 5;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002560 }
2561}
2562
2563static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002564add_logical(void)
2565{
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002566 if (g_partitions > 5 || ptes[4].part_table->sys_ind) {
2567 struct pte *pe = &ptes[g_partitions];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002568
Rob Landley081e3842006-08-03 20:07:35 +00002569 pe->sectorbuffer = xzalloc(sector_size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002570 pe->part_table = pt_offset(pe->sectorbuffer, 0);
2571 pe->ext_pointer = pe->part_table + 1;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002572 pe->offset_from_dev_start = 0;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002573 pe->changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002574 g_partitions++;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002575 }
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002576 add_partition(g_partitions - 1, LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002577}
2578
2579static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002580new_partition(void)
2581{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002582 int i, free_primary = 0;
2583
2584 if (warn_geometry())
2585 return;
2586
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002587 if (LABEL_IS_SUN) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002588 add_sun_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002589 return;
2590 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002591 if (LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002592 sgi_add_partition(get_partition(0, g_partitions), LINUX_NATIVE);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002593 return;
2594 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002595 if (LABEL_IS_AIX) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002596 puts("Sorry - this fdisk cannot handle AIX disk labels.\n"
Denis Vlasenkobd852072007-03-19 14:43:38 +00002597"If you want to add DOS-type partitions, create a new empty DOS partition\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002598"table first (use 'o'). This will destroy the present disk contents.");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002599 return;
2600 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002601
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002602 for (i = 0; i < 4; i++)
2603 free_primary += !ptes[i].part_table->sys_ind;
Eric Andersenc48d49a2003-07-03 10:02:32 +00002604
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002605 if (!free_primary && g_partitions >= MAXIMUM_PARTS) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002606 puts("The maximum number of partitions has been created");
Eric Andersen84bdea82004-05-19 10:49:17 +00002607 return;
Rob Landleyb73451d2006-02-24 16:29:00 +00002608 }
Eric Andersenc48d49a2003-07-03 10:02:32 +00002609
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002610 if (!free_primary) {
2611 if (extended_offset)
2612 add_logical();
2613 else
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002614 puts("You must delete some partition and add "
2615 "an extended partition first");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002616 } else {
Denis Vlasenkodfce08f2007-03-19 14:45:10 +00002617 char c, line[80];
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00002618 snprintf(line, sizeof(line),
2619 "Command action\n"
2620 " %s\n"
2621 " p primary partition (1-4)\n",
2622 (extended_offset ?
2623 "l logical (5 or over)" : "e extended"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002624 while (1) {
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002625 c = read_nonempty(line);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002626 if ((c | 0x20) == 'p') {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002627 i = get_nonexisting_partition(0, 4);
2628 if (i >= 0)
2629 add_partition(i, LINUX_NATIVE);
2630 return;
2631 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002632 if (c == 'l' && extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002633 add_logical();
2634 return;
2635 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002636 if (c == 'e' && !extended_offset) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002637 i = get_nonexisting_partition(0, 4);
2638 if (i >= 0)
2639 add_partition(i, EXTENDED);
2640 return;
2641 }
Denis Vlasenkobd852072007-03-19 14:43:38 +00002642 printf("Invalid partition number "
2643 "for type '%c'\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002644 }
2645 }
2646}
2647
2648static void
Denys Vlasenkob347df92011-08-09 22:49:15 +02002649reread_partition_table(int leave)
2650{
2651 int i;
2652
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002653 puts("Calling ioctl() to re-read partition table");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002654 sync();
2655 /* Users with slow external USB disks on a 320MHz ARM system (year 2011)
2656 * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
2657 */
2658 sleep(1);
2659 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
2660 "WARNING: rereading partition table "
2661 "failed, kernel still uses old table");
2662#if 0
2663 if (dos_changed)
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002664 puts(
Denys Vlasenkob347df92011-08-09 22:49:15 +02002665 "\nWARNING: If you have created or modified any DOS 6.x\n"
2666 "partitions, please see the fdisk manual page for additional\n"
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002667 "information");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002668#endif
2669
2670 if (leave) {
2671 if (ENABLE_FEATURE_CLEAN_UP)
2672 close_dev_fd();
2673 exit(i != 0);
2674 }
2675}
2676
2677static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002678write_table(void)
2679{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002680 int i;
2681
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002682 if (LABEL_IS_DOS) {
Rob Landleyb73451d2006-02-24 16:29:00 +00002683 for (i = 0; i < 3; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002684 if (ptes[i].changed)
2685 ptes[3].changed = 1;
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002686 for (i = 3; i < g_partitions; i++) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002687 struct pte *pe = &ptes[i];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002688 if (pe->changed) {
2689 write_part_table_flag(pe->sectorbuffer);
Denys Vlasenkod958e902010-04-06 02:32:26 +02002690 write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002691 }
2692 }
2693 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002694 else if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002695 /* no test on change? the "altered" msg below might be mistaken */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002696 sgi_write_table();
2697 }
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002698 else if (LABEL_IS_SUN) {
Denys Vlasenko80856b32011-07-13 09:06:32 +02002699 for (i = 0; i < 8; i++) {
2700 if (ptes[i].changed) {
2701 sun_write_table();
2702 break;
2703 }
2704 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002705 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002706
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002707 puts("The partition table has been altered.");
Denys Vlasenkob347df92011-08-09 22:49:15 +02002708 reread_partition_table(1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002709}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002710#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002711
Denis Vlasenko834410a2006-11-29 12:00:28 +00002712#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002713#define MAX_PER_LINE 16
2714static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002715print_buffer(char *pbuffer)
2716{
2717 int i,l;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002718
2719 for (i = 0, l = 0; i < sector_size; i++, l++) {
2720 if (l == 0)
2721 printf("0x%03X:", i);
2722 printf(" %02X", (unsigned char) pbuffer[i]);
2723 if (l == MAX_PER_LINE - 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002724 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002725 l = -1;
2726 }
2727 }
2728 if (l > 0)
Denis Vlasenko4daad902007-09-27 10:20:47 +00002729 bb_putchar('\n');
2730 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002731}
2732
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002733static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002734print_raw(void)
2735{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002736 int i;
2737
Denis Vlasenkobd852072007-03-19 14:43:38 +00002738 printf("Device: %s\n", disk_device);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002739 if (LABEL_IS_SGI || LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002740 print_buffer(MBRbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002741 else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002742 for (i = 3; i < g_partitions; i++)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002743 print_buffer(ptes[i].sectorbuffer);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002744 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002745}
2746
2747static void
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002748move_begin(unsigned i)
Rob Landleyb73451d2006-02-24 16:29:00 +00002749{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002750 struct pte *pe = &ptes[i];
2751 struct partition *p = pe->part_table;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002752 sector_t new, first, nr_sects;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002753
2754 if (warn_geometry())
2755 return;
Denys Vlasenkod958e902010-04-06 02:32:26 +02002756 nr_sects = get_nr_sects(p);
2757 if (!p->sys_ind || !nr_sects || IS_EXTENDED(p->sys_ind)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002758 printf("Partition %u has no data area\n", i + 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002759 return;
2760 }
Denys Vlasenkofcad7682010-04-06 16:56:33 +02002761 first = get_partition_start_from_dev_start(pe); /* == pe->offset_from_dev_start + get_start_sect(p) */
Denys Vlasenkod958e902010-04-06 02:32:26 +02002762 new = read_int(0 /*was:first*/, first, first + nr_sects - 1, first, "New beginning of data");
2763 if (new != first) {
2764 sector_t new_relative = new - pe->offset_from_dev_start;
2765 nr_sects += (get_start_sect(p) - new_relative);
2766 set_start_sect(p, new_relative);
2767 set_nr_sects(p, nr_sects);
2768 read_nonempty("Recalculate C/H/S values? (Y/N): ");
2769 if ((line_ptr[0] | 0x20) == 'y')
2770 set_hsc_start_end(p, new, new + nr_sects - 1);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002771 pe->changed = 1;
2772 }
2773}
2774
2775static void
Rob Landleyb73451d2006-02-24 16:29:00 +00002776xselect(void)
2777{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002778 char c;
2779
Rob Landleyb73451d2006-02-24 16:29:00 +00002780 while (1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00002781 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02002782 c = 0x20 | read_nonempty("Expert command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002783 switch (c) {
2784 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002785 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002786 sun_set_alt_cyl();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002787 break;
2788 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002789 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002790 move_begin(get_partition(0, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002791 break;
2792 case 'c':
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002793 user_cylinders = g_cylinders =
2794 read_int(1, g_cylinders, 1048576, 0,
Denis Vlasenkobd852072007-03-19 14:43:38 +00002795 "Number of cylinders");
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002796 if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002797 sun_set_ncyl(g_cylinders);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002798 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002799 warn_cylinders();
2800 break;
2801 case 'd':
2802 print_raw();
2803 break;
2804 case 'e':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002805 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002806 sgi_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002807 else if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002808 sun_set_xcyl();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002809 else if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002810 x_list_table(1);
2811 break;
2812 case 'f':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002813 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002814 fix_partition_table_order();
2815 break;
2816 case 'g':
Denis Vlasenko834410a2006-11-29 12:00:28 +00002817#if ENABLE_FEATURE_SGI_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002818 create_sgilabel();
2819#endif
2820 break;
2821 case 'h':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002822 user_heads = g_heads = read_int(1, g_heads, 256, 0, "Number of heads");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002823 update_units();
2824 break;
2825 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002826 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002827 sun_set_ilfact();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002828 break;
2829 case 'o':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002830 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002831 sun_set_rspeed();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002832 break;
2833 case 'p':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002834 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002835 list_table(1);
2836 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002837 x_list_table(0);
2838 break;
2839 case 'q':
Denis Vlasenko4437d192008-04-17 00:12:10 +00002840 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002841 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00002842 bb_putchar('\n');
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00002843 exit(EXIT_SUCCESS);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002844 case 'r':
2845 return;
2846 case 's':
Denys Vlasenkod958e902010-04-06 02:32:26 +02002847 user_sectors = g_sectors = read_int(1, g_sectors, 63, 0, "Number of sectors");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002848 if (dos_compatible_flag) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00002849 sector_offset = g_sectors;
Denys Vlasenkod60752f2015-10-07 22:42:45 +02002850 puts("Warning: setting sector offset for DOS "
Denys Vlasenkoc52dc0e2017-08-10 15:12:11 +02002851 "compatibility");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002852 }
2853 update_units();
2854 break;
2855 case 'v':
2856 verify();
2857 break;
2858 case 'w':
2859 write_table(); /* does not return */
2860 break;
2861 case 'y':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00002862 if (LABEL_IS_SUN)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002863 sun_set_pcylcount();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002864 break;
2865 default:
2866 xmenu();
2867 }
2868 }
2869}
2870#endif /* ADVANCED mode */
2871
2872static int
Rob Landleyb73451d2006-02-24 16:29:00 +00002873is_ide_cdrom_or_tape(const char *device)
2874{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002875 FILE *procf;
2876 char buf[100];
2877 struct stat statbuf;
2878 int is_ide = 0;
2879
2880 /* No device was given explicitly, and we are trying some
2881 likely things. But opening /dev/hdc may produce errors like
2882 "hdc: tray open or drive not ready"
2883 if it happens to be a CD-ROM drive. It even happens that
2884 the process hangs on the attempt to read a music CD.
2885 So try to be careful. This only works since 2.1.73. */
2886
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002887 if (!is_prefixed_with(device, "/dev/hd"))
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002888 return 0;
2889
2890 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
Denis Vlasenko5415c852008-07-21 23:05:26 +00002891 procf = fopen_for_read(buf);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002892 if (procf != NULL && fgets(buf, sizeof(buf), procf))
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01002893 is_ide = (is_prefixed_with(buf, "cdrom") ||
2894 is_prefixed_with(buf, "tape"));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002895 else
2896 /* Now when this proc file does not exist, skip the
2897 device when it is read-only. */
2898 if (stat(device, &statbuf) == 0)
2899 is_ide = ((statbuf.st_mode & 0222) == 0);
2900
2901 if (procf)
2902 fclose(procf);
2903 return is_ide;
2904}
2905
Rob Landley5527b912006-02-25 03:46:10 +00002906
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002907static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002908open_list_and_close(const char *device, int user_specified)
Rob Landleyb73451d2006-02-24 16:29:00 +00002909{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002910 int gb;
2911
2912 disk_device = device;
2913 if (setjmp(listingbuf))
2914 return;
2915 if (!user_specified)
2916 if (is_ide_cdrom_or_tape(device))
2917 return;
Denis Vlasenko4437d192008-04-17 00:12:10 +00002918
2919 /* Open disk_device, save file descriptor to dev_fd */
2920 errno = 0;
2921 gb = get_boot(TRY_ONLY);
2922 if (gb > 0) { /* I/O error */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002923 /* Ignore other errors, since we try IDE
2924 and SCSI hard disks which may not be
2925 installed on the system. */
Denis Vlasenko4437d192008-04-17 00:12:10 +00002926 if (user_specified || errno == EACCES)
2927 bb_perror_msg("can't open '%s'", device);
2928 return;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002929 }
Denis Vlasenko4437d192008-04-17 00:12:10 +00002930
2931 if (gb < 0) { /* no DOS signature */
2932 list_disk_geometry();
2933 if (LABEL_IS_AIX)
2934 goto ret;
2935#if ENABLE_FEATURE_OSF_LABEL
2936 if (bsd_trydev(device) < 0)
2937#endif
2938 printf("Disk %s doesn't contain a valid "
2939 "partition table\n", device);
2940 } else {
2941 list_table(0);
2942#if ENABLE_FEATURE_FDISK_WRITABLE
2943 if (!LABEL_IS_SUN && g_partitions > 4) {
2944 delete_partition(ext_index);
2945 }
2946#endif
2947 }
2948 ret:
Denis Vlasenkoc033d512008-04-17 01:52:28 +00002949 close_dev_fd();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002950}
2951
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002952/* Is it a whole disk? The digit check is still useful
2953 for Xen devices for example. */
2954static int is_whole_disk(const char *disk)
2955{
2956 unsigned len;
2957 int fd = open(disk, O_RDONLY);
2958
2959 if (fd != -1) {
2960 struct hd_geometry geometry;
2961 int err = ioctl(fd, HDIO_GETGEO, &geometry);
2962 close(fd);
2963 if (!err)
2964 return (geometry.start == 0);
2965 }
2966
2967 /* Treat "nameN" as a partition name, not whole disk */
2968 /* note: mmcblk0 should work from the geometry check above */
2969 len = strlen(disk);
2970 if (len != 0 && isdigit(disk[len - 1]))
2971 return 0;
2972
2973 return 1;
2974}
2975
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002976/* for fdisk -l: try all things in /proc/partitions
2977 that look like a partition name (do not end in a digit) */
2978static void
Denis Vlasenko4437d192008-04-17 00:12:10 +00002979list_devs_in_proc_partititons(void)
Rob Landleyb73451d2006-02-24 16:29:00 +00002980{
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002981 FILE *procpt;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002982 char line[100], ptname[100], devname[120];
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002983 int ma, mi, sz;
2984
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00002985 procpt = fopen_or_warn("/proc/partitions", "r");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002986
2987 while (fgets(line, sizeof(line), procpt)) {
Denys Vlasenkoddf78502009-09-16 03:03:13 +02002988 if (sscanf(line, " %u %u %u %[^\n ]",
Rob Landleyb73451d2006-02-24 16:29:00 +00002989 &ma, &mi, &sz, ptname) != 4)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002990 continue;
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002991
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002992 sprintf(devname, "/dev/%s", ptname);
Lauri Kasanen9cfcc4d2011-04-30 21:31:05 +02002993 if (is_whole_disk(devname))
2994 open_list_and_close(devname, 0);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002995 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00002996#if ENABLE_FEATURE_CLEAN_UP
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002997 fclose(procpt);
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00002998#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00002999}
3000
Denis Vlasenko834410a2006-11-29 12:00:28 +00003001#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003002static void
Rob Landleyb73451d2006-02-24 16:29:00 +00003003unknown_command(int c)
3004{
Denis Vlasenkobd852072007-03-19 14:43:38 +00003005 printf("%c: unknown command\n", c);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003006}
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003007#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003008
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003009int fdisk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003010int fdisk_main(int argc UNUSED_PARAM, char **argv)
Rob Landleyb73451d2006-02-24 16:29:00 +00003011{
Denis Vlasenko834410a2006-11-29 12:00:28 +00003012 unsigned opt;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003013 /*
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003014 * fdisk -v
3015 * fdisk -l [-b sectorsize] [-u] device ...
3016 * fdisk -s [partition] ...
3017 * fdisk [-b sectorsize] [-u] device
3018 *
3019 * Options -C, -H, -S set the geometry.
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003020 */
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003021 INIT_G();
Denis Vlasenko8e1a0cc2007-03-18 14:42:45 +00003022
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003023 close_dev_fd(); /* needed: fd 3 must not stay closed */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003024
Denys Vlasenko237bedd2016-07-06 21:58:02 +02003025 opt = getopt32(argv, "b:+C:+H:+lS:+u" IF_FEATURE_FDISK_BLKSIZE("s"),
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003026 &sector_size, &user_cylinders, &user_heads, &user_sectors);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003027 argv += optind;
Denys Vlasenkod958e902010-04-06 02:32:26 +02003028 if (opt & OPT_b) {
Denis Vlasenko834410a2006-11-29 12:00:28 +00003029 /* Ugly: this sector size is really per device,
Denys Vlasenkod958e902010-04-06 02:32:26 +02003030 * so cannot be combined with multiple disks,
3031 * and the same goes for the C/H/S options.
3032 */
3033 if (sector_size < 512
3034 || sector_size > 0x10000
3035 || (sector_size & (sector_size-1)) /* not power of 2 */
3036 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00003037 bb_show_usage();
Denys Vlasenkod958e902010-04-06 02:32:26 +02003038 }
Denis Vlasenko834410a2006-11-29 12:00:28 +00003039 sector_offset = 2;
3040 user_set_sector_size = 1;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003041 }
Denis Vlasenko04e11c92008-02-10 19:44:20 +00003042 if (user_heads <= 0 || user_heads >= 256)
3043 user_heads = 0;
3044 if (user_sectors <= 0 || user_sectors >= 64)
3045 user_sectors = 0;
3046 if (opt & OPT_u)
3047 display_in_cyl_units = 0; // -u
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003048
Denis Vlasenko834410a2006-11-29 12:00:28 +00003049#if ENABLE_FEATURE_FDISK_WRITABLE
3050 if (opt & OPT_l) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003051 nowarn = 1;
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003052#endif
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003053 if (*argv) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003054 listing = 1;
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003055 do {
Denis Vlasenko4437d192008-04-17 00:12:10 +00003056 open_list_and_close(*argv, 1);
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003057 } while (*++argv);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003058 } else {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003059 /* we don't have device names, */
3060 /* use /proc/partitions instead */
Denis Vlasenko4437d192008-04-17 00:12:10 +00003061 list_devs_in_proc_partititons();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003062 }
3063 return 0;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003064#if ENABLE_FEATURE_FDISK_WRITABLE
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003065 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003066#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003067
Denis Vlasenko834410a2006-11-29 12:00:28 +00003068#if ENABLE_FEATURE_FDISK_BLKSIZE
3069 if (opt & OPT_s) {
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003070 int j;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003071
3072 nowarn = 1;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003073 if (!argv[0])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003074 bb_show_usage();
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003075 for (j = 0; argv[j]; j++) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +00003076 unsigned long long size;
3077 fd = xopen(argv[j], O_RDONLY);
Denis Vlasenko4437d192008-04-17 00:12:10 +00003078 size = bb_BLKGETSIZE_sectors(fd) / 2;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003079 close(fd);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003080 if (argv[1])
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003081 printf("%llu\n", size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003082 else
Denys Vlasenkoddf78502009-09-16 03:03:13 +02003083 printf("%s: %llu\n", argv[j], size);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003084 }
3085 return 0;
3086 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003087#endif
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003088
Denis Vlasenko834410a2006-11-29 12:00:28 +00003089#if ENABLE_FEATURE_FDISK_WRITABLE
Denys Vlasenkoe992bae2009-11-28 15:18:53 +01003090 if (!argv[0] || argv[1])
Manuel Novoa III cad53642003-03-19 09:13:01 +00003091 bb_show_usage();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003092
Denis Vlasenko834410a2006-11-29 12:00:28 +00003093 disk_device = argv[0];
Denis Vlasenko4437d192008-04-17 00:12:10 +00003094 get_boot(OPEN_MAIN);
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003095
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003096 if (LABEL_IS_OSF) {
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003097 /* OSF label, and no DOS label */
Denis Vlasenkobd852072007-03-19 14:43:38 +00003098 printf("Detected an OSF/1 disklabel on %s, entering "
3099 "disklabel mode\n", disk_device);
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003100 bsd_select();
Rob Landley5527b912006-02-25 03:46:10 +00003101 /*Why do we do this? It seems to be counter-intuitive*/
Denis Vlasenko4437d192008-04-17 00:12:10 +00003102 current_label_type = LABEL_DOS;
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003103 /* If we return we may want to make an empty DOS label? */
3104 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003105
3106 while (1) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +00003107 int c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00003108 bb_putchar('\n');
Denys Vlasenkod958e902010-04-06 02:32:26 +02003109 c = 0x20 | read_nonempty("Command (m for help): ");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003110 switch (c) {
3111 case 'a':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003112 if (LABEL_IS_DOS)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003113 toggle_active(get_partition(1, g_partitions));
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003114 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003115 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003116 0x01);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003117 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003118 sgi_set_bootpartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003119 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003120 else
3121 unknown_command(c);
3122 break;
3123 case 'b':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003124 if (LABEL_IS_SGI) {
Denis Vlasenkobd852072007-03-19 14:43:38 +00003125 printf("\nThe current boot file is: %s\n",
Rob Landleyb73451d2006-02-24 16:29:00 +00003126 sgi_get_bootfile());
Denis Vlasenkobd852072007-03-19 14:43:38 +00003127 if (read_maybe_empty("Please enter the name of the "
Denys Vlasenko69675782013-01-14 01:34:48 +01003128 "new boot file: ") == '\n')
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003129 puts("Boot file unchanged");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003130 else
3131 sgi_set_bootfile(line_ptr);
Denis Vlasenko834410a2006-11-29 12:00:28 +00003132 }
3133#if ENABLE_FEATURE_OSF_LABEL
3134 else
Denis Vlasenkoefeed5e2006-10-14 16:16:03 +00003135 bsd_select();
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003136#endif
3137 break;
3138 case 'c':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003139 if (LABEL_IS_DOS)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003140 toggle_dos_compatibility_flag();
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003141 else if (LABEL_IS_SUN)
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003142 toggle_sunflags(get_partition(1, g_partitions),
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003143 0x10);
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003144 else if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003145 sgi_set_swappartition(
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003146 get_partition(1, g_partitions));
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003147 else
3148 unknown_command(c);
3149 break;
3150 case 'd':
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003151 {
Eric Andersen040f4402003-07-30 08:40:37 +00003152 int j;
Eric Andersen040f4402003-07-30 08:40:37 +00003153 /* If sgi_label then don't use get_existing_partition,
3154 let the user select a partition, since
3155 get_existing_partition() only works for Linux-like
3156 partition tables */
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003157 if (!LABEL_IS_SGI) {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003158 j = get_existing_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003159 } else {
Denis Vlasenkof77f3692007-12-16 17:22:33 +00003160 j = get_partition(1, g_partitions);
Eric Andersen040f4402003-07-30 08:40:37 +00003161 }
Glenn L McGrath4dcc2dd2003-01-04 11:56:06 +00003162 if (j >= 0)
3163 delete_partition(j);
3164 }
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003165 break;
3166 case 'i':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003167 if (LABEL_IS_SGI)
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003168 create_sgiinfo();
3169 else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003170 unknown_command(c);
3171 case 'l':
3172 list_types(get_sys_types());
3173 break;
3174 case 'm':
3175 menu();
3176 break;
3177 case 'n':
3178 new_partition();
3179 break;
3180 case 'o':
3181 create_doslabel();
3182 break;
3183 case 'p':
3184 list_table(0);
3185 break;
3186 case 'q':
Denis Vlasenkoc033d512008-04-17 01:52:28 +00003187 if (ENABLE_FEATURE_CLEAN_UP)
3188 close_dev_fd();
Denis Vlasenko4daad902007-09-27 10:20:47 +00003189 bb_putchar('\n');
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003190 return 0;
3191 case 's':
Denis Vlasenko834410a2006-11-29 12:00:28 +00003192#if ENABLE_FEATURE_SUN_LABEL
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003193 create_sunlabel();
3194#endif
3195 break;
3196 case 't':
3197 change_sysid();
3198 break;
3199 case 'u':
3200 change_units();
3201 break;
3202 case 'v':
3203 verify();
3204 break;
3205 case 'w':
Denys Vlasenko80856b32011-07-13 09:06:32 +02003206 write_table(); /* does not return */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003207 break;
Denis Vlasenko834410a2006-11-29 12:00:28 +00003208#if ENABLE_FEATURE_FDISK_ADVANCED
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003209 case 'x':
Denis Vlasenko98ae2162006-10-12 19:30:44 +00003210 if (LABEL_IS_SGI) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +02003211 puts("\n\tSorry, no experts menu for SGI "
3212 "partition tables available\n");
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003213 } else
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003214 xselect();
3215 break;
3216#endif
3217 default:
3218 unknown_command(c);
3219 menu();
3220 }
3221 }
3222 return 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003223#endif /* FEATURE_FDISK_WRITABLE */
Glenn L McGrath441e7ef2002-11-26 22:00:21 +00003224}