1 /*
2 
3 Copyright (c) 2009-2024 ARM Limited. All rights reserved.
4 
5     SPDX-License-Identifier: Apache-2.0
6 
7 Licensed under the Apache License, Version 2.0 (the License); you may
8 not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 
11     www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an AS IS BASIS, WITHOUT
15 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 NOTICE: This file has been modified by Nordic Semiconductor ASA.
20 
21 */
22 
23 #ifndef SYSTEM_NRF53_APPROTECT_H
24 #define SYSTEM_NRF53_APPROTECT_H
25 
26 #include "nrf.h"
27 #include "nrf53_erratas.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /* Function that handles firmware-driven enabling or disabling of APPROTECT on devices where it is supported.
34         If ENABLE_APPROTECT is defined, the FW will lock the fw branch of the APPROTECT mechanism,
35                             preventing it from being opened.
36         If ENABLE_APPROTECT_USER_HANDLING is defined, the FW will not write to the fw branch of the APPROTECT mechanism.
37                             This allows later stages of the fw to handle APPROTECT,
38                             for example to implement authenticated debug.
39         Otherwise, the fw branch state is loaded from UICR.
40 
41          The same mechanism is implemented for SECURE APPROTECT, with the macros
42          ENABLE_SECURE_APPROTECT and ENABLE_SECURE_APPROTECT_USER_HANDLING. */
43 
nrf53_handle_approtect(void)44 static inline void nrf53_handle_approtect(void)
45 {
46     #if defined(NRF_APPLICATION)
47         #if defined (ENABLE_APPROTECT)
48             /* Prevent processor from unlocking APPROTECT soft branch after this point. */
49             NRF_CTRLAP_S->APPROTECT.LOCK = CTRLAPPERI_APPROTECT_LOCK_LOCK_Locked;
50 
51         #elif  defined (ENABLE_APPROTECT_USER_HANDLING)
52                 /* Do nothing, allow user code to handle APPROTECT. Use this if you want to enable authenticated debug. */
53 
54         #else
55             /* Load APPROTECT soft branch from UICR.
56                If UICR->APPROTECT is disabled, CTRLAP->APPROTECT will be disabled. */
57             NRF_CTRLAP_S->APPROTECT.DISABLE = NRF_UICR_S->APPROTECT;
58         #endif
59 
60         /* Secure APPROTECT is only available for Application core. */
61         #if defined (ENABLE_SECURE_APPROTECT)
62             /* Prevent processor from unlocking SECURE APPROTECT soft branch after this point. */
63             NRF_CTRLAP_S->SECUREAPPROTECT.LOCK = CTRLAPPERI_SECUREAPPROTECT_LOCK_LOCK_Locked;
64 
65         #elif  defined (ENABLE_SECURE_APPROTECT_USER_HANDLING)
66                 /* Do nothing, allow user code to handle SECURE APPROTECT. Use this if you want to enable authenticated debug. */
67 
68         #else
69             /* Load SECURE APPROTECT soft branch from UICR.
70                If UICR->SECUREAPPROTECT is disabled, CTRLAP->SECUREAPPROTECT will be disabled. */
71             NRF_CTRLAP_S->SECUREAPPROTECT.DISABLE = NRF_UICR_S->SECUREAPPROTECT;
72         #endif
73     #endif
74     #if defined(NRF_NETWORK)
75         #if defined (ENABLE_APPROTECT)
76             /* Prevent processor from unlocking APPROTECT soft branch after this point. */
77             NRF_CTRLAP_NS->APPROTECT.LOCK = CTRLAPPERI_APPROTECT_LOCK_LOCK_Locked;
78 
79         #elif  defined (ENABLE_APPROTECT_USER_HANDLING)
80                 /* Do nothing, allow user code to handle APPROTECT. Use this if you want to enable authenticated debug. */
81 
82         #else
83             /* Load APPROTECT soft branch from UICR.
84                If UICR->APPROTECT is disabled, CTRLAP->APPROTECT will be disabled. */
85             NRF_CTRLAP_NS->APPROTECT.DISABLE = NRF_UICR_NS->APPROTECT;
86         #endif
87     #endif
88 }
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #endif /* SYSTEM_NRF5_APPROTECT_H */
95