fdisk: rewrite dump first sector add dump disklabel command

Expert command (m for help): D

PMBR: offset = 0, size = 512 bytes.
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
000001c0  01 00 ee fe ff ff 01 00  00 00 ff 9f 0f 00 00 00
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa

GPT Header: offset = 512, size = 512 bytes.
00000200  45 46 49 20 50 41 52 54  00 00 01 00 00 02 00 00
00000210  ae 36 81 28 00 00 00 00  01 00 00 00 00 00 00 00
00000220  ff 9f 0f 00 00 00 00 00  00 08 00 00 00 00 00 00
00000230  de 9f 0f 00 00 00 00 00  e0 9d d8 d5 d6 da 1a 44
00000240  98 57 e4 11 64 88 ce 3b  02 00 00 00 00 00 00 00
00000250  80 00 00 00 80 00 00 00  4b c7 c9 54 00 00 00 00
00000260  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*

GPT Entries: offset = 1024, size = 16384 bytes.
00000400  af 3d c6 0f 83 84 72 47  8e 79 3d 69 d8 47 7d e4
00000410  bc ae 48 5e 22 e5 ca 4c  aa 98 14 6d c9 1d 72 f4
00000420  00 08 00 00 00 00 00 00  de 9f 0f 00 00 00 00 00
00000430  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-09-03 17:36:35 +02:00
parent 775001ad2b
commit e916600f54
3 changed files with 80 additions and 22 deletions

View File

@ -95,7 +95,8 @@ struct menu menu_generic = {
MENU_ENT ('t', N_("change a partition type")),
MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
MENU_XENT('d', N_("print the raw data of the first sector")),
MENU_XENT('d', N_("print the raw data of the first sector from the device")),
MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
MENU_SEP(N_("Misc")),
MENU_BENT ('m', N_("print this menu")),
@ -435,7 +436,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
if (ent->expert) {
switch (ent->key) {
case 'd':
print_raw(cxt);
dump_firstsector(cxt);
break;
case 'D':
dump_disklabel(cxt);
break;
case 'r':
rc = fdisk_context_enable_details(cxt, 0);

View File

@ -24,6 +24,7 @@
#include "c.h"
#include "xalloc.h"
#include "all-io.h"
#include "nls.h"
#include "rpmatch.h"
#include "blkdev.h"
@ -231,39 +232,90 @@ void list_disk_geometry(struct fdisk_context *cxt)
fdisk_colon(cxt, _("Disk identifier: %s"), id);
}
#define MAX_PER_LINE 16
static void print_buffer(struct fdisk_context *cxt, unsigned char pbuffer[])
static size_t skip_empty(const unsigned char *buf, size_t i, size_t sz)
{
unsigned int i, l;
size_t next;
const unsigned char *p0 = buf + i;
for (i = 0, l = 0; i < cxt->sector_size; i++, l++) {
if (l == 0)
printf("0x%03X:", i);
printf(" %02X", pbuffer[i]);
if (l == MAX_PER_LINE - 1) {
printf("\n");
for (next = i + 16; next < sz; next += 16) {
if (memcmp(p0, buf + next, 16) != 0)
break;
}
return next == i + 16 ? i : next;
}
static void dump_buffer(off_t base, unsigned char *buf, size_t sz, int all)
{
size_t i, l, next = 0;
if (!buf)
return;
for (i = 0, l = 0; i < sz; i++, l++) {
if (l == 0) {
if (all == 0 && !next)
next = skip_empty(buf, i, sz);
printf("%08jx ", base + i);
}
printf(" %02x", buf[i]);
if (l == 7) /* words separator */
fputs(" ", stdout);
else if (l == 15) {
fputc('\n', stdout); /* next line */
l = -1;
if (next > i) {
printf("*\n");
i = next - 1;
}
next = 0;
}
}
if (l > 0)
printf("\n");
printf("\n");
}
void print_raw(struct fdisk_context *cxt)
static void dump_blkdev(struct fdisk_context *cxt, const char *name,
off_t offset, size_t size, int all)
{
unsigned char *buf = NULL;
fdisk_colon(cxt, _("\n%s: offset = %ju, size = %zu bytes."),
name, offset, size);
if (lseek(cxt->dev_fd, offset, SEEK_SET) == (off_t) -1)
fdisk_warn(cxt, _("cannot seek"));
else if (!(buf = malloc(size)))
fdisk_warn(cxt, _("cannot allocate"));
else if (read_all(cxt->dev_fd, (char *) buf, size) != (ssize_t) size)
fdisk_warn(cxt, _("cannot read"));
else
dump_buffer(offset, buf, size, all);
free(buf);
}
void dump_firstsector(struct fdisk_context *cxt)
{
int all = !isatty(STDOUT_FILENO);
assert(cxt);
assert(cxt->label);
printf(_("Device: %s\n"), cxt->dev_path);
if (fdisk_is_disklabel(cxt, SUN) ||
fdisk_is_disklabel(cxt, SGI) ||
fdisk_is_disklabel(cxt, GPT) ||
fdisk_is_disklabel(cxt, DOS))
print_buffer(cxt, cxt->firstsector);
dump_blkdev(cxt, _("First sector"), 0, cxt->sector_size, all);
}
/* TODO: print also EBR (extended partition) buffer */
void dump_disklabel(struct fdisk_context *cxt)
{
int all = !isatty(STDOUT_FILENO);
int i = 0;
const char *name = NULL;
off_t offset = 0;
size_t size = 0;
assert(cxt);
assert(cxt->label);
while (fdisk_locate_disklabel(cxt, i++, &name, &offset, &size) == 0 && size)
dump_blkdev(cxt, name, offset, size, all);
}
static int is_ide_cdrom_or_tape(char *device)

View File

@ -33,9 +33,11 @@ extern int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
void *data __attribute__((__unused__)));
/* prototypes for fdisk.c */
extern void dump_firstsector(struct fdisk_context *cxt);
extern void dump_disklabel(struct fdisk_context *cxt);
extern void list_partition_types(struct fdisk_context *cxt);
extern void list_disk_geometry(struct fdisk_context *cxt);
extern void print_raw(struct fdisk_context *cxt);
extern void change_partition_type(struct fdisk_context *cxt);
extern struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt);
extern void reread_partition_table(struct fdisk_context *cxt, int leave);