blob: 1fb5eb65db39464937c7efe34602f46bfbf72fe6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassbf7fd502016-11-25 20:15:51 -07005# Class for an image, the output of binman
6#
7
Simon Glass19790632017-11-13 18:55:01 -07008from __future__ import print_function
9
Simon Glassbf7fd502016-11-25 20:15:51 -070010from collections import OrderedDict
11from operator import attrgetter
Simon Glass19790632017-11-13 18:55:01 -070012import re
13import sys
Simon Glassbf7fd502016-11-25 20:15:51 -070014
Simon Glassbf7fd502016-11-25 20:15:51 -070015import fdt_util
Simon Glass8f1da502018-06-01 09:38:12 -060016import bsection
Simon Glassbf7fd502016-11-25 20:15:51 -070017import 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
Simon Glassbf7fd502016-11-25 20:15:51 -070031 _filename: Output filename for image
Simon Glass8f1da502018-06-01 09:38:12 -060032 _sections: Sections present in this image (may be one or more)
Simon Glass7ae5f312018-06-01 09:38:19 -060033
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 Glassbf7fd502016-11-25 20:15:51 -070038 """
Simon Glass19790632017-11-13 18:55:01 -070039 def __init__(self, name, node, test=False):
Simon Glassbf7fd502016-11-25 20:15:51 -070040 self._node = node
41 self._name = name
42 self._size = None
Simon Glassbf7fd502016-11-25 20:15:51 -070043 self._filename = '%s.bin' % self._name
Simon Glass8f1da502018-06-01 09:38:12 -060044 if test:
45 self._section = bsection.Section('main-section', self._node, True)
46 else:
Simon Glass19790632017-11-13 18:55:01 -070047 self._ReadNode()
Simon Glassbf7fd502016-11-25 20:15:51 -070048
49 def _ReadNode(self):
50 """Read properties from the image node"""
51 self._size = fdt_util.GetInt(self._node, 'size')
Simon Glassbf7fd502016-11-25 20:15:51 -070052 filename = fdt_util.GetString(self._node, 'filename')
53 if filename:
54 self._filename = filename
Simon Glass8f1da502018-06-01 09:38:12 -060055 self._section = bsection.Section('main-section', self._node)
Simon Glassbf7fd502016-11-25 20:15:51 -070056
Simon Glass539aece2018-09-14 04:57:22 -060057 def GetFdtSet(self):
58 """Get the set of device tree files used by this image"""
59 return self._section.GetFdtSet()
60
Simon Glass078ab1a2018-07-06 10:27:41 -060061 def AddMissingProperties(self):
62 """Add properties that are not present in the device tree
63
Simon Glass3ab95982018-08-01 15:22:37 -060064 When binman has completed packing the entries the offset and size of
Simon Glass078ab1a2018-07-06 10:27:41 -060065 each entry are known. But before this the device tree may not specify
66 these. Add any missing properties, with a dummy value, so that the
67 size of the entry is correct. That way we can insert the correct values
68 later.
69 """
70 self._section.AddMissingProperties()
71
Simon Glassecab8972018-07-06 10:27:40 -060072 def ProcessFdt(self, fdt):
73 return self._section.ProcessFdt(fdt)
74
Simon Glassbf7fd502016-11-25 20:15:51 -070075 def GetEntryContents(self):
Simon Glass8f1da502018-06-01 09:38:12 -060076 """Call ObtainContents() for the section
Simon Glassbf7fd502016-11-25 20:15:51 -070077 """
Simon Glass8f1da502018-06-01 09:38:12 -060078 self._section.GetEntryContents()
Simon Glassbf7fd502016-11-25 20:15:51 -070079
Simon Glass3ab95982018-08-01 15:22:37 -060080 def GetEntryOffsets(self):
81 """Handle entries that want to set the offset/size of other entries
Simon Glassbf7fd502016-11-25 20:15:51 -070082
Simon Glass3ab95982018-08-01 15:22:37 -060083 This calls each entry's GetOffsets() method. If it returns a list
Simon Glassbf7fd502016-11-25 20:15:51 -070084 of entries to update, it updates them.
85 """
Simon Glass3ab95982018-08-01 15:22:37 -060086 self._section.GetEntryOffsets()
Simon Glassbf7fd502016-11-25 20:15:51 -070087
88 def PackEntries(self):
89 """Pack all entries into the image"""
Simon Glass8f1da502018-06-01 09:38:12 -060090 self._section.PackEntries()
Simon Glassbf7fd502016-11-25 20:15:51 -070091
Simon Glass8f1da502018-06-01 09:38:12 -060092 def CheckSize(self):
93 """Check that the image contents does not exceed its size, etc."""
94 self._size = self._section.CheckSize()
Simon Glassbf7fd502016-11-25 20:15:51 -070095
96 def CheckEntries(self):
97 """Check that entries do not overlap or extend outside the image"""
Simon Glass8f1da502018-06-01 09:38:12 -060098 self._section.CheckEntries()
Simon Glassbf7fd502016-11-25 20:15:51 -070099
Simon Glass078ab1a2018-07-06 10:27:41 -0600100 def SetCalculatedProperties(self):
101 self._section.SetCalculatedProperties()
102
Simon Glassdbf6be92018-08-01 15:22:42 -0600103 def SetImagePos(self):
104 self._section.SetImagePos(0)
105
Simon Glassbf7fd502016-11-25 20:15:51 -0700106 def ProcessEntryContents(self):
107 """Call the ProcessContents() method for each entry
108
109 This is intended to adjust the contents as needed by the entry type.
110 """
Simon Glass8f1da502018-06-01 09:38:12 -0600111 self._section.ProcessEntryContents()
Simon Glassbf7fd502016-11-25 20:15:51 -0700112
Simon Glass19790632017-11-13 18:55:01 -0700113 def WriteSymbols(self):
114 """Write symbol values into binary files for access at run time"""
Simon Glass8f1da502018-06-01 09:38:12 -0600115 self._section.WriteSymbols()
Simon Glass19790632017-11-13 18:55:01 -0700116
Simon Glassbf7fd502016-11-25 20:15:51 -0700117 def BuildImage(self):
118 """Write the image to a file"""
119 fname = tools.GetOutputFilename(self._filename)
120 with open(fname, 'wb') as fd:
Simon Glass8f1da502018-06-01 09:38:12 -0600121 self._section.BuildSection(fd, 0)
Simon Glassbf7fd502016-11-25 20:15:51 -0700122
Simon Glass8f1da502018-06-01 09:38:12 -0600123 def GetEntries(self):
124 return self._section.GetEntries()
Simon Glass3b0c3822018-06-01 09:38:20 -0600125
126 def WriteMap(self):
127 """Write a map of the image to a .map file"""
128 filename = '%s.map' % self._name
129 fname = tools.GetOutputFilename(filename)
130 with open(fname, 'w') as fd:
Simon Glass1be70d22018-07-17 13:25:49 -0600131 print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
132 file=fd)
Simon Glass3b0c3822018-06-01 09:38:20 -0600133 self._section.WriteMap(fd, 0)