summaryrefslogtreecommitdiffstats
path: root/fdisks/fdisk.c
diff options
context:
space:
mode:
authorKarel Zak2013-09-03 17:36:35 +0200
committerKarel Zak2013-09-16 16:47:10 +0200
commite916600f54be606f2253a79ef4c711b1078110b5 (patch)
treef5fdc25d05e3ea0116f6bcb461690ad64670d9a3 /fdisks/fdisk.c
parentlibfdisk: (gpt) support for disklabel dump (diff)
downloadkernel-qcow2-util-linux-e916600f54be606f2253a79ef4c711b1078110b5.tar.gz
kernel-qcow2-util-linux-e916600f54be606f2253a79ef4c711b1078110b5.tar.xz
kernel-qcow2-util-linux-e916600f54be606f2253a79ef4c711b1078110b5.zip
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>
Diffstat (limited to 'fdisks/fdisk.c')
-rw-r--r--fdisks/fdisk.c90
1 files changed, 71 insertions, 19 deletions
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)