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    """A set of files arranged in a section
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
38    def ReadNode(self):
39        super().ReadNode()
40        self._pattern = fdt_util.GetString(self._node, 'pattern')
41        if not self._pattern:
42            self.Raise("Missing 'pattern' property")
43        self._files_compress = fdt_util.GetString(self._node, 'files-compress',
44                                                  'none')
45        self._files_align = fdt_util.GetInt(self._node, 'files-align');
46        self._require_matches = fdt_util.GetBool(self._node,
47                                                'require-matches')
48
49    def ExpandEntries(self):
50        files = tools.GetInputFilenameGlob(self._pattern)
51        if self._require_matches and not files:
52            self.Raise("Pattern '%s' matched no files" % self._pattern)
53        for fname in files:
54            if not os.path.isfile(fname):
55                continue
56            name = os.path.basename(fname)
57            subnode = self._node.FindNode(name)
58            if not subnode:
59                subnode = state.AddSubnode(self._node, name)
60            state.AddString(subnode, 'type', 'blob')
61            state.AddString(subnode, 'filename', fname)
62            state.AddString(subnode, 'compress', self._files_compress)
63            if self._files_align:
64                state.AddInt(subnode, 'align', self._files_align)
65
66        # Read entries again, now that we have some
67        self._ReadEntries()
68