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 dtoc import fdt_util
10 from patman import tools
11 from patman import tout
13 class Entry_blob(Entry):
14 """Entry containing an arbitrary binary blob
16 Note: This should not be used by itself. It is normally used as a parent
17 class by other entry types.
19 Properties / Entry arguments:
20 - filename: Filename of file to read into entry
21 - compress: Compression algorithm to use:
23 lz4: Use lz4 compression (via 'lz4' command-line utility)
25 This entry reads data from a file and places it in the entry. The
26 default filename is often specified specified by the subclass. See for
27 example the 'u_boot' entry which provides the filename 'u-boot.bin'.
29 If compression is enabled, an extra 'uncomp-size' property is written to
30 the node (if enabled with -u) which provides the uncompressed size of the
33 def __init__(self, section, etype, node):
34 super().__init__(section, etype, node)
35 self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
36 self.compress = fdt_util.GetString(self._node, 'compress', 'none')
38 def ObtainContents(self):
39 self._filename = self.GetDefaultFilename()
40 self._pathname = tools.GetInputFilename(self._filename,
41 self.section.GetAllowMissing())
42 # Allow the file to be missing
43 if self.external and not self._pathname:
47 self.ReadBlobContents()
50 def CompressData(self, indata):
51 if self.compress != 'none':
52 self.uncomp_size = len(indata)
53 data = tools.Compress(indata, self.compress)
56 def ReadBlobContents(self):
57 """Read blob contents into memory
59 This function compresses the data before storing if needed.
61 We assume the data is small enough to fit into memory. If this
62 is used for large filesystem image that might not be true.
63 In that case, Image.BuildImage() could be adjusted to use a
64 new Entry method which can read in chunks. Then we could copy
65 the data in chunks and avoid reading it all at once. For now
66 this seems like an unnecessary complication.
68 indata = tools.ReadFile(self._pathname)
69 data = self.CompressData(indata)
70 self.SetContents(data)
73 def GetDefaultFilename(self):