binman: Add support for a collection of entries
[platform/kernel/u-boot.git] / tools / binman / etype / collection.py
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
8 from collections import OrderedDict
9 import os
10
11 from binman.entry import Entry
12 from dtoc import fdt_util
13
14 class 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     def __init__(self, section, etype, node):
26         super().__init__(section, etype, node)
27         self.content = fdt_util.GetPhandleList(self._node, 'content')
28         if not self.content:
29             self.Raise("Collection must have a 'content' property")
30
31     def GetContents(self):
32         """Get the contents of this entry
33
34         Returns:
35             bytes content of the entry
36         """
37         # Join up all the data
38         self.Info('Getting content')
39         data = b''
40         for entry_phandle in self.content:
41             entry_data = self.section.GetContentsByPhandle(entry_phandle, self)
42             if entry_data is None:
43                 # Data not available yet
44                 return None
45             data += entry_data
46
47         self.Info('Returning contents size %x' % len(data))
48
49         return data
50
51     def ObtainContents(self):
52         data = self.GetContents()
53         if data is None:
54             return False
55         self.SetContents(data)
56         return True
57
58     def ProcessContents(self):
59         # The blob may have changed due to WriteSymbols()
60         data = self.GetContents()
61         return self.ProcessContentsUpdate(data)