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 entry import Entry
14 class Entry_blob(Entry):
15 """Entry containing an 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 Entry.__init__(self, section, etype, node)
36 self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
37 self.compress = fdt_util.GetString(self._node, 'compress', 'none')
39 def ObtainContents(self):
40 self._filename = self.GetDefaultFilename()
41 self._pathname = tools.GetInputFilename(self._filename)
42 self.ReadBlobContents()
45 def CompressData(self, indata):
46 if self.compress != 'none':
47 self.uncomp_size = len(indata)
48 data = tools.Compress(indata, self.compress)
51 def ReadBlobContents(self):
52 """Read blob contents into memory
54 This function compresses the data before storing if needed.
56 We assume the data is small enough to fit into memory. If this
57 is used for large filesystem image that might not be true.
58 In that case, Image.BuildImage() could be adjusted to use a
59 new Entry method which can read in chunks. Then we could copy
60 the data in chunks and avoid reading it all at once. For now
61 this seems like an unnecessary complication.
63 indata = tools.ReadFile(self._pathname)
64 data = self.CompressData(indata)
65 self.SetContents(data)
68 def GetDefaultFilename(self):
71 def ReadData(self, decomp=True):
72 indata = Entry.ReadData(self, decomp)
74 data = tools.Decompress(indata, self.compress)
76 tout.Info("%s: Decompressing data size %#x with algo '%s' to data size %#x" %
77 (self.GetPath(), len(indata), self.compress,