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