Merge tag 'u-boot-imx-20190719' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / tools / binman / etype / blob.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
4 #
5 # Entry-type module for blobs, which are binary objects read from files
6 #
7
8 from entry import Entry
9 import fdt_util
10 import state
11 import tools
12 import tout
13
14 class Entry_blob(Entry):
15     """Entry containing an arbitrary binary blob
16
17     Note: This should not be used by itself. It is normally used as a parent
18     class by other entry types.
19
20     Properties / Entry arguments:
21         - filename: Filename of file to read into entry
22         - compress: Compression algorithm to use:
23             none: No compression
24             lz4: Use lz4 compression (via 'lz4' command-line utility)
25
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'.
29
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
32     data.
33     """
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')
38
39     def ObtainContents(self):
40         self._filename = self.GetDefaultFilename()
41         self._pathname = tools.GetInputFilename(self._filename)
42         self.ReadBlobContents()
43         return True
44
45     def CompressData(self, indata):
46         if self.compress != 'none':
47             self.uncomp_size = len(indata)
48         data = tools.Compress(indata, self.compress)
49         return data
50
51     def ReadBlobContents(self):
52         """Read blob contents into memory
53
54         This function compresses the data before storing if needed.
55
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.
62         """
63         indata = tools.ReadFile(self._pathname)
64         data = self.CompressData(indata)
65         self.SetContents(data)
66         return True
67
68     def GetDefaultFilename(self):
69         return self._filename
70
71     def ReadData(self, decomp=True):
72         indata = Entry.ReadData(self, decomp)
73         if decomp:
74             data = tools.Decompress(indata, self.compress)
75             if self.uncomp_size:
76                 tout.Info("%s: Decompressing data size %#x with algo '%s' to data size %#x" %
77                           (self.GetPath(), len(indata), self.compress,
78                            len(data)))
79         else:
80             data = indata
81         return data