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