]> git.sur5r.net Git - u-boot/blob - tools/binman/image.py
binman: Avoid setting sys.path globally
[u-boot] / 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     def __init__(self, name, node, test=False):
35         self._node = node
36         self._name = name
37         self._size = None
38         self._filename = '%s.bin' % self._name
39         if test:
40             self._section = bsection.Section('main-section', self._node, True)
41         else:
42             self._ReadNode()
43
44     def _ReadNode(self):
45         """Read properties from the image node"""
46         self._size = fdt_util.GetInt(self._node, 'size')
47         filename = fdt_util.GetString(self._node, 'filename')
48         if filename:
49             self._filename = filename
50         self._section = bsection.Section('main-section', self._node)
51
52     def GetEntryContents(self):
53         """Call ObtainContents() for the section
54         """
55         self._section.GetEntryContents()
56
57     def GetEntryPositions(self):
58         """Handle entries that want to set the position/size of other entries
59
60         This calls each entry's GetPositions() method. If it returns a list
61         of entries to update, it updates them.
62         """
63         self._section.GetEntryPositions()
64
65     def PackEntries(self):
66         """Pack all entries into the image"""
67         self._section.PackEntries()
68
69     def CheckSize(self):
70         """Check that the image contents does not exceed its size, etc."""
71         self._size = self._section.CheckSize()
72
73     def CheckEntries(self):
74         """Check that entries do not overlap or extend outside the image"""
75         self._section.CheckEntries()
76
77     def ProcessEntryContents(self):
78         """Call the ProcessContents() method for each entry
79
80         This is intended to adjust the contents as needed by the entry type.
81         """
82         self._section.ProcessEntryContents()
83
84     def WriteSymbols(self):
85         """Write symbol values into binary files for access at run time"""
86         self._section.WriteSymbols()
87
88     def BuildImage(self):
89         """Write the image to a file"""
90         fname = tools.GetOutputFilename(self._filename)
91         with open(fname, 'wb') as fd:
92             self._section.BuildSection(fd, 0)
93
94     def GetEntries(self):
95         return self._section.GetEntries()