Introduce fdtgrep for subsetting and hashing FDTs
Subsystems (2)
v3 cover Introduce fdtgrep for subsetting and hashing FDTs
This series adds two new functions, fdt_first_region() and fdt_next_regions() which map FDT parts such as nodes and properties to their regions in the FDT binary. The function is then used to implement a grep utility for FDTs.
The core code is quite simple and small, but it grew a little due to the need to make it iterative (first/next/next). Also this series adds tests and a grep utility, so quite a bit of code is built on it.
The use for this feature is twofold. Firstly it provides a convenient way of performing a structure-aware grep of the tree. For example it is possible to grep for a node and get all the properties associated with that node. Trees can be subsetted easily, by specifying the nodes that are required, and then writing out the regions returned by this function. This is useful for small resource-constrained systems, such as boot loaders, which want to use an FDT but do not need to know about all of it. The full FDT can be grepped to pull out the few things that are needed - this can be an automatic part of the build system and does not require the FDT source code.
This first use makes it easy to implement an FDT grep. Options are provided to search for matching nodes (by name or compatible string), properties and also for any of the above. It is possible to search for non-matches also (useful for excluding a particular property from the FDT, for example). The output is like fdtdump, but only with the regions selected by the grep. Options are also provided to print the string table, memory reservation table, etc. The fdtgrep utility can output valid source, which can be used by dtc, but it can also directly output a new .dtb binary.
Secondly it makes it easy to hash parts of the tree and detect changes. The intent is to get a list of regions which will be invariant provided those parts are invariant. For example, if you request a list of regions for all nodes but exclude the property "data", then you will get the same region contents regardless of any change to "data" properties. An assumption is made here that the tree ordering remains the same.
This second use is the subject of a recent series sent to the U-Boot mailing list, to enhance FIT images to support verified boot. Briefly, this works by signing configurations (consisting of particular kernel and FDT combinations) so that the boot loader can verify that these combinations are valid and permitted. Since a FIT image is in fact an FDT, we need to be able to hash particular regions of the FDT for the signing and verification process. This is done by using the region functions to select the data that needs to be hashed for a particular configuration.
The fdtgrep utility could be used to replace all of the functions of fdtdump. However fdtdump is intended as a separate, simple way of dumping the tree (for verifying against dtc output for example). So fdtdump remains a separate program and this series leaves it alone.
Note: a somewhat unfortunately feature of this implementation is that a state structure needs to be kept around between calls of fdt_next_region(). This is declared in libfdt.h but really should be opaque.
Changes in v3:
- Add a feature to include all subnodes
- Adjust help and command line processing to follow new approach
- Rename -V to -I to avoid using -V for a different purpose to other tools
- Rename -s to -e since it only 'enters' the node and does not include it all
- Add -s option to include all subnodes
- Add -f option to display offset; make -a display an absolute file address
- Add documentation on fdtgrep
Changes in v2:
- Move region code to separate fdt_region.c file
- Fix info->count <= info->max_regions in fdt_add_region() merge case
- Add new FDT_ERR_TOODEEP error type and use it
- Change returned error from BADLAYOUT to BADSTRUCTURE
- Return FDT_ERR_BADLAYOUT error if strings block is before structure block
- Add note that changes in node/property order can cause false hash misses
- Add more comments about the -1 return value from h_include
- Drop FDT_IS_COMPAT and pass node offset to h_include function
- Drop stale comment about names / wildcards
- Move to a model with fdt_first_region()/fdt_next_region()
- Add long comment explaining theory of operation
- Add local fdt_find_regions() function since libfdt no longer has it
Simon Glass (3):
libfdt: Add function to find regions in an FDT
Add documentation for fdtget/put
Add fdtgrep to grep and subset FDTs
.gitignore | 1 + Documentation/manual.txt | 125 +++++++ Makefile | 4 + Makefile.utils | 7 + fdtgrep.c | 873 +++++++++++++++++++++++++++++++++++++++++++++++ libfdt/Makefile.libfdt | 3 +- libfdt/fdt_region.c | 457 +++++++++++++++++++++++++ libfdt/libfdt.h | 217 +++++++++++- tests/.gitignore | 1 + tests/Makefile.tests | 3 +- tests/grep.dts | 23 ++ tests/region_tree.c | 352 +++++++++++++++++++ tests/run_tests.sh | 361 +++++++++++++++++++- tests/tests.sh | 1 + 14 files changed, 2424 insertions(+), 4 deletions(-) create mode 100644 fdtgrep.c create mode 100644 libfdt/fdt_region.c create mode 100644 tests/grep.dts create mode 100644 tests/region_tree.c
Signature · 6 lines
1.9.1.423.g4596e3a -- To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 1395500836-12735-1-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
PATCH RESEND v3 1/3 libfdt: Add function to find regions in an FDT
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
---
Changes in v3:
- Add a feature to include all subnodes
Changes in v2:
- Move region code to separate fdt_region.c file
- Fix info->count <= info->max_regions in fdt_add_region() merge case
- Add new FDT_ERR_TOODEEP error type and use it
- Change returned error from BADLAYOUT to BADSTRUCTURE
- Return FDT_ERR_BADLAYOUT error if strings block is before structure block
- Add note that changes in node/property order can cause false hash misses
- Add more comments about the -1 return value from h_include
- Drop FDT_IS_COMPAT and pass node offset to h_include function
- Drop stale comment about names / wildcards
- Move to a model with fdt_first_region()/fdt_next_region()
- Add long comment explaining theory of operation
libfdt/Makefile.libfdt | 3 +- libfdt/fdt_region.c | 457 +++++++++++++++++++++++++++++++++++++++++++++++++ libfdt/libfdt.h | 217 ++++++++++++++++++++++- tests/.gitignore | 1 + tests/Makefile.tests | 3 +- tests/region_tree.c | 352 +++++++++++++++++++++++++++++++++++++ tests/run_tests.sh | 5 + 7 files changed, 1035 insertions(+), 3 deletions(-) create mode 100644 libfdt/fdt_region.c create mode 100644 tests/region_tree.c
libfdt/Makefile.libfdt +2 -1
@@ -6,5 +6,6 @@ LIBFDT_soname = libfdt.$(SHAREDLIB_EXT).1 LIBFDT_INCLUDES = fdt.h libfdt.h libfdt_env.h LIBFDT_VERSION = version.lds -LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_rw.c fdt_strerror.c fdt_empty_tree.c +LIBFDT_SRCS = fdt.c fdt_ro.c fdt_wip.c fdt_sw.c fdt_region.c +LIBFDT_SRCS += fdt_rw.c fdt_strerror.c fdt_empty_tree.c LIBFDT_OBJS = $(LIBFDT_SRCS:%.c=%.o)
libfdt/fdt_region.c new file +457 -0
457 lines not on this page · raw message
libfdt/libfdt.h +216 -1
227 lines not on this page · raw message
tests/.gitignore +1 -0
@@ -41,6 +41,7 @@ tmp.* /phandle_format /propname_escapes /references +/region_tree /root_node /rw_tree1 /set_name
tests/Makefile.tests +2 -1
@@ -21,7 +21,8 @@ LIB_TESTS_L = get_mem_rsv \ add_subnode_with_nops path_offset_aliases \ utilfdt_test \ integer-expressions \ - subnode_iterate + subnode_iterate \ + region_tree LIB_TESTS = $(LIB_TESTS_L:%=$(TESTS_PREFIX)%) LIBTREE_TESTS_L = truncated_property
tests/region_tree.c new file +352 -0
352 lines not on this page · raw message
tests/run_tests.sh +5 -0
@@ -249,6 +249,11 @@ libfdt_tests () { # Specific bug tests run_test add_subnode_with_nops + + # Tests for fdt_find_regions() + for flags in $(seq 0 15); do + run_test region_tree ${flags} + done } dtc_tests () {
Signature · 6 lines
1.9.1.423.g4596e3a -- To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 1395500836-12735-2-git-send-email-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org
Re: [RESEND PATCH v3 1/3] libfdt: Add function to find regions in an FDT
Hi,
Quoted 9 lines · Simon Glass
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
Signed-off-by Simon Glass <[email protected]>
Have I sent this to the right place? Any comments?
Regards,
Simon
--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ0ehgSd1WprpMd1ZY=LaZaKkBG3MaNPV5ja27Q2p12WvA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [RESEND PATCH v3 1/3] libfdt: Add function to find regions in an FDT
Quoted 6 lines · Simon Glass
Hi,
Quoted 3 lines · Simon Glass
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
Quoted 8 lines
Quoted 6 lines
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
Signed-off-by Simon Glass <[email protected]>Have I sent this to the right place? Any comments?
Sorry. I know you've resent this several times, and I've been procrastinating about it since forever.
Basically, I'm just not convinced. For all your efforts to explain the rationale, it just seems like a really ad-hoc set of flags and conditions that doesn't seem to form a coherent whole.
From the lack of other responses, I'm assuming there's not really anyone else who sees it as a compelling feature either.
So, sorry. For the forseeable future, you'll need to leave this implemented in your own code, using but outside of libfdt itself.
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [RESEND PATCH v3 1/3] libfdt: Add function to find regions in an FDT
Hi David,
Quoted 14 lines · David Gibson
Quoted 13 lines · Simon Glass
Hi,
Quoted 9 lines · Simon Glass
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
Signed-off-by Simon Glass <[email protected]>
Quoted 8 lines
Quoted 1 line
Have I sent this to the right place? Any comments?
Sorry. I know you've resent this several times, and I've been procrastinating about it since forever.
Basically, I'm just not convinced. For all your efforts to explain the rationale, it just seems like a really ad-hoc set of flags and conditions that doesn't seem to form a coherent whole.
Are you referring to fdtgrep or the new library function? I'd be happy enough getting the library function to start with in if there isn't much need for the grep utility.
Quoted 3 lines
From the lack of other responses, I'm assuming there's not really anyone else who sees it as a compelling feature either.
Is there normally a lot of mailing list traffic for new libfdt features? This is used for verified boot in U-Boot for example.
Quoted 3 lines
So, sorry. For the forseeable future, you'll need to leave this implemented in your own code, using but outside of libfdt itself.
Regards,
Simon
--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2OHco3GjH_rPC+7Fu36QqBK2_sWvcaF1WOWFpBNkZTcQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Re: [RESEND PATCH v3 1/3] libfdt: Add function to find regions in an FDT
Quoted 22 lines · Simon Glass
Hi David,
Quoted 19 lines · David Gibson
Quoted 6 lines · Simon Glass
Hi,
Quoted 3 lines · Simon Glass
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
Quoted 8 lines
Quoted 6 lines
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
Signed-off-by Simon Glass <[email protected]>Have I sent this to the right place? Any comments?
Sorry. I know you've resent this several times, and I've been procrastinating about it since forever.
Quoted 7 lines
Quoted 3 lines
Basically, I'm just not convinced. For all your efforts to explain the rationale, it just seems like a really ad-hoc set of flags and conditions that doesn't seem to form a coherent whole.
Are you referring to fdtgrep or the new library function? I'd be happy enough getting the library function to start with in if there isn't much need for the grep utility.
The library function primarily.
Quoted 5 lines
Quoted 2 lines
From the lack of other responses, I'm assuming there's not really anyone else who sees it as a compelling feature either.
Is there normally a lot of mailing list traffic for new libfdt features? This is used for verified boot in U-Boot for example.
Hm, that's true I guess.
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [RESEND PATCH v3 1/3] libfdt: Add function to find regions in an FDT
Hi David,
Quoted 34 lines · David Gibson
Quoted 22 lines · Simon Glass
Hi David,
Quoted 19 lines · David Gibson
Quoted 6 lines · Simon Glass
Hi,
Quoted 3 lines · Simon Glass
Given a set of nodes and properties, find the regions of the device tree which describe those parts.
Quoted 8 lines
Quoted 6 lines
A test is provided which builds a tree while tracking where the regions should be, then calls fdt_first/next_region() to make sure that it agrees.
Further tests will come as part of fdtgrep.
Signed-off-by Simon Glass <[email protected]>Have I sent this to the right place? Any comments?
Sorry. I know you've resent this several times, and I've been procrastinating about it since forever.
Quoted 7 lines
Quoted 3 lines
Basically, I'm just not convinced. For all your efforts to explain the rationale, it just seems like a really ad-hoc set of flags and conditions that doesn't seem to form a coherent whole.
Are you referring to fdtgrep or the new library function? I'd be happy enough getting the library function to start with in if there isn't much need for the grep utility.
The library function primarily.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
From the lack of other responses, I'm assuming there's not really anyone else who sees it as a compelling feature either.
Is there normally a lot of mailing list traffic for new libfdt features? This is used for verified boot in U-Boot for example.
Hm, that's true I guess.
I'd like to dredge this up again.
I'm currently working on adding device tree support in U-Boot SPL (the tiny loader that loads U-Boot). U-Boot currently uses device tree for normal operation, but since SPL can run in very small RAM sizes there is often not enough space for a full (~40KB) device tree. In fact often all that is needed is the UART and perhaps SPI/I2C. This is <1KB.
There seem to be a several options here:
1. Create a separate device tree file for each board, just for SPL, with just what is needed to boot SPL
2. Use #ifdef and the preprocessor to remove what is not needed, and run dtc twice with different #defines
3. Use fdtgrep to chop down the device tree file by removing unwanted nodes
I think that option 3 is the best. It avoids copying source code, or littering it with #ifdef.
If necessary I suppose this tool could just live in U-Boot but that seems a shame. I understand that fdtgrep perhaps has too many options for your liking. Is there some subset that you think would be worthy of including as a new device-tree-compiler tool?
If so, then I would rather create fdtgrep (or fdtprune or whatever) there than add a special tool to U-Boot.
Please let me know what you think.
Regards,
Simon
PATCH RESEND v3 2/3 Add documentation for fdtget/put
In reply to a message not in the archiveThe other tools have documentation so it seems reasonable to add something here.
---
Changes in v3: None
Changes in v2: None
Documentation/manual.txt | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
Documentation/manual.txt +38 -0
@@ -658,3 +658,41 @@ The fdtdump program prints a readable version of a flat device tree file. The syntax of the fdtdump command line is: fdtdump <DTB-file-name> + + +3) fdtget -- Get individual properties and lists from a Device Tree + +Ths fdtget program allows you to extract properties from nodes, and also +list nodes and properties. + +The syntax of the fdtget commandline is described in the help (fdtget -h). +Common uses are: + + fdtget -ts <DTB-file-name> /node compatible + - Read the compatible string of a node. The -ts is optional since + fdtget will normally guess the type correctly. + + fdtget -bx <DTB-file-name> /node bytes + - Read the bytes in a property and output in hex. The 'b' prefix + forces byte output, since otherwise fdtget will output in 32-bit + or 16-bit words if the property length is so-aligned. + + fdtget -p <DTB-file-name> /node + - List the properties in a node + + fdtget -l <DTB-file-name> / + - List the subnodes for a node (here, the root node) + +You can use -d to provide a default value for when the property does not +exist. + + +4) fdtput -- Write individual properties to a Device Tree + +Ths fdtput program allows you to write properties to nodes. + +The syntax of the fdtput commandline is described in the help (fdtput -h) and +the type options are the same as fdtget. You can use -c to create a node if +it does not exist already, and -p to create all required nodes (like +'mkdir -p' does with directories). Unfortunately fdtput does not support +deleting nodes or properties.
Signature · 6 lines
1.9.1.423.g4596e3a -- To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH RESEND v3 3/3 Add fdtgrep to grep and subset FDTs
In reply to a message not in the archivefdtgrep operates like a grep for device trees, working on binary .dtb files.
fdtgrep can find nodes by name or compatible string. It can find properties by name. Using a list of of nodes/properties/compatible strings to match, fdtgrep creates either .dts text output containing just those matches, or .dtb output. In the .dtb case, the output is a valid FDT which only includes the selected nodes/properties. This is useful in resource-constrained systems where a full FDT is too large to fit in available memory (e.g. U-Boot SPL).
fdtgrep also supports producing raw binary output, without the normal FDT headers. This can be useful for hashing part of an FDT and making sure that that part does not change, even if other parts do. This makes it possible to detect an important change, while ignoring a change that is of no interest.
At its simplest fdtgrep can be used as follows;
fdtgrep /holiday tests/grep.dtb
and it produces just that node (with a skeleton to hold it):
/ {
holiday {
compatible = "ixtapa", "mexico";
weather = "sunny";
status = "okay";
};
};The binary form of this is:
fdtgrep -n /holiday -O dtb tests/grep.dtb
which produces a valid DTB file with just the above nodes.
Various options are provided to search for properties, to ensure that nodes/properties are not present, and to search for nodes by compatible string.
---
Changes in v3:
- Adjust help and command line processing to follow new approach
- Rename -V to -I to avoid using -V for a different purpose to other tools
- Rename -s to -e since it only 'enters' the node and does not include it all
- Add -s option to include all subnodes
- Add -f option to display offset; make -a display an absolute file address
- Add documentation on fdtgrep
Changes in v2:
- Add local fdt_find_regions() function since libfdt no longer has it
.gitignore | 1 + Documentation/manual.txt | 87 +++++ Makefile | 4 + Makefile.utils | 7 + fdtgrep.c | 873 +++++++++++++++++++++++++++++++++++++++++++++++ tests/grep.dts | 23 ++ tests/run_tests.sh | 356 ++++++++++++++++++- tests/tests.sh | 1 + 8 files changed, 1351 insertions(+), 1 deletion(-) create mode 100644 fdtgrep.c create mode 100644 tests/grep.dts
.gitignore +1 -0
@@ -12,6 +12,7 @@ lex.yy.c /convert-dtsv0 /version_gen.h /fdtget +/fdtgrep /fdtput /patches /.pc
Documentation/manual.txt +87 -0
90 lines not on this page · raw message
Makefile +4 -0
@@ -112,6 +112,7 @@ BIN += dtc BIN += fdtdump BIN += fdtget BIN += fdtput +BIN += fdtgrep SCRIPTS = dtdiff
@@ -124,6 +125,7 @@ ifneq ($(DEPTARGETS),) -include $(FDTDUMP_OBJS:%.o=%.d) -include $(FDTGET_OBJS:%.o=%.d) -include $(FDTPUT_OBJS:%.o=%.d) +-include $(FDTGREP_OBJS:%.o=%.d) endif
@@ -188,6 +190,7 @@ fdtget: $(FDTGET_OBJS) $(LIBFDT_archive) fdtput: $(FDTPUT_OBJS) $(LIBFDT_archive) +fdtgrep: $(FDTGREP_OBJS) $(LIBFDT_archive) # # Testsuite rules
@@ -198,6 +201,7 @@ TESTS_BIN += dtc TESTS_BIN += convert-dtsv0 TESTS_BIN += fdtput TESTS_BIN += fdtget +TESTS_BIN += fdtgrep include tests/Makefile.tests
Makefile.utils +7 -0
@@ -22,3 +22,10 @@ FDTPUT_SRCS = \ util.c FDTPUT_OBJS = $(FDTPUT_SRCS:%.c=%.o) + + +FDTGREP_SRCS = \ + fdtgrep.c \ + util.c + +FDTGREP_OBJS = $(FDTGREP_SRCS:%.c=%.o)
fdtgrep.c new file +873 -0
873 lines not on this page · raw message
tests/grep.dts new file +23 -0
@@ -0,0 +1,23 @@ +/dts-v1/; +/memreserve/ 1 2; +/ { + model = "MyBoardName"; + compatible = "MyBoardName", "MyBoardFamilyName"; + #address-cells = <2>; + #size-cells = <2>; + chosen { + bootargs = "root=/dev/sda2"; + linux,platform = <0x600>; + }; + holiday { + compatible = "ixtapa", "mexico"; + weather = "sunny"; + status = "okay"; + flight@1 { + airline = "alaska"; + }; + flight@2 { + airline = "lan"; + }; + }; +};
tests/run_tests.sh +355 -1
374 lines not on this page · raw message
tests/tests.sh +1 -0
@@ -21,6 +21,7 @@ FAIL_IF_SIGNAL () { DTC=../dtc DTGET=../fdtget DTPUT=../fdtput +DTGREP=../fdtgrep verbose_run () { if [ -z "$QUIET_TEST" ]; then
Signature · 6 lines
1.9.1.423.g4596e3a -- To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html