Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2574ef6 | 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 | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 5 | # Class for an image, the output of binman |
| 6 | # |
| 7 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 10 | from collections import OrderedDict |
| 11 | from operator import attrgetter |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 12 | import re |
| 13 | import sys |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 15 | import fdt_util |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 16 | import bsection |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | import tools |
| 18 | |
| 19 | class Image: |
| 20 | """A Image, representing an output from binman |
| 21 | |
| 22 | An image is comprised of a collection of entries each containing binary |
| 23 | data. The image size must be large enough to hold all of this data. |
| 24 | |
| 25 | This class implements the various operations needed for images. |
| 26 | |
| 27 | Atrtributes: |
| 28 | _node: Node object that contains the image definition in device tree |
| 29 | _name: Image name |
| 30 | _size: Image size in bytes, or None if not known yet |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 31 | _filename: Output filename for image |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 32 | _sections: Sections present in this image (may be one or more) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 33 | |
| 34 | Args: |
| 35 | test: True if this is being called from a test of Images. This this case |
| 36 | there is no device tree defining the structure of the section, so |
| 37 | we create a section manually. |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 39 | def __init__(self, name, node, test=False): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | self._node = node |
| 41 | self._name = name |
| 42 | self._size = None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 43 | self._filename = '%s.bin' % self._name |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 44 | if test: |
Simon Glass | 7c767ad | 2018-09-14 04:57:33 -0600 | [diff] [blame] | 45 | self._section = bsection.Section('main-section', None, self._node, |
| 46 | self, True) |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 47 | else: |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 48 | self._ReadNode() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 49 | |
| 50 | def _ReadNode(self): |
| 51 | """Read properties from the image node""" |
| 52 | self._size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 53 | filename = fdt_util.GetString(self._node, 'filename') |
| 54 | if filename: |
| 55 | self._filename = filename |
Simon Glass | 7c767ad | 2018-09-14 04:57:33 -0600 | [diff] [blame] | 56 | self._section = bsection.Section('main-section', None, self._node, self) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 57 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 58 | def GetFdtSet(self): |
| 59 | """Get the set of device tree files used by this image""" |
| 60 | return self._section.GetFdtSet() |
| 61 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 62 | def ExpandEntries(self): |
| 63 | """Expand out any entries which have calculated sub-entries |
| 64 | |
| 65 | Some entries are expanded out at runtime, e.g. 'files', which produces |
| 66 | a section containing a list of files. Process these entries so that |
| 67 | this information is added to the device tree. |
| 68 | """ |
| 69 | self._section.ExpandEntries() |
| 70 | |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 71 | def AddMissingProperties(self): |
| 72 | """Add properties that are not present in the device tree |
| 73 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 74 | When binman has completed packing the entries the offset and size of |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 75 | each entry are known. But before this the device tree may not specify |
| 76 | these. Add any missing properties, with a dummy value, so that the |
| 77 | size of the entry is correct. That way we can insert the correct values |
| 78 | later. |
| 79 | """ |
| 80 | self._section.AddMissingProperties() |
| 81 | |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 82 | def ProcessFdt(self, fdt): |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 83 | """Allow entries to adjust the device tree |
| 84 | |
| 85 | Some entries need to adjust the device tree for their purposes. This |
| 86 | may involve adding or deleting properties. |
| 87 | """ |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 88 | return self._section.ProcessFdt(fdt) |
| 89 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 90 | def GetEntryContents(self): |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 91 | """Call ObtainContents() for the section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 92 | """ |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 93 | self._section.GetEntryContents() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 94 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 95 | def GetEntryOffsets(self): |
| 96 | """Handle entries that want to set the offset/size of other entries |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 97 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 98 | This calls each entry's GetOffsets() method. If it returns a list |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 99 | of entries to update, it updates them. |
| 100 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 101 | self._section.GetEntryOffsets() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 102 | |
| 103 | def PackEntries(self): |
| 104 | """Pack all entries into the image""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 105 | self._section.PackEntries() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 106 | |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 107 | def CheckSize(self): |
| 108 | """Check that the image contents does not exceed its size, etc.""" |
| 109 | self._size = self._section.CheckSize() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 110 | |
| 111 | def CheckEntries(self): |
| 112 | """Check that entries do not overlap or extend outside the image""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 113 | self._section.CheckEntries() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 114 | |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 115 | def SetCalculatedProperties(self): |
| 116 | self._section.SetCalculatedProperties() |
| 117 | |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 118 | def SetImagePos(self): |
| 119 | self._section.SetImagePos(0) |
| 120 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 121 | def ProcessEntryContents(self): |
| 122 | """Call the ProcessContents() method for each entry |
| 123 | |
| 124 | This is intended to adjust the contents as needed by the entry type. |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 125 | |
| 126 | Returns: |
| 127 | True if the new data size is OK, False if expansion is needed |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 128 | """ |
Simon Glass | ec84985 | 2019-07-08 14:25:35 -0600 | [diff] [blame] | 129 | return self._section.ProcessEntryContents() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 130 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 131 | def WriteSymbols(self): |
| 132 | """Write symbol values into binary files for access at run time""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 133 | self._section.WriteSymbols() |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 134 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 135 | def BuildImage(self): |
| 136 | """Write the image to a file""" |
| 137 | fname = tools.GetOutputFilename(self._filename) |
| 138 | with open(fname, 'wb') as fd: |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 139 | self._section.BuildSection(fd, 0) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 140 | |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 141 | def GetEntries(self): |
| 142 | return self._section.GetEntries() |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 143 | |
| 144 | def WriteMap(self): |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 145 | """Write a map of the image to a .map file |
| 146 | |
| 147 | Returns: |
| 148 | Filename of map file written |
| 149 | """ |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 150 | filename = '%s.map' % self._name |
| 151 | fname = tools.GetOutputFilename(filename) |
| 152 | with open(fname, 'w') as fd: |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 153 | print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'), |
| 154 | file=fd) |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 155 | self._section.WriteMap(fd, 0) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 156 | return fname |