collection.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2021 Google LLC
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Support for a collection of entries from other parts of an image
  6. from collections import OrderedDict
  7. import os
  8. from binman.entry import Entry
  9. from dtoc import fdt_util
  10. class Entry_collection(Entry):
  11. """An entry which contains a collection of other entries
  12. Properties / Entry arguments:
  13. - content: List of phandles to entries to include
  14. This allows reusing the contents of other entries. The contents of the
  15. listed entries are combined to form this entry. This serves as a useful
  16. base class for entry types which need to process data from elsewhere in
  17. the image, not necessarily child entries.
  18. """
  19. def __init__(self, section, etype, node):
  20. super().__init__(section, etype, node)
  21. self.content = fdt_util.GetPhandleList(self._node, 'content')
  22. if not self.content:
  23. self.Raise("Collection must have a 'content' property")
  24. def GetContents(self, required):
  25. """Get the contents of this entry
  26. Args:
  27. required: True if the data must be present, False if it is OK to
  28. return None
  29. Returns:
  30. bytes content of the entry
  31. """
  32. # Join up all the data
  33. self.Info('Getting contents, required=%s' % required)
  34. data = bytearray()
  35. for entry_phandle in self.content:
  36. entry_data = self.section.GetContentsByPhandle(entry_phandle, self,
  37. required)
  38. if not required and entry_data is None:
  39. self.Info('Contents not available yet')
  40. # Data not available yet
  41. return None
  42. data += entry_data
  43. self.Info('Returning contents size %x' % len(data))
  44. return data
  45. def ObtainContents(self):
  46. data = self.GetContents(False)
  47. if data is None:
  48. return False
  49. self.SetContents(data)
  50. return True
  51. def ProcessContents(self):
  52. # The blob may have changed due to WriteSymbols()
  53. data = self.GetContents(True)
  54. return self.ProcessContentsUpdate(data)