2006-12-06 17:25:33 -06:00
|
|
|
/*
|
|
|
|
fdisk.h
|
|
|
|
*/
|
|
|
|
|
2009-10-15 18:26:46 -05:00
|
|
|
#include "c.h"
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
#define DEFAULT_SECTOR_SIZE 512
|
|
|
|
#define MAX_SECTOR_SIZE 2048
|
|
|
|
#define SECTOR_SIZE 512 /* still used in BSD code */
|
2006-12-06 17:25:35 -06:00
|
|
|
#define MAXIMUM_PARTS 60
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
#define ACTIVE_FLAG 0x80
|
|
|
|
|
|
|
|
#define EXTENDED 0x05
|
|
|
|
#define WIN98_EXTENDED 0x0f
|
|
|
|
#define LINUX_PARTITION 0x81
|
|
|
|
#define LINUX_SWAP 0x82
|
|
|
|
#define LINUX_NATIVE 0x83
|
|
|
|
#define LINUX_EXTENDED 0x85
|
2006-12-06 17:25:46 -06:00
|
|
|
#define LINUX_LVM 0x8e
|
|
|
|
#define LINUX_RAID 0xfd
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2012-05-06 07:10:18 -05:00
|
|
|
#define ALIGN_UP 1
|
|
|
|
#define ALIGN_DOWN 2
|
|
|
|
#define ALIGN_NEAREST 3
|
|
|
|
|
|
|
|
#define LINE_LENGTH 800
|
|
|
|
|
2006-12-06 17:25:37 -06:00
|
|
|
#define IS_EXTENDED(i) \
|
|
|
|
((i) == EXTENDED || (i) == WIN98_EXTENDED || (i) == LINUX_EXTENDED)
|
|
|
|
|
2006-12-06 17:25:39 -06:00
|
|
|
#define cround(n) (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
|
|
|
|
#define scround(x) (((x)+units_per_sector-1)/units_per_sector)
|
2006-12-06 17:25:33 -06:00
|
|
|
|
2012-05-21 15:32:25 -05:00
|
|
|
/* fdisk debugging flags/options */
|
|
|
|
#define FDISK_DEBUG_INIT (1 << 1)
|
|
|
|
#define FDISK_DEBUG_CONTEXT (1 << 2)
|
|
|
|
#define FDISK_DEBUG_ALL 0xFFFF
|
|
|
|
|
|
|
|
# define ON_DBG(m, x) do { \
|
|
|
|
if ((FDISK_DEBUG_ ## m) & fdisk_debug_mask) { \
|
|
|
|
x; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
# define DBG(m, x) do { \
|
|
|
|
if ((FDISK_DEBUG_ ## m) & fdisk_debug_mask) { \
|
|
|
|
fprintf(stderr, "%d: fdisk: %8s: ", getpid(), # m); \
|
|
|
|
x; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
# define DBG_FLUSH do { \
|
|
|
|
if (fdisk_debug_mask && \
|
|
|
|
fdisk_debug_mask != FDISK_DEBUG_INIT) \
|
|
|
|
fflush(stderr); \
|
|
|
|
} while(0)
|
|
|
|
|
2012-05-23 04:09:49 -05:00
|
|
|
static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
|
|
|
|
dbgprint(const char *mesg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, mesg);
|
|
|
|
vfprintf(stderr, mesg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2012-05-21 15:32:25 -05:00
|
|
|
extern int fdisk_debug_mask;
|
|
|
|
extern void fdisk_init_debug(int mask);
|
|
|
|
|
2006-12-06 17:25:35 -06:00
|
|
|
struct partition {
|
|
|
|
unsigned char boot_ind; /* 0x80 - active */
|
|
|
|
unsigned char head; /* starting head */
|
|
|
|
unsigned char sector; /* starting sector */
|
|
|
|
unsigned char cyl; /* starting cylinder */
|
|
|
|
unsigned char sys_ind; /* What partition type */
|
|
|
|
unsigned char end_head; /* end head */
|
|
|
|
unsigned char end_sector; /* end sector */
|
|
|
|
unsigned char end_cyl; /* end cylinder */
|
|
|
|
unsigned char start4[4]; /* starting sector counting from 0 */
|
|
|
|
unsigned char size4[4]; /* nr of sectors in partition */
|
2012-01-05 10:01:19 -06:00
|
|
|
} __attribute__ ((packed));
|
2006-12-06 17:25:35 -06:00
|
|
|
|
2011-11-09 12:04:12 -06:00
|
|
|
enum menutype {
|
|
|
|
MAIN_MENU,
|
|
|
|
EXPERT_MENU,
|
|
|
|
};
|
|
|
|
|
2012-04-27 06:23:51 -05:00
|
|
|
enum failure {
|
|
|
|
ioctl_error,
|
|
|
|
unable_to_read,
|
|
|
|
unable_to_seek,
|
|
|
|
unable_to_write
|
|
|
|
};
|
2006-12-06 17:25:33 -06:00
|
|
|
|
2006-12-06 17:25:43 -06:00
|
|
|
struct geom {
|
|
|
|
unsigned int heads;
|
|
|
|
unsigned int sectors;
|
|
|
|
unsigned int cylinders;
|
|
|
|
};
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
typedef unsigned long long sector_t;
|
|
|
|
|
2012-05-21 15:28:03 -05:00
|
|
|
struct fdisk_context {
|
|
|
|
int dev_fd; /* device descriptor */
|
|
|
|
char *dev_path; /* device path */
|
2012-06-03 13:15:17 -05:00
|
|
|
|
|
|
|
/* topology */
|
|
|
|
unsigned long io_size;
|
|
|
|
unsigned long min_io_size;
|
|
|
|
unsigned long phy_sector_size; /* physical size */
|
|
|
|
unsigned long sector_size; /* logical size */
|
|
|
|
unsigned long alignment_offset;
|
2012-06-03 13:15:20 -05:00
|
|
|
|
|
|
|
/* geometry */
|
|
|
|
sector_t total_sectors; /* in logical sectors */
|
2012-05-21 15:28:03 -05:00
|
|
|
};
|
|
|
|
|
2012-05-23 04:24:04 -05:00
|
|
|
extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern int fdisk_dev_has_topology(struct fdisk_context *cxt);
|
2012-05-21 15:28:03 -05:00
|
|
|
extern void fdisk_free_context(struct fdisk_context *cxt);
|
|
|
|
|
2006-12-06 17:25:33 -06:00
|
|
|
/* prototypes for fdisk.c */
|
2006-12-06 17:26:12 -06:00
|
|
|
extern char *disk_device, *line_ptr;
|
|
|
|
extern int fd, partitions;
|
|
|
|
extern unsigned int display_in_cyl_units, units_per_sector;
|
2006-12-06 17:25:43 -06:00
|
|
|
extern void change_units(void);
|
2012-05-27 14:44:04 -05:00
|
|
|
extern void fatal(struct fdisk_context *cxt, enum failure why);
|
|
|
|
extern void get_geometry(struct fdisk_context *, struct geom *);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern int get_partition(struct fdisk_context *cxt, int warn, int max);
|
2006-12-06 17:25:35 -06:00
|
|
|
extern void list_types(struct systypes *sys);
|
2011-01-05 09:50:47 -06:00
|
|
|
extern int read_line (int *asked);
|
2006-12-06 17:25:33 -06:00
|
|
|
extern char read_char(char *mesg);
|
2006-12-06 17:25:35 -06:00
|
|
|
extern int read_hex(struct systypes *sys);
|
2012-05-27 14:44:04 -05:00
|
|
|
extern void reread_partition_table(struct fdisk_context *cxt, int leave);
|
2006-12-06 17:25:43 -06:00
|
|
|
extern struct partition *get_part_table(int);
|
|
|
|
extern int valid_part_table_flag(unsigned char *b);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern unsigned int read_int(struct fdisk_context *cxt,
|
|
|
|
unsigned int low, unsigned int dflt,
|
2006-12-06 17:26:12 -06:00
|
|
|
unsigned int high, unsigned int base, char *mesg);
|
2011-11-09 12:04:12 -06:00
|
|
|
extern void print_menu(enum menutype);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern void print_partition_size(struct fdisk_context *cxt, int num, sector_t start, sector_t stop, int sysid);
|
2006-12-06 17:25:43 -06:00
|
|
|
|
2009-10-16 14:49:33 -05:00
|
|
|
extern void zeroize_mbr_buffer(void);
|
2012-05-27 14:44:13 -05:00
|
|
|
extern void fill_bounds(sector_t *first, sector_t *last);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern unsigned int heads, cylinders;
|
2012-05-27 14:44:13 -05:00
|
|
|
extern sector_t sectors;
|
2007-06-27 16:36:08 -05:00
|
|
|
extern char *partition_type(unsigned char type);
|
|
|
|
extern void update_units(void);
|
|
|
|
extern char read_chars(char *mesg);
|
|
|
|
extern void set_changed(int);
|
|
|
|
extern void set_all_unchanged(void);
|
2012-04-28 17:02:45 -05:00
|
|
|
extern int warn_geometry(void);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern void warn_limits(struct fdisk_context *cxt);
|
|
|
|
extern void warn_alignment(struct fdisk_context *cxt);
|
|
|
|
extern unsigned int read_int_with_suffix(struct fdisk_context *cxt,
|
|
|
|
unsigned int low, unsigned int dflt, unsigned int high,
|
2012-05-06 07:10:18 -05:00
|
|
|
unsigned int base, char *mesg, int *is_suffix_used);
|
2012-06-03 13:15:17 -05:00
|
|
|
extern sector_t align_lba(struct fdisk_context *cxt, sector_t lba, int direction);
|
|
|
|
extern int get_partition_dflt(struct fdisk_context *cxt, int warn, int max, int dflt);
|
2006-12-06 17:25:39 -06:00
|
|
|
|
|
|
|
#define PLURAL 0
|
|
|
|
#define SINGULAR 1
|
2007-05-30 10:41:14 -05:00
|
|
|
extern const char * str_units(int);
|
2006-12-06 17:25:33 -06:00
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
extern sector_t get_nr_sects(struct partition *p);
|
2006-12-06 17:25:35 -06:00
|
|
|
|
2011-07-03 05:01:21 -05:00
|
|
|
enum labeltype {
|
2011-11-09 12:04:12 -06:00
|
|
|
DOS_LABEL = 1,
|
|
|
|
SUN_LABEL = 2,
|
|
|
|
SGI_LABEL = 4,
|
|
|
|
AIX_LABEL = 8,
|
|
|
|
OSF_LABEL = 16,
|
|
|
|
MAC_LABEL = 32,
|
|
|
|
ANY_LABEL = -1
|
2011-07-03 05:01:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern enum labeltype disklabel;
|
2006-12-06 17:25:43 -06:00
|
|
|
|
2012-04-28 17:02:45 -05:00
|
|
|
/*
|
|
|
|
* Raw disk label. For DOS-type partition tables the MBR,
|
|
|
|
* with descriptions of the primary partitions.
|
|
|
|
*/
|
2012-05-02 07:05:51 -05:00
|
|
|
extern unsigned char *MBRbuffer;
|
|
|
|
extern int MBRbuffer_changed;
|
2012-05-06 07:10:18 -05:00
|
|
|
extern unsigned long grain;
|
2012-06-03 13:15:20 -05:00
|
|
|
|
2012-04-28 17:02:45 -05:00
|
|
|
/* start_sect and nr_sects are stored little endian on all machines */
|
|
|
|
/* moreover, they are not aligned correctly */
|
2012-05-02 07:05:51 -05:00
|
|
|
static inline void
|
2012-04-28 17:02:45 -05:00
|
|
|
store4_little_endian(unsigned char *cp, unsigned int val) {
|
|
|
|
cp[0] = (val & 0xff);
|
|
|
|
cp[1] = ((val >> 8) & 0xff);
|
|
|
|
cp[2] = ((val >> 16) & 0xff);
|
|
|
|
cp[3] = ((val >> 24) & 0xff);
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:05:51 -05:00
|
|
|
static inline unsigned int read4_little_endian(const unsigned char *cp)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
|
|
|
return (unsigned int)(cp[0]) + ((unsigned int)(cp[1]) << 8)
|
|
|
|
+ ((unsigned int)(cp[2]) << 16)
|
|
|
|
+ ((unsigned int)(cp[3]) << 24);
|
|
|
|
}
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
static inline void set_nr_sects(struct partition *p, sector_t nr_sects)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
|
|
|
store4_little_endian(p->size4, nr_sects);
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:05:51 -05:00
|
|
|
static inline void set_start_sect(struct partition *p, unsigned int start_sect)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
|
|
|
store4_little_endian(p->start4, start_sect);
|
|
|
|
}
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
static inline void seek_sector(struct fdisk_context *cxt, sector_t secno)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
2012-06-03 13:15:17 -05:00
|
|
|
off_t offset = (off_t) secno * cxt->sector_size;
|
2012-05-27 14:44:04 -05:00
|
|
|
if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1)
|
|
|
|
fatal(cxt, unable_to_seek);
|
2012-04-28 17:02:45 -05:00
|
|
|
}
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
static inline void read_sector(struct fdisk_context *cxt, sector_t secno, unsigned char *buf)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
2012-05-27 14:44:04 -05:00
|
|
|
seek_sector(cxt, secno);
|
2012-06-03 13:15:17 -05:00
|
|
|
if (read(cxt->dev_fd, buf, cxt->sector_size) != cxt->sector_size)
|
2012-05-27 14:44:04 -05:00
|
|
|
fatal(cxt, unable_to_read);
|
2012-04-28 17:02:45 -05:00
|
|
|
}
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
static inline void write_sector(struct fdisk_context *cxt, sector_t secno, unsigned char *buf)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
2012-05-27 14:44:04 -05:00
|
|
|
seek_sector(cxt, secno);
|
2012-06-03 13:15:17 -05:00
|
|
|
if (write(cxt->dev_fd, buf, cxt->sector_size) != cxt->sector_size)
|
2012-05-27 14:44:04 -05:00
|
|
|
fatal(cxt, unable_to_write);
|
2012-04-28 17:02:45 -05:00
|
|
|
}
|
|
|
|
|
2012-05-27 14:44:13 -05:00
|
|
|
static inline sector_t get_start_sect(struct partition *p)
|
2012-04-28 17:02:45 -05:00
|
|
|
{
|
|
|
|
return read4_little_endian(p->start4);
|
|
|
|
}
|
|
|
|
|
2012-05-06 07:10:18 -05:00
|
|
|
static inline int is_cleared_partition(struct partition *p)
|
|
|
|
{
|
|
|
|
return !(!p || p->boot_ind || p->head || p->sector || p->cyl ||
|
|
|
|
p->sys_ind || p->end_head || p->end_sector || p->end_cyl ||
|
|
|
|
get_start_sect(p) || get_nr_sects(p));
|
|
|
|
}
|