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