Skip to contentSkip to conversation

Allow dtc to decompile device tre blobs in hexdump format

PATCH David Gibson 2 participants 4 messages Apr 4, 2010 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

When debugging handoffs between different boot stages which use flattened device trees to communicate, it's often useful to dump the device tree blob being passed and use dtc to decompile it to something readable. However, with some debugging tools it's possible to perform a hex memory dump of the device tree, but it's awkward or impossible to directly dump the memory as a binary blob.

Obviously, it's quite straightforward to write a script or program to convert the hex dump back to binary, but I'm not aware of a standard tool to do so.

This patch, therefore, adds a "hex" input format to dtc which allows it to directly (or almost so) parse and decompile a device tree blob supplied as a hex dump. The input dtc expects in this mode must have no addresses or other extraneous text which could contain valid hex digits, however it will silently ignore any whitespace, punctuation, or other non-hex-digit formatting in the dump. The dump must also either be either byte-by-byte, or display each hex number as big-endian (so "od -t x1" will produce suitable output on any system, but "od -t x2" will produce suitable output only when run on a big-endian system).

Thus, if addresses and any header/footer are removed (usually easy to do in an editor), dtc will accept quite a wide range of likely hex dump formats.

dtc.c +4 -1
Index: dtc/dtc.c
===================================================================
@@ -62,6 +62,7 @@ static void  __attribute__ ((noreturn)) 
 	fprintf(stderr, "\t\tInput formats are:\n");
 	fprintf(stderr, "\t\t\tdts - device tree source text\n");
 	fprintf(stderr, "\t\t\tdtb - device tree blob\n");
+	fprintf(stderr, "\t\t\thex - device tree blob hexdump\n");
 	fprintf(stderr, "\t\t\tfs - /proc/device-tree style directory\n");
 	fprintf(stderr, "\t-o <output file>\n");
 	fprintf(stderr, "\t-O <output format>\n");
@@ -186,8 +187,10 @@ int main(int argc, char *argv[])
 		bi = dt_from_source(arg);
 	else if (streq(inform, "fs"))
 		bi = dt_from_fs(arg);
-	else if(streq(inform, "dtb"))
+	else if (streq(inform, "dtb"))
 		bi = dt_from_blob(arg);
+	else if (streq(inform, "hex"))
+		bi = dt_from_hex(arg);
 	else
 		die("Unknown input format \"%s\"\n", inform);
 
