binman: Record the parent section of each section
[oweals/u-boot.git] / tools / binman / etype / u_boot_ucode.py
index 8e51e99a1190700daf7facd9ed595ca081ea5f93..a00e530295b6323b083c3b8e78635db9ee037c4f 100644 (file)
@@ -1,8 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
 # Copyright (c) 2016 Google, Inc
 # Written by Simon Glass <sjg@chromium.org>
 #
-# SPDX-License-Identifier:      GPL-2.0+
-#
 # Entry-type module for a U-Boot binary with an embedded microcode pointer
 #
 
@@ -13,6 +12,12 @@ import tools
 class Entry_u_boot_ucode(Entry_blob):
     """U-Boot microcode block
 
+    Properties / Entry arguments:
+        None
+
+    The contents of this entry are filled in automatically by other entries
+    which must also be in the image.
+
     U-Boot on x86 needs a single block of microcode. This is collected from
     the various microcode update nodes in the device tree. It is also unable
     to read the microcode from the device tree on platforms that use FSP
@@ -52,39 +57,43 @@ class Entry_u_boot_ucode(Entry_blob):
             the Entry_u_boot_dtb_with_ucode entry, and uses it as the
             contents of this entry.
     """
-    def __init__(self, image, etype, node):
-        Entry_blob.__init__(self, image, etype, node)
+    def __init__(self, section, etype, node):
+        Entry_blob.__init__(self, section, etype, node)
 
     def ObtainContents(self):
-        # If the image does not need microcode, there is nothing to do
-        ucode_dest_entry = self.image.FindEntryType('u-boot-with-ucode-ptr')
-        if ucode_dest_entry and not ucode_dest_entry.target_pos:
+        # If the section does not need microcode, there is nothing to do
+        found = False
+        for suffix in ['', '-spl', '-tpl']:
+            name = 'u-boot%s-with-ucode-ptr' % suffix
+            entry = self.section.FindEntryType(name)
+            if entry and entry.target_offset:
+                found = True
+        if not found:
             self.data = ''
             return True
-
-        # Handle microcode in SPL image as well
-        ucode_dest_entry = self.image.FindEntryType('u-boot-spl-with-ucode-ptr')
-        if ucode_dest_entry and not ucode_dest_entry.target_pos:
-            self.data = ''
+        # Get the microcode from the device tree entry. If it is not available
+        # yet, return False so we will be called later. If the section simply
+        # doesn't exist, then we may as well return True, since we are going to
+        # get an error anyway.
+        for suffix in ['', '-spl', '-tpl']:
+            name = 'u-boot%s-dtb-with-ucode' % suffix
+            fdt_entry = self.section.FindEntryType(name)
+            if fdt_entry:
+                break
+        if not fdt_entry:
             return True
-
-        # Get the microcode from the device tree entry
-        fdt_entry = self.image.FindEntryType('u-boot-dtb-with-ucode')
-        if not fdt_entry or not fdt_entry.ucode_data:
+        if not fdt_entry.ready:
             return False
 
         if not fdt_entry.collate:
-            # This section can be empty
+            # This binary can be empty
             self.data = ''
             return True
 
         # Write it out to a file
-        dtb_name = 'u-boot-ucode.bin'
-        fname = tools.GetOutputFilename(dtb_name)
-        with open(fname, 'wb') as fd:
-            fd.write(fdt_entry.ucode_data)
+        self._pathname = tools.GetOutputFilename('u-boot-ucode.bin')
+        tools.WriteFile(self._pathname, fdt_entry.ucode_data)
 
-        self._pathname = fname
-        self.ReadContents()
+        self.ReadBlobContents()
 
         return True