blob.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 entry import Entry
  8. import fdt_util
  9. import state
  10. import tools
  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. self._uncompressed_size = None
  32. def ObtainContents(self):
  33. self._filename = self.GetDefaultFilename()
  34. self._pathname = tools.GetInputFilename(self._filename)
  35. self.ReadBlobContents()
  36. return True
  37. def ReadBlobContents(self):
  38. # We assume the data is small enough to fit into memory. If this
  39. # is used for large filesystem image that might not be true.
  40. # In that case, Image.BuildImage() could be adjusted to use a
  41. # new Entry method which can read in chunks. Then we could copy
  42. # the data in chunks and avoid reading it all at once. For now
  43. # this seems like an unnecessary complication.
  44. data = tools.ReadFile(self._pathname)
  45. if self._compress == 'lz4':
  46. self._uncompressed_size = len(data)
  47. '''
  48. import lz4 # Import this only if needed (python-lz4 dependency)
  49. try:
  50. data = lz4.frame.compress(data)
  51. except AttributeError:
  52. data = lz4.compress(data)
  53. '''
  54. data = tools.Run('lz4', '-c', self._pathname, )
  55. self.SetContents(data)
  56. return True
  57. def GetDefaultFilename(self):
  58. return self._filename
  59. def AddMissingProperties(self):
  60. Entry.AddMissingProperties(self)
  61. if self._compress != 'none':
  62. state.AddZeroProp(self._node, 'uncomp-size')
  63. def SetCalculatedProperties(self):
  64. Entry.SetCalculatedProperties(self)
  65. if self._uncompressed_size is not None:
  66. state.SetInt(self._node, 'uncomp-size', self._uncompressed_size)