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
8 from binman.entry import Entry
9 from binman import state
10 from dtoc import fdt_util
11 from patman import tools
12 from patman import tout
14 class Entry_blob(Entry):
15 """Arbitrary binary blob
17 Note: This should not be used by itself. It is normally used as a parent
18 class by other entry types.
20 Properties / Entry arguments:
21 - filename: Filename of file to read into entry
22 - compress: Compression algorithm to use:
24 lz4: Use lz4 compression (via 'lz4' command-line utility)
26 This entry reads data from a file and places it in the entry. The
27 default filename is often specified specified by the subclass. See for
28 example the 'u-boot' entry which provides the filename 'u-boot.bin'.
30 If compression is enabled, an extra 'uncomp-size' property is written to
31 the node (if enabled with -u) which provides the uncompressed size of the
34 def __init__(self, section, etype, node):
35 super().__init__(section, etype, node)
36 self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
38 def ObtainContents(self, fake_size=0):
39 self._filename = self.GetDefaultFilename()
40 self._pathname = tools.get_input_filename(self._filename,
41 self.external and self.section.GetAllowMissing())
42 # Allow the file to be missing
43 if not self._pathname:
44 self._pathname, faked = self.check_fake_fname(self._filename,
51 self.ReadBlobContents()
54 def ReadFileContents(self, pathname):
55 """Read blob contents into memory
57 This function compresses the data before returning if needed.
59 We assume the data is small enough to fit into memory. If this
60 is used for large filesystem image that might not be true.
61 In that case, Image.BuildImage() could be adjusted to use a
62 new Entry method which can read in chunks. Then we could copy
63 the data in chunks and avoid reading it all at once. For now
64 this seems like an unnecessary complication.
67 pathname (str): Pathname to read from
72 state.TimingStart('read')
73 indata = tools.read_file(pathname)
74 state.TimingAccum('read')
75 state.TimingStart('compress')
76 data = self.CompressData(indata)
77 state.TimingAccum('compress')
80 def ReadBlobContents(self):
81 data = self.ReadFileContents(self._pathname)
82 self.SetContents(data)
85 def GetDefaultFilename(self):
88 def ProcessContents(self):
89 # The blob may have changed due to WriteSymbols()
90 return self.ProcessContentsUpdate(self.data)
92 def CheckFakedBlobs(self, faked_blobs_list):
93 """Check if any entries in this section have faked external blobs
95 If there are faked blobs, the entries are added to the list
98 fake_blobs_list: List of Entry objects to be added to
101 faked_blobs_list.append(self)