blob: 4b922b51c427eebeaa63a8f50c67af3b70c8ffa4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass2574ef62016-11-25 20:15:51 -07005# Class for an image, the output of binman
6#
7
Simon Glass4ca8e042017-11-13 18:55:01 -07008from __future__ import print_function
9
Simon Glass2574ef62016-11-25 20:15:51 -070010from collections import OrderedDict
11from operator import attrgetter
Simon Glass4ca8e042017-11-13 18:55:01 -070012import re
13import sys
Simon Glass2574ef62016-11-25 20:15:51 -070014
Simon Glass2574ef62016-11-25 20:15:51 -070015import fdt_util
Simon Glasseca32212018-06-01 09:38:12 -060016import bsection
Simon Glass2574ef62016-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 Glass2574ef62016-11-25 20:15:51 -070031 _filename: Output filename for image
Simon Glasseca32212018-06-01 09:38:12 -060032 _sections: Sections present in this image (may be one or more)
Simon Glass1e324002018-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 Glass2574ef62016-11-25 20:15:51 -070038 """
Simon Glass4ca8e042017-11-13 18:55:01 -070039 def __init__(self, name, node, test=False):
Simon Glass2574ef62016-11-25 20:15:51 -070040 self._node = node
41 self._name = name
42 self._size = None
Simon Glass2574ef62016-11-25 20:15:51 -070043 self._filename = '%s.bin' % self._name
Simon Glasseca32212018-06-01 09:38:12 -060044 if test:
45 self._section = bsection.Section('main-section', self._node, True)
46 else:
Simon Glass4ca8e042017-11-13 18:55:01 -070047 self._ReadNode()
Simon Glass2574ef62016-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 Glass2574ef62016-11-25 20:15:51 -070052 filename = fdt_util.GetString(self._node, 'filename')
53 if filename:
54 self._filename = filename
Simon Glasseca32212018-06-01 09:38:12 -060055 self._section = bsection.Section('main-section', self._node)
Simon Glass2574ef62016-11-25 20:15:51 -070056
Simon Glass0c9d5b52018-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 Glassac6328c2018-09-14 04:57:28 -060061 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
Simon Glasse22f8fa2018-07-06 10:27:41 -060070 def AddMissingProperties(self):
71 """Add properties that are not present in the device tree
72
Simon Glasse8561af2018-08-01 15:22:37 -060073 When binman has completed packing the entries the offset and size of
Simon Glasse22f8fa2018-07-06 10:27:41 -060074 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
Simon Glass92307732018-07-06 10:27:40 -060081 def ProcessFdt(self, fdt):
Simon Glasse219aa42018-09-14 04:57:24 -060082 """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 """
Simon Glass92307732018-07-06 10:27:40 -060087 return self._section.ProcessFdt(fdt)
88
Simon Glass2574ef62016-11-25 20:15:51 -070089 def GetEntryContents(self):
Simon Glasseca32212018-06-01 09:38:12 -060090 """Call ObtainContents() for the section
Simon Glass2574ef62016-11-25 20:15:51 -070091 """
Simon Glasseca32212018-06-01 09:38:12 -060092 self._section.GetEntryContents()
Simon Glass2574ef62016-11-25 20:15:51 -070093
Simon Glasse8561af2018-08-01 15:22:37 -060094 def GetEntryOffsets(self):
95 """Handle entries that want to set the offset/size of other entries
Simon Glass2574ef62016-11-25 20:15:51 -070096
Simon Glasse8561af2018-08-01 15:22:37 -060097 This calls each entry's GetOffsets() method. If it returns a list
Simon Glass2574ef62016-11-25 20:15:51 -070098 of entries to update, it updates them.
99 """
Simon Glasse8561af2018-08-01 15:22:37 -0600100 self._section.GetEntryOffsets()
Simon Glass2574ef62016-11-25 20:15:51 -0700101
102 def PackEntries(self):
103 """Pack all entries into the image"""
Simon Glasseca32212018-06-01 09:38:12 -0600104 self._section.PackEntries()
Simon Glass2574ef62016-11-25 20:15:51 -0700105
Simon Glasseca32212018-06-01 09:38:12 -0600106 def CheckSize(self):
107 """Check that the image contents does not exceed its size, etc."""
108 self._size = self._section.CheckSize()
Simon Glass2574ef62016-11-25 20:15:51 -0700109
110 def CheckEntries(self):
111 """Check that entries do not overlap or extend outside the image"""
Simon Glasseca32212018-06-01 09:38:12 -0600112 self._section.CheckEntries()
Simon Glass2574ef62016-11-25 20:15:51 -0700113
Simon Glasse22f8fa2018-07-06 10:27:41 -0600114 def SetCalculatedProperties(self):
115 self._section.SetCalculatedProperties()
116
Simon Glass9dcc8612018-08-01 15:22:42 -0600117 def SetImagePos(self):
118 self._section.SetImagePos(0)
119
Simon Glass2574ef62016-11-25 20:15:51 -0700120 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 """
Simon Glasseca32212018-06-01 09:38:12 -0600125 self._section.ProcessEntryContents()
Simon Glass2574ef62016-11-25 20:15:51 -0700126
Simon Glass4ca8e042017-11-13 18:55:01 -0700127 def WriteSymbols(self):
128 """Write symbol values into binary files for access at run time"""
Simon Glasseca32212018-06-01 09:38:12 -0600129 self._section.WriteSymbols()
Simon Glass4ca8e042017-11-13 18:55:01 -0700130
Simon Glass2574ef62016-11-25 20:15:51 -0700131 def BuildImage(self):
132 """Write the image to a file"""
133 fname = tools.GetOutputFilename(self._filename)
134 with open(fname, 'wb') as fd:
Simon Glasseca32212018-06-01 09:38:12 -0600135 self._section.BuildSection(fd, 0)
Simon Glass2574ef62016-11-25 20:15:51 -0700136
Simon Glasseca32212018-06-01 09:38:12 -0600137 def GetEntries(self):
138 return self._section.GetEntries()
Simon Glass30732662018-06-01 09:38:20 -0600139
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:
Simon Glass7eca7922018-07-17 13:25:49 -0600145 print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
146 file=fd)
Simon Glass30732662018-06-01 09:38:20 -0600147 self._section.WriteMap(fd, 0)