Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 5 | # Class for an image, the output of binman |
| 6 | # |
| 7 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 10 | from collections import OrderedDict |
| 11 | from operator import attrgetter |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 12 | import re |
| 13 | import sys |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | |
Simon Glass | ffded75 | 2019-07-08 14:25:46 -0600 | [diff] [blame^] | 15 | from etype import fdtmap |
| 16 | from etype import image_header |
| 17 | import fdt |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 18 | import fdt_util |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 19 | import bsection |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 20 | import tools |
| 21 | |
| 22 | class Image: |
| 23 | """A Image, representing an output from binman |
| 24 | |
| 25 | An image is comprised of a collection of entries each containing binary |
| 26 | data. The image size must be large enough to hold all of this data. |
| 27 | |
| 28 | This class implements the various operations needed for images. |
| 29 | |
| 30 | Atrtributes: |
| 31 | _node: Node object that contains the image definition in device tree |
| 32 | _name: Image name |
| 33 | _size: Image size in bytes, or None if not known yet |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 34 | _filename: Output filename for image |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 35 | _sections: Sections present in this image (may be one or more) |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 36 | |
| 37 | Args: |
| 38 | test: True if this is being called from a test of Images. This this case |
| 39 | there is no device tree defining the structure of the section, so |
| 40 | we create a section manually. |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 41 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 42 | def __init__(self, name, node, test=False): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 43 | self._node = node |
| 44 | self._name = name |
| 45 | self._size = None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | self._filename = '%s.bin' % self._name |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 47 | if test: |
Simon Glass | 08723a7 | 2018-09-14 04:57:33 -0600 | [diff] [blame] | 48 | self._section = bsection.Section('main-section', None, self._node, |
| 49 | self, True) |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 50 | else: |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 51 | self._ReadNode() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 52 | |
Simon Glass | ffded75 | 2019-07-08 14:25:46 -0600 | [diff] [blame^] | 53 | @classmethod |
| 54 | def FromFile(cls, fname): |
| 55 | """Convert an image file into an Image for use in binman |
| 56 | |
| 57 | Args: |
| 58 | fname: Filename of image file to read |
| 59 | |
| 60 | Returns: |
| 61 | Image object on success |
| 62 | |
| 63 | Raises: |
| 64 | ValueError if something goes wrong |
| 65 | """ |
| 66 | data = tools.ReadFile(fname) |
| 67 | size = len(data) |
| 68 | |
| 69 | # First look for an image header |
| 70 | pos = image_header.LocateHeaderOffset(data) |
| 71 | if pos is None: |
| 72 | # Look for the FDT map |
| 73 | pos = fdtmap.LocateFdtmap(data) |
| 74 | if pos is None: |
| 75 | raise ValueError('Cannot find FDT map in image') |
| 76 | |
| 77 | # We don't know the FDT size, so check its header first |
| 78 | probe_dtb = fdt.Fdt.FromData( |
| 79 | data[pos + fdtmap.FDTMAP_HDR_LEN:pos + 256]) |
| 80 | dtb_size = probe_dtb.GetFdtObj().totalsize() |
| 81 | fdtmap_data = data[pos:pos + dtb_size + fdtmap.FDTMAP_HDR_LEN] |
| 82 | dtb = fdt.Fdt.FromData(fdtmap_data[fdtmap.FDTMAP_HDR_LEN:]) |
| 83 | dtb.Scan() |
| 84 | |
| 85 | # Return an Image with the associated nodes |
| 86 | return Image('image', dtb.GetRoot()) |
| 87 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 88 | def _ReadNode(self): |
| 89 | """Read properties from the image node""" |
| 90 | self._size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 91 | filename = fdt_util.GetString(self._node, 'filename') |
| 92 | if filename: |
| 93 | self._filename = filename |
Simon Glass | 08723a7 | 2018-09-14 04:57:33 -0600 | [diff] [blame] | 94 | self._section = bsection.Section('main-section', None, self._node, self) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 95 | |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 96 | def Raise(self, msg): |
| 97 | """Convenience function to raise an error referencing an image""" |
| 98 | raise ValueError("Image '%s': %s" % (self._node.path, msg)) |
| 99 | |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 100 | def GetFdtSet(self): |
| 101 | """Get the set of device tree files used by this image""" |
| 102 | return self._section.GetFdtSet() |
| 103 | |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 104 | def ExpandEntries(self): |
| 105 | """Expand out any entries which have calculated sub-entries |
| 106 | |
| 107 | Some entries are expanded out at runtime, e.g. 'files', which produces |
| 108 | a section containing a list of files. Process these entries so that |
| 109 | this information is added to the device tree. |
| 110 | """ |
| 111 | self._section.ExpandEntries() |
| 112 | |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 113 | def AddMissingProperties(self): |
| 114 | """Add properties that are not present in the device tree |
| 115 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 116 | When binman has completed packing the entries the offset and size of |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 117 | each entry are known. But before this the device tree may not specify |
| 118 | these. Add any missing properties, with a dummy value, so that the |
| 119 | size of the entry is correct. That way we can insert the correct values |
| 120 | later. |
| 121 | """ |
| 122 | self._section.AddMissingProperties() |
| 123 | |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 124 | def ProcessFdt(self, fdt): |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 125 | """Allow entries to adjust the device tree |
| 126 | |
| 127 | Some entries need to adjust the device tree for their purposes. This |
| 128 | may involve adding or deleting properties. |
| 129 | """ |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 130 | return self._section.ProcessFdt(fdt) |
| 131 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 132 | def GetEntryContents(self): |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 133 | """Call ObtainContents() for the section |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 134 | """ |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 135 | self._section.GetEntryContents() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 136 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 137 | def GetEntryOffsets(self): |
| 138 | """Handle entries that want to set the offset/size of other entries |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 139 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 140 | This calls each entry's GetOffsets() method. If it returns a list |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 141 | of entries to update, it updates them. |
| 142 | """ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 143 | self._section.GetEntryOffsets() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 144 | |
Simon Glass | c52c9e7 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 145 | def ResetForPack(self): |
| 146 | """Reset offset/size fields so that packing can be done again""" |
| 147 | self._section.ResetForPack() |
| 148 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 149 | def PackEntries(self): |
| 150 | """Pack all entries into the image""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 151 | self._section.PackEntries() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 152 | |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 153 | def CheckSize(self): |
| 154 | """Check that the image contents does not exceed its size, etc.""" |
| 155 | self._size = self._section.CheckSize() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 156 | |
| 157 | def CheckEntries(self): |
| 158 | """Check that entries do not overlap or extend outside the image""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 159 | self._section.CheckEntries() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 160 | |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 161 | def SetCalculatedProperties(self): |
| 162 | self._section.SetCalculatedProperties() |
| 163 | |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 164 | def SetImagePos(self): |
| 165 | self._section.SetImagePos(0) |
| 166 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 167 | def ProcessEntryContents(self): |
| 168 | """Call the ProcessContents() method for each entry |
| 169 | |
| 170 | This is intended to adjust the contents as needed by the entry type. |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 171 | |
| 172 | Returns: |
| 173 | True if the new data size is OK, False if expansion is needed |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 174 | """ |
Simon Glass | a0dcaf2 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 175 | return self._section.ProcessEntryContents() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 176 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 177 | def WriteSymbols(self): |
| 178 | """Write symbol values into binary files for access at run time""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 179 | self._section.WriteSymbols() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 180 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 181 | def BuildImage(self): |
| 182 | """Write the image to a file""" |
| 183 | fname = tools.GetOutputFilename(self._filename) |
| 184 | with open(fname, 'wb') as fd: |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 185 | self._section.BuildSection(fd, 0) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 186 | |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 187 | def GetEntries(self): |
| 188 | return self._section.GetEntries() |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 189 | |
| 190 | def WriteMap(self): |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 191 | """Write a map of the image to a .map file |
| 192 | |
| 193 | Returns: |
| 194 | Filename of map file written |
| 195 | """ |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 196 | filename = '%s.map' % self._name |
| 197 | fname = tools.GetOutputFilename(filename) |
| 198 | with open(fname, 'w') as fd: |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 199 | print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'), |
| 200 | file=fd) |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 201 | self._section.WriteMap(fd, 0) |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 202 | return fname |
Simon Glass | 41b8ba0 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 203 | |
| 204 | def BuildEntryList(self): |
| 205 | """List the files in an image |
| 206 | |
| 207 | Returns: |
| 208 | List of entry.EntryInfo objects describing all entries in the image |
| 209 | """ |
| 210 | entries = [] |
| 211 | self._section.ListEntries(entries, 0) |
| 212 | return entries |