blob.py 2.5 KB

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