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        fname = self.GetDefaultFilename()
48        return {self.GetFdtEtype(): [self, fname]}
49
50    def WriteData(self, data, decomp=True):
51        ok = super().WriteData(data, decomp)
52
53        # Update the state module, since it has the authoritative record of the
54        # device trees used. If we don't do this, then state.GetFdtContents()
55        # will still return the old contents
56        state.UpdateFdtContents(self.GetFdtEtype(), data)
57        return ok
58