Blame view

tools/binman/image.py 5.03 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  # SPDX-License-Identifier: GPL-2.0+
bf7fd50b3   Simon Glass   binman: Introduce...
2
3
4
  # Copyright (c) 2016 Google, Inc
  # Written by Simon Glass <sjg@chromium.org>
  #
bf7fd50b3   Simon Glass   binman: Introduce...
5
6
  # Class for an image, the output of binman
  #
197906326   Simon Glass   binman: Support a...
7
  from __future__ import print_function
bf7fd50b3   Simon Glass   binman: Introduce...
8
9
  from collections import OrderedDict
  from operator import attrgetter
197906326   Simon Glass   binman: Support a...
10
11
  import re
  import sys
bf7fd50b3   Simon Glass   binman: Introduce...
12

bf7fd50b3   Simon Glass   binman: Introduce...
13
  import fdt_util
8f1da50cc   Simon Glass   binman: Refactor ...
14
  import bsection
bf7fd50b3   Simon Glass   binman: Introduce...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  import tools
  
  class Image:
      """A Image, representing an output from binman
  
      An image is comprised of a collection of entries each containing binary
      data. The image size must be large enough to hold all of this data.
  
      This class implements the various operations needed for images.
  
      Atrtributes:
          _node: Node object that contains the image definition in device tree
          _name: Image name
          _size: Image size in bytes, or None if not known yet
bf7fd50b3   Simon Glass   binman: Introduce...
29
          _filename: Output filename for image
8f1da50cc   Simon Glass   binman: Refactor ...
30
          _sections: Sections present in this image (may be one or more)
7ae5f315b   Simon Glass   binman: Tidy up s...
31
32
33
34
35
  
      Args:
          test: True if this is being called from a test of Images. This this case
              there is no device tree defining the structure of the section, so
              we create a section manually.
bf7fd50b3   Simon Glass   binman: Introduce...
36
      """
197906326   Simon Glass   binman: Support a...
37
      def __init__(self, name, node, test=False):
bf7fd50b3   Simon Glass   binman: Introduce...
38
39
40
          self._node = node
          self._name = name
          self._size = None
bf7fd50b3   Simon Glass   binman: Introduce...
41
          self._filename = '%s.bin' % self._name
8f1da50cc   Simon Glass   binman: Refactor ...
42
          if test:
08723a7ab   Simon Glass   binman: Record th...
43
44
              self._section = bsection.Section('main-section', None, self._node,
                                               self, True)
8f1da50cc   Simon Glass   binman: Refactor ...
45
          else:
197906326   Simon Glass   binman: Support a...
46
              self._ReadNode()
bf7fd50b3   Simon Glass   binman: Introduce...
47
48
49
50
  
      def _ReadNode(self):
          """Read properties from the image node"""
          self._size = fdt_util.GetInt(self._node, 'size')
bf7fd50b3   Simon Glass   binman: Introduce...
51
52
53
          filename = fdt_util.GetString(self._node, 'filename')
          if filename:
              self._filename = filename
08723a7ab   Simon Glass   binman: Record th...
54
          self._section = bsection.Section('main-section', None, self._node, self)
bf7fd50b3   Simon Glass   binman: Introduce...
55

539aece51   Simon Glass   binman: Obtain th...
56
57
58
      def GetFdtSet(self):
          """Get the set of device tree files used by this image"""
          return self._section.GetFdtSet()
0a98b28b0   Simon Glass   binman: Support a...
59
60
61
62
63
64
65
66
      def ExpandEntries(self):
          """Expand out any entries which have calculated sub-entries
  
          Some entries are expanded out at runtime, e.g. 'files', which produces
          a section containing a list of files. Process these entries so that
          this information is added to the device tree.
          """
          self._section.ExpandEntries()
078ab1a2f   Simon Glass   binman: Add a Set...
67
68
      def AddMissingProperties(self):
          """Add properties that are not present in the device tree
3ab9598df   Simon Glass   binman: Rename 'p...
69
          When binman has completed packing the entries the offset and size of
078ab1a2f   Simon Glass   binman: Add a Set...
70
71
72
73
74
75
          each entry are known. But before this the device tree may not specify
          these. Add any missing properties, with a dummy value, so that the
          size of the entry is correct. That way we can insert the correct values
          later.
          """
          self._section.AddMissingProperties()
ecab89737   Simon Glass   binman: Add a Pro...
76
      def ProcessFdt(self, fdt):
