Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 1 | ########################## |
| 2 | Hardware Abstraction Layer |
| 3 | ########################## |
| 4 | |
| 5 | :Organization: Arm Limited |
| 6 | :Contact: tf-m@lists.trustedfirmware.org |
| 7 | |
| 8 | :API Version: 0.9 |
| 9 | |
| 10 | ************ |
| 11 | Introduction |
| 12 | ************ |
| 13 | :term:`TF-M` :term:`HAL` abstracts the hardware-oriented and platform specific |
| 14 | operations on the :term:`SPE` side and provides a set of APIs to the upper |
| 15 | layers such as :term:`SPM`, :term:`RoT Service`. |
| 16 | The :term:`HAL` aims to cover the platform different aspects whereas common |
| 17 | architecturally defined aspects are done generically within the common |
| 18 | :term:`SPE`. |
| 19 | In some cases, although the operations are defined architecturally, |
| 20 | it may not be possible to generalize implementations because lots of information |
| 21 | is only known to platforms. |
| 22 | It is more efficient to define a :term:`HAL` API for those architectural |
| 23 | operations as well. |
| 24 | |
| 25 | .. note:: |
| 26 | :term:`TBSA-M` provides the hardware requirements for security purposes. |
| 27 | :term:`TF-M` :term:`HAL` tries to reference :term:`TBSA-M` recommendations in |
| 28 | the interfaces from the software perspective only. Please reference |
| 29 | :term:`TBSA-M` for your security hardware design. |
| 30 | |
| 31 | Design Goals |
| 32 | ============ |
| 33 | :term:`TF-M` :term:`HAL` is designed to simplify the integration efforts on |
| 34 | different platforms. |
| 35 | |
| 36 | :term:`TF-M` :term:`HAL` is designed to make it easy to use the hardware and |
| 37 | develop the :term:`SPM` and :term:`RoT Service` which need to access the |
| 38 | devices. |
| 39 | |
| 40 | :term:`TF-M` :term:`HAL` is designed to make the structure clearer and let the |
| 41 | :term:`TF-M` mainly focus on :term:`PSA` implementation. |
| 42 | |
| 43 | ******** |
| 44 | Overview |
| 45 | ******** |
| 46 | This section provides an overview of the abstraction layer structure. |
| 47 | |
| 48 | .. figure:: media/hal_structure.png |
| 49 | |
| 50 | Here lists a minimal set of necessary functionalities: |
| 51 | |
| 52 | - **Isolation API**: Provides the necessary isolation functionalities required |
| 53 | by the :term:`PSA-FF-M` and :term:`TBSA-M`, and provides APIs to :term:`SPM` |
| 54 | to check the permissions of memory access. |
| 55 | - **Platform API**: Provides the platform initialization, platform-specific |
| 56 | memory information, system reset, etc. |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 57 | - **Log dev API**: Provides the log system functions. |
| 58 | - **Interrupt API**: Provides the interrupt functions. |
| 59 | |
| 60 | .. note:: |
| 61 | - There is a non-secure :term:`HAL` that focuses on the mailbox operation API |
| 62 | for Dual-core topology. For more information about it, please refer to |
| 63 | :doc:`Mailbox Design in TF-M on Dual-core System |
| 64 | </docs/design_documents/dual-cpu/mailbox_design_on_dual_core_system>`. |
| 65 | - The minimal set of :term:`TF-M` :term:`HAL` is sufficient for Secure |
| 66 | Partitions by using customized peripheral interfaces. To provide easier |
| 67 | portability for the Secure Partitions, a Secure Partition :term:`HAL` is |
| 68 | provided in this design too. |
| 69 | - The debug mechanisms give the external entity the corresponding right to |
| 70 | access the system assets. :term:`TF-M` ensures that the external entity is |
| 71 | permitted access to those assets. Currently, :term:`TF-M` only needs the |
| 72 | debug authentication. The whole debug mechanism and related :term:`HAL` will |
| 73 | be enhanced in the future. Please refer to the :doc:`Debug authentication |
| 74 | settings section </platform/readme>` for more detail. |
| 75 | |
| 76 | ***************** |
| 77 | Design Principles |
| 78 | ***************** |
| 79 | As :term:`TF-M` runs on resource-constrained devices, the :term:`HAL` tries to |
| 80 | avoid multiple level abstractions which cost more resources. |
| 81 | |
| 82 | Part of the :term:`HAL` interfaces does not focus on exact hardware operations |
| 83 | such as power on/off or PIN manipulation. |
| 84 | Instead, the :term:`HAL` abstracts higher-level interfaces to reserve the |
| 85 | implementation flexibility for the platform vendors. |
| 86 | |
| 87 | The :term:`TF-M` :term:`HAL` should be easy to deprecate APIs and provide |
| 88 | compatibilities. |
| 89 | Any API incompatibility should be detected during building. |
| 90 | |
| 91 | :term:`TF-M` relies on the :term:`HAL` APIs to be implemented correctly and |
| 92 | trusts the :term:`HAL` APIs. |
| 93 | :term:`TFM` can provide assertions to detect common programming errors but |
| 94 | essentially no further extensive checks will be provided. |
| 95 | |
| 96 | ************ |
| 97 | Source Files |
| 98 | ************ |
| 99 | This section describes the source file of the :term:`TF-M` :term:`HAL`, |
| 100 | including the header and c files. |
| 101 | |
| 102 | tfm_hal_defs.h |
| 103 | ============== |
| 104 | This header file contains the definitions of common macros and types used by all |
| 105 | :term:`HAL` APIs. Please refer to `Status Codes`_ for detailed definitions. |
| 106 | |
| 107 | tfm_hal_[module].[h/c] |
| 108 | ====================== |
| 109 | All other headers and c files are classified by the modules, such as isolation, |
| 110 | platform, interrupt, devices, etc. |
| 111 | |
| 112 | .. note:: |
| 113 | There are common files in the platform folder include the implemented |
| 114 | :term:`HAL` APIs. The platform vendors can use them directly but need to |
| 115 | implement all the sub APIs. |
| 116 | |
| 117 | ************ |
| 118 | Status Codes |
| 119 | ************ |
| 120 | These are common status and error codes for all :term:`HAL` APIs. |
| 121 | |
| 122 | Types |
| 123 | ===== |
| 124 | tfm_hal_status_t |
| 125 | ---------------- |
| 126 | This is a status code to be used as the return type of :term:`HAL` APIs. |
| 127 | |
| 128 | .. code-block:: c |
| 129 | |
Summer Qin | 2ecfc9f | 2020-08-20 14:20:27 +0800 | [diff] [blame] | 130 | enum tfm_hal_status_t { |
| 131 | TFM_HAL_ERROR_MEM_FAULT = CHAR_MIN, |
| 132 | TFM_HAL_ERROR_MAX_VALUE, |
| 133 | TFM_HAL_ERROR_BAD_STATE, |
| 134 | TFM_HAL_ERROR_NOT_SUPPORTED, |
| 135 | TFM_HAL_ERROR_INVALID_INPUT, |
| 136 | TFM_HAL_ERROR_NOT_INIT, |
| 137 | TFM_HAL_ERROR_GENERIC, |
| 138 | TFM_HAL_SUCCESS = 0 |
| 139 | }; |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 140 | |
| 141 | Error Codes |
| 142 | =========== |
| 143 | Negative values indicate an error. Zero and positive values indicate success. |
| 144 | |
| 145 | Here is the general list. The detailed usages for each error code are described |
| 146 | in the API introduction sections. |
| 147 | |
| 148 | TFM_HAL_SUCCESS |
| 149 | --------------- |
| 150 | Status code to indicate general success. |
| 151 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 152 | TFM_HAL_ERROR_GENERIC |
| 153 | --------------------- |
| 154 | Status code to indicate an error that does not correspond to any defined failure |
| 155 | cause. |
| 156 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 157 | TFM_HAL_ERROR_NOT_INIT |
| 158 | ---------------------- |
| 159 | Status code to indicate that the module is not initialed. |
| 160 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 161 | TFM_HAL_ERROR_INVALID_INPUT |
| 162 | --------------------------- |
| 163 | Status code to indicate that the input is invalid. |
| 164 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 165 | TFM_HAL_ERROR_NOT_SUPPORTED |
| 166 | --------------------------- |
| 167 | Status code to indicate that the requested operation or a parameter is not |
| 168 | supported. |
| 169 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 170 | TFM_HAL_ERROR_BAD_STATE |
| 171 | ----------------------- |
| 172 | Status code to indicate that the requested action cannot be performed in the |
| 173 | current state. |
| 174 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 175 | TFM_HAL_ERROR_MAX_VALUE |
| 176 | ----------------------- |
| 177 | Status code to indicate that the current number has got the max value. |
| 178 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 179 | TFM_HAL_ERROR_MEM_FAULT |
| 180 | ----------------------- |
| 181 | Status code to indicate that the memory check failed. |
| 182 | |
Edison Ai | f4b6113 | 2020-07-01 18:35:17 +0800 | [diff] [blame^] | 183 | *************************** |
| 184 | API Definition for TF-M SPM |
| 185 | *************************** |
| 186 | This section describes the APIs defined for :term:`TF-M` :term:`SPM`. |
| 187 | |
| 188 | Log API |
| 189 | ======= |
| 190 | The log API is used by the :term:`TF-M` :doc:`log system <tfm_log_system_design_document>`. |
| 191 | The log device could be uart, memory, usb, etc. |
| 192 | |
| 193 | APIs |
| 194 | ---- |
| 195 | tfm_hal_output_partition_log() |
| 196 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 197 | **Prototype** |
| 198 | |
| 199 | .. code-block:: c |
| 200 | |
| 201 | int32_t tfm_hal_output_partition_log(const unsigned char *str, uint32_t len) |
| 202 | |
| 203 | **Description** |
| 204 | |
| 205 | This API is called by Secure Partition to output logs. |
| 206 | |
| 207 | **Parameter** |
| 208 | |
| 209 | - ``str`` - The string to output. |
| 210 | - ``len`` - Length of the string in bytes. |
| 211 | |
| 212 | **Return Values** |
| 213 | |
| 214 | - Positive values - Number of bytes output. |
| 215 | - ``TFM_HAL_ERROR_NOT_INIT`` - The log device has not been initialized. |
| 216 | - ``TFM_HAL_ERROR_INVALID_INPUT`` - Invalid inputs when ``str`` is ``NULL`` or |
| 217 | ``len`` is zero. |
| 218 | |
| 219 | **Note** |
| 220 | |
| 221 | None. |
| 222 | |
| 223 | tfm_hal_output_spm_log() |
| 224 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 225 | **Prototype** |
| 226 | |
| 227 | .. code-block:: c |
| 228 | |
| 229 | int32_t tfm_hal_output_spm_log(const unsigned char *str, uint32_t len) |
| 230 | |
| 231 | **Description** |
| 232 | |
| 233 | This API is called by :term:`SPM` to output logs. |
| 234 | |
| 235 | **Parameter** |
| 236 | |
| 237 | - ``str`` - The string to output. |
| 238 | - ``len`` - Length of the string in bytes. |
| 239 | |
| 240 | **Return Values** |
| 241 | |
| 242 | - Positive numbers - Number of bytes output. |
| 243 | - ``TFM_HAL_ERROR_NOT_INIT`` - The log device has not been initialized. |
| 244 | - ``TFM_HAL_ERROR_INVALID_INPUT`` - Invalid inputs when ``str`` is ``NULL`` |
| 245 | or ``len`` is zero. |
| 246 | |
| 247 | **Note** |
| 248 | |
| 249 | Please check :doc:`TF-M log system <tfm_log_system_design_document>` for more |
| 250 | information. |
| 251 | |
Edison Ai | 75afd37 | 2020-04-21 18:29:57 +0800 | [diff] [blame] | 252 | -------------- |
| 253 | |
| 254 | *Copyright (c) 2020, Arm Limited. All rights reserved.* |