binman: Obtain the list of device trees from the config
[oweals/u-boot.git] / tools / binman / image.py
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
8 from __future__ import print_function
9
10 from collections import OrderedDict
11 from operator import attrgetter
12 import re
13 import sys
14
15 import fdt_util
16 import bsection
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
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 AddMissingProperties(self):
62         """Add properties that are not present in the device tree
63
64         When binman has completed packing the entries the offset and size of
65         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
72     def ProcessFdt(self, fdt):
73         return self._section.ProcessFdt(fdt)
74
75     def GetEntryContents(self):
76         """Call ObtainContents() for the section
77         """
78         self._section.GetEntryContents()
79
80     def GetEntryOffsets(self):
81         """Handle entries that want to set the offset/size of other entries
82
83         This calls each entry's GetOffsets() method. If it returns a list
84         of entries to update, it updates them.
85         """
86         self._section.GetEntryOffsets()
87
88     def PackEntries(self):
89         """Pack all entries into the image"""
90         self._section.PackEntries()
91
92     def CheckSize(self):
93         """Check that the image contents does not exceed its size, etc."""
94         self._size = self._section.CheckSize()
95
96     def CheckEntries(self):
97         """Check that entries do not overlap or extend outside the image"""
98         self._section.CheckEntries()
99
100     def SetCalculatedProperties(self):
101         self._section.SetCalculatedProperties()
102
103     def SetImagePos(self):
104         self._section.SetImagePos(0)
105
106     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         """
111         self._section.ProcessEntryContents()
112
113     def WriteSymbols(self):
114         """Write symbol values into binary files for access at run time"""
115         self._section.WriteSymbols()
116
117     def BuildImage(self):
118         """Write the image to a file"""
119         fname = tools.GetOutputFilename(self._filename)
120         with open(fname, 'wb') as fd:
121             self._section.BuildSection(fd, 0)
122
123     def GetEntries(self):
124         return self._section.GetEntries()
125
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:
131             print('%8s  %8s  %8s  %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
132                   file=fd)
133             self._section.WriteMap(fd, 0)