1 /*
2  * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __BF0_HAL_PATCH_H
8 #define __BF0_HAL_PATCH_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /* Includes ------------------------------------------------------------------*/
15 #include "bf0_hal_def.h"
16 
17 /** @defgroup PATCH ROM Patch
18   * @ingroup BF0_HAL_Driver
19   * @brief Handle patch for ROM
20   * @{
21   */
22 
23 
24 #ifdef SOC_SF32LB55X
25 #define PATCH_AON           4
26 #else
27 #define PATCH_AON           0
28 #endif
29 #define MAX_PATCH_ENTRIES   32
30 
31 struct patch_entry_desc
32 {
33     uint32_t break_addr;     // break out address, should be 4 bytes-aligned (requreied by hardware)
34     uint32_t data;              // data replaced.
35 };
36 
37 #define QUOTE_ME(X) #X
38 #ifndef PATCH_GENERATE
39 #define PATCH(address,offset,func)
40 #define PATCH_DATA(address,offset,data)
41 #endif
42 
43 #define PATCH_TAG 0x50544348
44 
45 /**
46   * @brief  Advanced API to install patches for LCPU/BCPU
47   * @param  patch_entries Patch descriptor arrays
48   * @param  size size of patch_entries in bytes.
49   * @param  cer Previous patch enable register value, used in restore patch from standby.
50   * @retval 32-bit value, each bit represent on patch installed.
51 */
52 int HAL_PATCH_install2(struct patch_entry_desc *patch_entries, uint32_t size, int cer);
53 
54 
55 /**
56   * @brief  Install patches LCPU/BCPU
57   * @retval 32-bit value, each bit represent on patch installed.
58 */
59 int HAL_PATCH_install(void);
60 
61 /**
62   * @brief  Save patches
63   * @param  patch_entries Patch descriptor arrays
64   * @param  size size of patch_entries in bytes.
65   * @param[out]  cer Previous patch enable register value, used in restore patch from standby.
66   * @retval Save patch numbers.
67 */
68 int HAL_PATCH_save(struct patch_entry_desc *patch_entries, uint32_t size, uint32_t *cer);
69 
70 /**
71   * @brief  Patch image entry, used to install hooks
72 */
73 void HAL_PATCH_Entry(void);
74 
75 
76 #if 0
77 __asm("pro_"QUOTE_ME(func)":\n" \
78       "    ADD SP, #4\n" \
79       "    b "QUOTE_ME(func)" \n")
80 #endif
81 
82 /// @} PATCH
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif