binman: Allow mkimage to use a non-zero fake-blob size
[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 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
13
14 class Entry_blob(Entry):
15     """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         super().__init__(section, etype, node)
36         self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
37
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,
45                                                           fake_size)
46             self.missing = True
47             if not faked:
48                 self.SetContents(b'')
49                 return True
50
51         self.ReadBlobContents()
52         return True
53
54     def ReadFileContents(self, pathname):
55         """Read blob contents into memory
56
57         This function compresses the data before returning if needed.
58
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.
65
66         Args:
67             pathname (str): Pathname to read from
68
69         Returns:
70             bytes: Data read
71         """
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')
78         return data
79
80     def ReadBlobContents(self):
81         data = self.ReadFileContents(self._pathname)
82         self.SetContents(data)
83         return True
84
85     def GetDefaultFilename(self):
86         return self._filename
87
88     def ProcessContents(self):
89         # The blob may have changed due to WriteSymbols()
90         return self.ProcessContentsUpdate(self.data)
91
92     def CheckFakedBlobs(self, faked_blobs_list):
93         """Check if any entries in this section have faked external blobs
94
95         If there are faked blobs, the entries are added to the list
96
97         Args:
98             fake_blobs_list: List of Entry objects to be added to
99         """
100         if self.faked:
101             faked_blobs_list.append(self)