Merge branch 'master' of git://git.denx.de/u-boot-spi
[oweals/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 tools
11
12 class Entry_blob(Entry):
13     """Entry containing an arbitrary binary blob
14
15     Note: This should not be used by itself. It is normally used as a parent
16     class by other entry types.
17
18     Properties / Entry arguments:
19         - filename: Filename of file to read into entry
20
21     This entry reads data from a file and places it in the entry. The
22     default filename is often specified specified by the subclass. See for
23     example the 'u_boot' entry which provides the filename 'u-boot.bin'.
24     """
25     def __init__(self, section, etype, node):
26         Entry.__init__(self, section, etype, node)
27         self._filename = fdt_util.GetString(self._node, "filename", self.etype)
28
29     def ObtainContents(self):
30         self._filename = self.GetDefaultFilename()
31         self._pathname = tools.GetInputFilename(self._filename)
32         self.ReadBlobContents()
33         return True
34
35     def ReadBlobContents(self):
36         with open(self._pathname) as fd:
37             # We assume the data is small enough to fit into memory. If this
38             # is used for large filesystem image that might not be true.
39             # In that case, Image.BuildImage() could be adjusted to use a
40             # new Entry method which can read in chunks. Then we could copy
41             # the data in chunks and avoid reading it all at once. For now
42             # this seems like an unnecessary complication.
43             self.SetContents(fd.read())
44         return True
45
46     def GetDefaultFilename(self):
47         return self._filename