1# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
5# Entry-type module for testing purposes. Not used in real images.
6#
7
8from collections import OrderedDict
9
10from binman.entry import Entry, EntryArg
11from dtoc import fdt_util
12from patman import tools
13
14
15class Entry__testing(Entry):
16    """A fake entry used for testing
17
18    This entry should not be used in normal images. It is a special entry with
19    strange features used for testing.
20
21    Properties / Entry arguments
22        test-str-fdt: Test string, normally in the node
23        test-int-fdt: Test integer, normally in the node
24        test-str-arg: Test string, normally in the entry arguments
25        test-int-arg: Test integer, normally in the entry arguments
26
27    The entry has a single 'a' byte as its contents. Operation is controlled by
28    a number of properties in the node, as follows:
29
30    Properties:
31        return-invalid-entry: Return an invalid entry from GetOffsets()
32        return-unknown-contents: Refuse to provide any contents (to cause a
33            failure)
34        bad-update-contents: Return a larger size in ProcessContents
35        bad-shrink-contents: Return a larger size in ProcessContents
36        never-complete-process-fdt: Refund to process the FDT (to cause a
37            failure)
38        require-args: Require that all used args are present (generating an
39            error if not)
40        force-bad-datatype: Force a call to GetEntryArgsOrProps() with a bad
41            data type (generating an error)
42    """
43    def __init__(self, section, etype, node):
44        super().__init__(section, etype, node)
45
46    def ReadNode(self):
47        super().ReadNode()
48        self.return_invalid_entry = fdt_util.GetBool(self._node,
49                                                     'return-invalid-entry')
50        self.return_unknown_contents = fdt_util.GetBool(self._node,
51                                                     'return-unknown-contents')
52        self.bad_update_contents = fdt_util.GetBool(self._node,
53                                                    'bad-update-contents')
54        self.bad_shrink_contents = fdt_util.GetBool(self._node,
55                                                    'bad-shrink-contents')
56        self.return_contents_once = fdt_util.GetBool(self._node,
57                                                     'return-contents-once')
58        self.bad_update_contents_twice = fdt_util.GetBool(self._node,
59                                                    'bad-update-contents-twice')
60        self.return_contents_later = fdt_util.GetBool(self._node,
61                                                     'return-contents-later')
62
63        # Set to True when the entry is ready to process the FDT.
64        self.process_fdt_ready = False
65        self.never_complete_process_fdt = fdt_util.GetBool(self._node,
66                                                'never-complete-process-fdt')
67        self.require_args = fdt_util.GetBool(self._node, 'require-args')
68
69        # This should be picked up by GetEntryArgsOrProps()
70        self.test_existing_prop = 'existing'
71        self.force_bad_datatype = fdt_util.GetBool(self._node,
72                                                   'force-bad-datatype')
73        (self.test_str_fdt, self.test_str_arg, self.test_int_fdt,
74         self.test_int_arg, existing) = self.GetEntryArgsOrProps([
75            EntryArg('test-str-fdt', str),
76            EntryArg('test-str-arg', str),
77            EntryArg('test-int-fdt', int),
78            EntryArg('test-int-arg', int),
79            EntryArg('test-existing-prop', str)], self.require_args)
80        if self.force_bad_datatype:
81            self.GetEntryArgsOrProps([EntryArg('test-bad-datatype-arg', bool)])
82        self.return_contents = True
83        self.contents = b'aa'
84
85    def ObtainContents(self):
86        if self.return_unknown_contents or not self.return_contents:
87            return False
88        if self.return_contents_later:
89            self.return_contents_later = False
90            return False
91        self.data = self.contents
92        self.contents_size = len(self.data)
93        if self.return_contents_once:
94            self.return_contents = False
95        return True
96
97    def GetOffsets(self):
98        if self.return_invalid_entry :
99            return {'invalid-entry': [1, 2]}
100        return {}
101
102    def ProcessContents(self):
103        data = self.contents
104        if self.bad_update_contents:
105            # Request to update the contents with something larger, to cause a
106            # failure.
107            if self.bad_update_contents_twice:
108                data = self.data + b'a'
109            else:
110                data = b'aaa'
111            return self.ProcessContentsUpdate(data)
112        if self.bad_shrink_contents:
113            # Request to update the contents with something smaller, to cause a
114            # failure.
115            data = b'a'
116            return self.ProcessContentsUpdate(data)
117        if self.bad_shrink_contents:
118            # Request to update the contents with something smaller, to cause a
119            # failure.
120            data = b'a'
121            return self.ProcessContentsUpdate(data)
122        return True
123
124    def ProcessFdt(self, fdt):
125        """Force reprocessing the first time"""
126        ready = self.process_fdt_ready
127        if not self.never_complete_process_fdt:
128            self.process_fdt_ready = True
129        return ready
130