1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
5 # Entry-type module for blobs, which are binary objects read from files
10 from binman.entry import Entry
11 from binman import state
12 from dtoc import fdt_util
13 from patman import tools
14 from patman import tout
16 class Entry_blob(Entry):
17 """Arbitrary binary blob
19 Note: This should not be used by itself. It is normally used as a parent
20 class by other entry types.
22 Properties / Entry arguments:
23 - filename: Filename of file to read into entry
24 - compress: Compression algorithm to use:
26 lz4: Use lz4 compression (via 'lz4' command-line utility)
28 This entry reads data from a file and places it in the entry. The
29 default filename is often specified specified by the subclass. See for
30 example the 'u-boot' entry which provides the filename 'u-boot.bin'.
32 If compression is enabled, an extra 'uncomp-size' property is written to
33 the node (if enabled with -u) which provides the uncompressed size of the
36 def __init__(self, section, etype, node):
37 super().__init__(section, etype, node)
38 self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
40 def ObtainContents(self):
41 if self.allow_fake and not pathlib.Path(self._filename).is_file():
42 with open(self._filename, "wb") as out:
46 self._filename = self.GetDefaultFilename()
47 self._pathname = tools.GetInputFilename(self._filename,
48 self.external and self.section.GetAllowMissing())
49 # Allow the file to be missing
50 if not self._pathname:
55 self.ReadBlobContents()
58 def ReadFileContents(self, pathname):
59 """Read blob contents into memory
61 This function compresses the data before returning if needed.
63 We assume the data is small enough to fit into memory. If this
64 is used for large filesystem image that might not be true.
65 In that case, Image.BuildImage() could be adjusted to use a
66 new Entry method which can read in chunks. Then we could copy
67 the data in chunks and avoid reading it all at once. For now
68 this seems like an unnecessary complication.
71 pathname (str): Pathname to read from
76 state.TimingStart('read')
77 indata = tools.ReadFile(pathname)
78 state.TimingAccum('read')
79 state.TimingStart('compress')
80 data = self.CompressData(indata)
81 state.TimingAccum('compress')
84 def ReadBlobContents(self):
85 data = self.ReadFileContents(self._pathname)
86 self.SetContents(data)
89 def GetDefaultFilename(self):
92 def ProcessContents(self):
93 # The blob may have changed due to WriteSymbols()
94 return self.ProcessContentsUpdate(self.data)
96 def CheckFakedBlobs(self, faked_blobs_list):
97 """Check if any entries in this section have faked external blobs
99 If there are faked blobs, the entries are added to the list
102 fake_blobs_list: List of Entry objects to be added to
105 faked_blobs_list.append(self)