binman: Move state logic into the state module

Rather than reaching into this module from control, move the code that
needs this info into state.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/binman/control.py b/tools/binman/control.py
index 00dcb8a..fd8b779 100644
--- a/tools/binman/control.py
+++ b/tools/binman/control.py
@@ -121,8 +121,6 @@
                     outfd.write(infd.read())
             dtb = fdt.FdtScan(fname)
 
-            # Note the file so that GetFdt() can find it
-            state.fdt_files['u-boot.dtb'] = dtb
             node = _FindBinmanNode(dtb)
             if not node:
                 raise ValueError("Device tree '%s' does not have a 'binman' "
@@ -139,6 +137,8 @@
                 if skip:
                     print 'Skipping images: %s\n' % ', '.join(skip)
 
+            state.Prepare(dtb)
+
             # Prepare the device tree by making sure that any missing
             # properties are added (e.g. 'pos' and 'size'). The values of these
             # may not be correct yet, but we add placeholders so that the
@@ -151,9 +151,10 @@
                     image.AddMissingProperties()
                 image.ProcessFdt(dtb)
 
-            dtb.Sync(auto_resize=True)
-            dtb.Pack()
-            dtb.Flush()
+            for dtb_item in state.GetFdts():
+                dtb_item.Sync(auto_resize=True)
+                dtb_item.Pack()
+                dtb_item.Flush()
 
             for image in images.values():
                 # Perform all steps for this image, including checking and
@@ -168,14 +169,18 @@
                 image.SetImagePos()
                 if options.update_fdt:
                     image.SetCalculatedProperties()
-                    dtb.Sync()
+                    for dtb_item in state.GetFdts():
+                        dtb_item.Sync()
                 image.ProcessEntryContents()
                 image.WriteSymbols()
                 image.BuildImage()
                 if options.map:
                     image.WriteMap()
-            with open(fname, 'wb') as outfd:
-                outfd.write(dtb.GetContents())
+
+            # Write the updated FDTs to our output files
+            for dtb_item in state.GetFdts():
+                tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
+
         finally:
             tools.FinaliseOutputDir()
     finally:
diff --git a/tools/binman/state.py b/tools/binman/state.py
index 6bc24dd..9583b3f 100644
--- a/tools/binman/state.py
+++ b/tools/binman/state.py
@@ -18,6 +18,15 @@
 # Arguments passed to binman to provide arguments to entries
 entry_args = {}
 
+# Set of all device tree files references by images
+fdt_set = Set()
+
+# Same as above, but excluding the main one
+fdt_subset = Set()
+
+# The DTB which contains the full image information
+main_dtb = None
+
 def GetFdt(fname):
     """Get the Fdt object for a particular device-tree filename
 
@@ -75,3 +84,37 @@
         String value of argument
     """
     return entry_args.get(name)
+
+def Prepare(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:
+        dtb: Main dtb
+    """
+    global fdt_set, fdt_subset, fdt_files, main_dtb
+    # Import these here in case libfdt.py is not available, in which case
+    # the above help option still works.
+    import fdt
+    import fdt_util
+
+    # If we are updating the DTBs we need to put these updated versions
+    # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb'
+    # since it is assumed to be the one passed in with options.dt, and
+    # was handled just above.
+    main_dtb = dtb
+    fdt_files.clear()
+    fdt_files['u-boot.dtb'] = dtb
+    fdt_set = Set()
+    fdt_subset = Set()
+
+def GetFdts():
+    """Yield all device tree files being used by binman
+
+    Yields:
+        Device trees being used (U-Boot proper, SPL, TPL)
+    """
+    yield main_dtb
+