files.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for a set of files which are placed in individual
  6. # sub-entries
  7. #
  8. import glob
  9. import os
  10. from binman.etype.section import Entry_section
  11. from dtoc import fdt_util
  12. from patman import tools
  13. class Entry_files(Entry_section):
  14. """A set of files arranged in a section
  15. Properties / Entry arguments:
  16. - pattern: Filename pattern to match the files to include
  17. - files-compress: Compression algorithm to use:
  18. none: No compression
  19. lz4: Use lz4 compression (via 'lz4' command-line utility)
  20. - files-align: Align each file to the given alignment
  21. This entry reads a number of files and places each in a separate sub-entry
  22. within this entry. To access these you need to enable device-tree updates
  23. at run-time so you can obtain the file positions.
  24. """
  25. def __init__(self, section, etype, node):
  26. # Put this here to allow entry-docs and help to work without libfdt
  27. global state
  28. from binman import state
  29. super().__init__(section, etype, node)
  30. def ReadNode(self):
  31. super().ReadNode()
  32. self._pattern = fdt_util.GetString(self._node, 'pattern')
  33. if not self._pattern:
  34. self.Raise("Missing 'pattern' property")
  35. self._files_compress = fdt_util.GetString(self._node, 'files-compress',
  36. 'none')
  37. self._files_align = fdt_util.GetInt(self._node, 'files-align');
  38. self._require_matches = fdt_util.GetBool(self._node,
  39. 'require-matches')
  40. def ExpandEntries(self):
  41. files = tools.GetInputFilenameGlob(self._pattern)
  42. if self._require_matches and not files:
  43. self.Raise("Pattern '%s' matched no files" % self._pattern)
  44. for fname in files:
  45. if not os.path.isfile(fname):
  46. continue
  47. name = os.path.basename(fname)
  48. subnode = self._node.FindNode(name)
  49. if not subnode:
  50. subnode = state.AddSubnode(self._node, name)
  51. state.AddString(subnode, 'type', 'blob')
  52. state.AddString(subnode, 'filename', fname)
  53. state.AddString(subnode, 'compress', self._files_compress)
  54. if self._files_align:
  55. state.AddInt(subnode, 'align', self._files_align)
  56. # Read entries again, now that we have some
  57. self._ReadEntries()