]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: rewrite dump first sector add dump disklabel command
authorKarel Zak <kzak@redhat.com>
Tue, 3 Sep 2013 15:36:35 +0000 (17:36 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:47:10 +0000 (16:47 +0200)
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>
fdisks/fdisk-menu.c
fdisks/fdisk.c
fdisks/fdisk.h

index 0ead2e4779d3ce4a0134712aa06a8034316dc4c0..e381c34f3a38007182884d92b76e437e8af835f2 100644 (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);
index 0bc9fcd79492469ad6573d18b98a6380eeb0e6f1..3ee32d28bdee02a005f506ae175814a60b441499 100644 (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);
 }
 
+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)
index 0f427e29e83e39911c2451961c0510783b14b23d..c97bf9435a3216ce09e8f43d2b102e55e038bc8a 100644 (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);