-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32_boot_xip.c
183 lines (154 loc) · 4.85 KB
/
stm32_boot_xip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
******************************************************************************
* @file stm32_boot_xip.c
* @author MCD Application Team
* @brief this file manages the boot in the mode execute in place.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32_boot_xip.h"
#include "stm32_extmem_conf.h"
/** @addtogroup BOOT
* @{
*/
/** @addtogroup BOOT_XIP
* @{
*/
/* Private typedefs ----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
/* offset of the image from the boot memory base */
#ifndef EXTMEM_XIP_IMAGE_OFFSET
#define EXTMEM_XIP_IMAGE_OFFSET 0
#endif
/* offset of the vector table from the start of the image */
#ifndef EXTMEM_HEADER_OFFSET
#define EXTMEM_HEADER_OFFSET 0
#endif
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
BOOTStatus_TypeDef JumpToApplication(void);
BOOTStatus_TypeDef MapMemory(void);
BOOTStatus_TypeDef GetBaseAddress(uint32_t MemIndex, uint32_t *BaseAddress);
/**
* @addtogroup BOOT_XIP_Exported_Functions Boot XIP exported functions
* @{
*/
BOOTStatus_TypeDef BOOT_Application(void)
{
BOOTStatus_TypeDef retr;
/* mount the memory */
retr = MapMemory();
if (BOOT_OK == retr)
{
/* jump on the application */
retr = JumpToApplication();
}
return retr;
}
/**
* @}
*/
/**
* @defgroup BOOT_XIP_Private_Functions Boot XIP private functions
* @{
*/
/**
* @brief this function maps the memory
* @return @ref BOOTStatus_TypeDef
*/
BOOTStatus_TypeDef MapMemory(void)
{
BOOTStatus_TypeDef retr = BOOT_OK;
/* Map all the memory */
for (uint8_t index = 0; index < (sizeof(extmem_list_config) / sizeof(EXTMEM_DefinitionTypeDef)); index++)
{
switch(EXTMEM_MemoryMappedMode(index, EXTMEM_ENABLE))
{
case EXTMEM_ERROR_NOTSUPPORTED :
if (EXTMEM_MEMORY_BOOTXIP == index)
{
retr = BOOT_ERROR_INCOMPATIBLEMEMORY;
}
else
{
/* We considers the memory will be not used any more */
EXTMEM_DeInit(index);
}
case EXTMEM_OK:
break;
default :
retr = BOOT_ERROR_MAPPEDMODEFAIL;
break;
}
}
return retr;
}
/**
* @brief This function jumps to the application through its vector table
* @return @ref BOOTStatus_TypeDef
*/
BOOTStatus_TypeDef JumpToApplication(void)
{
uint32_t primask_bit;
typedef void (*pFunction)(void);
static pFunction JumpToApp;
uint32_t Application_vector;
if (EXTMEM_OK != EXTMEM_GetMapAddress(EXTMEM_MEMORY_BOOTXIP, &Application_vector))
{
return BOOT_ERROR_INCOMPATIBLEMEMORY;
}
/* Suspend SysTick */
HAL_SuspendTick();
#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
/* if I-Cache is enabled, disable I-Cache-----------------------------------*/
if (SCB->CCR & SCB_CCR_IC_Msk)
{
SCB_DisableICache();
}
#endif /* defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U) */
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
/* if D-Cache is enabled, disable D-Cache-----------------------------------*/
if (SCB->CCR & SCB_CCR_DC_Msk)
{
SCB_DisableDCache();
}
#endif /* defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U) */
/* Initialize user application's Stack Pointer & Jump to user application */
primask_bit = __get_PRIMASK();
__disable_irq();
/* Apply offsets for image location and vector table offset */
Application_vector += EXTMEM_XIP_IMAGE_OFFSET + EXTMEM_HEADER_OFFSET;
SCB->VTOR = (uint32_t)Application_vector;
JumpToApp = (pFunction) (*(__IO uint32_t *)(Application_vector + 4u));
#if ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
(defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) || \
(defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) )
/* on ARM v8m, set MSPLIM before setting MSP to avoid unwanted stack overflow faults */
__set_MSPLIM(0x00000000);
#endif /* __ARM_ARCH_8M_MAIN__ or __ARM_ARCH_8M_BASE__ */
__set_MSP(*(__IO uint32_t*) Application_vector);
/* Re-enable the interrupts */
__set_PRIMASK(primask_bit);
JumpToApp();
return BOOT_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/