| 1 | #!/usr/bin/python3 |
| 2 | # Copyright (c) 2022, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | ''' |
| 6 | This is a python module for defining and executing SP setup actions, targeting |
| 7 | a system deploying an SPM implementation. |
| 8 | Each action consists of a function, that processes the SP layout json file and |
| 9 | other provided arguments. |
| 10 | At the core of this is the SpSetupActions which provides a means to register |
| 11 | the functions into a table of actions, and execute them all when invoking |
| 12 | SpSetupActions.run_actions. |
| 13 | Registering the function is done by using the decorator '@SpSetupActions.sp_action' |
| 14 | at function definition. |
| 15 | |
| 16 | Functions can be called: |
| 17 | - once only, or per SP defined in the SP layout file; |
| 18 | - following an order, from lowest to highest of their execution order. |
| 19 | More information in the doc comments below. |
| 20 | ''' |
| 21 | import bisectscip-python python python-stdlib 3.11 bisect/__init__: |
| 22 | |
| 23 | DEFAULT_ACTION_ORDERscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/DEFAULT_ACTION_ORDER. = 100 |
| 24 | |
| 25 | class _ConfiguredActionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#: |
| 26 | """ |
| 27 | Wraps action function with its configuration. |
| 28 | """ |
| 29 | def __init__scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(self), actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(action), exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(exec_order)=DEFAULT_ACTION_ORDERscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/DEFAULT_ACTION_ORDER., global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(global_action)=True, log_callsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(log_calls) = False): |
| 30 | selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(self).exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#exec_order. = exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(exec_order) |
| 31 | selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(self).__name__scip-python python python-stdlib 3.11 builtins/__name__# = actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(action).__name__ |
| 32 | def logged_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#logged_action().(actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#logged_action().(action)): |
| 33 | def inner_logged_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp_layout), spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(args) :dictscip-python python python-stdlib 3.11 builtins/dict#): |
| 34 | printscip-python python python-stdlib 3.11 builtins/print().(f"Calling {actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#logged_action().(action).__name__} -> {spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp)}") |
| 35 | return actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#logged_action().(action)(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp_layout), spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(sp), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action().(args)) |
| 36 | return inner_logged_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#inner_logged_action(). |
| 37 | selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(self).actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#action. = logged_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#logged_action().(actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(action)) if log_callsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(log_calls) is True else actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(action) |
| 38 | selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(self).global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#global_action. = global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__init__().(global_action) |
| 39 | |
| 40 | def __lt__scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__lt__().(selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__lt__().(self), otherscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__lt__().(other)): |
| 41 | """ |
| 42 | To allow for ordered inserts in a list of actions. |
| 43 | """ |
| 44 | return selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__lt__().(self).exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#exec_order. < otherscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__lt__().(other).exec_order |
| 45 | |
| 46 | def __call__scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(self), sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(sp_layout), spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(sp), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(args) :dictscip-python python python-stdlib 3.11 builtins/dict#): |
| 47 | """ |
| 48 | Calls action function. |
| 49 | """ |
| 50 | return selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(self).actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#action.(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(sp_layout), spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(sp), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__call__().(args)) |
| 51 | |
| 52 | def __repr__scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__repr__().(selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__repr__().(self)) -> strscip-python python python-stdlib 3.11 builtins/str#: |
| 53 | """ |
| 54 | Pretty format to show debug information about the action. |
| 55 | """ |
| 56 | return f"func: {selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__repr__().(self).__name__scip-python python python-stdlib 3.11 builtins/__name__#}; global:{selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__repr__().(self).global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#global_action.}; exec_order: {selfscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#__repr__().(self).exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#exec_order.}" |
| 57 | |
| 58 | class SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#: |
| 59 | actionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#actions. = [] |
| 60 | |
| 61 | def sp_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(in_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(in_action) = None, global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(global_action) = False, log_callsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(log_calls)=False, exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(exec_order)=DEFAULT_ACTION_ORDERscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/DEFAULT_ACTION_ORDER.): |
| 62 | """ |
| 63 | Function decorator that registers and configures action. |
| 64 | |
| 65 | :param in_action - function to register |
| 66 | :param global_action - make the function global, i.e. make it be |
| 67 | only called once. |
| 68 | :param log_calls - at every call to action, a useful log will be printed. |
| 69 | :param exec_order - action's calling order. |
| 70 | """ |
| 71 | def append_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(action)): |
| 72 | actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(action) = _ConfiguredActionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/_ConfiguredAction#(actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(action), exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(exec_order), global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(global_action), log_callsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(log_calls)) |
| 73 | bisectscip-python python python-stdlib 3.11 bisect/__init__:.insortscip-python python python-stdlib 3.11 bisect/insort.(SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#.actionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#actions., actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(action)) |
| 74 | return actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(action) |
| 75 | if in_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(in_action) is not None: |
| 76 | return append_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action().(in_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(in_action)) |
| 77 | return append_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_action(). |
| 78 | |
| 79 | def run_actionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layout): dictscip-python python python-stdlib 3.11 builtins/dict#, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args): dictscip-python python python-stdlib 3.11 builtins/dict#, verbosescip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(verbose)=False): |
| 80 | """ |
| 81 | Executes all actions in accordance to their registering configuration: |
| 82 | - If set as "global" it will be called once. |
| 83 | - Actions are called respecting the order established by their "exec_order" field. |
| 84 | |
| 85 | :param sp_layout - dictionary containing the SP layout information. |
| 86 | :param args - arguments to be propagated through the call of actions. |
| 87 | :param verbose - prints actions information in order of execution. |
| 88 | """ |
| 89 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args)["called"] = [] # for debug purposes |
| 90 | def append_calledscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(action), spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(sp), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(args) :dictscip-python python python-stdlib 3.11 builtins/dict#): |
| 91 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(args)["called"].append(f"{actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(action).__name__} -> {spscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(sp)}") |
| 92 | return argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(args) |
| 93 | |
| 94 | for action in SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#.actionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#actions.: |
| 95 | if verbosescip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(verbose): |
| 96 | printscip-python python python-stdlib 3.11 builtins/print().(f"Calling {action}") |
| 97 | if action.global_action: |
| 98 | scope = "global" |
| 99 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args) = action(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layout), scope, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args)) |
| 100 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args) = append_calledscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(action, scope, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args)) |
| 101 | else: |
| 102 | # Functions that are not global called for each SP defined in |
| 103 | # the SP layout. |
| 104 | for sp in sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layout).keysscip-python python python-stdlib 3.11 builtins/dict#keys().(): |
| 105 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args) = action(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layout), sp, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args)) |
| 106 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args) = append_calledscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#append_called().(action, sp, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(args)) |
| 107 | |
| 108 | if __name__scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/ == "__main__": |
| 109 | # Executing this module will have the following test code/playground executed |
| 110 | sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/sp_layout. = { |
| 111 | "partition1" : { |
| 112 | "boot-info": True, |
| 113 | "image": { |
| 114 | "file": "partition.bin", |
| 115 | "offset":"0x2000" |
| 116 | }, |
| 117 | "pm": { |
| 118 | "file": "cactus.dts", |
| 119 | "offset":"0x1000" |
| 120 | }, |
| 121 | "owner": "SiP" |
| 122 | }, |
| 123 | "partition2" : { |
| 124 | "image": "partition.bin", |
| 125 | "pm": "cactus-secondary.dts", |
| 126 | "owner": "Plat" |
| 127 | }, |
| 128 | "partition3" : { |
| 129 | "image": "partition.bin", |
| 130 | "pm": "cactus-tertiary.dts", |
| 131 | "owner": "Plat" |
| 132 | }, |
| 133 | "partition4" : { |
| 134 | "image": "ivy.bin", |
| 135 | "pm": "ivy.dts", |
| 136 | "owner": "Plat" |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #Example of how to use this module |
| 141 | @SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#.sp_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(global_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(global_action)=True) |
| 142 | def my_action1scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(sp_layout), _, argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(args) :dictscip-python python python-stdlib 3.11 builtins/dict#): |
| 143 | printscip-python python python-stdlib 3.11 builtins/print().(f"inside function my_action1{sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(sp_layout)}\n\n args:{argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(args)})") |
| 144 | return argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action1().(args) # Always return args in action function. |
| 145 | @SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#.sp_actionscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(exec_orderscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#sp_action().(exec_order)=1) |
| 146 | def my_action2scip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(sp_layout), sp_namescip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(sp_name), argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(args) :dictscip-python python python-stdlib 3.11 builtins/dict#): |
| 147 | printscip-python python python-stdlib 3.11 builtins/print().(f"inside function my_action2; SP: {sp_namescip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(sp_name)} {sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(sp_layout)} args:{argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(args)}") |
| 148 | return argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/my_action2().(args) |
| 149 | |
| 150 | # Example arguments to be propagated through the functions. |
| 151 | # 'args' can be extended in the action functions. |
| 152 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/args. = dictscip-python python python-stdlib 3.11 builtins/dict#() |
| 153 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/args.["arg1"] = 0xEEE |
| 154 | argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/args.["arg2"] = 0xFF |
| 155 | SpSetupActionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#.run_actionsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/SpSetupActions#run_actions().(sp_layoutscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/sp_layout., argsscip-python python trusted-firmware-a 2.12.1 `tools.sptool.spactions`/args.) |