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 U-Boot device tree files
6#
7
8from binman.entry import Entry
9from binman.etype.blob import Entry_blob
10
11class Entry_blob_dtb(Entry_blob):
12    """A blob that holds a device tree
13
14    This is a blob containing a device tree. The contents of the blob are
15    obtained from the list of available device-tree files, managed by the
16    'state' module.
17    """
18    def __init__(self, section, etype, node):
19        # Put this here to allow entry-docs and help to work without libfdt
20        global state
21        from binman import state
22
23        super().__init__(section, etype, node)
24
25    def ObtainContents(self):
26        """Get the device-tree from the list held by the 'state' module"""
27        self._filename = self.GetDefaultFilename()
28        self._pathname, _ = state.GetFdtContents(self.GetFdtEtype())
29        return super().ReadBlobContents()
30
31    def ProcessContents(self):
32        """Re-read the DTB contents so that we get any calculated properties"""
33        _, indata = state.GetFdtContents(self.GetFdtEtype())
34        data = self.CompressData(indata)
35        return self.ProcessContentsUpdate(data)
36
37    def GetFdtEtype(self):
38        """Get the entry type of this device tree
39
40        This can be 'u-boot-dtb', 'u-boot-spl-dtb' or 'u-boot-tpl-dtb'
41        Returns:
42            Entry type if any, e.g. 'u-boot-dtb'
43        """
44        return None
45
46    def GetFdts(self):
47        """Get the device trees used by this entry
48
49        Returns:
50            Dict:
51                key: Filename from this entry (without the path)
52                value: Tuple:
53                    Fdt object for this dtb, or None if not available
54                    Filename of file containing this dtb
55        """
56        fname = self.GetDefaultFilename()
57        return {self.GetFdtEtype(): [self, fname]}
58
59    def WriteData(self, data, decomp=True):
60        ok = super().WriteData(data, decomp)
61
62        # Update the state module, since it has the authoritative record of the
63        # device trees used. If we don't do this, then state.GetFdtContents()
64        # will still return the old contents
65        state.UpdateFdtContents(self.GetFdtEtype(), data)
66        return ok
67