Blame view
tools/binman/image.py
5.03 KB
83d290c56
|
1 |
# SPDX-License-Identifier: GPL-2.0+ |
bf7fd50b3
|
2 3 4 |
# Copyright (c) 2016 Google, Inc # Written by Simon Glass <sjg@chromium.org> # |
bf7fd50b3
|
5 6 |
# Class for an image, the output of binman # |
197906326
|
7 |
from __future__ import print_function |
bf7fd50b3
|
8 9 |
from collections import OrderedDict from operator import attrgetter |
197906326
|
10 11 |
import re import sys |
bf7fd50b3
|
12 |
|
bf7fd50b3
|
13 |
import fdt_util |
8f1da50cc
|
14 |
import bsection |
bf7fd50b3
|
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
|
29 |
_filename: Output filename for image |
8f1da50cc
|
30 |
_sections: Sections present in this image (may be one or more) |
7ae5f315b
|
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
|
36 |
""" |
197906326
|
37 |
def __init__(self, name, node, test=False): |
bf7fd50b3
|
38 39 40 |
self._node = node self._name = name self._size = None |
bf7fd50b3
|
41 |
self._filename = '%s.bin' % self._name |
8f1da50cc
|
42 |
if test: |
08723a7ab
|
43 44 |
self._section = bsection.Section('main-section', None, self._node, self, True) |
8f1da50cc
|
45 |
else: |
197906326
|
46 |
self._ReadNode() |
bf7fd50b3
|
47 48 49 50 |
def _ReadNode(self): """Read properties from the image node""" self._size = fdt_util.GetInt(self._node, 'size') |
bf7fd50b3
|
51 52 53 |
filename = fdt_util.GetString(self._node, 'filename') if filename: self._filename = filename |
08723a7ab
|
54 |
self._section = bsection.Section('main-section', None, self._node, self) |
bf7fd50b3
|
55 |
|
539aece51
|
56 57 58 |
def GetFdtSet(self): """Get the set of device tree files used by this image""" return self._section.GetFdtSet() |
0a98b28b0
|
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
|
67 68 |
def AddMissingProperties(self): """Add properties that are not present in the device tree |
3ab9598df
|
69 |
When binman has completed packing the entries the offset and size of |
078ab1a2f
|
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
|
76 |
def ProcessFdt(self, fdt): |
6ed45ba0a
|
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
|
82 |
return self._section.ProcessFdt(fdt) |
bf7fd50b3
|
83 |
def GetEntryContents(self): |
8f1da50cc
|
84 |
"""Call ObtainContents() for the section |
bf7fd50b3
|
85 |
""" |
8f1da50cc
|
86 |
self._section.GetEntryContents() |
bf7fd50b3
|
87 |
|
3ab9598df
|
88 89 |
def GetEntryOffsets(self): """Handle entries that want to set the offset/size of other entries |
bf7fd50b3
|
90 |
|
3ab9598df
|
91 |
This calls each entry's GetOffsets() method. If it returns a list |
bf7fd50b3
|
92 93 |
of entries to update, it updates them. """ |
3ab9598df
|
94 |
self._section.GetEntryOffsets() |
bf7fd50b3
|
95 96 97 |
def PackEntries(self): """Pack all entries into the image""" |
8f1da50cc
|
98 |
self._section.PackEntries() |
bf7fd50b3
|
99 |
|
8f1da50cc
|
100 101 102 |
def CheckSize(self): """Check that the image contents does not exceed its size, etc.""" self._size = self._section.CheckSize() |
bf7fd50b3
|
103 104 105 |
def CheckEntries(self): """Check that entries do not overlap or extend outside the image""" |
8f1da50cc
|
106 |
self._section.CheckEntries() |
bf7fd50b3
|
107 |
|
078ab1a2f
|
108 109 |
def SetCalculatedProperties(self): self._section.SetCalculatedProperties() |
dbf6be9f7
|
110 111 |
def SetImagePos(self): self._section.SetImagePos(0) |
bf7fd50b3
|
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
|
117 |
self._section.ProcessEntryContents() |
bf7fd50b3
|
118 |
|
197906326
|
119 120 |
def WriteSymbols(self): """Write symbol values into binary files for access at run time""" |
8f1da50cc
|
121 |
self._section.WriteSymbols() |
197906326
|
122 |
|
bf7fd50b3
|
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
|
127 |
self._section.BuildSection(fd, 0) |
bf7fd50b3
|
128 |
|
8f1da50cc
|
129 130 |
def GetEntries(self): return self._section.GetEntries() |
3b0c3821d
|
131 132 |
def WriteMap(self): |
163ed6c34
|
133 134 135 136 137 |
"""Write a map of the image to a .map file Returns: Filename of map file written """ |
3b0c3821d
|
138 139 140 |
filename = '%s.map' % self._name fname = tools.GetOutputFilename(filename) with open(fname, 'w') as fd: |
1be70d20d
|
141 142 |
print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'), file=fd) |
3b0c3821d
|
143 |
self._section.WriteMap(fd, 0) |
163ed6c34
|
144 |
return fname |