binman: Obtain the list of device trees from the config

We always have a device tree for U-Boot proper. But we may also have one
for SPL and TPL. Add a new Entry method to find out what DTs an entry
has, and use that list when updating DTs.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/binman/bsection.py b/tools/binman/bsection.py
index 4f5c33f..44adb82 100644
--- a/tools/binman/bsection.py
+++ b/tools/binman/bsection.py
@@ -8,6 +8,7 @@
 from __future__ import print_function
 
 from collections import OrderedDict
+from sets import Set
 import sys
 
 import fdt_util
@@ -92,6 +93,13 @@
             entry.SetPrefix(self._name_prefix)
             self._entries[node.name] = entry
 
+    def GetFdtSet(self):
+        """Get the set of device tree files used by this image"""
+        fdt_set = Set()
+        for entry in self._entries.values():
+            fdt_set.update(entry.GetFdtSet())
+        return fdt_set
+
     def SetOffset(self, offset):
         self._offset = offset
 
diff --git a/tools/binman/control.py b/tools/binman/control.py
index fd8b779..49d49a0 100644
--- a/tools/binman/control.py
+++ b/tools/binman/control.py
@@ -137,7 +137,7 @@
                 if skip:
                     print 'Skipping images: %s\n' % ', '.join(skip)
 
-            state.Prepare(dtb)
+            state.Prepare(images, dtb)
 
             # Prepare the device tree by making sure that any missing
             # properties are added (e.g. 'pos' and 'size'). The values of these
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index 4b87156..e5f55774 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -18,6 +18,7 @@
     have_importlib = False
 
 import os
+from sets import Set
 import sys
 
 import fdt_util
@@ -164,6 +165,20 @@
     def GetDefaultFilename(self):
         return None
 
+    def GetFdtSet(self):
+        """Get the set of device trees used by this entry
+
+        Returns:
+            Set containing the filename from this entry, if it is a .dtb, else
+            an empty set
+        """
+        fname = self.GetDefaultFilename()
+        # It would be better to use isinstance(self, Entry_blob_dtb) here but
+        # we cannot access Entry_blob_dtb
+        if fname and fname.endswith('.dtb'):
+            return Set([fname])
+        return Set()
+
     def AddMissingProperties(self):
         """Add new properties to the device tree as needed for this entry"""
         for prop in ['offset', 'size', 'image-pos']:
diff --git a/tools/binman/image.py b/tools/binman/image.py
index 68126bc..1fb5eb6 100644
--- a/tools/binman/image.py
+++ b/tools/binman/image.py
@@ -54,6 +54,10 @@
             self._filename = filename
         self._section = bsection.Section('main-section', self._node)
 
+    def GetFdtSet(self):
+        """Get the set of device tree files used by this image"""
+        return self._section.GetFdtSet()
+
     def AddMissingProperties(self):
         """Add properties that are not present in the device tree
 
diff --git a/tools/binman/state.py b/tools/binman/state.py
index 5f25b90..600eb86 100644
--- a/tools/binman/state.py
+++ b/tools/binman/state.py
@@ -18,6 +18,10 @@
 # Arguments passed to binman to provide arguments to entries
 entry_args = {}
 
+# True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in
+# ftest.py)
+use_fake_dtb = True
+
 # Set of all device tree files references by images
 fdt_set = Set()
 
@@ -85,13 +89,14 @@
     """
     return entry_args.get(name)
 
-def Prepare(dtb):
+def Prepare(images, dtb):
     """Get device tree files ready for use
 
     This sets up a set of device tree files that can be retrieved by GetFdts().
     At present there is only one, that for U-Boot proper.
 
     Args:
+        images: List of images being used
         dtb: Main dtb
     """
     global fdt_set, fdt_subset, fdt_files, main_dtb
@@ -107,8 +112,19 @@
     main_dtb = dtb
     fdt_files.clear()
     fdt_files['u-boot.dtb'] = dtb
-    fdt_set = Set()
     fdt_subset = Set()
+    if not use_fake_dtb:
+        for image in images.values():
+            fdt_subset.update(image.GetFdtSet())
+        fdt_subset.discard('u-boot.dtb')
+        for other_fname in fdt_subset:
+            infile = tools.GetInputFilename(other_fname)
+            other_fname_dtb = fdt_util.EnsureCompiled(infile)
+            out_fname = tools.GetOutputFilename('%s.out' %
+                    os.path.split(other_fname)[1])
+            tools.WriteFile(out_fname, tools.ReadFile(other_fname_dtb))
+            other_dtb = fdt.FdtScan(out_fname)
+            fdt_files[other_fname] = other_dtb
 
 def GetFdts():
     """Yield all device tree files being used by binman