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
8from binman.entry import Entry
9from binman import state
10from dtoc import fdt_util
11from u_boot_pylib import tools
12from u_boot_pylib import tout
13
14class 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, auto_write_symbols=False):
35        super().__init__(section, etype, node,
36                         auto_write_symbols=auto_write_symbols)
37        self._filename = fdt_util.GetString(self._node, 'filename', self.etype)
38        self.elf_fname = fdt_util.GetString(self._node, 'elf-filename',
39                                            self.elf_fname)
40        self.elf_base_sym = fdt_util.GetString(self._node, 'elf-base-sym')
41        if not self.auto_write_symbols:
42            if fdt_util.GetBool(self._node, 'write-symbols'):
43                self.auto_write_symbols = True
44
45    def ObtainContents(self, fake_size=0):
46        self._filename = self.GetDefaultFilename()
47        self._pathname = tools.get_input_filename(self._filename,
48            self.external and (self.optional or self.section.GetAllowMissing()))
49        # Allow the file to be missing
50        if not self._pathname:
51            self._pathname, faked = self.check_fake_fname(self._filename,
52                                                          fake_size)
53            self.missing = True
54            if not faked:
55                self.SetContents(b'')
56                return True
57
58        self.ReadBlobContents()
59        return True
60
61    def ReadFileContents(self, pathname):
62        """Read blob contents into memory
63
64        This function compresses the data before returning if needed.
65
66        We assume the data is small enough to fit into memory. If this
67        is used for large filesystem image that might not be true.
68        In that case, Image.BuildImage() could be adjusted to use a
69        new Entry method which can read in chunks. Then we could copy
70        the data in chunks and avoid reading it all at once. For now
71        this seems like an unnecessary complication.
72
73        Args:
74            pathname (str): Pathname to read from
75
76        Returns:
77            bytes: Data read
78        """
79        state.TimingStart('read')
80        indata = tools.read_file(pathname)
81        state.TimingAccum('read')
82        state.TimingStart('compress')
83        data = self.CompressData(indata)
84        state.TimingAccum('compress')
85        return data
86
87    def ReadBlobContents(self):
88        data = self.ReadFileContents(self._pathname)
89        self.SetContents(data)
90        return True
91
92    def GetDefaultFilename(self):
93        return self._filename
94
95    def ProcessContents(self):
96        # The blob may have changed due to WriteSymbols()
97        return self.ProcessContentsUpdate(self.data)
98
99    def CheckFakedBlobs(self, faked_blobs_list):
100        """Check if any entries in this section have faked external blobs
101
102        If there are faked blobs, the entries are added to the list
103
104        Args:
105            faked_blobs_list: List of Entry objects to be added to
106        """
107        if self.faked:
108            faked_blobs_list.append(self)
109