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 entry import Entry, EntryArg 11import fdt_util 12import 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: Implement ProcessContents() incorrectly so as to 35 cause a failure 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 Entry.__init__(self, section, etype, node) 45 self.return_invalid_entry = fdt_util.GetBool(self._node, 46 'return-invalid-entry') 47 self.return_unknown_contents = fdt_util.GetBool(self._node, 48 'return-unknown-contents') 49 self.bad_update_contents = fdt_util.GetBool(self._node, 50 'bad-update-contents') 51 self.return_contents_once = fdt_util.GetBool(self._node, 52 'return-contents-once') 53 54 # Set to True when the entry is ready to process the FDT. 55 self.process_fdt_ready = False 56 self.never_complete_process_fdt = fdt_util.GetBool(self._node, 57 'never-complete-process-fdt') 58 self.require_args = fdt_util.GetBool(self._node, 'require-args') 59 60 # This should be picked up by GetEntryArgsOrProps() 61 self.test_existing_prop = 'existing' 62 self.force_bad_datatype = fdt_util.GetBool(self._node, 63 'force-bad-datatype') 64 (self.test_str_fdt, self.test_str_arg, self.test_int_fdt, 65 self.test_int_arg, existing) = self.GetEntryArgsOrProps([ 66 EntryArg('test-str-fdt', str), 67 EntryArg('test-str-arg', str), 68 EntryArg('test-int-fdt', int), 69 EntryArg('test-int-arg', int), 70 EntryArg('test-existing-prop', str)], self.require_args) 71 if self.force_bad_datatype: 72 self.GetEntryArgsOrProps([EntryArg('test-bad-datatype-arg', bool)]) 73 self.return_contents = True 74 75 def ObtainContents(self): 76 if self.return_unknown_contents or not self.return_contents: 77 return False 78 self.data = 'a' 79 self.contents_size = len(self.data) 80 if self.return_contents_once: 81 self.return_contents = False 82 return True 83 84 def GetOffsets(self): 85 if self.return_invalid_entry : 86 return {'invalid-entry': [1, 2]} 87 return {} 88 89 def ProcessContents(self): 90 if self.bad_update_contents: 91 # Request to update the conents with something larger, to cause a 92 # failure. 93 self.ProcessContentsUpdate('aa') 94 95 def ProcessFdt(self, fdt): 96 """Force reprocessing the first time""" 97 ready = self.process_fdt_ready 98 if not self.never_complete_process_fdt: 99 self.process_fdt_ready = True 100 return ready 101