diff options
-rw-r--r-- | fdisks/fdisk-menu.c | 8 | ||||
-rw-r--r-- | fdisks/fdisk.c | 90 | ||||
-rw-r--r-- | fdisks/fdisk.h | 4 |
3 files changed, 80 insertions, 22 deletions
diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c index 0ead2e477..e381c34f3 100644 --- a/fdisks/fdisk-menu.c +++ b/fdisks/fdisk-menu.c @@ -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); diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 0bc9fcd79..3ee32d28b 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -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); } +static size_t skip_empty(const unsigned char *buf, size_t i, size_t sz) +{ + size_t next; + const unsigned char *p0 = buf + i; + + for (next = i + 16; next < sz; next += 16) { + if (memcmp(p0, buf + next, 16) != 0) + break; + } + + return next == i + 16 ? i : next; +} -#define MAX_PER_LINE 16 -static void print_buffer(struct fdisk_context *cxt, unsigned char pbuffer[]) +static void dump_buffer(off_t base, unsigned char *buf, size_t sz, int all) { - unsigned int i, l; - - 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"); + 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); +} + +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); - /* TODO: print also EBR (extended partition) buffer */ + 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) diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 0f427e29e..c97bf9435 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -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); |