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 dtoc import fdt_util 10from patman import tools 11from patman import tout 12 13class Entry_blob(Entry): 14 """Entry containing an arbitrary binary blob 15 16 Note: This should not be used by itself. It is normally used as a parent 17 class by other entry types. 18 19 Properties / Entry arguments: 20 - filename: Filename of file to read into entry 21 - compress: Compression algorithm to use: 22 none: No compression 23 lz4: Use lz4 compression (via 'lz4' command-line utility) 24 25 This entry reads data from a file and places it in the entry. The 26 default filename is often specified specified by the subclass. See for 27 example the 'u_boot' entry which provides the filename 'u-boot.bin'. 28 29 If compression is enabled, an extra 'uncomp-size' property is written to 30 the node (if enabled with -u) which provides the uncompressed size of the 31 data. 32 """ 33 def __init__(self, section, etype, node): 34 super().__init__(section, etype, node) 35 self._filename = fdt_util.GetString(self._node, 'filename', self.etype) 36 37 def ObtainContents(self): 38 self._filename = self.GetDefaultFilename() 39 self._pathname = tools.GetInputFilename(self._filename, 40 self.external and self.section.GetAllowMissing()) 41 # Allow the file to be missing 42 if not self._pathname: 43 self.SetContents(b'') 44 self.missing = True 45 return True 46 47 self.ReadBlobContents() 48 return True 49 50 def ReadBlobContents(self): 51 """Read blob contents into memory 52 53 This function compresses the data before storing if needed. 54 55 We assume the data is small enough to fit into memory. If this 56 is used for large filesystem image that might not be true. 57 In that case, Image.BuildImage() could be adjusted to use a 58 new Entry method which can read in chunks. Then we could copy 59 the data in chunks and avoid reading it all at once. For now 60 this seems like an unnecessary complication. 61 """ 62 indata = tools.ReadFile(self._pathname) 63 data = self.CompressData(indata) 64 self.SetContents(data) 65 return True 66 67 def GetDefaultFilename(self): 68 return self._filename 69 70 def ProcessContents(self): 71 # The blob may have changed due to WriteSymbols() 72 return self.ProcessContentsUpdate(self.data) 73