65ebb2ecf4d83490691ca1e431d13f993732c5b2
[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 import pathlib
9
10 from binman.entry import Entry
11 from binman import state
12 from dtoc import fdt_util
13 from patman import tools
14 from patman import tout
15
16 class Entry_blob(Entry):
17     """Arbitrary binary blob
18
19     Note: This should not be used by itself. It is normally used as a parent
20     class by other entry types.
21
22     Properties / Entry arguments:
23         - filename: Filename of file to read into entry
24         - compress: Compression algorithm to use:
25             none: No compression
26             lz4: Use lz4 compression (via 'lz4' command-line utility)
27
28     This entry reads data from a file and places it in the entry. The
29     default filename is often specified specified by the subclass. See for
30     example the 'u-boot' entry which provides the filename 'u-boot.bin'.
31
32     If compression is enabled, an extra 'uncomp-size' property is written to
33     the node (if enabled with -u) which provides the uncompressed size of the
34     data.
35     """
36     def __init__(self, section, etype, node):
37         super().__init__(section, etype, node)
38         self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
39
40     def ObtainContents(self):
41         if self.allow_fake and not pathlib.Path(self._filename).is_file():
42             with open(self._filename, "wb") as out:
43                 out.truncate(1024)
44             self.faked = True
45
46         self._filename = self.GetDefaultFilename()
47         self._pathname = tools.GetInputFilename(self._filename,
48             self.external and self.section.GetAllowMissing())
49         # Allow the file to be missing
50         if not self._pathname:
51             self.SetContents(b'')
52             self.missing = True
53             return True
54
55         self.ReadBlobContents()
56         return True
57
58     def ReadFileContents(self, pathname):
59         """Read blob contents into memory
60
61         This function compresses the data before returning if needed.
62
63         We assume the data is small enough to fit into memory. If this
64         is used for large filesystem image that might not be true.
65         In that case, Image.BuildImage() could be adjusted to use a
66         new Entry method which can read in chunks. Then we could copy
67         the data in chunks and avoid reading it all at once. For now
68         this seems like an unnecessary complication.
69
70         Args:
71             pathname (str): Pathname to read from
72
73         Returns:
74             bytes: Data read
75         """
76         state.TimingStart('read')
77         indata = tools.ReadFile(pathname)
78         state.TimingAccum('read')
79         state.TimingStart('compress')
80         data = self.CompressData(indata)
81         state.TimingAccum('compress')
82         return data
83
84     def ReadBlobContents(self):
85         data = self.ReadFileContents(self._pathname)
86         self.SetContents(data)
87         return True
88
89     def GetDefaultFilename(self):
90         return self._filename
91
92     def ProcessContents(self):
93         # The blob may have changed due to WriteSymbols()
94         return self.ProcessContentsUpdate(self.data)
95
96     def CheckFakedBlobs(self, faked_blobs_list):
97         """Check if any entries in this section have faked external blobs
98
99         If there are faked blobs, the entries are added to the list
100
101         Args:
102             fake_blobs_list: List of Entry objects to be added to
103         """
104         if self.faked:
105             faked_blobs_list.append(self)