6ed45ba0a   Simon Glass   binman: Support u...
77
78
79
80
81
          """Allow entries to adjust the device tree
  
          Some entries need to adjust the device tree for their purposes. This
          may involve adding or deleting properties.
          """
ecab89737   Simon Glass   binman: Add a Pro...
82
          return self._section.ProcessFdt(fdt)
bf7fd50b3   Simon Glass   binman: Introduce...
83
      def GetEntryContents(self):
8f1da50cc   Simon Glass   binman: Refactor ...
84
          """Call ObtainContents() for the section
bf7fd50b3   Simon Glass   binman: Introduce...
85
          """
8f1da50cc   Simon Glass   binman: Refactor ...
86
          self._section.GetEntryContents()
bf7fd50b3   Simon Glass   binman: Introduce...
87

3ab9598df   Simon Glass   binman: Rename 'p...
88
89
      def GetEntryOffsets(self):
          """Handle entries that want to set the offset/size of other entries
bf7fd50b3   Simon Glass   binman: Introduce...
90

3ab9598df   Simon Glass   binman: Rename 'p...
91
          This calls each entry's GetOffsets() method. If it returns a list
bf7fd50b3   Simon Glass   binman: Introduce...
92
93
          of entries to update, it updates them.
          """
3ab9598df   Simon Glass   binman: Rename 'p...
94
          self._section.GetEntryOffsets()
bf7fd50b3   Simon Glass   binman: Introduce...
95
96
97
  
      def PackEntries(self):
          """Pack all entries into the image"""
8f1da50cc   Simon Glass   binman: Refactor ...
98
          self._section.PackEntries()
bf7fd50b3   Simon Glass   binman: Introduce...
99

8f1da50cc   Simon Glass   binman: Refactor ...
100
101
102
      def CheckSize(self):
          """Check that the image contents does not exceed its size, etc."""
          self._size = self._section.CheckSize()
bf7fd50b3   Simon Glass   binman: Introduce...
103
104
105
  
      def CheckEntries(self):
          """Check that entries do not overlap or extend outside the image"""
8f1da50cc   Simon Glass   binman: Refactor ...
106
          self._section.CheckEntries()
bf7fd50b3   Simon Glass   binman: Introduce...
107

078ab1a2f   Simon Glass   binman: Add a Set...
108
109
      def SetCalculatedProperties(self):
          self._section.SetCalculatedProperties()
dbf6be9f7   Simon Glass   binman: Add a new...
110
111
      def SetImagePos(self):
          self._section.SetImagePos(0)
bf7fd50b3   Simon Glass   binman: Introduce...
112
113
114
115
116
      def ProcessEntryContents(self):
          """Call the ProcessContents() method for each entry
  
          This is intended to adjust the contents as needed by the entry type.
          """
8f1da50cc   Simon Glass   binman: Refactor ...
117
          self._section.ProcessEntryContents()
bf7fd50b3   Simon Glass   binman: Introduce...
118

197906326   Simon Glass   binman: Support a...
119
120
      def WriteSymbols(self):
          """Write symbol values into binary files for access at run time"""
8f1da50cc   Simon Glass   binman: Refactor ...
121
          self._section.WriteSymbols()
197906326   Simon Glass   binman: Support a...
122

bf7fd50b3   Simon Glass   binman: Introduce...
123
124
125
126
      def BuildImage(self):
          """Write the image to a file"""
          fname = tools.GetOutputFilename(self._filename)
          with open(fname, 'wb') as fd:
8f1da50cc   Simon Glass   binman: Refactor ...
127
              self._section.BuildSection(fd, 0)
bf7fd50b3   Simon Glass   binman: Introduce...
128

8f1da50cc   Simon Glass   binman: Refactor ...
129
130
      def GetEntries(self):
          return self._section.GetEntries()
3b0c3821d   Simon Glass   binman: Add suppo...
131
132
  
      def WriteMap(self):
163ed6c34   Simon Glass   binman: Allow wri...
133
134
135
136
137
          """Write a map of the image to a .map file
  
          Returns:
              Filename of map file written
          """
3b0c3821d   Simon Glass   binman: Add suppo...
138
139
140
          filename = '%s.map' % self._name
          fname = tools.GetOutputFilename(filename)
          with open(fname, 'w') as fd:
1be70d20d   Simon Glass   binman: Show the ...
141
142
              print('%8s  %8s  %8s  %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
                    file=fd)
3b0c3821d   Simon Glass   binman: Add suppo...
143
              self._section.WriteMap(fd, 0)
163ed6c34   Simon Glass   binman: Allow wri...
144
          return fname