dtc.h +1 -0
Index: dtc/dtc.h
===================================================================
@@ -231,6 +231,7 @@ void dt_to_blob(FILE *f, struct boot_inf
 void dt_to_asm(FILE *f, struct boot_info *bi, int version);
 
 struct boot_info *dt_from_blob(const char *fname);
+struct boot_info *dt_from_hex(const char *fname);
 
 /* Tree source */
 
flattree.c +126 -57

220 lines not on this page · raw message

tests/Makefile.tests +1 -1
Index: dtc/tests/Makefile.tests
===================================================================
@@ -33,7 +33,7 @@ TESTS_TARGETS = $(TESTS) $(TESTS_TREES)
 TESTS_DEPFILES = $(TESTS:%=%.d) \
 	$(addprefix $(TESTS_PREFIX),testutils.d trees.d dumptrees.d)
 
-TESTS_CLEANFILES_L =  *.output vglog.* vgcore.* *.dtb *.test.dts *.dtsv1 tmp.*
+TESTS_CLEANFILES_L =  *.output vglog.* vgcore.* *.dtb *.test.dts *.dtsv1 tmp.* *.hex
 TESTS_CLEANFILES_L += dumptrees
 TESTS_CLEANFILES = $(TESTS) $(TESTS_CLEANFILES_L:%=$(TESTS_PREFIX)%)
 
tests/run_tests.sh +18 -0
Index: dtc/tests/run_tests.sh
===================================================================
@@ -6,6 +6,16 @@ if [ -z "$CC" ]; then
     CC=gcc
 fi
 
+hexdump_canon() {
+    hexdump -C $1 | cut -c9-61
+}
+
+hexdump_canon_caps() {
+    hexdump_canon $1 | tr 'a-z' 'A-Z'
+}
+
+hexdumpers="hexdump_canon hexdump_canon_caps"
+
 export QUIET_TEST=1
 
 export VALGRIND=
@@ -282,6 +292,14 @@ dtc_tests () {
 	run_dtc_test -I dtb -O dts -o odts_$tree.test.dts $tree
 	run_dtc_test -I dts -O dtb -o odts_$tree.test.dtb odts_$tree.test.dts
 	run_test dtbs_equal_ordered $tree odts_$tree.test.dtb
+
+	for h in $hexdumpers; do
+	    hextree=$tree.$h.test.hex
+	    $h $tree > $hextree
+	    run_dtc_test -I hex -O dts -o odts_$hextree.test.dts $hextree
+	    run_dtc_test -I dts -O dtb -o odts_$hextree.test.dtb odts_$hextree.test.dts
+	    run_test dtbs_equal_ordered $tree odts_$hextree.test.dtb
+    	done
     done
 
     # Check version conversions
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#1 lore raw
Quoted 22 lines · David Gibson

When debugging handoffs between different boot stages which use flattened device trees to communicate, it's often useful to dump the device tree blob being passed and use dtc to decompile it to something readable.  However, with some debugging tools it's possible to perform a hex memory dump of the device tree, but it's awkward or impossible to directly dump the memory as a binary blob.

Obviously, it's quite straightforward to write a script or program to convert the hex dump back to binary, but I'm not aware of a standard tool to do so.

This patch, therefore, adds a "hex" input format to dtc which allows it to directly (or almost so) parse and decompile a device tree blob supplied as a hex dump.  The input dtc expects in this mode must have no addresses or other extraneous text which could contain valid hex digits, however it will silently ignore any whitespace, punctuation, or other non-hex-digit formatting in the dump.  The dump must also either be either byte-by-byte, or display each hex number as big-endian (so "od -t x1" will produce suitable output on any system, but "od -t x2" will produce suitable output only when run on a big-endian system).

Quoted 3 lines

Thus, if addresses and any header/footer are removed (usually easy to do in an editor), dtc will accept quite a wide range of likely hex dump formats.

Does it really make sense to build marshaling tools into dtc itself? I think it would make more sense to keep transformation tools independent. ie. what if I have srecord format? or intel hex? A trivial python or perl script would do the job for raw hex. The ascii2binary tool should also work. Also, objcopy handles srecord/ihex conversion beautifully.

I don't think it makes sense to be teaching dtc different representations of the same dtb format.

g.

#3 lore raw
In reply to a message not in the archive
Quoted 24 lines · Grant Likely
Quoted 22 lines · David Gibson

When debugging handoffs between different boot stages which use flattened device trees to communicate, it's often useful to dump the device tree blob being passed and use dtc to decompile it to something readable.  However, with some debugging tools it's possible to perform a hex memory dump of the device tree, but it's awkward or impossible to directly dump the memory as a binary blob.

Obviously, it's quite straightforward to write a script or program to convert the hex dump back to binary, but I'm not aware of a standard tool to do so.

This patch, therefore, adds a "hex" input format to dtc which allows it to directly (or almost so) parse and decompile a device tree blob supplied as a hex dump.  The input dtc expects in this mode must have no addresses or other extraneous text which could contain valid hex digits, however it will silently ignore any whitespace, punctuation, or other non-hex-digit formatting in the dump.  The dump must also either be either byte-by-byte, or display each hex number as big-endian (so "od -t x1" will produce suitable output on any system, but "od -t x2" will produce suitable output only when run on a big-endian system).

Quoted 5 lines
Quoted 3 lines

Thus, if addresses and any header/footer are removed (usually easy to do in an editor), dtc will accept quite a wide range of likely hex dump formats.

Does it really make sense to build marshaling tools into dtc itself?

Well.. is it essential that dtc do this? Clearly not. Is it a sufficient convenience that it's worth it? Maybe.

Quoted 2 lines

I think it would make more sense to keep transformation tools independent. ie. what if I have srecord format? or intel hex?

If they're a) sufficiently widely used, b) not to hard to implement and c) there aren't widely used existing conversion tools then I would have no problem with adding support for such formats to.

Quoted 2 lines

A
trivial python or perl script would do the job for raw hex.

I've written that trivial python or perl script too darn many times, and I'm sick of it. I can't be the only one.

Quoted 3 lines

The
ascii2binary tool should also work. Also, objcopy handles srecord/ihex conversion beautifully.

Ah, I wasn't previously aware of ascii2binary. If it's flexible enough, that might well undermine the justification for this patch.

Quoted 2 lines

I don't think it makes sense to be teaching dtc different representations of the same dtb format.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#4 lore raw
Quoted 9 lines · David Gibson
Quoted 8 lines · Grant Likely
Quoted 6 lines · David Gibson

When debugging handoffs between different boot stages which use flattened device trees to communicate, it's often useful to dump the device tree blob being passed and use dtc to decompile it to something readable.  However, with some debugging tools it's possible to perform a hex memory dump of the device tree, but it's awkward or impossible to directly dump the memory as a binary blob.

[...]

Quoted 13 lines
Quoted 2 lines

Does it really make sense to build marshaling tools into dtc itself?

Well.. is it essential that dtc do this?  Clearly not.  Is it a sufficient convenience that it's worth it?  Maybe.

Quoted 2 lines

I think it would make more sense to keep transformation tools independent.  ie. what if I have srecord format?  or intel hex?

If they're a) sufficiently widely used, b) not to hard to implement and c) there aren't widely used existing conversion tools then I would have no problem with adding support for such formats to.

Quoted 5 lines
Quoted 2 lines

A
trivial python or perl script would do the job for raw hex.

I've written that trivial python or perl script too darn many times, and I'm sick of it.  I can't be the only one.

Oh, I agree. I don't like reimplementing tools over and over again either. What I'm saying is rather than increasing the complexity of the core dtc codebase, make it a separate script and store it inside the dtc repo so it is available to all who need it.

Quoted 6 lines
Quoted 3 lines

 The
ascii2binary tool should also work.  Also, objcopy handles srecord/ihex conversion beautifully.

Ah, I wasn't previously aware of ascii2binary.  If it's flexible enough, that might well undermine the justification for this patch.

:-)

g.