blob.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) 2016 Google, Inc
  2. # Written by Simon Glass <sjg@chromium.org>
  3. #
  4. # SPDX-License-Identifier: GPL-2.0+
  5. #
  6. # Entry-type module for blobs, which are binary objects read from files
  7. #
  8. from entry import Entry
  9. import fdt_util
  10. import tools
  11. class Entry_blob(Entry):
  12. def __init__(self, image, etype, node):
  13. Entry.__init__(self, image, etype, node)
  14. self._filename = fdt_util.GetString(self._node, "filename", self.etype)
  15. def ObtainContents(self):
  16. self._filename = self.GetDefaultFilename()
  17. self._pathname = tools.GetInputFilename(self._filename)
  18. self.ReadContents()
  19. return True
  20. def ReadContents(self):
  21. with open(self._pathname) as fd:
  22. # We assume the data is small enough to fit into memory. If this
  23. # is used for large filesystem image that might not be true.
  24. # In that case, Image.BuildImage() could be adjusted to use a
  25. # new Entry method which can read in chunks. Then we could copy
  26. # the data in chunks and avoid reading it all at once. For now
  27. # this seems like an unnecessary complication.
  28. self.data = fd.read()
  29. self.contents_size = len(self.data)
  30. return True
  31. def GetDefaultFilename(self):
  32. return self._filename