summaryrefslogtreecommitdiff
path: root/tools/binman/image.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/binman/image.py')
-rw-r--r--tools/binman/image.py315
1 files changed, 232 insertions, 83 deletions
diff --git a/tools/binman/image.py b/tools/binman/image.py
index f237ae302d..fb6e591ca6 100644
--- a/tools/binman/image.py
+++ b/tools/binman/image.py
@@ -8,15 +8,21 @@
from __future__ import print_function
from collections import OrderedDict
+import fnmatch
from operator import attrgetter
import re
import sys
+from entry import Entry
+from etype import fdtmap
+from etype import image_header
+from etype import section
+import fdt
import fdt_util
-import bsection
import tools
+import tout
-class Image:
+class Image(section.Entry_section):
"""A Image, representing an output from binman
An image is comprised of a collection of entries each containing binary
@@ -24,12 +30,8 @@ class Image:
This class implements the various operations needed for images.
- Atrtributes:
- _node: Node object that contains the image definition in device tree
- _name: Image name
- _size: Image size in bytes, or None if not known yet
- _filename: Output filename for image
- _sections: Sections present in this image (may be one or more)
+ Attributes:
+ filename: Output filename for image
Args:
test: True if this is being called from a test of Images. This this case
@@ -37,106 +39,94 @@ class Image:
we create a section manually.
"""
def __init__(self, name, node, test=False):
- self._node = node
- self._name = name
- self._size = None
- self._filename = '%s.bin' % self._name
- if test:
- self._section = bsection.Section('main-section', None, self._node,
- self, True)
- else:
- self._ReadNode()
-
- def _ReadNode(self):
- """Read properties from the image node"""
- self._size = fdt_util.GetInt(self._node, 'size')
- filename = fdt_util.GetString(self._node, 'filename')
- if filename:
- self._filename = filename
- self._section = bsection.Section('main-section', None, self._node, self)
-
- def GetFdtSet(self):
- """Get the set of device tree files used by this image"""
- return self._section.GetFdtSet()
-
- def ExpandEntries(self):
- """Expand out any entries which have calculated sub-entries
-
- Some entries are expanded out at runtime, e.g. 'files', which produces
- a section containing a list of files. Process these entries so that
- this information is added to the device tree.
- """
- self._section.ExpandEntries()
+ self.image = self
+ section.Entry_section.__init__(self, None, 'section', node, test)
+ self.name = 'main-section'
+ self.image_name = name
+ self._filename = '%s.bin' % self.image_name
+ if not test:
+ filename = fdt_util.GetString(self._node, 'filename')
+ if filename:
+ self._filename = filename
- def AddMissingProperties(self):
- """Add properties that are not present in the device tree
+ @classmethod
+ def FromFile(cls, fname):
+ """Convert an image file into an Image for use in binman
- When binman has completed packing the entries the offset and size of
- each entry are known. But before this the device tree may not specify
- these. Add any missing properties, with a dummy value, so that the
- size of the entry is correct. That way we can insert the correct values
- later.
- """
- self._section.AddMissingProperties()
+ Args:
+ fname: Filename of image file to read
- def ProcessFdt(self, fdt):
- """Allow entries to adjust the device tree
+ Returns:
+ Image object on success
- Some entries need to adjust the device tree for their purposes. This
- may involve adding or deleting properties.
+ Raises:
+ ValueError if something goes wrong
"""
- return self._section.ProcessFdt(fdt)
+ data = tools.ReadFile(fname)
+ size = len(data)
- def GetEntryContents(self):
- """Call ObtainContents() for the section
- """
- self._section.GetEntryContents()
+ # First look for an image header
+ pos = image_header.LocateHeaderOffset(data)
+ if pos is None:
+ # Look for the FDT map
+ pos = fdtmap.LocateFdtmap(data)
+ if pos is None:
+ raise ValueError('Cannot find FDT map in image')
- def GetEntryOffsets(self):
- """Handle entries that want to set the offset/size of other entries
+ # We don't know the FDT size, so check its header first
+ probe_dtb = fdt.Fdt.FromData(
+ data[pos + fdtmap.FDTMAP_HDR_LEN:pos + 256])
+ dtb_size = probe_dtb.GetFdtObj().totalsize()
+ fdtmap_data = data[pos:pos + dtb_size + fdtmap.FDTMAP_HDR_LEN]
+ dtb = fdt.Fdt.FromData(fdtmap_data[fdtmap.FDTMAP_HDR_LEN:])
+ dtb.Scan()
- This calls each entry's GetOffsets() method. If it returns a list
- of entries to update, it updates them.
- """
- self._section.GetEntryOffsets()
+ # Return an Image with the associated nodes
+ image = Image('image', dtb.GetRoot())
+ image._data = data
+ return image
+
+ def Raise(self, msg):
+ """Convenience function to raise an error referencing an image"""
+ raise ValueError("Image '%s': %s" % (self._node.path, msg))
def PackEntries(self):
"""Pack all entries into the image"""
- self._section.PackEntries()
-
- def CheckSize(self):
- """Check that the image contents does not exceed its size, etc."""
- self._size = self._section.CheckSize()
-
- def CheckEntries(self):
- """Check that entries do not overlap or extend outside the image"""
- self._section.CheckEntries()
-
- def SetCalculatedProperties(self):
- self._section.SetCalculatedProperties()
+ section.Entry_section.Pack(self, 0)
def SetImagePos(self):
- self._section.SetImagePos(0)
+ # This first section in the image so it starts at 0
+ section.Entry_section.SetImagePos(self, 0)
def ProcessEntryContents(self):
"""Call the ProcessContents() method for each entry
This is intended to adjust the contents as needed by the entry type.
+
+ Returns:
+ True if the new data size is OK, False if expansion is needed
"""
- self._section.ProcessEntryContents()
+ sizes_ok = True
+ for entry in self._entries.values():
+ if not entry.ProcessContents():
+ sizes_ok = False
+ tout.Debug("Entry '%s' size change" % self._node.path)
+ return sizes_ok
def WriteSymbols(self):
"""Write symbol values into binary files for access at run time"""
- self._section.WriteSymbols()
+ section.Entry_section.WriteSymbols(self, self)
+
+ def BuildSection(self, fd, base_offset):
+ """Write the section to a file"""
+ fd.seek(base_offset)
+ fd.write(self.GetData())
def BuildImage(self):
"""Write the image to a file"""
fname = tools.GetOutputFilename(self._filename)
with open(fname, 'wb') as fd:
- self._section.BuildSection(fd, 0)
-
- def GetEntries(self):
- return self._section.GetEntries()
+ self.BuildSection(fd, 0)
def WriteMap(self):
"""Write a map of the image to a .map file
@@ -144,10 +134,169 @@ class Image:
Returns:
Filename of map file written
"""
- filename = '%s.map' % self._name
+ filename = '%s.map' % self.image_name
fname = tools.GetOutputFilename(filename)
with open(fname, 'w') as fd:
print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
file=fd)
- self._section.WriteMap(fd, 0)
+ section.Entry_section.WriteMap(self, fd, 0)
return fname
+
+ def BuildEntryList(self):
+ """List the files in an image
+
+ Returns:
+ List of entry.EntryInfo objects describing all entries in the image
+ """
+ entries = []
+ self.ListEntries(entries, 0)
+ return entries
+
+ def FindEntryPath(self, entry_path):
+ """Find an entry at a given path in the image
+
+ Args:
+ entry_path: Path to entry (e.g. /ro-section/u-boot')
+
+ Returns:
+ Entry object corresponding to that past
+
+ Raises:
+ ValueError if no entry found
+ """
+ parts = entry_path.split('/')
+ entries = self.GetEntries()
+ parent = '/'
+ for part in parts:
+ entry = entries.get(part)
+ if not entry:
+ raise ValueError("Entry '%s' not found in '%s'" %
+ (part, parent))
+ parent = entry.GetPath()
+ entries = entry.GetEntries()
+ return entry
+
+ def ReadData(self, decomp=True):
+ return self._data
+
+ def GetListEntries(self, entry_paths):
+ """List the entries in an image
+
+ This decodes the supplied image and returns a list of entries from that
+ image, preceded by a header.
+
+ Args:
+ entry_paths: List of paths to match (each can have wildcards). Only
+ entries whose names match one of these paths will be printed
+
+ Returns:
+ String error message if something went wrong, otherwise
+ 3-Tuple:
+ List of EntryInfo objects
+ List of lines, each
+ List of text columns, each a string
+ List of widths of each column
+ """
+ def _EntryToStrings(entry):
+ """Convert an entry to a list of strings, one for each column
+
+ Args:
+ entry: EntryInfo object containing information to output
+
+ Returns:
+ List of strings, one for each field in entry
+ """
+ def _AppendHex(val):
+ """Append a hex value, or an empty string if val is None
+
+ Args:
+ val: Integer value, or None if none
+ """
+ args.append('' if val is None else '>%x' % val)
+
+ args = [' ' * entry.indent + entry.name]
+ _AppendHex(entry.image_pos)
+ _AppendHex(entry.size)
+ args.append(entry.etype)
+ _AppendHex(entry.offset)
+ _AppendHex(entry.uncomp_size)
+ return args
+
+ def _DoLine(lines, line):
+ """Add a line to the output list
+
+ This adds a line (a list of columns) to the output list. It also updates
+ the widths[] array with the maximum width of each column
+
+ Args:
+ lines: List of lines to add to
+ line: List of strings, one for each column
+ """
+ for i, item in enumerate(line):
+ widths[i] = max(widths[i], len(item))
+ lines.append(line)
+
+ def _NameInPaths(fname, entry_paths):
+ """Check if a filename is in a list of wildcarded paths
+
+ Args:
+ fname: Filename to check
+ entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*',
+ 'section/u-boot'])
+
+ Returns:
+ True if any wildcard matches the filename (using Unix filename
+ pattern matching, not regular expressions)
+ False if not
+ """
+ for path in entry_paths:
+ if fnmatch.fnmatch(fname, path):
+ return True
+ return False
+
+ entries = self.BuildEntryList()
+
+ # This is our list of lines. Each item in the list is a list of strings, one
+ # for each column
+ lines = []
+ HEADER = ['Name', 'Image-pos', 'Size', 'Entry-type', 'Offset',
+ 'Uncomp-size']
+ num_columns = len(HEADER)
+
+ # This records the width of each column, calculated as the maximum width of
+ # all the strings in that column
+ widths = [0] * num_columns
+ _DoLine(lines, HEADER)
+
+ # We won't print anything unless it has at least this indent. So at the
+ # start we will print nothing, unless a path matches (or there are no
+ # entry paths)
+ MAX_INDENT = 100
+ min_indent = MAX_INDENT
+ path_stack = []
+ path = ''
+ indent = 0
+ selected_entries = []
+ for entry in entries:
+ if entry.indent > indent:
+ path_stack.append(path)
+ elif entry.indent < indent:
+ path_stack.pop()
+ if path_stack:
+ path = path_stack[-1] + '/' + entry.name
+ indent = entry.indent
+
+ # If there are entry paths to match and we are not looking at a
+ # sub-entry of a previously matched entry, we need to check the path
+ if entry_paths and indent <= min_indent:
+ if _NameInPaths(path[1:], entry_paths):
+ # Print this entry and all sub-entries (=higher indent)
+ min_indent = indent
+ else:
+ # Don't print this entry, nor any following entries until we get
+ # a path match
+ min_indent = MAX_INDENT
+ continue
+ _DoLine(lines, _EntryToStrings(entry))
+ selected_entries.append(entry)
+ return selected_entries, lines, widths