diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2009-11-17 17:00:53 +1100 |
---|---|---|
committer | Jon Loeliger <jdl@jdl.com> | 2009-11-17 08:09:04 -0600 |
commit | 0ef21055ee414697aac7226c171d921214ef3981 (patch) | |
tree | 67c91457c43477a186922d97bbf46ebe225d0092 | |
parent | b31b271f91b867343fe21a20da4d24b03c1ef421 (diff) | |
download | dtc-0ef21055ee414697aac7226c171d921214ef3981.zip dtc-0ef21055ee414697aac7226c171d921214ef3981.tar.gz dtc-0ef21055ee414697aac7226c171d921214ef3981.tar.bz2 |
Assorted cleanups and extensions for ftdump
This patch makes a number of minor changes to the ftdump debugging
tool.
* There was an endian bug in one place, which this fixes.
* We now use const qualifiers in a number of places where we can
* ftdump can now be instructed to read from stdin by giving "-" as
the filename.
* The buffer into which the blob is read is increased from 16k to
64k, and is now dynamically allocated.
* ftdump now emits source in dts-v1 format
Since ftdump is little used these days, these fixes are arguably of
little use. On the other hand, I already did the work of making the
changes some time back, so I guess we might as well fold these small
fixes and improvements in.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r-- | ftdump.c | 47 |
1 files changed, 30 insertions, 17 deletions
@@ -4,15 +4,18 @@ #include <stdint.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <ctype.h> #include <fdt.h> #include <libfdt_env.h> +#define FTDUMP_BUF_SIZE 65536 + #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) #define PALIGN(p, a) ((void *)(ALIGN((unsigned long)(p), (a)))) -#define GET_CELL(p) (p += 4, *((uint32_t *)(p-4))) +#define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4))) static int is_printable_string(const void *data, int len) { @@ -38,10 +41,10 @@ static int is_printable_string(const void *data, int len) return 1; } -static void print_data(const void *data, int len) +static void print_data(const char *data, int len) { int i; - const uint8_t *s; + const char *p = data; /* no data, don't print */ if (len == 0) @@ -52,13 +55,13 @@ static void print_data(const void *data, int len) } else if ((len % 4) == 0) { printf(" = <"); for (i = 0; i < len; i += 4) - printf("%08x%s", *((const uint32_t *)data + i), + printf("0x%08x%s", fdt32_to_cpu(GET_CELL(p)), i < (len - 4) ? " " : ""); printf(">"); } else { printf(" = ["); - for (i = 0, s = data; i < len; i++) - printf("%02x%s", s[i], i < len - 1 ? " " : ""); + for (i = 0; i < len; i++) + printf("%02x%s", *p++, i < len - 1 ? " " : ""); printf("]"); } } @@ -71,13 +74,12 @@ static void dump_blob(void *blob) uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings); struct fdt_reserve_entry *p_rsvmap = (struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap); - char *p_struct = (char *)blob + off_dt; - char *p_strings = (char *)blob + off_str; + const char *p_struct = (const char *)blob + off_dt; + const char *p_strings = (const char *)blob + off_str; uint32_t version = fdt32_to_cpu(bph->version); uint32_t totalsize = fdt32_to_cpu(bph->totalsize); uint32_t tag; - char *p; - char *s, *t; + const char *p, *s, *t; int depth, sz, shift; int i; uint64_t addr, size; @@ -85,6 +87,7 @@ static void dump_blob(void *blob) depth = 0; shift = 4; + printf("/dts-v1/;\n"); printf("// magic:\t\t0x%x\n", fdt32_to_cpu(bph->magic)); printf("// totalsize:\t\t0x%x (%d)\n", totalsize, totalsize); printf("// off_dt_struct:\t0x%x\n", off_dt); @@ -167,7 +170,7 @@ static void dump_blob(void *blob) int main(int argc, char *argv[]) { FILE *fp; - char buf[16384]; /* 16k max */ + char *buf; int size; if (argc < 2) { @@ -175,15 +178,25 @@ int main(int argc, char *argv[]) return 5; } - fp = fopen(argv[1], "rb"); - if (fp == NULL) { - fprintf(stderr, "unable to open %s\n", argv[1]); + if (strcmp(argv[1], "-") == 0) { + fp = stdin; + } else { + fp = fopen(argv[1], "rb"); + if (fp == NULL) { + fprintf(stderr, "unable to open %s\n", argv[1]); + return 10; + } + } + + buf = malloc(FTDUMP_BUF_SIZE); + if (!buf) { + fprintf(stderr, "Couldn't allocate %d byte buffer\n", FTDUMP_BUF_SIZE); return 10; } - size = fread(buf, 1, sizeof(buf), fp); - if (size == sizeof(buf)) { /* too large */ - fprintf(stderr, "file too large\n"); + size = fread(buf, 1, FTDUMP_BUF_SIZE, fp); + if (size == FTDUMP_BUF_SIZE) { + fprintf(stderr, "file too large (maximum is %d bytes)\n", FTDUMP_BUF_SIZE); return 10; } |