1# SPDX-License-Identifier: GPL-2.0+ 2# Copyright (c) 2016 Google, Inc 3# Written by Simon Glass <sjg@chromium.org> 4# 5# Class for an image, the output of binman 6# 7 8from __future__ import print_function 9 10from collections import OrderedDict 11from operator import attrgetter 12import re 13import sys 14 15import fdt_util 16import bsection 17import tools 18 19class 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 31 _filename: Output filename for image 32 _sections: Sections present in this image (may be one or more) 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. 38 """ 39 def __init__(self, name, node, test=False): 40 self._node = node 41 self._name = name 42 self._size = None 43 self._filename = '%s.bin' % self._name 44 if test: 45 self._section = bsection.Section('main-section', self._node, True) 46 else: 47 self._ReadNode() 48 49 def _ReadNode(self): 50 """Read properties from the image node""" 51 self._size = fdt_util.GetInt(self._node, 'size') 52 filename = fdt_util.GetString(self._node, 'filename') 53 if filename: 54 self._filename = filename 55 self._section = bsection.Section('main-section', self._node) 56 57 def GetFdtSet(self): 58 """Get the set of device tree files used by this image""" 59 return self._section.GetFdtSet() 60 61 def ExpandEntries(self): 62 """Expand out any entries which have calculated sub-entries 63 64 Some entries are expanded out at runtime, e.g. 'files', which produces 65 a section containing a list of files. Process these entries so that 66 this information is added to the device tree. 67 """ 68 self._section.ExpandEntries() 69 70 def AddMissingProperties(self): 71 """Add properties that are not present in the device tree 72 73 When binman has completed packing the entries the offset and size of 74 each entry are known. But before this the device tree may not specify 75 these. Add any missing properties, with a dummy value, so that the 76 size of the entry is correct. That way we can insert the correct values 77 later. 78 """ 79 self._section.AddMissingProperties() 80 81 def ProcessFdt(self, fdt): 82 """Allow entries to adjust the device tree 83 84 Some entries need to adjust the device tree for their purposes. This 85 may involve adding or deleting properties. 86 """ 87 return self._section.ProcessFdt(fdt) 88 89 def GetEntryContents(self): 90 """Call ObtainContents() for the section 91 """ 92 self._section.GetEntryContents() 93 94 def GetEntryOffsets(self): 95 """Handle entries that want to set the offset/size of other entries 96 97 This calls each entry's GetOffsets() method. If it returns a list 98 of entries to update, it updates them. 99 """ 100 self._section.GetEntryOffsets() 101 102 def PackEntries(self): 103 """Pack all entries into the image""" 104 self._section.PackEntries() 105 106 def CheckSize(self): 107 """Check that the image contents does not exceed its size, etc.""" 108 self._size = self._section.CheckSize() 109 110 def CheckEntries(self): 111 """Check that entries do not overlap or extend outside the image""" 112 self._section.CheckEntries() 113 114 def SetCalculatedProperties(self): 115 self._section.SetCalculatedProperties() 116 117 def SetImagePos(self): 118 self._section.SetImagePos(0) 119 120 def ProcessEntryContents(self): 121 """Call the ProcessContents() method for each entry 122 123 This is intended to adjust the contents as needed by the entry type. 124 """ 125 self._section.ProcessEntryContents() 126 127 def WriteSymbols(self): 128 """Write symbol values into binary files for access at run time""" 129 self._section.WriteSymbols() 130 131 def BuildImage(self): 132 """Write the image to a file""" 133 fname = tools.GetOutputFilename(self._filename) 134 with open(fname, 'wb') as fd: 135 self._section.BuildSection(fd, 0) 136 137 def GetEntries(self): 138 return self._section.GetEntries() 139 140 def WriteMap(self): 141 """Write a map of the image to a .map file""" 142 filename = '%s.map' % self._name 143 fname = tools.GetOutputFilename(filename) 144 with open(fname, 'w') as fd: 145 print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'), 146 file=fd) 147 self._section.WriteMap(fd, 0) 148